I try to write float values to flash memory of STM32F103RBT6. but it cannot read the value and CPU reset by IWDT. my code is in below:
char str[100]; float Val; float Data; Data=123.456; FLASH_Unlock(); FLASH_ClearFlag(FLASH_FLAG_BSY | FLASH_FLAG_EOP | FLASH_FLAG_PGERR | FLASH_FLAG_WRPRTERR); FLASH_ErasePage(0x08002000); //write------------------------ Val = *((unsigned long*)&Data); FLASH_ProgramWord(0x08002000,Val); //Read----------------------------- float *mem = (float *)0x08002000; sprintf(str,"%f\n", *mem); uart_out_str(str);
How ever the following code wires and reads integer values
u32 Data; Data=123; FLASH_Unlock(); FLASH_ClearFlag(FLASH_FLAG_BSY | FLASH_FLAG_EOP | FLASH_FLAG_PGERR | FLASH_FLAG_WRPRTERR); FLASH_ErasePage(0x08002000); //write------------------------ FLASH_ProgramWord(0x08002000, Data); //Read----------------------------- u32 *mem = (u32 *)0x08002000; sprintf(str,"%u\n", *mem);uart_out_str(str);
But have you made sure that "to flash" really is a relevant fact?
Can you work with float values in RAM?
And did you end up with a valid floating point value at that address in flash?
Sorry I didn't understand well. In RAM it is done by assigning RAM registers with float type values like:
float A=1.23;
It is possible to write to EEPROM with the similar code for those micro controllers that include internal EEPROM like STM32L152RC.
I modified the code a little bit as below and could write and read float values. In deed I write float values as unsigned int converted and read as well.
unsigned int outVal; union { unsigned int i; float f; }var1,var2; var1.f=1231.2345; FLASH_Unlock(); FLASH_ClearFlag(FLASH_FLAG_BSY | FLASH_FLAG_EOP | FLASH_FLAG_PGERR | FLASH_FLAG_WRPRTERR); FLASH_ErasePage(0x0801FC00); //write------------------------ outVal = *((unsigned int*)&var1.i); FLASH_ProgramWord(0x0801FC00,outVal); //Read----------------------------- u32 *mem = (u32 *)0x0801FC00; var2.i=*mem; sprintf(str,"%.2f\n", var2.f);uart_out_str(str); //------- FLASH_Lock();
you saved my day;