From 6953cbea2caa55e5c1e3e3254f0e9274f9bdcfd5 Mon Sep 17 00:00:00 2001 From: Marcos Vives Del Sol Date: Sat, 13 Jun 2015 23:10:36 +0200 Subject: [PATCH] Further work for RC522 --- libnfc/chips/rc522-internal.h | 15 ++++++++++++++ libnfc/chips/rc522.c | 37 +++++++++++++++++++++++++++++++---- libnfc/chips/rc522.h | 4 ++-- libnfc/drivers/rc522_uart.c | 17 ++++++++-------- 4 files changed, 59 insertions(+), 14 deletions(-) diff --git a/libnfc/chips/rc522-internal.h b/libnfc/chips/rc522-internal.h index 4415207..4e9186b 100644 --- a/libnfc/chips/rc522-internal.h +++ b/libnfc/chips/rc522-internal.h @@ -3,7 +3,11 @@ #ifndef __NFC_CHIPS_RC522_INTERNAL_H__ #define __NFC_CHIPS_RC522_INTERNAL_H__ +#define RC522_FIFO_SIZE 64 + #define RC522_REG_CommandReg 0x01 +#define RC522_REG_CommandReg_RcvOff (1 << 5) +#define RC522_REG_CommandReg_PowerDown (1 << 4) #define RC522_REG_ComlEnReg 0x02 @@ -92,4 +96,15 @@ #define RC522_REG_TestDAC2Reg 0x3A #define RC522_REG_TestADCReg 0x3B +#define RC522_CMD_Idle 0x0 +#define RC522_CMD_Mem 0x1 +#define RC522_CMD_GenerateRandomId 0x2 +#define RC522_CMD_CalcCRC 0x3 +#define RC522_CMD_Transmit 0x4 +#define RC522_CMD_NoCmdChange 0x7 +#define RC522_CMD_Receive 0x8 +#define RC522_CMD_Transceive 0xC +#define RC522_CMD_MFAuthent 0xE +#define RC522_CMD_SoftReset 0xF + #endif diff --git a/libnfc/chips/rc522.c b/libnfc/chips/rc522.c index d4545a8..7a6539e 100644 --- a/libnfc/chips/rc522.c +++ b/libnfc/chips/rc522.c @@ -54,7 +54,7 @@ int rc522_data_new(struct nfc_device * pnd, const struct rc522_io * io) { int rc522_read_reg(struct nfc_device * pnd, uint8_t reg) { uint8_t val; - int ret = CHIP_DATA(pnd)->io->read(pnd, reg, &val, 1, RC522_TIMEOUT); + int ret = CHIP_DATA(pnd)->io->read(pnd, reg, &val, 1); if (ret < 0) { return ret; } @@ -72,11 +72,12 @@ int rc522_write_reg(struct nfc_device * pnd, uint8_t reg, uint8_t val, uint8_t m val = (val & mask) | (oldval & ~mask); } - return CHIP_DATA(pnd)->io->write(pnd, reg, &val, 1, RC522_TIMEOUT); + return CHIP_DATA(pnd)->io->write(pnd, reg, &val, 1); } -int rc522_set_speed(struct nfc_device * pnd, nfc_baud_rate speed) { +int rc522_set_baud_rate(struct nfc_device * pnd, nfc_baud_rate speed) { uint8_t txVal, rxVal; + int ret; switch (speed) { case NBR_106: @@ -108,6 +109,23 @@ int rc522_set_speed(struct nfc_device * pnd, nfc_baud_rate speed) { rc522_write_reg(pnd, RC522_REG_RxModeReg, rxVal, RC522_REG_RxModeReg_RxSpeed_MASK); } +int rc522_initiator_select_passive_target_ext(struct nfc_device * pnd, const nfc_modulation nm, const uint8_t * pbtInitData, const size_t szInitData, nfc_target * pnt, int timeout) +{ + int ret; + + if (nm.nmt != NMT_ISO14443A) { + return NFC_EINVARG; + } + + ret = rc522_set_baud_rate(pnd, nm.nbr); + if (ret < 0) { + return ret; + } + + // TODO + return NFC_ENOTIMPL; +} + int rc522_get_supported_modulation(struct nfc_device * pnd, const nfc_mode mode, const nfc_modulation_type ** const supported_mt) { switch (mode) { case N_INITIATOR: @@ -196,7 +214,7 @@ int rc522_set_property_bool(struct nfc_device * pnd, const nfc_property property return NFC_SUCCESS; } - return rc522_set_speed(pnd, NBR_106); + return rc522_set_baud_rate(pnd, NBR_106); case NP_AUTO_ISO14443_4: case NP_ACCEPT_INVALID_FRAMES: @@ -210,3 +228,14 @@ int rc522_set_property_bool(struct nfc_device * pnd, const nfc_property property return NFC_EINVARG; } + +int rc522_set_property_int(struct nfc_device * pnd, const nfc_property property, const int value) { + // TODO + return NFC_ENOTIMPL; +} + +int rc522_idle(struct nfc_device * pnd) { + // Set idle and disable RX demodulator to save energy + return rc522_write_reg(pnd, RC522_REG_CommandReg, RC522_CMD_Idle | RC522_REG_CommandReg_PowerDown, 0xFF); +} + diff --git a/libnfc/chips/rc522.h b/libnfc/chips/rc522.h index dac87bb..41153d2 100644 --- a/libnfc/chips/rc522.h +++ b/libnfc/chips/rc522.h @@ -23,8 +23,8 @@ #include struct rc522_io { - int (*read)(struct nfc_device * pnd, uint8_t reg, uint8_t * data, size_t size, unsigned int timeout); - int (*write)(struct nfc_device * pnd, uint8_t reg, const uint8_t * data, size_t size, unsigned int timeout); + int (*read)(struct nfc_device * pnd, uint8_t reg, uint8_t * data, size_t size); + int (*write)(struct nfc_device * pnd, uint8_t reg, const uint8_t * data, size_t size); }; int rc522_data_new(struct nfc_device * pnd, const struct rc522_io * io); diff --git a/libnfc/drivers/rc522_uart.c b/libnfc/drivers/rc522_uart.c index c65bfbc..8d49b44 100644 --- a/libnfc/drivers/rc522_uart.c +++ b/libnfc/drivers/rc522_uart.c @@ -38,6 +38,7 @@ #define RC522_UART_DEFAULT_SPEED 9600 #define RC522_UART_DRIVER_NAME "rc522_uart" +#define RC522_UART_IO_TIMEOUT 50 #define LOG_CATEGORY "libnfc.driver.rc522_uart" #define LOG_GROUP NFC_LOG_GROUP_DRIVER @@ -245,16 +246,16 @@ uint8_t rc522_uart_pack(int reg, int op) { return op << 7 | reg; } -int rc522_uart_read(struct nfc_device * pnd, uint8_t reg, uint8_t * data, size_t size, unsigned int timeout) { +int rc522_uart_read(struct nfc_device * pnd, uint8_t reg, uint8_t * data, size_t size) { uint8_t cmd = rc522_uart_pack(reg, READ); while (size > 0) { - pnd->last_error = uart_send(pnd->driver_data, &cmd, 1, timeout); + pnd->last_error = uart_send(pnd->driver_data, &cmd, 1, RC522_UART_IO_TIMEOUT); if (pnd->last_error < 0) { goto error; } - pnd->last_error = uart_receive(pnd->driver_data, data, 1, timeout); + pnd->last_error = uart_receive(pnd->driver_data, data, 1, RC522_UART_IO_TIMEOUT); if (pnd->last_error < 0) { goto error; } @@ -270,19 +271,19 @@ error: return pnd->last_error; } -int rc522_uart_write(struct nfc_device * pnd, uint8_t reg, uint8_t * data, size_t size, unsigned int timeout) { +int rc522_uart_write(struct nfc_device * pnd, uint8_t reg, uint8_t * data, size_t size) { uint8_t cmd = rc522_uart_pack(reg, WRITE); while (size > 0) { // First: send write request - pnd->last_error = uart_send(pnd->driver_data, &cmd, 1, timeout); + pnd->last_error = uart_send(pnd->driver_data, &cmd, 1, RC522_UART_IO_TIMEOUT); if (pnd->last_error < 0) { goto error; } // Second: wait for a reply uint8_t reply; - pnd->last_error = uart_receive(pnd->driver_data, &reply, 1, timeout); + pnd->last_error = uart_receive(pnd->driver_data, &reply, 1, RC522_UART_IO_TIMEOUT); if (pnd->last_error < 0) { return pnd->last_error; } @@ -294,7 +295,7 @@ int rc522_uart_write(struct nfc_device * pnd, uint8_t reg, uint8_t * data, size_ } // Fourth: send register data - pnd->last_error = uart_send(pnd->driver_data, data, 1, timeout); + pnd->last_error = uart_send(pnd->driver_data, data, 1, RC522_UART_IO_TIMEOUT); if (pnd->last_error < 0) { goto error; } @@ -349,7 +350,7 @@ const struct nfc_driver rc522_uart_driver = { .get_supported_baud_rate = rc522_get_supported_baud_rate, .device_get_information_about = rc522_get_information_about, - .abort_command = rc522_uart_abort_command, + .abort_command = rc522_idle, .idle = rc522_idle, .powerdown = rc522_softdown, };