2018-08-29 20:14:49 +00:00
|
|
|
/*
|
|
|
|
* This software is experimental and a work in progress.
|
|
|
|
* Under no circumstances should these files be used in relation to any critical system(s).
|
|
|
|
* Use of these files is at your own risk.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
|
|
|
|
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
|
|
|
|
* PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
|
|
|
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
|
|
|
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
|
|
|
* DEALINGS IN THE SOFTWARE.
|
|
|
|
*
|
2018-10-19 04:33:23 +00:00
|
|
|
* This files are free to use from https://github.com/rogerclarkmelbourne/Arduino_STM32 and
|
2018-08-29 20:14:49 +00:00
|
|
|
* https://github.com/leaflabs/libmaple
|
|
|
|
*
|
|
|
|
* Modifications for QMK and STM32F303 by Yiancar
|
|
|
|
*/
|
|
|
|
|
2021-08-30 23:25:41 +00:00
|
|
|
#include <hal.h>
|
2022-11-23 08:50:19 +00:00
|
|
|
#include "legacy_flash_ops.h"
|
2018-08-29 20:14:49 +00:00
|
|
|
|
2021-10-16 01:58:48 +00:00
|
|
|
#if defined(STM32F1XX)
|
2019-08-30 18:19:03 +00:00
|
|
|
# define FLASH_SR_WRPERR FLASH_SR_WRPRTERR
|
2018-10-19 04:33:23 +00:00
|
|
|
#endif
|
2018-08-29 20:14:49 +00:00
|
|
|
|
2021-10-18 05:23:20 +00:00
|
|
|
#if defined(MCU_GD32V)
|
|
|
|
/* GigaDevice GD32VF103 is a STM32F103 clone at heart. */
|
|
|
|
# include "gd32v_compatibility.h"
|
|
|
|
#endif
|
|
|
|
|
2021-10-16 01:58:48 +00:00
|
|
|
#if defined(STM32F4XX)
|
2021-09-15 15:30:26 +00:00
|
|
|
# define FLASH_SR_PGERR (FLASH_SR_PGSERR | FLASH_SR_PGPERR | FLASH_SR_PGAERR)
|
|
|
|
|
|
|
|
# define FLASH_KEY1 0x45670123U
|
|
|
|
# define FLASH_KEY2 0xCDEF89ABU
|
|
|
|
|
|
|
|
static uint8_t ADDR2PAGE(uint32_t Page_Address) {
|
|
|
|
switch (Page_Address) {
|
|
|
|
case 0x08000000 ... 0x08003FFF:
|
|
|
|
return 0;
|
|
|
|
case 0x08004000 ... 0x08007FFF:
|
|
|
|
return 1;
|
|
|
|
case 0x08008000 ... 0x0800BFFF:
|
|
|
|
return 2;
|
|
|
|
case 0x0800C000 ... 0x0800FFFF:
|
|
|
|
return 3;
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: bad times...
|
|
|
|
return 7;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2018-08-29 20:14:49 +00:00
|
|
|
/* Delay definition */
|
2019-08-30 18:19:03 +00:00
|
|
|
#define EraseTimeout ((uint32_t)0x00000FFF)
|
|
|
|
#define ProgramTimeout ((uint32_t)0x0000001F)
|
2018-08-29 20:14:49 +00:00
|
|
|
|
|
|
|
#define ASSERT(exp) (void)((0))
|
|
|
|
|
|
|
|
/**
|
2019-08-30 18:19:03 +00:00
|
|
|
* @brief Inserts a time delay.
|
|
|
|
* @param None
|
|
|
|
* @retval None
|
|
|
|
*/
|
|
|
|
static void delay(void) {
|
2018-08-29 20:14:49 +00:00
|
|
|
__IO uint32_t i = 0;
|
2019-08-30 18:19:03 +00:00
|
|
|
for (i = 0xFF; i != 0; i--) {
|
|
|
|
}
|
2018-08-29 20:14:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-08-30 18:19:03 +00:00
|
|
|
* @brief Returns the FLASH Status.
|
|
|
|
* @param None
|
|
|
|
* @retval FLASH Status: The returned value can be: FLASH_BUSY, FLASH_ERROR_PG,
|
|
|
|
* FLASH_ERROR_WRP or FLASH_COMPLETE
|
|
|
|
*/
|
|
|
|
FLASH_Status FLASH_GetStatus(void) {
|
|
|
|
if ((FLASH->SR & FLASH_SR_BSY) == FLASH_SR_BSY) return FLASH_BUSY;
|
2018-08-29 20:14:49 +00:00
|
|
|
|
2019-08-30 18:19:03 +00:00
|
|
|
if ((FLASH->SR & FLASH_SR_PGERR) != 0) return FLASH_ERROR_PG;
|
2018-08-29 20:14:49 +00:00
|
|
|
|
2019-08-30 18:19:03 +00:00
|
|
|
if ((FLASH->SR & FLASH_SR_WRPERR) != 0) return FLASH_ERROR_WRP;
|
2018-08-29 20:14:49 +00:00
|
|
|
|
2021-09-15 15:30:26 +00:00
|
|
|
#if defined(FLASH_OBR_OPTERR)
|
2019-08-30 18:19:03 +00:00
|
|
|
if ((FLASH->SR & FLASH_OBR_OPTERR) != 0) return FLASH_ERROR_OPT;
|
2021-09-15 15:30:26 +00:00
|
|
|
#endif
|
2018-08-29 20:14:49 +00:00
|
|
|
|
|
|
|
return FLASH_COMPLETE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-08-30 18:19:03 +00:00
|
|
|
* @brief Waits for a Flash operation to complete or a TIMEOUT to occur.
|
|
|
|
* @param Timeout: FLASH progamming Timeout
|
|
|
|
* @retval FLASH Status: The returned value can be: FLASH_ERROR_PG,
|
|
|
|
* FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT.
|
|
|
|
*/
|
|
|
|
FLASH_Status FLASH_WaitForLastOperation(uint32_t Timeout) {
|
2018-08-29 20:14:49 +00:00
|
|
|
FLASH_Status status;
|
|
|
|
|
|
|
|
/* Check for the Flash Status */
|
|
|
|
status = FLASH_GetStatus();
|
|
|
|
/* Wait for a Flash operation to complete or a TIMEOUT to occur */
|
2019-08-30 18:19:03 +00:00
|
|
|
while ((status == FLASH_BUSY) && (Timeout != 0x00)) {
|
2018-08-29 20:14:49 +00:00
|
|
|
delay();
|
|
|
|
status = FLASH_GetStatus();
|
|
|
|
Timeout--;
|
|
|
|
}
|
2019-08-30 18:19:03 +00:00
|
|
|
if (Timeout == 0) status = FLASH_TIMEOUT;
|
2018-08-29 20:14:49 +00:00
|
|
|
/* Return the operation status */
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-08-30 18:19:03 +00:00
|
|
|
* @brief Erases a specified FLASH page.
|
|
|
|
* @param Page_Address: The page address to be erased.
|
|
|
|
* @retval FLASH Status: The returned value can be: FLASH_BUSY, FLASH_ERROR_PG,
|
|
|
|
* FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT.
|
|
|
|
*/
|
|
|
|
FLASH_Status FLASH_ErasePage(uint32_t Page_Address) {
|
2018-08-29 20:14:49 +00:00
|
|
|
FLASH_Status status = FLASH_COMPLETE;
|
|
|
|
/* Check the parameters */
|
|
|
|
ASSERT(IS_FLASH_ADDRESS(Page_Address));
|
|
|
|
/* Wait for last operation to be completed */
|
|
|
|
status = FLASH_WaitForLastOperation(EraseTimeout);
|
2018-10-19 04:33:23 +00:00
|
|
|
|
2019-08-30 18:19:03 +00:00
|
|
|
if (status == FLASH_COMPLETE) {
|
2018-08-29 20:14:49 +00:00
|
|
|
/* if the previous operation is completed, proceed to erase the page */
|
2021-09-15 15:30:26 +00:00
|
|
|
#if defined(FLASH_CR_SNB)
|
|
|
|
FLASH->CR &= ~FLASH_CR_SNB;
|
|
|
|
FLASH->CR |= FLASH_CR_SER | (ADDR2PAGE(Page_Address) << FLASH_CR_SNB_Pos);
|
|
|
|
#else
|
2018-08-29 20:14:49 +00:00
|
|
|
FLASH->CR |= FLASH_CR_PER;
|
|
|
|
FLASH->AR = Page_Address;
|
2021-09-15 15:30:26 +00:00
|
|
|
#endif
|
2018-08-29 20:14:49 +00:00
|
|
|
FLASH->CR |= FLASH_CR_STRT;
|
|
|
|
|
|
|
|
/* Wait for last operation to be completed */
|
|
|
|
status = FLASH_WaitForLastOperation(EraseTimeout);
|
2019-08-30 18:19:03 +00:00
|
|
|
if (status != FLASH_TIMEOUT) {
|
2021-09-15 15:30:26 +00:00
|
|
|
/* if the erase operation is completed, disable the configured Bits */
|
|
|
|
#if defined(FLASH_CR_SNB)
|
|
|
|
FLASH->CR &= ~(FLASH_CR_SER | FLASH_CR_SNB);
|
|
|
|
#else
|
2018-08-29 20:14:49 +00:00
|
|
|
FLASH->CR &= ~FLASH_CR_PER;
|
2021-09-15 15:30:26 +00:00
|
|
|
#endif
|
2018-08-29 20:14:49 +00:00
|
|
|
}
|
|
|
|
FLASH->SR = (FLASH_SR_EOP | FLASH_SR_PGERR | FLASH_SR_WRPERR);
|
|
|
|
}
|
|
|
|
/* Return the Erase Status */
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-08-30 18:19:03 +00:00
|
|
|
* @brief Programs a half word at a specified address.
|
|
|
|
* @param Address: specifies the address to be programmed.
|
|
|
|
* @param Data: specifies the data to be programmed.
|
|
|
|
* @retval FLASH Status: The returned value can be: FLASH_ERROR_PG,
|
|
|
|
* FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT.
|
|
|
|
*/
|
|
|
|
FLASH_Status FLASH_ProgramHalfWord(uint32_t Address, uint16_t Data) {
|
2018-08-29 20:14:49 +00:00
|
|
|
FLASH_Status status = FLASH_BAD_ADDRESS;
|
|
|
|
|
2019-08-30 18:19:03 +00:00
|
|
|
if (IS_FLASH_ADDRESS(Address)) {
|
2018-08-29 20:14:49 +00:00
|
|
|
/* Wait for last operation to be completed */
|
|
|
|
status = FLASH_WaitForLastOperation(ProgramTimeout);
|
2019-08-30 18:19:03 +00:00
|
|
|
if (status == FLASH_COMPLETE) {
|
2018-08-29 20:14:49 +00:00
|
|
|
/* if the previous operation is completed, proceed to program the new data */
|
2021-09-15 15:30:26 +00:00
|
|
|
|
|
|
|
#if defined(FLASH_CR_PSIZE)
|
|
|
|
FLASH->CR &= ~FLASH_CR_PSIZE;
|
|
|
|
FLASH->CR |= FLASH_CR_PSIZE_0;
|
|
|
|
#endif
|
2018-08-29 20:14:49 +00:00
|
|
|
FLASH->CR |= FLASH_CR_PG;
|
|
|
|
*(__IO uint16_t*)Address = Data;
|
|
|
|
/* Wait for last operation to be completed */
|
|
|
|
status = FLASH_WaitForLastOperation(ProgramTimeout);
|
2019-08-30 18:19:03 +00:00
|
|
|
if (status != FLASH_TIMEOUT) {
|
2018-08-29 20:14:49 +00:00
|
|
|
/* if the program operation is completed, disable the PG Bit */
|
|
|
|
FLASH->CR &= ~FLASH_CR_PG;
|
|
|
|
}
|
|
|
|
FLASH->SR = (FLASH_SR_EOP | FLASH_SR_PGERR | FLASH_SR_WRPERR);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-08-30 18:19:03 +00:00
|
|
|
* @brief Unlocks the FLASH Program Erase Controller.
|
|
|
|
* @param None
|
|
|
|
* @retval None
|
|
|
|
*/
|
|
|
|
void FLASH_Unlock(void) {
|
2021-08-02 04:27:57 +00:00
|
|
|
if (FLASH->CR & FLASH_CR_LOCK) {
|
|
|
|
/* Authorize the FPEC Access */
|
|
|
|
FLASH->KEYR = FLASH_KEY1;
|
|
|
|
FLASH->KEYR = FLASH_KEY2;
|
|
|
|
}
|
2018-08-29 20:14:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-08-30 18:19:03 +00:00
|
|
|
* @brief Locks the FLASH Program Erase Controller.
|
|
|
|
* @param None
|
|
|
|
* @retval None
|
|
|
|
*/
|
|
|
|
void FLASH_Lock(void) {
|
2018-08-29 20:14:49 +00:00
|
|
|
/* Set the Lock Bit to lock the FPEC and the FCR */
|
|
|
|
FLASH->CR |= FLASH_CR_LOCK;
|
|
|
|
}
|