Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

stmflash.c 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. }