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"
25 #elif defined(EEPROM_EMU_STM32F072xB)
27 #include "stm32f0xx.h"
29 #error "not implemented."
32 #include "flash_stm32.h"
34 #if defined(EEPROM_EMU_STM32F103xB)
35 #define FLASH_SR_WRPERR FLASH_SR_WRPRTERR
38 /* Delay definition */
39 #define EraseTimeout ((uint32_t)0x00000FFF)
40 #define ProgramTimeout ((uint32_t)0x0000001F)
42 #define ASSERT(exp) (void)((0))
45 * @brief Inserts a time delay.
49 static void delay(void)
52 for(i = 0xFF; i != 0; i--) { }
56 * @brief Returns the FLASH Status.
58 * @retval FLASH Status: The returned value can be: FLASH_BUSY, FLASH_ERROR_PG,
59 * FLASH_ERROR_WRP or FLASH_COMPLETE
61 FLASH_Status FLASH_GetStatus(void)
63 if ((FLASH->SR & FLASH_SR_BSY) == FLASH_SR_BSY)
66 if ((FLASH->SR & FLASH_SR_PGERR) != 0)
67 return FLASH_ERROR_PG;
69 if ((FLASH->SR & FLASH_SR_WRPERR) != 0 )
70 return FLASH_ERROR_WRP;
72 if ((FLASH->SR & FLASH_OBR_OPTERR) != 0 )
73 return FLASH_ERROR_OPT;
75 return FLASH_COMPLETE;
79 * @brief Waits for a Flash operation to complete or a TIMEOUT to occur.
80 * @param Timeout: FLASH progamming Timeout
81 * @retval FLASH Status: The returned value can be: FLASH_ERROR_PG,
82 * FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT.
84 FLASH_Status FLASH_WaitForLastOperation(uint32_t Timeout)
88 /* Check for the Flash Status */
89 status = FLASH_GetStatus();
90 /* Wait for a Flash operation to complete or a TIMEOUT to occur */
91 while ((status == FLASH_BUSY) && (Timeout != 0x00))
94 status = FLASH_GetStatus();
98 status = FLASH_TIMEOUT;
99 /* Return the operation status */
104 * @brief Erases a specified FLASH page.
105 * @param Page_Address: The page address to be erased.
106 * @retval FLASH Status: The returned value can be: FLASH_BUSY, FLASH_ERROR_PG,
107 * FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT.
109 FLASH_Status FLASH_ErasePage(uint32_t Page_Address)
111 FLASH_Status status = FLASH_COMPLETE;
112 /* Check the parameters */
113 ASSERT(IS_FLASH_ADDRESS(Page_Address));
114 /* Wait for last operation to be completed */
115 status = FLASH_WaitForLastOperation(EraseTimeout);
117 if(status == FLASH_COMPLETE)
119 /* if the previous operation is completed, proceed to erase the page */
120 FLASH->CR |= FLASH_CR_PER;
121 FLASH->AR = Page_Address;
122 FLASH->CR |= FLASH_CR_STRT;
124 /* Wait for last operation to be completed */
125 status = FLASH_WaitForLastOperation(EraseTimeout);
126 if(status != FLASH_TIMEOUT)
128 /* if the erase operation is completed, disable the PER Bit */
129 FLASH->CR &= ~FLASH_CR_PER;
131 FLASH->SR = (FLASH_SR_EOP | FLASH_SR_PGERR | FLASH_SR_WRPERR);
133 /* Return the Erase Status */
138 * @brief Programs a half word at a specified address.
139 * @param Address: specifies the address to be programmed.
140 * @param Data: specifies the data to be programmed.
141 * @retval FLASH Status: The returned value can be: FLASH_ERROR_PG,
142 * FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT.
144 FLASH_Status FLASH_ProgramHalfWord(uint32_t Address, uint16_t Data)
146 FLASH_Status status = FLASH_BAD_ADDRESS;
148 if (IS_FLASH_ADDRESS(Address))
150 /* Wait for last operation to be completed */
151 status = FLASH_WaitForLastOperation(ProgramTimeout);
152 if(status == FLASH_COMPLETE)
154 /* if the previous operation is completed, proceed to program the new data */
155 FLASH->CR |= FLASH_CR_PG;
156 *(__IO uint16_t*)Address = Data;
157 /* Wait for last operation to be completed */
158 status = FLASH_WaitForLastOperation(ProgramTimeout);
159 if(status != FLASH_TIMEOUT)
161 /* if the program operation is completed, disable the PG Bit */
162 FLASH->CR &= ~FLASH_CR_PG;
164 FLASH->SR = (FLASH_SR_EOP | FLASH_SR_PGERR | FLASH_SR_WRPERR);
171 * @brief Unlocks the FLASH Program Erase Controller.
175 void FLASH_Unlock(void)
177 /* Authorize the FPEC Access */
178 FLASH->KEYR = FLASH_KEY1;
179 FLASH->KEYR = FLASH_KEY2;
183 * @brief Locks the FLASH Program Erase Controller.
187 void FLASH_Lock(void)
189 /* Set the Lock Bit to lock the FPEC and the FCR */
190 FLASH->CR |= FLASH_CR_LOCK;
194 * @brief Clears the FLASH's pending flags.
195 * @param FLASH_FLAG: specifies the FLASH flags to clear.
196 * This parameter can be any combination of the following values:
197 * @arg FLASH_FLAG_PGERR: FLASH Programming error flag flag
198 * @arg FLASH_FLAG_WRPERR: FLASH Write protected error flag
199 * @arg FLASH_FLAG_EOP: FLASH End of Programming flag
202 void FLASH_ClearFlag(uint32_t FLASH_FLAG)
204 /* Clear the flags */
205 FLASH->SR = FLASH_FLAG;