flash_stm32.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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. #define STM32F303xC
  19. #include "stm32f3xx.h"
  20. #include "flash_stm32.h"
  21. #define FLASH_KEY1 ((uint32_t)0x45670123)
  22. #define FLASH_KEY2 ((uint32_t)0xCDEF89AB)
  23. /* Delay definition */
  24. #define EraseTimeout ((uint32_t)0x00000FFF)
  25. #define ProgramTimeout ((uint32_t)0x0000001F)
  26. #define ASSERT(exp) (void)((0))
  27. /**
  28. * @brief Inserts a time delay.
  29. * @param None
  30. * @retval None
  31. */
  32. static void delay(void)
  33. {
  34. __IO uint32_t i = 0;
  35. for(i = 0xFF; i != 0; i--) { }
  36. }
  37. /**
  38. * @brief Returns the FLASH Status.
  39. * @param None
  40. * @retval FLASH Status: The returned value can be: FLASH_BUSY, FLASH_ERROR_PG,
  41. * FLASH_ERROR_WRP or FLASH_COMPLETE
  42. */
  43. FLASH_Status FLASH_GetStatus(void)
  44. {
  45. if ((FLASH->SR & FLASH_SR_BSY) == FLASH_SR_BSY)
  46. return FLASH_BUSY;
  47. if ((FLASH->SR & FLASH_SR_PGERR) != 0)
  48. return FLASH_ERROR_PG;
  49. if ((FLASH->SR & FLASH_SR_WRPERR) != 0 )
  50. return FLASH_ERROR_WRP;
  51. if ((FLASH->SR & FLASH_OBR_OPTERR) != 0 )
  52. return FLASH_ERROR_OPT;
  53. return FLASH_COMPLETE;
  54. }
  55. /**
  56. * @brief Waits for a Flash operation to complete or a TIMEOUT to occur.
  57. * @param Timeout: FLASH progamming Timeout
  58. * @retval FLASH Status: The returned value can be: FLASH_ERROR_PG,
  59. * FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT.
  60. */
  61. FLASH_Status FLASH_WaitForLastOperation(uint32_t Timeout)
  62. {
  63. FLASH_Status status;
  64. /* Check for the Flash Status */
  65. status = FLASH_GetStatus();
  66. /* Wait for a Flash operation to complete or a TIMEOUT to occur */
  67. while ((status == FLASH_BUSY) && (Timeout != 0x00))
  68. {
  69. delay();
  70. status = FLASH_GetStatus();
  71. Timeout--;
  72. }
  73. if (Timeout == 0)
  74. status = FLASH_TIMEOUT;
  75. /* Return the operation status */
  76. return status;
  77. }
  78. /**
  79. * @brief Erases a specified FLASH page.
  80. * @param Page_Address: The page address to be erased.
  81. * @retval FLASH Status: The returned value can be: FLASH_BUSY, FLASH_ERROR_PG,
  82. * FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT.
  83. */
  84. FLASH_Status FLASH_ErasePage(uint32_t Page_Address)
  85. {
  86. FLASH_Status status = FLASH_COMPLETE;
  87. /* Check the parameters */
  88. ASSERT(IS_FLASH_ADDRESS(Page_Address));
  89. /* Wait for last operation to be completed */
  90. status = FLASH_WaitForLastOperation(EraseTimeout);
  91. if(status == FLASH_COMPLETE)
  92. {
  93. /* if the previous operation is completed, proceed to erase the page */
  94. FLASH->CR |= FLASH_CR_PER;
  95. FLASH->AR = Page_Address;
  96. FLASH->CR |= FLASH_CR_STRT;
  97. /* Wait for last operation to be completed */
  98. status = FLASH_WaitForLastOperation(EraseTimeout);
  99. if(status != FLASH_TIMEOUT)
  100. {
  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. {
  118. FLASH_Status status = FLASH_BAD_ADDRESS;
  119. if (IS_FLASH_ADDRESS(Address))
  120. {
  121. /* Wait for last operation to be completed */
  122. status = FLASH_WaitForLastOperation(ProgramTimeout);
  123. if(status == FLASH_COMPLETE)
  124. {
  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. {
  132. /* if the program operation is completed, disable the PG Bit */
  133. FLASH->CR &= ~FLASH_CR_PG;
  134. }
  135. FLASH->SR = (FLASH_SR_EOP | FLASH_SR_PGERR | FLASH_SR_WRPERR);
  136. }
  137. }
  138. return status;
  139. }
  140. /**
  141. * @brief Unlocks the FLASH Program Erase Controller.
  142. * @param None
  143. * @retval None
  144. */
  145. void FLASH_Unlock(void)
  146. {
  147. /* Authorize the FPEC Access */
  148. FLASH->KEYR = FLASH_KEY1;
  149. FLASH->KEYR = FLASH_KEY2;
  150. }
  151. /**
  152. * @brief Locks the FLASH Program Erase Controller.
  153. * @param None
  154. * @retval None
  155. */
  156. void FLASH_Lock(void)
  157. {
  158. /* Set the Lock Bit to lock the FPEC and the FCR */
  159. FLASH->CR |= FLASH_CR_LOCK;
  160. }