flash_stm32.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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. #elif defined(EEPROM_EMU_STM32F042x6)
  28. # define STM32F042x6
  29. # include "stm32f0xx.h"
  30. #else
  31. # error "not implemented."
  32. #endif
  33. #include "flash_stm32.h"
  34. #if defined(EEPROM_EMU_STM32F103xB)
  35. # define FLASH_SR_WRPERR FLASH_SR_WRPRTERR
  36. #endif
  37. /* Delay definition */
  38. #define EraseTimeout ((uint32_t)0x00000FFF)
  39. #define ProgramTimeout ((uint32_t)0x0000001F)
  40. #define ASSERT(exp) (void)((0))
  41. /**
  42. * @brief Inserts a time delay.
  43. * @param None
  44. * @retval None
  45. */
  46. static void delay(void) {
  47. __IO uint32_t i = 0;
  48. for (i = 0xFF; i != 0; i--) {
  49. }
  50. }
  51. /**
  52. * @brief Returns the FLASH Status.
  53. * @param None
  54. * @retval FLASH Status: The returned value can be: FLASH_BUSY, FLASH_ERROR_PG,
  55. * FLASH_ERROR_WRP or FLASH_COMPLETE
  56. */
  57. FLASH_Status FLASH_GetStatus(void) {
  58. if ((FLASH->SR & FLASH_SR_BSY) == FLASH_SR_BSY) return FLASH_BUSY;
  59. if ((FLASH->SR & FLASH_SR_PGERR) != 0) return FLASH_ERROR_PG;
  60. if ((FLASH->SR & FLASH_SR_WRPERR) != 0) return FLASH_ERROR_WRP;
  61. if ((FLASH->SR & FLASH_OBR_OPTERR) != 0) return FLASH_ERROR_OPT;
  62. return FLASH_COMPLETE;
  63. }
  64. /**
  65. * @brief Waits for a Flash operation to complete or a TIMEOUT to occur.
  66. * @param Timeout: FLASH progamming Timeout
  67. * @retval FLASH Status: The returned value can be: FLASH_ERROR_PG,
  68. * FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT.
  69. */
  70. FLASH_Status FLASH_WaitForLastOperation(uint32_t Timeout) {
  71. FLASH_Status status;
  72. /* Check for the Flash Status */
  73. status = FLASH_GetStatus();
  74. /* Wait for a Flash operation to complete or a TIMEOUT to occur */
  75. while ((status == FLASH_BUSY) && (Timeout != 0x00)) {
  76. delay();
  77. status = FLASH_GetStatus();
  78. Timeout--;
  79. }
  80. if (Timeout == 0) status = FLASH_TIMEOUT;
  81. /* Return the operation status */
  82. return status;
  83. }
  84. /**
  85. * @brief Erases a specified FLASH page.
  86. * @param Page_Address: The page address to be erased.
  87. * @retval FLASH Status: The returned value can be: FLASH_BUSY, FLASH_ERROR_PG,
  88. * FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT.
  89. */
  90. FLASH_Status FLASH_ErasePage(uint32_t Page_Address) {
  91. FLASH_Status status = FLASH_COMPLETE;
  92. /* Check the parameters */
  93. ASSERT(IS_FLASH_ADDRESS(Page_Address));
  94. /* Wait for last operation to be completed */
  95. status = FLASH_WaitForLastOperation(EraseTimeout);
  96. if (status == FLASH_COMPLETE) {
  97. /* if the previous operation is completed, proceed to erase the page */
  98. FLASH->CR |= FLASH_CR_PER;
  99. FLASH->AR = Page_Address;
  100. FLASH->CR |= FLASH_CR_STRT;
  101. /* Wait for last operation to be completed */
  102. status = FLASH_WaitForLastOperation(EraseTimeout);
  103. if (status != FLASH_TIMEOUT) {
  104. /* if the erase operation is completed, disable the PER Bit */
  105. FLASH->CR &= ~FLASH_CR_PER;
  106. }
  107. FLASH->SR = (FLASH_SR_EOP | FLASH_SR_PGERR | FLASH_SR_WRPERR);
  108. }
  109. /* Return the Erase Status */
  110. return status;
  111. }
  112. /**
  113. * @brief Programs a half word at a specified address.
  114. * @param Address: specifies the address to be programmed.
  115. * @param Data: specifies the data to be programmed.
  116. * @retval FLASH Status: The returned value can be: FLASH_ERROR_PG,
  117. * FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT.
  118. */
  119. FLASH_Status FLASH_ProgramHalfWord(uint32_t Address, uint16_t Data) {
  120. FLASH_Status status = FLASH_BAD_ADDRESS;
  121. if (IS_FLASH_ADDRESS(Address)) {
  122. /* Wait for last operation to be completed */
  123. status = FLASH_WaitForLastOperation(ProgramTimeout);
  124. if (status == FLASH_COMPLETE) {
  125. /* if the previous operation is completed, proceed to program the new data */
  126. FLASH->CR |= FLASH_CR_PG;
  127. *(__IO uint16_t*)Address = Data;
  128. /* Wait for last operation to be completed */
  129. status = FLASH_WaitForLastOperation(ProgramTimeout);
  130. if (status != FLASH_TIMEOUT) {
  131. /* if the program operation is completed, disable the PG Bit */
  132. FLASH->CR &= ~FLASH_CR_PG;
  133. }
  134. FLASH->SR = (FLASH_SR_EOP | FLASH_SR_PGERR | FLASH_SR_WRPERR);
  135. }
  136. }
  137. return status;
  138. }
  139. /**
  140. * @brief Unlocks the FLASH Program Erase Controller.
  141. * @param None
  142. * @retval None
  143. */
  144. void FLASH_Unlock(void) {
  145. /* Authorize the FPEC Access */
  146. FLASH->KEYR = FLASH_KEY1;
  147. FLASH->KEYR = FLASH_KEY2;
  148. }
  149. /**
  150. * @brief Locks the FLASH Program Erase Controller.
  151. * @param None
  152. * @retval None
  153. */
  154. void FLASH_Lock(void) {
  155. /* Set the Lock Bit to lock the FPEC and the FCR */
  156. FLASH->CR |= FLASH_CR_LOCK;
  157. }
  158. /**
  159. * @brief Clears the FLASH's pending flags.
  160. * @param FLASH_FLAG: specifies the FLASH flags to clear.
  161. * This parameter can be any combination of the following values:
  162. * @arg FLASH_FLAG_PGERR: FLASH Programming error flag flag
  163. * @arg FLASH_FLAG_WRPERR: FLASH Write protected error flag
  164. * @arg FLASH_FLAG_EOP: FLASH End of Programming flag
  165. * @retval None
  166. */
  167. void FLASH_ClearFlag(uint32_t FLASH_FLAG) {
  168. /* Clear the flags */
  169. FLASH->SR = FLASH_FLAG;
  170. }