/** * @brief Erases PAGE and PAGE1 and writes VALID_PAGE header to PAGE * @param None * @retval Status of the last operation (Flash write or erase) done during * EEPROM formating */ static FLASH_Status EE_Format(void) { FLASH_Status FlashStatus = FLASH_COMPLETE; /* Erase Page0 */ FlashStatus = FLASH_EraseSector(PAGE0_ID, FLASH_VOLTAGE_RANGE ); /* If erase operation was failed, a Flash error code is returned */ if (FlashStatus != FLASH_COMPLETE) { return FlashStatus; } /* Set Page0 as valid page: Write VALID_PAGE at Page0 base address */ FlashStatus = FLASH_ProgramHalfWord(PAGE0_BASE_ADDRESS, VALID_PAGE ); /* If program operation was failed, a Flash error code is returned */ if (FlashStatus != FLASH_COMPLETE) { return FlashStatus; } /* Erase Page1 */ FlashStatus = FLASH_EraseSector(PAGE1_ID, FLASH_VOLTAGE_RANGE ); /* Return Page1 erase operation status */ return FlashStatus; }
FLASH_Status eraseHelper(uint32_t pageAddress) { #if defined(SERIES_STM32F37x) || defined(SERIES_STM32F30x) return FLASH_ErasePage(pageAddress); #else if (pageAddress == PAGE0_BASE_ADDRESS) return FLASH_EraseSector(PAGE0_ID, VOLTAGE_RANGE); else if (pageAddress == PAGE1_BASE_ADDRESS) return FLASH_EraseSector(PAGE1_ID, VOLTAGE_RANGE); else return FLASH_ERROR_PROGRAM; #endif }
u8 bsp_Firmware_Erase(u8 APP_Select) { u8 status=0; FLASH_Unlock(); //解锁 FLASH_DataCacheCmd(DISABLE);//FLASH擦除期间,必须禁止数据缓存 FLASH_ClearFlag(FLASH_FLAG_EOP|FLASH_FLAG_OPERR|FLASH_FLAG_WRPERR| FLASH_FLAG_PGAERR | FLASH_FLAG_PGPERR|FLASH_FLAG_PGSERR); if(APP_Select) status=FLASH_EraseSector(FLASH_Sector_6 ,VoltageRange_3);//VCC=2.7~3.6V之间!! else status=FLASH_EraseSector(FLASH_Sector_5 ,VoltageRange_3);//VCC=2.7~3.6V之间!! MCU_WriteStatus=status; return MCU_WriteStatus; }
void Flash_EraseDataBuffer() { FLASH_Unlock(); FLASH_ClearFlag( FLASH_FLAG_EOP | FLASH_FLAG_WRPERR | FLASH_FLAG_PGAERR | FLASH_FLAG_PGPERR | FLASH_FLAG_PGSERR); // Erase Sectors // 8-10 is data logger FLASH_EraseSector(FLASH_Sector_8, VoltageRange_3); FLASH_EraseSector(FLASH_Sector_9, VoltageRange_3); FLASH_EraseSector(FLASH_Sector_10, VoltageRange_3); Flash_ResetFlashAddress(); FLASH_Lock(); }
/** * @brief Main program * @param None * @retval None */ ErrorStatus eep_erase( void ) { ErrorStatus Err = SUCCESS; /* Enable the flash control register access */ FLASH_Unlock(); /* Erase the user Flash area ************************************************/ /* area defined by FLASH_USER_START_ADDR and FLASH_USER_END_ADDR */ /* Clear pending flags (if any) */ FLASH_ClearFlag(FLASH_FLAG_EOP | FLASH_FLAG_OPERR | FLASH_FLAG_WRPERR | FLASH_FLAG_PGAERR | FLASH_FLAG_PGPERR|FLASH_FLAG_PGSERR); /* Get the number of the start and end sectors */ uwStartSector = GetSector(FLASH_USER_START_ADDR); /* Device voltage range supposed to be [2.7V to 3.6V], the operation will be done by word */ if (FLASH_EraseSector(uwStartSector, VoltageRange_3) != FLASH_COMPLETE) { /* Error occurred while sector erase. User can add here some code to deal with this error */ Err = ERROR; } FLASH_Lock(); return Err; }
static bool restoreCalibrateParamToFlash ( CalibrateParam* p ) { int i ; int n = sizeof(*p) / sizeof(uint32_t) ; uint32_t Address = CALIBRATE_PARAM_ADDR ; uint32_t *d = (uint32_t *) p ; FLASH_Unlock(); FLASH_EraseSector(FLASH_Sector_5,VoltageRange_3); for ( i=0;i<n;i++){ FLASH_ProgramWord( Address, *d ); d ++ ; Address += 4; } FLASH_Lock(); // 检验是否正确写入 bool ret = true ; d = (uint32_t *) p ; uint32_t *d2 = (uint32_t*)CALIBRATE_PARAM_ADDR ; for ( i=0; i<n; i++ ) { if ( d[i] != d2[i] ) { ret = false ; break ; } } return ret ; }
bool write(uint32_t addr, uint16_t * data, const uint16_t data_len) { int sector; /* Check bounds */ if ((addr >= (SECTOR11_START_ADDR + SECTOR_SIZE_128KB)) || (addr < SECTOR0_START_ADDR)) return false; /* Need to compute sector to erase */ if (addr >= SECTOR5_START_ADDR) sector = 5 + ((addr - SECTOR5_START_ADDR)/SECTOR_SIZE_128KB); else if (addr >= SECTOR4_START_ADDR) sector = 4; else sector = (addr - SECTOR0_START_ADDR)/SECTOR_SIZE_16KB; /* Unlock flash */ FLASH_Unlock(); FLASH_EraseSector(sector * 0x08, VoltageRange_3); /* Write data */ for (int i = 0; i < data_len; i += 2) { FLASH_ProgramHalfWord(addr, *data); data++; addr += 2; } return true; }
int stm32_flash_erase(uint32_t offset, uint16_t len) { uint32_t StartSector, EndSector; uint32_t flash_user_start_addr, flash_user_end_addr; uint32_t addr; uint8_t i; /* Get the number of the start and end sectors */ flash_user_start_addr = ADDR_FLASH_SECTOR_0 + offset; flash_user_end_addr = flash_user_start_addr + len; StartSector = GetSector(flash_user_start_addr); EndSector = GetSector(flash_user_end_addr); /* Erase sector */ for (i = StartSector; i <= EndSector; i += 8) { /* Device voltage range supposed to be [2.7V to 3.6V], the operation will be done by word */ if (FLASH_EraseSector(i, VoltageRange_3) != FLASH_COMPLETE) { return -1; } } return 0; }
/** * @brief ½«²ÎÊýдÈëFLASHÖÐ * * @param b:ÊÇ·ñÓÐLEDÌáʾ * @retval none */ void Flash_WriteParams(uint8_t b) { FLASH_Status status; uint32_t i; FLASH_Unlock(); FLASH_ClearFlag(FLASH_FLAG_EOP | FLASH_FLAG_PGPERR | FLASH_FLAG_WRPERR); // //if (FLASH_ErasePage(FLASH_WRITE_ADDR) == FLASH_COMPLETE) if(FLASH_EraseSector(FLASH_WRITE_SECTOR, VoltageRange_3) == FLASH_COMPLETE) { for (i = 0; i < sizeof(config_t); i += 4) { status = FLASH_ProgramWord(FLASH_WRITE_ADDR + i, *(uint32_t *) ((char *) &cfg + i)); if (status != FLASH_COMPLETE) { System_FailureMode(1); break; // TODO: fail } } } else System_FailureMode(1); FLASH_Lock(); Flash_Read(); }
const DC3Error_t FLASH_eraseSector( const uint32_t sectorAddr ) { DC3Error_t status = ERR_NONE; /* These flags have to be cleared before any operation can be done on the * flash memory */ // DBG_printf("Clearing flash flags. Status before clear: %x\n",FLASH->SR ); FLASH_ClearFlag( FLASH_FLAG_PGSERR | FLASH_FLAG_PGPERR | FLASH_FLAG_PGAERR | FLASH_FLAG_WRPERR | FLASH_FLAG_OPERR | FLASH_FLAG_EOP ); // DBG_printf("Status after clear: %x\n",FLASH->SR ); WRN_printf("*** Erasing Flash sector addr 0x%08x ***\n", sectorAddr); /*Look up the STM32 value of the sector given the sector address */ uint16_t sector = FLASH_sectorAddrToFlashSect( sectorAddr ); DBG_printf("Erasing sector (0x%04x) with base addr: 0x%08x...\n", sector, sectorAddr); /* This is a blocking operation but it's only for a single sector so should * not take that long */ FLASH_Status flash_status = FLASH_EraseSector(sector, VoltageRange_3); status = FLASH_statusToErrorCode( flash_status ); if (ERR_NONE != status) { ERR_printf("Flash error %d ( error 0x%08x) while erasing sector " "0x%04x (base addr: 0x%08x)\n", flash_status, status, sector, sectorAddr); DBG_printf("FLASH SR:%lx\n", FLASH->SR); } return( status ); }
uint16_t flash_helper_erase_new_app(uint32_t new_app_size) { FLASH_Unlock(); FLASH_ClearFlag(FLASH_FLAG_OPERR | FLASH_FLAG_WRPERR | FLASH_FLAG_PGAERR | FLASH_FLAG_PGPERR | FLASH_FLAG_PGSERR); new_app_size += flash_addr[NEW_APP_BASE]; mcpwm_unlock(); mcpwm_release_motor(); utils_sys_lock_cnt(); RCC_APB1PeriphClockCmd(RCC_APB1Periph_WWDG, DISABLE); for (int i = 0;i < NEW_APP_SECTORS;i++) { if (new_app_size > flash_addr[NEW_APP_BASE + i]) { uint16_t res = FLASH_EraseSector(flash_sector[NEW_APP_BASE + i], VoltageRange_3); if (res != FLASH_COMPLETE) { return res; } } else { break; } } RCC_APB1PeriphClockCmd(RCC_APB1Periph_WWDG, ENABLE); utils_sys_unlock_cnt(); return FLASH_COMPLETE; }
bool flash_write_word(uint32_t add, uint32_t value) { /* Unlock the Flash to enable the flash control register access *************/ FLASH_Unlock(); /* Clear pending flags (if any) */ FLASH_ClearFlag(FLASH_FLAG_EOP | FLASH_FLAG_OPERR | FLASH_FLAG_WRPERR | FLASH_FLAG_PGAERR | FLASH_FLAG_PGPERR|FLASH_FLAG_PGSERR); if (FLASH_EraseSector(FLASH_Sector_11, VoltageRange_4) != FLASH_COMPLETE) { goto fop_error; } //Program the user Flash area word by word if (FLASH_ProgramWord(add, value) != FLASH_COMPLETE) { goto fop_error; } FLASH_Lock(); return true; fop_error: FLASH_Lock(); return false; }
/******************************************************************************* * Function Name : FLASH_If_Erase * Description : Erase sector * Input : None * Output : None * Return : None *******************************************************************************/ uint16_t FLASH_If_Erase(uint32_t Add) { /* Unlock the internal flash */ FLASH_Unlock(); FLASH_ClearFlags(); #if defined (STM32F2XX) || defined (STM32F4XX) /* Disable sector write protection if it's already protected */ uint16_t OB_WRP_Sector = FLASH_SectorToWriteProtect(FLASH_INTERNAL, Add); FLASH_WriteProtection_Disable(OB_WRP_Sector); /* Check which sector has to be erased */ uint16_t FLASH_Sector = FLASH_SectorToErase(FLASH_INTERNAL, Add); FLASH_Status status = FLASH_EraseSector(FLASH_Sector, VoltageRange_3); #elif defined(STM32F10X_CL) /* Call the standard Flash erase function */ FLASH_ErasePage(Add); #endif /* STM32F2XX */ /* Lock the internal flash */ FLASH_Lock(); if (status != FLASH_COMPLETE) return MAL_FAIL; return MAL_OK; }
uint16_t flash_helper_erase_new_app(uint32_t new_app_size) { FLASH_Unlock(); FLASH_ClearFlag(FLASH_FLAG_OPERR | FLASH_FLAG_WRPERR | FLASH_FLAG_PGAERR | FLASH_FLAG_PGPERR | FLASH_FLAG_PGSERR); new_app_size += flash_addr[NEW_APP_BASE]; mc_interface_unlock(); mc_interface_release_motor(); utils_sys_lock_cnt(); timeout_configure_IWDT_slowest(); for (int i = 0;i < NEW_APP_SECTORS;i++) { if (new_app_size > flash_addr[NEW_APP_BASE + i]) { uint16_t res = FLASH_EraseSector(flash_sector[NEW_APP_BASE + i], VoltageRange_3); if (res != FLASH_COMPLETE) { FLASH_Lock(); return res; } } else { break; } } FLASH_Lock(); timeout_configure_IWDT(); utils_sys_unlock_cnt(); return FLASH_COMPLETE; }
void writeFlash(u8 address_num,u32 data) //address_num should start from 0 up to 255 { const u8 data_count=255; u32 flash_data[data_count]; for (u8 i = 0; i < data_count; ++i) { flash_data[i] = readFlash(i); } flash_data[address_num] = data; FLASH_Unlock();// you need to unlcok flash first /* Clear All pending flags */ FLASH_ClearFlag( FLASH_FLAG_EOP | FLASH_FLAG_WRPERR | FLASH_FLAG_PGAERR | FLASH_FLAG_PGPERR | FLASH_FLAG_PGSERR); //you need to erase entire sector before write anything FLASH_EraseSector(FLASH_Sector_11, VoltageRange_3); //VoltageRange_3 ((uint8_t)0x02) /*!<Device operating range: 2.7V to 3.6V */ for (u8 i = 0; i < data_count; ++i) { //write back the whole sector memory FLASH_ProgramWord((u32)(startAddress + i * 4), flash_data[i]); } FLASH_Lock();//lock flash at the end }
OSStatus internalFlashErase(uint32_t StartAddress, uint32_t EndAddress) { platform_log_trace(); OSStatus err = kNoErr; uint32_t StartSector, EndSector, i = 0, j = 0; /* Get the sector where start the user flash area */ StartSector = _GetSector(StartAddress); EndSector = _GetSector(EndAddress); for(i = StartSector; i <= EndSector; i += 8) { /* Device voltage range supposed to be [2.7V to 3.6V], the operation will be done by word */ _GetAddress(i, &StartAddress, &EndAddress); for(j=StartAddress; j<=EndAddress; j+=8){ if( (*(uint32_t *)(j))!=0xFFFFFFFF ) break; } if( j>EndAddress ) continue; require_action(FLASH_EraseSector(i, VoltageRange_3) == FLASH_COMPLETE, exit, err = kWriteErr); } exit: return err; }
int flash_erase(const uint32_t addr, const int32_t len) { int start_sector, end_sector; /* Check bounds */ if (addr < SECTOR0_START_ADDR) return -1; /* Need to compute sector to erase */ start_sector = get_sector_num(addr); if (len == -1) end_sector = 11; else end_sector = get_sector_num(addr+(len*BYTES_PER_WORD)-1); FLASH_Unlock(); for (int i = start_sector; i <= end_sector; ++i) { FLASH_ClearFlag(FLASH_FLAG_EOP | FLASH_FLAG_OPERR | FLASH_FLAG_WRPERR | FLASH_FLAG_PGAERR | FLASH_FLAG_PGPERR|FLASH_FLAG_PGSERR); FLASH_EraseSector(i * 0x08, VoltageRange_3); } FLASH_Lock(); return 0; }
//void SD_TEST(void) //{ // u8 res; // UINT bw,index; // res=f_mount( &fatfs_SDCARD,"0:/",1 ); // res=f_open(&file_sdif, "0:/sdif.dat",FA_OPEN_EXISTING|FA_WRITE|FA_READ); // res=f_write(&file_sdif,SDTEST_WriteBuffer,512,&bw); // res=f_close(&file_sdif); // res=f_open(&file_sdif, "0:/sdif.dat",FA_OPEN_EXISTING |FA_WRITE); // res=f_write(&file_sdif,SDTEST_WriteBuffer,512,&bw); // res=f_close(&file_sdif); // res=f_open(&file_sdif, "0:/sdif.dat",FA_OPEN_EXISTING | FA_READ); // index=0; // while(SDTEST_ReadBuffer[31]!=0x04) // { // index++; // res=f_read(&file_sdif,SDTEST_ReadBuffer,512,&bw); // } // f_close(&file_sdif); // // //} u8 bsp_ChangeAPPtoRun(u8 App_Select) { u8 status=0; u8 Flash_status=0; FLASH_Unlock(); //解锁 FLASH_DataCacheCmd(DISABLE);//FLASH擦除期间,必须禁止数据缓存 FLASH_ClearFlag(FLASH_FLAG_EOP|FLASH_FLAG_OPERR|FLASH_FLAG_WRPERR| FLASH_FLAG_PGAERR | FLASH_FLAG_PGPERR|FLASH_FLAG_PGSERR); status=FLASH_EraseSector(FLASH_Sector_3 ,VoltageRange_3);//VCC=2.7~3.6V之间!! if(status==FLASH_COMPLETE) { __set_PRIMASK(1); status=FLASH_ProgramByte(App_ToRun_Flag,App_Select); //SysID __set_PRIMASK(0); } FLASH_DataCacheCmd(ENABLE); //FLASH擦除结束,开启数据缓存 FLASH_Lock();//上锁 if(status==FLASH_COMPLETE) { Flash_status=0; } else { Flash_status=status; } return Flash_status; }
uint16_t EE_WriteBuf(uint16_t EEAddress, uint16_t* _data, uint16_t len) { uint16_t * rData, * fData; uint32_t adr; uint32_t ret; uint32_t i; volatile FLASH_Status FLASHStatus = FLASH_COMPLETE; ret = 0; // check data rData = (uint16_t *)_data; fData = (uint16_t *)(EEAddress + EEPROM_START_ADDRESS); adr = EEAddress + EEPROM_START_ADDRESS; for (i = 0; i < len; i++) { if (rData[i] != fData[i] ) { ret = 1; if (fData[i] != 0xFFFF) { ret = 2; i = len; } } } if (ret) { FLASH_Unlock(); FLASH_ClearFlag(FLASH_FLAG_EOP | FLASH_FLAG_OPERR | FLASH_FLAG_WRPERR | FLASH_FLAG_PGAERR | FLASH_FLAG_PGPERR|FLASH_FLAG_PGSERR); // Clear Page if (ret == 2) FLASHStatus = FLASH_EraseSector(PAGE0_ID, VOLTAGE_RANGE); ret = 0; if (FLASHStatus == FLASH_COMPLETE) { for (i = 0; i < len; i++) { FLASHStatus = FLASH_ProgramHalfWord(adr, rData[i]); adr += 2; } // check flash write rData = (uint16_t *)_data; fData = (uint16_t *)(EEAddress + EEPROM_START_ADDRESS); for (i = 0; i < len; i++) { if (rData[i] != fData[i] ) ret = 1; } } else { ret = 2; } FLASH_Lock(); } return ret; }
void bsp_SysinfoSave(void) { u8 status=0; u16 index=0; SleepTime=0; memcpy(MCU_ROM_Write,(u8*)ADDR_FLASH_SECTOR_7,1024+16); //?????,???????????buffer memcpy(MCU_ROM_Write+1024,&char_SysID_Length,1); memcpy(MCU_ROM_Write+1024+1,char_SysID,char_SysID_Length); memcpy(MCU_ROM_Write+1024+9,&Battery_Threshold,1); memcpy(MCU_ROM_Write+1024+10,&Key_Sound,1); FLASH_Unlock(); //?? FLASH_DataCacheCmd(DISABLE);//FLASH????,???????? FLASH_ClearFlag(FLASH_FLAG_EOP|FLASH_FLAG_OPERR|FLASH_FLAG_WRPERR| FLASH_FLAG_PGAERR | FLASH_FLAG_PGPERR|FLASH_FLAG_PGSERR); status=FLASH_EraseSector(FLASH_Sector_7 ,VoltageRange_3);//VCC=2.7~3.6V??!! if(status==FLASH_COMPLETE) { for(index=0;index<(1024+16);index++) { status=FLASH_ProgramByte(ADDR_FLASH_SECTOR_7+index,MCU_ROM_Write[index]); //SysID } } FLASH_DataCacheCmd(ENABLE); //FLASH????,?????? FLASH_Lock();//?? if(status==FLASH_COMPLETE) { MCU_WriteStatus=0; } else { MCU_WriteStatus=status; } // FLASH_Unlock(); //?? // FLASH_DataCacheCmd(DISABLE);//FLASH????,???????? // FLASH_ClearFlag(FLASH_FLAG_EOP|FLASH_FLAG_OPERR|FLASH_FLAG_WRPERR| FLASH_FLAG_PGAERR | FLASH_FLAG_PGPERR|FLASH_FLAG_PGSERR); // status=FLASH_EraseSector(FLASH_Sector_7 ,VoltageRange_3);//VCC=2.7~3.6V??!! // if(status==FLASH_COMPLETE) // { // status=FLASH_ProgramByte(ADDR_FLASH_SECTOR_7+1024,char_SysID_Length);//???? // for(index=0;index<char_SysID_Length;index++) // { // status=FLASH_ProgramByte(ADDR_FLASH_SECTOR_7+1024+index+1,char_SysID[index]); //SysID // } // status=FLASH_ProgramByte(ADDR_FLASH_SECTOR_7+1024+9,Battery_Threshold); // status=FLASH_ProgramByte(ADDR_FLASH_SECTOR_7+1024+10,Key_Sound); // // } // FLASH_DataCacheCmd(ENABLE); //FLASH????,?????? // FLASH_Lock();//?? }
FLASH_Status flash_open_erase_sector(uint32_t address){ uint8_t i; for (i = 0; i < 12; ++i) { if (address == kSectorBaseAddress[i]) { FLASH_EraseSector(i * 8, VoltageRange_3); } } }
//Store the computed offsets to internal FLASH void store_offsets() { union { float val; struct { uint8_t bytes[4]; }; }extract_float; uint32_t ADDRESS = BACKUP_ADDRESS; uint8_t i,j; IWDG_ReloadCounter(); FLASH_Unlock(); //unlock FLASH to enable writing FLASH_ClearFlag(FLASH_FLAG_EOP | FLASH_FLAG_OPERR | FLASH_FLAG_WRPERR | FLASH_FLAG_PGAERR | FLASH_FLAG_PGPERR|FLASH_FLAG_PGSERR); //clear any pending flags FLASH_EraseSector(BACKUP_SECTOR,VoltageRange_3); //Erase the sector where data is to be written too FLASH_ProgramHalfWord(ADDRESS, ASI_ZERO); ADDRESS+=2; for(j=0;j<6;j++) { extract_float.val = AN_OFFSET[j]; for(i=0;i<4;i++) { FLASH_ProgramByte(ADDRESS, extract_float.bytes[i]); ADDRESS+=1; } } for(i=0;i<3;i++) { FLASH_ProgramHalfWord(ADDRESS, MAG_OFFSET[i]); ADDRESS+=2; } for(j=0;j<3;j++) { extract_float.val = MAG_SCALE[j]; for(i=0;i<4;i++) { FLASH_ProgramByte(ADDRESS, extract_float.bytes[i]); ADDRESS+=1; } } FLASH_ProgramHalfWord(ADDRESS, ground_alt_offset); ADDRESS+=2; for(i=0;i<8;i++) { FLASH_ProgramHalfWord(ADDRESS, RC_trim[i]); ADDRESS+=2; } FLASH_Lock(); }
void writeEEPROM(uint8_t b, uint8_t updateProfile) { FLASH_Status status; uint32_t i; uint8_t chk = 0; const uint8_t *p; int tries = 0; // prepare checksum/version constants mcfg.version = EEPROM_CONF_VERSION; mcfg.size = sizeof(master_t); mcfg.magic_be = 0xBE; mcfg.magic_ef = 0xEF; mcfg.chk = 0; // when updateProfile = true, we copy contents of cfg to global configuration. when false, only profile number is updated, and then that profile is loaded on readEEPROM() if (updateProfile) { // copy current in-memory profile to stored configuration memcpy(&mcfg.profile[mcfg.current_profile], &cfg, sizeof(config_t)); } // recalculate checksum before writing for (p = (const uint8_t *)&mcfg; p < ((const uint8_t *)&mcfg + sizeof(master_t)); p++) chk ^= *p; mcfg.chk = chk; // write it retry: FLASH_Unlock(); FLASH_ClearFlag(FLASH_FLAG_EOP | FLASH_FLAG_OPERR | FLASH_FLAG_WRPERR | FLASH_FLAG_PGAERR | FLASH_FLAG_PGPERR|FLASH_FLAG_PGSERR); if (FLASH_EraseSector(FLASH_Sector_11, VoltageRange_3) == FLASH_COMPLETE) { for (i = 0; i < sizeof(master_t); i += 4) { status = FLASH_ProgramWord(FLASH_WRITE_ADDR + i, *(uint32_t *) ((char *)&mcfg + i)); if (status != FLASH_COMPLETE) { FLASH_Lock(); tries++; if (tries < 3) goto retry; else break; } } } FLASH_Lock(); // Flash write failed - just die now if (tries == 3 || !validEEPROM()) { failureMode(10); } // re-read written data readEEPROM(); if (b) blinkLED(15, 20, 1); }
static uint32_t flash_erase(uint32_t addr) { if(FLASH_EraseSector(addr) == FLASH_OK) { return BL_FLASH_OK; } else { return BL_FLASH_ERR; } }
sys_res arch_IntfErase(adr_t nAdr) { FLASH_Status res = FLASH_COMPLETE; stm32_intf_Lock(); res = FLASH_EraseSector(arch_flashGetSector(nAdr), VoltageRange_3); stm32_intf_Unlock(); if (res == FLASH_COMPLETE) return SYS_R_OK; return SYS_R_TMO; }
/* * Erase flash page if it is not already erased. This is to save write cycles and * prevent the memory from getting erased in case of unstable voltage at boot. */ static uint16_t EE_EraseSectorIfNotEmpty(uint32_t FLASH_Sector, uint8_t VoltageRange) { uint8_t *addr = flash_helper_get_sector_address(FLASH_Sector); for (unsigned int i = 0;i < PAGE_SIZE;i++) { if (addr[i] != 0xFF) { return FLASH_EraseSector(FLASH_Sector, VoltageRange); } } return FLASH_COMPLETE; }
/*=====================================================================================================*/ int8_t Flash_EraseSector( uint32_t eraseSector ) { int8_t status = ERROR; FLASH_Unlock(); FLASH_ClearFlag(FLASH_FLAG_EOP | FLASH_FLAG_OPERR | FLASH_FLAG_WRPERR | FLASH_FLAG_PGAERR | FLASH_FLAG_PGPERR | FLASH_FLAG_PGSERR); status = FLASH_EraseSector(eraseSector, VoltageRange_3); FLASH_Lock(); return status; }
/* ********************************************************************************************************* * 函 数 名: bsp_WriteCpuFlash * 功能说明: 写数据到CPU 内部Flash。 * 形 参: _ulFlashAddr : Flash地址 * _ucpSrc : 数据缓冲区 * _ulSize : 数据大小(单位是字节) * 返 回 值: 0-成功,1-数据长度或地址溢出,2-写Flash出错(估计Flash寿命到) ********************************************************************************************************* */ uint8_t bsp_WriteCpuFlash(uint32_t _ulFlashAddr, uint8_t *_ucpSrc, uint32_t _ulSize) { uint32_t i; uint8_t ucRet; /* 如果偏移地址超过芯片容量,则不改写输出缓冲区 */ if (_ulFlashAddr + _ulSize > FLASH_BASE_ADDR + FLASH_SIZE) { return 1; } /* 长度为0时不继续操作 */ if (_ulSize == 0) { return 0; } ucRet = bsp_CmpCpuFlash(_ulFlashAddr, _ucpSrc, _ulSize); if (ucRet == FLASH_IS_EQU) { return 0; } __set_PRIMASK(1); /* 关中断 */ /* FLASH 解锁 */ FLASH_Unlock(); /* Clear pending flags (if any) */ FLASH_ClearFlag(FLASH_FLAG_EOP | FLASH_FLAG_OPERR | FLASH_FLAG_WRPERR | FLASH_FLAG_PGAERR | FLASH_FLAG_PGPERR|FLASH_FLAG_PGSERR); /* 需要擦除 */ if (ucRet == FLASH_REQ_ERASE) { FLASH_EraseSector(bsp_GetSector(_ulFlashAddr), VoltageRange_3); } /* 按字节模式编程(为提高效率,可以按字编程,一次写入4字节) */ for (i = 0; i < _ulSize; i++) { FLASH_ProgramByte(_ulFlashAddr++, *_ucpSrc++); } /* Flash 加锁,禁止写Flash控制寄存器 */ FLASH_Lock(); __set_PRIMASK(0); /* 开中断 */ return 0; }
/*=====================================================================================================*/ void Flash_ErasePage(u32 ErasePage) { FLASH_Status FLASHStatus; FLASH_Unlock(); FLASH_ClearFlag(FLASH_FLAG_EOP | FLASH_FLAG_OPERR | FLASH_FLAG_WRPERR | FLASH_FLAG_PGAERR | FLASH_FLAG_PGPERR | FLASH_FLAG_PGSERR); FLASHStatus = FLASH_EraseSector(ErasePage, VoltageRange_3); while (FLASHStatus != FLASH_COMPLETE); FLASH_Lock(); }
FLASH_Status flash_erase_sector(uint32_t address){ uint8_t i; FLASH_Unlock(); FLASH_ClearFlag(FLASH_FLAG_EOP | FLASH_FLAG_OPERR | FLASH_FLAG_WRPERR | FLASH_FLAG_PGAERR | FLASH_FLAG_PGPERR|FLASH_FLAG_PGSERR); for (i = 0; i < 12; ++i) { if (address == kSectorBaseAddress[i]) { FLASH_EraseSector(i * 8, VoltageRange_3); } } FLASH_Lock(); }