flash_stm32.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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. /* 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. __IO uint32_t i = 0;
  34. for (i = 0xFF; i != 0; i--) {
  35. }
  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. if ((FLASH->SR & FLASH_SR_BSY) == FLASH_SR_BSY) return FLASH_BUSY;
  45. if ((FLASH->SR & FLASH_SR_PGERR) != 0) return FLASH_ERROR_PG;
  46. if ((FLASH->SR & FLASH_SR_WRPERR) != 0) return FLASH_ERROR_WRP;
  47. if ((FLASH->SR & FLASH_OBR_OPTERR) != 0) return FLASH_ERROR_OPT;
  48. return FLASH_COMPLETE;
  49. }
  50. /**
  51. * @brief Waits for a Flash operation to complete or a TIMEOUT to occur.
  52. * @param Timeout: FLASH progamming Timeout
  53. * @retval FLASH Status: The returned value can be: FLASH_ERROR_PG,
  54. * FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT.
  55. */
  56. FLASH_Status FLASH_WaitForLastOperation(uint32_t Timeout) {
  57. FLASH_Status status;
  58. /* Check for the Flash Status */
  59. status = FLASH_GetStatus();
  60. /* Wait for a Flash operation to complete or a TIMEOUT to occur */
  61. while ((status == FLASH_BUSY) && (Timeout != 0x00)) {
  62. delay();
  63. status = FLASH_GetStatus();
  64. Timeout--;
  65. }
  66. if (Timeout == 0) status = FLASH_TIMEOUT;
  67. /* Return the operation status */
  68. return status;
  69. }
  70. /**
  71. * @brief Erases a specified FLASH page.
  72. * @param Page_Address: The page address to be erased.
  73. * @retval FLASH Status: The returned value can be: FLASH_BUSY, FLASH_ERROR_PG,
  74. * FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT.
  75. */
  76. FLASH_Status FLASH_ErasePage(uint32_t Page_Address) {
  77. FLASH_Status status = FLASH_COMPLETE;
  78. /* Check the parameters */
  79. ASSERT(IS_FLASH_ADDRESS(Page_Address));
  80. /* Wait for last operation to be completed */
  81. status = FLASH_WaitForLastOperation(EraseTimeout);
  82. if (status == FLASH_COMPLETE) {
  83. /* if the previous operation is completed, proceed to erase the page */
  84. FLASH->CR |= FLASH_CR_PER;
  85. FLASH->AR = Page_Address;
  86. FLASH->CR |= FLASH_CR_STRT;
  87. /* Wait for last operation to be completed */
  88. status = FLASH_WaitForLastOperation(EraseTimeout);
  89. if (status != FLASH_TIMEOUT) {
  90. /* if the erase operation is completed, disable the PER Bit */
  91. FLASH->CR &= ~FLASH_CR_PER;
  92. }
  93. FLASH->SR = (FLASH_SR_EOP | FLASH_SR_PGERR | FLASH_SR_WRPERR);
  94. }
  95. /* Return the Erase Status */
  96. return status;
  97. }
  98. /**
  99. * @brief Programs a half word at a specified address.
  100. * @param Address: specifies the address to be programmed.
  101. * @param Data: specifies the data to be programmed.
  102. * @retval FLASH Status: The returned value can be: FLASH_ERROR_PG,
  103. * FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT.
  104. */
  105. FLASH_Status FLASH_ProgramHalfWord(uint32_t Address, uint16_t Data) {
  106. FLASH_Status status = FLASH_BAD_ADDRESS;
  107. if (IS_FLASH_ADDRESS(Address)) {
  108. /* Wait for last operation to be completed */
  109. status = FLASH_WaitForLastOperation(ProgramTimeout);
  110. if (status == FLASH_COMPLETE) {
  111. /* if the previous operation is completed, proceed to program the new data */
  112. FLASH->CR |= FLASH_CR_PG;
  113. *(__IO uint16_t*)Address = Data;
  114. /* Wait for last operation to be completed */
  115. status = FLASH_WaitForLastOperation(ProgramTimeout);
  116. if (status != FLASH_TIMEOUT) {
  117. /* if the program operation is completed, disable the PG Bit */
  118. FLASH->CR &= ~FLASH_CR_PG;
  119. }
  120. FLASH->SR = (FLASH_SR_EOP | FLASH_SR_PGERR | FLASH_SR_WRPERR);
  121. }
  122. }
  123. return status;
  124. }
  125. /**
  126. * @brief Unlocks the FLASH Program Erase Controller.
  127. * @param None
  128. * @retval None
  129. */
  130. void FLASH_Unlock(void) {
  131. if (FLASH->CR & FLASH_CR_LOCK) {
  132. /* Authorize the FPEC Access */
  133. FLASH->KEYR = FLASH_KEY1;
  134. FLASH->KEYR = FLASH_KEY2;
  135. }
  136. }
  137. /**
  138. * @brief Locks the FLASH Program Erase Controller.
  139. * @param None
  140. * @retval None
  141. */
  142. void FLASH_Lock(void) {
  143. /* Set the Lock Bit to lock the FPEC and the FCR */
  144. FLASH->CR |= FLASH_CR_LOCK;
  145. }