本帖最后由 shipeng1989 于 2019-5-16 08:41 编辑
最近使用STM32F429的USB_OTG_FS做了一个BOOTLOADER可通过USB(PA11,PA12)读取U盘中的文件或者UART二选一更新APP。UART升级一切正常,唯有USB升级有个小问题:升级过程都一切顺利,但是到了要跳转到APP时芯片先是背光驱动IO无输出或输出低电平(其它IO状态未知,怀疑和背光驱动IO相似)等了几秒后芯片复位,复位后也能正常进入APP。但就是复位之前那段时间要等好几秒不明白是什么原因。如果是HardFault异常中断,我的看门狗没有使能,芯片没理由复位啊?这个问题还无法仿真,因为一旦芯片复位仿真器就与芯片失去同步了。我通过将代码一段一段注释排除发现:如将"USBH_Process(&USB_OTG_Core, &USB_Host);"函数及所在的循环注释就不会发生复位问题跳转APP正常,求高人解惑。
U盘升级函数:
/*
*********************************************************************************************************
* Function Name : Programing_From_Flie
* Function Detail : Programing The Chip From a Flie
* Paramater : None
* Return Value : None
*********************************************************************************************************
*/
void Programing_From_Flie(void)
{
static uint8_t page_buffer[4096];
#if defined BK180H
const char object_path[]="0:BK180H.bin";
#elif defined BK300H
const char object_path[]="0:BK300H.bin";
#elif defined U180
const char object_path[]="0:U180.bin";
#endif
PrintMessage("Udisk Connected!");
SysTick_Init();
do
{
USBH_Process(&USB_OTG_Core, &USB_Host);
}
while (count_ms<8000 && (USB_Host.gState!=HOST_CLASS || USBH_MSC_BOTXferParam.MSCState!=0x05));
SysTick->CTRL=0x00; //Stop Counter
char string_buf[24]="Scanning for ";
for (u8 i=13;i<24;i++)string_buf=object_path[i-11];
PrintMessage(string_buf);
result = f_mount(&fs,"0:",1); /* Mount a logical drive */
if (result != FR_OK)
{
PrintMessage("Mounting Failed!");
return;
}
result = f_open(&file, object_path, FA_OPEN_EXISTING | FA_READ);
if(result == FR_OK)
{
if (file.fsize==0 || file.fsize>0x1F8000)
{
PrintMessage("File Size Error!");
goto close_file_exit;
}
if (FLASH_If_GetWriteProtectionStatus() == 0)
{
/* Disable the write protection */
if (FLASH_If_DisableWriteProtection()==1)
{
PrintMessage("Write Protection disabled!");
}
else
{
PrintMessage("Error: Flash write unprotection failed!");
goto close_file_exit;
}
}
PrintMessage("Chip Erassing");
FLASH_If_Erase(APPLICATION_ADDRESS,file.fsize);
PrintMessage("Chip Programming");
uint32_t flashdestination = APPLICATION_ADDRESS;
uint32_t packets_max = file.fsize/4096+(file.fsize%4096==0?0:1);
for (uint32_t packets_readed=0;packets_readed<packets_max;packets_readed++)
{
result = f_read(&file, page_buffer, sizeof(page_buffer), &fnum);
if(result==FR_OK)
{
/* Write received data in Flash */
if (FLASH_If_Write(&flashdestination, (uint32_t *) page_buffer, (fnum+3)/4) == 0)
{
ShowProgressPercent((packets_readed+1)*100/packets_max);
}
else /* An error occurred while writing to Flash memory */
{
/* End session */
PrintMessage("An error occurred while writing to Flash memory");
PrintMessage("End session");
goto close_file_exit;
}
}
else
{
PrintMessage("Error File Reading!");
goto close_file_exit;
}
}
f_close(&file);
PrintMessage("Going to the new APP within Seconds...");
SysTick_Init();
for (uint8_t i=5;i!=0;i--)
{
count_ms = 0;
LCD_Show8x16sym(272-lcd_x*16,(lcd_y[lcd_x]-12)*8,i+'0',WHITE,BLACK);
while (count_ms<1000);
}
SysTick->CTRL=0x00; //Stop Counter
//跳转APP代码
/* Test if user code is programmed starting from address "APPLICATION_ADDRESS" */
if (((*(__IO uint32_t*)APPLICATION_ADDRESS) & 0x2FFE0000 ) == 0x20000000)
{
__asm("CPSID I");__asm("CPSID F");
USBH_DeInit(&USB_OTG_Core,&USB_Host);
/* Jump to user application */
JumpAddress = *(__IO uint32_t*) (APPLICATION_ADDRESS + 4);
Jump_To_Application = (pFunction) JumpAddress;
/* Initialize user application's Stack Pointer */
__set_PSP(*(__IO uint32_t*) APPLICATION_ADDRESS);
__set_CONTROL(0);
__set_MSP(*(__IO uint32_t*) APPLICATION_ADDRESS);
Jump_To_Application();
//APP跳转代码结束
}
}
else
{
PrintMessage("Error File Opening!");
}
close_file_exit:
f_close(&file);
}
|