24#define ST25DV16KC_I2C_ADDR_USER (0x53 << 1)
25#define ST25DV16KC_I2C_ADDR_SYSTEM (0x57 << 1)
30#define ST25DV16KC_REG_GPO_CTRL_DYN 0x2000U
31#define ST25DV16KC_REG_EH_CTRL_DYN 0x2001U
32#define ST25DV16KC_REG_RF_MNGT_DYN 0x2002U
33#define ST25DV16KC_REG_I2C_SSO_DYN 0x2003U
34#define ST25DV16KC_REG_MB_CTRL_DYN 0x2006U
35#define ST25DV16KC_REG_MB_LEN_DYN 0x2007U
36#define ST25DV16KC_REG_MB_DATA 0x2008U
42#define ST25DV16KC_SYS_REG_RF_MNGT 0x0003U
43#define ST25DV16KC_SYS_REG_MB_MODE 0x000DU
44#define ST25DV16KC_SYS_REG_I2C_PWD 0x0900U
49#define ST25DV16KC_GPO_EN 0x80U
50#define ST25DV16KC_GPO_MSG_READY_EN 0x40U
51#define ST25DV16KC_GPO_RF_ACTIVITY 0x20U
52#define ST25DV16KC_GPO_RF_INTERRUPT 0x10U
53#define ST25DV16KC_GPO_FIELD_CHANGE 0x08U
54#define ST25DV16KC_GPO_RF_PUT_MSG 0x04U
55#define ST25DV16KC_GPO_RF_GET_MSG 0x02U
56#define ST25DV16KC_GPO_RF_WRITE 0x01U
59#define ST25DV16KC_GPO_ALL_EN (ST25DV16KC_GPO_EN | ST25DV16KC_GPO_MSG_READY_EN | \
60 ST25DV16KC_GPO_FIELD_CHANGE | ST25DV16KC_GPO_RF_WRITE)
63#define ST25DV16KC_GPO_INT_EN (ST25DV16KC_GPO_EN | ST25DV16KC_GPO_FIELD_CHANGE)
68#define ST25DV16KC_MB_EN 0x01U
69#define ST25DV16KC_MB_RF_PUT_MSG 0x02U
70#define ST25DV16KC_MB_I2C_PUT_MSG 0x04U
75#define ST25DV16KC_RF_SLEEP 0x01U
76#define ST25DV16KC_RF_DISABLE 0x02U
81#define ST25DV16KC_EH_FIELD_ON 0x04U
87#define ST25DV16KC_RF_BUSY_TIMEOUT 20U
92#define ST25DV16KC_MB_SIZE 256U
101#define EEPROM_TIMEOUT 5000
107static HAL_StatusTypeDef nfc_st25dv16kc_PresentPassword(I2C_HandleTypeDef *hi2c)
109 HAL_StatusTypeDef status;
110 uint8_t pwd_payload[17] = {0,0,0,0,0,0,0,0,9,0,0,0,0,0,0,0,0};
127static HAL_StatusTypeDef nfc_st25dv16kc_PresentPassword(I2C_HandleTypeDef *hi2c)
129 HAL_StatusTypeDef status;
137 uint8_t pwd_payload[9];
138 pwd_payload[8] = 0x09;
140 static uint8_t ppp = 0;
145 pwd_payload[i] = ((ppp>>i) & 1);
146 buf[i] = pwd_payload[i]+
'0';
155 if (status == HAL_OK)
170static HAL_StatusTypeDef nfc_st25dv16kc_SetRFMgmt(I2C_HandleTypeDef *hi2c, uint8_t enable)
172 HAL_StatusTypeDef status = HAL_ERROR;
173 uint8_t val = enable ? 0x00 : 0x01;
178 if ((status = nfc_st25dv16kc_PresentPassword(hi2c)) != HAL_OK)
187#define ST25_ADDR_SYST 0xAE
188#define ST25_ADDR_USER 0xA6
191#define REG_GPO_DEFAULT 0x00
192#define REG_RF_MNGT_DEFAULT 0x00
193#define REG_IT_TIME_DEFAULT 0x03
194static HAL_StatusTypeDef ST25_FactoryReset_Config(I2C_HandleTypeDef *hi2c)
196 HAL_StatusTypeDef status;
200 nfc_st25dv16kc_PresentPassword(hi2c);
203 data = REG_GPO_DEFAULT;
204 status = HAL_I2C_Mem_Write(hi2c, ST25_ADDR_SYST, 0x0000,
205 I2C_MEMADD_SIZE_16BIT, &data, 1, 100);
206 if (status != HAL_OK)
return status;
209 data = REG_RF_MNGT_DEFAULT;
210 status = HAL_I2C_Mem_Write(hi2c, ST25_ADDR_SYST, 0x0003,
211 I2C_MEMADD_SIZE_16BIT, &data, 1, 100);
215 uint8_t empty[16] = {0};
216 for(uint16_t i = 0; i < 2048; i += 16) {
217 HAL_I2C_Mem_Write(hi2c, ST25_ADDR_USER, i,
218 I2C_MEMADD_SIZE_16BIT, empty, 16, 100);
234 HAL_StatusTypeDef status = HAL_ERROR;
235 uint8_t reg_val = value;
238 for (i = 0; i < 5; i++)
241 if (status == HAL_OK)
246 _isNfc = (status == HAL_OK);
263 HAL_StatusTypeDef status;
280 if ((status = nfc_st25dv16kc_PresentPassword(hi2c)) != HAL_OK)
287 for (i = 0; i < 5; i++)
290 if (status == HAL_OK)
294 if (status != HAL_OK)
312 HAL_StatusTypeDef status = HAL_ERROR;
318 if (status == HAL_OK && onOff != NULL)
326 HAL_StatusTypeDef status = HAL_ERROR;
343 HAL_StatusTypeDef status = HAL_ERROR;
359 if (status == HAL_OK)
371int8_t nfc_st25dv16kc_IsRFBusy(I2C_HandleTypeDef *hi2c)
381 I2C_MEMADD_SIZE_16BIT, ®_val, 1, 50) != HAL_OK)
386 return (reg_val & 0x02U) ? 1 : 0;
392 HAL_StatusTypeDef status = HAL_ERROR;
409 if (status == HAL_OK)
417 uint16_t page_offset = (addr + i) % 4;
418 uint16_t max_in_page = 4 - page_offset;
419 uint16_t remaining = len - i;
420 uint16_t chunk = remaining < max_in_page ? remaining : max_in_page;
429 }
while (++retry < 10);
431 if (status != HAL_OK)
443 HAL_StatusTypeDef status = HAL_ERROR;
470 uint16_t msg_len = (uint16_t) mb_len + 1U;
488 HAL_StatusTypeDef status = HAL_ERROR;
This file contains all the function prototypes for the i2c.c file.
HAL_StatusTypeDef I2C_IsDeviceReadyMT(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint32_t Trials, uint32_t Timeout)
Wrapper around HAL_I2C_IsDeviceReady() that recovers from a busy bus. If the HAL I2C bus is in a BUSY...
void writeLog(const char *format,...)
Format and send a log message over UART (printf-style). Available only when WRITELOG is defined; comp...
HAL_StatusTypeDef nfc_st25dv16kc_WriteMailBox(I2C_HandleTypeDef *hi2c, uint8_t *pData, uint16_t len)
Write raw data into the Mailbox so the RF reader (phone) can retrieve it.
#define ST25DV16KC_MB_SIZE
#define ST25DV16KC_GPO_ALL_EN
HAL_StatusTypeDef nfc_st25dv16kc_ReadEEPROM(I2C_HandleTypeDef *hi2c, uint16_t addr, uint8_t *pData, uint16_t len)
Read data from the EEPROM user memory.
#define ST25DV16KC_SYS_REG_I2C_PWD
#define ST25DV16KC_REG_RF_MNGT_DYN
#define ST25DV16KC_REG_MB_DATA
HAL_StatusTypeDef nfc_st25dv16kc_On(I2C_HandleTypeDef *hi2c)
Turn the NFC GPO ON (all events: MsgReady, FieldChange, RFWrite). Enables host-side reading/writing v...
#define ST25DV16KC_RF_SLEEP
HAL_StatusTypeDef nfc_st25dv16kc_ProcessMailBox(I2C_HandleTypeDef *hi2c)
Process incoming data from the RF Mailbox. Checks MB_CTRL_DYN for an RF message; if present reads it ...
#define ST25DV16KC_REG_MB_LEN_DYN
#define ST25DV16KC_I2C_ADDR_USER
HAL_StatusTypeDef nfc_st25dv16kc_Off(I2C_HandleTypeDef *hi2c)
Turn the NFC GPO OFF and enable GPO RF to achieve minimum current (~1 µA).
__weak void nfc_st25dv16kc_OnMailboxData(uint8_t *data, uint16_t len)
Weak callback invoked by nfc_st25dv16kc_ProcessMailBox() when new data has arrived in the Mailbox....
HAL_StatusTypeDef nfc_st25dv16kc_IsOn(I2C_HandleTypeDef *hi2c, uint8_t *onOff)
#define ST25DV16KC_REG_MB_CTRL_DYN
int8_t nfc_st25dv16kc_Is(I2C_HandleTypeDef *hi2c, int8_t tryInit)
Check whether the ST25DV16KC has been successfully initialised.
HAL_StatusTypeDef nfc_st25dv16kc_WriteEEPROM(I2C_HandleTypeDef *hi2c, uint16_t addr, uint8_t *pData, uint16_t len)
Write data to the EEPROM user memory (4-byte page writes).
HAL_StatusTypeDef nfc_st25dv16kc_Init(I2C_HandleTypeDef *hi2c)
Check the I2C bus and try to find the NFC chip. Enables the mailbox, clears it, and turns GPO off (lo...
static HAL_StatusTypeDef nfc_st25dv16kc_WriteGPO(I2C_HandleTypeDef *hi2c, uint8_t value)
Write a value to GPO_CTRL_DYN with retry to tolerate transient NACKs from the RF field.
#define ST25DV16KC_I2C_ADDR_SYSTEM
#define ST25DV16KC_GPO_EN
#define ST25DV16KC_MB_RF_PUT_MSG
#define ST25DV16KC_REG_GPO_CTRL_DYN
void HAL_Delay(__IO uint32_t Delay)