STM32F4 Discovery - Writing / Reading Flash memory

烈酒焚心 提交于 2019-12-02 03:04:46

Thanks to @phyloflash I got the answer. When you call HAL_FLASH_Program you specify the size of the data to be written and the address of the first byte of this data. In my case, HAL_FLASH_Program(TYPEPROGRAM_WORD, &userConfig[0], data); a word means 4 bytes so the first 4 bytes of userConfig were written. This also implies that parameter defined as uint8_t data has to be consistent with the size of the data you are going to write, so it should be uint32_t data.

I've modified the code to take into account these considerations. Code below is proved and working:

//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);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!