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