?? stm32l1xx_flash.c
字號(hào):
* @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;
uint32_t tmp = 0, tmp1 = 0;
/* 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));
/* Get the User Option byte register */
tmp1 = (FLASH->OBR & 0x000F0000) >> 16;
/* Calculate the user option byte to write */
tmp = (uint32_t)(((uint32_t)~((uint32_t)((uint32_t)(OB_IWDG) | (uint32_t)(OB_STOP) | (uint32_t)(OB_STDBY) | tmp1))) << ((uint32_t)0x10));
tmp |= ((uint32_t)(OB_IWDG) | ((uint32_t)OB_STOP) | (uint32_t)(OB_STDBY) | tmp1);
/* Wait for last operation to be completed */
status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
if(status == FLASH_COMPLETE)
{
/* Write the User Option Byte */
OB->USER = tmp;
}
/* Wait for last operation to be completed */
status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
/* Return the Option Byte program Status */
return status;
}
/**
* @brief Programs the FLASH brownout reset threshold level Option Byte.
* @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_BOR: Selects the brownout reset threshold level
* This parameter can be one of the following values:
* @arg OB_BOR_OFF: BOR is disabled at power down, the reset is asserted when the VDD
* power supply reaches the PDR(Power Down Reset) threshold (1.5V)
* @arg OB_BOR_LEVEL1: BOR Reset threshold levels for 1.7V - 1.8V VDD power supply
* @arg OB_BOR_LEVEL2: BOR Reset threshold levels for 1.9V - 2.0V VDD power supply
* @arg OB_BOR_LEVEL3: BOR Reset threshold levels for 2.3V - 2.4V VDD power supply
* @arg OB_BOR_LEVEL4: BOR Reset threshold levels for 2.55V - 2.65V VDD power supply
* @arg OB_BOR_LEVEL5: BOR Reset threshold levels for 2.8V - 2.9V VDD power supply
* @retval FLASH Status: The returned value can be:
* FLASH_ERROR_PROGRAM, FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT.
*/
FLASH_Status FLASH_OB_BORConfig(uint8_t OB_BOR)
{
FLASH_Status status = FLASH_COMPLETE;
uint32_t tmp = 0, tmp1 = 0;
/* Check the parameters */
assert_param(IS_OB_BOR_LEVEL(OB_BOR));
/* Get the User Option byte register */
tmp1 = (FLASH->OBR & 0x00700000) >> 16;
/* Calculate the option byte to write */
tmp = (uint32_t)~(OB_BOR | tmp1)<<16;
tmp |= (OB_BOR | tmp1);
/* Wait for last operation to be completed */
status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
if(status == FLASH_COMPLETE)
{
/* Write the BOR Option Byte */
OB->USER = tmp;
}
/* Wait for last operation to be completed */
status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
/* Return the Option Byte program Status */
return status;
}
/**
* @brief Returns the FLASH User Option Bytes values.
* @param None
* @retval The FLASH User Option Bytes .
*/
uint8_t FLASH_OB_GetUser(void)
{
/* Return the User Option Byte */
return (uint8_t)(FLASH->OBR >> 20);
}
/**
* @brief Returns the FLASH Write Protection Option Bytes value.
* @param None
* @retval The FLASH Write Protection Option Bytes value
*/
uint32_t FLASH_OB_GetWRP(void)
{
/* Return the FLASH write protection Register value */
return (uint32_t)(FLASH->WRPR);
}
/**
* @brief Checks whether the FLASH Read out Protection Status is set or not.
* @param None
* @retval FLASH ReadOut Protection Status(SET or RESET)
*/
FlagStatus FLASH_OB_GetRDP(void)
{
FlagStatus readstatus = RESET;
if ((uint8_t)(FLASH->OBR) != (uint8_t)OB_RDP_Level_0)
{
readstatus = SET;
}
else
{
readstatus = RESET;
}
return readstatus;
}
/**
* @brief Returns the FLASH BOR level.
* @param None
* @retval The FLASH User Option Bytes .
*/
uint8_t FLASH_OB_GetBOR(void)
{
/* Return the BOR level */
return (uint8_t)((FLASH->OBR & (uint32_t)0x000F0000) >> 16);
}
/**
* @}
*/
/** @defgroup FLASH_Group5 Interrupts and flags management functions
* @brief Interrupts and flags management functions
*
@verbatim
===============================================================================
Interrupts and flags management functions
===============================================================================
@endverbatim
* @{
*/
/**
* @brief Enables or disables the specified FLASH interrupts.
* @param FLASH_IT: specifies the FLASH interrupt sources to be enabled or
* disabled.
* This parameter can be any combination of the following values:
* @arg FLASH_IT_EOP: FLASH end of programming Interrupt
* @arg FLASH_IT_ERR: FLASH Error Interrupt
* @retval None
*/
void FLASH_ITConfig(uint32_t FLASH_IT, FunctionalState NewState)
{
/* Check the parameters */
assert_param(IS_FLASH_IT(FLASH_IT));
assert_param(IS_FUNCTIONAL_STATE(NewState));
if(NewState != DISABLE)
{
/* Enable the interrupt sources */
FLASH->PECR |= FLASH_IT;
}
else
{
/* Disable the interrupt sources */
FLASH->PECR &= ~(uint32_t)FLASH_IT;
}
}
/**
* @brief Checks whether the specified FLASH flag is set or not.
* @param FLASH_FLAG: specifies the FLASH flag to check.
* This parameter can be one of the following values:
* @arg FLASH_FLAG_BSY: FLASH write/erase operations in progress flag
* @arg FLASH_FLAG_EOP: FLASH End of Operation flag
* @arg FLASH_FLAG_READY: FLASH Ready flag after low power mode
* @arg FLASH_FLAG_ENDHV: FLASH End of high voltage flag
* @arg FLASH_FLAG_WRPERR: FLASH Write protected error flag
* @arg FLASH_FLAG_PGAERR: FLASH Programming Alignment error flag
* @arg FLASH_FLAG_SIZERR: FLASH size error flag
* @arg FLASH_FLAG_OPTVERR: FLASH Option validity error flag
* @retval The new state of FLASH_FLAG (SET or RESET).
*/
FlagStatus FLASH_GetFlagStatus(uint32_t FLASH_FLAG)
{
FlagStatus bitstatus = RESET;
/* Check the parameters */
assert_param(IS_FLASH_GET_FLAG(FLASH_FLAG));
if((FLASH->SR & FLASH_FLAG) != (uint32_t)RESET)
{
bitstatus = SET;
}
else
{
bitstatus = RESET;
}
/* Return the new state of FLASH_FLAG (SET or RESET) */
return bitstatus;
}
/**
* @brief Clears the FLASH抯 pending flags.
* @param FLASH_FLAG: specifies the FLASH flags to clear.
* This parameter can be any combination of the following values:
* @arg FLASH_FLAG_EOP: FLASH End of Operation flag
* @arg FLASH_FLAG_WRPERR: FLASH Write protected error flag
* @arg FLASH_FLAG_PGAERR: FLASH Programming Alignment error flag
* @arg FLASH_FLAG_SIZERR: FLASH size error flag
* @arg FLASH_FLAG_OPTVERR: FLASH Option validity error flag
* @retval None
*/
void FLASH_ClearFlag(uint32_t FLASH_FLAG)
{
/* Check the parameters */
assert_param(IS_FLASH_CLEAR_FLAG(FLASH_FLAG));
/* Clear the flags */
FLASH->SR = FLASH_FLAG;
}
/**
* @brief Returns the FLASH Status.
* @param None
* @retval FLASH Status: The returned value can be:
* FLASH_BUSY, FLASH_ERROR_PROGRAM, FLASH_ERROR_WRP or FLASH_COMPLETE.
*/
FLASH_Status FLASH_GetStatus(void)
{
FLASH_Status FLASHstatus = FLASH_COMPLETE;
if((FLASH->SR & FLASH_FLAG_BSY) == FLASH_FLAG_BSY)
{
FLASHstatus = FLASH_BUSY;
}
else
{
if((FLASH->SR & (uint32_t)FLASH_FLAG_WRPERR)!= (uint32_t)0x00)
{
FLASHstatus = FLASH_ERROR_WRP;
}
else
{
if((FLASH->SR & (uint32_t)0xFEF0) != (uint32_t)0x00)
{
FLASHstatus = FLASH_ERROR_PROGRAM;
}
else
{
FLASHstatus = FLASH_COMPLETE;
}
}
}
/* Return the FLASH Status */
return FLASHstatus;
}
/**
* @brief Waits for a FLASH operation to complete or a TIMEOUT to occur.
* @param Timeout: FLASH programming Timeout
* @retval FLASH Status: The returned value can be: FLASH_BUSY,
* FLASH_ERROR_PROGRAM, FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT.
*/
FLASH_Status FLASH_WaitForLastOperation(uint32_t Timeout)
{
FLASH_Status status = FLASH_COMPLETE;
/* Check for the FLASH Status */
status = FLASH_GetStatus();
/* Wait for a FLASH operation to complete or a TIMEOUT to occur */
while((status == FLASH_BUSY) && (Timeout != 0x00))
{
status = FLASH_GetStatus();
Timeout--;
}
if(Timeout == 0x00 )
{
status = FLASH_TIMEOUT;
}
/* Return the operation status */
return status;
}
/**
* @}
*/
/**
* @}
*/
/**
* @}
*/
/**
* @}
*/
/******************* (C) COPYRIGHT 2010 STMicroelectronics *****END OF FILE****/
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -