SRD Standelone programmation box: MemoryManager.c Source File

| SRD Standelone programmation box 2.0 (16/11/15 10:00:00) Documentation of the SRD standelone programmation box

- Main Page
- Data Structures
- Files

- File List
- Globals

All Data Structures Files Functions Variables Enumerations Enumerator Macros Pages
MemoryManager.c
Go to the documentation of this file.
1 /*############################################################################################*/
2 /*############################################################################################*/
3 /**
4 * \file MemoryManager.c
5 * \author Thibaut LALUQUE 3D-Ouest
6 * \date 24/11/2014
7 * \version 1.0
8 * \brief Source file for Memory management.
9 * \details All Right Reserved.
10 */
11 /*############################################################################################*/
12 /*############################################################################################*/
13
14
15 /*############################################################################################*/
16 /*## INCLUDES ################################################################################*/
17 /*############################################################################################*/
18 #include "MemoryManager.h"
19 /*############################################################################################*/
20 /*## END INCLUDES ############################################################################*/
21 /*############################################################################################*/
22
23
24 /*############################################################################################*/
25 /*## LOCAL DEFINES ###########################################################################*/
26 /*############################################################################################*/
27
28 /*############################################################################################*/
29 /*## END LOCAL DEFINES ######################################################################*/
30 /*############################################################################################*/
31
32
33 /*############################################################################################*/
34 /*## LOCAL FONCTIONS DECLARATION #############################################################*/
35 /*############################################################################################*/
36
37 /*############################################################################################*/
38 /*## END LOCAL FONCTIONS DECLARATION #########################################################*/
39 /*############################################################################################*/
40
41
42 /*############################################################################################*/
43 /*## LOCAL VARIABLES #########################################################################*/
44 /*############################################################################################*/
45
46 /*############################################################################################*/
47 /*## END LOCAL VARIABLES #####################################################################*/
48 /*############################################################################################*/
49
50 /*############################################################################################*/
51 /*## GLOBAL VARIABLES ########################################################################*/
52 /*############################################################################################*/
53 tMemoryManagerCtrl MemoryManagerCtrl;
54 tMemoryManagerDiving DivingParameters;
55 tMemoryManagerSerial SerialParameters;
56 /*############################################################################################*/
57 /*## END GLOBAL VARIABLES ####################################################################*/
58 /*############################################################################################*/
59
60
61 /*############################################################################################*/
62 /*## LOCAL FONCTIONS #########################################################################*/
63 /*############################################################################################*/
64
65 /*############################################################################################*/
66 /*## END LOCAL FONCTIONS #####################################################################*/
67 /*############################################################################################*/
68
69
70 /*############################################################################################*/
71 /*## EXTERNAL FONCTIONS ######################################################################*/
72 /*############################################################################################*/
73 void MemoryManagerInit (void) {
74
75 FM25V20Init();
76 WatchdogKick();
77
78 MemoryManagerCtrl.isValid.all = 0; //At start-up all is false
79
80 signed short result = MemoryManagerReadDiving(&DivingParameters);
81
82 if(result == 0) {
83 MemoryManagerCheckDiving(&DivingParameters);
84 } else {
85 MemoryManagerCtrl.isValid.bits.diving = 0;
86 }
87
88
89 result = MemoryManagerReadSerial(&SerialParameters);
90
91 if(result == 0) {
92 MemoryManagerCheckSerial(&SerialParameters);
93 } else {
94 MemoryManagerCtrl.isValid.bits.serial = 0;
95 }
96
97 result = MemoryManagerComputeNbrLog ();
98 if(result != 0) {
99 MemoryManagerCtrl.nbrLog = 0;
100 }
101 }
102
103 signed short MemoryManagerReadDiving (tMemoryManagerDiving* divingParameters) {
104 signed short result;
105
106 tFM25V20_LPMState LPM = FM25V20IsInLowPower();
107
108 /*=== Wake up device if it's in lpm ===*/
109 if(LPM == FM25V20_LowPowerMode) {
110 FM25V20WakeUp();
111 WatchdogKick();
112 }
113
114 result = FM25V20ReadData(MEMORYMANAGER_ADDR_DIVING_START, (unsigned char*)divingParameters, MEMORYMANAGER_DIVING_BLOCK_SIZE);
115
116 /*=== restore low power mode ===*/
117 if(LPM == FM25V20_LowPowerMode) {
118 FM25V20LPM();
119 }
120
121 if(result == 0) {
122 return 0;
123 } else {
124 return -1;
125 }
126 }
127
128 void MemoryManagerCheckDiving (tMemoryManagerDiving* divingParameters) {
129
130 unsigned char checksumExpected = ChecksumByteCompute((unsigned char*)divingParameters, MEMORYMANAGER_DIVING_BLOCK_SIZE-1);
131
132 if(checksumExpected != divingParameters->checksum || (divingParameters->depth == 0 && divingParameters->filterLSB == 0 && divingParameters->filterMSB == 0) ) {
133 MemoryManagerCtrl.isValid.bits.diving = 0;
134 } else {
135 MemoryManagerCtrl.isValid.bits.diving = 1;
136 }
137 }
138
139 signed short MemoryManagerWriteDiving (tMemoryManagerDiving* divingParameters) {
140 signed short result;
141
142 tFM25V20_LPMState LPM = FM25V20IsInLowPower();
143
144 /*=== Wake up device if it's in lpm ===*/
145 if(LPM == FM25V20_LowPowerMode) {
146 FM25V20WakeUp();
147 WatchdogKick();
148 }
149
150 divingParameters->checksum = ChecksumByteCompute((unsigned char*)divingParameters, MEMORYMANAGER_DIVING_BLOCK_SIZE-1);
151
152 result = FM25V20WriteData(MEMORYMANAGER_ADDR_DIVING_START, (unsigned char*)divingParameters, MEMORYMANAGER_DIVING_BLOCK_SIZE);
153
154 /*=== restore low power mode ===*/
155 if(LPM == FM25V20_LowPowerMode) {
156 FM25V20LPM();
157 }
158
159 if(result == 0 && (divingParameters->depth != 0 && (divingParameters->filterLSB != 0 || divingParameters->filterMSB != 0)) ) {
160 MemoryManagerCtrl.isValid.bits.diving = 1;
161 return 0;
162 } else {
163 MemoryManagerCtrl.isValid.bits.diving = 0;
164 return -1;
165 }
166 }
167
168 signed short MemoryManagerReadSerial (tMemoryManagerSerial* serialParameters) {
169 signed short result;
170
171 tFM25V20_LPMState LPM = FM25V20IsInLowPower();
172
173 /*=== Wake up device if it's in lpm ===*/
174 if(LPM == FM25V20_LowPowerMode) {
175 FM25V20WakeUp();
176 WatchdogKick();
177 }
178
179 result = FM25V20ReadData(MEMORYMANAGER_ADDR_SERIAL_START, (unsigned char*)serialParameters, MEMORYMANAGER_SERIAL_BLOCK_SIZE);
180
181 /*=== restore low power mode ===*/
182 if(LPM == FM25V20_LowPowerMode) {
183 FM25V20LPM();
184 }
185
186 if(result == 0) {
187 return 0;
188 } else {
189 return -1;
190 }
191 }
192
193 void MemoryManagerCheckSerial (tMemoryManagerSerial* serialParameters) {
194
195 unsigned char checksumExpected = ChecksumByteCompute((unsigned char*)serialParameters, MEMORYMANAGER_SERIAL_BLOCK_SIZE-1);
196
197 if(checksumExpected != serialParameters->checksum || (serialParameters->serial == 0)) {
198 MemoryManagerCtrl.isValid.bits.serial = 0;
199 } else {
200 MemoryManagerCtrl.isValid.bits.serial = 1;
201 }
202 }
203
204 signed short MemoryManagerWriteSerial (tMemoryManagerSerial* serialParameters) {
205 signed short result;
206
207 tFM25V20_LPMState LPM = FM25V20IsInLowPower();
208
209 /*=== Wake up device if it's in lpm ===*/
210 if(LPM == FM25V20_LowPowerMode) {
211 FM25V20WakeUp();
212 WatchdogKick();
213 }
214
215 serialParameters->checksum = ChecksumByteCompute((unsigned char*)serialParameters, MEMORYMANAGER_SERIAL_BLOCK_SIZE-1);
216
217 result = FM25V20WriteData(MEMORYMANAGER_ADDR_SERIAL_START, (unsigned char*)serialParameters, MEMORYMANAGER_SERIAL_BLOCK_SIZE);
218
219 /*=== restore low power mode ===*/
220 if(LPM == FM25V20_LowPowerMode) {
221 FM25V20LPM();
222 }
223
224 if(result == 0 && serialParameters->serial != 0) {
225 MemoryManagerCtrl.isValid.bits.serial = 1;
226 return 0;
227 } else {
228 MemoryManagerCtrl.isValid.bits.serial = 0;
229 return -1;
230 }
231 }
232
233 signed short MemoryManagerWriteLog(unsigned long date, tMemoryManagerRWOp opType, tSRDEeprom* data) {
234
235 tMemoryManagerLog log;
236 unsigned char i;
237
238 if(MemoryManagerCtrl.nbrLog < MEMORYMANAGER_MAX_NBR_LOG) {
239
240 log.date = date;
241 log.logOp = opType;
242
243 for(i=0 ; i<SRD_MEMORY_NBR_BYTE ; i++) {
244 *( ((unsigned char*)&log) + 8 + i) = *( ( (unsigned char*)data ) + i );
245 }
246
247 unsigned long checksum = ChecksumByteOnShortCompute((unsigned char*)(&log),MEMORYMANAGER_LOG_SIZE);
248 log.logOp |= (checksum<<16);
249
250 signed short result;
251
252 tFM25V20_LPMState LPM = FM25V20IsInLowPower();
253
254 /*=== Wake up device if it's in lpm ===*/
255 if(LPM == FM25V20_LowPowerMode) {
256 FM25V20WakeUp();
257 WatchdogKick();
258 }
259
260 unsigned long memAddr = MEMORYMANAGER_ADDR_LOG_START + (MemoryManagerCtrl.nbrLog * MEMORYMANAGER_LOG_SIZE);
261
262 result = FM25V20WriteData(memAddr, (unsigned char*)(&log), MEMORYMANAGER_LOG_SIZE);
263
264 /*=== restore low power mode ===*/
265 if(LPM == FM25V20_LowPowerMode) {
266 FM25V20LPM();
267 }
268
269 if(result == 0) {
270 MemoryManagerCtrl.nbrLog++;
271 return 0;
272 } else {
273 return -1;
274 }
275 } else {
276 return -1;
277 }
278 }
279
280 signed short MemoryManagerClearLog(void) {
281 signed short result = 0;
282
283 tFM25V20_LPMState LPM = FM25V20IsInLowPower();
284
285 /*=== Wake up device if it's in lpm ===*/
286 if(LPM == FM25V20_LowPowerMode) {
287 FM25V20WakeUp();
288 WatchdogKick();
289 }
290
291 MemoryManagerCtrl.nbrLog = 0;
292
293 unsigned char data[MEMORYMANAGER_LOG_SIZE] = {0};
294
295 //Erase
296 while(result == 0 && MemoryManagerCtrl.nbrLog < MEMORYMANAGER_MAX_NBR_LOG) {
297 WatchdogKick();
298 result = FM25V20WriteData(MEMORYMANAGER_ADDR_LOG_START + (MemoryManagerCtrl.nbrLog * MEMORYMANAGER_LOG_SIZE), data, MEMORYMANAGER_LOG_SIZE);
299 MemoryManagerCtrl.nbrLog++;
300 }
301
302 MemoryManagerCtrl.nbrLog = 0; //Clear log counter
303
304 /*=== restore low power mode ===*/
305 if(LPM == FM25V20_LowPowerMode) {
306 FM25V20LPM();
307 }
308
309 if(result == 0) {
310 return 0;
311 } else {
312 return -1;
313 }
314 }
315
316 signed short MemoryManagerComputeNbrLog (void) {
317
318 signed short result = 0;
319
320 tFM25V20_LPMState LPM = FM25V20IsInLowPower();
321
322 /*=== Wake up device if it's in lpm ===*/
323 if(LPM == FM25V20_LowPowerMode) {
324 FM25V20WakeUp();
325 WatchdogKick();
326 }
327
328 MemoryManagerCtrl.nbrLog = 0;
329 unsigned long data = 0xFFFFFFFF;
330
331
332 while(result == 0 && MemoryManagerCtrl.nbrLog < MEMORYMANAGER_MAX_NBR_LOG && data != 0) {
333 WatchdogKick();
334 result = FM25V20ReadData(MEMORYMANAGER_ADDR_LOG_START + (MemoryManagerCtrl.nbrLog * MEMORYMANAGER_LOG_SIZE), (unsigned char*)&data, 4);
335 MemoryManagerCtrl.nbrLog++;
336 }
337
338 /*=== restore low power mode ===*/
339 if(LPM == FM25V20_LowPowerMode) {
340 FM25V20LPM();
341 }
342
343 if(result == -1) {
344 return -1;
345 } else {
346 return 0;
347 }
348 }
349
350 signed short MemoryManagerReadLog(unsigned short addr, tMemoryManagerLog* data) {
351
352 signed short result;
353
354 if(addr < MemoryManagerCtrl.nbrLog) {
355 tFM25V20_LPMState LPM = FM25V20IsInLowPower();
356
357 /*=== Wake up device if it's in lpm ===*/
358 if(LPM == FM25V20_LowPowerMode) {
359 FM25V20WakeUp();
360 WatchdogKick();
361 }
362
363 unsigned long memAddr = MEMORYMANAGER_ADDR_LOG_START + (addr * MEMORYMANAGER_LOG_SIZE);
364
365 result = FM25V20ReadData(memAddr, (unsigned char*)data, MEMORYMANAGER_LOG_SIZE);
366
367 unsigned long checksumSave = data->logOp;
368 checksumSave = (checksumSave >> 16) & 0x0000FFFF;
369
370 data->logOp &= 0x0000FFFF;
371
372 unsigned long checksum = ChecksumByteOnShortCompute((unsigned char*)data,MEMORYMANAGER_LOG_SIZE);
373
374 data->logOp |= (checksumSave<<16);
375
376 if(checksum != checksumSave) {
377 result = -1;
378 }
379
380 /*=== restore low power mode ===*/
381 if(LPM == FM25V20_LowPowerMode) {
382 FM25V20LPM();
383 }
384
385 return result;
386
387 } else {
388 return -1;
389 }
390 }
391 /*############################################################################################*/
392 /*## END EXTERNAL FONCTIONS ##################################################################*/
393 /*############################################################################################*/
MemoryManagerReadLog
signed short MemoryManagerReadLog(unsigned short addr, tMemoryManagerLog *data)
Definition: MemoryManager.c:350
FM25V20Init
void FM25V20Init(void)
Function used to initialize the FM25V20 memory.
Definition: FM25V20.c:67
tMemoryManagerSerial
Define a structure used to save the serial parameters.
Definition: MemoryManager.h:100
tMemoryManagerDiving::checksum
unsigned char checksum
Definition: MemoryManager.h:91
SRD_MEMORY_NBR_BYTE
#define SRD_MEMORY_NBR_BYTE
Define the number of byte used in the SRD RFID memory.
Definition: SRDMemoryMapping.h:106
tMemoryManagerSerial::checksum
unsigned char checksum
Definition: MemoryManager.h:102
MemoryManagerReadSerial
signed short MemoryManagerReadSerial(tMemoryManagerSerial *serialParameters)
function used read/write and check the serial parameters in the memory.
Definition: MemoryManager.c:168
tMemoryManagerDiving::filterLSB
unsigned char filterLSB
Definition: MemoryManager.h:90
FM25V20WriteData
signed char FM25V20WriteData(unsigned long addr, unsigned char data[], unsigned long dataLength)
Write data to the memory.
Definition: FM25V20.c:199
ChecksumByteOnShortCompute
unsigned short ChecksumByteOnShortCompute(unsigned char *data, unsigned long dataLength)
Definition: checksum.c:111
WatchdogKick
void WatchdogKick(void)
Kick the watchdog to avoid reset after the watchdog have started.
Definition: watchdog.c:86
MemoryManagerCtrl
tMemoryManagerCtrl MemoryManagerCtrl
Variable used to control the memory manager.
Definition: MemoryManager.c:53
MemoryManagerWriteDiving
signed short MemoryManagerWriteDiving(tMemoryManagerDiving *divingParameters)
Definition: MemoryManager.c:139
MemoryManagerInit
void MemoryManagerInit(void)
function use to initialize the memory manager.
Definition: MemoryManager.c:73
DivingParameters
tMemoryManagerDiving DivingParameters
Variable used to control the diving parameters.
Definition: MemoryManager.c:54
tMemoryManagerLog
Define a structure used to store a log in memory.
Definition: MemoryManager.h:140
MEMORYMANAGER_ADDR_SERIAL_START
#define MEMORYMANAGER_ADDR_SERIAL_START
Define memory mapping of the log and control structure.
Definition: MemoryManager.h:48
FM25V20LPM
void FM25V20LPM(void)
Put the memory in a low power mode.
Definition: FM25V20.c:178
MemoryManagerCheckDiving
void MemoryManagerCheckDiving(tMemoryManagerDiving *divingParameters)
Definition: MemoryManager.c:128
MemoryManagerReadDiving
signed short MemoryManagerReadDiving(tMemoryManagerDiving *divingParameters)
function used read/write and check the diving parameters in the memory.
Definition: MemoryManager.c:103
FM25V20_LowPowerMode
Definition: FM25V20.h:162
tMemoryManagerCtrl
Define a structure used to control the memory operation.
Definition: MemoryManager.h:122
tMemoryManagerRWOp
tMemoryManagerRWOp
Define the log operation (write or read operation).
Definition: MemoryManager.h:111
MemoryManagerWriteSerial
signed short MemoryManagerWriteSerial(tMemoryManagerSerial *serialParameters)
Definition: MemoryManager.c:204
MEMORYMANAGER_MAX_NBR_LOG
#define MEMORYMANAGER_MAX_NBR_LOG
Define the maximum number of log that we can save in the memory.
Definition: MemoryManager.h:70
MEMORYMANAGER_ADDR_DIVING_START
#define MEMORYMANAGER_ADDR_DIVING_START
Define memory mapping of the log and control structure.
Definition: MemoryManager.h:40
tMemoryManagerLog::logOp
unsigned long logOp
Definition: MemoryManager.h:142
SerialParameters
tMemoryManagerSerial SerialParameters
Variable used to control the serial parameters.
Definition: MemoryManager.c:55
tMemoryManagerDiving::depth
unsigned char depth
Definition: MemoryManager.h:88
MemoryManager.h
Headerfile for Memory manager.
tMemoryManagerCtrl::nbrLog
unsigned long nbrLog
Definition: MemoryManager.h:131
MEMORYMANAGER_LOG_SIZE
#define MEMORYMANAGER_LOG_SIZE
Define memory mapping of the log and control structure.
Definition: MemoryManager.h:61
MemoryManagerWriteLog
signed short MemoryManagerWriteLog(unsigned long date, tMemoryManagerRWOp opType, tSRDEeprom *data)
function used read/write and check the log in the memory.
Definition: MemoryManager.c:233
tSRDEeprom
Structure used to represent and store SRD RFID memory.
Definition: SRDMemoryMapping.h:130
tMemoryManagerDiving
Define a structure used to save the diving parameters.
Definition: MemoryManager.h:87
MEMORYMANAGER_DIVING_BLOCK_SIZE
#define MEMORYMANAGER_DIVING_BLOCK_SIZE
Define memory mapping of the log and control structure.
Definition: MemoryManager.h:45
tMemoryManagerDiving::filterMSB
unsigned char filterMSB
Definition: MemoryManager.h:89
MEMORYMANAGER_ADDR_LOG_START
#define MEMORYMANAGER_ADDR_LOG_START
Define memory mapping of the log and control structure.
Definition: MemoryManager.h:60
MEMORYMANAGER_SERIAL_BLOCK_SIZE
#define MEMORYMANAGER_SERIAL_BLOCK_SIZE
Define memory mapping of the log and control structure.
Definition: MemoryManager.h:58
tMemoryManagerCtrl::isValid
union tMemoryManagerCtrl::@2 isValid
MemoryManagerCheckSerial
void MemoryManagerCheckSerial(tMemoryManagerSerial *serialParameters)
Definition: MemoryManager.c:193
FM25V20IsInLowPower
tFM25V20_LPMState FM25V20IsInLowPower(void)
Get the low power status of the memory.
Definition: FM25V20.c:244
tMemoryManagerLog::date
unsigned long date
Definition: MemoryManager.h:141
MemoryManagerClearLog
signed short MemoryManagerClearLog(void)
Definition: MemoryManager.c:280
tMemoryManagerCtrl::bits
struct tMemoryManagerCtrl::@2::@3 bits
ChecksumByteCompute
unsigned char ChecksumByteCompute(unsigned char *data, unsigned long dataLength)
Compute and return a checksum on the right data type.
Definition: checksum.c:78
MemoryManagerComputeNbrLog
signed short MemoryManagerComputeNbrLog(void)
Return the number of log stored in memory.
Definition: MemoryManager.c:316
tFM25V20_LPMState
tFM25V20_LPMState
enumeration used to set the memory power mode.
Definition: FM25V20.h:161
FM25V20WakeUp
void FM25V20WakeUp(void)
Wake up the memory from the low power mode.
Definition: FM25V20.c:189
tMemoryManagerSerial::serial
unsigned long long serial
Definition: MemoryManager.h:101
FM25V20ReadData
signed char FM25V20ReadData(unsigned long addr, unsigned char data[], unsigned long dataLength)
Read data from the memory.
Definition: FM25V20.c:224
tMemoryManagerCtrl::all
unsigned short all
Definition: MemoryManager.h:129