Skip to main content
RJARC.1
Associate II
Jun 13, 2023 9:26 AM UTC
Question

STM32WB55, write in the internal flash memory with FreeRTOS

Hi,

I try to save the configuration application in the internal flash memory of my STM32WB55 with this next fonction :

static int write(long offset, const uint8_t *buf, size_t size)
{
 size_t i;
 
 uint32_t addr = stm32wb_onchip_flash.addr + offset;
 
 volatile __ALIGN_BEGIN uint64_t write_data __ALIGN_END;
 volatile __ALIGN_BEGIN uint64_t read_data __ALIGN_END;
 vTaskSuspendAll();
 __HAL_FLASH_CLEAR_FLAG(0xFFFFFFFF);
 
 
 HAL_FLASH_Unlock();
 
 
 __enable_irq(); //To be sure that HAL_FLASH_Program to not bloc in get tick function
 
 
 for (i = 0; i < size; i+=8, buf+=8, addr+=8)
 {
 memcpy(&write_data, buf, 8);
 /* write data */
 while(LL_FLASH_IsActiveFlag_OperationSuspended());
 
 volatile HAL_StatusTypeDef status = HAL_FLASH_Program(FLASH_TYPEPROGRAM_DOUBLEWORD,addr, write_data);
 
 if (status != HAL_OK)
 {
 volatile uint32_t lastFlashError = HAL_FLASH_GetError();
 return -1;
 }
 read_data = *(uint64_t *)addr;
 /* check data */
 
 if (read_data != write_data)
 {
 __NOP();
 return -1;
 }
 
 }
 // vPortExitCritical();
 HAL_FLASH_Lock();
 xTaskResumeAll();
 return size;
}

This function works very well at initialization of the application. But when i call this function from a FreeRTOS task the HAL_FLASH_Program function return to me an error. The error is 0xA0, PGA error, but this function is always called with align adresse.

I already checked the option bytes without sucess... May be somebody have an idea to help me :\

Thank you.

This topic has been closed for replies.

1 reply

RJARC.1
RJARC.1Author
Associate II
Jun 19, 2023 12:16 PM UTC

Update: 

I tried to lock the sempahores 2 and 7 before trying to write in the flash without success. 

i use this fonction : 

 

while(LL_HSEM_2StepLock(HSEM, 7, 0)) { }; 
LL_HSEM_ReleaseLock(HSEM, 2, 0);
 
For the moment, i do not use the BLE stack in my project. 

Somebody can help me ? 

Thank you.

Best Regards.