This example provides a description of how to enable and disable the write protection
for FLASH integrated within STM32F40xx/41xx and STM32F427x/437x Devices.
By maintaining the Key push-button pressed at Reset, the program will check the
write protection status of FLASH_WRP_SECTORS (defined in main.c)
- If FLASH_WRP_SECTORS are write protected, the write protection will be disabled.
Then LED1 will turn ON, if the protection disable operation is done correctly,
otherwise LED3 turns ON.
- If FLASH_WRP_SECTORS are not write protected, the write protection will be enabled.
Then LED4 will turn ON, if the protection enable operation is done correctly,
otherwise LED3 turns ON.
If after reset the Key push-button is not pressed, the program will simply turn ON LED2.
2. main.c
/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
#define FLASH_WRP_SECTORS (OB_WRP_Sector_2 | OB_WRP_Sector_3) /* sectors 2 and 3 */
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
__IO uint32_t SectorsWRPStatus = 0xFFF;
/* Private function prototypes -----------------------------------------------*/
/* Private functions ---------------------------------------------------------*/
/**
* @brief Main program
* @param None
* @retval None
*/
int main(void)
{
/*!< At this stage the microcontroller clock setting is already configured,
this is done through SystemInit() function which is called from startup
file (startup_stm32f40xx.s/startup_stm32f427x.s) before to branch to
application main.
To reconfigure the default setting of SystemInit() function, refer to
system_stm32f4xx.c file
*/
/* Initialize LEDs mounted on EVAL board */
STM_EVAL_LEDInit(LED1);
STM_EVAL_LEDInit(LED2);
STM_EVAL_LEDInit(LED3);
STM_EVAL_LEDInit(LED4);
/* Initialize Key Button mounted on EVAL board */
STM_EVAL_PBInit(BUTTON_KEY, BUTTON_MODE_GPIO);
/* Test if Key push-button on EVAL board is pressed */
if (STM_EVAL_PBGetState(BUTTON_KEY) == 0x00)
{
/* Get FLASH_WRP_SECTORS write protection status */
SectorsWRPStatus = FLASH_OB_GetWRP() & FLASH_WRP_SECTORS;
if (SectorsWRPStatus == 0x00)
{
/* If FLASH_WRP_SECTORS are write protected, disable the write protection */
/* Enable the Flash option control register access */
FLASH_OB_Unlock();
/* Disable FLASH_WRP_SECTORS write protection */
FLASH_OB_WRPConfig(FLASH_WRP_SECTORS, DISABLE);
/* Start the Option Bytes programming process */
if (FLASH_OB_Launch() != FLASH_COMPLETE)
{
/* User can add here some code to deal with this error */
while (1)
{
}
}
/* Disable the Flash option control register access (recommended to protect
the option Bytes against possible unwanted operations) */
FLASH_OB_Lock();
/* Get FLASH_WRP_SECTORS write protection status */
SectorsWRPStatus = FLASH_OB_GetWRP() & FLASH_WRP_SECTORS;
/* Check if FLASH_WRP_SECTORS write protection is disabled */
if (SectorsWRPStatus == FLASH_WRP_SECTORS)
{
/* OK, turn ON LED1 */
STM_EVAL_LEDOn(LED1);
}
else
{
/* KO, turn ON LED3 */
STM_EVAL_LEDOn(LED3);
}
}
else
{ /* If FLASH_WRP_SECTORS are not write protected, enable the write protection */
/* Enable the Flash option control register access */
FLASH_OB_Unlock();
/* Enable FLASH_WRP_SECTORS write protection */
FLASH_OB_WRPConfig(FLASH_WRP_SECTORS, ENABLE);
/* Start the Option Bytes programming process */
if (FLASH_OB_Launch() != FLASH_COMPLETE)
{
/* User can add here some code to deal with this error */
while (1)
{
}
}
/* Disable the Flash option control register access (recommended to protect
the option Bytes against possible unwanted operations) */
FLASH_OB_Lock();
/* Get FLASH_WRP_SECTORS write protection status */
SectorsWRPStatus = FLASH_OB_GetWRP() & FLASH_WRP_SECTORS;
/* Check if FLASH_WRP_SECTORS are write protected */
if (SectorsWRPStatus == 0x00)
{
/* OK, turn ON LED4 */
STM_EVAL_LEDOn(LED4);
}
else
{
/* KO, turn ON LED3 */
STM_EVAL_LEDOn(LED3);
}
}
}
/* Turn ON LED2 */
STM_EVAL_LEDOn(LED2);
while (1)
{
}
}
3. Explain code
uint16_t FLASH_OB_GetWRP(void)
{
/* Return the FLASH write protection Register value */
return (*(__IO uint16_t *)(OPTCR_BYTE2_ADDRESS));
}
It will return Register value at address : OPTCR_BYTE2_ADDRESS.
(__IO uint16_t *)(OPTCR_BYTE2_ADDRESS) : Pointer point to address.
void FLASH_OB_Unlock(void)
{
if((FLASH->OPTCR & FLASH_OPTCR_OPTLOCK) != RESET)
{
/* Authorizes the Option Byte register programming ,read datasheet to know these value*/
FLASH->OPTKEYR = FLASH_OPT_KEY1;
FLASH->OPTKEYR = FLASH_OPT_KEY2;
}
}
FLASH_OPTCR
0: Write protection active on sector i
1: Write protection not active on sector i
The 10 best blackjack table games - JTM Hub
ReplyDeleteLearn 안동 출장마사지 to play at 충주 출장안마 the best blackjack table 전주 출장안마 games at JTM Hub. We 천안 출장마사지 have many live dealers and 계룡 출장안마 have the best online tables to entertain you.