L14-Click 1.0
STM32WLE5CC LoRaWAN Sensor Platform
Loading...
Searching...
No Matches
nfc.c
Go to the documentation of this file.
1/*
2 * nfc.c
3 *
4 * Created on: 9. 3. 2026
5 * Author: Milan
6 */
7
8#include "nfc.h"
9#include "nfc_st25dv16kc.h"
10//#include "nfc_st25r3.h"
11#include "mysensors.h"
12#include "mysensors_base.h"
13#include "main.h"
14#include <string.h>
15#define NFC_ADDR 0
16
17static systemParams_t _systemParamsBck = {}; // helper - bckUp
18
19#ifdef INC_NFC_ST25R3_H_
20#define NFC_INIT nfc_st25r3_Init
21#define NFC_IS nfc_st25r3_Is
22#define NFC_READ nfc_st25r3_ReadEEPROM
23#define NFC_ON nfc_st25r3_On
24#define NFC_OFF nfc_st25r3_Off
25#define NFC_WRITE nfc_st25r3_WriteEEPROM
26#define NFC_MAILBOX nfc_st25r3_ProcessMailBox
27#endif
28
29#ifdef INC_NFC_ST25DV16KC_H_
30#define NFC_INIT nfc_st25dv16kc_Init
31#define NFC_IS nfc_st25dv16kc_Is
32#define NFC_READ nfc_st25dv16kc_ReadEEPROM
33#define NFC_ON nfc_st25dv16kc_On
34#define NFC_OFF nfc_st25dv16kc_Off
35#define NFC_WRITE nfc_st25dv16kc_WriteEEPROM
36#define NFC_MAILBOX nfc_st25dv16kc_ProcessMailBox
37#endif
38
39
40/**
41 * @brief reading data from NFC to _systemParamsBck
42 * @retval HAL_OK - the block has beep OK, HAL_ERROR NFC chip read error, HAL_TIMEOUT - CRC non valid, HAL_BUSY - the sign/version is not valid
43 */
44static HAL_StatusTypeDef nfc_ReadSystemParamsToBck(I2C_HandleTypeDef *hi2c)
45{
46 HAL_StatusTypeDef status = HAL_ERROR;
47
48 if (NFC_IS(hi2c, _tryInit))
49 {
50 do
51 {
52 if ((status = NFC_READ(hi2c, NFC_ADDR, (uint8_t*) &_systemParamsBck, sizeof(_systemParamsBck))) != HAL_OK)
53 break;
54
56 {
57 status = HAL_TIMEOUT; // CRC non valid
58 break;
59 }
60
61 if (strcmp(_systemParamsBck.Sign, SYSTEMPARAMS_SIGN) != 0 || _systemParamsBck.AppVersion[1] != SYSTEMPARAMS_APPVER01)
62 {
63 status = HAL_BUSY; // the sign/version is not valid
64 break;
65 }
66 } while (0);
67 }
68 return status;
69}
70
71HAL_StatusTypeDef nfc_Init(I2C_HandleTypeDef *hi2c)
72{
73 HAL_StatusTypeDef status = NFC_INIT(hi2c);
74
75 if (status == HAL_OK)
76 {
77 status = nfc_ReadSystemParamsToBck(hi2c);
78 switch (status)
79 {
80 case HAL_OK: // all is OK
81 // all is OK, can be rewrite main block
83 systemParams_Correction(); // correction in data
84 writeLog("nfc_ReadSystemParams, ok");
85 break;
86 case HAL_TIMEOUT:
87 writeLog("nfc_ReadSystemParams, CRC non valid");
88 status = nfc_WriteSystemParams(hi2c); // write current system block
89 break;
90 case HAL_BUSY:
91 writeLog("nfc_ReadSystemParams, sign non valid");
92 status = nfc_WriteSystemParams(hi2c); // write current system block
93 break;
94 case HAL_ERROR:
95 break;
96 }
97 }
98 return status;
99}
100
101HAL_StatusTypeDef nfc_WriteSystemParams(I2C_HandleTypeDef *hi2c)
102{
103 HAL_StatusTypeDef status = HAL_ERROR;
104
105 i2c_OnOff(1);
106 if (NFC_IS(hi2c, _tryInit))
107 {
109
110 status = NFC_ON(hi2c);
111 if (status == HAL_OK)
112 {
113 status = NFC_WRITE(hi2c, NFC_ADDR, (uint8_t*) &_systemParams, sizeof(_systemParams));
114 NFC_OFF(hi2c);
115 }
116 }
117 i2c_OnOff(0);
118 return status;
119}
120
121HAL_StatusTypeDef nfc_FactoryReset(I2C_HandleTypeDef *hi2c)
122{
123 HAL_StatusTypeDef status = HAL_ERROR;
124
125 i2c_OnOff(1);
126 if (NFC_IS(hi2c, _tryInit))
127 {
128 status = NFC_ON(hi2c);
129 if (status == HAL_OK)
130 {
131 const char buf[] = "XXXX";
132 status = NFC_WRITE(hi2c, NFC_ADDR, (uint8_t*) buf, sizeof(buf));
133 NFC_OFF(hi2c);
134 }
135 }
136 i2c_OnOff(0);
137 return status;
138}
139
140HAL_StatusTypeDef nfc_ReadSystemParams(I2C_HandleTypeDef *hi2c)
141{
142 HAL_StatusTypeDef status = HAL_ERROR;
143
144 i2c_OnOff(1);
145 if (NFC_IS(hi2c, _tryInit))
146 {
147 do
148 {
149 if ((status = nfc_ReadSystemParamsToBck(hi2c)) != HAL_OK)
150 break;
152 } while(0);
153 }
154 i2c_OnOff(0);
155 return status;
156}
157
158
159HAL_StatusTypeDef nfc_ProcessMailBox(I2C_HandleTypeDef *hi2c)
160{
161 return NFC_MAILBOX(hi2c);
162}
: Header for main.c file. This file contains the common defines of the application.
void writeLog(const char *format,...)
Format and send a log message over UART (printf-style). Available only when WRITELOG is defined; comp...
Definition main.c:97
void i2c_OnOff(uint8_t onOff)
possible to I2C turn on/off - cumulative
Definition mysensors.c:184
systemParams_t _systemParams
void systemParams_SetCRCSystemParams()
Recalculate and store the CRC in _systemParams.Crc. Call this after modifying any field of _systemPar...
uint8_t systemParams_CheckCRC(const systemParams_t *par)
Verify the CRC field of a systemParams_t structure.
int8_t _tryInit
#define SYSTEMPARAMS_SIGN
4-byte ASCII signature stored at offset 0 of systemParams_t to validate data integrity.
#define SYSTEMPARAMS_APPVER01
Application firmware major version number (plain integer) - check of validity struct together with SY...
void systemParams_Correction()
Clamp all RW fields of _systemParams to their valid ranges. Called after loading parameters from NFC ...
#define NFC_ADDR
Definition nfc.c:15
static HAL_StatusTypeDef nfc_ReadSystemParamsToBck(I2C_HandleTypeDef *hi2c)
reading data from NFC to _systemParamsBck
Definition nfc.c:44
#define NFC_READ
Definition nfc.c:32
HAL_StatusTypeDef nfc_WriteSystemParams(I2C_HandleTypeDef *hi2c)
Write system params to NFC.
Definition nfc.c:101
#define NFC_ON
Definition nfc.c:33
#define NFC_MAILBOX
Definition nfc.c:36
HAL_StatusTypeDef nfc_ProcessMailBox(I2C_HandleTypeDef *hi2c)
processing if NFC INT is fired
Definition nfc.c:159
#define NFC_WRITE
Definition nfc.c:35
HAL_StatusTypeDef nfc_ReadSystemParams(I2C_HandleTypeDef *hi2c)
reading _systemParams from NFC - all data will be rewrite in _systemParams
Definition nfc.c:140
HAL_StatusTypeDef nfc_FactoryReset(I2C_HandleTypeDef *hi2c)
reset NFC data to factory - default state
Definition nfc.c:121
static systemParams_t _systemParamsBck
Definition nfc.c:17
#define NFC_INIT
Definition nfc.c:30
HAL_StatusTypeDef nfc_Init(I2C_HandleTypeDef *hi2c)
NFC initialization and read NFC tag systemParams from addr 0. If data there are not valid,...
Definition nfc.c:71
#define NFC_OFF
Definition nfc.c:34
#define NFC_IS
Definition nfc.c:31
System configuration parameters – persisted in NFC EEPROM at address 0. The structure contains all mo...