L14-Click 1.0
STM32WLE5CC LoRaWAN Sensor Platform
Loading...
Searching...
No Matches
nfc_st25dv16kc.c File Reference
#include "i2c.h"
#include "nfc_st25dv16kc.h"
#include <string.h>
Include dependency graph for nfc_st25dv16kc.c:

Go to the source code of this file.

Macros

#define ST25DV16KC_I2C_ADDR_USER   (0x53 << 1)
#define ST25DV16KC_I2C_ADDR_SYSTEM   (0x57 << 1)
#define ST25DV16KC_REG_GPO_CTRL_DYN   0x2000U
#define ST25DV16KC_REG_EH_CTRL_DYN   0x2001U
#define ST25DV16KC_REG_RF_MNGT_DYN   0x2002U
#define ST25DV16KC_REG_I2C_SSO_DYN   0x2003U
#define ST25DV16KC_REG_MB_CTRL_DYN   0x2006U
#define ST25DV16KC_REG_MB_LEN_DYN   0x2007U
#define ST25DV16KC_REG_MB_DATA   0x2008U
#define ST25DV16KC_SYS_REG_RF_MNGT   0x0003U
#define ST25DV16KC_SYS_REG_MB_MODE   0x000DU
#define ST25DV16KC_SYS_REG_I2C_PWD   0x0900U
#define ST25DV16KC_GPO_EN   0x80U
#define ST25DV16KC_GPO_MSG_READY_EN   0x40U
#define ST25DV16KC_GPO_RF_ACTIVITY   0x20U
#define ST25DV16KC_GPO_RF_INTERRUPT   0x10U
#define ST25DV16KC_GPO_FIELD_CHANGE   0x08U
#define ST25DV16KC_GPO_RF_PUT_MSG   0x04U
#define ST25DV16KC_GPO_RF_GET_MSG   0x02U
#define ST25DV16KC_GPO_RF_WRITE   0x01U
#define ST25DV16KC_GPO_ALL_EN
#define ST25DV16KC_GPO_INT_EN   (ST25DV16KC_GPO_EN | ST25DV16KC_GPO_FIELD_CHANGE)
#define ST25DV16KC_MB_EN   0x01U
#define ST25DV16KC_MB_RF_PUT_MSG   0x02U
#define ST25DV16KC_MB_I2C_PUT_MSG   0x04U
#define ST25DV16KC_RF_SLEEP   0x01U
#define ST25DV16KC_RF_DISABLE   0x02U
#define ST25DV16KC_EH_FIELD_ON   0x04U
#define ST25DV16KC_RF_BUSY_TIMEOUT   20U
#define ST25DV16KC_MB_SIZE   256U
#define EEPROM_TIMEOUT   5000

Functions

static HAL_StatusTypeDef nfc_st25dv16kc_WriteGPO (I2C_HandleTypeDef *hi2c, uint8_t value)
 Present the default I2C password to open a security session. The payload is: [0x09 validation code] + [8-byte pwd] + [8-byte pwd]. Default password is 8 x 0x00.
int8_t nfc_st25dv16kc_Is (I2C_HandleTypeDef *hi2c, int8_t tryInit)
 Check whether the ST25DV16KC has been successfully initialised.
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 (low-power state).
HAL_StatusTypeDef nfc_st25dv16kc_IsOn (I2C_HandleTypeDef *hi2c, uint8_t *onOff)
HAL_StatusTypeDef nfc_st25dv16kc_On (I2C_HandleTypeDef *hi2c)
 Turn the NFC GPO ON (all events: MsgReady, FieldChange, RFWrite). Enables host-side reading/writing via the Mailbox.
HAL_StatusTypeDef nfc_st25dv16kc_Off (I2C_HandleTypeDef *hi2c)
 Turn the NFC GPO OFF and enable GPO RF to achieve minimum current (~1 µA).
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_ReadEEPROM (I2C_HandleTypeDef *hi2c, uint16_t addr, uint8_t *pData, uint16_t len)
 Read data from the EEPROM user memory.
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 and invokes nfc_st25dv16kc_OnMailboxData().
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.
__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. Override in main.c or application code to process the received message.

Variables

static int8_t _isNfc = 0
static int8_t _isOnOff = 1

Macro Definition Documentation

◆ EEPROM_TIMEOUT

#define EEPROM_TIMEOUT   5000

Definition at line 101 of file nfc_st25dv16kc.c.

Referenced by nfc_st25dv16kc_ReadEEPROM(), and nfc_st25dv16kc_WriteEEPROM().

◆ ST25DV16KC_EH_FIELD_ON

#define ST25DV16KC_EH_FIELD_ON   0x04U

RF field is currently detected

Definition at line 81 of file nfc_st25dv16kc.c.

◆ ST25DV16KC_GPO_ALL_EN

#define ST25DV16KC_GPO_ALL_EN
Value:
#define ST25DV16KC_GPO_MSG_READY_EN
#define ST25DV16KC_GPO_RF_WRITE
#define ST25DV16KC_GPO_EN
#define ST25DV16KC_GPO_FIELD_CHANGE

GPO value used by nfc_st25dv16kc_On(): all events enabled

Definition at line 59 of file nfc_st25dv16kc.c.

Referenced by nfc_st25dv16kc_On().

◆ ST25DV16KC_GPO_EN

#define ST25DV16KC_GPO_EN   0x80U

GPO output enable

Definition at line 49 of file nfc_st25dv16kc.c.

Referenced by nfc_st25dv16kc_IsOn(), and nfc_st25dv16kc_Off().

◆ ST25DV16KC_GPO_FIELD_CHANGE

#define ST25DV16KC_GPO_FIELD_CHANGE   0x08U

GPO on RF field change

Definition at line 53 of file nfc_st25dv16kc.c.

◆ ST25DV16KC_GPO_INT_EN

#define ST25DV16KC_GPO_INT_EN   (ST25DV16KC_GPO_EN | ST25DV16KC_GPO_FIELD_CHANGE)

GPO value used by nfc_st25dv16kc_INT_On(): field-change interrupt only

Definition at line 63 of file nfc_st25dv16kc.c.

◆ ST25DV16KC_GPO_MSG_READY_EN

#define ST25DV16KC_GPO_MSG_READY_EN   0x40U

GPO on mailbox message-ready

Definition at line 50 of file nfc_st25dv16kc.c.

◆ ST25DV16KC_GPO_RF_ACTIVITY

#define ST25DV16KC_GPO_RF_ACTIVITY   0x20U

GPO on RF activity

Definition at line 51 of file nfc_st25dv16kc.c.

◆ ST25DV16KC_GPO_RF_GET_MSG

#define ST25DV16KC_GPO_RF_GET_MSG   0x02U

GPO when RF reads mailbox

Definition at line 55 of file nfc_st25dv16kc.c.

◆ ST25DV16KC_GPO_RF_INTERRUPT

#define ST25DV16KC_GPO_RF_INTERRUPT   0x10U

GPO on RF interrupt

Definition at line 52 of file nfc_st25dv16kc.c.

◆ ST25DV16KC_GPO_RF_PUT_MSG

#define ST25DV16KC_GPO_RF_PUT_MSG   0x04U

GPO when RF writes to mailbox

Definition at line 54 of file nfc_st25dv16kc.c.

◆ ST25DV16KC_GPO_RF_WRITE

#define ST25DV16KC_GPO_RF_WRITE   0x01U

GPO on RF EEPROM write

Definition at line 56 of file nfc_st25dv16kc.c.

◆ ST25DV16KC_I2C_ADDR_SYSTEM

#define ST25DV16KC_I2C_ADDR_SYSTEM   (0x57 << 1)

System configuration

Definition at line 25 of file nfc_st25dv16kc.c.

◆ ST25DV16KC_I2C_ADDR_USER

◆ ST25DV16KC_MB_EN

#define ST25DV16KC_MB_EN   0x01U

Enable mailbox

Definition at line 68 of file nfc_st25dv16kc.c.

Referenced by nfc_st25dv16kc_Init(), and nfc_st25dv16kc_WriteMailBox().

◆ ST25DV16KC_MB_I2C_PUT_MSG

#define ST25DV16KC_MB_I2C_PUT_MSG   0x04U

I2C host has written to mailbox

Definition at line 70 of file nfc_st25dv16kc.c.

◆ ST25DV16KC_MB_RF_PUT_MSG

#define ST25DV16KC_MB_RF_PUT_MSG   0x02U

RF has written to mailbox

Definition at line 69 of file nfc_st25dv16kc.c.

Referenced by nfc_st25dv16kc_ProcessMailBox().

◆ ST25DV16KC_MB_SIZE

#define ST25DV16KC_MB_SIZE   256U

Mailbox buffer size in bytes

Definition at line 92 of file nfc_st25dv16kc.c.

Referenced by nfc_st25dv16kc_ProcessMailBox(), and nfc_st25dv16kc_WriteMailBox().

◆ ST25DV16KC_REG_EH_CTRL_DYN

#define ST25DV16KC_REG_EH_CTRL_DYN   0x2001U

Energy-harvesting ctrl (dyn)

Definition at line 31 of file nfc_st25dv16kc.c.

◆ ST25DV16KC_REG_GPO_CTRL_DYN

#define ST25DV16KC_REG_GPO_CTRL_DYN   0x2000U

GPO control (dynamic)

Definition at line 30 of file nfc_st25dv16kc.c.

Referenced by nfc_st25dv16kc_IsOn(), and nfc_st25dv16kc_WriteGPO().

◆ ST25DV16KC_REG_I2C_SSO_DYN

#define ST25DV16KC_REG_I2C_SSO_DYN   0x2003U

I2C security-session open

Definition at line 33 of file nfc_st25dv16kc.c.

◆ ST25DV16KC_REG_MB_CTRL_DYN

#define ST25DV16KC_REG_MB_CTRL_DYN   0x2006U

Mailbox control (dynamic)

Definition at line 34 of file nfc_st25dv16kc.c.

Referenced by nfc_st25dv16kc_Init(), nfc_st25dv16kc_ProcessMailBox(), and nfc_st25dv16kc_WriteMailBox().

◆ ST25DV16KC_REG_MB_DATA

#define ST25DV16KC_REG_MB_DATA   0x2008U

Mailbox RAM start (256 bytes)

Definition at line 36 of file nfc_st25dv16kc.c.

Referenced by nfc_st25dv16kc_ProcessMailBox(), and nfc_st25dv16kc_WriteMailBox().

◆ ST25DV16KC_REG_MB_LEN_DYN

#define ST25DV16KC_REG_MB_LEN_DYN   0x2007U

Mailbox message length (dyn)

Definition at line 35 of file nfc_st25dv16kc.c.

Referenced by nfc_st25dv16kc_ProcessMailBox().

◆ ST25DV16KC_REG_RF_MNGT_DYN

#define ST25DV16KC_REG_RF_MNGT_DYN   0x2002U

RF management (dynamic)

Definition at line 32 of file nfc_st25dv16kc.c.

Referenced by nfc_st25dv16kc_Off().

◆ ST25DV16KC_RF_BUSY_TIMEOUT

#define ST25DV16KC_RF_BUSY_TIMEOUT   20U

Maximum number of 5 ms wait cycles before giving up (100 ms total).

Definition at line 87 of file nfc_st25dv16kc.c.

◆ ST25DV16KC_RF_DISABLE

#define ST25DV16KC_RF_DISABLE   0x02U

Fully disable RF interface

Definition at line 76 of file nfc_st25dv16kc.c.

◆ ST25DV16KC_RF_SLEEP

#define ST25DV16KC_RF_SLEEP   0x01U

Put RF analog block to sleep

Definition at line 75 of file nfc_st25dv16kc.c.

Referenced by nfc_st25dv16kc_Off().

◆ ST25DV16KC_SYS_REG_I2C_PWD

#define ST25DV16KC_SYS_REG_I2C_PWD   0x0900U

I2C password register

Definition at line 44 of file nfc_st25dv16kc.c.

◆ ST25DV16KC_SYS_REG_MB_MODE

#define ST25DV16KC_SYS_REG_MB_MODE   0x000DU

Mailbox enable (static)

Definition at line 43 of file nfc_st25dv16kc.c.

◆ ST25DV16KC_SYS_REG_RF_MNGT

#define ST25DV16KC_SYS_REG_RF_MNGT   0x0003U

RF_MNGT static register

Definition at line 42 of file nfc_st25dv16kc.c.

Function Documentation

◆ nfc_st25dv16kc_Init()

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 (low-power state).

Parameters
hi2cPointer to I2C handle.
Return values
HAL_OK– NFC found and initialised, HAL_ERROR – not on the bus.

< User memory / EEPROM

< Enable mailbox

< User memory / EEPROM

< Mailbox control (dynamic)

< User memory / EEPROM

< Mailbox control (dynamic)

Definition at line 161 of file nfc_st25dv16kc.c.

References _isNfc, HAL_Delay(), I2C_IsDeviceReadyMT(), nfc_st25dv16kc_Off(), ST25DV16KC_I2C_ADDR_USER, ST25DV16KC_MB_EN, and ST25DV16KC_REG_MB_CTRL_DYN.

Referenced by nfc_st25dv16kc_Is().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ nfc_st25dv16kc_Is()

int8_t nfc_st25dv16kc_Is ( I2C_HandleTypeDef * hi2c,
int8_t tryInit )

Check whether the ST25DV16KC has been successfully initialised.

Parameters
hi2cPointer to I2C handle.
tryInitWhen 1, attempt Init() if not yet initialised; 0 – no retry.
Return values
1– NFC present and ready, 0 – not available.

Definition at line 154 of file nfc_st25dv16kc.c.

References _isNfc, and nfc_st25dv16kc_Init().

Here is the call graph for this function:

◆ nfc_st25dv16kc_IsOn()

HAL_StatusTypeDef nfc_st25dv16kc_IsOn ( I2C_HandleTypeDef * hi2c,
uint8_t * onOff )

< User memory / EEPROM

< GPO control (dynamic)

< GPO output enable

Definition at line 208 of file nfc_st25dv16kc.c.

References _isNfc, ST25DV16KC_GPO_EN, ST25DV16KC_I2C_ADDR_USER, and ST25DV16KC_REG_GPO_CTRL_DYN.

◆ nfc_st25dv16kc_Off()

HAL_StatusTypeDef nfc_st25dv16kc_Off ( I2C_HandleTypeDef * hi2c)

Turn the NFC GPO OFF and enable GPO RF to achieve minimum current (~1 µA).

Parameters
hi2cPointer to I2C handle.
Return values
HAL_OKon success, HAL_ERROR if NFC not available.

< GPO output enable

< Put RF analog block to sleep

< User memory / EEPROM

< RF management (dynamic)

Definition at line 239 of file nfc_st25dv16kc.c.

References _isNfc, _isOnOff, nfc_st25dv16kc_WriteGPO(), ST25DV16KC_GPO_EN, ST25DV16KC_I2C_ADDR_USER, ST25DV16KC_REG_RF_MNGT_DYN, and ST25DV16KC_RF_SLEEP.

Referenced by nfc_st25dv16kc_Init().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ nfc_st25dv16kc_On()

HAL_StatusTypeDef nfc_st25dv16kc_On ( I2C_HandleTypeDef * hi2c)

Turn the NFC GPO ON (all events: MsgReady, FieldChange, RFWrite). Enables host-side reading/writing via the Mailbox.

Parameters
hi2cPointer to I2C handle.
Return values
HAL_OKon success, HAL_ERROR if NFC not available.

< GPO output enable

< GPO on mailbox message-ready

< GPO on RF field change

< GPO on RF EEPROM write

Definition at line 222 of file nfc_st25dv16kc.c.

References _isNfc, _isOnOff, nfc_st25dv16kc_WriteGPO(), and ST25DV16KC_GPO_ALL_EN.

Here is the call graph for this function:

◆ nfc_st25dv16kc_OnMailboxData()

__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. Override in main.c or application code to process the received message.

Parameters
dataPointer to received data buffer.
lenNumber of bytes received.

Definition at line 409 of file nfc_st25dv16kc.c.

Referenced by nfc_st25dv16kc_ProcessMailBox().

Here is the caller graph for this function:

◆ nfc_st25dv16kc_ProcessMailBox()

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 and invokes nfc_st25dv16kc_OnMailboxData().

Parameters
hi2cPointer to I2C handle.
Return values
HAL_OK– message read, HAL_ERROR – no message or NFC absent.

< User memory / EEPROM

< Mailbox control (dynamic)

< RF has written to mailbox

< User memory / EEPROM

< Mailbox message length (dyn)

< Mailbox buffer size in bytes

< Mailbox buffer size in bytes

< Mailbox buffer size in bytes

< User memory / EEPROM

< Mailbox RAM start (256 bytes)

Definition at line 348 of file nfc_st25dv16kc.c.

References _isNfc, nfc_st25dv16kc_OnMailboxData(), ST25DV16KC_I2C_ADDR_USER, ST25DV16KC_MB_RF_PUT_MSG, ST25DV16KC_MB_SIZE, ST25DV16KC_REG_MB_CTRL_DYN, ST25DV16KC_REG_MB_DATA, and ST25DV16KC_REG_MB_LEN_DYN.

Here is the call graph for this function:

◆ nfc_st25dv16kc_ReadEEPROM()

HAL_StatusTypeDef nfc_st25dv16kc_ReadEEPROM ( I2C_HandleTypeDef * hi2c,
uint16_t addr,
uint8_t * pData,
uint16_t len )

Read data from the EEPROM user memory.

Parameters
hi2cPointer to I2C handle.
addrStart address within EEPROM.
pDataPointer to destination buffer.
lenNumber of bytes to read.
Return values
HAL_OKon success, HAL_ERROR on failure or NFC absent.

< User memory / EEPROM

Definition at line 339 of file nfc_st25dv16kc.c.

References _isNfc, EEPROM_TIMEOUT, and ST25DV16KC_I2C_ADDR_USER.

◆ nfc_st25dv16kc_WriteEEPROM()

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).

Parameters
hi2cPointer to I2C handle.
addrStart address within EEPROM (0x0000–0x07FF for 16Kbit).
pDataPointer to data buffer to write.
lenNumber of bytes to write.
Return values
HAL_OKon success, HAL_ERROR on failure or NFC absent.

< User memory / EEPROM

Definition at line 288 of file nfc_st25dv16kc.c.

References _isNfc, EEPROM_TIMEOUT, HAL_Delay(), and ST25DV16KC_I2C_ADDR_USER.

Here is the call graph for this function:

◆ nfc_st25dv16kc_WriteGPO()

HAL_StatusTypeDef nfc_st25dv16kc_WriteGPO ( I2C_HandleTypeDef * hi2c,
uint8_t value )
static

Present the default I2C password to open a security session. The payload is: [0x09 validation code] + [8-byte pwd] + [8-byte pwd]. Default password is 8 x 0x00.

Parameters
hi2cPointer to I2C handle.
Return values
HAL_StatusTypeDefresult of the I2C write.

static HAL_StatusTypeDef nfc_st25dv16kc_PresentPassword(I2C_HandleTypeDef *hi2c) { HAL_StatusTypeDef status; uint8_t pwd_payload[17]; pwd_payload[0] = 0x09; // Validation code

memset(&pwd_payload[1], 0x00, 16); // 8-byte pwd repeated twice (default)

status = HAL_I2C_Mem_Write(hi2c, ST25DV16KC_I2C_ADDR_SYSTEM, ST25DV16KC_SYS_REG_I2C_PWD, I2C_MEMADD_SIZE_16BIT, pwd_payload, 17, 200); return status; }

Write a value to GPO_CTRL_DYN with retry to tolerate transient NACKs from the RF field.

Parameters
hi2cPointer to I2C handle.
valueByte value to write to GPO_CTRL_DYN.
Return values
HAL_StatusTypeDefresult of the last I2C write attempt.

< User memory / EEPROM

< GPO control (dynamic)

Definition at line 132 of file nfc_st25dv16kc.c.

References _isNfc, HAL_Delay(), ST25DV16KC_I2C_ADDR_USER, and ST25DV16KC_REG_GPO_CTRL_DYN.

Referenced by nfc_st25dv16kc_Off(), and nfc_st25dv16kc_On().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ nfc_st25dv16kc_WriteMailBox()

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.

Parameters
hi2cPointer to I2C handle.
pDataPointer to data buffer.
lenNumber of bytes to write (max ST25DV16KC_MB_SIZE).
Return values
HAL_OKon success, HAL_ERROR on failure or NFC absent.

< Mailbox buffer size in bytes

< Mailbox buffer size in bytes

< Enable mailbox

< User memory / EEPROM

< Mailbox control (dynamic)

< User memory / EEPROM

< Mailbox RAM start (256 bytes)

Definition at line 384 of file nfc_st25dv16kc.c.

References _isNfc, ST25DV16KC_I2C_ADDR_USER, ST25DV16KC_MB_EN, ST25DV16KC_MB_SIZE, ST25DV16KC_REG_MB_CTRL_DYN, and ST25DV16KC_REG_MB_DATA.

Variable Documentation

◆ _isNfc

◆ _isOnOff

int8_t _isOnOff = 1
static