shipeng1989 发表于 2019-5-15 18:32:59

U盘IAP升级程序跳转APP异常

最近使用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正常,求高人解惑。

http://bbs.21ic.com/static/image/hrline/4.gif


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;

      #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="Scanning for ";
      for (u8 i=13;i<24;i++)string_buf=object_path;
      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-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("CPSIDI");__asm("CPSIDF");
                        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);      
}

另外说明下,APP中未用到USB功能并且我在跳转前也关闭了中断的,不知为何还是会发生异常。

luscu 发表于 2019-5-16 10:12:50

建议将工程上传才方便排查原因,只需要最少但是必须的功能代码,只包含USB升级功能的代码,这样看代码看不出问题。也可能是应用程序地址分配问题。

shipeng1989 发表于 2019-5-16 10:44:27

luscu 发表于 2019-5-16 10:12
建议将工程上传才方便排查原因,只需要最少但是必须的功能代码,只包含USB升级功能的代码,这样看代码看不 ...

感谢兄台的热情回复,好巧不巧就在几分钟前我刚把问题解决了,办法就是在跳转APP前把USB的外设时钟关闭:RCC_AHB2PeriphClockCmd( RCC_AHB2Periph_OTG_FS, DISABLE) ;就不会有跳转异常的问题发生了。我怀疑原因可能是USB中断在BOOTLOADER中打开之后,跳至APP时并没有使用USB功能因此没有设置USB中断入口从而导致中断溢出,保险起见以后在跳转APP前应将所有使能的外设全部关闭。

发表于 2019-5-16 11:25:33

楼主,应用程序中是否有使用USB?如果没有使用,不应该只关闭时钟,同时也要讲USBd进行einit一下,关闭USB的中断功能。

shipeng1989 发表于 2019-5-16 15:00:44

安 发表于 2019-5-16 11:25
楼主,应用程序中是否有使用USB?如果没有使用,不应该只关闭时钟,同时也要讲USBd进行einit一下,关闭USB ...

我看了一下例程里面的USBH_DeInit函数,里面并没有禁用中断这个操作,在发现关闭时钟这个办法之前我其实也试过跳转前调用USBH_DeInit函数,然并卵。我感觉只要断了USB的时钟量她有通天的本事也不能再兴风作浪了。所以也就懒得去改USBH_DeInit函数了。

huangyanga 发表于 2019-6-11 10:31:33

shipeng1989 发表于 2019-5-16 15:00
我看了一下例程里面的USBH_DeInit函数,里面并没有禁用中断这个操作,在发现关闭时钟这个办法之前我其实 ...

原来时钟关了后,她还真的有通天的本事!:lol
页: [1]
查看完整版本: U盘IAP升级程序跳转APP异常