cancel
Showing results for 
Search instead for 
Did you mean: 

stm32L5 write flash problem

tonbaebae
Associate II

Hi. I am testing flash read and write operations.

In the main function, when I write 1 to address 0x08006000, I can correctly read back 1. However, when using FreeRTOS and performing the same operation within a task, I read back  garbled values (such as 191, 111, etc.).

I was wondering if it's a problem with my functions or it's not usable within FreeRTOS.

void main()

{

FLASH_Write(ADDRESS, &data);
printf("ADDRESS:%d\n",FLASH_Read(ADDRESS));

}

 

void FLASH_Write(uint32_t Address, uint8_t *pData)
{
HAL_FLASH_Unlock();

FLASH_EraseInitTypeDef EraseInitStruct;
EraseInitStruct.TypeErase = FLASH_TYPEERASE_PAGES;
EraseInitStruct.Page = 12; //address 08006000
EraseInitStruct.Banks = FLASH_BANK_1;
EraseInitStruct.NbPages = 1;
uint32_t PageError;

HAL_StatusTypeDef status = HAL_FLASHEx_Erase(&EraseInitStruct, &PageError);

/* FLASH word program */
__HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_ALL_ERRORS);
HAL_StatusTypeDef status2 = HAL_FLASH_Program(FLASH_TYPEPROGRAM_DOUBLEWORD, Address, pData);

/* Lock the Flash to disable the flash control register access */
HAL_FLASH_Lock();
}

uint8_t FLASH_Read(uint32_t Address)
{
return (*(uint8_t *)(Address));
}

1 REPLY 1
Danish1
Lead II

I wouldn't expect FreeRTOS to cause a problem.

Your example is not complete, in that we cannot see where ADDRESS is defined, nor where data is defined and set to its expected value of 1.

Your example code does not make use of the erasing/programming results status, PageError and status2. It might be helpful to print those out just to confirm that your code thinks programming was successful.

It might be that when you have FreeRTOS in your code, that address 0x08006000 is part of your program code. In which case much can go wrong!

(Another possibility is that another thread in FreeRTOS is doing things with the FLASH - but I wouldn't expect that if the code you show is the entire program.)