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.
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.
13 * This files are free to use from https://github.com/rogerclarkmelbourne/Arduino_STM32 and
14 * https://github.com/leaflabs/libmaple
16 * Modifications for QMK and STM32F303 by Yiancar
19 #if defined(EEPROM_EMU_STM32F303xC)
21 #include "stm32f3xx.h"
22 #elif defined(EEPROM_EMU_STM32F103xB)
24 #include "stm32f1xx.h"
26 #error "not implemented."
29 #include "flash_stm32.h"
31 #if defined(EEPROM_EMU_STM32F103xB)
32 #define FLASH_SR_WRPERR FLASH_SR_WRPRTERR
35 /* Delay definition */
36 #define EraseTimeout ((uint32_t)0x00000FFF)
37 #define ProgramTimeout ((uint32_t)0x0000001F)
39 #define ASSERT(exp) (void)((0))
42 * @brief Inserts a time delay.
46 static void delay(void)
49 for(i = 0xFF; i != 0; i--) { }
53 * @brief Returns the FLASH Status.
55 * @retval FLASH Status: The returned value can be: FLASH_BUSY, FLASH_ERROR_PG,
56 * FLASH_ERROR_WRP or FLASH_COMPLETE
58 FLASH_Status FLASH_GetStatus(void)
60 if ((FLASH->SR & FLASH_SR_BSY) == FLASH_SR_BSY)
63 if ((FLASH->SR & FLASH_SR_PGERR) != 0)
64 return FLASH_ERROR_PG;
66 if ((FLASH->SR & FLASH_SR_WRPERR) != 0 )
67 return FLASH_ERROR_WRP;
69 if ((FLASH->SR & FLASH_OBR_OPTERR) != 0 )
70 return FLASH_ERROR_OPT;
72 return FLASH_COMPLETE;
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.
81 FLASH_Status FLASH_WaitForLastOperation(uint32_t Timeout)
85 /* Check for the Flash Status */
86 status = FLASH_GetStatus();
87 /* Wait for a Flash operation to complete or a TIMEOUT to occur */
88 while ((status == FLASH_BUSY) && (Timeout != 0x00))
91 status = FLASH_GetStatus();
95 status = FLASH_TIMEOUT;
96 /* Return the operation status */
101 * @brief Erases a specified FLASH page.
102 * @param Page_Address: The page address to be erased.
103 * @retval FLASH Status: The returned value can be: FLASH_BUSY, FLASH_ERROR_PG,
104 * FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT.
106 FLASH_Status FLASH_ErasePage(uint32_t Page_Address)
108 FLASH_Status status = FLASH_COMPLETE;
109 /* Check the parameters */
110 ASSERT(IS_FLASH_ADDRESS(Page_Address));
111 /* Wait for last operation to be completed */
112 status = FLASH_WaitForLastOperation(EraseTimeout);
114 if(status == FLASH_COMPLETE)
116 /* if the previous operation is completed, proceed to erase the page */
117 FLASH->CR |= FLASH_CR_PER;
118 FLASH->AR = Page_Address;
119 FLASH->CR |= FLASH_CR_STRT;
121 /* Wait for last operation to be completed */
122 status = FLASH_WaitForLastOperation(EraseTimeout);
123 if(status != FLASH_TIMEOUT)
125 /* if the erase operation is completed, disable the PER Bit */
126 FLASH->CR &= ~FLASH_CR_PER;
128 FLASH->SR = (FLASH_SR_EOP | FLASH_SR_PGERR | FLASH_SR_WRPERR);
130 /* Return the Erase Status */
135 * @brief Programs a half word at a specified address.
136 * @param Address: specifies the address to be programmed.
137 * @param Data: specifies the data to be programmed.
138 * @retval FLASH Status: The returned value can be: FLASH_ERROR_PG,
139 * FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT.
141 FLASH_Status FLASH_ProgramHalfWord(uint32_t Address, uint16_t Data)
143 FLASH_Status status = FLASH_BAD_ADDRESS;
145 if (IS_FLASH_ADDRESS(Address))
147 /* Wait for last operation to be completed */
148 status = FLASH_WaitForLastOperation(ProgramTimeout);
149 if(status == FLASH_COMPLETE)
151 /* if the previous operation is completed, proceed to program the new data */
152 FLASH->CR |= FLASH_CR_PG;
153 *(__IO uint16_t*)Address = Data;
154 /* Wait for last operation to be completed */
155 status = FLASH_WaitForLastOperation(ProgramTimeout);
156 if(status != FLASH_TIMEOUT)
158 /* if the program operation is completed, disable the PG Bit */
159 FLASH->CR &= ~FLASH_CR_PG;
161 FLASH->SR = (FLASH_SR_EOP | FLASH_SR_PGERR | FLASH_SR_WRPERR);
168 * @brief Unlocks the FLASH Program Erase Controller.
172 void FLASH_Unlock(void)
174 /* Authorize the FPEC Access */
175 FLASH->KEYR = FLASH_KEY1;
176 FLASH->KEYR = FLASH_KEY2;
180 * @brief Locks the FLASH Program Erase Controller.
184 void FLASH_Lock(void)
186 /* Set the Lock Bit to lock the FPEC and the FCR */
187 FLASH->CR |= FLASH_CR_LOCK;