ESP8266 OTA升级错误

岁月静好
Posts: 1
Joined: Tue Aug 07, 2018 2:21 pm

ESP8266 OTA升级错误

Postby 岁月静好 » Tue Aug 07, 2018 2:32 pm

因为项目要求,无法使用官方的升级接口,所以自己写flash进行升级,但是遇到了问题

Code: Select all

reboot to use1
state: 5 -> 0 (0)
rm 0
pm close 7
del if0
usl

 ets Jan  8 2013,rst cause:2, boot mode:(3,7)

load 0x40100000, len 2408, room 16
tail 8
chksum 0xe5
load 0x3ffe8000, len 776, room 0
tail 8
chksum 0x84
load 0x3ffe8310, len 632, room 0
tail 8
chksum 0xd8
csum 0xd8

2nd boot version : 1.6
  SPI Speed      : 40MHz
  SPI Mode       : DIO
  SPI Flash Size & Map: 32Mbit(512KB+512KB)
jump to run user1 @ 1000

error magic!
first boot failed, reboot to try backup bin


一直是错误的,我是用C#写了一个上位机,分段读取bin文件,然后通过UDP发给esp8266,并且在写入完成后把数据读出来了
数据跟我发送的是一致的,但是还是无法启动,以下是我的升级代码

Code: Select all

#define USER1 0x01000
#define USER2 0x81000 //0x101000

uint16 History_Pack = 0;
uint32 OTA_ADDR;

void OTA_upgrade(void)
{

   uint16 PackLength = 0;
   uint16 TotalPack,CurrentPack;
   uint16 Sector;
   uint16 Sector_count = 0,num = 0;
   uint8 bin = 0;
    //总包数
    TotalPack = (uint16)char_change_int( UDP_ReceiveBuff.Pack_data, 2, UDP_ReceiveBuff.Data_Length);
    //当前分包
    CurrentPack = (uint16)char_change_int( UDP_ReceiveBuff.Pack_data, 2, UDP_ReceiveBuff.Data_Length);
    //包长
    PackLength = (uint16)char_change_int( UDP_ReceiveBuff.Pack_data, 2, UDP_ReceiveBuff.Data_Length);

    os_printf("\r\n[OTA]TotalPack:%d CurrentPack:%d PackLength:%d",TotalPack,CurrentPack,PackLength);

    if (CurrentPack == 1 && TotalPack != 0)
    {
      //查询 user bin
      uint8_t userBin = system_upgrade_userbin_check();
      switch (userBin)
      {
      case UPGRADE_FW_BIN1:
            OTA_ADDR = USER2;
            Sector_count = 129;
            Sector = 130 + ((TotalPack * PackLength) / 4096);
         break; //  user2.bin
      case UPGRADE_FW_BIN2:
            OTA_ADDR = USER1;
            Sector_count = 1;
            Sector = 2 + ((TotalPack * PackLength) / 4096);
         break; // user1.bin
      default:  os_printf("[OTA]Invalid userbin number!\n"); return;
      }
      while(1)
      {
         //擦除扇区(flash必须先擦再写)
         if(spi_flash_erase_sector (Sector_count) == SPI_FLASH_RESULT_OK) 
         {
            os_printf("\r\n[OTA]flash_erase_sector %d OK\n",Sector_count);    
            num = 0;
            Sector_count++;   
         }else
          num++;
          if(num > 10 || Sector_count >= Sector)
             break;
      }      
    }

    if(History_Pack < CurrentPack && History_Pack != CurrentPack)
    {
      bin = OTA_Write((uint32 *)UDP_ReceiveBuff.Pack_data,PackLength);
      if(bin)os_printf("\r\n[OTA]write %d OK",CurrentPack);
    }
   else
      bin = 1;

   if(bin)
   {   
      
      //写入成功
      OTA_upgrade_Reply(TotalPack, CurrentPack, 1);
      History_Pack = CurrentPack;
   }
   else
   {   
      //写入失败
      OTA_upgrade_Reply(TotalPack, CurrentPack, 0);
   }

   if((CurrentPack) >= TotalPack)
   {
      os_printf("\r\n[OTA]reboot OK",CurrentPack);

      system_upgrade_flag_set(UPGRADE_FLAG_FINISH);
      //重启系统,运行新软件
      system_upgrade_reboot();      
   }

}


uint8 OTA_Write(uint32 *pBuffer,uint32 length)
{   uint32 Write_data[45] = {0};
   uint32 Read_data[45] = {0};
   uint8 i = 0;
   uint8 Ip_num[4]={0};
   for(i = 0; i < (length / 4); i++)
   {
      Ip_num[3]=(uint8)((pBuffer[i]>>24)&0x000000ff);
      Ip_num[2]=(uint8)((pBuffer[i]>>16)&0x000000ff);
      Ip_num[1]=(uint8)((pBuffer[i]>>8)&0x000000ff);
      Ip_num[0]=(uint8)(pBuffer[i]&0x000000ff);

      Write_data[i] = (Ip_num[0] << 24) + (Ip_num[1] << 16) + (Ip_num[2] << 8) + (Ip_num[3]);

   }

      //写入数据到A扇区(flash必须4个字节对齐)
      if(spi_flash_write (OTA_ADDR, Write_data,length) == SPI_FLASH_RESULT_OK){
         //读取扇区
         spi_flash_read(OTA_ADDR, Read_data,length);
         os_printf("\r\n[OTA]READ:");
         for(i = 0; i < (length / 4); i++)
            os_printf(" %X",Read_data[i]);

         OTA_ADDR = OTA_ADDR + length;
         os_printf("\r\n[OTA]OTA_ADDR %d",OTA_ADDR);

         return 1;               
      }else return 0;   
}


测试的代码,写的有点乱,最开始打印写入的数据发现字节序反了,所以临时调换了一下
以下是程序打印的LOG

Code: Select all

[OTA]READ: EA040001 4001040 0 F0370300 A4180040 B4180040 12C1C0F2 61080261 4C26105 D961E971 39C1ED05 32A0A6DD 2CD042D 50C8401 F6FFC000 3D0C48 C18B2E29 A1D04411 1F3FFC0 2D0D 1C03854D CC327C F2C62200 8BF129E1 68C10C05 C0779D1 59B11B66 69918221 CC2210A A6185592 210DA891 1BD9AA99
[OTA]OTA_ADDR 4224
[OTA]write 1 OK
 -->OTA_upgrade
[OTA]TotalPack:1915 CurrentPack:2 PackLength:128
[OTA]READ: 99F12D01 3D0E0C84 1E3FFC0 2D0F 3D0C0C84 1E0FFC0 28E1 3D014D01 C54D002D E3D010C 8401DAFF C000002D C3D0FB2 E070C84 B0BD30B2 4E0701D5 FFC00000 8BCC08F1 1BDD079D B442210C 22210B32 210D1B22 29B14A33 39D16662 8C28E185 4B000C02 C851D861 E871F881 84112C1 400DF000 F0CCFF3F FF00
[OTA]OTA_ADDR 4352
[OTA]write 2 OK
 -->OTA_upgrade
[OTA]TotalPack:1915 CurrentPack:3 PackLength:128
[OTA]READ: FF FF0000 12C1E0C9 21980268 22881272 407A204 B20401 F951E941 59010911 D9313001 21D2040F 52040BE2 40CF204 D00BB11 80AA0100 FF1180EE 1807730 60553082 403B0AA 30620408 B20405F0 EE308066 1F2040E 90883000 BB119204 480FF11 F0EE3080 9901B099 30F832B2 409F0DD 3000BB11 B06630B2
[OTA]OTA_ADDR 4480
[OTA]write 3 OK
 -->OTA_upgrade

上位机的LOG

找到设备9800
正在传输升级包  1915/1
正在传输数据:EA-04-00-01-04-00-10-40-00-00-00-00-F0-37-03-00-A4-18-00-40-B4-18-00-40-12-C1-C0-F2-61-08-02-61-04-C2-61-05-D9-61-E9-71-39-C1-ED-05-32-A0-A6-DD-02-CD-04-2D-05-0C-84-01-F6-FF-C0-00-00-3D-0C-48-C1-8B-2E-29-A1-D0-44-11-01-F3-FF-C0-00-00-2D-0D-1C-03-85-4D-00-CC-32-7C-F2-C6-22-00-8B-F1-29-E1-68-C1-0C-05-0C-07-79-D1-59-B1-1B-66-69-91-82-21-0C-C2-21-0A-A6-18-55-92-21-0D-A8-91-1B-D9-AA-99
正在传输升级包  1915/2
正在传输数据:99-F1-2D-01-3D-0E-0C-84-01-E3-FF-C0-00-00-2D-0F-3D-0C-0C-84-01-E0-FF-C0-00-00-28-E1-3D-01-4D-01-C5-4D-00-2D-0E-3D-01-0C-84-01-DA-FF-C0-00-00-2D-0C-3D-0F-B2-0E-07-0C-84-B0-BD-30-B2-4E-07-01-D5-FF-C0-00-00-8B-CC-08-F1-1B-DD-07-9D-B4-42-21-0C-22-21-0B-32-21-0D-1B-22-29-B1-4A-33-39-D1-66-62-8C-28-E1-85-4B-00-0C-02-C8-51-D8-61-E8-71-F8-81-08-41-12-C1-40-0D-F0-00-F0-CC-FF-3F-00-00-FF-00
正在传输升级包  1915/3
正在传输数据:00-00-00-FF-00-FF-00-00-12-C1-E0-C9-21-98-02-68-22-88-12-72-04-07-A2-04-00-B2-04-01-F9-51-E9-41-59-01-09-11-D9-31-30-01-21-D2-04-0F-52-04-0B-E2-04-0C-F2-04-0D-00-BB-11-80-AA-01-00-FF-11-80-EE-01-80-77-30-60-55-30-82-04-03-B0-AA-30-62-04-08-B2-04-05-F0-EE-30-80-66-01-F2-04-0E-90-88-30-00-BB-11-92-04-04-80-FF-11-F0-EE-30-80-99-01-B0-99-30-F8-32-B2-04-09-F0-DD-30-00-BB-11-B0-66-30-B2

Who is online

Users browsing this forum: No registered users and 1 guest