Skip to content

Commit

Permalink
change the return type of i2c_write_byte
Browse files Browse the repository at this point in the history
  • Loading branch information
dangfan committed Dec 10, 2024
1 parent 9834767 commit 257ff91
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 70 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Expand Down
20 changes: 9 additions & 11 deletions include/device.h
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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

Expand Down
94 changes: 41 additions & 53 deletions interfaces/NFC/fm.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,79 +2,58 @@
#include "device.h"
#include <stdint.h>

#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;
addr |= 0x20;
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);
uint8_t data[2] = {0x60 | (addr >> 8), addr & 0xFF};
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);
Expand All @@ -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
}

Expand Down Expand Up @@ -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);
Expand All @@ -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++) {
Expand All @@ -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) {
Expand All @@ -220,4 +208,4 @@ uint8_t fm_crc8(const uint8_t *data, const uint8_t data_length) {
return crc8 & 0xff;
}

#endif
#endif
13 changes: 8 additions & 5 deletions interfaces/NFC/nfc.c
Original file line number Diff line number Diff line change
Expand Up @@ -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))) {
Expand All @@ -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
}

Expand All @@ -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) {
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 257ff91

Please sign in to comment.