flash_stm32.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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. #include <hal.h>
  19. #include "flash_stm32.h"
  20. #if defined(EEPROM_EMU_STM32F103xB)
  21. # define FLASH_SR_WRPERR FLASH_SR_WRPRTERR
  22. #endif
  23. #if defined(EEPROM_EMU_STM32F401xC)
  24. # define FLASH_SR_PGERR (FLASH_SR_PGSERR | FLASH_SR_PGPERR | FLASH_SR_PGAERR)
  25. # define FLASH_KEY1 0x45670123U
  26. # define FLASH_KEY2 0xCDEF89ABU
  27. static uint8_t ADDR2PAGE(uint32_t Page_Address) {
  28. switch (Page_Address) {
  29. case 0x08000000 ... 0x08003FFF:
  30. return 0;
  31. case 0x08004000 ... 0x08007FFF:
  32. return 1;
  33. case 0x08008000 ... 0x0800BFFF:
  34. return 2;
  35. case 0x0800C000 ... 0x0800FFFF:
  36. return 3;
  37. }
  38. // TODO: bad times...
  39. return 7;
  40. }
  41. #endif
  42. /* Delay definition */
  43. #define EraseTimeout ((uint32_t)0x00000FFF)
  44. #define ProgramTimeout ((uint32_t)0x0000001F)
  45. #define ASSERT(exp) (void)((0))
  46. /**
  47. * @brief Inserts a time delay.
  48. * @param None
  49. * @retval None
  50. */
  51. static void delay(void) {
  52. __IO uint32_t i = 0;
  53. for (i = 0xFF; i != 0; i--) {
  54. }
  55. }
  56. /**
  57. * @brief Returns the FLASH Status.
  58. * @param None
  59. * @retval FLASH Status: The returned value can be: FLASH_BUSY, FLASH_ERROR_PG,
  60. * FLASH_ERROR_WRP or FLASH_COMPLETE
  61. */
  62. FLASH_Status FLASH_GetStatus(void) {
  63. if ((FLASH->SR & FLASH_SR_BSY) == FLASH_SR_BSY) return FLASH_BUSY;
  64. if ((FLASH->SR & FLASH_SR_PGERR) != 0) return FLASH_ERROR_PG;
  65. if ((FLASH->SR & FLASH_SR_WRPERR) != 0) return FLASH_ERROR_WRP;
  66. #if defined(FLASH_OBR_OPTERR)
  67. if ((FLASH->SR & FLASH_OBR_OPTERR) != 0) return FLASH_ERROR_OPT;
  68. #endif
  69. return FLASH_COMPLETE;
  70. }
  71. /**
  72. * @brief Waits for a Flash operation to complete or a TIMEOUT to occur.
  73. * @param Timeout: FLASH progamming Timeout
  74. * @retval FLASH Status: The returned value can be: FLASH_ERROR_PG,
  75. * FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT.
  76. */
  77. FLASH_Status FLASH_WaitForLastOperation(uint32_t Timeout) {
  78. FLASH_Status status;
  79. /* Check for the Flash Status */
  80. status = FLASH_GetStatus();
  81. /* Wait for a Flash operation to complete or a TIMEOUT to occur */
  82. while ((status == FLASH_BUSY) && (Timeout != 0x00)) {
  83. delay();
  84. status = FLASH_GetStatus();
  85. Timeout--;
  86. }
  87. if (Timeout == 0) status = FLASH_TIMEOUT;
  88. /* Return the operation status */
  89. return status;
  90. }
  91. /**
  92. * @brief Erases a specified FLASH page.
  93. * @param Page_Address: The page address to be erased.
  94. * @retval FLASH Status: The returned value can be: FLASH_BUSY, FLASH_ERROR_PG,
  95. * FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT.
  96. */
  97. FLASH_Status FLASH_ErasePage(uint32_t Page_Address) {
  98. FLASH_Status status = FLASH_COMPLETE;
  99. /* Check the parameters */
  100. ASSERT(IS_FLASH_ADDRESS(Page_Address));
  101. /* Wait for last operation to be completed */
  102. status = FLASH_WaitForLastOperation(EraseTimeout);
  103. if (status == FLASH_COMPLETE) {
  104. /* if the previous operation is completed, proceed to erase the page */
  105. #if defined(FLASH_CR_SNB)
  106. FLASH->CR &= ~FLASH_CR_SNB;
  107. FLASH->CR |= FLASH_CR_SER | (ADDR2PAGE(Page_Address) << FLASH_CR_SNB_Pos);
  108. #else
  109. FLASH->CR |= FLASH_CR_PER;
  110. FLASH->AR = Page_Address;
  111. #endif
  112. FLASH->CR |= FLASH_CR_STRT;
  113. /* Wait for last operation to be completed */
  114. status = FLASH_WaitForLastOperation(EraseTimeout);
  115. if (status != FLASH_TIMEOUT) {
  116. /* if the erase operation is completed, disable the configured Bits */
  117. #if defined(FLASH_CR_SNB)
  118. FLASH->CR &= ~(FLASH_CR_SER | FLASH_CR_SNB);
  119. #else
  120. FLASH->CR &= ~FLASH_CR_PER;
  121. #endif
  122. }
  123. FLASH->SR = (FLASH_SR_EOP | FLASH_SR_PGERR | FLASH_SR_WRPERR);
  124. }
  125. /* Return the Erase Status */
  126. return status;
  127. }
  128. /**
  129. * @brief Programs a half word at a specified address.
  130. * @param Address: specifies the address to be programmed.
  131. * @param Data: specifies the data to be programmed.
  132. * @retval FLASH Status: The returned value can be: FLASH_ERROR_PG,
  133. * FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT.
  134. */
  135. FLASH_Status FLASH_ProgramHalfWord(uint32_t Address, uint16_t Data) {
  136. FLASH_Status status = FLASH_BAD_ADDRESS;
  137. if (IS_FLASH_ADDRESS(Address)) {
  138. /* Wait for last operation to be completed */
  139. status = FLASH_WaitForLastOperation(ProgramTimeout);
  140. if (status == FLASH_COMPLETE) {
  141. /* if the previous operation is completed, proceed to program the new data */
  142. #if defined(FLASH_CR_PSIZE)
  143. FLASH->CR &= ~FLASH_CR_PSIZE;
  144. FLASH->CR |= FLASH_CR_PSIZE_0;
  145. #endif
  146. FLASH->CR |= FLASH_CR_PG;
  147. *(__IO uint16_t*)Address = Data;
  148. /* Wait for last operation to be completed */
  149. status = FLASH_WaitForLastOperation(ProgramTimeout);
  150. if (status != FLASH_TIMEOUT) {
  151. /* if the program operation is completed, disable the PG Bit */
  152. FLASH->CR &= ~FLASH_CR_PG;
  153. }
  154. FLASH->SR = (FLASH_SR_EOP | FLASH_SR_PGERR | FLASH_SR_WRPERR);
  155. }
  156. }
  157. return status;
  158. }
  159. /**
  160. * @brief Unlocks the FLASH Program Erase Controller.
  161. * @param None
  162. * @retval None
  163. */
  164. void FLASH_Unlock(void) {
  165. if (FLASH->CR & FLASH_CR_LOCK) {
  166. /* Authorize the FPEC Access */
  167. FLASH->KEYR = FLASH_KEY1;
  168. FLASH->KEYR = FLASH_KEY2;
  169. }
  170. }
  171. /**
  172. * @brief Locks the FLASH Program Erase Controller.
  173. * @param None
  174. * @retval None
  175. */
  176. void FLASH_Lock(void) {
  177. /* Set the Lock Bit to lock the FPEC and the FCR */
  178. FLASH->CR |= FLASH_CR_LOCK;
  179. }