flash_stm32.c 6.0 KB

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