📄 stm32f10x_flash.c
字号:
* - OB_STDBY_NoRST: No reset generated when entering in STANDBY
* - OB_STDBY_RST: Reset generated when entering in STANDBY
* Output : None
* Return : FLASH Status: The returned value can be: FLASH_BUSY,
* FLASH_ERROR_PG or FLASH_ERROR_WRP or FLASH_COMPLETE or
* FLASH_TIMEOUT.
*******************************************************************************/
FLASH_Status FLASH_UserOptionByteConfig(u16 OB_IWDG, u16 OB_STOP, u16 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));
/* Authorize the small information block programming */
FLASH->OPTKEYR = FLASH_KEY1;
FLASH->OPTKEYR = FLASH_KEY2;
/* Wait for last operation to be completed */
status = FLASH_WaitForLastOperation(ProgramTimeout);
if(status == FLASH_COMPLETE)
{
/* Enable the Option Bytes Programming operation */
FLASH->CR |= CR_OPTPG_Set;
OB->USER = ( OB_IWDG | OB_STOP |OB_STDBY) | (u16)0xF8;
/* Wait for last operation to be completed */
status = FLASH_WaitForLastOperation(ProgramTimeout);
if(status != FLASH_BUSY)
{
/* if the program operation is completed, disable the OPTPG Bit */
FLASH->CR &= CR_OPTPG_Reset;
}
}
/* Return the Option Byte program Status */
return status;
}
/*******************************************************************************
* Function Name : FLASH_GetUserOptionByte
* Description : Returns the FLASH User Option Bytes values.
* Input : None
* Output : None
* Return : The FLASH User Option Bytes values:IWDG_SW(Bit0), RST_STOP(Bit1)
* and RST_STDBY(Bit2).
*******************************************************************************/
u32 FLASH_GetUserOptionByte(void)
{
/* Return the User Option Byte */
return (u32)(FLASH->OBR >> 2);
}
/*******************************************************************************
* Function Name : FLASH_GetWriteProtectionOptionByte
* Description : Returns the FLASH Write Protection Option Bytes Register value.
* Input : None
* Output : None
* Return : The FLASH Write Protection Option Bytes Register value
*******************************************************************************/
u32 FLASH_GetWriteProtectionOptionByte(void)
{
/* Return the Falsh write protection Register value */
return (u32)(FLASH->WRPR);
}
/*******************************************************************************
* Function Name : FLASH_GetReadOutProtectionStatus
* Description : Checks whether the FLASH Read Out Protection Status is set
* or not.
* Input : None
* Output : None
* Return : FLASH ReadOut Protection Status(SET or RESET)
*******************************************************************************/
FlagStatus FLASH_GetReadOutProtectionStatus(void)
{
FlagStatus readoutstatus = RESET;
if ((FLASH->OBR & RDPRT_Mask) != (u32)RESET)
{
readoutstatus = SET;
}
else
{
readoutstatus = RESET;
}
return readoutstatus;
}
/*******************************************************************************
* Function Name : FLASH_GetPrefetchBufferStatus
* Description : Checks whether the FLASH Prefetch Buffer status is set or not.
* Input : None
* Output : None
* Return : FLASH Prefetch Buffer Status (SET or RESET).
*******************************************************************************/
FlagStatus FLASH_GetPrefetchBufferStatus(void)
{
FlagStatus bitstatus = RESET;
if ((FLASH->ACR & ACR_PRFTBS_Mask) != (u32)RESET)
{
bitstatus = SET;
}
else
{
bitstatus = RESET;
}
/* Return the new state of FLASH Prefetch Buffer Status (SET or RESET) */
return bitstatus;
}
/*******************************************************************************
* Function Name : FLASH_ITConfig
* Description : Enables or disables the specified FLASH interrupts.
* Input : - FLASH_IT: specifies the FLASH interrupt sources to be
* enabled or disabled.
* This parameter can be any combination of the following values:
* - FLASH_IT_ERROR: FLASH Error Interrupt
* - FLASH_IT_EOP: FLASH end of operation Interrupt
* Output : None
* Return : None
*******************************************************************************/
void FLASH_ITConfig(u16 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->CR |= FLASH_IT;
}
else
{
/* Disable the interrupt sources */
FLASH->CR &= ~(u32)FLASH_IT;
}
}
/*******************************************************************************
* Function Name : FLASH_GetFlagStatus
* Description : Checks whether the specified FLASH flag is set or not.
* Input : - FLASH_FLAG: specifies the FLASH flag to check.
* This parameter can be one of the following values:
* - FLASH_FLAG_BSY: FLASH Busy flag
* - FLASH_FLAG_PGERR: FLASH Program error flag
* - FLASH_FLAG_WRPRTERR: FLASH Write protected error flag
* - FLASH_FLAG_EOP: FLASH End of Operation flag
* - FLASH_FLAG_OPTERR: FLASH Option Byte error flag
* Output : None
* Return : The new state of FLASH_FLAG (SET or RESET).
*******************************************************************************/
FlagStatus FLASH_GetFlagStatus(u16 FLASH_FLAG)
{
FlagStatus bitstatus = RESET;
/* Check the parameters */
assert_param(IS_FLASH_GET_FLAG(FLASH_FLAG)) ;
if(FLASH_FLAG == FLASH_FLAG_OPTERR)
{
if((FLASH->OBR & FLASH_FLAG_OPTERR) != (u32)RESET)
{
bitstatus = SET;
}
else
{
bitstatus = RESET;
}
}
else
{
if((FLASH->SR & FLASH_FLAG) != (u32)RESET)
{
bitstatus = SET;
}
else
{
bitstatus = RESET;
}
}
/* Return the new state of FLASH_FLAG (SET or RESET) */
return bitstatus;
}
/*******************************************************************************
* Function Name : FLASH_ClearFlag
* Description : Clears the FLASH抯 pending flags.
* Input : - FLASH_FLAG: specifies the FLASH flags to clear.
* This parameter can be any combination of the following values:
* - FLASH_FLAG_BSY: FLASH Busy flag
* - FLASH_FLAG_PGERR: FLASH Program error flag
* - FLASH_FLAG_WRPRTERR: FLASH Write protected error flag
* - FLASH_FLAG_EOP: FLASH End of Operation flag
* Output : None
* Return : None
*******************************************************************************/
void FLASH_ClearFlag(u16 FLASH_FLAG)
{
/* Check the parameters */
assert_param(IS_FLASH_CLEAR_FLAG(FLASH_FLAG)) ;
/* Clear the flags */
FLASH->SR = FLASH_FLAG;
}
/*******************************************************************************
* Function Name : FLASH_GetStatus
* Description : Returns the FLASH Status.
* Input : None
* Output : None
* Return : FLASH Status: The returned value can be: FLASH_BUSY,
* FLASH_ERROR_PG or 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 & FLASH_FLAG_PGERR)
{
flashstatus = FLASH_ERROR_PG;
}
else
{
if(FLASH->SR & FLASH_FLAG_WRPRTERR)
{
flashstatus = FLASH_ERROR_WRP;
}
else
{
flashstatus = FLASH_COMPLETE;
}
}
}
/* Return the Flash Status */
return flashstatus;
}
/*******************************************************************************
* Function Name : FLASH_WaitForLastOperation
* Description : Waits for a Flash operation to complete or a TIMEOUT to occur.
* Input : - Timeout: FLASH progamming Timeout
* Output : None
* Return : FLASH Status: The returned value can be: FLASH_BUSY,
* FLASH_ERROR_PG or FLASH_ERROR_WRP or FLASH_COMPLETE or
* FLASH_TIMEOUT.
*******************************************************************************/
FLASH_Status FLASH_WaitForLastOperation(u32 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))
{
delay();
status = FLASH_GetStatus();
Timeout--;
}
if(Timeout == 0x00 )
{
status = FLASH_TIMEOUT;
}
/* Return the operation status */
return status;
}
/*******************************************************************************
* Function Name : delay
* Description : Inserts a time delay.
* Input : None
* Output : None
* Return : None
*******************************************************************************/
static void delay(void)
{
vu32 i = 0;
for(i = 0xFF; i != 0; i--)
{
}
}
#endif
/******************* (C) COPYRIGHT 2007 STMicroelectronics *****END OF FILE****/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -