/* Unlock the FLASH control register access */static u8 ubFLASH_Unlock(void){
u8 sta =0;if(READ_BIT(FLASH->CR, FLASH_CR_LOCK)!=0x00U){
/* Authorize the FLASH Registers access */WRITE_REG(FLASH->KEYR, FLASH_KEY1);WRITE_REG(FLASH->KEYR, FLASH_KEY2);/* verify Flash is unlock */if(READ_BIT(FLASH->CR, FLASH_CR_LOCK)!=0x00U){
sta =1;}}return sta;}/* Lock the FLASH control register access */static u8 ubFLASH_Lock(void){
u8 sta =1;/* Set the LOCK Bit to lock the FLASH Registers access */SET_BIT(FLASH->CR, FLASH_CR_LOCK);/* verify Flash is locked */if(READ_BIT(FLASH->CR, FLASH_CR_LOCK)!=0x00u){
sta =0;}return sta;}
Flash选项字节解锁与加锁
/* Unlock the FLASH Option Bytes Registers access */static u8 ubFLASH_OB_Unlock(void){
u8 sta =1;if(READ_BIT(FLASH->CR, FLASH_CR_OPTLOCK)!=0x00U){
/* Authorizes the Option Byte register programming */WRITE_REG(FLASH->OPTKEYR, FLASH_OPTKEY1);WRITE_REG(FLASH->OPTKEYR, FLASH_OPTKEY2);/* verify option bytes are unlocked */if(READ_BIT(FLASH->CR, FLASH_CR_OPTLOCK)==0x00U){
sta =0;}}return sta;}/* Lock the FLASH Option Bytes Registers access */static u8 ubFLASH_OB_Lock(void){
u8 sta =1;/* Set the OPTLOCK Bit to lock the FLASH Option Byte Registers access */SET_BIT(FLASH->CR, FLASH_CR_OPTLOCK);/* verify option bytes are locked */if(READ_BIT(FLASH->CR, FLASH_CR_OPTLOCK)!=0x00u){
sta =0;}return sta;}
Flash擦除页
/* Gets the page of a given address */static u32 ulGetPage(u32 startAddr){
return((startAddr - FLASH_BASE)/ FLASH_PAGE_SIZE);}/* Erase the specified FLASH memory page */static u8 ubFLASH_PageErase(u32 page){
u32 tmp =0;
u32 time =0;
u8 res =0;/* Get configuration register, then clear page number */
tmp =(FLASH->CR &~FLASH_CR_PNB);/* Set page number, Page Erase bit & Start bit */
FLASH->CR =(tmp |(FLASH_CR_STRT |(page << FLASH_CR_PNB_Pos)| FLASH_CR_PER));/* wait for BSY1 in order to be sure that flash operation is ended before allowing prefetch in flash */while((FLASH->SR & FLASH_SR_BSY1)!=0x00U){
if((++tim