2020-09-21 11:52 PM
Hello STM Team,
I am using stm32L5 and I am writing an application in which I have to store the negative as well as positive float values in the internal flash memory in dual bank mode.
I am erasing the page first and then writing it.
I am facing issue in storing the negative values. For positive values it is working fine.
As library is allowing to store only unsigned int, I have multiplied the data value with required power of 10 to convert it to integer and after reading the data I am dividing the same with power of 10.
My logic:
uint64_t addr = 0x0803F800
float value = 50.123;
float value1 = -50.123
void flash()
{
float dest = 0;
pageNumber = calculatePage(addr);
if( pageNumber < 128)
bankNumber = BANK1;
else
bankNumber = BANK2;
FLASH_Erase(bankNumber, pageNumber);
value = value * 1000;
if((FLASH_WRITE(addr, &value,2) != 0)) {
print("Error\n");
}
dest = FLASH_READ(addr);
dest = (float)dest/1000;
print("data = %f", dest);
value++;
}
FLASH_WRITE(uint32_t destination, uint32_t *p_source, uint32_t length)
{
uint32_t status = FLASHIF_OK;
uint32_t i = 0;
HAL_FLASH_Unlock();
__HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_ALL_ERRORS);
/* DataLength must be a multiple of 64 bit */
for (i = 0; (i < length / 2) /*&& (destination <= (USER_FLASH_END_ADDRESS - 8))*/; i++)
{
if (HAL_FLASH_Program(FLASH_TYPEPROGRAM_DOUBLEWORD, destination, *((uint64_t *)(p_source + 2*i))) == HAL_OK)
{
if (*(uint64_t*)destination != *(uint64_t *)(p_source + 2*i))
{
/* Flash content doesn't match SRAM content */
status = FLASHIF_WRITINGCTRL_ERROR;
break;
}
/* Increment FLASH destination address */
destination += 8;
}
else
{
/* Error occurred while writing data in Flash memory */
status = FLASHIF_WRITING_ERROR;
break;
}
}
HAL_FLASH_Lock();
return status;
}
uint32_t FLASH_READ(uint32_t address)
{
return *(uint32_t*)address;
}
Note : For variable value, It is working fine.
For variable value1, It is displaying value 0.
Queries:
1) Please let me know how to store negative values in internal flash memory.
2) Also, please let me know if there is any better and efficient way instead of multiplying and dividing the float data.