cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F4 Discovery - Write float in Flash memory

filippo
Associate II
Posted on July 10, 2014 at 10:34

I'm trying to receive a float number through a VCP and writing it in the Flash memory of the STM32F4 discovery board.

The functions used to write in the Flash memory (FLASH_ProgramDoubleWord...FLASH_ProgramByte) accept an unsigned integer value as data input,but I've managed to write a signed integer in the Flash memory using this code:

int dataflash1 = -1000;
int gain;
uint32_t Address= 0x08008000;
.......
FLASH_ProgramWord(Address,dataflash1);
.......
gain=*(int*)Address;

Can someone tell me how can I write a float data in the Flash memory? Is it possible to send/receive a float data through VCP? Thank you.
4 REPLIES 4
chen
Associate II
Posted on July 10, 2014 at 13:40

Hi

First understand how the computer/compiler defines a floating point number :

http://en.wikipedia.org/wiki/Floating_point

Look at the section ''

Internal representation

''

and at the ''Single'' representation - this is usually (but not always) what C uses.

''Can someone tell me how can I write a float data in the Flash memory? ''

If you can cast the float into the input parameter of your Flash write function and it writes 4 bytes then it should work.

''Is it possible to send/receive a float data through VCP?''

Have a look at :

http://www.cplusplus.com/reference/cstdlib/atof/?kw=atof

Basically, the number will come in as a string, so it must be converted from the string back into a number (that the computer understands).

filippo
Associate II
Posted on July 10, 2014 at 15:17

Hi,

I already tried to cast the input data by doing:

float dataflash1 = 456;
float gain;
uint32_t Address= 0x08008000;
.......
FLASH_ProgramWord(Address, (uint32)dataflash1);
.......
gain=*(float*)Address;

but I don't know how to interpretate the results: dataflash1 d_gain 456 -> 1.72359711 e-043 123000.456 -> 1.72359711 e-040 9999456 -> 1.40129706 e-039 999999 -> 1.40129706 e-039 - 9999456 -> 0
ivani
Associate II
Posted on July 10, 2014 at 16:13 The direct call:

FLASH_ProgramWord(Address, (uint32)dataflash1);

is actually converting the float variable dataflash1 into equivalent integer value (i.e. 123U instead of 345). You need to do something like:

FLASH_ProgramWord(Address, *(uint32 *)&dataflash1);

to preserve the original format of the variable and only mimic it as an integer container.
filippo
Associate II
Posted on July 10, 2014 at 16:21

Hi Ivan,

It totally makes sense, that's why it works.

Thanks.