1
votes

Can someone tell me what stupid thing I am doing wrong or understanding? As a test, I am trying to write a simple number into flash and retrieve it. Once successful, I will expand this to 6 signed values. My device is an STM32L476RG

uint64_t data = 88;
Erase_Flash();
HAL_FLASH_Unlock();

Address = ADDR_FLASH_PAGE_256;
if (HAL_FLASH_Program(FLASH_TYPEPROGRAM_FAST, Address, data) != HAL_OK)
    serprintf("Error writing flash.");

HAL_FLASH_Lock();
uint8_t *flash_biases = (uint8_t*) (ADDR_FLASH_PAGE_256);

enter image description here

Based on what I've read, I should be able to access the flash memory like I have. But it's not retrieving the value I expect.

The Erase_Flash() function looks like this:

void Erase_Flash() {
    HAL_FLASH_Unlock();
    /* Clear OPTVERR bit set on virgin samples */
    __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_OPTVERR);

    /* Fill EraseInit structure*/
    EraseInitStruct.TypeErase = FLASH_TYPEERASE_MASSERASE;
    EraseInitStruct.Banks = FLASH_BANK_2;

    if (HAL_FLASHEx_Erase(&EraseInitStruct, &PAGEError) != HAL_OK) {
        serprintf("Error erasing flash.");
    }
    HAL_FLASH_Lock();
}
1
What is the exact model of your device?Tagli
Could you post Erase_Flash()? Is Address of type uint32_t? What is the byte value at Address right before invoking HAL_FLASH_Program() (should be 0xFF)? Is the flash sector read/write protected (check option bytes)? Is the flash sector really unused (check linker script)?rel
@rel posted! The flash section is unused. There is another section of code a coworker wrote that is working fine at that value.jgauthier
@Tagli stm32L476RG, and post edited to reflect itjgauthier
Like @Tagli already pointed in the answer below, you could use FLASH_Program_DoubleWord() or HAL_FLASH_Program() with FLASH_TYPEPROGRAM_DOUBLEWORD, check: github.com/STMicroelectronics/STM32CubeL4/blob/master/Drivers/… . In fast programming mode HAL_FLASH_Program() interprets the third parameter as a pointer! Quite confusing. Fast programming is not available on all STM32 variants... See also: 3.3.7 Flash main memory programming sequences in the RM0351 Reference manual.rel

1 Answers

3
votes

FLASH_TYPEPROGRAM_FAST mode is used for writing 32 double words at once and when this mode is used, the third argument (data) becomes the raw starting address of that 32 double word data source, not the data itself.

Currently, your code fetches data from the address starting from 0x88 and writes it (a total of 256 bytes) to the flash. It appears that there is 245 on address 0x88.

You need to use FLASH_TYPEPROGRAM_DOUBLEWORD for writing uint64_t data.