Flash.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. #include "Flash.h"
  2. /********************************
  3. * @brief OTA参数初始化
  4. * @retval FLASH_COMPLETE初始成功
  5. ********************************/
  6. uint8_t Flash_RangeErase(uint32_t FlashAddress, uint32_t Size)
  7. {
  8. uint8_t flag = 1;
  9. uint32_t msize=0;
  10. FLASH_Status FlashStatus = FLASH_COMPLETE;
  11. while(Size > msize) {
  12. FLASH_Unlock();// Unlock the Flash to enable the flash control register access
  13. FLASH_ClearFlag(FLASH_FLAG_EOP | FLASH_FLAG_PGERR | FLASH_FLAG_WRPRTERR); // Clear pending flags (if any)
  14. FlashStatus = FLASH_ErasePage(FlashAddress+msize);
  15. FLASH_Lock(); // Lock the Flash to disable the flash control register access
  16. /* If erase operation was failed, a Flash error code is returned */
  17. if (FlashStatus != FLASH_COMPLETE) {
  18. flag = 0;
  19. break;
  20. }
  21. msize += FLASH_PAGE_SIZE;
  22. }
  23. return flag;
  24. }
  25. #if 1
  26. uint8_t Flash_PageErase(uint8_t _PageNumber)
  27. {
  28. uint8_t flag = 0;
  29. if(_PageNumber > 127)
  30. return 0;
  31. FLASH_Unlock();// Unlock the Flash to enable the flash control register access
  32. FLASH_ClearFlag(FLASH_FLAG_EOP | FLASH_FLAG_PGERR | FLASH_FLAG_WRPRTERR); // Clear pending flags (if any)
  33. if( FLASH_ErasePage(FLASH_USER_START_ADDR + (FLASH_PAGE_SIZE * _PageNumber)) == FLASH_COMPLETE )
  34. {
  35. flag = 1;
  36. }
  37. FLASH_Lock(); // Lock the Flash to disable the flash control register access
  38. return flag;
  39. }
  40. uint8_t Flash_BufferWrite(uint32_t address, uint32_t *p_buffer, uint32_t len)
  41. {
  42. uint8_t flag = 1;
  43. uint32_t EndAddr = 0;
  44. //uint32_t *p_buffer = (uint32_t *)buffer;
  45. if(address%4||len%4) {
  46. return 0;
  47. }
  48. FLASH_Unlock();// Unlock the Flash to enable the flash control register access
  49. FLASH_ClearFlag(FLASH_FLAG_EOP | FLASH_FLAG_PGERR | FLASH_FLAG_WRPRTERR); // Clear pending flags (if any)
  50. EndAddr = address + len;
  51. while (address < EndAddr) {
  52. if (FLASH_ProgramWord(address, *p_buffer) == FLASH_COMPLETE) {
  53. p_buffer++;
  54. address += 4;
  55. } else {
  56. flag = 0;// Error occurred while writing data in Flash memory.
  57. }
  58. }
  59. FLASH_Lock(); // Lock the Flash to disable the flash control register access
  60. return flag;
  61. }
  62. void Flash_BufferRead(uint32_t address, uint8_t *buffer, uint32_t len)
  63. {
  64. uint32_t EndAddr = 0;
  65. uint8_t *p_buffer = (uint8_t *)buffer;
  66. if(address%4||len%4) {
  67. return ;
  68. }
  69. FLASH_Unlock();// Unlock the Flash to enable the flash control register access
  70. FLASH_ClearFlag(FLASH_FLAG_EOP | FLASH_FLAG_PGERR | FLASH_FLAG_WRPRTERR); // Clear pending flags (if any)
  71. EndAddr = address + len;
  72. while (address < EndAddr) {
  73. *p_buffer++ = *(__IO uint8_t *)address;
  74. address += 1;
  75. }
  76. FLASH_Lock(); // Lock the Flash to disable the flash control register access
  77. }
  78. //读取flash中有效数据的长度
  79. uint32_t Flash_DatalenRead(uint32_t address,uint32_t len)
  80. {
  81. uint32_t BeginAddr = 0;
  82. uint32_t addrlen = 0;
  83. uint32_t data;
  84. if(address%4||len%4) {
  85. return addrlen;
  86. }
  87. FLASH_Unlock();// Unlock the Flash to enable the flash control register access
  88. FLASH_ClearFlag(FLASH_FLAG_EOP | FLASH_FLAG_PGERR | FLASH_FLAG_WRPRTERR); // Clear pending flags (if any)
  89. BeginAddr = address + len - 4;
  90. while(address < BeginAddr)
  91. {
  92. data = *(__IO uint32_t *)BeginAddr;
  93. if(data != 0xFFFFFFFF)
  94. break;
  95. BeginAddr -= 4;
  96. }
  97. FLASH_Lock(); // Lock the Flash to disable the flash control register access
  98. if(address <= BeginAddr)
  99. {
  100. addrlen = (BeginAddr+4) - address;
  101. }
  102. return addrlen;
  103. }
  104. #else
  105. uint8_t Flash_PageErase(uint8_t _PageNumber)
  106. {
  107. uint8_t flag = 0;
  108. if(_PageNumber > 127)
  109. return 0;
  110. FLASH_Unlock();// Unlock the Flash to enable the flash control register access
  111. FLASH_ClearFlag(FLASH_FLAG_EOP | FLASH_FLAG_PGERR | FLASH_FLAG_WRPRTERR); // Clear pending flags (if any)
  112. if( FLASH_ErasePage(FLASH_USER_START_ADDR + (FLASH_PAGE_SIZE * _PageNumber)) == FLASH_COMPLETE )
  113. {
  114. flag = 1;
  115. }
  116. FLASH_Lock(); // Lock the Flash to disable the flash control register access
  117. return flag;
  118. }
  119. uint8_t Flash_BufferWrite(uint8_t _PageNumber,uint32_t *pBuffer,uint16_t Num)
  120. {
  121. uint8_t flag = 1;
  122. uint32_t Address = 0,EndAddr = 0;
  123. FLASH_Unlock();// Unlock the Flash to enable the flash control register access
  124. FLASH_ClearFlag(FLASH_FLAG_EOP | FLASH_FLAG_PGERR | FLASH_FLAG_WRPRTERR); // Clear pending flags (if any)
  125. Address = FLASH_USER_START_ADDR + FLASH_PAGE_SIZE * _PageNumber;
  126. EndAddr = Address + Num;
  127. while (Address < EndAddr) {
  128. if (FLASH_ProgramWord(Address, *pBuffer++) == FLASH_COMPLETE) {
  129. Address += 4;
  130. } else {
  131. flag = 0;// Error occurred while writing data in Flash memory.
  132. }
  133. }
  134. FLASH_Lock(); // Lock the Flash to disable the flash control register access
  135. return flag;
  136. }
  137. /*
  138. void Flash_BufferRead(uint8_t _PageNumber,uint32_t *pBuffer,uint16_t Num)
  139. {
  140. uint8_t i;
  141. uint32_t Address = 0;
  142. Address = FLASH_USER_START_ADDR + (FLASH_PAGE_SIZE * _PageNumber );
  143. for( i=0; i<Num/4; i++ )
  144. {
  145. *pBuffer++ = *(__IO uint32_t *)Address;
  146. Address += 4;
  147. }
  148. for( i=0; i<Num%4; i++ )
  149. {
  150. *(uint8_t *)pBuffer = *(__IO uint8_t *)Address;
  151. ( (uint8_t *)pBuffer )++;//地址加1
  152. Address += 1;
  153. }
  154. }*/
  155. void Flash_BufferRead(uint8_t _PageNumber,uint8_t *pBuffer,uint16_t Num)
  156. {
  157. uint8_t i;
  158. uint32_t Address = 0;
  159. Address = FLASH_USER_START_ADDR + (FLASH_PAGE_SIZE * _PageNumber );
  160. for( i=0; i< Num; i++)
  161. {
  162. *pBuffer++ = *(__IO uint8_t *)Address++;
  163. }
  164. }
  165. #endif