ota协议增加上位机通知信息
authorkyz <kongyuzhong@google.com>
Thu, 9 Jan 2025 08:16:11 +0000 (16:16 +0800)
committerkyz <kongyuzhong@google.com>
Thu, 9 Jan 2025 09:02:47 +0000 (17:02 +0800)
STM32/code/Modbus_EquipmentData/Get_EquipmentData.c
STM32/code/app/rn2026_upgrade.c
STM32/code/mcu_com/mcu_com.c
STM32/code/mcu_com/mcu_com_ota.c [new file with mode: 0644]

index 09834cca3026e6ab89ff5795dff0e6a541b7fd0f..a59b9d21515c38c282521c06cce07d2803ab4e88 100644 (file)
@@ -37,11 +37,11 @@ void Modbus_Init(void)
     //Md_RegisterData.DeviceAddr = chipflashpara.tMdbus_DeviceParam.DeviceAddr;
     mb_device_config.device_addr = chipflashpara.tMdbus_DeviceParam.DeviceAddr<<8;
     if( chipflashpara.ComPara.UART_baud[PORT_UART_IFACE_RS4852] == 115200 ){
-        mb_device_config.device_baud = 1;
+        mb_device_config.device_baud = 0x0100;
     }else if( chipflashpara.ComPara.UART_baud[PORT_UART_IFACE_RS4852] == 9600 ){
-        mb_device_config.device_baud = 0;
+        mb_device_config.device_baud = 0x0000;
     }else{
-        mb_device_config.device_baud = 0;
+        mb_device_config.device_baud = 0x0000;
         chipflashpara.ComPara.UART_baud[PORT_UART_IFACE_RS4852] = 9600;
         Init_UART_ALL();
         SetRunFlag(TCM_FLAG_CHIPFLASHPARA_Save, FLAG_SET);
index bbd607b39300b87bab8b3c8472c8fe68eee01bf9..ce6e10ac2d3582676a269e3580baa2b881dfe2b1 100644 (file)
@@ -87,7 +87,7 @@ void rn2026_erase_flash( uint32_t erase_size )
        rn2026_write( 0xF000, upgrade_cmd, 8 );
 }
 
-
+extern void ota_reboot_notify( void );
 void rn2026_upgrade_finish( void )
 {
        uint16_t cmd_crc;
@@ -104,6 +104,7 @@ void rn2026_upgrade_finish( void )
 
        printf( "system restart ...\n" );
        SetRunFlag(TCM_FLAG_HARD_INIT, FLAG_SET);       //复位
+    ota_reboot_notify(); //notify host
 }
 
 void rn2026_write_page( uint32_t page, uint8_t *page_data )
index 9e797601738dc38f30df5e2392a2b02282211b01..18fbd55204872653f4f760447ebd5e62aa75b080 100644 (file)
@@ -769,6 +769,14 @@ void Task_McuCom2(void)
     {
         return;
     }
+    {
+        //OTA 下载模式,不主动上报数据
+        extern int32_t is_ota_start( void );
+        if( is_ota_start() ){
+            return;
+        }
+    }
+
     ComHard_Get_ComTest(&ComTest);
     switch (McuCom.com2_status)
     {
diff --git a/STM32/code/mcu_com/mcu_com_ota.c b/STM32/code/mcu_com/mcu_com_ota.c
new file mode 100644 (file)
index 0000000..5bdca69
--- /dev/null
@@ -0,0 +1,203 @@
+#include "def.h"
+#include "fun.h"
+#include "port.h"
+#include "mcu_com.h"
+#include "mcu_com_hard.h"
+#include "concentrator.h"
+#include "Get_EquipmentData.h"
+#include "protocol_modbus_rtu.h"
+#include "mcu_com_second_machine_sensor.h"
+#include "runflag.h"
+#include "App_para.h"
+#include "chipflash.h"
+#include "firmware.h"
+
+
+typedef struct{
+    uint8_t img_type;      //1:MCU, 2:RN, 0:None
+    uint32_t img_size;     //Îļþ³¤¶È
+    uint8_t img_ver[4];
+    uint16_t img_crc;
+
+    uint32_t img_received; //ÒѾ­ÊÕµ½Êý¾Ý³¤¶È
+
+    uint32_t start;    //0:Stop , 1:Start
+    uint32_t act_time;  //30s Éϴλʱ¼ä£¬30Ã볬ʱ£¬Í˳öÏÂÔØ
+    uint32_t port; 
+}ota_info_t;
+
+static ota_info_t ota;
+
+void ota_ver( int32_t port, uint8_t* data, uint16_t len  );
+void ota_start( int32_t port, uint8_t* data, uint16_t len );
+void ota_write( int32_t port, uint8_t* data, uint16_t len );
+
+extern int32_t rn2026_upgrade_start( void );
+extern int32_t mcucom_ota_ack( int32_t port, uint8_t ota_cmd,  uint8_t *data, uint16_t len );
+extern const uint8_t SW_VER[4];
+extern uint8_t RN_VER[4];
+void ota_ver( int32_t port, uint8_t* data, uint16_t len  )
+{
+    uint8_t ver_data[8];
+    memcpy( ver_data, SW_VER, 4 );
+    memcpy( &ver_data[4], RN_VER, 4 );
+    
+    mcucom_ota_ack( port, 1, ver_data, 8 );
+    HAL_Delay(20);
+}
+
+void ota_start( int32_t port, uint8_t* data, uint16_t len )
+{
+    uint8_t resp = 1;
+    uint32_t now;
+
+    now = HAL_GetTick();
+    ota.act_time = now; //update active time
+
+    ota.img_type = data[0];
+    memcpy( &ota.img_ver[0], &data[1], 4 );
+    ota.img_size = (data[5]<<24)|(data[6]<<16)|(data[7]<<8)|(data[8]);
+    ota.img_crc = (data[9]<<8)|(data[10]);
+    if( ota.img_size > 0x30000 ){
+        resp = 3; //03 ¹Ì¼þÐÅÏ¢Òì³£
+    }else if( ota.img_type != 1 && ota.img_type != 2){
+        resp = 3; //03 ¹Ì¼þÐÅÏ¢Òì³£
+    }else if( ota.img_type == 1 && ota.img_ver[0] == SW_VER[0] && ota.img_ver[1] == SW_VER[1]  ){
+        resp = 2; //02 ¹Ì¼þ°æ±¾ÒѾ­×îУ¬²»ÐèÒª¸üÐÂ
+    }else if( ota.img_type == 2 && ota.img_ver[0] == RN_VER[0] && ota.img_ver[1] == RN_VER[1]  ){
+        resp = 2;  //02        ¹Ì¼þ°æ±¾ÒѾ­×îУ¬²»ÐèÒª¸üÐÂ
+    }else{
+        uint8_t sector;
+               /* ²Á³ýÈ«²¿Éý¼¶ÇøµÄÊý¾Ý,¼´½«Ð´ÈëÉý¼¶°üÊý¾Ý */
+        printf( "erase sector start\n"  );
+               for(uint32_t addr = UPDATE_PACK_ADDRESS; addr < UPDATE_PACK_END; ){
+            printf( "erase sector, 0x%08X\n", addr );
+            sector = GetChipFlashSector(addr);
+            while(ChipFlashErase(sector)==0);
+            addr += GetChipFlashSectorSize(sector);
+        }
+        printf( "erase sector end\n"  );
+        resp = 1;
+        ota.start = 1;
+       }       
+    ota.img_received = 0;
+    mcucom_ota_ack( port, 2, &resp, 1 );
+
+    HAL_Delay(10);
+}
+
+void ota_reboot_notify( void )
+{
+    uint8_t resp = 4;
+    mcucom_ota_ack( PORT_UART_IFACE_WIFI, 3, &resp, 1 );
+    printf( "OTA notify host for reboot ...........\n" );
+    HAL_Delay(10);
+}
+
+void ota_write( int32_t port, uint8_t* data, uint16_t len )
+{
+    uint8_t resp = 0;
+    uint32_t off;
+    uint8_t *p;
+    uint32_t data_crc;
+
+    ota.act_time = HAL_GetTick(); //update active time
+
+/*
+MCU»ØÓ¦ Ò»¸ö×Ö½Ú£º
+01     : ³É¹¦£¬µÈ´ýÏÂÒ»¸öÊý¾Ý°ü£¬Èç¹ûÊÇ×îºóÒ»¸ö°üÔò×Ô¶¯ÖØÆôÍê³ÉÉý¼¶
+02     £ºÉý¼¶Ê§°Ü£¬Êý¾Ý²»ºÏ·¨
+03  £ºÉý¼¶Ê§°Ü£¬ÊÕµ½×îºóÒ»°üºóÑéÖ¤¹Ì¼þÄÚÈÝ´íÎó
+
+*/
+    if( len <= 4 ){
+        resp = 2;  //Êý¾Ý²»ºÏ·¨
+    }else{
+
+        off = (data[0]<<24)|(data[1]<<16)|(data[2]<<8)|(data[3]);
+        len -= 4;
+        if( off == ota.img_received ){
+            ota.img_received += len;
+            resp = 1;
+            Update_File(off, data+4, len );
+            if( ota.img_received >= ota.img_size ){
+                p = (uint8_t *)UPDATE_PACK_ADDRESS;
+                uint32_t crc = 0;
+
+                //check img
+                resp = 3;
+                //mcu img,-> write update_flag
+                //rn img -> update rn
+                if( ota.img_type == 1 ){
+                    //Îļþ×îºó12¸ö×ÖÊÇÇ°ÃæÊý¾ÝµÄУÑéÂ룬·Ö±ðÊÇflag size crc
+                    //È¡³öcrc£¬½øÐÐУÑé
+                    memcpy( &crc, p+ota.img_size-4, 4 );
+                    data_crc = ModBusCRC16( p, ota.img_size-12 );
+                    if( crc == data_crc ){
+                        printf( "MCU img data crc ok!\n" );
+                        resp = 1;
+                        Update_Over( ota.img_size );
+                        ota_reboot_notify(); //notify host
+                        SetRunFlag(TCM_FLAG_HARD_INIT, FLAG_SET);      //¸´Î»
+                    }else{
+                        printf( "MCU img data crc fail  %x / %x!\n", crc, data_crc );
+                    }
+                }else if( ota.img_type == 2 ){
+                    //TODO
+                    //Îļþǰ512×Ö½ÚÊÇУÑéÂ룬ǰ16×Ö½Ú·Ö±ðÊÇflag ver size crc
+                    //È¡³öcrc£¬½øÐÐУÑé
+                    p = (uint8_t *)UPDATE_PACK_ADDRESS;
+                    memcpy( &crc, p+12, 4 );
+                    data_crc = ModBusCRC16( p+512, ota.img_size-512 );
+                    if( crc == data_crc ){
+                        printf( "RN img data crc ok!\n" );
+                        resp = 1;
+                        rn2026_upgrade_start(); //¿ªÊ¼spi·¢ËÍÊý¾Ýµ½RNоƬ
+                    }else{
+                        printf( "RN img data crc fail  %x / %x!\n", crc, data_crc );
+                    }
+                }
+            }
+            printf( "ota update 0x%06X/0x%06X\n", ota.img_received, ota.img_size );
+        }else{
+            resp = 2;  //Êý¾Ý²»ºÏ·¨
+        }
+    }
+    mcucom_ota_ack( port, 3, &resp, 1 );
+    //TODO£º add ack before reboot 
+    HAL_Delay(20);
+}
+
+int32_t is_ota_start( void )
+{
+    uint8_t resp;
+
+    //mcucom task ÖÐÿÃëµ÷Óô˺¯Êý¼ì²é״̬
+    if( ota.start && HAL_GetTick() - ota.act_time > 30*1000 ){
+            ota.start = 0;
+            resp = 4;
+            mcucom_ota_ack( PORT_UART_IFACE_WIFI, 2, &resp, 1 );
+            HAL_Delay(10);
+            printf( "OTA notify host for timeout  %d  ...........\n", ota.act_time - HAL_GetTick() );
+
+    }
+
+    return ota.start;
+}
+
+int32_t ota_data_handle( int32_t port, uint8_t cmd, uint8_t *data, uint16_t len )
+{
+    ota.port = port;
+    switch( cmd ){
+        case 1: //read mcu version
+            ota_ver( port, data, len );
+            break;
+        case 2: //update request
+            ota_start( port, data, len );
+            break;
+        case 3: //write img data
+            ota_write( port, data, len );
+            break;
+    }
+    return 0;
+}