1

So I have a buffer:

uint32_t buff[2];
buff[0] = 12;
buff[1] = 13;
...

I can write this to the flash memory with the method:

HAL_FLASH_Program(TYPEPROGRAM_WORD, (uint32_t)(startAddress+(i*4)), *buff)

The definition of HAL_FLASH_Program is:

HAL_StatusTypeDef HAL_FLASH_Program(uint32_t TypeProgram, uint32_t Address, uint64_t Data)

That works perfectly. Now is there a way I can store chars instead or ints?

timrau
  • 22,578
  • 4
  • 51
  • 64
matt
  • 2,312
  • 5
  • 34
  • 57
  • See [Allocating memory in Flash for user data (STM32F4 HAL)](https://stackoverflow.com/a/28505272/1398841) – phoenix Sep 25 '18 at 14:11

1 Answers1

1

You can use HAL_FLASH_Program with TYPEPROGRAM_BYTE to write a single 1-byte char.

If your data is a bit long (a struct, a string...), you can also write the bulk with TYPEPROGRAM_WORD, or even TYPEPROGRAM_DOUBLEWORD (8 bytes at a time), and then either complete with single bytes as needed or pad the excess with zeros. That would certainly be a bit faster, but maybe it's not significant for you.

ElderBug
  • 5,926
  • 16
  • 25
  • My data is just `TCHAR id[11]` it then contains something like `['a','B','c','D', ...]`. Could I just do `HAL_FLASH_Program(TYPEPROGRAM_BYTE, (uint32_t)(startAddress+(i*4)), *id[i]);` ? – matt Jun 11 '15 at 10:34
  • 1
    @matt Yes, if you do that in a loop. Except that it would be `startAddress+i` (or `startAddress+i*sizeof(TCHAR)`) and `id[i]`. You also have to make sure `TCHAR` is 1 byte. `char` is always 1 byte, but `TCHAR` could be more. – ElderBug Jun 11 '15 at 11:02
  • @matt Also, if you want to store many things, you can use a struct with everything you want inside as a mapping, then store and load using `startAddress+offsetof(map,id[0])`. – ElderBug Jun 11 '15 at 11:08