--- /dev/null
+#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;
+}