1
0
mirror of https://github.com/whowechina/aic_pico.git synced 2025-01-31 12:13:47 +01:00

pn5180 card polling code working

This commit is contained in:
whowe 2023-12-02 01:01:30 +08:00
parent 1ea439d5ff
commit 895fcf9501
3 changed files with 124 additions and 69 deletions

View File

@ -116,14 +116,27 @@ static void core1_loop()
} }
} }
static enum {
NFC_UNKNOWN,
NFC_PN532,
NFC_PN5180
} nfc_module = NFC_PN5180;
void detect_card() void detect_card()
{ {
pn532_config_sam(); if (nfc_module == NFC_PN532) {
pn532_config_sam();
}
uint8_t id[20] = { 0 }; uint8_t id[20] = { 0 };
int len = sizeof(id); int len = sizeof(id);
bool mifare = pn532_poll_mifare(id, &len); bool mifare = false;
if (nfc_module == NFC_PN532) {
mifare = pn532_poll_mifare(id, &len);
} else if (nfc_module == NFC_PN5180) {
mifare = pn5180_poll_mifare(id, &len);
}
if (mifare) { if (mifare) {
hid_cardio.current[0] = REPORT_ID_EAMU; hid_cardio.current[0] = REPORT_ID_EAMU;
hid_cardio.current[1] = 0xe0; hid_cardio.current[1] = 0xe0;
@ -137,13 +150,29 @@ void detect_card()
return; return;
} }
bool felica = pn532_poll_felica(id, id + 8, id + 16, false); bool felica = false;
if (nfc_module == NFC_PN532) {
felica = pn532_poll_felica(id, id + 8, id + 16, false);
} else if (nfc_module == NFC_PN5180) {
felica = pn5180_poll_felica(id, id + 8, id + 16, false);
}
if (felica) { if (felica) {
hid_cardio.current[0] = REPORT_ID_FELICA; hid_cardio.current[0] = REPORT_ID_FELICA;
memcpy(hid_cardio.current + 1, id, 8); memcpy(hid_cardio.current + 1, id, 8);
return; return;
} }
bool vicinity = false;
if (nfc_module == NFC_PN5180) {
vicinity = pn5180_poll_vicinity(id, &len);
}
if (vicinity) {
hid_cardio.current[0] = REPORT_ID_EAMU;
memcpy(hid_cardio.current + 1, id, 8);
return;
}
memset(hid_cardio.current, 0, 9); memset(hid_cardio.current, 0, 9);
} }
@ -215,6 +244,7 @@ void init()
pn532_set_wait_loop(wait_loop); pn532_set_wait_loop(wait_loop);
pn5180_init(spi0, 16, 18, 19, 27, 17, 26); pn5180_init(spi0, 16, 18, 19, 27, 17, 26);
pn5180_set_wait_loop(wait_loop);
aime_init(cdc_aime_putc); aime_init(cdc_aime_putc);

View File

@ -64,17 +64,28 @@ void pn5180_init(spi_inst_t *port, uint8_t rx, uint8_t sck, uint8_t tx,
spi.busy = busy; spi.busy = busy;
} }
static pn5180_wait_loop_t wait_loop = NULL;
static inline void wait_not_busy() static inline void wait_not_busy()
{ {
int count = 0;
while (gpio_get(spi.busy)) { while (gpio_get(spi.busy)) {
sleep_us(10); sleep_us(10);
count += 10;
if ((count > 1000) && wait_loop) {
wait_loop();
count = 0;
}
} }
} }
static inline void wait_until_busy() static void sleep_ms_with_loop(uint32_t ms)
{ {
while (!gpio_get(spi.busy)) { for (uint32_t i = 0; i < ms; i++) {
sleep_us(10); sleep_ms(1);
if (wait_loop) {
wait_loop();
}
} }
} }
@ -82,17 +93,15 @@ static inline void begin_transmission()
{ {
wait_not_busy(); wait_not_busy();
gpio_put(spi.nss, 0); gpio_put(spi.nss, 0);
sleep_ms(10); sleep_ms_with_loop(2);
} }
static inline void end_transmission() static inline void end_transmission()
{ {
gpio_put(spi.nss, 1); gpio_put(spi.nss, 1);
sleep_ms(10); sleep_ms_with_loop(3);
} }
static pn5180_wait_loop_t wait_loop = NULL;
void pn5180_set_wait_loop(pn5180_wait_loop_t loop) void pn5180_set_wait_loop(pn5180_wait_loop_t loop)
{ {
wait_loop = loop; wait_loop = loop;
@ -191,6 +200,9 @@ void pn5180_reset()
gpio_put(spi.rst, 1); gpio_put(spi.rst, 1);
sleep_ms(1); sleep_ms(1);
while ((pn5180_get_irq() & (1 << 2)) == 0) { while ((pn5180_get_irq() & (1 << 2)) == 0) {
if (wait_loop) {
wait_loop();
}
sleep_ms(1); sleep_ms(1);
} }
@ -212,24 +224,31 @@ uint32_t pn5180_get_rx()
return pn5180_read_reg(PN5180_REG_RX_STATUS); return pn5180_read_reg(PN5180_REG_RX_STATUS);
} }
static void tx_config(uint32_t cfg) static void rf_crc_off()
{ {
pn5180_write_reg(PN5180_REG_CRC_TX_CONFIG, cfg); pn5180_and_reg(PN5180_REG_CRC_TX_CONFIG, 0xfffffffe);
pn5180_write_reg(PN5180_REG_CRC_RX_CONFIG, cfg); pn5180_and_reg(PN5180_REG_CRC_RX_CONFIG, 0xfffffffe);
} }
static void anti_collision(uint8_t cmd, uint8_t uid[6]) static void rf_crc_on()
{ {
tx_config(0xfffffffe); pn5180_or_reg(PN5180_REG_CRC_TX_CONFIG, 0x01);
uint8_t buf[7] = { cmd, 0x20 }; pn5180_or_reg(PN5180_REG_CRC_RX_CONFIG, 0x01);
pn5180_send_data(buf, 2, 0); }
pn5180_read_data(buf + 2, 5); // uid
memmove(uid, buf + 2, 5);
tx_config(0x01); static void anti_collision(uint8_t code, uint8_t uid[5], uint8_t *sak)
buf[1] = 0x70; {
pn5180_send_data(buf, 7, 0); rf_crc_off();
pn5180_read_data(uid + 5, 1); // sak uint8_t cmd[7] = { code, 0x20 };
pn5180_send_data(cmd, 2, 0);
pn5180_read_data(cmd + 2, 5); // uid
memmove(uid, cmd + 2, 5);
rf_crc_on();
cmd[0] = code;
cmd[1] = 0x70;
pn5180_send_data(cmd, 7, 0);
pn5180_read_data(sak, 1); // sak
} }
bool pn5180_poll_mifare(uint8_t *uid, int *len) bool pn5180_poll_mifare(uint8_t *uid, int *len)
@ -238,6 +257,7 @@ bool pn5180_poll_mifare(uint8_t *uid, int *len)
pn5180_load_rf_config(0x00, 0x80); pn5180_load_rf_config(0x00, 0x80);
pn5180_rf_on(); pn5180_rf_on();
rf_crc_off();
pn5180_and_reg(PN5180_REG_IRQ_CLEAR, 0x000fffff); pn5180_and_reg(PN5180_REG_IRQ_CLEAR, 0x000fffff);
pn5180_and_reg(PN5180_REG_SYSTEM_CONFIG, 0xfffffff8); pn5180_and_reg(PN5180_REG_SYSTEM_CONFIG, 0xfffffff8);
@ -245,21 +265,24 @@ bool pn5180_poll_mifare(uint8_t *uid, int *len)
uint8_t cmd[1] = {0x26}; uint8_t cmd[1] = {0x26};
pn5180_send_data(cmd, 1, 7); pn5180_send_data(cmd, 1, 7);
uint8_t buf[32] = {0}; uint8_t buf[5] = {0};
pn5180_read_data(buf, 2); pn5180_read_data(buf, 2);
anti_collision(0x93, buf + 2); uint8_t sak;
anti_collision(0x93, buf, &sak);
bool result = false; bool result = false;
if ((buf[2] & 0x04) == 0) { if ((sak & 0x04) == 0) {
if (*len >= 4) { if (*len >= 4) {
*len = 4; *len = 4;
memmove(len, buf + 2, 4); memmove(uid, buf, 4);
result = true; result = true;
} }
} else if (buf[2] == 0x88) { } else if (sak == 0x88) {
anti_collision(0x95, buf + 5); memmove(uid, buf + 1, 3);
memmove(uid, buf + 3, 7); anti_collision(0x95, buf + 5, &sak);
memmove(uid + 3, buf, 4);
if (*len >= 7) { if (*len >= 7) {
*len = 7; *len = 7;
result = true; result = true;
@ -271,44 +294,6 @@ bool pn5180_poll_mifare(uint8_t *uid, int *len)
return result; return result;
} }
bool pn5180_poll_14443(uint8_t *uid, int *len)
{
pn5180_reset();
pn5180_load_rf_config(0x0d, 0x8d);
pn5180_rf_on();
pn5180_clear_irq(0x0fffff);
pn5180_and_reg(PN5180_REG_SYSTEM_CONFIG, 0xfffffff8);
pn5180_or_reg(PN5180_REG_SYSTEM_CONFIG, 0x03);
uint8_t cmd[] = {0x26, 0x01, 0x00};
pn5180_send_data(cmd, 3, 0);
sleep_ms(1);
if ((pn5180_get_irq() & 0x4000) == 0) {
pn5180_rf_off();
return false;
}
while ((pn5180_get_irq() & 0x01) == 0) {
sleep_ms(1);
}
int idlen = pn5180_get_rx() & 0x1ff;
bool result = false;
if (idlen <= *len) {
*len = idlen;
pn5180_read_data(uid, idlen);
result = true;
}
pn5180_rf_off();
return result;
}
bool pn5180_poll_felica(uint8_t uid[8], uint8_t pmm[8], uint8_t syscode[2], bool from_cache) bool pn5180_poll_felica(uint8_t uid[8], uint8_t pmm[8], uint8_t syscode[2], bool from_cache)
{ {
pn5180_reset(); pn5180_reset();
@ -341,6 +326,46 @@ bool pn5180_poll_felica(uint8_t uid[8], uint8_t pmm[8], uint8_t syscode[2], bool
return result; return result;
} }
bool pn5180_poll_vicinity(uint8_t *uid, int *len)
{
pn5180_reset();
pn5180_load_rf_config(0x0d, 0x8d);
pn5180_rf_on();
pn5180_clear_irq(0x0fffff);
pn5180_and_reg(PN5180_REG_SYSTEM_CONFIG, 0xfffffff8);
pn5180_or_reg(PN5180_REG_SYSTEM_CONFIG, 0x03);
uint8_t cmd[] = {0x26, 0x01, 0x00};
pn5180_send_data(cmd, 3, 0);
sleep_ms(1);
if ((pn5180_get_irq() & 0x4000) == 0) {
pn5180_rf_off();
return false;
}
while ((pn5180_get_irq() & 0x01) == 0) {
if (wait_loop) {
wait_loop();
}
sleep_ms(1);
}
int idlen = pn5180_get_rx() & 0x1ff;
bool result = false;
if (idlen <= *len) {
*len = idlen;
pn5180_read_data(uid, idlen);
result = true;
}
pn5180_rf_off();
return result;
}
void pn5180_print_rf_cfg() void pn5180_print_rf_cfg()
{ {

View File

@ -44,8 +44,8 @@ uint32_t pn5180_get_rx();
void pn5180_reset(); void pn5180_reset();
bool pn5180_poll_mifare(uint8_t *uid, int *len); bool pn5180_poll_mifare(uint8_t *uid, int *len);
bool pn5180_poll_14443(uint8_t *uid, int *len);
bool pn5180_poll_felica(uint8_t uid[8], uint8_t pmm[8], uint8_t syscode[2], bool from_cache); bool pn5180_poll_felica(uint8_t uid[8], uint8_t pmm[8], uint8_t syscode[2], bool from_cache);
bool pn5180_poll_vicinity(uint8_t *uid, int *len);
void pn5180_print_rf_cfg(); void pn5180_print_rf_cfg();