flash.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. #include "flash.h"
  2. ////读取指定地址的半字(16位数据)
  3. ////faddr:读地址(此地址必须为2的倍数!!)
  4. ////返回值:对应数据.
  5. u16 STMFLASH_ReadHalfWord(u32 faddr)
  6. {
  7. return *(vu16*)faddr;
  8. }
  9. //
  10. //
  11. //#if STM32_FLASH_WREN //如果使能了写
  12. ////不检查的写入
  13. ////WriteAddr:起始地址
  14. ////pBuffer:数据指针
  15. ////NumToWrite:半字(16位)数
  16. //void STMFLASH_Write_NoCheck(u32 WriteAddr,u16 *pBuffer,u16 NumToWrite)
  17. //{
  18. // u16 i;
  19. // for(i=0;i<NumToWrite;i++)
  20. // {
  21. // FLASH_ProgramHalfWord(WriteAddr,pBuffer[i]);
  22. // WriteAddr+=2;//地址增加2.
  23. // }
  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. //
  36. //
  37. //u16 STMFLASH_BUF[STM_SECTOR_SIZE/2];//最多是2K字节
  38. //void STMFLASH_Write(u32 WriteAddr,u16 *pBuffer,u16 NumToWrite)
  39. //{
  40. // u32 secpos; //扇区地址
  41. // u16 secoff; //扇区内偏移地址(16位字计算)
  42. // u16 secremain; //扇区内剩余地址(16位字计算)
  43. // u16 i;
  44. // u32 offaddr; //去掉0X08000000后的地址
  45. // if(WriteAddr<STM32_FLASH_BASE||(WriteAddr>=(STM32_FLASH_BASE+1024*STM32_FLASH_SIZE)))return;//非法地址
  46. // FLASH_Unlock(); //解锁
  47. // offaddr=WriteAddr-STM32_FLASH_BASE; //实际偏移地址.
  48. // secpos=offaddr/STM_SECTOR_SIZE; //扇区地址 0~127 for STM32F103RBT6
  49. // secoff=(offaddr%STM_SECTOR_SIZE)/2; //在扇区内的偏移(2个字节为基本单位.)
  50. // secremain=STM_SECTOR_SIZE/2-secoff; //扇区剩余空间大小
  51. // if(NumToWrite<=secremain)secremain=NumToWrite;//不大于该扇区范围
  52. // while(1)
  53. // {
  54. // STMFLASH_Read(secpos*STM_SECTOR_SIZE+STM32_FLASH_BASE,STMFLASH_BUF,STM_SECTOR_SIZE/2);//读出整个扇区的内容
  55. // for(i=0;i<secremain;i++)//校验数据
  56. // {
  57. // if(STMFLASH_BUF[secoff+i]!=0XFFFF)break;//需要擦除
  58. // }
  59. // if(i<secremain)//需要擦除
  60. // {
  61. // FLASH_ErasePage(secpos*STM_SECTOR_SIZE+STM32_FLASH_BASE);//擦除这个扇区
  62. // for(i=0;i<secremain;i++)//复制
  63. // {
  64. // STMFLASH_BUF[i+secoff]=pBuffer[i];
  65. // }
  66. // STMFLASH_Write_NoCheck(secpos*STM_SECTOR_SIZE+STM32_FLASH_BASE,STMFLASH_BUF,STM_SECTOR_SIZE/2);//写入整个扇区
  67. // }else STMFLASH_Write_NoCheck(WriteAddr,pBuffer,secremain);//写已经擦除了的,直接写入扇区剩余区间.
  68. // if(NumToWrite==secremain)break;//写入结束了
  69. // else//写入未结束
  70. // {
  71. // secpos++; //扇区地址增1
  72. // secoff=0; //偏移位置为0
  73. // pBuffer+=secremain; //指针偏移
  74. // WriteAddr+=secremain; //写地址偏移
  75. // NumToWrite-=secremain; //字节(16位)数递减
  76. // if(NumToWrite>(STM_SECTOR_SIZE/2))secremain=STM_SECTOR_SIZE/2;//下一个扇区还是写不完
  77. // else secremain=NumToWrite;//下一个扇区可以写完了
  78. // }
  79. // };
  80. // FLASH_Lock();//上锁
  81. //}
  82. //#endif
  83. //
  84. //
  85. //
  86. ////从指定地址开始读出指定长度的数据
  87. ////ReadAddr:起始地址
  88. ////pBuffer:数据指针
  89. ////NumToWrite:半字(16位)数
  90. void STMFLASH_Read(u32 ReadAddr,u16 *pBuffer,u16 NumToRead)
  91. {
  92. u16 i;
  93. for(i=0;i<NumToRead;i++)
  94. {
  95. pBuffer[i]=STMFLASH_ReadHalfWord(ReadAddr);//读取2个字节.
  96. ReadAddr+=2;//偏移2个字节.
  97. }
  98. }
  99. //
  100. ////////////////////////////////////////////////////////////////////////////////////////////////////////
  101. ////WriteAddr:起始地址
  102. ////WriteData:要写入的数据
  103. //void Test_Write(u32 WriteAddr,u16 WriteData)
  104. //{
  105. // STMFLASH_Write(WriteAddr,&WriteData,1);//写入一个字
  106. //}
  107. /**********************************************************************************************************
  108. *写页数据,擦除整页1024字节,并写入Byte数据,数据可少于1024Btye,但必须是偶数。
  109. ************************************************************************************************************/
  110. void WritePageData(unsigned long WriteAddr,unsigned char *pData,unsigned short DataLen)
  111. {
  112. unsigned short i;
  113. unsigned short temp;
  114. FLASH_Status status;
  115. if(WriteAddr<STM32_FLASH_BASE || (WriteAddr>=(STM32_FLASH_BASE+1024*STM32_FLASH_SIZE)))return;//非法地址
  116. if(WriteAddr&0x0000003ff)return;//非页首地址
  117. FLASH_Unlock(); //解锁
  118. status=FLASH_ErasePage(WriteAddr);//擦除这个扇区
  119. if(status!=FLASH_COMPLETE){
  120. return;
  121. }
  122. //写入
  123. for(i = 0; i < DataLen; i+=2)
  124. {
  125. //数据8位融合为16位
  126. temp = (((unsigned short)pData[i+1])<<8) + ((unsigned short)pData[i]);
  127. FLASH_ProgramHalfWord((WriteAddr+i),temp);
  128. }
  129. FLASH_Lock();//上锁
  130. }
  131. /**********************************************************************************************************
  132. ************************************************************************************************************/
  133. void ReadFlashData(unsigned long ReadAddr,unsigned char *pData,unsigned short DataLen)
  134. {
  135. int i,j;
  136. unsigned short temp;
  137. j=0;
  138. for(i=0;i<DataLen;i+=2){
  139. temp=STMFLASH_ReadHalfWord(ReadAddr+i);
  140. pData[j++]=(unsigned char)(temp&0xff);
  141. pData[j++]=(temp>>8)&0xff;
  142. }
  143. }