31
31
/*
32
32
* Defines
33
33
*/
34
- #define FLASH_SECTORS 12
35
- #define BOOTLOADER_BASE 11
36
- #define APP_BASE 0
37
- #define NEW_APP_BASE 8
38
- #define NEW_APP_SECTORS 3
39
- #define APP_MAX_SIZE (3 * (1 << 17))
40
- #define EEPROM_EMULATION_SIZE 0x8000
34
+ #define FLASH_SECTORS 12
35
+ #define BOOTLOADER_BASE 11
36
+ #define APP_BASE 0
37
+ #define NEW_APP_BASE 8
38
+ #define NEW_APP_SECTORS 3
39
+ #define APP_MAX_SIZE (393216 - 8) // Note that the bootloader needs 8 extra bytes
41
40
42
41
// Base address of the Flash sectors
43
- #define ADDR_FLASH_SECTOR_0 ((uint32_t)0x08000000) // Base @ of Sector 0, 16 Kbytes
44
- #define ADDR_FLASH_SECTOR_1 ((uint32_t)0x08004000) // Base @ of Sector 1, 16 Kbytes
45
- #define ADDR_FLASH_SECTOR_2 ((uint32_t)0x08008000) // Base @ of Sector 2, 16 Kbytes
46
- #define ADDR_FLASH_SECTOR_3 ((uint32_t)0x0800C000) // Base @ of Sector 3, 16 Kbytes
47
- #define ADDR_FLASH_SECTOR_4 ((uint32_t)0x08010000) // Base @ of Sector 4, 64 Kbytes
48
- #define ADDR_FLASH_SECTOR_5 ((uint32_t)0x08020000) // Base @ of Sector 5, 128 Kbytes
49
- #define ADDR_FLASH_SECTOR_6 ((uint32_t)0x08040000) // Base @ of Sector 6, 128 Kbytes
50
- #define ADDR_FLASH_SECTOR_7 ((uint32_t)0x08060000) // Base @ of Sector 7, 128 Kbytes
51
- #define ADDR_FLASH_SECTOR_8 ((uint32_t)0x08080000) // Base @ of Sector 8, 128 Kbytes
52
- #define ADDR_FLASH_SECTOR_9 ((uint32_t)0x080A0000) // Base @ of Sector 9, 128 Kbytes
53
- #define ADDR_FLASH_SECTOR_10 ((uint32_t)0x080C0000) // Base @ of Sector 10, 128 Kbytes
54
- #define ADDR_FLASH_SECTOR_11 ((uint32_t)0x080E0000) // Base @ of Sector 11, 128 Kbytes
55
-
56
- #define APP_CRC_WAS_CALCULATED_FLAG ((uint32_t)0xAAAAAAAA)
57
- #define APP_CRC_WAS_CALCULATED_FLAG_ADDRESS (uint32_t*)(APP_MAX_SIZE - 8)
58
-
59
- #define VECTOR_TABLE_ADDRESS ((uint32_t *)ADDR_FLASH_SECTOR_0)
60
- #define VECTOR_TABLE_SIZE ((uint32_t)(ADDR_FLASH_SECTOR_1 - ADDR_FLASH_SECTOR_0))
61
-
62
- #define APP_START_ADDRESS ((uint32_t *)(ADDR_FLASH_SECTOR_1 + EEPROM_EMULATION_SIZE))
63
- #define APP_SIZE ((uint32_t)(APP_MAX_SIZE - VECTOR_TABLE_SIZE - EEPROM_EMULATION_SIZE))
42
+ #define ADDR_FLASH_SECTOR_0 ((uint32_t)0x08000000) // Base @ of Sector 0, 16 Kbytes
43
+ #define ADDR_FLASH_SECTOR_1 ((uint32_t)0x08004000) // Base @ of Sector 1, 16 Kbytes
44
+ #define ADDR_FLASH_SECTOR_2 ((uint32_t)0x08008000) // Base @ of Sector 2, 16 Kbytes
45
+ #define ADDR_FLASH_SECTOR_3 ((uint32_t)0x0800C000) // Base @ of Sector 3, 16 Kbytes
46
+ #define ADDR_FLASH_SECTOR_4 ((uint32_t)0x08010000) // Base @ of Sector 4, 64 Kbytes
47
+ #define ADDR_FLASH_SECTOR_5 ((uint32_t)0x08020000) // Base @ of Sector 5, 128 Kbytes
48
+ #define ADDR_FLASH_SECTOR_6 ((uint32_t)0x08040000) // Base @ of Sector 6, 128 Kbytes
49
+ #define ADDR_FLASH_SECTOR_7 ((uint32_t)0x08060000) // Base @ of Sector 7, 128 Kbytes
50
+ #define ADDR_FLASH_SECTOR_8 ((uint32_t)0x08080000) // Base @ of Sector 8, 128 Kbytes
51
+ #define ADDR_FLASH_SECTOR_9 ((uint32_t)0x080A0000) // Base @ of Sector 9, 128 Kbytes
52
+ #define ADDR_FLASH_SECTOR_10 ((uint32_t)0x080C0000) // Base @ of Sector 10, 128 Kbytes
53
+ #define ADDR_FLASH_SECTOR_11 ((uint32_t)0x080E0000) // Base @ of Sector 11, 128 Kbytes
54
+
55
+ #define VECTOR_TABLE_ADDRESS ((uint32_t*)ADDR_FLASH_SECTOR_0)
56
+ #define VECTOR_TABLE_SIZE ((uint32_t)(ADDR_FLASH_SECTOR_1 - ADDR_FLASH_SECTOR_0))
57
+ #define EEPROM_EMULATION_SIZE ((uint32_t)(ADDR_FLASH_SECTOR_4 - ADDR_FLASH_SECTOR_2))
58
+
59
+ #define APP_START_ADDRESS ((uint32_t*)(ADDR_FLASH_SECTOR_3))
60
+ #define APP_SIZE ((uint32_t)(APP_MAX_SIZE - VECTOR_TABLE_SIZE - EEPROM_EMULATION_SIZE))
61
+
62
+ #define APP_CRC_WAS_CALCULATED_FLAG ((uint32_t)0x00000000)
63
+ #define APP_CRC_WAS_CALCULATED_FLAG_ADDRESS ((uint32_t*)(ADDR_FLASH_SECTOR_0 + APP_MAX_SIZE - 8))
64
+ #define APP_CRC_ADDRESS ((uint32_t*)(ADDR_FLASH_SECTOR_0 + APP_MAX_SIZE - 4))
64
65
65
66
typedef struct {
66
67
uint32_t crc_flag ;
@@ -225,55 +226,53 @@ uint32_t flash_helper_verify_flash_memory(void) {
225
226
uint32_t crc ;
226
227
// Look for a flag indicating that the CRC was previously computed.
227
228
// If it is blank (0xFFFFFFFF), calculate and store the CRC.
228
- if ( (APP_CRC_WAS_CALCULATED_FLAG_ADDRESS )[0 ] == APP_CRC_WAS_CALCULATED_FLAG )
229
- {
229
+ if (APP_CRC_WAS_CALCULATED_FLAG_ADDRESS [0 ] == APP_CRC_WAS_CALCULATED_FLAG ) {
230
230
RCC_AHB1PeriphClockCmd (RCC_AHB1Periph_CRC , ENABLE );
231
231
crc32_reset ();
232
232
233
233
// compute vector table (sector 0)
234
- crc32 (( VECTOR_TABLE_ADDRESS ) , (VECTOR_TABLE_SIZE )/ 4 );
234
+ crc32 (VECTOR_TABLE_ADDRESS , (VECTOR_TABLE_SIZE ) / 4 );
235
235
236
236
// skip emulated EEPROM (sector 1 and 2)
237
237
238
238
// compute application code
239
- crc = crc32 (APP_START_ADDRESS , (APP_SIZE )/ 4 );
239
+ crc = crc32 (APP_START_ADDRESS , (APP_SIZE ) / 4 );
240
240
241
241
RCC_AHB1PeriphClockCmd (RCC_AHB1Periph_CRC , DISABLE );
242
242
243
243
// A CRC over the full image should return zero.
244
- return (crc == 0 )? FAULT_CODE_NONE : FAULT_CODE_FLASH_CORRUPTION ;
245
- }
246
- else {
244
+ return (crc == 0 ) ? FAULT_CODE_NONE : FAULT_CODE_FLASH_CORRUPTION ;
245
+ } else {
247
246
FLASH_Unlock ();
248
247
FLASH_ClearFlag (FLASH_FLAG_OPERR | FLASH_FLAG_WRPERR | FLASH_FLAG_PGAERR |
249
- FLASH_FLAG_PGPERR | FLASH_FLAG_PGSERR );
248
+ FLASH_FLAG_PGPERR | FLASH_FLAG_PGSERR );
250
249
251
- // Write the flag to indicate CRC has been computed.
252
- uint16_t res = FLASH_ProgramWord ((uint32_t )APP_CRC_WAS_CALCULATED_FLAG_ADDRESS , APP_CRC_WAS_CALCULATED_FLAG );
253
- if (res != FLASH_COMPLETE ) {
254
- FLASH_Lock ();
255
- return FAULT_CODE_FLASH_CORRUPTION ;
256
- }
250
+ // Write the flag to indicate CRC has been computed.
251
+ uint16_t res = FLASH_ProgramWord ((uint32_t )APP_CRC_WAS_CALCULATED_FLAG_ADDRESS , APP_CRC_WAS_CALCULATED_FLAG );
252
+ if (res != FLASH_COMPLETE ) {
253
+ FLASH_Lock ();
254
+ return FAULT_CODE_FLASH_CORRUPTION ;
255
+ }
257
256
258
- // Compute flash crc including the new flag
257
+ // Compute flash crc including the new flag
259
258
RCC_AHB1PeriphClockCmd (RCC_AHB1Periph_CRC , ENABLE );
260
259
crc32_reset ();
261
260
262
261
// compute vector table (sector 0)
263
- crc32 (VECTOR_TABLE_ADDRESS , (VECTOR_TABLE_SIZE )/ 4 );
262
+ crc32 (VECTOR_TABLE_ADDRESS , (VECTOR_TABLE_SIZE ) / 4 );
264
263
265
264
// skip emulated EEPROM (sector 1 and 2)
266
265
267
266
// compute application code
268
- crc = crc32 (APP_START_ADDRESS , (APP_SIZE - 4 )/ 4 );
267
+ crc = crc32 (APP_START_ADDRESS , (APP_SIZE - 4 ) / 4 );
269
268
270
269
RCC_AHB1PeriphClockCmd (RCC_AHB1Periph_CRC , DISABLE );
271
270
272
271
//Store CRC
273
- res = FLASH_ProgramWord (APP_MAX_SIZE - 4 , crc );
272
+ res = FLASH_ProgramWord (( uint32_t ) APP_CRC_ADDRESS , crc );
274
273
if (res != FLASH_COMPLETE ) {
275
274
FLASH_Lock ();
276
- return FAULT_CODE_FLASH_CORRUPTION ;
275
+ return FAULT_CODE_FLASH_CORRUPTION ;
277
276
}
278
277
FLASH_Lock ();
279
278
@@ -285,29 +284,33 @@ uint32_t flash_helper_verify_flash_memory(void) {
285
284
286
285
uint32_t flash_helper_verify_flash_memory_chunk (void ) {
287
286
static uint32_t index = 0 ;
288
- const uint32_t chunk_size = 8192 ;
287
+ uint32_t chunk_size = 1024 ;
289
288
uint32_t res = FAULT_CODE_NONE ;
290
289
uint32_t crc = 0 ;
290
+ uint32_t tot_bytes = VECTOR_TABLE_SIZE + APP_SIZE ;
291
291
292
292
// Make sure RCC_AHB1Periph_CRC is enabled
293
293
if (index == 0 ) {
294
294
crc32_reset ();
295
295
}
296
296
297
- if (index < VECTOR_TABLE_SIZE ) {
298
- crc32 (( VECTOR_TABLE_ADDRESS + index ), chunk_size / 4 ) ;
297
+ if (( index + chunk_size ) >= tot_bytes ) {
298
+ chunk_size = tot_bytes - index ;
299
299
}
300
- else {
301
- crc = crc32 ((uint32_t * )((uint32_t )APP_START_ADDRESS + index - VECTOR_TABLE_SIZE ), chunk_size /4 );
300
+
301
+ if (index < VECTOR_TABLE_SIZE ) {
302
+ crc32 (VECTOR_TABLE_ADDRESS + index / 4 , chunk_size / 4 );
303
+ } else {
304
+ crc = crc32 (APP_START_ADDRESS + (index - VECTOR_TABLE_SIZE ) / 4 , chunk_size / 4 );
302
305
}
303
306
304
307
index += chunk_size ;
305
- if (index >= ( VECTOR_TABLE_SIZE + APP_SIZE ) ) {
308
+ if (index >= tot_bytes ) {
306
309
index = 0 ;
307
310
if (crc != 0 ) {
308
311
res = FAULT_CODE_FLASH_CORRUPTION ;
309
312
}
310
313
}
314
+
311
315
return res ;
312
316
}
313
-
0 commit comments