flash_stm32.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. /*
  2. * This software is experimental and a work in progress.
  3. * Under no circumstances should these files be used in relation to any critical system(s).
  4. * Use of these files is at your own risk.
  5. *
  6. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
  7. * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  8. * PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  9. * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  10. * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  11. * DEALINGS IN THE SOFTWARE.
  12. *
  13. * This files are free to use from https://github.com/rogerclarkmelbourne/Arduino_STM32 and
  14. * https://github.com/leaflabs/libmaple
  15. *
  16. * Modifications for QMK and STM32F303 by Yiancar
  17. */
  18. #if defined(EEPROM_EMU_STM32F303xC)
  19. #define STM32F303xC
  20. #include "stm32f3xx.h"
  21. #elif defined(EEPROM_EMU_STM32F103xB)
  22. #define STM32F103xB
  23. #include "stm32f1xx.h"
  24. #elif defined(EEPROM_EMU_STM32F072xB)
  25. #define STM32F072xB
  26. #include "stm32f0xx.h"
  27. #else
  28. #error "not implemented."
  29. #endif
  30. #include "flash_stm32.h"
  31. #if defined(EEPROM_EMU_STM32F103xB)
  32. #define FLASH_SR_WRPERR FLASH_SR_WRPRTERR
  33. #endif
  34. /* Delay definition */
  35. #define EraseTimeout ((uint32_t)0x00000FFF)
  36. #define ProgramTimeout ((uint32_t)0x0000001F)
  37. #define ASSERT(exp) (void)((0))
  38. /**
  39. * @brief Inserts a time delay.
  40. * @param None
  41. * @retval None
  42. */
  43. static void delay(void)
  44. {
  45. __IO uint32_t i = 0;
  46. for(i = 0xFF; i != 0; i--) { }
  47. }
  48. /**
  49. * @brief Returns the FLASH Status.
  50. * @param None
  51. * @retval FLASH Status: The returned value can be: FLASH_BUSY, FLASH_ERROR_PG,
  52. * FLASH_ERROR_WRP or FLASH_COMPLETE
  53. */
  54. FLASH_Status FLASH_GetStatus(void)
  55. {
  56. if ((FLASH->SR & FLASH_SR_BSY) == FLASH_SR_BSY)
  57. return FLASH_BUSY;
  58. if ((FLASH->SR & FLASH_SR_PGERR) != 0)
  59. return FLASH_ERROR_PG;
  60. if ((FLASH->SR & FLASH_SR_WRPERR) != 0 )
  61. return FLASH_ERROR_WRP;
  62. if ((FLASH->SR & FLASH_OBR_OPTERR) != 0 )
  63. return FLASH_ERROR_OPT;
  64. return FLASH_COMPLETE;
  65. }
  66. /**
  67. * @brief Waits for a Flash operation to complete or a TIMEOUT to occur.
  68. * @param Timeout: FLASH progamming Timeout
  69. * @retval FLASH Status: The returned value can be: FLASH_ERROR_PG,
  70. * FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT.
  71. */
  72. FLASH_Status FLASH_WaitForLastOperation(uint32_t Timeout)
  73. {
  74. FLASH_Status status;
  75. /* Check for the Flash Status */
  76. status = FLASH_GetStatus();
  77. /* Wait for a Flash operation to complete or a TIMEOUT to occur */
  78. while ((status == FLASH_BUSY) && (Timeout != 0x00))
  79. {
  80. delay();
  81. status = FLASH_GetStatus();
  82. Timeout--;
  83. }
  84. if (Timeout == 0)
  85. status = FLASH_TIMEOUT;
  86. /* Return the operation status */
  87. return status;
  88. }
  89. /**
  90. * @brief Erases a specified FLASH page.
  91. * @param Page_Address: The page address to be erased.
  92. * @retval FLASH Status: The returned value can be: FLASH_BUSY, FLASH_ERROR_PG,
  93. * FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT.
  94. */
  95. FLASH_Status FLASH_ErasePage(uint32_t Page_Address)
  96. {
  97. FLASH_Status status = FLASH_COMPLETE;
  98. /* Check the parameters */
  99. ASSERT(IS_FLASH_ADDRESS(Page_Address));
  100. /* Wait for last operation to be completed */
  101. status = FLASH_WaitForLastOperation(EraseTimeout);
  102. if(status == FLASH_COMPLETE)
  103. {
  104. /* if the previous operation is completed, proceed to erase the page */
  105. FLASH->CR |= FLASH_CR_PER;
  106. FLASH->AR = Page_Address;
  107. FLASH->CR |= FLASH_CR_STRT;
  108. /* Wait for last operation to be completed */
  109. status = FLASH_WaitForLastOperation(EraseTimeout);
  110. if(status != FLASH_TIMEOUT)
  111. {
  112. /* if the erase operation is completed, disable the PER Bit */
  113. FLASH->CR &= ~FLASH_CR_PER;
  114. }
  115. FLASH->SR = (FLASH_SR_EOP | FLASH_SR_PGERR | FLASH_SR_WRPERR);
  116. }
  117. /* Return the Erase Status */
  118. return status;
  119. }
  120. /**
  121. * @brief Programs a half word at a specified address.
  122. * @param Address: specifies the address to be programmed.
  123. * @param Data: specifies the data to be programmed.
  124. * @retval FLASH Status: The returned value can be: FLASH_ERROR_PG,
  125. * FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT.
  126. */
  127. FLASH_Status FLASH_ProgramHalfWord(uint32_t Address, uint16_t Data)
  128. {
  129. FLASH_Status status = FLASH_BAD_ADDRESS;
  130. if (IS_FLASH_ADDRESS(Address))
  131. {
  132. /* Wait for last operation to be completed */
  133. status = FLASH_WaitForLastOperation(ProgramTimeout);
  134. if(status == FLASH_COMPLETE)
  135. {
  136. /* if the previous operation is completed, proceed to program the new data */
  137. FLASH->CR |= FLASH_CR_PG;
  138. *(__IO uint16_t*)Address = Data;
  139. /* Wait for last operation to be completed */
  140. status = FLASH_WaitForLastOperation(ProgramTimeout);
  141. if(status != FLASH_TIMEOUT)
  142. {
  143. /* if the program operation is completed, disable the PG Bit */
  144. FLASH->CR &= ~FLASH_CR_PG;
  145. }
  146. FLASH->SR = (FLASH_SR_EOP | FLASH_SR_PGERR | FLASH_SR_WRPERR);
  147. }
  148. }
  149. return status;
  150. }
  151. /**
  152. * @brief Unlocks the FLASH Program Erase Controller.
  153. * @param None
  154. * @retval None
  155. */
  156. void FLASH_Unlock(void)
  157. {
  158. /* Authorize the FPEC Access */
  159. FLASH->KEYR = FLASH_KEY1;
  160. FLASH->KEYR = FLASH_KEY2;
  161. }
  162. /**
  163. * @brief Locks the FLASH Program Erase Controller.
  164. * @param None
  165. * @retval None
  166. */
  167. void FLASH_Lock(void)
  168. {
  169. /* Set the Lock Bit to lock the FPEC and the FCR */
  170. FLASH->CR |= FLASH_CR_LOCK;
  171. }
  172. /**
  173. * @brief Clears the FLASH's pending flags.
  174. * @param FLASH_FLAG: specifies the FLASH flags to clear.
  175. * This parameter can be any combination of the following values:
  176. * @arg FLASH_FLAG_PGERR: FLASH Programming error flag flag
  177. * @arg FLASH_FLAG_WRPERR: FLASH Write protected error flag
  178. * @arg FLASH_FLAG_EOP: FLASH End of Programming flag
  179. * @retval None
  180. */
  181. void FLASH_ClearFlag(uint32_t FLASH_FLAG)
  182. {
  183. /* Clear the flags */
  184. FLASH->SR = FLASH_FLAG;
  185. }