main.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. /*
  2. * Copyright (c) 2006-2018, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2019-09-22 Warfalcon first version
  9. */
  10. //链接地址
  11. //https://gitee.com/spunky_973/rt-fota
  12. #include <rtthread.h>
  13. #include <rtdevice.h>
  14. #include <board.h>
  15. #if defined(RT_USING_FINSH) && defined(FINSH_USING_MSH)
  16. #include <finsh.h>
  17. #include <shell.h>
  18. #endif
  19. #include <fal.h>
  20. #include <rt_fota.h>
  21. #include <fal_def.h>
  22. #define DBG_ENABLE
  23. #define DBG_SECTION_NAME "main"
  24. #ifdef RT_MAIN_DEBUG
  25. #define DBG_LEVEL DBG_LOG
  26. #else
  27. #define DBG_LEVEL DBG_INFO
  28. #endif
  29. #define DBG_COLOR
  30. #include <rtdbg.h>
  31. #include "sys_config.h"
  32. #include <easyflash.h>
  33. #define BL_VERSION "1.1.2"
  34. #define COMPILE "20210408"
  35. #define RTU_DTU GET_PIN(B,13)
  36. #define HMI_THREAD_PRIORITY 5 //线程优先级
  37. #define HMI_THREAD_STACK_SIZE 2048 //线程栈空间大小
  38. #define HMI_THREAD_TIMESLICE 5 //时间片
  39. #define MAX_SIZE 512
  40. /* 消息队列控制块 */
  41. static struct rt_messagequeue rx_mq;//接收数据消息队列
  42. struct serial_configure otaconfig = RT_SERIAL_CONFIG_DEFAULT; /*初始化配置参数*/
  43. rt_device_t otaserial; /*串口设备句柄*/
  44. const char boot_log_buf[] =
  45. " ____ _____ _____ ___ _____ _ \r\n\
  46. | _ \\_ _| | ___/ _ \\ _ _|/ \\ \r\n\
  47. | |_) || |_____ | |_ || || | | / _ \\ \r\n\
  48. | _ < | |_____ | _| ||_|| | |/ ___ \\ \r\n\
  49. |_| \\_\\|_| |_| \\___/ |_/_/ \\_\\ \r\n";
  50. static char msg_pool[256];
  51. static char rx_buffer[256];
  52. static rt_err_t uart_input(rt_device_t dev, rt_size_t size);
  53. extern void HMI_WRITE(char *fmt,...);
  54. int HMI_Thread(void);
  55. static void Hmi_thread_entry(void *parameter);
  56. static rt_thread_t hmi1 = RT_NULL;
  57. /* 串口接收消息结构*/
  58. struct rx_msg
  59. {
  60. rt_device_t dev;
  61. rt_size_t size;
  62. };
  63. void rt_fota_print_log(void)
  64. {
  65. LOG_RAW("%s", boot_log_buf);
  66. LOG_RAW("2016 - 2019 Copyright by Radiation @ warfalcon \r\n");
  67. LOG_RAW("Version: %s build %s\r\n\r\n", RT_FOTA_SW_VERSION, __DATE__);
  68. }
  69. int Ota_uart2_init(void)
  70. {
  71. otaserial = rt_device_find("uart4");
  72. /*修改串口配置参数*/
  73. otaconfig.baud_rate = BAUD_RATE_115200; //修改波特率115200
  74. otaconfig.data_bits = DATA_BITS_8; //数据位8
  75. otaconfig.stop_bits = STOP_BITS_1; //停止位1
  76. otaconfig.bufsz = 1024; //缓冲区1k
  77. otaconfig.parity = PARITY_NONE; //无奇偶校验
  78. /* step3:控制串口设备。通过控制接口传入命令控制字,与控制参数 */
  79. rt_device_control(otaserial, RT_DEVICE_CTRL_CONFIG, &otaconfig);
  80. /* step4:打开串口设备。以中断接收及轮询发送模式打开串口设备 */
  81. if(rt_device_open(otaserial, RT_DEVICE_FLAG_DMA_RX)==RT_EOK){
  82. rt_kprintf("HMI Init Success!!\n");
  83. HMI_WRITE("sleep=%d",1);
  84. HMI_WRITE("sleep=%d",1);
  85. HMI_WRITE("sleep=%d",1);
  86. return 0;
  87. }
  88. else {
  89. rt_kprintf("HMI Init Failed!!\n");
  90. return RT_EOK;
  91. }
  92. }
  93. /* 接收数据回调函数 */
  94. static rt_err_t uart_input(rt_device_t dev, rt_size_t size)
  95. {
  96. struct rx_msg msg;
  97. rt_err_t result;
  98. msg.dev = dev;
  99. msg.size = size;
  100. result = rt_mq_send(&rx_mq, &msg, sizeof(msg));
  101. if ( result == -RT_EFULL)
  102. {
  103. /* 消息队列满 */
  104. rt_kprintf("message queue full!\n");
  105. }
  106. return result;
  107. }
  108. /*串口屏写数据*/
  109. void HMI_WRITE(char *fmt, ...)
  110. {
  111. va_list ap;
  112. //末尾需要拼接的3个ff
  113. const char hmi_hex[2]={0x0D,0X0A};
  114. //末尾需要拼接的3个ff
  115. const char hmi_end[3]={0xff,0xff,0xff};
  116. static char str[MAX_SIZE];
  117. memset(str,'\0',sizeof(str));
  118. rt_size_t str_length;
  119. va_start(ap,fmt);
  120. str_length = rt_vsnprintf(str, sizeof(str) - 1, fmt, ap);
  121. if (str_length > MAX_SIZE - 1)
  122. str_length = MAX_SIZE - 1;
  123. rt_device_write(otaserial, 0, str, str_length);
  124. va_end(ap);
  125. if(strstr(str, "sleep")!=NULL)
  126. {
  127. rt_device_write(otaserial, 0,hmi_end,3);
  128. }
  129. else if (strstr(str, "t7.txt")!=NULL) {
  130. rt_device_write(otaserial, 0,hmi_end,3);
  131. }
  132. else if (strstr(str, "t5.txt")!=NULL) {
  133. rt_device_write(otaserial, 0,hmi_end,3);
  134. }
  135. else if (strstr(str, "update.val")!=NULL) {
  136. rt_device_write(otaserial, 0,hmi_end,3);
  137. }
  138. else {
  139. rt_device_write(otaserial, 0,hmi_hex,2);
  140. }
  141. }
  142. static void HMI_Decode(char *cmd,uint8_t cmd_len)
  143. {
  144. //擦除APP分区,将df_area分区固件搬运至app分区
  145. if(strstr(rx_buffer,"resetapp")!=NULL)
  146. {
  147. rt_kprintf("固件恢复!!!\n");
  148. HMI_WRITE("固件恢复!!!");
  149. const char *dst_part_name = "app";
  150. const char *src_part_name = "df_area";
  151. const struct fal_partition *dst_part;
  152. const struct fal_partition *src_part;
  153. dst_part = fal_partition_find(dst_part_name);
  154. src_part = fal_partition_find(src_part_name);
  155. if (dst_part == RT_NULL || src_part == RT_NULL)
  156. {
  157. if (dst_part == RT_NULL)
  158. rt_kprintf("%s partition is not exist!\n", dst_part_name);
  159. if (src_part == RT_NULL)
  160. rt_kprintf("%s partition is not exist!\n", src_part_name);
  161. }
  162. else
  163. {
  164. rt_kprintf("Clone %s partition to %s partition:\n", src_part_name, dst_part_name);
  165. HMI_WRITE("Clone %s partition to %s partition:", src_part_name, dst_part_name);
  166. if (fal_partition_erase(dst_part, 0, dst_part->len) >= 0)
  167. {
  168. int clone_pos = 0;
  169. int clone_len = 0, clone_tol_len;
  170. static int clone_num;
  171. rt_uint8_t *buf = rt_malloc(4096);
  172. if (dst_part->len < src_part->len)
  173. clone_tol_len = dst_part->len;
  174. else
  175. clone_tol_len = src_part->len;
  176. while ((clone_pos < clone_tol_len) && (buf != RT_NULL))
  177. {
  178. clone_len = fal_partition_read(src_part, clone_pos, buf, 4096);
  179. if (clone_len < 0)
  180. {
  181. rt_kprintf("\nread %s partition failed, clone stop!\n", src_part_name);
  182. break;
  183. }
  184. if (fal_partition_write(dst_part, clone_pos, buf, clone_len) < 0)
  185. {
  186. rt_kprintf("\nwrite %s partition failed, clone stop!\n", dst_part_name);
  187. break;
  188. }
  189. clone_pos += clone_len;
  190. clone_num++;
  191. HMI_WRITE("debug.update.val=%d",clone_num);
  192. if(clone_num==99) clone_num = 0;
  193. }
  194. if (clone_pos >= clone_tol_len)
  195. {
  196. HMI_WRITE("Clone partition success, total %d bytes!", clone_tol_len);
  197. rt_kprintf("\nClone partition success, total %d bytes!\n", clone_tol_len);
  198. HMI_WRITE("debug.update.val=100");
  199. HMI_WRITE("debug.update.val=100");
  200. HMI_WRITE("debug.update.val=100");
  201. }
  202. else
  203. {
  204. HMI_WRITE("Clone partition failed!");
  205. rt_kprintf("Clone partition failed!");
  206. }
  207. if (buf)
  208. rt_free(buf);
  209. }
  210. }
  211. }
  212. //备份app分区至df_area分区
  213. else if(strstr(rx_buffer, "backupapp")!=NULL)
  214. {
  215. }
  216. //启动程序
  217. else if(strstr(rx_buffer, "startup")!=NULL)
  218. {
  219. rt_fota_start_application();
  220. }
  221. //重启bootloader
  222. else if(strstr(rx_buffer, "bootloader")!=NULL)
  223. {
  224. uint8_t boot=1;
  225. if(easyflash_init()==EF_NO_ERR)
  226. {
  227. ef_set_env_blob("stop_in_bootloader", &boot, sizeof(boot));
  228. }
  229. rt_hw_cpu_reset();
  230. }
  231. //擦除app分区
  232. else if (strstr(rx_buffer, "eraseapp")!=NULL) {
  233. struct fal_partition fal_erase;
  234. fal_erase.offset=204800;
  235. rt_sprintf(fal_erase.name, "%s","app");
  236. rt_sprintf(fal_erase.flash_name, "%s","onchip_flash");
  237. fal_partition_erase(&fal_erase,0,319488);
  238. }
  239. //擦除easyflash
  240. else if(strstr(rx_buffer, "eraseeasyflash")!=NULL){
  241. rt_kprintf("erase easyflash...\n");
  242. //uint8_t isave = 0;
  243. ef_env_set_default();
  244. //ef_set_env_blob("iSave", &isave, sizeof(isave));
  245. HMI_WRITE("easyflash擦除成功!");
  246. }
  247. else if(strstr(rx_buffer, "erasedf_area")!=NULL){
  248. struct fal_partition fal_erase;
  249. fal_erase.offset=15729152;
  250. rt_sprintf(fal_erase.name, "%s","df_area");
  251. rt_sprintf(fal_erase.flash_name, "%s","w25q16m");
  252. fal_partition_erase(&fal_erase,0,524288);
  253. }
  254. }
  255. /*HMI*/
  256. int HMI_Thread(void)
  257. {
  258. Ota_uart2_init();
  259. rt_err_t ret = RT_EOK;
  260. /* 初始化消息队列 */
  261. rt_mq_init(&rx_mq, "rx_mq",
  262. msg_pool, /* 存放消息的缓冲区 */
  263. sizeof(struct rx_msg), /* 一条消息的最大长度 */
  264. sizeof(msg_pool), /* 存放消息的缓冲区大小 */
  265. RT_IPC_FLAG_FIFO); /* 如果有多个线程等待,按照先来先得到的方法分配消息 */
  266. /* 创建线程*/
  267. hmi1 = rt_thread_create("Hmi_Fresh_thread",
  268. Hmi_thread_entry,
  269. RT_NULL,
  270. HMI_THREAD_STACK_SIZE,
  271. HMI_THREAD_PRIORITY,
  272. HMI_THREAD_TIMESLICE);
  273. /* 如果获得线程控制块,启动这个线程 */
  274. if (hmi1 != RT_NULL)
  275. {
  276. rt_thread_startup(hmi1);
  277. }
  278. else
  279. {
  280. ret = RT_ERROR;
  281. }
  282. return ret;
  283. }
  284. /*入口函数*/
  285. static void Hmi_thread_entry(void *parameter)
  286. {
  287. /* 设置接收回调函数 */
  288. rt_device_set_rx_indicate(otaserial, uart_input);
  289. struct rx_msg msg;
  290. rt_err_t result;
  291. rt_uint32_t rx_length;
  292. rt_uint32_t start_timeout = 0;
  293. ef_set_env_blob("driver_bl", &BL_VERSION, sizeof(BL_VERSION));
  294. ef_set_env_blob("driver_compile", &COMPILE, sizeof(COMPILE));
  295. HMI_WRITE("debug.t5.txt=\"%s\"",BL_VERSION);
  296. HMI_WRITE("debug.t7.txt=\"%s\"",COMPILE);
  297. while(1)
  298. {
  299. rt_memset(&msg, 0, sizeof(msg));
  300. /* 从消息队列中读取消息*/
  301. result = rt_mq_recv(&rx_mq, &msg, sizeof(msg), 1000);
  302. if (result == RT_EOK)
  303. {
  304. rx_length = rt_device_read(msg.dev, 0, rx_buffer, msg.size);
  305. rx_buffer[rx_length] = '\0';
  306. LOG_I("%s\n",rx_buffer);
  307. HMI_Decode(rx_buffer,rx_length);
  308. }
  309. else{
  310. //rt_device_write(otaserial, 0, &rx_buffer[0], rx_length);
  311. HMI_WRITE("bootloader mode");
  312. //rt_kprintf("bootloader mode");
  313. start_timeout++;
  314. if(start_timeout==300)
  315. {
  316. rt_fota_start_application();
  317. start_timeout = 0;
  318. }
  319. }
  320. /*********************************************************************************************************************/
  321. //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);
  322. //rt_kprintf("workSta.vbat.txt=%c%d.%02d%c\n",hmi_str,voltage/100,voltage%100,hmi_str);
  323. }
  324. }
  325. int main(void)
  326. {
  327. #if defined(RT_USING_FINSH) && defined(FINSH_USING_MSH)
  328. finsh_set_prompt("rt-fota />");
  329. #endif
  330. extern void rt_fota_init(void);
  331. easyflash_init();
  332. rt_fota_init();
  333. HMI_Thread();
  334. return RT_EOK;
  335. }
  336. //INIT_DEVICE_EXPORT(Ota_uart2_init);