STM32F4发现-写入/读取闪存 [英] STM32F4 Discovery - Writing / Reading Flash memory

查看:173
本文介绍了STM32F4发现-写入/读取闪存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我在这里的第一篇文章,对不起,如果格式或某些内容有误.我按照我们大学的建议成功写入和读取STM32F4 Discovery的闪存此处(两个帖子都用相同的方式解释):

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

上面的代码可以正常工作,并将1写入userConfig[0].从这一点来看,我有以下问题.我只能在闪存中写入一个字节,却不知道如何写入更多字节.我试图更改HAL_FLASH_Program(TYPEPROGRAM_WORD, &userConfig[0], data);的地址,但仅适用于&userConfig[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);
  }

预先感谢您的帮助.

推荐答案

感谢@phyloflash,我得到了答案.调用HAL_FLASH_Program时,可以指定要写入的数据的大小以及该数据的第一个字节的地址. 在我的情况下,HAL_FLASH_Program(TYPEPROGRAM_WORD, &userConfig[0], data);一个单词表示4个字节,因此写入了userConfig的前4个字节.这也意味着定义为uint8_t data的参数必须与要写入的数据大小一致,因此应为uint32_t data.

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

这篇关于STM32F4发现-写入/读取闪存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆