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
13#include "main.h"
14#include "utils/mydefs.h"
15#include <string.h>
16#define NFC_ADDR 0
17
18
19
20#ifdef INC_NFC_ST25R3_H_
21#define NFC_INIT nfc_st25r3_Init
22#define NFC_IS nfc_st25r3_Is
23#define NFC_READ nfc_st25r3_ReadEEPROM
24#define NFC_ON nfc_st25r3_On
25#define NFC_OFF nfc_st25r3_Off
26#define NFC_WRITE nfc_st25r3_WriteEEPROM
27#define NFC_MAILBOX nfc_st25r3_ProcessMailBox
28#endif
29
30#ifdef INC_NFC_ST25DV16KC_H_
31#define NFC_INIT nfc_st25dv16kc_Init
32#define NFC_IS nfc_st25dv16kc_Is
33#define NFC_READ nfc_st25dv16kc_ReadEEPROM
34#define NFC_ON nfc_st25dv16kc_On
35#define NFC_OFF nfc_st25dv16kc_Off
36#define NFC_WRITE nfc_st25dv16kc_WriteEEPROM
37#define NFC_MAILBOX nfc_st25dv16kc_ProcessMailBox
38#endif
39
40
41/**
42 * @brief reading data from NFC to _systemParamsBck
43 * @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
44 */
45static HAL_StatusTypeDef nfc_ReadSystemParamsToBck(I2C_HandleTypeDef *hi2c)
46{
47 HAL_StatusTypeDef status = HAL_ERROR;
48
49 if (NFC_IS(hi2c, _tryInit))
50 {
51 do
52 {
53 if ((status = NFC_READ(hi2c, NFC_ADDR, (uint8_t*) &_systemParamsBck, sizeof(_systemParamsBck))) != HAL_OK)
54 break;
55
57 {
58 status = HAL_TIMEOUT; // CRC non valid
59 break;
60 }
61
62 if (strcmp(_systemParamsBck.Sign, SYSTEMPARAMS_SIGN) != 0 || _systemParamsBck.AppVersion[1] != SYSTEMPARAMS_APPVER01)
63 {
64 status = HAL_BUSY; // the sign/version is not valid
65 break;
66 }
67 } while (0);
68 }
69 return status;
70}
71
72static HAL_StatusTypeDef nfc_WriteMeasureNFC(I2C_HandleTypeDef *hi2c)
73{
74 HAL_StatusTypeDef status = HAL_ERROR;
75
76 i2c_OnOff(1);
77 if (NFC_IS(hi2c, _tryInit))
78 {
79 status = NFC_ON(hi2c);
80 if (status == HAL_OK)
81 {
83 status = NFC_WRITE(hi2c, MEASUREDATANFC_ADDR, (uint8_t*) &_measureNFC, sizeof(_measureNFC));
84 NFC_OFF(hi2c);
85 }
86 }
87 i2c_OnOff(0);
88 return status;
89}
90
91static HAL_StatusTypeDef nfc_ReadMeasureNFC(I2C_HandleTypeDef *hi2c)
92{
93 HAL_StatusTypeDef status = HAL_ERROR;
94
95 i2c_OnOff(1);
96 if (NFC_IS(hi2c, _tryInit))
97 {
98 status = NFC_ON(hi2c);
99 if (status == HAL_OK)
100 {
101 status = NFC_READ(hi2c, MEASUREDATANFC_ADDR, (uint8_t*) &_measureNFC, sizeof(_measureNFC));
102 NFC_OFF(hi2c);
103 }
104 }
105 i2c_OnOff(0);
106 return status;
107}
108
109HAL_StatusTypeDef nfc_Init(I2C_HandleTypeDef *hi2c)
110{
111 HAL_StatusTypeDef status = NFC_INIT(hi2c);
112
113 if (status == HAL_OK)
114 {
115 status = nfc_ReadSystemParamsToBck(hi2c);
116 switch (status)
117 {
118 case HAL_OK: // all is OK
119 // all is OK, can be rewrite main block
121 systemParams_Correction(); // correction in data
122 writeLog("nfc_ReadSystemParams, ok");
123 if (nfc_ReadMeasureNFC(hi2c) != HAL_OK || !sensorsBase_CheckCRCMeasureNFC())
124 {
125 writeLog("sensorsBase_CheckCRCMeasureNFC, CRC non valid");
128 }
129 break;
130 case HAL_TIMEOUT:
131 writeLog("nfc_ReadSystemParams, CRC non valid");
132 status = nfc_WriteSystemParams(hi2c); // write current system block
135 break;
136 case HAL_BUSY:
137 writeLog("nfc_ReadSystemParams, sign non valid");
138 status = nfc_WriteSystemParams(hi2c); // write current system block
141 break;
142 case HAL_ERROR:
143 break;
144 }
145 }
146 return status;
147}
148
149HAL_StatusTypeDef nfc_WriteSystemParams(I2C_HandleTypeDef *hi2c)
150{
151 HAL_StatusTypeDef status = HAL_ERROR;
152
153 i2c_OnOff(1);
154 if (NFC_IS(hi2c, _tryInit))
155 {
157
158 status = NFC_ON(hi2c);
159 if (status == HAL_OK)
160 {
161 status = NFC_WRITE(hi2c, NFC_ADDR, (uint8_t*) &_systemParams, sizeof(_systemParams));
162 NFC_OFF(hi2c);
163 }
164 }
165 i2c_OnOff(0);
166 return status;
167}
168
169HAL_StatusTypeDef nfc_FactoryReset(I2C_HandleTypeDef *hi2c)
170{
171 HAL_StatusTypeDef status = HAL_ERROR;
172
173 i2c_OnOff(1);
174 if (NFC_IS(hi2c, _tryInit))
175 {
176 status = NFC_ON(hi2c);
177 if (status == HAL_OK)
178 {
179 const char buf[] = "XXXX";
180 status = NFC_WRITE(hi2c, NFC_ADDR, (uint8_t*) buf, sizeof(buf));
181 status = NFC_WRITE(hi2c, MEASUREDATANFC_ADDR, (uint8_t*) buf, sizeof(buf));
182 NFC_OFF(hi2c);
183 }
184 }
185 i2c_OnOff(0);
186 return status;
187}
188
189HAL_StatusTypeDef nfc_ReadSystemParams(I2C_HandleTypeDef *hi2c)
190{
191 HAL_StatusTypeDef status = HAL_ERROR;
192
193 i2c_OnOff(1);
194 if (NFC_IS(hi2c, _tryInit))
195 {
196 do
197 {
198 if ((status = nfc_ReadSystemParamsToBck(hi2c)) != HAL_OK)
199 break;
201 } while(0);
202 }
203 i2c_OnOff(0);
204 return status;
205}
206
207
208HAL_StatusTypeDef nfc_WriteMeasureData(I2C_HandleTypeDef *hi2c, const measureDataNFC_t* data)
209{
210 HAL_StatusTypeDef status = HAL_ERROR;
211
212 i2c_OnOff(1);
213 if (NFC_IS(hi2c, _tryInit))
214 {
215 do
216 {
217 uint32_t dataNfcAdd = MEASUREDATANFC_ADDR + sizeof(measureNFC_t);
218
219 // 1. read control block from NFC
220 if (nfc_ReadMeasureNFC(hi2c) != HAL_OK || !sensorsBase_CheckCRCMeasureNFC())
221 {
222 writeLog("nfc_WriteMeasureData, CRC non valid");
224 }
225
226 // 2. write measure data on next address
227 dataNfcAdd += _measureNFC.Inx * sizeof(measureDataNFC_t);
228 writeLog("nfc_WriteMeasureData: addr:%d, inx:%d", (int)dataNfcAdd, (int)_measureNFC.Inx);
229 if ((status = NFC_WRITE(hi2c, dataNfcAdd, (uint8_t*) data, sizeof(measureDataNFC_t))) != HAL_OK)
230 break;
231 _measureNFC.Inx = INX_GET(_measureNFC.Inx + 1, _measureNFC.MaxCount); // next index
232 if (_measureNFC.Count < _measureNFC.MaxCount)
233 _measureNFC.Count++;
234
235 // 3. write NFC control block
236 if ((status = nfc_WriteMeasureNFC(hi2c)) != HAL_OK)
237 break;
238 } while(0);
239 writeLog("nfc_WriteMeasureData: %s", (status == HAL_OK) ? "OK" : "failed");
240 }
241 i2c_OnOff(0);
242 return status;
243}
244
245
246HAL_StatusTypeDef nfc_ProcessMailBox(I2C_HandleTypeDef *hi2c)
247{
248 return NFC_MAILBOX(hi2c);
249}
: 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:105
#define INX_GET(nInx, nMax)
The circular index GET - for circular queue both direction Example usage:
Definition mydefs.h:65
void i2c_OnOff(uint8_t onOff)
possible to I2C turn on/off - cumulative
Definition mysensors.c:190
systemParams_t _systemParams
void sensorsBase_SetCRCMeasureNFC()
Recalculate and store the CRC in _measureNFC.Crc. Call this after modifying any field of _measureNFC ...
void systemParams_SetCRCSystemParams()
Recalculate and store the CRC in _systemParams.Crc. Call this after modifying any field of _systemPar...
void sensorsBase_MeasureNFCInit()
the default initialization of _measureNFC struct
uint8_t sensorsBase_CheckCRCMeasureNFC()
Verify the CRC field of a _measureNFC structure.
systemParams_t _systemParamsBck
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 MEASUREDATANFC_ADDR
The address, on which is the begin of measured data in NFC. The address is modulo 4,...
measureNFC_t _measureNFC
#define NFC_ADDR
Definition nfc.c:16
static HAL_StatusTypeDef nfc_ReadSystemParamsToBck(I2C_HandleTypeDef *hi2c)
reading data from NFC to _systemParamsBck
Definition nfc.c:45
#define NFC_READ
Definition nfc.c:33
HAL_StatusTypeDef nfc_WriteSystemParams(I2C_HandleTypeDef *hi2c)
Write system params to NFC.
Definition nfc.c:149
static HAL_StatusTypeDef nfc_ReadMeasureNFC(I2C_HandleTypeDef *hi2c)
Definition nfc.c:91
#define NFC_ON
Definition nfc.c:34
#define NFC_MAILBOX
Definition nfc.c:37
HAL_StatusTypeDef nfc_ProcessMailBox(I2C_HandleTypeDef *hi2c)
processing if NFC INT is fired
Definition nfc.c:246
#define NFC_WRITE
Definition nfc.c:36
HAL_StatusTypeDef nfc_ReadSystemParams(I2C_HandleTypeDef *hi2c)
reading _systemParams from NFC - all data will be rewrite in _systemParams
Definition nfc.c:189
static HAL_StatusTypeDef nfc_WriteMeasureNFC(I2C_HandleTypeDef *hi2c)
Definition nfc.c:72
HAL_StatusTypeDef nfc_FactoryReset(I2C_HandleTypeDef *hi2c)
reset NFC data to factory - default state
Definition nfc.c:169
#define NFC_INIT
Definition nfc.c:31
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:109
#define NFC_OFF
Definition nfc.c:35
#define NFC_IS
Definition nfc.c:32
HAL_StatusTypeDef nfc_WriteMeasureData(I2C_HandleTypeDef *hi2c, const measureDataNFC_t *data)
write the measure data paylod in measureDataNFC_t to NFC on next free position - in circular queue
Definition nfc.c:208
block of one measure data in CayennelLPP payload format The Data field contains the same payload as i...
the control block of NFC on address MEASUREDATANFC_ADDR where are stored the last measure data The EE...