Skip to content

Commit

Permalink
Merge pull request #106 from canokeys/feature/i2c_nack_check
Browse files Browse the repository at this point in the history
Feature/i2c nack check
  • Loading branch information
z4yx authored Dec 13, 2024
2 parents 23604a6 + 4ba075a commit 5303543
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 29 deletions.
25 changes: 15 additions & 10 deletions include/device.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@
#define WAIT_ENTRY_CCID 0
#define WAIT_ENTRY_CTAPHID 1

typedef enum {
FM_STATUS_OK = 0,
FM_STATUS_NACK = 1,
} fm_status_t;

// functions should be implemented by device
/**
* Delay processing for specific milliseconds
Expand Down Expand Up @@ -72,10 +77,10 @@ void spi_receive(uint8_t *buf, uint8_t len);
void i2c_start(void);
void i2c_stop(void);
void scl_delay(void);
uint8_t i2c_read_ack(void);
fm_status_t i2c_read_ack(void);
void i2c_send_ack(void);
void i2c_send_nack(void);
bool i2c_write_byte(uint8_t data);
fm_status_t i2c_write_byte(uint8_t data);
uint8_t i2c_read_byte(void);
#endif

Expand Down Expand Up @@ -115,15 +120,15 @@ void stop_blinking(void);
uint8_t device_is_blinking(void);
bool device_allow_kbd_touch(void);
void fm11_init(void);
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);
fm_status_t fm_read_regs(uint16_t reg, uint8_t *buf, uint8_t len);
fm_status_t fm_write_regs(uint16_t reg, const uint8_t *buf, uint8_t len);
fm_status_t fm_read_eeprom(uint16_t addr, uint8_t *buf, uint8_t len);
fm_status_t fm_write_eeprom(uint16_t addr, const uint8_t *buf, uint8_t len);
fm_status_t fm_read_fifo(uint8_t *buf, uint8_t len);
fm_status_t fm_write_fifo(uint8_t *buf, uint8_t len);
#if NFC_CHIP == NFC_CHIP_FM11NT
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);
fm_status_t fm11nt_read(uint16_t addr, uint8_t *buf, uint8_t len);
fm_status_t 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
38 changes: 19 additions & 19 deletions interfaces/NFC/fm.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,58 +2,58 @@
#include "device.h"
#include <stdint.h>

#define I2C_WRITE_WITH_CHECK(data) \
do { \
if (!i2c_write_byte(data)) return false; \
#define I2C_WRITE_WITH_CHECK(data) \
do { \
if (i2c_write_byte(data) == FM_STATUS_NACK) return FM_STATUS_NACK; \
} while (0)

static void device_delay_us(int us) {
for (int i = 0; i < us * 10; ++i)
asm volatile("nop");
}

bool fm_read_regs(uint16_t reg, uint8_t *buf, uint8_t len) {
fm_status_t 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;
return FM_STATUS_OK;
#elif NFC_CHIP == NFC_CHIP_FM11NT
return fm11nt_read(reg, buf, len);
#endif
}

bool fm_write_regs(uint16_t reg, const uint8_t *buf, uint8_t len) {
fm_status_t 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;
return FM_STATUS_OK;
#elif NFC_CHIP == NFC_CHIP_FM11NT
return fm11nt_write(reg, buf, len);
#endif
}

bool fm_read_eeprom(uint16_t addr, uint8_t *buf, uint8_t len) {
fm_status_t 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;
return FM_STATUS_OK;
#elif NFC_CHIP == NFC_CHIP_FM11NT
return fm11nt_read(addr, buf, len);
#endif
}

bool fm_write_eeprom(uint16_t addr, const uint8_t *buf, uint8_t len) {
fm_status_t 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 @@ -69,35 +69,35 @@ bool 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;
return FM_STATUS_OK;
#elif NFC_CHIP == NFC_CHIP_FM11NT
const bool ret = fm11nt_write(addr, buf, len);
device_delay(10);
return ret;
#endif
}

bool fm_read_fifo(uint8_t *buf, uint8_t len) {
fm_status_t 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;
return FM_STATUS_OK;
#elif NFC_CHIP == NFC_CHIP_FM11NT
return fm11nt_read(FM_REG_FIFO_ACCESS, buf, len);
#endif
}

bool fm_write_fifo(uint8_t *buf, uint8_t len) {
fm_status_t 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;
return FM_STATUS_OK;
#elif NFC_CHIP == NFC_CHIP_FM11NT
return fm11nt_write(FM_REG_FIFO_ACCESS, buf, len);
#endif
Expand Down Expand Up @@ -140,7 +140,7 @@ void fm11_init(void) {

#define I2C_ADDR 0x57

bool fm11nt_read(uint16_t addr, uint8_t *buf, uint8_t len) {
fm_status_t fm11nt_read(uint16_t addr, uint8_t *buf, uint8_t len) {
uint8_t slave_id = (I2C_ADDR << 1) | 0;
i2c_start();
I2C_WRITE_WITH_CHECK(slave_id);
Expand Down Expand Up @@ -171,10 +171,10 @@ bool fm11nt_read(uint16_t addr, uint8_t *buf, uint8_t len) {
scl_delay();
}

return true;
return FM_STATUS_OK;
}

bool fm11nt_write(const uint16_t addr, const uint8_t *buf, const uint8_t len) {
fm_status_t 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_WITH_CHECK(slave_id);
Expand All @@ -190,7 +190,7 @@ bool fm11nt_write(const uint16_t addr, const uint8_t *buf, const uint8_t len) {
}
i2c_stop();

return true;
return FM_STATUS_OK;
}

uint8_t fm_crc8(const uint8_t *data, const uint8_t data_length) {
Expand Down

0 comments on commit 5303543

Please sign in to comment.