📄 stm32f0xx_flash.c
字号:
* @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)
{
FLASH_Status status = FLASH_COMPLETE;
/* Check the parameters */
assert_param(IS_FLASH_PROGRAM_ADDRESS(Address));
/* Wait for last operation to be completed */
status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
if(status == FLASH_COMPLETE)
{
/* If the previous operation is completed, proceed to program the new data */
FLASH->CR |= FLASH_CR_PG;
*(__IO uint16_t*)Address = Data;
/* Wait for last operation to be completed */
status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
/* Disable the PG Bit */
FLASH->CR &= ~FLASH_CR_PG;
}
/* Return the Program Status */
return status;
}
/**
* @}
*/
/** @defgroup FLASH_Group3 Option Bytes Programming functions
* @brief Option Bytes Programming functions
*
@verbatim
===============================================================================
##### Option Bytes Programming functions #####
===============================================================================
[..] The FLASH_Option Bytes Programming_functions, includes the following functions:
(+) void FLASH_OB_Unlock(void);
(+) void FLASH_OB_Lock(void);
(+) void FLASH_OB_Launch(void);
(+) FLASH_Status FLASH_OB_Erase(void);
(+) FLASH_Status FLASH_OB_WRPConfig(uint32_t OB_WRP, FunctionalState NewState);
(+) FLASH_Status FLASH_OB_RDPConfig(uint8_t OB_RDP);
(+) FLASH_Status FLASH_OB_UserConfig(uint8_t OB_IWDG, uint8_t OB_STOP, uint8_t OB_STDBY);
(+) FLASH_Status FLASH_OB_BOOTConfig(uint8_t OB_BOOT1);
(+) FLASH_Status FLASH_OB_VDDAConfig(uint8_t OB_VDDA_ANALOG);
(+) FLASH_Status FLASH_OB_WriteUser(uint8_t OB_USER);
(+) FLASH_ProgramOptionByteData(uint32_t Address, uint8_t Data);
(+) uint8_t FLASH_OB_GetUser(void);
(+) uint32_t FLASH_OB_GetWRP(void);
(+) FlagStatus FLASH_OB_GetRDP(void);
[..] Any operation of erase or program should follow these steps:
(#) Call the FLASH_OB_Unlock() function to enable the Option Bytes registers access
(#) Call one or several functions to program the desired option bytes
(++) FLASH_Status FLASH_OB_RDPConfig(uint8_t OB_RDP) => to set the desired read Protection Level
(++) FLASH_Status FLASH_OB_WRPConfig(uint32_t OB_WRP, FunctionalState NewState)
=> to Enable/Disable the desired sector write protection
(++) FLASH_Status FLASH_OB_UserConfig(uint8_t OB_IWDG, uint8_t OB_STOP, uint8_t OB_STDBY)
=> to configure the user option Bytes: IWDG, STOP and the Standby.
(++) FLASH_Status FLASH_OB_BOOTConfig(uint8_t OB_BOOT1)
=> to set or reset BOOT1
(++) FLASH_Status FLASH_OB_VDDAConfig(uint8_t OB_VDDA_ANALOG)
=> to enable or disable the VDDA Analog Monitoring
(++) You can write all User Options bytes at once using a single function
by calling FLASH_Status FLASH_OB_WriteUser(uint8_t OB_USER)
(++) FLASH_ProgramOptionByteData(uint32_t Address, uint8_t Data) to program the
two half word in the option bytes
(#) Once all needed option bytes to be programmed are correctly written, call the
FLASH_OB_Launch(void) function to launch the Option Bytes programming process.
(#) Call the FLASH_OB_Lock() to disable the Option Bytes registers access (recommended
to protect the option Bytes against possible unwanted operations)
@endverbatim
* @{
*/
/**
* @brief Unlocks the option bytes block access.
* @param None
* @retval None
*/
void FLASH_OB_Unlock(void)
{
if((FLASH->CR & FLASH_CR_OPTWRE) == RESET)
{
/* Unlocking the option bytes block access */
FLASH->OPTKEYR = FLASH_OPTKEY1;
FLASH->OPTKEYR = FLASH_OPTKEY2;
}
}
/**
* @brief Locks the option bytes block access.
* @param None
* @retval None
*/
void FLASH_OB_Lock(void)
{
/* Set the OPTWREN Bit to lock the option bytes block access */
FLASH->CR &= ~FLASH_CR_OPTWRE;
}
/**
* @brief Launch the option byte loading.
* @param None
* @retval None
*/
void FLASH_OB_Launch(void)
{
/* Set the OBL_Launch bit to launch the option byte loading */
FLASH->CR |= FLASH_CR_OBL_LAUNCH;
}
/**
* @brief Erases the FLASH option bytes.
* @note To correctly run this function, the FLASH_OB_Unlock() function
* must be called before.
* Call the FLASH_OB_Lock() to disable the flash control register access and the option bytes
* (recommended to protect the FLASH memory against possible unwanted operation)
* @note This functions erases all option bytes except the Read protection (RDP).
* @param None
* @retval FLASH Status: The returned value can be: FLASH_ERROR_PG,
* FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT.
*/
FLASH_Status FLASH_OB_Erase(void)
{
uint16_t rdptmp = OB_RDP_Level_0;
FLASH_Status status = FLASH_COMPLETE;
/* Get the actual read protection Option Byte value */
if(FLASH_OB_GetRDP() != RESET)
{
rdptmp = 0x00;
}
/* Wait for last operation to be completed */
status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
if(status == FLASH_COMPLETE)
{
/* If the previous operation is completed, proceed to erase the option bytes */
FLASH->CR |= FLASH_CR_OPTER;
FLASH->CR |= FLASH_CR_STRT;
/* Wait for last operation to be completed */
status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
if(status == FLASH_COMPLETE)
{
/* If the erase operation is completed, disable the OPTER Bit */
FLASH->CR &= ~FLASH_CR_OPTER;
/* Enable the Option Bytes Programming operation */
FLASH->CR |= FLASH_CR_OPTPG;
/* Restore the last read protection Option Byte value */
OB->RDP = (uint16_t)rdptmp;
/* Wait for last operation to be completed */
status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
if(status != FLASH_TIMEOUT)
{
/* if the program operation is completed, disable the OPTPG Bit */
FLASH->CR &= ~FLASH_CR_OPTPG;
}
}
else
{
if (status != FLASH_TIMEOUT)
{
/* Disable the OPTPG Bit */
FLASH->CR &= ~FLASH_CR_OPTPG;
}
}
}
/* Return the erase status */
return status;
}
/**
* @brief Write protects the desired pages
* @note To correctly run this function, the FLASH_OB_Unlock() function
* must be called before.
* Call the FLASH_OB_Lock() to disable the flash control register access and the option bytes
* (recommended to protect the FLASH memory against possible unwanted operation)
* @param OB_WRP: specifies the address of the pages to be write protected.
* This parameter can be:
* @arg OB_WRP_Pages0to3..OB_WRP_Pages60to63
* @arg OB_WRP_AllPages
* @retval FLASH Status: The returned value can be:
* FLASH_ERROR_PROGRAM, FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT.
*/
FLASH_Status FLASH_OB_EnableWRP(uint32_t OB_WRP)
{
uint16_t WRP0_Data = 0xFFFF, WRP1_Data = 0xFFFF;
FLASH_Status status = FLASH_COMPLETE;
/* Check the parameters */
assert_param(IS_OB_WRP(OB_WRP));
OB_WRP = (uint32_t)(~OB_WRP);
WRP0_Data = (uint16_t)(OB_WRP & OB_WRP0_WRP0);
WRP1_Data = (uint16_t)((OB_WRP & OB_WRP0_nWRP0) >> 8);
/* Wait for last operation to be completed */
status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
if(status == FLASH_COMPLETE)
{
FLASH->CR |= FLASH_CR_OPTPG;
if(WRP0_Data != 0xFF)
{
OB->WRP0 = WRP0_Data;
/* Wait for last operation to be completed */
status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
}
if((status == FLASH_COMPLETE) && (WRP1_Data != 0xFF))
{
OB->WRP1 = WRP1_Data;
/* Wait for last operation to be completed */
status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
}
if(status != FLASH_TIMEOUT)
{
/* if the program operation is completed, disable the OPTPG Bit */
FLASH->CR &= ~FLASH_CR_OPTPG;
}
}
/* Return the write protection operation Status */
return status;
}
/**
* @brief Enables or disables the read out protection.
* @note To correctly run this function, the FLASH_OB_Unlock() function
* must be called before.
* Call the FLASH_OB_Lock() to disable the flash control register access and the option bytes
* (recommended to protect the FLASH memory against possible unwanted operation)
* @param FLASH_ReadProtection_Level: specifies the read protection level.
* This parameter can be:
* @arg OB_RDP_Level_0: No protection
* @arg OB_RDP_Level_1: Read protection of the memory
* @arg OB_RDP_Level_2: Chip protection
* @retval FLASH Status: The returned value can be:
* FLASH_ERROR_PROGRAM, FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT.
*/
FLASH_Status FLASH_OB_RDPConfig(uint8_t OB_RDP)
{
FLASH_Status status = FLASH_COMPLETE;
/* Check the parameters */
assert_param(IS_OB_RDP(OB_RDP));
status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
if(status == FLASH_COMPLETE)
{
FLASH->CR |= FLASH_CR_OPTER;
FLASH->CR |= FLASH_CR_STRT;
/* Wait for last operation to be completed */
status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
if(status == FLASH_COMPLETE)
{
/* If the erase operation is completed, disable the OPTER Bit */
FLASH->CR &= ~FLASH_CR_OPTER;
/* Enable the Option Bytes Programming operation */
FLASH->CR |= FLASH_CR_OPTPG;
OB->RDP = OB_RDP;
/* Wait for last operation to be completed */
status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
if(status != FLASH_TIMEOUT)
{
/* if the program operation is completed, disable the OPTPG Bit */
FLASH->CR &= ~FLASH_CR_OPTPG;
}
}
else
{
if(status != FLASH_TIMEOUT)
{
/* Disable the OPTER Bit */
FLASH->CR &= ~FLASH_CR_OPTER;
}
}
}
/* Return the protection operation Status */
return status;
}
/**
* @brief Programs the FLASH User Option Byte: IWDG_SW / RST_STOP / RST_STDBY.
* @note To correctly run this function, the FLASH_OB_Unlock() function
* must be called before.
* Call the FLASH_OB_Lock() to disable the flash control register access and the option bytes
* (recommended to protect the FLASH memory against possible unwanted operation)
* @param OB_IWDG: Selects the WDG mode
* This parameter can be one of the following values:
* @arg OB_IWDG_SW: Software WDG selected
* @arg OB_IWDG_HW: Hardware WDG selected
* @param OB_STOP: Reset event when entering STOP mode.
* This parameter can be one of the following values:
* @arg OB_STOP_NoRST: No reset generated when entering in STOP
* @arg OB_STOP_RST: Reset generated when entering in STOP
* @param OB_STDBY: Reset event when entering Standby mode.
* This parameter can be one of the following values:
* @arg OB_STDBY_NoRST: No reset generated when entering in STANDBY
* @arg OB_STDBY_RST: Reset generated when entering in STANDBY
* @retval FLASH Status: The returned value can be:
* FLASH_ERROR_PROGRAM, FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT.
*/
FLASH_Status FLASH_OB_UserConfig(uint8_t OB_IWDG, uint8_t OB_STOP, uint8_t OB_STDBY)
{
FLASH_Status status = FLASH_COMPLETE;
/* Check the parameters */
assert_param(IS_OB_IWDG_SOURCE(OB_IWDG));
assert_param(IS_OB_STOP_SOURCE(OB_STOP));
assert_param(IS_OB_STDBY_SOURCE(OB_STDBY));
/* Wait for last operation to be completed */
status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
if(status == FLASH_COMPLETE)
{
/* Enable the Option Bytes Programming operation */
FLASH->CR |= FLASH_CR_OPTPG;
OB->USER = (uint16_t)((uint16_t)(OB_IWDG | OB_STOP) | (uint16_t)(OB_STDBY | 0xF8));
/* Wait for last operation to be completed */
status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
if(status != FLASH_TIMEOUT)
{
/* If the program operation is completed, disable the OPTPG Bit */
FLASH->CR &= ~FLASH_CR_OPTPG;
}
}
/* Return the Option Byte program Status */
return status;
}
/**
* @brief Sets or resets the BOOT1.
* @param OB_BOOT1: Set or Reset the BOOT1.
* This parameter can be one of the following values:
* @arg OB_BOOT1_RESET: BOOT1 Reset
* @arg OB_BOOT1_SET: BOOT1 Set
* @retval None
*/
FLASH_Status FLASH_OB_BOOTConfig(uint8_t OB_BOOT1)
{
FLASH_Status status = FLASH_COMPLETE;
/* Check the parameters */
assert_param(IS_OB_BOOT1(OB_BOOT1));
/* Wait for last operation to be completed */
status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
if(status == FLASH_COMPLETE)
{
/* Enable the Option Bytes Programming operation */
FLASH->CR |= FLASH_CR_OPTPG;
OB->USER = OB_BOOT1 | 0xEF;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -