comparison OtherSources/firmwareEraseProgram.c @ 5:e65d01b6a17e

MOVE files for other applications
author JeanDo
date Fri, 15 Dec 2017 01:45:20 +0100
parents
children 7801c5d8a562
comparison
equal deleted inserted replaced
4:89a87ddc2e47 5:e65d01b6a17e
1 /**
2 ******************************************************************************
3 * @file firmwareEraseProgram.c
4 * @author heinrichs/weikamp, Christian Weikamp
5 * @version V0.0.1
6 * @date 05-May-2015
7 * @version V0.0.1
8 * @since 05-May-2015
9 * @brief erase and program the STM32F4xx internal FLASH memory
10 *
11 @verbatim
12 ==============================================================================
13 ##### How to use #####
14 ==============================================================================
15 ADDR_FLASH_SECTOR_0 to/with ADDR_FLASH_SECTOR_5 (256KB) is used for this bootloader
16
17 ADDR_FLASH_SECTOR_23 is blocked and used for Font T48 and image_heinrichs_weikamp
18 Font T24 for button text is not blocked / protected
19 other fonts should not be used here
20
21
22 ==============================================================================
23 ##### From AN2557 #####
24 STM32F10xxx In-Application programming CD00161640.pdf 2010
25 ==============================================================================
26 User program conditions
27 The user application to be loaded into the Flash memory using IAP should be built with
28 these configuration settings:
29 1. Set the program load address at 0x08003000, using your toolchain linker file
30 2. Relocate the vector table at address 0x08003000, using the
31 "NVIC_SetVectorTable"function or the VECT_TAB_OFFSET definition inside the
32 "system_stm32f10x.c"
33
34 can be found here system_stm32f4xx.c
35
36
37 @endverbatim
38 ******************************************************************************
39 * @attention
40 *
41 * <h2><center>&copy; COPYRIGHT(c) 2015 heinrichs weikamp</center></h2>
42 *
43 ******************************************************************************
44 */
45
46 /* Includes ------------------------------------------------------------------*/
47 #include "stm32f4xx_hal.h"
48 #include "stdio.h"
49 #include "firmwareEraseProgram.h"
50 #include "settings.h" // to access SHardwareData
51
52 /* Exported variables --------------------------------------------------------*/
53
54 /* Private types -------------------------------------------------------------*/
55
56 /* Base address of the Flash sectors Bank 1 */
57 #define ADDR_FLASH_SECTOR_0 ((uint32_t)0x08000000) /* Base @ of Sector 0, 16 Kbytes */
58 #define ADDR_FLASH_SECTOR_1 ((uint32_t)0x08004000) /* Base @ of Sector 1, 16 Kbytes */
59 #define ADDR_FLASH_SECTOR_2 ((uint32_t)0x08008000) /* Base @ of Sector 2, 16 Kbytes */
60 #define ADDR_FLASH_SECTOR_3 ((uint32_t)0x0800C000) /* Base @ of Sector 3, 16 Kbytes */
61 #define ADDR_FLASH_SECTOR_4 ((uint32_t)0x08010000) /* Base @ of Sector 4, 64 Kbytes */
62 #define ADDR_FLASH_SECTOR_5 ((uint32_t)0x08020000) /* Base @ of Sector 5, 128 Kbytes */
63 #define ADDR_FLASH_SECTOR_6 ((uint32_t)0x08040000) /* Base @ of Sector 6, 128 Kbytes */
64 #define ADDR_FLASH_SECTOR_7 ((uint32_t)0x08060000) /* Base @ of Sector 7, 128 Kbytes */
65 #define ADDR_FLASH_SECTOR_8 ((uint32_t)0x08080000) /* Base @ of Sector 8, 128 Kbytes */
66 #define ADDR_FLASH_SECTOR_9 ((uint32_t)0x080A0000) /* Base @ of Sector 9, 128 Kbytes */
67 #define ADDR_FLASH_SECTOR_10 ((uint32_t)0x080C0000) /* Base @ of Sector 10, 128 Kbytes */
68 #define ADDR_FLASH_SECTOR_11 ((uint32_t)0x080E0000) /* Base @ of Sector 11, 128 Kbytes */
69
70 /* Base address of the Flash sectors Bank 2 */
71 #define ADDR_FLASH_SECTOR_12 ((uint32_t)0x08100000) /* Base @ of Sector 0, 16 Kbytes */
72 #define ADDR_FLASH_SECTOR_13 ((uint32_t)0x08104000) /* Base @ of Sector 1, 16 Kbytes */
73 #define ADDR_FLASH_SECTOR_14 ((uint32_t)0x08108000) /* Base @ of Sector 2, 16 Kbytes */
74 #define ADDR_FLASH_SECTOR_15 ((uint32_t)0x0810C000) /* Base @ of Sector 3, 16 Kbytes */
75 #define ADDR_FLASH_SECTOR_16 ((uint32_t)0x08110000) /* Base @ of Sector 4, 64 Kbytes */
76 #define ADDR_FLASH_SECTOR_17 ((uint32_t)0x08120000) /* Base @ of Sector 5, 128 Kbytes */
77 #define ADDR_FLASH_SECTOR_18 ((uint32_t)0x08140000) /* Base @ of Sector 6, 128 Kbytes */
78 #define ADDR_FLASH_SECTOR_19 ((uint32_t)0x08160000) /* Base @ of Sector 7, 128 Kbytes */
79 #define ADDR_FLASH_SECTOR_20 ((uint32_t)0x08180000) /* Base @ of Sector 8, 128 Kbytes */
80 #define ADDR_FLASH_SECTOR_21 ((uint32_t)0x081A0000) /* Base @ of Sector 9, 128 Kbytes */
81 #define ADDR_FLASH_SECTOR_22 ((uint32_t)0x081C0000) /* Base @ of Sector 10, 128 Kbytes */
82 #define ADDR_FLASH_SECTOR_23 ((uint32_t)0x081E0000) /* Base @ of Sector 11, 128 Kbytes */
83
84 #define SECTOR_SIZE_128KB ((uint32_t)0x00020000)
85
86 #define FLASH_FW_START_ADDR ADDR_FLASH_SECTOR_6
87 #define FLASH_FW_END_ADDR (ADDR_FLASH_SECTOR_12 - 1)
88
89 #define FLASH_FW2_START_ADDR ADDR_FLASH_SECTOR_12
90 #define FLASH_FW2_END_ADDR (ADDR_FLASH_SECTOR_22 + SECTOR_SIZE_128KB - 1)
91
92 /* Private variables ---------------------------------------------------------*/
93
94 static FLASH_EraseInitTypeDef EraseInitStruct; /*Variable used for Erase procedure*/
95
96 uint32_t FirstSector = 0, NbOfSectors = 0, Address = 0;
97 uint32_t SectorError = 0;
98 __IO uint32_t data32 = 0 , MemoryProgramStatus = 0;
99
100
101
102 /* Private function prototypes -----------------------------------------------*/
103 //static void firmware_Error_Handler(HAL_StatusTypeDef reason);
104 static uint32_t GetSector(uint32_t Address);
105 uint8_t hardware_programm_sub(uint8_t *buffer64, uint8_t length, uint32_t startAddress);
106
107 /* Exported functions --------------------------------------------------------*/
108
109 const SHardwareData* hardwareDataGetPointer(void)
110 {
111 return (SHardwareData*)HARDWAREDATA_ADDRESS;
112 }
113
114 uint8_t hardware_programmPrimaryBluetoothNameSet(void)
115 {
116 uint8_t data = 0xF0;
117 return hardware_programm_sub(&data, 1, HARDWAREDATA_ADDRESS + 7);
118 }
119
120
121 uint8_t hardware_programmSecondaryBluetoothNameSet(void)
122 {
123 uint8_t data = 0xF0;
124 return hardware_programm_sub(&data, 1, HARDWAREDATA_ADDRESS + 52 + 7);
125 }
126
127
128 uint8_t hardware_programmProductionData(uint8_t *buffer52)
129 {
130 buffer52[7] = 0xFF;// production_bluetooth_name_set
131 return hardware_programm_sub(buffer52, 52, HARDWAREDATA_ADDRESS);// check base_bootloader.c of OSTC4bootloader code and settings.h
132 }
133
134
135 uint8_t hardware_programmSecondarySerial(uint8_t *buffer12)
136 {
137 buffer12[7] = 0xFF;// secondary_bluetooth_name_set
138 return hardware_programm_sub(buffer12, 12, HARDWAREDATA_ADDRESS + 52);
139 }
140
141
142 uint8_t hardware_programm_sub(uint8_t *buffer, uint8_t length, uint32_t startAddress)
143 {
144 HAL_StatusTypeDef answer;
145
146 uint32_t ptr = 0;
147 uint8_t data8;
148
149 // test empty
150 Address = startAddress;
151 for(int i=0;i<length;i++)
152 {
153 if((*(uint8_t *)Address != 0xFF) && (buffer[i] != 0xFF))
154 return 0xE0;
155 Address = Address + 1;
156 }
157
158 // start programming
159 HAL_FLASH_Unlock();
160
161 Address = startAddress;
162 ptr = 0;
163 answer = HAL_OK;
164 while (ptr < length)
165 {
166 if(buffer[ptr] != 0xFF)
167 {
168 answer = HAL_FLASH_Program(FLASH_TYPEPROGRAM_BYTE, Address, buffer[ptr]);
169 }
170 if (answer == HAL_OK)
171 {
172 Address = Address + 1;
173 ptr++;
174 }
175 else
176 {
177 HAL_FLASH_Lock();
178 return answer;
179 }
180 }
181 HAL_FLASH_Lock();
182
183 /* Check if the programmed data is OK
184 MemoryProgramStatus = 0: data programmed correctly
185 MemoryProgramStatus != 0: number of words not programmed correctly ******/
186 Address = startAddress; // check base_bootloader.c of OSTC4bootloader code
187 MemoryProgramStatus = 0x0;
188
189 ptr = 0;
190 while(ptr < length)
191 {
192 data8 = *(__IO uint8_t*)Address;
193
194 if((buffer[ptr] != 0xFF) && (data8 != buffer[ptr]))
195 {
196 MemoryProgramStatus++;
197 }
198
199 Address = Address + 1;
200 ptr++;
201 }
202
203 /* Check if there is an issue to program data */
204 if (MemoryProgramStatus == 0)
205 {
206 return HAL_OK;
207 }
208 else
209 {
210 return 0xEE;
211 }
212 }
213
214
215 uint8_t firmware2_variable_upperpart_eraseFlashMemory(uint32_t length, uint32_t offset)
216 {
217 uint32_t startAddress, endAddress;
218
219 // HAL_StatusTypeDef answer;
220 HAL_FLASH_Unlock();
221
222 startAddress = FLASH_FW2_START_ADDR + offset;
223 endAddress = startAddress + length;
224
225 if(endAddress > FLASH_FW2_END_ADDR)
226 endAddress = FLASH_FW2_END_ADDR;
227
228 FirstSector = GetSector(startAddress);
229 NbOfSectors = GetSector(endAddress) - FirstSector + 1;
230
231 EraseInitStruct.TypeErase = FLASH_TYPEERASE_SECTORS;
232 EraseInitStruct.VoltageRange = FLASH_VOLTAGE_RANGE_1;
233 EraseInitStruct.Sector = FirstSector;
234 EraseInitStruct.NbSectors = NbOfSectors;
235
236 return HAL_FLASHEx_Erase(&EraseInitStruct, &SectorError);
237 }
238
239
240 uint8_t firmware2_variable_upperpart_programFlashMemory(uint32_t length, uint32_t offset, uint8_t *pBuffer1, uint32_t pBuffer1Size, uint8_t *pBuffer2)
241 {
242 HAL_StatusTypeDef answer;
243 uint32_t ptr = 0;
244 uint32_t length1, length2;
245
246 if((pBuffer2) && (length > pBuffer1Size))
247 {
248 length1 = pBuffer1Size;
249 length2 = length - length1;
250 }
251 else
252 {
253 length1 = length;
254 length2 = 0;
255 }
256
257 Address = FLASH_FW2_START_ADDR + offset;
258
259 ptr = 0;
260 while ((Address <= FLASH_FW2_END_ADDR) && (ptr < length1))
261 {
262 answer = HAL_FLASH_Program(FLASH_TYPEPROGRAM_BYTE, Address, pBuffer1[ptr]);
263 if (answer == HAL_OK)
264 {
265 Address = Address + 1;
266 ptr++;
267 }
268 else
269 {
270 return answer;
271 }
272 }
273 ptr = 0;
274 while ((Address <= FLASH_FW2_END_ADDR) && (ptr < length2))
275 {
276 answer = HAL_FLASH_Program(FLASH_TYPEPROGRAM_BYTE, Address, pBuffer2[ptr]);
277 if (answer == HAL_OK)
278 {
279 Address = Address + 1;
280 ptr++;
281 }
282 else
283 {
284 return answer;
285 }
286 }
287 HAL_FLASH_Lock();
288
289 Address = FLASH_FW2_START_ADDR + offset;;
290 MemoryProgramStatus = 0x0;
291
292 ptr = 0;
293 while ((Address <= FLASH_FW2_END_ADDR) && (ptr < length1))
294 {
295 data32 = *(__IO uint8_t*)Address;
296
297 if (data32 != pBuffer1[ptr])
298 {
299 MemoryProgramStatus++;
300 }
301
302 Address = Address + 1;
303 ptr++;
304 }
305 ptr = 0;
306 while ((Address <= FLASH_FW2_END_ADDR) && (ptr < length2))
307 {
308 data32 = *(__IO uint8_t*)Address;
309
310 if (data32 != pBuffer2[ptr])
311 {
312 MemoryProgramStatus++;
313 }
314
315 Address = Address + 1;
316 ptr++;
317 }
318
319 if (MemoryProgramStatus == 0)
320 {
321 return HAL_OK;
322 }
323 else
324 {
325 return 0xEE;
326 }
327 }
328
329 uint8_t firmware_eraseFlashMemory(void)
330 {
331 // HAL_StatusTypeDef answer;
332 /* Unlock the Flash to enable the flash control register access *************/
333 HAL_FLASH_Unlock();
334
335 /* Erase the user Flash area
336 (area defined by FLASH_USER_START_ADDR and FLASH_USER_END_ADDR) ***********/
337
338 /* Get the 1st sector to erase */
339 FirstSector = GetSector(FLASH_FW_START_ADDR);
340 /* Get the number of sector to erase from 1st sector*/
341 NbOfSectors = GetSector(FLASH_FW_END_ADDR) - FirstSector + 1;
342
343 /* Fill EraseInit structure*/
344 EraseInitStruct.TypeErase = FLASH_TYPEERASE_SECTORS;
345 EraseInitStruct.VoltageRange = FLASH_VOLTAGE_RANGE_1;
346 EraseInitStruct.Sector = FirstSector;
347 EraseInitStruct.NbSectors = NbOfSectors;
348
349 /* Note: If an erase operation in Flash memory also concerns data in the data or instruction cache,
350 you have to make sure that these data are rewritten before they are accessed during code
351 execution. If this cannot be done safely, it is recommended to flush the caches by setting the
352 DCRST and ICRST bits in the FLASH_CR register. */
353 return HAL_FLASHEx_Erase(&EraseInitStruct, &SectorError);
354 }
355
356 uint8_t firmware_programFlashMemory(uint8_t *pBuffer1, uint32_t length1)//, uint8_t *pBuffer2, uint32_t length2)
357 {
358 HAL_StatusTypeDef answer;
359
360 /* Program the user Flash area word by word
361 (area defined by FLASH_USER_START_ADDR and FLASH_USER_END_ADDR) ***********/
362
363 uint32_t ptr = 0;
364
365 Address = FLASH_FW_START_ADDR;
366
367 ptr = 0;
368 while ((Address <= FLASH_FW_END_ADDR) && (ptr < length1))
369 {
370 answer = HAL_FLASH_Program(FLASH_TYPEPROGRAM_BYTE, Address, pBuffer1[ptr]);
371 if (answer == HAL_OK)
372 // if (HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD, Address, DATA_32) == HAL_OK)
373 {
374 Address = Address + 1;//4;
375 ptr++;
376 }
377 else
378 {
379 return answer;
380 }
381 }
382 /* same for pBuffer2
383 ptr = 0;
384 while ((Address < FLASH_FW_END_ADDR) && (ptr < length2))
385 {
386
387 if (HAL_FLASH_Program(FLASH_TYPEPROGRAM_BYTE, Address, pBuffer2[ptr]) == HAL_OK)
388 {
389 Address = Address + 1;
390 ptr++;
391 }
392 else
393 {
394 firmware_Error_Handler();
395 }
396 }
397 */
398 /* Lock the Flash to disable the flash control register access (recommended
399 to protect the FLASH memory against possible unwanted operation) *********/
400 HAL_FLASH_Lock();
401
402 /* Check if the programmed data is OK
403 MemoryProgramStatus = 0: data programmed correctly
404 MemoryProgramStatus != 0: number of words not programmed correctly ******/
405 Address = FLASH_FW_START_ADDR;
406 MemoryProgramStatus = 0x0;
407
408 ptr = 0;
409 while ((Address <= FLASH_FW_END_ADDR) && (ptr < length1))
410 {
411 data32 = *(__IO uint8_t*)Address;
412
413 if (data32 != pBuffer1[ptr])
414 {
415 MemoryProgramStatus++;
416 }
417
418 Address = Address + 1;//4;
419 ptr++;
420 }
421 /* same for pBuffer2
422 ptr = 0;
423 while ((Address < FLASH_FW_END_ADDR) && (ptr < length2))
424 {
425 data32 = *(__IO uint32_t*)Address;
426
427 if (data32 != pBuffer2[ptr])
428 {
429 MemoryProgramStatus++;
430 }
431
432 Address = Address + 1;//4;
433 ptr++;
434 }
435 */
436 /* Check if there is an issue to program data */
437 if (MemoryProgramStatus == 0)
438 {
439 return HAL_OK;
440 /* No error detected. Switch on LED3 */
441 }
442 else
443 {
444 return 0xEE;
445 }
446
447
448 }
449
450 /* Private functions ---------------------------------------------------------*/
451
452 /**
453 * @brief Gets the sector of a given address
454 * @param None
455 * @retval The sector of a given address
456 */
457 static uint32_t GetSector(uint32_t Address)
458 {
459 uint32_t sector = 0;
460
461 if((Address < ADDR_FLASH_SECTOR_1) && (Address >= ADDR_FLASH_SECTOR_0))
462 {
463 sector = FLASH_SECTOR_0;
464 }
465 else if((Address < ADDR_FLASH_SECTOR_2) && (Address >= ADDR_FLASH_SECTOR_1))
466 {
467 sector = FLASH_SECTOR_1;
468 }
469 else if((Address < ADDR_FLASH_SECTOR_3) && (Address >= ADDR_FLASH_SECTOR_2))
470 {
471 sector = FLASH_SECTOR_2;
472 }
473 else if((Address < ADDR_FLASH_SECTOR_4) && (Address >= ADDR_FLASH_SECTOR_3))
474 {
475 sector = FLASH_SECTOR_3;
476 }
477 else if((Address < ADDR_FLASH_SECTOR_5) && (Address >= ADDR_FLASH_SECTOR_4))
478 {
479 sector = FLASH_SECTOR_4;
480 }
481 else if((Address < ADDR_FLASH_SECTOR_6) && (Address >= ADDR_FLASH_SECTOR_5))
482 {
483 sector = FLASH_SECTOR_5;
484 }
485 else if((Address < ADDR_FLASH_SECTOR_7) && (Address >= ADDR_FLASH_SECTOR_6))
486 {
487 sector = FLASH_SECTOR_6;
488 }
489 else if((Address < ADDR_FLASH_SECTOR_8) && (Address >= ADDR_FLASH_SECTOR_7))
490 {
491 sector = FLASH_SECTOR_7;
492 }
493 else if((Address < ADDR_FLASH_SECTOR_9) && (Address >= ADDR_FLASH_SECTOR_8))
494 {
495 sector = FLASH_SECTOR_8;
496 }
497 else if((Address < ADDR_FLASH_SECTOR_10) && (Address >= ADDR_FLASH_SECTOR_9))
498 {
499 sector = FLASH_SECTOR_9;
500 }
501 else if((Address < ADDR_FLASH_SECTOR_11) && (Address >= ADDR_FLASH_SECTOR_10))
502 {
503 sector = FLASH_SECTOR_10;
504 }
505 else if((Address < ADDR_FLASH_SECTOR_12) && (Address >= ADDR_FLASH_SECTOR_11))
506 {
507 sector = FLASH_SECTOR_11;
508 }
509 else if((Address < ADDR_FLASH_SECTOR_13) && (Address >= ADDR_FLASH_SECTOR_12))
510 {
511 sector = FLASH_SECTOR_12;
512 }
513 else if((Address < ADDR_FLASH_SECTOR_14) && (Address >= ADDR_FLASH_SECTOR_13))
514 {
515 sector = FLASH_SECTOR_13;
516 }
517 else if((Address < ADDR_FLASH_SECTOR_15) && (Address >= ADDR_FLASH_SECTOR_14))
518 {
519 sector = FLASH_SECTOR_14;
520 }
521 else if((Address < ADDR_FLASH_SECTOR_16) && (Address >= ADDR_FLASH_SECTOR_15))
522 {
523 sector = FLASH_SECTOR_15;
524 }
525 else if((Address < ADDR_FLASH_SECTOR_17) && (Address >= ADDR_FLASH_SECTOR_16))
526 {
527 sector = FLASH_SECTOR_16;
528 }
529 else if((Address < ADDR_FLASH_SECTOR_18) && (Address >= ADDR_FLASH_SECTOR_17))
530 {
531 sector = FLASH_SECTOR_17;
532 }
533 else if((Address < ADDR_FLASH_SECTOR_19) && (Address >= ADDR_FLASH_SECTOR_18))
534 {
535 sector = FLASH_SECTOR_18;
536 }
537 else if((Address < ADDR_FLASH_SECTOR_20) && (Address >= ADDR_FLASH_SECTOR_19))
538 {
539 sector = FLASH_SECTOR_19;
540 }
541 else if((Address < ADDR_FLASH_SECTOR_21) && (Address >= ADDR_FLASH_SECTOR_20))
542 {
543 sector = FLASH_SECTOR_20;
544 }
545 else if((Address < ADDR_FLASH_SECTOR_22) && (Address >= ADDR_FLASH_SECTOR_21))
546 {
547 sector = FLASH_SECTOR_21;
548 }
549 else if((Address < ADDR_FLASH_SECTOR_23) && (Address >= ADDR_FLASH_SECTOR_22))
550 {
551 sector = FLASH_SECTOR_22;
552 }
553 else/*(Address < FLASH_END_ADDR) && (Address >= ADDR_FLASH_SECTOR_23))*/
554 {
555 sector = FLASH_SECTOR_23;
556 }
557
558 return sector;
559 }
560
561 /*
562 static void firmware_Error_Handler(HAL_StatusTypeDef reason)
563 {
564 static HAL_StatusTypeDef last_reason = HAL_OK;
565
566 last_reason = reason;
567 while(1)
568 {
569 }
570 }
571 */