cancel
Showing results for 
Search instead for 
Did you mean: 

Flash writing issue

satyam9896
Associate II

Split from this thread: https://community.st.com/t5/stm32-mcus-embedded-software/flash-writing/td-p/753438

Hi @SofLit  thanks and

 

void StartUpdateFWBlueNRG(uint32_t SizeOfUpdate, uint32_t uwCRCValue)
{
  __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_PGAERR);
__HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_WRPERR );
__HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_FWWERR);
__HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_NOTZEROERR);

  // BootInfo* boot_info = (BootInfo*)BOOT_INFO_ADDRESS;
// uint32_t target_address = (boot_info->active_app == 1) ? 0x0801C000 : 0x08005000;
    FLASH_EraseInitTypeDef EraseInitStruct;
    uint32_t SectorError = 0;
    OTA_PRINTF("Start FLASH Erase\r\n");

    SizeOfUpdateBlueFW = SizeOfUpdate;
    AspecteduwCRCValue = uwCRCValue;
    // Compute first and last pages
    uint32_t FirstPage = GetPage(0x0801C000);
    uint32_t LastPage = GetPage(0x08022EE0-1);
    uint32_t NbPages = LastPage - FirstPage + 1;

    // Compute number of pages to erase
    EraseInitStruct.TypeErase = FLASH_TYPEERASE_PAGES;
    EraseInitStruct.PageAddress = 0x0801C000;
    EraseInitStruct.NbPages = NbPages;

    // Unlock the Flash
    HAL_FLASH_Unlock();

    // Clear all flash error flags
    __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_PGAERR);
__HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_WRPERR );
__HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_FWWERR);
__HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_NOTZEROERR);


    if (HAL_FLASHEx_Erase(&EraseInitStruct, &SectorError) != HAL_OK){
        uint16_t  errorcode = HAL_FLASH_GetError();
       OTA_PRINTF("Flash Erase Error: 0x%08lX\r\n", (long)errorcode);  
       OTA_ERROR_FUNCTION();
    } else {
        OTA_PRINTF("End FLASH Erase %ld Pages of %d Bytes\r\n", (long)EraseInitStruct.NbPages, FLASH_PAGE_SIZE);
    }

    // Lock the Flash
    HAL_FLASH_Lock();
}

 

now im able to write the flash however my erase flash doesnt work and hence writting flash as well but if that area is clear i can able to write also there is no issue in address selection also bcz same procedure i directly use in main.c file it works well.

 
1 REPLY 1
satyam9896
Associate II

its working by using _I0 macro however im not able to erase the flash and there is not problrm in address selection also i believe bcz same approcch if i do in main.c file it works 

void StartUpdateFWBlueNRG(uint32_t SizeOfUpdate, uint32_t uwCRCValue)
{
  __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_PGAERR);
__HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_WRPERR );
__HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_FWWERR);
__HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_NOTZEROERR);
 
  // BootInfo* boot_info = (BootInfo*)BOOT_INFO_ADDRESS;
// uint32_t target_address = (boot_info->active_app == 1) ? 0x0801C000 : 0x08005000;
    FLASH_EraseInitTypeDef EraseInitStruct;
    uint32_t SectorError = 0;
    OTA_PRINTF("Start FLASH Erase\r\n");
 
    SizeOfUpdateBlueFW = SizeOfUpdate;
    AspecteduwCRCValue = uwCRCValue;
    WritingAddress = OTA_ADDRESS_START;
// WritingAddress = target_address;
 
    // Compute first and last pages
    uint32_t FirstPage = GetPage(0x0801C000);
   // uint32_t LastAddress = WritingAddress + SizeOfUpdate - 1;
    uint32_t LastPage = GetPage(0x08022EE0-1);
  uint32_t NbPages = LastPage - FirstPage + 1;
 
    // Compute number of pages to erase
    EraseInitStruct.TypeErase = FLASH_TYPEERASE_PAGES;
    EraseInitStruct.PageAddress = 0x0801C000;
    EraseInitStruct.NbPages = NbPages/*((LastPage - FirstPage) / FLASH_PAGE_SIZE) + 1*/;
 
    // Unlock the Flash
    HAL_FLASH_Unlock();
 
    // Clear all flash error flags
    __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_PGAERR);
__HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_WRPERR );
__HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_FWWERR);
__HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_NOTZEROERR);
// __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_PGFLASH_FLAG_EOPAERR);
 
    if (HAL_FLASHEx_Erase(&EraseInitStruct, &SectorError) != HAL_OK){
        uint16_t  errorcode = HAL_FLASH_GetError();
       OTA_PRINTF("Flash Erase Error: 0x%08lX\r\n", (long)errorcode);  
       OTA_ERROR_FUNCTION();
    } else {
        OTA_PRINTF("End FLASH Erase %ld Pages of %d Bytes\r\n", (long)EraseInitStruct.NbPages, FLASH_PAGE_SIZE);
    }
 
    // Lock the Flash
    HAL_FLASH_Lock();
}