Skip to main content
Explorer
August 6, 2023
Solved

How to write and read double values with fractional part successfully at flash memory?

  • August 6, 2023
  • 3 replies
  • 2299 views

Hello Team,

I have imported the Flash_EraseProgram from the github about nucleo-L412RB:

https://github.com/STMicroelectronics/STM32CubeL4/blob/master/Projects/NUCLEO-L412RB-P/Examples/FLASH 


I have modified the code, in order to store 6 double values at flash memory and also Ι have created a simple function that read the values from the memory.

When the values are uint64_t without fractional part I can write the memory successfully(MemoryPrograamStatus ==0) and I can read them correctly.

When I try to store double words with fractional part, MemoryProgramSatus isn't 0, which means that the memory is not be writed correctly, and also I cannnot read back the values correctly.

I attach the code and what I can see at the memory and the buffers.

Thanks in advance.

 

Screenshot_2.png

    This topic has been closed for replies.
    Best answer by TDK
     if (HAL_FLASH_Program(FLASH_TYPEPROGRAM_DOUBLEWORD, Address, (uint64_t)send_data[i]) == HAL_OK)
     {
     Address = Address + 8;
     }

    This code converts the double value to a uint64_t and writes that. Instead, you want to interpret the memory as a uint64_t:

     if (HAL_FLASH_Program(FLASH_TYPEPROGRAM_DOUBLEWORD, Address, *((uint64_t*)&send_data[i])) == HAL_OK)
     {
     Address = Address + 8;
     }

     

    Same thing on the receiving end, but it looks like your receiving code is fine.

    3 replies

    Graduate II
    August 6, 2023

    The Arduino libraries have Put() and Get() functions that might serve as a reference:

    float f = 123.456f; //Variable to store in EEPROM.

    EEPROM.put(eeAddress, f);

    https://wiki-content.arduino.cc/en/Tutorial/LibraryExamples/EEPROMPut

    https://wiki-content.arduino.cc/en/Tutorial/LibraryExamples/EEPROMGet

    https://github.com/PaulStoffregen/EEPROM/blob/master/EEPROM.h

    TDKAnswer
    Super User
    August 6, 2023
     if (HAL_FLASH_Program(FLASH_TYPEPROGRAM_DOUBLEWORD, Address, (uint64_t)send_data[i]) == HAL_OK)
     {
     Address = Address + 8;
     }

    This code converts the double value to a uint64_t and writes that. Instead, you want to interpret the memory as a uint64_t:

     if (HAL_FLASH_Program(FLASH_TYPEPROGRAM_DOUBLEWORD, Address, *((uint64_t*)&send_data[i])) == HAL_OK)
     {
     Address = Address + 8;
     }

     

    Same thing on the receiving end, but it looks like your receiving code is fine.

    Graduate II
    August 7, 2023
    *DATA_64 = *(__IO double*)StartPageAddress; 

    NO, you need to cast the data to a uint64_t, it doesn't know how to cram doubles into a different destination type. You want to copy the data into FLASH as a byte-for-byte copy (8 bytes at a time), so *(volatile uint64_t *) gets the data as the 8-bytes, as represented in memory.

    double *a = (double *)0x08010000; // how you point to an array of doubles situated in FLASH at address 0x08010000;