/* 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((++time)> FLASH_OPT_OVERTIMER){
res =1;break;}}/* If operation is completed or interrupted, disable the Page Erase Bit */CLEAR_BIT(FLASH->CR, FLASH_CR_PER);return res;}
/* Program double-word (64-bit) at a specified address *//* Must EN PG bit before and DIS PG bit after */static u8 ubFLASH_Program_DoubleWord(u32 addr, u64 data){
u32 time =0;/* Wait for last operation to be completed */while((FLASH->SR & FLASH_SR_BSY1)!=0x00U){if((++time)> FLASH_OPT_OVERTIMER){return1;}}/* Set PG bit */SET_BIT(FLASH->CR, FLASH_CR_PG);/* Program first word */*(u32 *)addr =(u32)data;/* Barrier to ensure programming is performed in 2 steps, in right order
(independently of compiler optimization behavior) */__ISB();/* Program second word */*(u32 *)(addr +4U)=(u32)(data >>32U);/* Wait for last operation to be completed */while((FLASH->SR & FLASH_SR_BSY1)!=0x00U){if((++time)> FLASH_OPT_OVERTIMER){return2;}}return0;}
Flash LL库双字写
/* Program double-word(64-bit) at a specified address */
u8 ubFlash_Write_DoubleWord(u32 startAddr, u64 * pDat, u16 len){
u32 page =0, time =0;
u8 tryCount =0, res =0;
u16 i =0;
FLASH_UNLOCK:if(ubFLASH_Unlock()){if((++tryCount)< FLASH_OPT_TRY_COUNT){
res =ubFlash_WaitFor_Operate(FLASH_OPT_OVERTIMER);dprintf("Wait For Operate %s...%d\r\n",(res ?"Fail":"OK"), res);goto FLASH_UNLOCK;}else{dprintf("Flash Unlock Fail...\r\n");return1;}}
page =ulGetPage(startAddr);
tryCount =0;
FLASH_ERASE:if(ubFLASH_PageErase(page)){if((++tryCount)< FLASH_OPT_TRY_COUNT){
res =ubFlash_WaitFor_Operate(FLASH_OPT_OVERTIMER);dprintf("Wait For Operate %s...%d\r\n",(res ?"Fail":"OK"), res);goto FLASH_ERASE;}else{ubFLASH_Lock();dprintf("Flash Erase Fail...\r\n");return2;}}
tryCount =0;for(i =0; i < len;++i){while(tryCount < FLASH_OPT_TRY_COUNT){if(ubFLASH_Program_DoubleWord(startAddr , pDat[i])){
res =ubFlash_WaitFor_Operate(FLASH_OPT_OVERTIMER);dprintf("Wait For Operate %s...%d\r\n",(res ?"Fail":"OK"), res);
tryCount++;}else{
startAddr +=8;
tryCount =0;break;}}if(tryCount){ubFLASH_Lock();dprintf("Write Flash Fail...\r\n");return3;}}ubFLASH_Lock();dprintf("Write Flash OK...\r\n");return0;}
Init UART OK..
Vref:3298mV
SystemParamStore Size:8811
Read System Param OK...******************** System Parameter ********************
Meas Masx Range:20000dm
Angle Demar :0==>0
Dist Demar :0==>0
Meas Type :6
Meas Unit :1
Gryo Cali :0==>000**********************************************************
Gyro Type MPU6887 ID 0x0F
Set Meas Type:6
Shutdown OverTime:20s
Bat Sat :0
Set Meas Type:2
Set Meas Type:9
Set Meas Type:3
Set Meas Type:5
Set Meas Type:7
Set Meas Type:8
SystemParam CRC8...E7
Write Flash OK...
Save Param OK...
Overtime Shutdown...
Init UART OK..
Vref:3302mV
SystemParamStore Size:8811
Read System Param OK...******************** System Parameter ********************
Meas Max Range:20000dm
Angle Demar :0==>0
Dist Demar :0==>0
Meas Type :8
Meas Unit :1
Gryo Cali :0==>000**********************************************************
Gyro Type MPU6887 ID 0x0F
Set Meas Type:8
Shutdown OverTime:20s
Bat Sat :0
SystemParam CRC8...E7
Write Flash OK...
Save Param OK...
Overtime Shutdown...