You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

208 lines
6.3 KiB

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