main.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  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 RT_FOTA_SW_VERSION
  34. #define COMPILE "20261029"
  35. #define RTU_DTU GET_PIN(B,13)
  36. #define Beep GET_PIN(B, 4)
  37. #define HMI_THREAD_PRIORITY 5 //线程优先级
  38. #define HMI_THREAD_STACK_SIZE 2048 //线程栈空间大小
  39. #define HMI_THREAD_TIMESLICE 5 //时间片
  40. #define MAX_SIZE 512
  41. /* 消息队列控制块 */
  42. static struct rt_messagequeue rx_mq;//接收数据消息队列
  43. struct serial_configure otaconfig = RT_SERIAL_CONFIG_DEFAULT; /*初始化配置参数*/
  44. rt_device_t otaserial; /*串口设备句柄*/
  45. const char boot_log_buf[] =
  46. " ____ _____ _____ ___ _____ _ \r\n\
  47. | _ \\_ _| | ___/ _ \\ _ _|/ \\ \r\n\
  48. | |_) || |_____ | |_ || || | | / _ \\ \r\n\
  49. | _ < | |_____ | _| ||_|| | |/ ___ \\ \r\n\
  50. |_| \\_\\|_| |_| \\___/ |_/_/ \\_\\ \r\n";
  51. static char msg_pool[256];
  52. static char rx_buffer[256];
  53. static rt_err_t uart_input(rt_device_t dev, rt_size_t size);
  54. extern void HMI_WRITE(char *fmt,...);
  55. int HMI_Thread(void);
  56. static void Hmi_thread_entry(void *parameter);
  57. static rt_thread_t hmi1 = RT_NULL;
  58. /* 串口接收消息结构*/
  59. struct rx_msg
  60. {
  61. rt_device_t dev;
  62. rt_size_t size;
  63. };
  64. void rt_fota_print_log(void)
  65. {
  66. LOG_RAW("%s", boot_log_buf);
  67. LOG_RAW("2016 - 2019 Copyright by Radiation @ warfalcon \r\n");
  68. LOG_RAW("Version: %s build %s\r\n\r\n", RT_FOTA_SW_VERSION, __DATE__);
  69. }
  70. void Beep_Tip(uint16_t volnum)
  71. {
  72. rt_pin_mode(Beep, PIN_MODE_OUTPUT);
  73. rt_pin_write(Beep, 1);
  74. rt_thread_mdelay(volnum);
  75. rt_pin_write(Beep, 0);
  76. }
  77. int Ota_uart2_init(void)
  78. {
  79. otaserial = rt_device_find("uart4");
  80. /*修改串口配置参数*/
  81. otaconfig.baud_rate = BAUD_RATE_115200; //修改波特率115200
  82. otaconfig.data_bits = DATA_BITS_8; //数据位8
  83. otaconfig.stop_bits = STOP_BITS_1; //停止位1
  84. otaconfig.bufsz = 1024; //缓冲区1k
  85. otaconfig.parity = PARITY_NONE; //无奇偶校验
  86. /* step3:控制串口设备。通过控制接口传入命令控制字,与控制参数 */
  87. rt_device_control(otaserial, RT_DEVICE_CTRL_CONFIG, &otaconfig);
  88. /* step4:打开串口设备。以中断接收及轮询发送模式打开串口设备 */
  89. if(rt_device_open(otaserial, RT_DEVICE_FLAG_DMA_RX)==RT_EOK){
  90. rt_kprintf("HMI Init Success!!\n");
  91. return 0;
  92. }
  93. else {
  94. rt_kprintf("HMI Init Failed!!\n");
  95. return RT_EOK;
  96. }
  97. }
  98. /* 接收数据回调函数 */
  99. static rt_err_t uart_input(rt_device_t dev, rt_size_t size)
  100. {
  101. struct rx_msg msg;
  102. rt_err_t result;
  103. msg.dev = dev;
  104. msg.size = size;
  105. result = rt_mq_send(&rx_mq, &msg, sizeof(msg));
  106. if ( result == -RT_EFULL)
  107. {
  108. /* 消息队列满 */
  109. rt_kprintf("message queue full!\n");
  110. }
  111. return result;
  112. }
  113. /*串口屏写数据*/
  114. void HMI_WRITE(char *fmt, ...)
  115. {
  116. va_list ap;
  117. //末尾需要拼接的3个ff
  118. const char hmi_hex[2]={0x0D,0X0A};
  119. //末尾需要拼接的3个ff
  120. const char hmi_end[3]={0xff,0xff,0xff};
  121. static char str[MAX_SIZE];
  122. memset(str,'\0',sizeof(str));
  123. rt_size_t str_length;
  124. va_start(ap,fmt);
  125. str_length = rt_vsnprintf(str, sizeof(str) - 1, fmt, ap);
  126. if (str_length > MAX_SIZE - 1)
  127. str_length = MAX_SIZE - 1;
  128. rt_device_write(otaserial, 0, str, str_length);
  129. va_end(ap);
  130. if(strstr(str, "sleep")!=NULL)
  131. {
  132. rt_device_write(otaserial, 0,hmi_end,3);
  133. }
  134. else if (strstr(str, "t7.txt")!=NULL) {
  135. rt_device_write(otaserial, 0,hmi_end,3);
  136. }
  137. else if (strstr(str, "t5.txt")!=NULL) {
  138. rt_device_write(otaserial, 0,hmi_end,3);
  139. }
  140. else if (strstr(str, "update.val")!=NULL) {
  141. rt_device_write(otaserial, 0,hmi_end,3);
  142. }
  143. else {
  144. rt_device_write(otaserial, 0,hmi_hex,2);
  145. }
  146. }
  147. static void HMI_Decode(char *cmd,uint8_t cmd_len)
  148. {
  149. //擦除APP分区并从df_area备份恢复固件(v1.3.0:校验 magic/CRC 后恢复,保留备份)
  150. if(strstr(rx_buffer,"resetapp")!=NULL)
  151. {
  152. rt_kprintf("固件恢复!!!\n");
  153. HMI_WRITE("固件恢复!!!");
  154. if (rt_fota_restore_app_from_df(0) != RT_FOTA_NO_ERR)
  155. {
  156. rt_kprintf("resetapp: no valid backup or restore failed!\n");
  157. HMI_WRITE("恢复失败:无有效备份!");
  158. }
  159. else
  160. {
  161. rt_kprintf("resetapp done.\n");
  162. HMI_WRITE("固件恢复完成!");
  163. }
  164. }
  165. //备份app分区至df_area分区(v1.3.0)
  166. else if(strstr(rx_buffer, "backupapp")!=NULL)
  167. {
  168. rt_kprintf("backupapp: backup app partition to df_area...\n");
  169. HMI_WRITE("备份固件...");
  170. if (rt_fota_backup_app_to_df() != RT_FOTA_NO_ERR)
  171. {
  172. rt_kprintf("backupapp failed!\n");
  173. HMI_WRITE("备份失败!");
  174. }
  175. else
  176. {
  177. rt_kprintf("backupapp done.\n");
  178. HMI_WRITE("固件备份完成!");
  179. }
  180. }
  181. //从df_area备份恢复app分区(v1.3.0 调试命令,保留备份方便重复执行)
  182. else if(strstr(rx_buffer, "dbg_restore")!=NULL)
  183. {
  184. rt_kprintf("dbg_restore: restore app partition from df_area backup...\n");
  185. HMI_WRITE("恢复固件...");
  186. if (rt_fota_restore_app_from_df(0) != RT_FOTA_NO_ERR)
  187. {
  188. rt_kprintf("dbg_restore: no valid backup or restore failed!\n");
  189. HMI_WRITE("恢复失败:无有效备份!");
  190. }
  191. else
  192. {
  193. rt_kprintf("dbg_restore done.\n");
  194. HMI_WRITE("固件恢复完成!");
  195. }
  196. }
  197. //启动程序
  198. else if(strstr(rx_buffer, "startup")!=NULL)
  199. {
  200. rt_fota_start_application();
  201. }
  202. //重启bootloader
  203. else if(strstr(rx_buffer, "bootloader")!=NULL)
  204. {
  205. uint8_t boot=1;
  206. if(easyflash_init()==EF_NO_ERR)
  207. {
  208. ef_set_env_blob("stop_in_bootloader", &boot, sizeof(boot));
  209. }
  210. rt_hw_cpu_reset();
  211. }
  212. //擦除app分区
  213. else if (strstr(rx_buffer, "eraseapp")!=NULL) {
  214. const struct fal_partition *fal_erase;
  215. fal_erase = fal_partition_find("app");
  216. if (fal_erase != RT_NULL)
  217. {
  218. rt_uint32_t erased = 0, len;
  219. rt_kprintf("eraseapp: erase %s partition(%d bytes)...\n", fal_erase->name, fal_erase->len);
  220. /* 分块擦除 + 逐块喂狗:一次擦 312KB 的 onchip flash 需要数秒,
  221. * 单次调用会长时间不给 IWDG 喂狗,导致擦到一半被看门狗复位 */
  222. while (erased < (rt_uint32_t)fal_erase->len)
  223. {
  224. len = fal_erase->len - erased;
  225. if (len > 4096)
  226. len = 4096;
  227. if (fal_partition_erase(fal_erase, erased, len) < 0)
  228. break;
  229. erased += len;
  230. rt_fota_wdt_feed();
  231. }
  232. if (erased < (rt_uint32_t)fal_erase->len)
  233. rt_kprintf("eraseapp failed!\n");
  234. else
  235. rt_kprintf("eraseapp done.\n");
  236. }
  237. else
  238. {
  239. rt_kprintf("eraseapp: 'app' partition is not exist!\n");
  240. }
  241. }
  242. //擦除easyflash
  243. else if(strstr(rx_buffer, "eraseeasyflash")!=NULL){
  244. rt_kprintf("erase easyflash...\n");
  245. //uint8_t isave = 0;
  246. ef_env_set_default();
  247. //ef_set_env_blob("iSave", &isave, sizeof(isave));
  248. HMI_WRITE("easyflash擦除成功!");
  249. }
  250. else if(strstr(rx_buffer, "erasedf_area")!=NULL){
  251. const struct fal_partition *fal_erase;
  252. fal_erase = fal_partition_find("df_area");
  253. if (fal_erase != RT_NULL)
  254. {
  255. rt_uint32_t erased = 0, len;
  256. rt_kprintf("erasedf_area: erase %s partition(%d bytes)...\n", fal_erase->name, fal_erase->len);
  257. /* 分块擦除 + 逐块喂狗:512KB NOR 逐扇区擦最坏要几十秒,
  258. * 一次性调用必然触发 IWDG 10s 超时复位 */
  259. while (erased < (rt_uint32_t)fal_erase->len)
  260. {
  261. len = fal_erase->len - erased;
  262. if (len > 4096)
  263. len = 4096;
  264. if (fal_partition_erase(fal_erase, erased, len) < 0)
  265. break;
  266. erased += len;
  267. rt_fota_wdt_feed();
  268. }
  269. if (erased < (rt_uint32_t)fal_erase->len)
  270. rt_kprintf("erasedf_area failed!\n");
  271. else
  272. rt_kprintf("erasedf_area done.\n");
  273. }
  274. else
  275. {
  276. rt_kprintf("erasedf_area: 'df_area' partition is not exist!\n");
  277. }
  278. }
  279. }
  280. /*HMI*/
  281. int HMI_Thread(void)
  282. {
  283. Ota_uart2_init();
  284. rt_err_t ret = RT_EOK;
  285. /* 初始化消息队列 */
  286. rt_mq_init(&rx_mq, "rx_mq",
  287. msg_pool, /* 存放消息的缓冲区 */
  288. sizeof(struct rx_msg), /* 一条消息的最大长度 */
  289. sizeof(msg_pool), /* 存放消息的缓冲区大小 */
  290. RT_IPC_FLAG_FIFO); /* 如果有多个线程等待,按照先来先得到的方法分配消息 */
  291. /* 创建线程*/
  292. hmi1 = rt_thread_create("Hmi_Fresh_thread",
  293. Hmi_thread_entry,
  294. RT_NULL,
  295. HMI_THREAD_STACK_SIZE,
  296. HMI_THREAD_PRIORITY,
  297. HMI_THREAD_TIMESLICE);
  298. /* 如果获得线程控制块,启动这个线程 */
  299. if (hmi1 != RT_NULL)
  300. {
  301. rt_thread_startup(hmi1);
  302. }
  303. else
  304. {
  305. ret = RT_ERROR;
  306. }
  307. return ret;
  308. }
  309. /*入口函数*/
  310. static void Hmi_thread_entry(void *parameter)
  311. {
  312. /* 设置接收回调函数 */
  313. rt_device_set_rx_indicate(otaserial, uart_input);
  314. struct rx_msg msg;
  315. rt_err_t result;
  316. rt_uint32_t rx_length;
  317. rt_uint32_t start_timeout = 0;
  318. ef_set_env_blob("driver_bl", &BL_VERSION, sizeof(BL_VERSION));
  319. ef_set_env_blob("driver_compile", &COMPILE, sizeof(COMPILE));
  320. HMI_WRITE("debug.t5.txt=\"%s\"",BL_VERSION);
  321. HMI_WRITE("debug.t7.txt=\"%s\"",COMPILE);
  322. while(1)
  323. {
  324. rt_memset(&msg, 0, sizeof(msg));
  325. /* 从消息队列中读取消息*/
  326. result = rt_mq_recv(&rx_mq, &msg, sizeof(msg), 1000);
  327. if (result == RT_EOK)
  328. {
  329. /* 防止越界:msg.size 为环形缓冲区累计未读长度,可能超过 rx_buffer 容量 */
  330. if (msg.size > sizeof(rx_buffer) - 1)
  331. {
  332. LOG_W("HMI rx data truncated: %d > %d", (int)msg.size, (int)(sizeof(rx_buffer) - 1));
  333. msg.size = sizeof(rx_buffer) - 1;
  334. }
  335. rx_length = rt_device_read(msg.dev, 0, rx_buffer, msg.size);
  336. if (rx_length > sizeof(rx_buffer) - 1)
  337. {
  338. rx_length = sizeof(rx_buffer) - 1;
  339. }
  340. rx_buffer[rx_length] = '\0';
  341. LOG_I("%s\n",rx_buffer);
  342. HMI_Decode(rx_buffer,rx_length);
  343. }
  344. else{
  345. //rt_device_write(otaserial, 0, &rx_buffer[0], rx_length);
  346. HMI_WRITE("bootloader mode");
  347. //rt_kprintf("bootloader mode");
  348. start_timeout++;
  349. if(start_timeout==300)
  350. {
  351. rt_fota_start_application();
  352. start_timeout = 0;
  353. }
  354. }
  355. /*********************************************************************************************************************/
  356. //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);
  357. //rt_kprintf("workSta.vbat.txt=%c%d.%02d%c\n",hmi_str,voltage/100,voltage%100,hmi_str);
  358. }
  359. }
  360. int main(void)
  361. {
  362. #if defined(RT_USING_FINSH) && defined(FINSH_USING_MSH)
  363. finsh_set_prompt("rt-fota />");
  364. #endif
  365. extern void rt_fota_init(void);
  366. easyflash_init();
  367. rt_fota_print_log();
  368. rt_fota_init();
  369. HMI_Thread();
  370. Beep_Tip(100);
  371. return RT_EOK;
  372. }
  373. //INIT_DEVICE_EXPORT(Ota_uart2_init);