在STM32F10x中,内嵌了闪存存储器Flash,用于在线编程ICP或在程序编程IAP。
FPEC模块又称闪存编程和擦除控制器,包含了7个32位的寄存器,处理闪存的编程和擦除操作。其中有一个FLASH_CR寄存器,称为闪存控制寄存器。
FPEC模块如果被闪存锁锁定,则不能写入FLASH_CR。复位后,闪存锁处于锁定状态。
解锁的方法就是对 FLASH_KEYR 寄存器先写入 0x45670123, 再写入 0xCDEF89AB 。
在 StdPeriph_Driver\stm32f10x_flash.c 中,有一个解锁函数 FLASH_Unlock(),拷贝见下:
/**
* @brief Unlocks the FLASH Program Erase Controller.
* @note This function can be used for all STM32F10x devices.
* - For STM32F10X_XL devices this function unlocks Bank1 and Bank2.
* - For all other devices it unlocks Bank1 and it is equivalent
* to FLASH_UnlockBank1 function..
* @param None
* @retval None
*/
void FLASH_Unlock(void)
{
/* Authorize the FPEC of Bank1 Access */
FLASH->KEYR = FLASH_KEY1;
FLASH->KEYR = FLASH_K