STM32F4 Discovery - Writing / Reading Flash memory

二次信任 提交于 2019-12-31 04:51:09

问题


This is my first post here, sorry if format or something is wrong. I succeed writing and reading flash memory of the STM32F4 Discovery following the advises of our colleges here and here (both posts explain the same way):

__attribute__((__section__(".user_data"))) const char userConfig[64];
[...]
void Write_Flash(uint8_t data)
{
     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);
     HAL_FLASH_Program(TYPEPROGRAM_WORD, &userConfig[0], data);
     HAL_FLASH_Lock();
}
[...]
  dataSize=(sizeof dataBuffer) / (sizeof *dataBuffer);
  for (i=0;i<dataSize;i++) {
      dataBuffer[i]=i+1;
  }
  Write_Flash(dataBuffer[0]);

The above code works fine and writes 1 to userConfig[0]. From this point, I have the following problem. I can only write one byte in the flash memory and don't know how to write more. I've tried to change the address of the HAL_FLASH_Program(TYPEPROGRAM_WORD, &userConfig[0], data); but only works for &userConfig[0] This is my attempt to write several bytes with no success:

__attribute__((__section__(".user_data"))) const char userConfig[64];
[...]
void Write_Flash(uint8_t data, uint8_t i)
    {
         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);
         HAL_FLASH_Program(TYPEPROGRAM_WORD, &userConfig[i], data);
         HAL_FLASH_Lock();
    }
[...]
dataSize=(sizeof dataBuffer) / (sizeof *dataBuffer);
for (i=0;i<dataSize;i++) {
      dataBuffer[i]=i+1;
      Write_Flash(dataBuffer[i],i);
  }

Thanks in advance for your help.


回答1:


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);


来源:https://stackoverflow.com/questions/49220921/stm32f4-discovery-writing-reading-flash-memory

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