📄 stm32l1xx_flash.c
字号:
* this function to configure the Fixed Time Programming
* @param Address: specifies the address to be written.
* @param Data: specifies the data to be written.
* @retval FLASH Status: The returned value can be:
* FLASH_ERROR_PROGRAM, FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT.
*/
FLASH_Status DATA_EEPROM_ProgramHalfWord(uint32_t Address, uint16_t Data)
{
FLASH_Status status = FLASH_COMPLETE;
#if !defined (STM32L1XX_HD) && !defined (STM32L1XX_MDP)
uint32_t tmp = 0, tmpaddr = 0;
#endif
/* Check the parameters */
assert_param(IS_FLASH_DATA_ADDRESS(Address));
/* Wait for last operation to be completed */
status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
if(status == FLASH_COMPLETE)
{
#if !defined (STM32L1XX_HD) && !defined (STM32L1XX_MDP)
if(Data != (uint16_t)0x0000)
{
*(__IO uint16_t *)Address = Data;
/* Wait for last operation to be completed */
status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
}
else
{
if((Address & 0x3) != 0x3)
{
tmpaddr = Address & 0xFFFFFFFC;
tmp = * (__IO uint32_t *) tmpaddr;
tmpaddr = 0xFFFF << ((uint32_t) (0x8 * (Address & 0x3)));
tmp &= ~tmpaddr;
status = DATA_EEPROM_EraseWord(Address & 0xFFFFFFFC);
status = DATA_EEPROM_FastProgramWord((Address & 0xFFFFFFFC), tmp);
}
else
{
DATA_EEPROM_FastProgramByte(Address, 0x00);
DATA_EEPROM_FastProgramByte(Address + 1, 0x00);
}
}
#elif defined (STM32L1XX_HD) || defined (STM32L1XX_MDP)
*(__IO uint16_t *)Address = Data;
/* Wait for last operation to be completed */
status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
#endif
}
/* Return the Write Status */
return status;
}
/**
* @brief Programs a word at a specified address in data memory without erase.
* @note To correctly run this function, the DATA_EEPROM_Unlock() function
* must be called before.
* Call the DATA_EEPROM_Lock() to he data EEPROM access
* and Flash program erase control register access(recommended to protect
* the DATA_EEPROM against possible unwanted operation).
* @note The function DATA_EEPROM_FixedTimeProgramCmd() can be called before
* this function to configure the Fixed Time Programming.
* @param Address: specifies the address to be written.
* @param Data: specifies the data to be written.
* @retval FLASH Status: The returned value can be:
* FLASH_ERROR_PROGRAM, FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT.
*/
FLASH_Status DATA_EEPROM_ProgramWord(uint32_t Address, uint32_t Data)
{
FLASH_Status status = FLASH_COMPLETE;
/* Check the parameters */
assert_param(IS_FLASH_DATA_ADDRESS(Address));
/* Wait for last operation to be completed */
status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
if(status == FLASH_COMPLETE)
{
*(__IO uint32_t *)Address = Data;
/* Wait for last operation to be completed */
status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
}
/* Return the Write Status */
return status;
}
/**
* @}
*/
/** @defgroup FLASH_Group4 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_WRPConfig(uint32_t OB_WRP, FunctionalState NewState);
(+) FLASH_Status FLASH_OB_WRP1Config(uint32_t OB_WRP1, FunctionalState NewState);
(+) FLASH_Status FLASH_OB_WRP2Config(uint32_t OB_WRP2, 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_BORConfig(uint8_t OB_BOR);
(+) uint8_t FLASH_OB_GetUser(void);
(+) uint32_t FLASH_OB_GetWRP(void);
(+) uint32_t FLASH_OB_GetWRP1(void);
(+) uint32_t FLASH_OB_GetWRP2(void);
(+) FlagStatus FLASH_OB_GetRDP(void);
(+) uint8_t FLASH_OB_GetBOR(void);
(+) FLASH_Status FLASH_OB_BootConfig(uint16_t OB_BOOT);
[..] Any operation of erase or program should follow these steps:
(#) Call the FLASH_OB_Unlock() function to enable the Flash option control
register access.
(#) Call one or several functions to program the desired option bytes.
(++) void FLASH_OB_WRPConfig(uint32_t OB_WRP, FunctionalState NewState) => to Enable/Disable
the desired sector write protection.
(++) void FLASH_OB_RDPConfig(uint8_t OB_RDP) => to set the desired read Protection Level.
(++) void 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.
(++) void FLASH_OB_BORConfig(uint8_t OB_BOR) => to Set the BOR level.
(++) FLASH_Status FLASH_ProgramOTP(uint32_t Address, uint32_t Data) => to program the OTP 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 Flash option control register 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->PECR & FLASH_PECR_OPTLOCK) != RESET)
{
/* Unlocking the data memory and FLASH_PECR register access */
DATA_EEPROM_Unlock();
/* 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 OPTLOCK Bit to lock the option bytes block access */
FLASH->PECR |= FLASH_PECR_OPTLOCK;
}
/**
* @brief Launch the option byte loading.
* @param None
* @retval None
*/
void FLASH_OB_Launch(void)
{
/* Set the OBL_Launch bit to lauch the option byte loading */
FLASH->PECR |= FLASH_PECR_OBL_LAUNCH;
}
/**
* @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:
* @param value between OB_WRP_Pages0to15 and OB_WRP_Pages496to511
* @param OB_WRP_AllPages
* @param NewState: new state of the specified FLASH Pages Wtite protection.
* This parameter can be: ENABLE or DISABLE.
* @retval FLASH Status: The returned value can be:
* FLASH_ERROR_PROGRAM, FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT.
*/
FLASH_Status FLASH_OB_WRPConfig(uint32_t OB_WRP, FunctionalState NewState)
{
uint32_t WRP01_Data = 0, WRP23_Data = 0;
FLASH_Status status = FLASH_COMPLETE;
uint32_t tmp1 = 0, tmp2 = 0;
/* Check the parameters */
assert_param(IS_OB_WRP(OB_WRP));
assert_param(IS_FUNCTIONAL_STATE(NewState));
/* Wait for last operation to be completed */
status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
if(status == FLASH_COMPLETE)
{
if (NewState != DISABLE)
{
WRP01_Data = (uint16_t)(((OB_WRP & WRP01_MASK) | OB->WRP01));
WRP23_Data = (uint16_t)((((OB_WRP & WRP23_MASK)>>16 | OB->WRP23)));
tmp1 = (uint32_t)(~(WRP01_Data) << 16)|(WRP01_Data);
OB->WRP01 = tmp1;
tmp2 = (uint32_t)(~(WRP23_Data) << 16)|(WRP23_Data);
OB->WRP23 = tmp2;
}
else
{
WRP01_Data = (uint16_t)(~OB_WRP & (WRP01_MASK & OB->WRP01));
WRP23_Data = (uint16_t)((((~OB_WRP & WRP23_MASK)>>16 & OB->WRP23)));
tmp1 = (uint32_t)((~WRP01_Data) << 16)|(WRP01_Data);
OB->WRP01 = tmp1;
tmp2 = (uint32_t)((~WRP23_Data) << 16)|(WRP23_Data);
OB->WRP23 = tmp2;
}
/* Wait for last operation to be completed */
status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
}
/* Return the write protection operation Status */
return status;
}
/**
* @brief Write protects the desired pages.
* @note This function can be used only for STM32L1XX_HD and STM32L1XX_MDP
* density devices.
* 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_WRP1: specifies the address of the pages to be write protected.
* This parameter can be:
* @arg value between OB_WRP_Pages512to527 and OB_WRP_Pages1008to1023
* @arg OB_WRP_AllPages
* @param NewState: new state of the specified FLASH Pages Wtite protection.
* This parameter can be: ENABLE or DISABLE.
* @retval FLASH Status: The returned value can be:
* FLASH_ERROR_PROGRAM, FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT.
*/
FLASH_Status FLASH_OB_WRP1Config(uint32_t OB_WRP1, FunctionalState NewState)
{
uint32_t WRP45_Data = 0, WRP67_Data = 0;
FLASH_Status status = FLASH_COMPLETE;
uint32_t tmp1 = 0, tmp2 = 0;
/* Check the parameters */
assert_param(IS_OB_WRP(OB_WRP1));
assert_param(IS_FUNCTIONAL_STATE(NewState));
/* Wait for last operation to be completed */
status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
if(status == FLASH_COMPLETE)
{
if (NewState != DISABLE)
{
WRP45_Data = (uint16_t)(((OB_WRP1 & WRP45_MASK) | OB->WRP45));
WRP67_Data = (uint16_t)((((OB_WRP1 & WRP67_MASK)>>16 | OB->WRP67)));
tmp1 = (uint32_t)(~(WRP45_Data) << 16)|(WRP45_Data);
OB->WRP45 = tmp1;
tmp2 = (uint32_t)(~(WRP67_Data) << 16)|(WRP67_Data);
OB->WRP67 = tmp2;
}
else
{
WRP45_Data = (uint16_t)(~OB_WRP1 & (WRP45_MASK & OB->WRP45));
WRP67_Data = (uint16_t)((((~OB_WRP1 & WRP67_MASK)>>16 & OB->WRP67)));
tmp1 = (uint32_t)((~WRP45_Data) << 16)|(WRP45_Data);
OB->WRP45 = tmp1;
tmp2 = (uint32_t)((~WRP67_Data) << 16)|(WRP67_Data);
OB->WRP67 = tmp2;
}
/* Wait for last operation to be completed */
status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
}
/* Return the write protection operation Status */
return status;
}
/**
* @brief Write protects the desired pages.
* @note This function can be used only for STM32L1XX_HD density devices.
* 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_WRP2: specifies the address of the pages to be write protected.
* This parameter can be:
* @arg value between OB_WRP_Pages1024to1039 and OB_WRP_Pages1520to1535
* @arg OB_WRP_AllPages
* @param NewState: new state of the specified FLASH Pages Wtite protection.
* This parameter can be: ENABLE or DISABLE.
* @retval FLASH Status: The returned value can be:
* FLASH_ERROR_PROGRAM, FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT.
*/
FLASH_Status FLASH_OB_WRP2Config(uint32_t OB_WRP2, FunctionalState NewState)
{
uint32_t WRP89_Data = 0, WRP1011_Data = 0;
FLASH_Status status = FLASH_COMPLETE;
uint32_t tmp1 = 0, tmp2 = 0;
/* Check the parameters */
assert_param(IS_OB_WRP(OB_WRP2));
assert_param(IS_FUNCTIONAL_STATE(NewState));
/* Wait for last operation to be completed */
status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
if(status == FLASH_COMPLETE)
{
if (NewState != DISABLE)
{
WRP89_Data = (uint16_t)(((OB_WRP2 & WRP89_MASK) | OB->WRP89));
WRP1011_Data = (uint16_t)((((OB_WRP2 & WRP1011_MASK)>>16 | OB->WRP1011)));
tmp1 = (uint32_t)(~(WRP89_Data) << 16)|(WRP89_Data);
OB->WRP89 = tmp1;
tmp2 = (uint32_t)(~(WRP1011_Data) << 16)|(WRP1011_Data);
OB->WRP1011 = tmp2;
}
else
{
WRP89_Data = (uint16_t)(~OB_WRP2 & (WRP89_MASK & OB->WRP89));
WRP1011_Data = (uint16_t)((((~OB_WRP2 & WRP1011_MASK)>>16 & OB->WRP1011)));
tmp1 = (uint32_t)((~WRP89_Data) << 16)|(WRP89_Data);
OB->WRP89 = tmp1;
tmp2 = (uint32_t)((~WRP1011_Data) << 16)|(WRP1011_Data);
OB->WRP1011 = tmp2;
}
/* Wait for last operation to be completed */
status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
}
/* 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;
uint8_t tmp1 = 0;
uint32_t tmp2 = 0;
/* Check the parameters */
assert_param(IS_OB_RDP(OB_RDP));
status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
/* calculate the option byte to write */
tmp1 = (uint8_t)(~(OB_RDP ));
tmp2 = (uint32_t)(((uint32_t)((uint32_t)(tmp1) << 16)) | ((uint32_t)OB_RDP));
if(status == FLASH_COMPLETE)
{
/* program read protection level */
OB->RDP = tmp2;
}
/* Wait for last operation to be completed */
status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
/* Return the Read 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.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -