title: c - STM32F1 EEPROM emulation through flash
body: <p>I am trying to emulate EEPROM via flash on my STM32F1, as described <a href="https://stackoverflow.com/questions/49220921/stm32f4-discovery-writing-reading-flash-memory?noredirect=1&amp;lq=1">here</a> (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.</p>
  
  <pre><code>__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&lt;64/pow(2, flashTypeProgram);i++)
       {
           addressGap=pow(2, flashTypeProgram)*i;
           HAL_FLASH_Program(flashTypeProgram, &amp;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&lt;dataSize;i++) {
        dataBuffer[i]=0x1010101;  //0x1010101 puts 1 in each byte of userConfig
    }
    Write_Flash(dataBuffer,flashTypeProgram);
  </code></pre>
  
  <p>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.</p>
  
  <p>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 ?</p>
  
  <pre><code>MEMORY
   {
     DATA (RWX)      : ORIGIN = 0x08048800, LENGTH = 5k      /* 0x08048000 is the beginning of page 107 */
     ...
  }
  </code></pre>
  
  <p>that would be good to allow 5 pages for flash storage ?</p>
  
  <p>Next, I don't exactly understand this line : </p>
  
  <pre><code>__attribute__((__section__(".user_data"))) const char userConfig[64];
  </code></pre>
  
  <p>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 ?</p>
  
  <p>In advance, thanks !</p>