1
0
mirror of https://github.com/whowechina/aic_pico.git synced 2024-11-13 17:30:52 +01:00

PN5180 supports Mifare auth and block read

This commit is contained in:
whowechina 2024-03-16 21:38:09 +08:00
parent 6fe80851d3
commit 75dd5c6af5
8 changed files with 60 additions and 38 deletions

Binary file not shown.

View File

@ -40,6 +40,7 @@ static void handle_display()
aic_cfg->light.rgb ? "ON" : "OFF", aic_cfg->light.rgb ? "ON" : "OFF",
aic_cfg->light.led ? "ON" : "OFF"); aic_cfg->light.led ? "ON" : "OFF");
printf(" Level: [%d ~ %d]\n", aic_cfg->light.min, aic_cfg->light.max); printf(" Level: [%d ~ %d]\n", aic_cfg->light.min, aic_cfg->light.max);
printf("[AIME]\n");
printf(" Virtual AIC: %s\n", aic_cfg->virtual_aic ? "ON" : "OFF"); printf(" Virtual AIC: %s\n", aic_cfg->virtual_aic ? "ON" : "OFF");
} }
@ -57,7 +58,11 @@ static void handle_factory_reset()
static void handle_nfc() static void handle_nfc()
{ {
printf("NFC module: %s\n", nfc_module_name()); printf("NFC module: %s\n", nfc_module_name());
nfc_rf_field(true);
nfc_card_t card = nfc_detect_card(); nfc_card_t card = nfc_detect_card();
nfc_rf_field(false);
printf("Card: %s", nfc_card_name(card.card_type)); printf("Card: %s", nfc_card_name(card.card_type));
for (int i = 0; i < card.len; i++) { for (int i = 0; i < card.len; i++) {
printf(" %02x", card.uid[i]); printf(" %02x", card.uid[i]);

View File

@ -53,7 +53,7 @@ struct {
bool (*poll_felica)(uint8_t uid[8], uint8_t pmm[8], uint8_t syscode[2], bool from_cache); bool (*poll_felica)(uint8_t uid[8], uint8_t pmm[8], uint8_t syscode[2], bool from_cache);
bool (*poll_vicinity)(uint8_t uid[8]); bool (*poll_vicinity)(uint8_t uid[8]);
void (*rf_field)(bool on); void (*rf_field)(bool on);
bool (*mifare_auth)(const uint8_t uid[4], uint8_t block_id, uint8_t key_id, const uint8_t *key); bool (*mifare_auth)(const uint8_t uid[4], uint8_t block_id, uint8_t key_id, const uint8_t key[6]);
bool (*mifare_read)(uint8_t block_id, uint8_t block_data[16]); bool (*mifare_read)(uint8_t block_id, uint8_t block_data[16]);
void (*set_wait_loop)(nfc_wait_loop_t loop); void (*set_wait_loop)(nfc_wait_loop_t loop);
} api[3] = { } api[3] = {
@ -65,8 +65,8 @@ struct {
}, },
{ {
pn5180_poll_mifare, pn5180_poll_felica, pn5180_poll_vicinity, pn5180_poll_mifare, pn5180_poll_felica, pn5180_poll_vicinity,
func_null, pn5180_rf_field,
func_null, func_null, pn5180_mifare_auth, pn5180_mifare_read,
pn5180_set_wait_loop pn5180_set_wait_loop
}, },
{ 0 }, { 0 },

View File

@ -26,9 +26,11 @@
#define CMD_READ_EEPROM 0x07 #define CMD_READ_EEPROM 0x07
#define CMD_SEND_DATA 0x09 #define CMD_SEND_DATA 0x09
#define CMD_READ_DATA 0x0a #define CMD_READ_DATA 0x0a
#define CMD_MIFARE_AUTHENTICATE 0x0c
#define CMD_LOAD_RF_CONFIG 0x11 #define CMD_LOAD_RF_CONFIG 0x11
#define CMD_RF_ON 0x16 #define CMD_RF_ON 0x16
#define CMD_RF_OFF 0x17 #define CMD_RF_OFF 0x17
#define CMD_MIFARE_READ 0x30
static spi_inst_t *spi_port; static spi_inst_t *spi_port;
static uint8_t gpio_rst; static uint8_t gpio_rst;
@ -174,16 +176,10 @@ void pn5180_load_rf_config(uint8_t tx_cfg, uint8_t rx_cfg)
read_write(buf, sizeof(buf), NULL, 0); read_write(buf, sizeof(buf), NULL, 0);
} }
void pn5180_rf_on() void pn5180_rf_field(bool on)
{ {
uint8_t buf[] = { CMD_RF_ON, 0 }; uint8_t buf[] = { on ? CMD_RF_ON : CMD_RF_OFF, 0 };
read_write(buf, sizeof(buf), NULL, 0); read_write(buf, sizeof(buf), NULL, 0);
}
void pn5180_rf_off()
{
uint8_t buf[] = { CMD_RF_OFF, 0 };
read_write(buf, sizeof(buf), NULL, 0);
} }
void pn5180_reset() void pn5180_reset()
@ -248,7 +244,7 @@ bool pn5180_poll_mifare(uint8_t uid[7], int *len)
{ {
pn5180_reset(); pn5180_reset();
pn5180_load_rf_config(0x00, 0x80); pn5180_load_rf_config(0x00, 0x80);
pn5180_rf_on(); pn5180_rf_field(true);
rf_crc_off(); rf_crc_off();
@ -280,8 +276,6 @@ bool pn5180_poll_mifare(uint8_t uid[7], int *len)
} }
} }
pn5180_rf_off();
return result; return result;
} }
@ -289,7 +283,7 @@ bool pn5180_poll_felica(uint8_t uid[8], uint8_t pmm[8], uint8_t syscode[2], bool
{ {
pn5180_reset(); pn5180_reset();
pn5180_load_rf_config(0x08, 0x88); pn5180_load_rf_config(0x08, 0x88);
pn5180_rf_on(); pn5180_rf_field(true);
pn5180_and_reg(PN5180_REG_SYSTEM_CONFIG, 0xffffffbf); pn5180_and_reg(PN5180_REG_SYSTEM_CONFIG, 0xffffffbf);
pn5180_or_reg(PN5180_REG_SYSTEM_CONFIG, 0x03); pn5180_or_reg(PN5180_REG_SYSTEM_CONFIG, 0x03);
@ -312,8 +306,6 @@ bool pn5180_poll_felica(uint8_t uid[8], uint8_t pmm[8], uint8_t syscode[2], bool
result = true; result = true;
} }
pn5180_rf_off();
return result; return result;
} }
@ -321,7 +313,7 @@ bool pn5180_poll_vicinity(uint8_t uid[8])
{ {
pn5180_reset(); pn5180_reset();
pn5180_load_rf_config(0x0d, 0x8d); pn5180_load_rf_config(0x0d, 0x8d);
pn5180_rf_on(); pn5180_rf_field(true);
pn5180_clear_irq(0x0fffff); pn5180_clear_irq(0x0fffff);
pn5180_and_reg(PN5180_REG_SYSTEM_CONFIG, 0xfffffff8); pn5180_and_reg(PN5180_REG_SYSTEM_CONFIG, 0xfffffff8);
@ -333,7 +325,7 @@ bool pn5180_poll_vicinity(uint8_t uid[8])
sleep_ms(1); sleep_ms(1);
if ((pn5180_get_irq() & 0x4000) == 0) { if ((pn5180_get_irq() & 0x4000) == 0) {
pn5180_rf_off(); pn5180_rf_field(false);
return false; return false;
} }
@ -356,17 +348,42 @@ bool pn5180_poll_vicinity(uint8_t uid[8])
result = true; result = true;
} }
pn5180_rf_off();
return result; return result;
} }
void pn5180_print_rf_cfg() bool pn5180_mifare_auth(const uint8_t uid[4], uint8_t block_id, uint8_t key_id, const uint8_t key[6])
{ {
printf("RF_CONTROL_TX_CLK: %08lx\n", pn5180_read_reg(0x21)); uint8_t cmd[] = {
printf("TX_DATA_MOD: %08lx\n", pn5180_read_reg(0x16)); CMD_MIFARE_AUTHENTICATE,
printf("TX_UNDERSHOOT_CFG: %08lx\n", pn5180_read_reg(0x14)); key[0], key[1], key[2], key[3], key[4], key[5],
printf("TX_OVERSHOOT_CFG: %08lx\n", pn5180_read_reg(0x15)); key_id ? 0x61 : 0x60, block_id,
printf("RF_CONTROL_TX: %08lx\n", pn5180_read_reg(0x20)); uid[0], uid[1], uid[2], uid[3]
printf("ANT_CONTROL: %08lx\n", pn5180_read_reg(0x29)); };
}
uint8_t response = 0;
read_write(cmd, sizeof(cmd), &response, 1);
if ((response == 1) || (response == 2)) {
printf("\nMifare auth failed: %d", response);
return false;
}
return true;
}
bool pn5180_mifare_read(uint8_t block_id, uint8_t block_data[16])
{
uint8_t cmd[] = { CMD_MIFARE_READ, block_id };
pn5180_send_data(cmd, sizeof(cmd), 0);
sleep_ms_with_loop(5);
uint16_t len = pn5180_get_rx() & 0x1ff;
if (len != 16) {
printf("\nMifare read error (block %d): %d", block_id, len);
return false;
}
pn5180_read_data(block_data, 16);
return true;
}

View File

@ -34,8 +34,8 @@ void pn5180_read_data(uint8_t *data, uint8_t len);
void pn5180_read_eeprom(uint8_t addr, uint8_t *buf, uint8_t len); void pn5180_read_eeprom(uint8_t addr, uint8_t *buf, uint8_t len);
void pn5180_load_rf_config(uint8_t tx_cfg, uint8_t rx_cfg); void pn5180_load_rf_config(uint8_t tx_cfg, uint8_t rx_cfg);
void pn5180_rf_on(); void pn5180_rf_field(bool on);
void pn5180_rf_off();
uint32_t pn5180_get_irq(); uint32_t pn5180_get_irq();
void pn5180_clear_irq(uint32_t mask); void pn5180_clear_irq(uint32_t mask);
@ -47,6 +47,7 @@ bool pn5180_poll_mifare(uint8_t uid[7], 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[8]); bool pn5180_poll_vicinity(uint8_t uid[8]);
void pn5180_print_rf_cfg(); bool pn5180_mifare_auth(const uint8_t uid[4], uint8_t block_id, uint8_t key_id, const uint8_t key[6]);
bool pn5180_mifare_read(uint8_t block_id, uint8_t block_data[16]);
#endif #endif

View File

@ -371,7 +371,7 @@ bool pn532_poll_felica(uint8_t uid[8], uint8_t pmm[8], uint8_t syscode[2], bool
return true; return true;
} }
bool pn532_mifare_auth(const uint8_t uid[4], uint8_t block_id, uint8_t key_id, const uint8_t *key) bool pn532_mifare_auth(const uint8_t uid[4], uint8_t block_id, uint8_t key_id, const uint8_t key[6])
{ {
uint8_t param[] = { uint8_t param[] = {
1, key_id ? 0x61 : 0x60, block_id, 1, key_id ? 0x61 : 0x60, block_id,

View File

@ -29,7 +29,7 @@ void pn532_rf_field(bool on);
bool pn532_poll_mifare(uint8_t uid[7], int *len); bool pn532_poll_mifare(uint8_t uid[7], int *len);
bool pn532_poll_felica(uint8_t uid[8], uint8_t pmm[8], uint8_t syscode[2], bool from_cache); bool pn532_poll_felica(uint8_t uid[8], uint8_t pmm[8], uint8_t syscode[2], bool from_cache);
bool pn532_mifare_auth(const uint8_t uid[4], uint8_t block_id, uint8_t key_id, const uint8_t *key); bool pn532_mifare_auth(const uint8_t uid[4], uint8_t block_id, uint8_t key_id, const uint8_t key[6]);
bool pn532_mifare_read(uint8_t block_id, uint8_t block_data[16]); bool pn532_mifare_read(uint8_t block_id, uint8_t block_data[16]);
bool pn532_felica_read_wo_encrypt(uint16_t svc_code, uint16_t block_id, uint8_t block_data[16]); bool pn532_felica_read_wo_encrypt(uint16_t svc_code, uint16_t block_id, uint8_t block_data[16]);

View File

@ -146,11 +146,10 @@ void detect_card()
return; return;
} }
nfc_rf_field(true);
static nfc_card_t old_card = { 0 }; static nfc_card_t old_card = { 0 };
nfc_card_t card = nfc_detect_card();
nfc_rf_field(true);
nfc_card_t card = nfc_detect_card();
nfc_rf_field(false); nfc_rf_field(false);
if (memcmp(&old_card, &card, sizeof(old_card)) == 0) { if (memcmp(&old_card, &card, sizeof(old_card)) == 0) {