cancel
Showing results for 
Search instead for 
Did you mean: 

Getting Flash writing problems on STM32L4, Memory getting written but memory showing 0xFFFFFFFF

Hjain.1
Associate II

so basically im using stm32l4 as a dual bank, so essentially there are two apps and a bootloader, now im in either app1 or app2 , and i clear the other apps flash addresses, now when i write to that address the data sometimes get written and sometimes it shows as 0XFFFFFFFF which is empty but from the flash write double word and flash program i get as value as 1 in return which means it is writing to that address

for a larger context im trying to do a ota, it happens sometimes and fails sometimes due to this issue

5 REPLIES 5
Bob S
Principal

It would help to see your code. And if you are calling HAL functions, a value of "1" means an error, not success.

When you say "sometimes it shows as 0xfffffff" - WHERE does it show this value? In the debugger? Or in your code when you attempt to read back the data you just wrote? The debugger is not expecting FLASH memory space to change, so it doesn't normally re-read data blocks while stepping through a program.

Hjain.1
Associate II

yeah sure heres a snippet of the code:

                        char* crc_str     = strtok_r(NULL, delimiter, &rest);

                        uint16_t crc_len  = (uint16_t)strlen(crc_str);

                        uint32_t crc_recv = (uint32_t)hexstring_to_long(crc_str);

                        uint32_t crc_hex =

                            crc32(xServerString_raw, msg_len - (crc_len + 1));

                        if (crc_hex == crc_recv)

                        {

                            // CRC Verified

                            for (uint8_t i = 0; i < 32; i++)

                            {

                                ota_packet_hex[i] =

                                    hexstring_to_flash(hex_data_str);

                                hex_data_str += 16;

                            }

                            // push_telemetry_msg_bl(telemetry_tx_string);

                            if (flash_write(&address, ota_packet_hex, 32) == 0)

                            {

                                // Unable to write to flash

                                // Add failure msg

                                sprintf(telemetry_tx_string, "resp_ota:%d,%d,0,2,%lu",

                                        OTA_APP_DATA, OTA_TYPE_STARK, offset);

                            }

                            else

                            {

                                // Written to flash successfully

                                // Add success log

                                set_flash_status(offset);

                                sprintf(telemetry_tx_string, "resp_ota:%d,%d,1,0,%lu",

                                        OTA_APP_DATA, OTA_TYPE_STARK, offset);

                            }

                        }

uint8_t flash_write(uint32_t *address, uint64_t *data, uint16_t size)

{

    HAL_FLASH_Unlock();

    __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_ALL_ERRORS);

    for (int i = 0; i < size; i++)

    {

        HAL_StatusTypeDef ret;

        for (uint8_t try = 0; try < MAX_FLASH_RETIRES; try++)

        {

            ret = Flash_Program(FLASH_TYPEPROGRAM_DOUBLEWORD,

                                (*address) + (i * 8), data[i]);

            if (ret == HAL_OK)

            {

                break;

            }

            else

            {

                __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_ALL_ERRORS);

            }

        }

        if (ret != HAL_OK)

        {

            HAL_FLASH_Lock();

            return 0;

        }

    }

    HAL_FLASH_Lock();

    return 1;

}

the double word library we are using is standard HAL_library

for the second query, no once we get that our device has written in our flash then i check in stm32cube programmer and check at that address it sometimes is empty but we get that it has written in our flash

Bob S
Principal

You don't show Flash_Program(). Did you rename HAL_FLASH_Program() to Flash_Program()????

You should clear error before every call to HAL_FLASH_Program().

How is "address" defined? The address of "address" is passed to your flash_write() function.

Why do you re-try the programming operation if it fails? That doesn't make any sense.

Hjain.1
Associate II

hey bob

yes its the same as HAL_FLASH_program

my address is defined as this  uint32_t address = (uint32_t)(&__app2_start__);

where app2 start address is 0x80800000

also i retry programming if it fails because im trying to ota update

in flash_program i do believe the error i getting cleared

if (status == HAL_OK)

    {

        pFlash.ErrorCode = HAL_FLASH_ERROR_NONE;

That line you show only clears errors if there were no errors (somewhere in flash_program()). You say that is the same as HAL_FLASH_Program(). If so, then that only happens after the "wait for previous operation" check at the beginning of the function.

In your flash_write() function, change your code so that it clear errors every time just before you call Program_Flash(). I admit that may be superstition on my part, but doing that fixed things on my L4xx system. This presumes that your Program_Flash() really is the same as the HAL version. I'll have to take your word for this since you didn't post that code.

If that doesn't fix things, then you are back to basic debugging.