comparison OtherSources/firmwareEraseProgram.c @ 1017:5924a2d1d3ba GasConsumption

Prepare custom block update function: In the flash area of the font lib some sectors may be used for custom data or a boot updater image. With this change a flash option is added to the maintainance menu. IMPORTANT: The fimwareEraseProgram.c is needed for compiling the firmware now => Add it e.g. by adding a link from the OtherSources location to your source folder.
author Ideenmodellierer
date Thu, 29 May 2025 22:04:46 +0200
parents 7801c5d8a562
children 4b6afe5551e1
comparison
equal deleted inserted replaced
1014:8c0134a287da 1017:5924a2d1d3ba
81 #define ADDR_FLASH_SECTOR_22 ((uint32_t)0x081C0000) /* Base @ of Sector 10, 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 */ 82 #define ADDR_FLASH_SECTOR_23 ((uint32_t)0x081E0000) /* Base @ of Sector 11, 128 Kbytes */
83 83
84 #define SECTOR_SIZE_128KB ((uint32_t)0x00020000) 84 #define SECTOR_SIZE_128KB ((uint32_t)0x00020000)
85 85
86 #define FLASH_BOOT_START_ADDR ADDR_FLASH_SECTOR_0
87 #define FLASH_BOOT_END_ADDR (ADDR_FLASH_SECTOR_5 - 1)
88
86 #define FLASH_FW_START_ADDR ADDR_FLASH_SECTOR_6 89 #define FLASH_FW_START_ADDR ADDR_FLASH_SECTOR_6
87 #define FLASH_FW_END_ADDR (ADDR_FLASH_SECTOR_12 - 1) 90 #define FLASH_FW_END_ADDR (ADDR_FLASH_SECTOR_12 - 1)
88 91
89 #define FLASH_FW2_START_ADDR ADDR_FLASH_SECTOR_12 92 #define FLASH_FW2_START_ADDR ADDR_FLASH_SECTOR_12
90 #define FLASH_FW2_END_ADDR (ADDR_FLASH_SECTOR_22 + SECTOR_SIZE_128KB - 1) 93 #define FLASH_FW2_END_ADDR (ADDR_FLASH_SECTOR_22 + SECTOR_SIZE_128KB - 1)
445 } 448 }
446 449
447 450
448 } 451 }
449 452
453
454 uint8_t bootloader_eraseFlashMemory(void)
455 {
456 // HAL_StatusTypeDef answer;
457 /* Unlock the Flash to enable the flash control register access *************/
458 HAL_FLASH_Unlock();
459
460 /* Erase the user Flash area
461 (area defined by FLASH_USER_START_ADDR and FLASH_USER_END_ADDR) ***********/
462
463 /* Get the 1st sector to erase */
464 FirstSector = GetSector(FLASH_BOOT_START_ADDR);
465 /* Get the number of sector to erase from 1st sector*/
466 NbOfSectors = GetSector(FLASH_BOOT_END_ADDR) - FirstSector + 1;
467
468 /* Fill EraseInit structure*/
469 EraseInitStruct.TypeErase = FLASH_TYPEERASE_SECTORS;
470 EraseInitStruct.VoltageRange = FLASH_VOLTAGE_RANGE_1;
471 EraseInitStruct.Sector = FirstSector;
472 EraseInitStruct.NbSectors = NbOfSectors;
473
474 /* Note: If an erase operation in Flash memory also concerns data in the data or instruction cache,
475 you have to make sure that these data are rewritten before they are accessed during code
476 execution. If this cannot be done safely, it is recommended to flush the caches by setting the
477 DCRST and ICRST bits in the FLASH_CR register. */
478 return HAL_FLASHEx_Erase(&EraseInitStruct, &SectorError);
479 }
480
481 uint8_t bootloader_programFlashMemory(uint8_t *pBuffer1, uint32_t length1, SHardwareData* pHwInfo)
482 {
483 HAL_StatusTypeDef answer;
484
485 /* Program the user Flash area word by word
486 (area defined by FLASH_USER_START_ADDR and FLASH_USER_END_ADDR) ***********/
487
488 uint32_t index = 0;
489 Address = FLASH_BOOT_START_ADDR;
490
491 uint8_t* pHardwareInfo = (uint8_t*) pHwInfo;
492 uint8_t tmp = 0;
493
494 index = 0;
495 while ((Address <= FLASH_FW_END_ADDR) && (index < length1))
496 {
497 if((Address >= HARDWAREDATA_ADDRESS) && (Address < HARDWAREDATA_ADDRESS + sizeof(SHardwareData)))
498 {
499 answer = HAL_FLASH_Program(FLASH_TYPEPROGRAM_BYTE, Address, *pHardwareInfo++);
500 }
501 else
502 {
503 answer = HAL_FLASH_Program(FLASH_TYPEPROGRAM_BYTE, Address, pBuffer1[index]);
504 }
505 if (answer == HAL_OK)
506 {
507 Address = Address + 1;
508 index++;
509 }
510 else
511 {
512 return answer;
513 }
514 }
515 /* Lock the Flash to disable the flash control register access (recommended
516 to protect the FLASH memory against possible unwanted operation) *********/
517 HAL_FLASH_Lock();
518
519 /* Check if the programmed data is OK
520 MemoryProgramStatus = 0: data programmed correctly
521 MemoryProgramStatus != 0: number of words not programmed correctly ******/
522 Address = FLASH_BOOT_START_ADDR;
523 MemoryProgramStatus = 0x0;
524
525 index = 0;
526 pHardwareInfo = (uint8_t*) pHwInfo;
527
528 while ((Address <= FLASH_FW_END_ADDR) && (index < length1))
529 {
530 if((Address >= HARDWAREDATA_ADDRESS) && (Address < HARDWAREDATA_ADDRESS + sizeof(SHardwareData)))
531 {
532 tmp = *pHardwareInfo++;
533 }
534 else
535 {
536 tmp = pBuffer1[index];
537 }
538
539 data32 = *(__IO uint8_t*)Address;
540 if (data32 != tmp)
541 {
542 MemoryProgramStatus++;
543 }
544
545 Address = Address + 1;
546 index++;
547 }
548 /* Check if there is an issue to program data */
549 if (MemoryProgramStatus == 0)
550 {
551 return HAL_OK;
552 /* No error detected. Switch on LED3 */
553 }
554 else
555 {
556 return 0xEE;
557 }
558 }
559
560
561 uint32_t CalcFletcher32(uint32_t startAddr, uint32_t endAddr)
562 {
563 uint32_t fletcher = 0;
564 uint16_t* pData = (uint16_t*) startAddr;
565 uint32_t index = 0;
566
567 uint16_t sum1 = 0;
568 uint16_t sum2 = 0;
569 for(index = startAddr; index <= endAddr; index +=2)
570 {
571 sum1 = sum1 + *pData++;
572 sum2 = (sum2 + sum1);
573 }
574 fletcher = (sum2 << 16) | sum1;
575
576 return fletcher;
577 }
578
579
450 /* Private functions ---------------------------------------------------------*/ 580 /* Private functions ---------------------------------------------------------*/
451 581
452 /** 582 /**
453 * @brief Gets the sector of a given address 583 * @brief Gets the sector of a given address
454 * @param None 584 * @param None