[STM32F1] 读写内部Flash

[复制链接]
900|5
 楼主| robertesth 发表于 2024-9-25 13:00 | 显示全部楼层 |阅读模式


  1. 1 #include "stm32f10x_flash.h"
  2. 2
  3. 3 #define StartServerManageFlashAddress    ((u32)0x08036000)//读写起始地址(内部flash的主存储块地址从0x08036000开始)
  4. 4
  5. 5 //从指定地址开始写入多个数据
  6. 6 void FLASH_WriteMoreData(uint32_t startAddress,uint16_t *writeData,uint16_t countToWrite)
  7. 7 {   
  8. 8     uint32_t offsetAddress=startAddress - FLASH_BASE;               //计算去掉0X08000000后的实际偏移地址
  9. 9   uint32_t sectorPosition=offsetAddress/SECTOR_SIZE;            //计算扇区地址,对于STM32F103VET6为0~255
  10. 10   uint32_t sectorStartAddress=sectorPosition*SECTOR_SIZE+FLASH_BASE;    //对应扇区的首地址
  11. 11     uint16_t dataIndex;
  12. 12     
  13. 13   if(startAddress<FLASH_BASE||((startAddress+countToWrite*2)>=(FLASH_BASE + SECTOR_SIZE * FLASH_SIZE)))
  14. 14   {
  15. 15     return;//非法地址
  16. 16   }
  17. 17   FLASH_Unlock();         //解锁写保护
  18. 18
  19. 19   FLASH_ErasePage(sectorStartAddress);//擦除这个扇区
  20. 20   
  21. 21   for(dataIndex=0;dataIndex<countToWrite;dataIndex++)
  22. 22   {
  23. 23     FLASH_ProgramHalfWord(startAddress+dataIndex*2,writeData[dataIndex]);
  24. 24   }
  25. 25   
  26. 26   FLASH_Lock();//上锁写保护
  27. 27 }
  28. 28
  29. 29 //读取指定地址的半字(16位数据)
  30. 30 uint16_t FLASH_ReadHalfWord(uint32_t address)
  31. 31 {
  32. 32   return *(__IO uint16_t*)address;
  33. 33 }
  34. 34
  35. 35 //从指定地址开始读取多个数据
  36. 36 void FLASH_ReadMoreData(uint32_t startAddress,uint16_t *readData,uint16_t countToRead)
  37. 37 {
  38. 38   uint16_t dataIndex;
  39. 39   for(dataIndex=0;dataIndex<countToRead;dataIndex++)
  40. 40   {
  41. 41     readData[dataIndex]=FLASH_ReadHalfWord(startAddress+dataIndex*2);
  42. 42   }
  43. 43 }
  44. 44
  45. 45 void write_to_flash(void)
  46. 46 {
  47. 47     u16 buff[1200];
  48. 48     u16 count_len = 2272 / 2;
  49. 50     FLASH_WriteMoreData(StartServerManageFlashAddress,buff,count_len);
  50. 55 }
  51. 56
  52. 57 void read_from_flash(void)
  53. 58 {
  54. 59     u16 buff[1200];
  55. 60     u16 count_len = 2272 / 2;
  56. 61     FLASH_WriteMoreData(StartServerManageFlashAddress,buff,count_len);
  57. 66
  58. 67  }


onlycook 发表于 2024-9-25 16:26 | 显示全部楼层
内部Flash有哪些品牌和型号
610u 发表于 2024-9-29 22:27 来自手机 | 显示全部楼层
uint32_t offsetAddress=startAddress - FLASH_BASE
g36xcv 发表于 2024-9-29 22:45 来自手机 | 显示全部楼层
   FLASH_WriteMoreData(StartServerManageFlashAddress,buff,count_len)
在水一方00 发表于 2024-9-30 23:56 来自手机 | 显示全部楼层
startAddress,uint16_t *writeData,uint16_t
o88ne 发表于 2024-10-2 21:07 来自手机 | 显示全部楼层
uint32_t sectorStartAddress=sectorPosition*SECTOR_SIZE+FLASH_BASE;    //对应扇区的首地址
您需要登录后才可以回帖 登录 | 注册

本版积分规则

49

主题

1420

帖子

0

粉丝
快速回复 在线客服 返回列表 返回顶部