You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

110 satır
3.2KB

  1. #include "stmflash.h"
  2. #include "stm32f10x_flash.h"
  3. u16 StoreBuffer[STORE_DATA_NUM];
  4. u8 Save_Buffer[STORE_DATA_NUM*2];
  5. //读取指定地址的半字(16位数据)
  6. //faddr:读地址(此地址必须为2的倍数!!)
  7. //返回值:对应数据.
  8. u16 STMFLASH_ReadHalfWord(u32 faddr)
  9. {
  10. return *(vu16*)faddr;
  11. }
  12. #if STM32_FLASH_WREN //如果使能了写
  13. //不检查的写入
  14. //WriteAddr:起始地址
  15. //pBuffer:数据指针
  16. //NumToWrite:半字(16位)数
  17. void STMFLASH_Write_NoCheck(u32 WriteAddr,u16 *pBuffer,u16 NumToWrite)
  18. {
  19. u16 i;
  20. for(i=0;i<NumToWrite;i++)
  21. {
  22. FLASH_ProgramHalfWord(WriteAddr,pBuffer[i]);
  23. WriteAddr+=2;//地址增加2.
  24. }
  25. }
  26. //从指定地址开始写入指定长度的数据
  27. //WriteAddr:起始地址(此地址必须为2的倍数!!)
  28. //pBuffer:数据指针
  29. //NumToWrite:半字(16位)数(就是要写入的16位数据的个数.)
  30. #if STM32_FLASH_SIZE<256
  31. #define STM_SECTOR_SIZE 1024 //字节
  32. #else
  33. #define STM_SECTOR_SIZE 2048
  34. #endif
  35. u16 STMFLASH_BUF[STM_SECTOR_SIZE/2];//最多是2K字节
  36. void STMFLASH_Write(u32 WriteAddr,u16 *pBuffer,u16 NumToWrite)
  37. {
  38. u32 secpos; //扇区地址
  39. u16 secoff; //扇区内偏移地址(16位字计算)
  40. u16 secremain; //扇区内剩余地址(16位字计算)
  41. u16 i;
  42. u32 offaddr; //去掉0X08000000后的地址
  43. if(WriteAddr<STM32_FLASH_BASE||(WriteAddr>=(STM32_FLASH_BASE+1024*STM32_FLASH_SIZE)))return;//非法地址
  44. FLASH_Unlock(); //解锁
  45. offaddr=WriteAddr-STM32_FLASH_BASE; //实际偏移地址.
  46. secpos=offaddr/STM_SECTOR_SIZE; //扇区地址 0~127 for STM32F103RBT6
  47. secoff=(offaddr%STM_SECTOR_SIZE)/2; //在扇区内的偏移(2个字节为基本单位.)
  48. secremain=STM_SECTOR_SIZE/2-secoff; //扇区剩余空间大小
  49. if(NumToWrite<=secremain)secremain=NumToWrite;//不大于该扇区范围
  50. while(1)
  51. {
  52. STMFLASH_Read(secpos*STM_SECTOR_SIZE+STM32_FLASH_BASE,STMFLASH_BUF,STM_SECTOR_SIZE/2);//读出整个扇区的内容
  53. for(i=0;i<secremain;i++)//校验数据
  54. {
  55. if(STMFLASH_BUF[secoff+i]!=0XFFFF)break;//需要擦除
  56. }
  57. if(i<secremain)//需要擦除
  58. {
  59. FLASH_ErasePage(secpos*STM_SECTOR_SIZE+STM32_FLASH_BASE);//擦除这个扇区
  60. for(i=0;i<secremain;i++)//复制
  61. {
  62. STMFLASH_BUF[i+secoff]=pBuffer[i];
  63. }
  64. STMFLASH_Write_NoCheck(secpos*STM_SECTOR_SIZE+STM32_FLASH_BASE,STMFLASH_BUF,STM_SECTOR_SIZE/2);//写入整个扇区
  65. }else STMFLASH_Write_NoCheck(WriteAddr,pBuffer,secremain);//写已经擦除了的,直接写入扇区剩余区间.
  66. if(NumToWrite==secremain)break;//写入结束了
  67. else//写入未结束
  68. {
  69. secpos++; //扇区地址增1
  70. secoff=0; //偏移位置为0
  71. pBuffer+=secremain; //指针偏移
  72. WriteAddr+=secremain; //写地址偏移
  73. NumToWrite-=secremain; //字节(16位)数递减
  74. if(NumToWrite>(STM_SECTOR_SIZE/2))secremain=STM_SECTOR_SIZE/2;//下一个扇区还是写不完
  75. else secremain=NumToWrite;//下一个扇区可以写完了
  76. }
  77. };
  78. FLASH_Lock();//上锁
  79. }
  80. #endif
  81. //从指定地址开始读出指定长度的数据
  82. //ReadAddr:起始地址
  83. //pBuffer:数据指针
  84. //NumToWrite:半字(16位)数
  85. void STMFLASH_Read(u32 ReadAddr,u16 *pBuffer,u16 NumToRead)
  86. {
  87. u16 i;
  88. for(i=0;i<NumToRead;i++)
  89. {
  90. pBuffer[i]=STMFLASH_ReadHalfWord(ReadAddr);//读取2个字节.
  91. ReadAddr+=2;//偏移2个字节.
  92. }
  93. }
  94. //////////////////////////////////////////////////////////////////////////////////////////////////////
  95. //WriteAddr:起始地址
  96. //WriteData:要写入的数据
  97. void Test_Write(u32 WriteAddr,u16 WriteData)
  98. {
  99. STMFLASH_Write(WriteAddr,&WriteData,1);//写入一个字
  100. }