NNP STM Generic Remote Module git-main
Loading...
Searching...
No Matches
eedata.c
Go to the documentation of this file.
1
9//#include "applicfg.h"
10#include "ObjDict.h"
11#include "objdictdef.h"
12#include "eedata.h"
13#include <stdlib.h>
14
15//============================
16// GLOBAL CODE
17//============================
18
19uint8_t page[FLASH_PAGE_SIZE * EEPROM_PAGE_SIZE];
20uint8_t memOpen = 0;
21
28{
29 UNS8 data[2];
30
31 EEPROM_read(0x00, data, 2);
32 if ((data[0] == 0xFF && data[1] == 0xFF) || (data[0] == 0x00 && data[1] == 0x00))
33 return 0;
34 else
35 return 1;
36}
37
51void SaveValues( void )
52{
53 UNS8 nSubIndices;
54 UNS32 size = 0;
55 UNS8 type = 0;
56 UNS8 data[MAX_BYTES_PER_SUBINDEX];
57 UNS32 abortCode = 0;
58 UNS16 counter = 2; //NOTE: counter starts at 2 (0 and 1 used to store size later)
59 UNS8 i = 0;
60
62 //loop through each index in RestoreList
63 for (i = 0; i < sizeof(RestoreList); i++)
64 {
65 //don't let user save/restore below 0x1018 in OD, currently to make sure 1st subindex specifies nSubIndices
66 if (RestoreList[i] < 0x1018)
67 continue;
68
69 //need to reset the size before the next "read"
70 size = 0;
71 type = 0;
72 //read the 0th subindex (nSubIndices) at the current index
73 abortCode = readLocalDict( &ObjDict_Data, RestoreList[i], 0, &nSubIndices, &size, &type, 0);
74 if ( abortCode != OD_SUCCESSFUL )
75 break;
76
77 if (type == uint8) // First subindex must be a UNS8 see objdictdef.h for datatypes
78 {
79 //loop through each subindex and write bytes to EEPROM
80 for (int k = 1; k <= nSubIndices; k++) //the first element of subIndexSize must contain the number of subindices
81 {
82 size = 0;
83 //?what happens here if subindex has more bytes than MAX_BYTES_PER_SUBINDEX?
84 abortCode = readLocalDict( &ObjDict_Data, RestoreList[i], k, data, &size, &type, 0);
85 if ( abortCode != OD_SUCCESSFUL )
86 break;
87
88 //write all the subindex bytes to EEPROM
89 if (counter+size < MAX_EEPROM_MEMORY )
90 {
91 EEPROM_write(counter, data, size );
92 counter += size;
93 }
94 }
95 }
96 }
97 //write the number of bytes used (current counter value) in the first 2 bytes of EEPROM
98 data[0] = (UNS8) counter;
99 data[1] = (UNS8)(counter >> 8);
100 EEPROM_write(0, data, 2);
102 //NOTE: disabling/enabling interrupts is handled in EEPROM_commit routine!
103}
104
117void RestoreValues ( void )
118{
119
120 UNS8 nSubIndices;
121 UNS32 size = 0;
122 UNS8 type = 0;
123 UNS8 data[MAX_BYTES_PER_SUBINDEX];
124 UNS32 abortCode = 0;;
125 UNS16 counter = 2;
126
127 for (int i = 0; i < sizeof(RestoreList); i++)
128 {
129 //don't let user save/restore below 0x1018 in OD, currently to make sure 1st subindex specifies nSubIndices
130 if (RestoreList[i] < 0x1018)
131 continue;
132
133 size = 0;
134 type = 0;
135
136 //read the 0th subindex (nSubIndices) at the current index
137 abortCode = readLocalDict( &ObjDict_Data, RestoreList[i], 0, &nSubIndices, &size, &type, 0);
138 if ( abortCode != OD_SUCCESSFUL )
139 break;
140
141 if ( type == uint8 )
142 {
143 //loop through each subindex and read bytes from EEPROM
144 for (int k = 1; k <= nSubIndices; k++)
145 {
146 size = 0;
147 //read current values from OD to get length of subindex
148 abortCode = readLocalDict( &ObjDict_Data, RestoreList[i], k, data, &size, &type, 0);
149 if ( abortCode != OD_SUCCESSFUL )
150 break;
151
152 //read all subindex bytes from EEPROM
153 if( counter+size < MAX_EEPROM_MEMORY )
154 {
155 EEPROM_read( counter, data, size );
156 counter += size;
157 }
158 //write data to OD
159 abortCode = writeLocalDict( &ObjDict_Data, RestoreList[i], k, data, &size, 0);
160 if ( abortCode != OD_SUCCESSFUL )
161 break;
162 }
163 }
164 }
165
166
167}
168
174{
175
176 UNS8 data[2];
177 data[0] = 0x00;
178 data[1] = 0x00;
179
180 EEPROM_open();
181 EEPROM_write(0x00, data, 0x02);
183
184 ResetModule();
185}
186
191void ResetModule(void)
192{
193 __disable_irq();
194
195 NVIC_SystemReset();
196}
197
205UNS8 ReadLocalFlashData( UNS32 nvAddress, UNS8 * data, UNS8 numData )
206{
207 if (nvAddress > 0x5000 && nvAddress < (0x3FFFF))
208 {
209 memcpy( data, (UNS8 *)nvAddress , numData );
210 }
211 else if ( nvAddress <= 0x5000 ) //Bootloader region
212 {
213 memcpy( data, (UNS8 *)nvAddress , numData );
214 }
215 else
216 return 2;
217
218
219 return 0;
220}
221
222
229{
230 // Space variable can be unused, or used to define a region of the EEPROM if set up that way
231
232 UNS8 byteErase[EEPROM_ERASE_SIZE];
233 memset(byteErase, 0xFF, EEPROM_ERASE_SIZE);
234
235 EEPROM_open();
236
237 for (int i = 0; i < MAX_EEPROM_MEMORY; i += EEPROM_ERASE_SIZE){
238 EEPROM_write(i, byteErase, EEPROM_ERASE_SIZE);
239 }
240
242}
243
244//============================
245// LOCAL CODE
246//============================
247
253 if(memOpen == 0){
254 memcpy(page, (void*) EEPROM_START_ADDRESS, (FLASH_PAGE_SIZE * EEPROM_PAGE_SIZE)); // Copy the EEPROM page into memory
255 memOpen = 1;
256 return 1;
257 }
258 return 0;
259}
260
261
267 FLASH_EraseInitTypeDef eraseInitStruct;
268 uint32_t PageError = 0;
269
270 if (memOpen == 0)
271 return 0;
272
273 __disable_irq();
274 HAL_FLASH_Unlock();
275
276 //Erase the page
277 eraseInitStruct.TypeErase = TYPEERASE_PAGES; // Just erase pages
278 eraseInitStruct.Page = 127 - EEPROM_PAGE_SIZE; // Erase the EEPROM pages, leaving parameters untouched
279 eraseInitStruct.NbPages = EEPROM_PAGE_SIZE;
280
281 if (HAL_FLASHEx_Erase(&eraseInitStruct, &PageError) != HAL_OK) {
282 HAL_FLASH_Lock(); // Re-lock the flash afterwards'
283 memset(page, 0, sizeof(page));
284 memOpen = 0;
285 __enable_irq();
286 return 0;
287 }
288
289 //Rewrite the page in flash
290 for (int i = 0; i < EEPROM_PAGE_SIZE * FLASH_PAGE_SIZE; i += 8) // Program into flash in double word increments (8 bytes)
291 {
292 if (HAL_FLASH_Program(TYPEPROGRAM_DOUBLEWORD, EEPROM_START_ADDRESS + i, *(uint64_t *)(page + i)) != HAL_OK)
293 {
294 return 0;
295 }
296 }
297
298 HAL_FLASH_Lock(); // Re-lock the flash afterwards
299 __enable_irq();
300
301 memOpen = 0;
302 return 1;
303}
304
310 if (memOpen == 0)
311 return 0;
312
313 memOpen = 0;
314
315 return 1;
316}
317
327void EEPROM_write(UNS16 address, UNS8 * data, UNS16 length)
328{
329 if (memOpen == 0)
330 return;
331
332 memcpy((page + address), data, length); // Copy updated data into location in the memory copy
333}
334
335
343void EEPROM_read(UNS16 address, UNS8 * data, UNS16 length)
344{
345 memcpy(data, (void *)(address + EEPROM_START_ADDRESS), length);
346}
347
348
349//============================
350// INTERRUPT SERVICE ROUTINES
351//============================
352
353
354//============================
355// HARDWARE SPECIFIC CODE
356//============================
357
358
359
This file is generated by the NNP Tool – Object Dictionary Editor, as originally developed by CAN Fes...
#define UNS8
Unsigned int8 representation in CANFest.
Definition applicfg.h:25
#define UNS16
Unsigned int16 representation in CANFest.
Definition applicfg.h:26
#define UNS32
Unsigned int32 representation in CANFest.
Definition applicfg.h:27
#define OD_SUCCESSFUL
Definition def.h:16
#define MAX_EEPROM_MEMORY
(6KB)
Definition eedata.h:13
#define EEPROM_ERASE_SIZE
must be divisible into 6144 (6KB) by factor of 2, defines size of chunks erased from EEPROM
Definition eedata.h:15
#define EEPROM_START_ADDRESS
Loation of the start of the EEPROM in flash.
Definition eedata.h:17
#define EEPROM_PAGE_SIZE
XXX: must match the page size in bootloader config.h.
Definition eedata.h:16
UNS8 CheckRestoreFlag(void)
checks whether or not RestoreValues() should be run.
Definition eedata.c:27
void EEPROM_write(UNS16 address, UNS8 *data, UNS16 length)
Writes bytes to a specified location in EEPROM.
Definition eedata.c:327
void RestoreValues(void)
Restores the values of custom OD entries (specified in RestoreList OD index 0x2900) to the OD from EE...
Definition eedata.c:117
void EEPROM_read(UNS16 address, UNS8 *data, UNS16 length)
Reads bytes from a specified location in EEPROM.
Definition eedata.c:343
void ResetToODDefault(void)
invoked by nmt_master – writes 0's to size causing bypass of OD restore, then causes reset
Definition eedata.c:173
void ResetModule(void)
resets module, called by ResetToODDefault()
Definition eedata.c:191
UNS8 EEPROM_commit()
Commits edits to EEPROM from ram to flash.
Definition eedata.c:266
void SaveValues(void)
Saves the values of custom OD entries (specified in RestoreList OD index 0x2900) to EEPROM from the O...
Definition eedata.c:51
void EEPROM_erase(UNS8 space)
writes 0xFF to entire EEPROM (6KB).
Definition eedata.c:228
UNS8 ReadLocalFlashData(UNS32 nvAddress, UNS8 *data, UNS8 numData)
reads CPU based flash data
Definition eedata.c:205
UNS8 EEPROM_discard()
Discards current edits to EEPROM without committing to flash.
Definition eedata.c:309
UNS8 EEPROM_open()
Copies the EEPROM into ram for modification by EEPROM_write()
Definition eedata.c:252