/* * Copyright (c) 2006-2018, RT-Thread Development Team * * SPDX-License-Identifier: Apache-2.0 * * Change Logs: * Date Author Notes * 2019-09-22 Warfalcon first version */ //链接地址 //https://gitee.com/spunky_973/rt-fota #include #include #include #if defined(RT_USING_FINSH) && defined(FINSH_USING_MSH) #include #include #endif #include #include #include #define DBG_ENABLE #define DBG_SECTION_NAME "main" #ifdef RT_MAIN_DEBUG #define DBG_LEVEL DBG_LOG #else #define DBG_LEVEL DBG_INFO #endif #define DBG_COLOR #include #include "sys_config.h" #include #define BL_VERSION "1.1.4" #define COMPILE "20230901" #define RTU_DTU GET_PIN(B,13) #define Beep GET_PIN(B, 4) #define HMI_THREAD_PRIORITY 5 //线程优先级 #define HMI_THREAD_STACK_SIZE 2048 //线程栈空间大小 #define HMI_THREAD_TIMESLICE 5 //时间片 #define MAX_SIZE 512 /* 消息队列控制块 */ static struct rt_messagequeue rx_mq;//接收数据消息队列 struct serial_configure otaconfig = RT_SERIAL_CONFIG_DEFAULT; /*初始化配置参数*/ rt_device_t otaserial; /*串口设备句柄*/ const char boot_log_buf[] = " ____ _____ _____ ___ _____ _ \r\n\ | _ \\_ _| | ___/ _ \\ _ _|/ \\ \r\n\ | |_) || |_____ | |_ || || | | / _ \\ \r\n\ | _ < | |_____ | _| ||_|| | |/ ___ \\ \r\n\ |_| \\_\\|_| |_| \\___/ |_/_/ \\_\\ \r\n"; static char msg_pool[256]; static char rx_buffer[256]; static rt_err_t uart_input(rt_device_t dev, rt_size_t size); extern void HMI_WRITE(char *fmt,...); int HMI_Thread(void); static void Hmi_thread_entry(void *parameter); static rt_thread_t hmi1 = RT_NULL; /* 串口接收消息结构*/ struct rx_msg { rt_device_t dev; rt_size_t size; }; void rt_fota_print_log(void) { LOG_RAW("%s", boot_log_buf); LOG_RAW("2016 - 2019 Copyright by Radiation @ warfalcon \r\n"); LOG_RAW("Version: %s build %s\r\n\r\n", RT_FOTA_SW_VERSION, __DATE__); } void Beep_Tip(uint16_t volnum) { rt_pin_mode(Beep, PIN_MODE_OUTPUT); rt_pin_write(Beep, 1); rt_thread_mdelay(volnum); rt_pin_write(Beep, 0); } int Ota_uart2_init(void) { otaserial = rt_device_find("uart4"); /*修改串口配置参数*/ otaconfig.baud_rate = BAUD_RATE_115200; //修改波特率115200 otaconfig.data_bits = DATA_BITS_8; //数据位8 otaconfig.stop_bits = STOP_BITS_1; //停止位1 otaconfig.bufsz = 1024; //缓冲区1k otaconfig.parity = PARITY_NONE; //无奇偶校验 /* step3:控制串口设备。通过控制接口传入命令控制字,与控制参数 */ rt_device_control(otaserial, RT_DEVICE_CTRL_CONFIG, &otaconfig); /* step4:打开串口设备。以中断接收及轮询发送模式打开串口设备 */ if(rt_device_open(otaserial, RT_DEVICE_FLAG_DMA_RX)==RT_EOK){ rt_kprintf("HMI Init Success!!\n"); HMI_WRITE("sleep=%d",1); HMI_WRITE("sleep=%d",1); HMI_WRITE("sleep=%d",1); return 0; } else { rt_kprintf("HMI Init Failed!!\n"); return RT_EOK; } } /* 接收数据回调函数 */ static rt_err_t uart_input(rt_device_t dev, rt_size_t size) { struct rx_msg msg; rt_err_t result; msg.dev = dev; msg.size = size; result = rt_mq_send(&rx_mq, &msg, sizeof(msg)); if ( result == -RT_EFULL) { /* 消息队列满 */ rt_kprintf("message queue full!\n"); } return result; } /*串口屏写数据*/ void HMI_WRITE(char *fmt, ...) { va_list ap; //末尾需要拼接的3个ff const char hmi_hex[2]={0x0D,0X0A}; //末尾需要拼接的3个ff const char hmi_end[3]={0xff,0xff,0xff}; static char str[MAX_SIZE]; memset(str,'\0',sizeof(str)); rt_size_t str_length; va_start(ap,fmt); str_length = rt_vsnprintf(str, sizeof(str) - 1, fmt, ap); if (str_length > MAX_SIZE - 1) str_length = MAX_SIZE - 1; rt_device_write(otaserial, 0, str, str_length); va_end(ap); if(strstr(str, "sleep")!=NULL) { rt_device_write(otaserial, 0,hmi_end,3); } else if (strstr(str, "t7.txt")!=NULL) { rt_device_write(otaserial, 0,hmi_end,3); } else if (strstr(str, "t5.txt")!=NULL) { rt_device_write(otaserial, 0,hmi_end,3); } else if (strstr(str, "update.val")!=NULL) { rt_device_write(otaserial, 0,hmi_end,3); } else { rt_device_write(otaserial, 0,hmi_hex,2); } } static void HMI_Decode(char *cmd,uint8_t cmd_len) { //擦除APP分区,将df_area分区固件搬运至app分区 if(strstr(rx_buffer,"resetapp")!=NULL) { rt_kprintf("固件恢复!!!\n"); HMI_WRITE("固件恢复!!!"); const char *dst_part_name = "app"; const char *src_part_name = "df_area"; const struct fal_partition *dst_part; const struct fal_partition *src_part; dst_part = fal_partition_find(dst_part_name); src_part = fal_partition_find(src_part_name); if (dst_part == RT_NULL || src_part == RT_NULL) { if (dst_part == RT_NULL) rt_kprintf("%s partition is not exist!\n", dst_part_name); if (src_part == RT_NULL) rt_kprintf("%s partition is not exist!\n", src_part_name); } else { rt_kprintf("Clone %s partition to %s partition:\n", src_part_name, dst_part_name); HMI_WRITE("Clone %s partition to %s partition:", src_part_name, dst_part_name); if (fal_partition_erase(dst_part, 0, dst_part->len) >= 0) { int clone_pos = 0; int clone_len = 0, clone_tol_len; static int clone_num; rt_uint8_t *buf = rt_malloc(4096); if (dst_part->len < src_part->len) clone_tol_len = dst_part->len; else clone_tol_len = src_part->len; while ((clone_pos < clone_tol_len) && (buf != RT_NULL)) { clone_len = fal_partition_read(src_part, clone_pos, buf, 4096); if (clone_len < 0) { rt_kprintf("\nread %s partition failed, clone stop!\n", src_part_name); break; } if (fal_partition_write(dst_part, clone_pos, buf, clone_len) < 0) { rt_kprintf("\nwrite %s partition failed, clone stop!\n", dst_part_name); break; } clone_pos += clone_len; clone_num++; HMI_WRITE("debug.update.val=%d",clone_num); if(clone_num==99) clone_num = 0; } if (clone_pos >= clone_tol_len) { HMI_WRITE("Clone partition success, total %d bytes!", clone_tol_len); rt_kprintf("\nClone partition success, total %d bytes!\n", clone_tol_len); HMI_WRITE("debug.update.val=100"); HMI_WRITE("debug.update.val=100"); HMI_WRITE("debug.update.val=100"); } else { HMI_WRITE("Clone partition failed!"); rt_kprintf("Clone partition failed!"); } if (buf) rt_free(buf); } } } //备份app分区至df_area分区 else if(strstr(rx_buffer, "backupapp")!=NULL) { } //启动程序 else if(strstr(rx_buffer, "startup")!=NULL) { rt_fota_start_application(); } //重启bootloader else if(strstr(rx_buffer, "bootloader")!=NULL) { uint8_t boot=1; if(easyflash_init()==EF_NO_ERR) { ef_set_env_blob("stop_in_bootloader", &boot, sizeof(boot)); } rt_hw_cpu_reset(); } //擦除app分区 else if (strstr(rx_buffer, "eraseapp")!=NULL) { struct fal_partition fal_erase; fal_erase.offset=204800; rt_sprintf(fal_erase.name, "%s","app"); rt_sprintf(fal_erase.flash_name, "%s","onchip_flash"); fal_partition_erase(&fal_erase,0,319488); } //擦除easyflash else if(strstr(rx_buffer, "eraseeasyflash")!=NULL){ rt_kprintf("erase easyflash...\n"); //uint8_t isave = 0; ef_env_set_default(); //ef_set_env_blob("iSave", &isave, sizeof(isave)); HMI_WRITE("easyflash擦除成功!"); } else if(strstr(rx_buffer, "erasedf_area")!=NULL){ struct fal_partition fal_erase; fal_erase.offset=15729152; rt_sprintf(fal_erase.name, "%s","df_area"); rt_sprintf(fal_erase.flash_name, "%s","w25q16m"); fal_partition_erase(&fal_erase,0,524288); } } /*HMI*/ int HMI_Thread(void) { Ota_uart2_init(); rt_err_t ret = RT_EOK; /* 初始化消息队列 */ rt_mq_init(&rx_mq, "rx_mq", msg_pool, /* 存放消息的缓冲区 */ sizeof(struct rx_msg), /* 一条消息的最大长度 */ sizeof(msg_pool), /* 存放消息的缓冲区大小 */ RT_IPC_FLAG_FIFO); /* 如果有多个线程等待,按照先来先得到的方法分配消息 */ /* 创建线程*/ hmi1 = rt_thread_create("Hmi_Fresh_thread", Hmi_thread_entry, RT_NULL, HMI_THREAD_STACK_SIZE, HMI_THREAD_PRIORITY, HMI_THREAD_TIMESLICE); /* 如果获得线程控制块,启动这个线程 */ if (hmi1 != RT_NULL) { rt_thread_startup(hmi1); } else { ret = RT_ERROR; } return ret; } /*入口函数*/ static void Hmi_thread_entry(void *parameter) { /* 设置接收回调函数 */ rt_device_set_rx_indicate(otaserial, uart_input); struct rx_msg msg; rt_err_t result; rt_uint32_t rx_length; rt_uint32_t start_timeout = 0; ef_set_env_blob("driver_bl", &BL_VERSION, sizeof(BL_VERSION)); ef_set_env_blob("driver_compile", &COMPILE, sizeof(COMPILE)); HMI_WRITE("debug.t5.txt=\"%s\"",BL_VERSION); HMI_WRITE("debug.t7.txt=\"%s\"",COMPILE); while(1) { rt_memset(&msg, 0, sizeof(msg)); /* 从消息队列中读取消息*/ result = rt_mq_recv(&rx_mq, &msg, sizeof(msg), 1000); if (result == RT_EOK) { rx_length = rt_device_read(msg.dev, 0, rx_buffer, msg.size); rx_buffer[rx_length] = '\0'; LOG_I("%s\n",rx_buffer); HMI_Decode(rx_buffer,rx_length); } else{ //rt_device_write(otaserial, 0, &rx_buffer[0], rx_length); HMI_WRITE("bootloader mode"); //rt_kprintf("bootloader mode"); start_timeout++; if(start_timeout==300) { rt_fota_start_application(); start_timeout = 0; } } /*********************************************************************************************************************/ //rt_kprintf("%04d-%02d-%02d %02d:%02d:%02d\n",tm.tm_year+1900,tm.tm_mon+1,tm.tm_mday,tm.tm_hour,tm.tm_min,tm.tm_sec); //rt_kprintf("workSta.vbat.txt=%c%d.%02d%c\n",hmi_str,voltage/100,voltage%100,hmi_str); } } int main(void) { #if defined(RT_USING_FINSH) && defined(FINSH_USING_MSH) finsh_set_prompt("rt-fota />"); #endif extern void rt_fota_init(void); easyflash_init(); rt_fota_init(); HMI_Thread(); Beep_Tip(100); return RT_EOK; } //INIT_DEVICE_EXPORT(Ota_uart2_init);