From 8c74095e42286df63501400934b93b65b73dc1b4 Mon Sep 17 00:00:00 2001 From: whowechina Date: Tue, 23 Apr 2024 10:49:30 +0800 Subject: [PATCH] detect_card_ex to ignore card types --- firmware/include/nfc.h | 120 +++++++++++++++++++++-------------------- firmware/src/lib/nfc.c | 23 ++++++-- 2 files changed, 80 insertions(+), 63 deletions(-) diff --git a/firmware/include/nfc.h b/firmware/include/nfc.h index 4f04110..8c829e5 100644 --- a/firmware/include/nfc.h +++ b/firmware/include/nfc.h @@ -1,59 +1,61 @@ -/* - * NFC Operations - * WHowe - * - */ - -#ifndef NFC_H -#define NFC_H - -#include -#include -#include "hardware/i2c.h" -#include "hardware/spi.h" - -typedef enum { - NFC_CARD_NONE = 0, - NFC_CARD_MIFARE, - NFC_CARD_FELICA, - NFC_CARD_VICINITY, -} nfc_card_type; - -const char *nfc_card_name(nfc_card_type card_type); - -typedef void (*nfc_wait_loop_t)(); -typedef struct { - nfc_card_type card_type; - uint16_t len; - union { - uint8_t uid[8]; - uint8_t idm[8]; - }; - uint8_t pmm[8]; - uint8_t syscode[2]; -} nfc_card_t; - -/* should init or attach i2c and spi port before init */ -void nfc_attach_i2c(i2c_inst_t *port); -void nfc_attach_spi(spi_inst_t *port, uint8_t rst, uint8_t nss, uint8_t busy); - -void nfc_init_i2c(i2c_inst_t *port, uint8_t scl, uint8_t sda, uint32_t freq); -void nfc_init_spi(spi_inst_t *port, uint8_t miso, uint8_t sck, uint8_t mosi, - uint8_t rst, uint8_t nss, uint8_t busy); - -void nfc_init(); - -/* should be called only after init */ -void nfc_set_wait_loop(nfc_wait_loop_t loop); - -void nfc_rf_field(bool on); - -nfc_card_t nfc_detect_card(); -void display_card(const nfc_card_t *card); - -const char *nfc_module_name(); - -bool nfc_mifare_auth(const uint8_t uid[4], uint8_t block_id, uint8_t key_id, const uint8_t *key); -bool nfc_mifare_read(uint8_t block_id, uint8_t block_data[16]); - -#endif +/* + * NFC Operations + * WHowe + * + */ + +#ifndef NFC_H +#define NFC_H + +#include +#include +#include "hardware/i2c.h" +#include "hardware/spi.h" + +typedef enum { + NFC_CARD_NONE = 0, + NFC_CARD_MIFARE, + NFC_CARD_FELICA, + NFC_CARD_VICINITY, +} nfc_card_type; + +const char *nfc_card_name(nfc_card_type card_type); + +typedef void (*nfc_wait_loop_t)(); +typedef struct { + nfc_card_type card_type; + uint16_t len; + union { + uint8_t uid[8]; + uint8_t idm[8]; + }; + uint8_t pmm[8]; + uint8_t syscode[2]; +} nfc_card_t; + +/* should init or attach i2c and spi port before init */ +void nfc_attach_i2c(i2c_inst_t *port); +void nfc_attach_spi(spi_inst_t *port, uint8_t rst, uint8_t nss, uint8_t busy); + +void nfc_init_i2c(i2c_inst_t *port, uint8_t scl, uint8_t sda, uint32_t freq); +void nfc_init_spi(spi_inst_t *port, uint8_t miso, uint8_t sck, uint8_t mosi, + uint8_t rst, uint8_t nss, uint8_t busy); + +void nfc_init(); + +/* should be called only after init */ +void nfc_set_wait_loop(nfc_wait_loop_t loop); + +void nfc_rf_field(bool on); + +nfc_card_t nfc_detect_card(); +nfc_card_t nfc_detect_card_ex(bool mifare, bool felica, bool vicinity); + +void display_card(const nfc_card_t *card); + +const char *nfc_module_name(); + +bool nfc_mifare_auth(const uint8_t uid[4], uint8_t block_id, uint8_t key_id, const uint8_t *key); +bool nfc_mifare_read(uint8_t block_id, uint8_t block_data[16]); + +#endif diff --git a/firmware/src/lib/nfc.c b/firmware/src/lib/nfc.c index c3ae770..2aec0b6 100644 --- a/firmware/src/lib/nfc.c +++ b/firmware/src/lib/nfc.c @@ -199,12 +199,27 @@ nfc_card_t nfc_detect_card() { nfc_card_t card = { 0 }; - if (!nfc_detect_mifare(&card) && - !nfc_detect_felica(&card) && - !nfc_detect_vicinity(&card)) { - card.card_type = NFC_CARD_NONE; + if (nfc_detect_mifare(&card) || + nfc_detect_felica(&card) || + nfc_detect_vicinity(&card)) { + return card; } + card.card_type = NFC_CARD_NONE; + return card; +} + +nfc_card_t nfc_detect_card_ex(bool mifare, bool felica, bool vicinity) +{ + nfc_card_t card = { 0 }; + + if ((mifare && nfc_detect_mifare(&card)) + || (felica && nfc_detect_felica(&card)) + || (vicinity && nfc_detect_vicinity(&card))) { + return card; + } + + card.card_type = NFC_CARD_NONE; return card; }