From 257ff917c4f2f26aed803316f429c83673f5c530 Mon Sep 17 00:00:00 2001 From: Fan DANG Date: Tue, 10 Dec 2024 17:30:25 +0800 Subject: [PATCH] change the return type of i2c_write_byte --- README.md | 2 +- include/device.h | 20 +++++----- interfaces/NFC/fm.c | 94 +++++++++++++++++++------------------------- interfaces/NFC/nfc.c | 13 +++--- 4 files changed, 59 insertions(+), 70 deletions(-) diff --git a/README.md b/README.md index 30fddd84..c12b4787 100644 --- a/README.md +++ b/README.md @@ -61,7 +61,7 @@ Use [Canokey-STM32](https://github.com/canokeys/canokey-stm32) as an example. * `uint8_t i2c_read_ack(void);` * `void i2c_send_ack(void);` * `void i2c_send_nack(void);` - * `void i2c_write_byte(uint8_t data);` + * `bool i2c_write_byte(uint8_t data);` * `uint8_t i2c_read_byte(void);` 2. You should also provide a `random32` and a optional `random_buffer` function in `rand.h`. diff --git a/include/device.h b/include/device.h index 10ae119e..3836c298 100644 --- a/include/device.h +++ b/include/device.h @@ -75,7 +75,7 @@ void scl_delay(void); uint8_t i2c_read_ack(void); void i2c_send_ack(void); void i2c_send_nack(void); -void i2c_write_byte(uint8_t data); +bool i2c_write_byte(uint8_t data); uint8_t i2c_read_byte(void); #endif @@ -115,17 +115,15 @@ void stop_blinking(void); uint8_t device_is_blinking(void); bool device_allow_kbd_touch(void); void fm11_init(void); -uint8_t fm_read_reg(uint16_t reg); -void fm_read_regs(uint16_t reg, uint8_t *buf, uint8_t len); -void fm_write_reg(uint16_t reg, uint8_t val); -void fm_write_regs(uint16_t reg, const uint8_t *buf, uint8_t len); -void fm_read_eeprom(uint16_t addr, uint8_t *buf, uint8_t len); -void fm_write_eeprom(uint16_t addr, const uint8_t *buf, uint8_t len); -void fm_read_fifo(uint8_t *buf, uint8_t len); -void fm_write_fifo(uint8_t *buf, uint8_t len); +bool fm_read_regs(uint16_t reg, uint8_t *buf, uint8_t len); +bool fm_write_regs(uint16_t reg, const uint8_t *buf, uint8_t len); +bool fm_read_eeprom(uint16_t addr, uint8_t *buf, uint8_t len); +bool fm_write_eeprom(uint16_t addr, const uint8_t *buf, uint8_t len); +bool fm_read_fifo(uint8_t *buf, uint8_t len); +bool fm_write_fifo(uint8_t *buf, uint8_t len); #if NFC_CHIP == NFC_CHIP_FM11NT -void fm11nt_read(uint16_t addr, uint8_t *buf, uint8_t len); -void fm11nt_write(uint16_t addr, const uint8_t *buf, uint8_t len); +bool fm11nt_read(uint16_t addr, uint8_t *buf, uint8_t len); +bool fm11nt_write(uint16_t addr, const uint8_t *buf, uint8_t len); uint8_t fm_crc8(const uint8_t *data, const uint8_t data_length); #endif diff --git a/interfaces/NFC/fm.c b/interfaces/NFC/fm.c index 669f8fb9..4498a126 100644 --- a/interfaces/NFC/fm.c +++ b/interfaces/NFC/fm.c @@ -2,29 +2,17 @@ #include "device.h" #include +#define I2C_WRITE_WITH_CHECK(data) \ + do { \ + if (!i2c_write_byte(data)) return false; \ + } while (0) + static void device_delay_us(int us) { for (int i = 0; i < us * 10; ++i) asm volatile("nop"); } -uint8_t fm_read_reg(uint16_t reg) { - uint8_t val; -#if NFC_CHIP == NFC_CHIP_FM11NC - fm_csn_low(); - uint8_t addr = reg; - addr |= 0x20; - spi_transmit(&addr, 1); - spi_receive(&val, 1); - fm_csn_high(); -#elif NFC_CHIP == NFC_CHIP_FM11NT - fm11nt_read(reg, &val, 1); -#else - val = 0; -#endif - return val; -} - -void fm_read_regs(uint16_t reg, uint8_t *buf, uint8_t len) { +bool fm_read_regs(uint16_t reg, uint8_t *buf, uint8_t len) { #if NFC_CHIP == NFC_CHIP_FM11NC fm_csn_low(); uint8_t addr = reg; @@ -32,36 +20,26 @@ void fm_read_regs(uint16_t reg, uint8_t *buf, uint8_t len) { spi_transmit(&addr, 1); spi_receive(buf, len); fm_csn_high(); + return true; #elif NFC_CHIP == NFC_CHIP_FM11NT - fm11nt_read(reg, buf, len); + return fm11nt_read(reg, buf, len); #endif } -void fm_write_reg(uint16_t reg, uint8_t val) { -#if NFC_CHIP == NFC_CHIP_FM11NC - fm_csn_low(); - uint8_t addr = reg; - spi_transmit(&addr, 1); - spi_transmit(&val, 1); - fm_csn_high(); -#elif NFC_CHIP == NFC_CHIP_FM11NT - fm11nt_write(reg, &val, 1); -#endif -} - -void fm_write_regs(uint16_t reg, const uint8_t *buf, uint8_t len) { +bool fm_write_regs(uint16_t reg, const uint8_t *buf, uint8_t len) { #if NFC_CHIP == NFC_CHIP_FM11NC fm_csn_low(); uint8_t addr = reg; spi_transmit(&addr, 1); spi_transmit(buf, len); fm_csn_high(); + return true; #elif NFC_CHIP == NFC_CHIP_FM11NT - fm11nt_write(reg, buf, len); + return fm11nt_write(reg, buf, len); #endif } -void fm_read_eeprom(uint16_t addr, uint8_t *buf, uint8_t len) { +bool fm_read_eeprom(uint16_t addr, uint8_t *buf, uint8_t len) { #if NFC_CHIP == NFC_CHIP_FM11NC fm_csn_low(); device_delay_us(100); @@ -69,12 +47,13 @@ void fm_read_eeprom(uint16_t addr, uint8_t *buf, uint8_t len) { spi_transmit(data, 2); spi_receive(buf, len); fm_csn_high(); + return true; #elif NFC_CHIP == NFC_CHIP_FM11NT - fm11nt_read(addr, buf, len); + return fm11nt_read(addr, buf, len); #endif } -void fm_write_eeprom(uint16_t addr, const uint8_t *buf, uint8_t len) { +bool fm_write_eeprom(uint16_t addr, const uint8_t *buf, uint8_t len) { #if NFC_CHIP == NFC_CHIP_FM11NC fm_csn_low(); device_delay_us(100); @@ -90,33 +69,37 @@ void fm_write_eeprom(uint16_t addr, const uint8_t *buf, uint8_t len) { spi_transmit(data, 2); spi_transmit(buf, len); fm_csn_high(); + return true; #elif NFC_CHIP == NFC_CHIP_FM11NT - fm11nt_write(addr, buf, len); + const bool ret = fm11nt_write(addr, buf, len); device_delay(10); + return ret; #endif } -void fm_read_fifo(uint8_t *buf, uint8_t len) { +bool fm_read_fifo(uint8_t *buf, uint8_t len) { #if NFC_CHIP == NFC_CHIP_FM11NC fm_csn_low(); uint8_t addr = 0xA0; spi_transmit(&addr, 1); spi_receive(buf, len); fm_csn_high(); + return true; #elif NFC_CHIP == NFC_CHIP_FM11NT - fm11nt_read(FM_REG_FIFO_ACCESS, buf, len); + return fm11nt_read(FM_REG_FIFO_ACCESS, buf, len); #endif } -void fm_write_fifo(uint8_t *buf, uint8_t len) { +bool fm_write_fifo(uint8_t *buf, uint8_t len) { #if NFC_CHIP == NFC_CHIP_FM11NC fm_csn_low(); uint8_t addr = 0x80; spi_transmit(&addr, 1); spi_transmit(buf, len); fm_csn_high(); + return true; #elif NFC_CHIP == NFC_CHIP_FM11NT - fm11nt_write(FM_REG_FIFO_ACCESS, buf, len); + return fm11nt_write(FM_REG_FIFO_ACCESS, buf, len); #endif } @@ -144,7 +127,8 @@ void fm11_init(void) { fm_write_eeprom(FM_EEPROM_ATS, ats, sizeof(ats)); fm_write_eeprom(FM_EEPROM_ATQA, atqa_sak, sizeof(atqa_sak)); fm_read_eeprom(FM_EEPROM_SN, crc_buffer, 9); - DBG_MSG("SN: "); PRINT_HEX(crc_buffer, 9); + DBG_MSG("SN: "); + PRINT_HEX(crc_buffer, 9); memcpy(crc_buffer + 9, atqa_sak, sizeof(atqa_sak)); const uint8_t crc8 = fm_crc8(crc_buffer, sizeof(crc_buffer)); fm_write_eeprom(FM_EEPROM_CRC8, &crc8, 1); @@ -156,19 +140,19 @@ void fm11_init(void) { #define I2C_ADDR 0x57 -void fm11nt_read(uint16_t addr, uint8_t *buf, uint8_t len) { +bool fm11nt_read(uint16_t addr, uint8_t *buf, uint8_t len) { uint8_t slave_id = (I2C_ADDR << 1) | 0; i2c_start(); - i2c_write_byte(slave_id); + I2C_WRITE_WITH_CHECK(slave_id); // set reg/eeprom addr - i2c_write_byte(addr >> 8); - i2c_write_byte(addr & 0xFF); + I2C_WRITE_WITH_CHECK(addr >> 8); + I2C_WRITE_WITH_CHECK(addr & 0xFF); // switch to read mode slave_id |= 1; i2c_start(); - i2c_write_byte(slave_id); + I2C_WRITE_WITH_CHECK(slave_id); // master transmit for (size_t k = 0; k < len; k++) { @@ -186,23 +170,27 @@ void fm11nt_read(uint16_t addr, uint8_t *buf, uint8_t len) { // wait to receive next byte from slave scl_delay(); } + + return true; } -void fm11nt_write(const uint16_t addr, const uint8_t *buf, const uint8_t len) { +bool fm11nt_write(const uint16_t addr, const uint8_t *buf, const uint8_t len) { const uint8_t slave_id = (I2C_ADDR << 1) | 0; i2c_start(); - i2c_write_byte(slave_id); + I2C_WRITE_WITH_CHECK(slave_id); // set reg/eeprom addr - i2c_write_byte(addr >> 8); - i2c_write_byte(addr & 0xFF); + I2C_WRITE_WITH_CHECK(addr >> 8); + I2C_WRITE_WITH_CHECK(addr & 0xFF); // master transmit for (size_t i = 0; i < len; i++) { // master write a byte to salve and check ACK signal - i2c_write_byte(buf[i]); + I2C_WRITE_WITH_CHECK(buf[i]); } i2c_stop(); + + return true; } uint8_t fm_crc8(const uint8_t *data, const uint8_t data_length) { @@ -220,4 +208,4 @@ uint8_t fm_crc8(const uint8_t *data, const uint8_t data_length) { return crc8 & 0xff; } -#endif +#endif \ No newline at end of file diff --git a/interfaces/NFC/nfc.c b/interfaces/NFC/nfc.c index 3e7b2607..c827a493 100644 --- a/interfaces/NFC/nfc.c +++ b/interfaces/NFC/nfc.c @@ -33,7 +33,7 @@ void nfc_init(void) { // NFC interface uses global_buffer w/o calling acquire_apdu_buffer(), because NFC mode is exclusive with USB mode apdu_cmd.data = global_buffer; apdu_resp.data = global_buffer; - fm_write_reg(FM_REG_FIFO_FLUSH, 0); // writing anything to this reg will flush FIFO buffer + fm_write_regs(FM_REG_FIFO_FLUSH, &block_number, 1); // writing anything to this reg will flush FIFO buffer } static void nfc_error_handler(int code __attribute__((unused))) { @@ -46,8 +46,10 @@ static void nfc_error_handler(int code __attribute__((unused))) { state_spinlock = 0; next_state = TO_RECEIVE; #if NFC_CHIP == NFC_CHIP_FM11NT - fm_write_reg(FM_REG_RF_TXEN, 0x77); // set NFC to IDLE - fm_write_reg(FM_REG_RESET_SILENCE, 0x55); // reset + uint8_t data = 0x77; // set NFC to IDLE + fm_write_regs(FM_REG_RF_TXEN, &data, 1); + data = 0x55; // reset + fm_write_regs(FM_REG_RESET_SILENCE, &data, 1); #endif } @@ -61,7 +63,8 @@ static void do_nfc_send_frame(uint8_t prologue, uint8_t *data, uint8_t len) { PRINT_HEX(tx_frame_buf, len + 1); fm_write_fifo(tx_frame_buf, len + 1); - fm_write_reg(FM_REG_RF_TXEN, 0x55); + const uint8_t val = 0x55; + fm_write_regs(FM_REG_RF_TXEN, &val, 1); } void nfc_send_frame(uint8_t prologue, uint8_t *data, uint8_t len) { @@ -181,7 +184,7 @@ void nfc_handler(void) { } if (irq[0] & MAIN_IRQ_RX_DONE) { - rx_frame_size = fm_read_reg(FM_REG_FIFO_WORDCNT); + fm_read_regs(FM_REG_FIFO_WORDCNT, &rx_frame_size, 1); if (rx_frame_size > 32) { nfc_error_handler(-5); return;