AnsweredAssumed Answered

Saving Float values into FLASH, reconstructing the Floats

Question asked by Reddy.Harinadha on Aug 6, 2014
Latest reply on Aug 7, 2014 by Clive One
I have tested EEPROM emulation code for STM32F4 recently. It works fine. Now I want to save some data written the following code for saving some floats.
For conversion of float to int32_t and again converting int32_t to float, 
I enabled CMSIS DSP library in project settings of IAR, defined ARM_MATH_CM4 in compiler settings, included arm_math.h . 

01.void EEPROMdataTest(void)
02.{
03.  /* Unlock the Flash to enable the flash control register access *************/
04.  HAL_FLASH_Unlock();
05.   
06.  /* EEPROM Init */
07.  uint16_t FlashStatus;
08.  FlashStatus = EE_Init();
09.  if(FlashStatus  != HAL_OK)
10.    printf("err during EE_Init() %d\r\n", FlashStatus);
11.   
12.  /* --- Store successively many values in the FLASH & Read---*/
13.  
14.  float myfloats[10] ={ 0.98345f, 1.95f, 95.95f, 0.01f, 99.99f,
15.  -0.94f, -1.94f, -95.95f, -0.01f, -99.99f};
16.  int32_t q31_var = 0; float result = 0;
17.  for (int i = 0; i<10; i++){ 
18.    uint16_t u16_var[2]; 
19.    arm_float_to_q31(&myfloats[i], &q31_var, 1);
20.    q31_to_u16_buffer(q31_var, &u16_var[0]);
21.       
22.    EE_WriteVariable(VirtAddVarTab[0], u16_var[0]); // MS part
23.    EE_WriteVariable(VirtAddVarTab[1], u16_var[1]); // LS part
24.    HAL_Delay(10);
25.    uint16_t u16_varRead[2];
26.    /* read the last stored variables data*/
27.    FlashStatus = EE_ReadVariable(VirtAddVarTab[0], &u16_varRead[0]); // MS part
28.    FlashStatus = EE_ReadVariable(VirtAddVarTab[1], &u16_varRead[1]); // LS part
29.    /* construct int32_t from two uint16_t values */
30.    q31_var = ((int32_t)u16_varRead[0] << 16) | ((int32_t)u16_varRead[1]);
31.    arm_q31_to_float(&q31_var, &result, 1);
32.     
33.    printf("%2d: %8.6f, %8.6f \r\n",i, myfloats[i], result);
34. 
35.  }
36. 
37.  /* Lock the Flash to disable the flash control register access (recommended
38.     to protect the FLASH memory against possible unwanted operation) *********/
39.  HAL_FLASH_Lock();
40.}
void q31_to_u16_buffer(int32_t inval, uint16_t* pBuffer)
{
  //pBuffer[0] = (uint16_t)((inval & 0xffff0000)>> 16);
  //pBuffer[1] = (uint16_t)(inval & 0x0000ffff);
    uint8_t big8[4];
    big8[0] = (uint8_t)((inval >> 24) & 0xff);
    big8[1] = (uint8_t)((inval >> 16) & 0xff);
    big8[2] = (uint8_t)((inval >> 8) & 0xff);
    big8[3] = (uint8_t)(inval & 0xff);       
    pBuffer[0] = ((uint16_t)big8[0]<<8) | (uint16_t)big8[1];
    pBuffer[1] = ((uint16_t)big8[2]<<8) | (uint16_t)big8[3];
}

I get the following result:
0: 0.983450, 0.983450
1: 1.950000, 1.000000
2: 95.949997, 1.000000
3: 0.010000, 0.010000
4: 99.989998, 1.000000
5: -0.940000, -0.940000
6: -1.940000, -1.000000
7: -95.949997, -1.000000
8: -0.010000, -0.010000
9: -99.989998, -1.000000

Where is it going wrong ?

Outcomes