我想做个stm32l151在线升级的程序,用的是STM32CubeMX生成的库函数,在进入主程序之前,我进行的FLASH的擦除,烧写,都正常,但是进入主循环后,在收到串口发送的数据后,再对其进行FLASH的擦除,烧写,就不正常了,首先是擦除,它会返回错误,我再读取数据,发现没有檫掉,强行写,也不对。 void EraseFlash(void) { static FLASH_EraseInitTypeDef EraseInitStruct; uint32_t SectorError[1]; HAL_FLASH_Unlock(); EraseInitStruct.TypeErase = FLASH_TYPEERASE_PAGES; EraseInitStruct .PageAddress =APPSAVE_FLASH_ADR ; HAL_Delay(5); EraseInitStruct.NbPages = 2 ; if(HAL_FLASHEx_Erase(&EraseInitStruct, &SectorError[0])!= HAL_OK) { //while(1); } HAL_FLASH_Lock(); Flash_EEPROM_Read(APPSAVE_FLASH_ADR, &temp[0], 500); } oid WrtieFlash(uint32_t startAddress, uint32_t WordCnt, uint32_t *Data) { uint32_t SectorError[1],i = 0,DataValue, endAddress; HAL_FLASH_Unlock(); endAddress = startAddress + 4*WordCnt; DataValue = Data[i++]; while (startAddress < endAddress) { if (HAL_FLASH_Program(TYPEPROGRAM_WORD, startAddress, DataValue) == HAL_OK) { startAddress = startAddress + 4; DataValue = Data[i++]; } } HAL_FLASH_Lock(); } 请帮我分析下,谢谢大侠们 |
WrtieFlash(APPSAVE_FLASH_ADR, 130, &flash_buffer[0]);
Flash_EEPROM_Read(APPSAVE_FLASH_ADR, &temp[0], 500);
这是我在进入主循环前的操作,是正常的,
if(Varialbe.FlashDataLen > 0)
{
for(i = 0; i <(Varialbe.FlashDataLen/4); i++)
{
flash_buffer[i] = (uint32_t)Varialbe.FlashBuffer[i+0] + (uint32_t)(Varialbe.FlashBuffer[i+1]<<8) + (uint32_t)(Varialbe.FlashBuffer[i+2]<<16) + (uint32_t)(Varialbe.FlashBuffer[i+3]<<24);
}
EraseFlash();
WrtieFlash(APPSAVE_FLASH_ADR, i, &flash_buffer[0]);
Varialbe.FlashDataLen = 0;
}
上面的是进入主循环后执行的,当收到串口的数据后,再对其进行擦除,写,就不对了,
2 调试口复位线接了没?
评分
查看全部评分
里已经选了啊,
2。调试口复位线没接,只用了4根线,VCC,SWCLK, SWDIO, GND
是可以正常的仿真和烧写程序,但是就像我上面说的那样,在主循环里不能正常的擦除,烧写FLASH,很郁闷
你好,我的也是擦除就失败。错误代码是FLASH_ERROR_PROGRAM,以下是我擦除的函数,麻烦帮我看看是什么问题。擦除的地址是0x0801FC00;用的是4.0的库STM32_USB-FS-Device_Lib_V4.0.0
uint8_t flashErasePage(uint32_t addr) {
uint8_t status = 1;
if (addr < FLASH_BASE || (addr >= (FLASH_BASE + 1024 * FLASH_SIZE))) return 1; //非法地址
//确保是页地址
addr = addr & 0xFFFFFF00;
FLASH_Unlock();
status = FLASH_ErasePage(addr);
if (status == FLASH_COMPLETE) {
status = 0;
}
FLASH_Lock();
return status;
}