#include "main.h" #include "pt_ext.h" #include "pt_task.h" #include "dev_boot.h" #include "dev_rs485.h" #include "ringbuffer.h" #include "update_protocol.h" #include "app_led.h" typedef struct { boot_para_t Para; }BootLoader_t; static struct { boot_para_t cfg; uint32_t PackageRecvIndx; uint32_t PackageRecvBytes; uint8_t isNeedUpdate; uint8_t isUpdateGoing; } g_update; static struct { uint8_t rb_pool[512]; ring_buf_t rb; uint8_t frame[256]; int frame_len; } g_recive; static void send_bytes(uint8_t* pbuf, int len) { dev_rs485_send(pbuf, len); } static void rx_callback(uint8_t data) { ring_buf_put(&g_recive.rb, &data, 1); } static void update_on_start( uint8_t* frame, int len, uint8_t* pload, int size) { update_protocol_req_t* pReq = (void *)&frame[sizeof(update_protocol_hd_t)]; g_update.cfg.AppFlag = 0; g_update.cfg.UpdateFlag = APP_UPDATE_FLAG; g_update.cfg.Crc32Check = pReq->AppCrc32; g_update.cfg.PackageNum = pReq->PackageNum; g_update.cfg.AppSize = pReq->AppSize; dev_boot_write_param(g_update.cfg); g_update.PackageRecvIndx = 0; g_update.PackageRecvBytes = 0; update_send_cmd(0x01, 1, 0); dev_boot_erase_app(); update_send_cmd(0x02, g_update.PackageRecvIndx, g_update.cfg.PackageNum); } static void update_on_download(uint8_t* frame, int len, uint8_t* pload, int size) { update_protocol_rsp_t* pRsp =(void *)&frame[sizeof(update_protocol_hd_t)]; dev_boot_write_app(pRsp->PackageIndex * 200, pload, size); g_update.PackageRecvIndx ++; g_update.PackageRecvBytes += size; if (g_update.PackageRecvBytes == g_update.cfg.AppSize){ if ( dev_boot_check_app()) { uint32_t crc_rom = dev_boot_get_app_crc32(g_update.cfg.AppSize); if (crc_rom == g_update.cfg.Crc32Check) { g_update.cfg.AppFlag = 0; g_update.cfg.UpdateFlag = 0; g_update.cfg.PackageNum = 0; dev_boot_write_param(g_update.cfg); NVIC_SystemReset(); } } }else{ update_send_cmd(0x02, g_update.PackageRecvIndx, g_update.cfg.PackageNum); } } static int update_cmd_process(uint8_t cmd, void* frame, int len, void* pload, int size) { int res = 0; switch (cmd) { case 0x81:{ update_on_start(frame, len, pload, size); res = 1; } break; case 0x82: { update_on_download(frame, len, pload, size); res = 1; }break; default: break; } return res; } static int read_frame(void) { uint8_t dat = 0; int len = ring_buf_get(&g_recive.rb, &dat, 1); if (len > 0) { if (g_recive.frame_len < sizeof(g_recive.frame)) { g_recive.frame[ g_recive.frame_len] = dat; g_recive.frame_len++; } return 1; } else { return 0; } } static int update_process(void* frame, int len) { uint8_t cmd = 0; uint8_t user_data[256] = {0}; int user_data_len = 0; int res = update_unpack(frame, len, &cmd, user_data, &user_data_len); if(res ==0) return -1; res = update_cmd_process(cmd,frame, len, user_data,user_data_len); if(res ==0){ return -2; } return 0; } int app_update_entry(struct pt *pt) { int len = 0; PT_BEGIN(pt); dev_boot_read_param(&g_update.cfg); if (g_update.cfg.UpdateFlag == APP_UPDATE_FLAG) { g_update.isNeedUpdate = 1; } else { g_update.isNeedUpdate = 1; if (dev_boot_check_app()) { // uint32_t crc_rom = dev_boot_get_app_crc32(g_update.cfg.AppSize); // if (crc_rom == g_update.cfg.Crc32Check) // { g_update.isNeedUpdate = 0; dev_boot_run_app(); // } } } if (g_update.isNeedUpdate) { ring_buf_init(&g_recive.rb, g_recive.rb_pool, sizeof(g_recive.rb_pool)); dev_rs485_init(); dev_rs485_rx_register(rx_callback); update_init(send_bytes); //²Á³ýAPP if ((g_update.cfg.UpdateFlag == APP_UPDATE_FLAG) && dev_boot_check_app() ){ dev_boot_erase_app(); } // uint8_t i[6] = {1,2,3,4,5,6}; // while(1) // { // dev_rs485_send(i,6); // } app_led_set_status( APP_LED_STATUS_UPDATE_RDY); } while (1){ PT_YIELD(pt); len = read_frame(); if (len == 0){ PT_DELAY_MS(pt, 20); len = read_frame(); if (len == 0){ if (g_recive.frame_len > 0){ app_led_set_status( APP_LED_STATUS_UPDATE_ING); update_process( g_recive.frame ,g_recive.frame_len); g_recive.frame_len = 0; }else{ if(g_update.cfg.UpdateFlag == APP_UPDATE_FLAG && g_update.PackageRecvBytes < g_update.cfg.AppSize){ update_send_cmd(0x02, g_update.PackageRecvIndx, g_update.cfg.PackageNum ); } } } } } PT_END(pt); } PT_TASK_APP_REG(app_update_entry);