/******************************************************************************************************* * * 函数名称: * * 函数功能: * * 入口参数: * * 出口参数: 无 * * 说明: * *******************************************************************************************************/ void FlashWRDPUnlock(void) { uint32_t rt; FlagStatus RDP_state; FLASH_Status Flash_state; uint32_t optcr_byte; //如果匹配,进行Flash的读写解锁 FLASH_ClearFlag(FLASH_FLAG_EOP|FLASH_FLAG_OPERR|FLASH_FLAG_WRPERR| FLASH_FLAG_PGAERR | FLASH_FLAG_PGPERR|FLASH_FLAG_PGSERR); FLASH_Unlock(); FLASH_OB_Unlock(); FLASH_ClearFlag(FLASH_FLAG_EOP|FLASH_FLAG_OPERR|FLASH_FLAG_WRPERR| FLASH_FLAG_PGAERR | FLASH_FLAG_PGPERR|FLASH_FLAG_PGSERR); rt = FLASH_OB_GetWRP(); do { //FlashWRPLock(WRITE, WRP_Sector_All); FLASH_OB_WRPConfig(WRP_Sector_All, DISABLE); //开启写保护 }while(FLASH_OB_Launch() != FLASH_COMPLETE); rt = FLASH_OB_GetWRP(); RDP_state = FLASH_OB_GetRDP(); do { FLASH_OB_RDPConfig(OB_RDP_Level_0);//关闭读保护 }while(FLASH_OB_Launch() != FLASH_COMPLETE); RDP_state = FLASH_OB_GetRDP(); FLASH_OB_Lock(); FLASH_Lock(); }
/******************************************************************************************************* * * 函数名称: * * 函数功能: * * 入口参数: * * 出口参数: 无 * * 说明: * *******************************************************************************************************/ int16_t DataAreaWRPLock(COMM_FRAME *pcf) { uint8_t rv_comm_buff[20]; uint16_t wrp_lock_param[2]; uint8_t i; uint8_t rt; uint8_t buff[20]; FlagStatus RDP_state; wrp_lock_param[0] = MAKE_HWORD((pcf)->param[2], (pcf)->param[1]); wrp_lock_param[1] = MAKE_HWORD((pcf)->param[4], (pcf)->param[3]); //加锁命令序列 for(i = 0; i < pcf->len - 5; i++) { rv_comm_buff[i] = pcf->param[5 + i]; } //匹配加锁命令序列 rt = MemoryCmp(rv_comm_buff, LockBuff, sizeof(LockBuff)); if(rt) { //如果匹配,进行Flash的读写加锁 FLASH_Unlock(); FLASH_OB_Unlock(); FLASH_ClearFlag(FLASH_FLAG_EOP|FLASH_FLAG_OPERR|FLASH_FLAG_WRPERR| FLASH_FLAG_PGAERR | FLASH_FLAG_PGPERR|FLASH_FLAG_PGSERR); //发送扇区擦除错误 buff[0] = FLASH_WRP_LOCK_CW; buff[1] = 0; SendFrame(UNIVER_ACK_CW, (uint8_t *)buff, 4); //2即2个字节,16bit if(FLASH_OB_GetRDP() != SET) { do { FLASH_OB_RDPConfig(OB_RDP_Level_1);//开启读保护 }while(FLASH_OB_Launch() != FLASH_COMPLETE); } RDP_state = FLASH_OB_GetRDP(); FLASH_OB_Lock(); FLASH_Lock(); } return 1; }
/** * @brief Get the Option byte configuration * @note The parameter Banks of the pOBInit structure must be exclusively FLASH_BANK_1 or FLASH_BANK_2 as this parameter is use to get the given Bank WRP, PCROP and secured area. * @param pOBInit pointer to an FLASH_OBInitStruct structure that * contains the configuration information for the programming. * * @retval None */ void HAL_FLASHEx_OBGetConfig(FLASH_OBProgramInitTypeDef *pOBInit) { /* Check the parameters */ assert_param(IS_FLASH_BANK_EXCLUSIVE(pOBInit->Banks)); pOBInit->OptionType = (OPTIONBYTE_WRP | OPTIONBYTE_RDP | \ OPTIONBYTE_USER | OPTIONBYTE_PCROP | \ OPTIONBYTE_BOR | OPTIONBYTE_BOOTADD | \ OPTIONBYTE_SECURE_AREA); /* Get write protection on the selected area */ FLASH_OB_GetWRP(&(pOBInit->WRPState), &(pOBInit->WRPSector), pOBInit->Banks); /* Get Read protection level */ pOBInit->RDPLevel = FLASH_OB_GetRDP(); /* Get the user option bytes */ pOBInit->USERConfig = FLASH_OB_GetUser(); /* Get the Proprietary code readout protection */ FLASH_OB_GetPCROP(&(pOBInit->PCROPConfig), &(pOBInit->PCROPStartAddr), &(pOBInit->PCROPEndAddr), pOBInit->Banks); /*Get BOR Level*/ pOBInit->BORLevel = FLASH_OB_GetBOR(); /*Get Boot Address*/ FLASH_OB_GetBootAdd(&(pOBInit->BootAddr0), &(pOBInit->BootAddr1)); /*Get Bank Secure area*/ FLASH_OB_GetSecureArea(&(pOBInit->SecureAreaConfig), &(pOBInit->SecureAreaStartAddr), &(pOBInit->SecureAreaEndAddr), pOBInit->Banks); }
/** * @brief Get the Option byte configuration * @param pOBInit pointer to an FLASH_OBInitStruct structure that * contains the configuration information for the programming. * * @retval None */ void HAL_FLASHEx_OBGetConfig(FLASH_OBProgramInitTypeDef *pOBInit) { pOBInit->OptionType = OPTIONBYTE_WRP | OPTIONBYTE_RDP | OPTIONBYTE_USER; /*Get WRP*/ pOBInit->WRPPage = FLASH_OB_GetWRP(); /*Get RDP Level*/ pOBInit->RDPLevel = FLASH_OB_GetRDP(); /*Get USER*/ pOBInit->USERConfig = FLASH_OB_GetUser(); }
/** * @brief Get the Option byte configuration * @param pOBInit: pointer to an FLASH_OBInitStruct structure that * contains the configuration information for the programming. * * @retval None */ void HAL_FLASHEx_OBGetConfig(FLASH_OBProgramInitTypeDef *pOBInit) { pOBInit->OptionType = OPTIONBYTE_WRP | OPTIONBYTE_RDP | OPTIONBYTE_USER | OPTIONBYTE_BOR; /*Get WRP*/ pOBInit->WRPSector = (uint32_t)FLASH_OB_GetWRP(); /*Get RDP Level*/ pOBInit->RDPLevel = (uint32_t)FLASH_OB_GetRDP(); /*Get USER*/ pOBInit->USERConfig = (uint8_t)FLASH_OB_GetUser(); /*Get BOR Level*/ pOBInit->BORLevel = (uint32_t)FLASH_OB_GetBOR(); }
void FlashWRP(void) { uint32_t rt; FlagStatus RDP_state; FLASH_Unlock(); FLASH_OB_Unlock(); FLASH_ClearFlag(FLASH_FLAG_EOP|FLASH_FLAG_OPERR|FLASH_FLAG_WRPERR| FLASH_FLAG_PGAERR | FLASH_FLAG_PGPERR|FLASH_FLAG_PGSERR); RDP_state = FLASH_OB_GetRDP(); rt = FLASH_OB_GetWRP(); do { FLASH_OB_WRPConfig(WRP_Sector_All, ENABLE); //开启写保护 }while(FLASH_OB_Launch() != FLASH_COMPLETE); rt = FLASH_OB_GetWRP(); FLASH_OB_Lock(); FLASH_Lock(); }
HAL_StatusTypeDef HAL_FLASHEx_OBErase(void) { uint8_t rdptmp = OB_RDP_LEVEL_0; HAL_StatusTypeDef status = HAL_ERROR; /* Get the actual read protection Option Byte value */ rdptmp = FLASH_OB_GetRDP(); /* Wait for last operation to be completed */ status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE); if(status == HAL_OK) { /* Clean the error context */ pFlash.ErrorCode = HAL_FLASH_ERROR_NONE; /* If the previous operation is completed, proceed to erase the option bytes */ SET_BIT(FLASH->CR, FLASH_CR_OPTER); SET_BIT(FLASH->CR, FLASH_CR_STRT); /* Wait for last operation to be completed */ status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE); /* If the erase operation is completed, disable the OPTER Bit */ CLEAR_BIT(FLASH->CR, FLASH_CR_OPTER); if(status == HAL_OK) { /* Restore the last read protection Option Byte value */ status = FLASH_OB_RDP_LevelConfig(rdptmp); } } /* Return the erase status */ return status; }
/******************************************************************************************************* * * 函数名称: * * 函数功能: * * 入口参数: * * 出口参数: 无 * * 说明: * *******************************************************************************************************/ int16_t DataAreaWRPUnlock(COMM_FRAME *pcf) { uint32_t wrp_state; uint8_t rv_comm_buff[20]; uint16_t wrp_unlock_param[2]; uint8_t i; uint8_t rt; uint8_t buff[20]; FlagStatus RDP_state; wrp_unlock_param[0] = MAKE_HWORD((pcf)->param[2], (pcf)->param[1]); wrp_unlock_param[1] = MAKE_HWORD((pcf)->param[4], (pcf)->param[3]); //加锁命令序列 for(i = 0; i < pcf->len - 5; i++) { rv_comm_buff[i] = pcf->param[5 + i]; } //匹配加锁命令序列 rt = MemoryCmp(rv_comm_buff, UnlockBuff, sizeof(UnlockBuff)); if(rt) { //如果匹配,进行Flash的读写解锁 FLASH_Unlock(); FLASH_OB_Unlock(); FLASH_ClearFlag(FLASH_FLAG_EOP|FLASH_FLAG_OPERR|FLASH_FLAG_WRPERR| FLASH_FLAG_PGAERR | FLASH_FLAG_PGPERR|FLASH_FLAG_PGSERR); wrp_state = FLASH_OB_GetWRP(); if(!wrp_state) { do { FLASH_OB_WRPConfig(WRP_Sector_All, DISABLE); //关闭写保护 }while(FLASH_OB_Launch() != FLASH_COMPLETE); buff[1] = 0; } else { buff[1] = 1; } //发送扇区擦除错误 buff[0] = FLASH_WRP_UNLOCK_CW; SendFrame(UNIVER_ACK_CW, (uint8_t *)buff, 4); //2即2个字节,16bit __disable_irq(); if(FLASH_OB_GetRDP() == SET) { do { FLASH_OB_RDPConfig(OB_RDP_Level_0);//关闭读保护 }while(FLASH_OB_Launch() != FLASH_COMPLETE); } else { buff[0] = FLASH_WRP_UNLOCK_CW; buff[1] = 1; SendFrame(UNIVER_ACK_CW, (uint8_t *)buff, 4); //2即2个字节,16bit } //解锁之后Flash全部擦除,RAM调试会执行到下面的程序 __enable_irq(); RDP_state = FLASH_OB_GetRDP(); FLASH_OB_Lock(); FLASH_Lock(); } return 1; }