From 77c08fafd2440699390a0691601954f8048be96c Mon Sep 17 00:00:00 2001 From: whowe Date: Thu, 30 Nov 2023 23:25:27 +0800 Subject: [PATCH] Copy debug command code to pn5180.c --- firmware/src/pn5180.c | 130 ++++++++++++++++++++++++++++++++++++++++++ firmware/src/pn5180.h | 4 ++ firmware/src/pn532.c | 2 +- 3 files changed, 135 insertions(+), 1 deletion(-) diff --git a/firmware/src/pn5180.c b/firmware/src/pn5180.c index b0cf8df..61f1f25 100644 --- a/firmware/src/pn5180.c +++ b/firmware/src/pn5180.c @@ -212,6 +212,136 @@ uint32_t pn5180_get_rx() return pn5180_read_reg(PN5180_REG_RX_STATUS); } +static void tx_config(uint32_t cfg) +{ + pn5180_write_reg(PN5180_REG_CRC_TX_CONFIG, cfg); + pn5180_write_reg(PN5180_REG_CRC_RX_CONFIG, cfg); +} + +static void anti_collision(uint8_t cmd, uint8_t uid[6]) +{ + tx_config(0xfffffffe); + uint8_t buf[7] = { cmd, 0x20 }; + pn5180_send_data(buf, 2, 0); + pn5180_read_data(buf + 2, 5); // uid + memmove(uid, buf + 2, 5); + + tx_config(0x01); + buf[1] = 0x70; + pn5180_send_data(buf, 7, 0); + pn5180_read_data(uid + 5, 1); // sak +} + +bool pn5180_poll_mifare(uint8_t *uid, int *len) +{ + pn5180_reset(); + pn5180_load_rf_config(0x00, 0x80); + pn5180_rf_on(); + + + pn5180_and_reg(PN5180_REG_IRQ_CLEAR, 0x000fffff); + pn5180_and_reg(PN5180_REG_SYSTEM_CONFIG, 0xfffffff8); + pn5180_or_reg(PN5180_REG_SYSTEM_CONFIG, 0x03); + + uint8_t cmd[1] = {0x26}; + pn5180_send_data(cmd, 1, 7); + uint8_t buf[32] = {0}; + pn5180_read_data(buf, 2); + + anti_collision(0x93, buf + 2); + + bool result = false; + if ((buf[2] & 0x04) == 0) { + if (*len >= 4) { + *len = 4; + memmove(len, buf + 2, 4); + result = true; + } + } else if (buf[2] == 0x88) { + anti_collision(0x95, buf + 5); + memmove(uid, buf + 3, 7); + if (*len >= 7) { + *len = 7; + result = true; + } + } + + pn5180_rf_off(); + + 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) +{ + pn5180_reset(); + pn5180_load_rf_config(0x08, 0x88); + pn5180_rf_on(); + + pn5180_and_reg(PN5180_REG_SYSTEM_CONFIG, 0xffffffbf); + pn5180_or_reg(PN5180_REG_SYSTEM_CONFIG, 0x03); + + uint8_t cmd[] = {0x06, 0x00, 0xff, 0xff, 0x01, 0x00}; + + pn5180_send_data(cmd, 6, 0x00); + + sleep_ms(1); + + uint8_t out[20] = {0}; + pn5180_read_data(out, 20); + + bool result = false; + if (out[1] == 0x01) { + result = true; + memmove(uid, out + 2, 8); + memmove(pmm, out + 10, 8); + memmove(syscode, out + 18, 2); + result = true; + } + + pn5180_rf_off(); + + return result; +} + + void pn5180_print_rf_cfg() { printf("RF_CONTROL_TX_CLK: %08lx\n", pn5180_read_reg(0x21)); diff --git a/firmware/src/pn5180.h b/firmware/src/pn5180.h index 1831fde..7e997ca 100644 --- a/firmware/src/pn5180.h +++ b/firmware/src/pn5180.h @@ -43,6 +43,10 @@ uint32_t pn5180_get_rx(); void pn5180_reset(); +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); + void pn5180_print_rf_cfg(); #endif diff --git a/firmware/src/pn532.c b/firmware/src/pn532.c index 8f08adc..69881c2 100644 --- a/firmware/src/pn532.c +++ b/firmware/src/pn532.c @@ -125,7 +125,7 @@ static bool read_ack() int pn532_write_data(const uint8_t *data, uint8_t len) { - uint8_t frame[5 + len]; + uint8_t frame[7 + len]; frame[0] = PN532_PREAMBLE; frame[1] = PN532_STARTCODE1; frame[2] = PN532_STARTCODE2;