-1

I am trying to emulate EEPROM via flash on my STM32F1, as described here (for STM32F4) for example, but I am struggling to change the code for STM32F1RB (medium density) specifications. I am using SW4STM32, CubeMX and the HAL API.

__attribute__((__section__(".user_data"))) const char userConfig[64];

//data type have to be consistent with the TYPEPROGRAM, i.e:
//TYPEPROGRAM_BYTE uint8_t data
//TYPEPROGRAM_HALFWORD uint16_t data
//TYPEPROGRAM_WORD uint32_t data
//TYPEPROGRAM_DOUBLEWORD uint64_t data
void Write_Flash(uint32_t data[],uint8_t flashTypeProgram)

{
     uint8_t addressGap;
     HAL_FLASH_Unlock();
     __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP | FLASH_FLAG_OPERR | FLASH_FLAG_WRPERR | FLASH_FLAG_PGAERR | FLASH_FLAG_PGSERR );
     FLASH_Erase_Sector(FLASH_SECTOR_6, VOLTAGE_RANGE_3);
     for (i=0;i<64/pow(2, flashTypeProgram);i++)
     {
         addressGap=pow(2, flashTypeProgram)*i;
         HAL_FLASH_Program(flashTypeProgram, &userConfig[0]+addressGap, data[i]);
     }
     HAL_FLASH_Lock();
     //TYPEPROGRAM_BYTE        Program byte (8-bit) at a specified address              $0
     //TYPEPROGRAM_HALFWORD    Program a half-word (16-bit) at a specified address      $1
     //TYPEPROGRAM_WORD        Program a word (32-bit) at a specified address           $2
     //TYPEPROGRAM_DOUBLEWORD  Program a double word (64-bit) at a specified address    $3
}
[...]
  flashTypeProgram=TYPEPROGRAM_WORD;
  dataSize=(sizeof dataBuffer) / (sizeof *dataBuffer);
  for (i=0;i<dataSize;i++) {
      dataBuffer[i]=0x1010101;  //0x1010101 puts 1 in each byte of userConfig
  }
  Write_Flash(dataBuffer,flashTypeProgram);

I looked into the way of writing data into flash memory with the HAL API, but as I don't want to "ruin" my board, messing with wrong memory part, I want to make sure I understand everything before trying on my own.

First of all, let's say I want to have 5 kbytes of data to be stored. How should I allow the Data zone ? starting from the end of the main memory zone ? For example I could assign pages 127 to 122 for my Data ? like this ?

MEMORY
 {
   DATA (RWX)      : ORIGIN = 0x08048800, LENGTH = 5k      /* 0x08048000 is the beginning of page 107 */
   ...
}

that would be good to allow 5 pages for flash storage ?

Next, I don't exactly understand this line :

__attribute__((__section__(".user_data"))) const char userConfig[64];

I get that it's for the user to be able to read the Flash stored data, but why is it assigned this way ? I mean, 64*sizeof(char) = 64 bytes, right ? How is he assigning only 64 bytes for reading purposes while he allowed 128 kB of storage ?

In advance, thanks !

weew
  • 363
  • 3
  • 12
J.Doe
  • 1
  • 2

1 Answers1

1

Firstly you should said that you're trying to write something in the Data Flash. The main difference is that you have to erase an entire page to write something. During data copying to the EEPROM there is no such kind of restriction.

I see that you're using only a HAL Library from someone's other sources and I suppose that you don't have full control what excacly you're doing. Please refer to the Flash Programming Manual for the STM32F1xx. There are written steps what excacly you should do and what registers are important during Data Flash programming.

Don't be affraid, you will not destroy anything.

Please do not modify below section. It only means where the Flash starts and how many bytes there is. You should be only interested by the code which will modify the Flash content (your code which you'll write).

MEMORY
{
   DATA (RWX)      : ORIGIN = 0x08048800, LENGTH = 5k      /* 0x08048000 is the beginning of page 107 *

I don't have HAL API in front of me but I suppose it requires an integer to be an address to which the data is written.

Firstly please reduce your code to minimal form. Resign from the loop and try to program only a half word (2 bytes) - the programming in this CPU is performed on the half words so your address has to be aligned to two.

Below line is just a declaration of some memory. You don't need it to program the Data Flash.

 __attribute__((__section__(".user_data"))) const char userConfig[64];

Did you look into the manual which I had recomended you?

pelkaid
  • 41
  • 4
  • Sorry, I wasn't too clear when I wrote my question, I'll edit it to expand on it. Thank you in advance. – J.Doe Jul 03 '18 at 09:07