usb_ota.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720
  1. #define LOG_TAG "usb_ota" // 该模块对应的标签。不定义时,默认:NO_TAG
  2. #define LOG_LVL LOG_LVL_ASSERT // 该模块对应的日志输出级别。不定义时,默认:调试级别
  3. #include <ulog.h> // 必须在 LOG_TAG 与 LOG_LVL 下面
  4. #include <stdio.h>
  5. #include <stdint.h>
  6. #include <stdlib.h>
  7. #include <rtthread.h>
  8. #include <finsh.h>
  9. #include <fal.h>
  10. #include "sys_config.h"
  11. #include "CH376HFT.h"
  12. #include "protocol.h"
  13. #define USB_OTA_BUFF_LEN 8192
  14. #define USB_OTA_DL_DELAY (10 * RT_TICK_PER_SECOND)
  15. #define DEFAULT_DOWNLOAD_PART "fm_area"
  16. #define STATUS_DIS_LEN 100 //显示缓冲区长度
  17. #define DIS_POS "reset.t3.txt=\"%s\""
  18. #define DIS_POS_value "reset.t3.txt=\"%s%d\%\""
  19. #define DIS_POS_progress "reset.j0.val=\%d"
  20. #define pagebootloader "page bootloader"
  21. #define u_succ "U盘检测成功"
  22. #define u_fail "U盘检测失败"
  23. //#define reboot_5s "配置成功,5秒后重启"
  24. #define reboot_5s "系统将在5秒后重启"
  25. #define find_file "检测到文件"
  26. #define find_file_1 "检测到文件1"
  27. #define find_file_2 "检测到文件2"
  28. #define find_file_3 "检测到文件3"
  29. #define find_file_4 "检测到文件4"
  30. #define notfound_file "未找到文件"
  31. #define notfound_file_1 "未找到文件1"
  32. #define notfound_file_2 "未找到文件2"
  33. #define notfound_file_3 "未找到文件3"
  34. #define notfound_file_4 "未找到文件4"
  35. #define filesize "升级文件大小:"
  36. #define startwritfw "准备写入固件"
  37. #define writingfw "正在写入固件"
  38. #define finddownload_part "找到固件分区,准备格式化"
  39. #define nodownload_part "未找到固件分区"
  40. #define erase_fail "固件分区格式化失败"
  41. #define erase_succ "固件分区格式化成功"
  42. #define malloc_fail "分配内存失败"
  43. #define write_error "固件写入错误"
  44. #define writefail "固件写入失败"
  45. #define writesucc "固件写入成功"
  46. #define file_size_i "文件超过限定大小"
  47. #define file_size_0 "文件大小为0"
  48. #define ch_config_succ "通道配置成功"
  49. #define ch_config_fail "通道配置失败"
  50. #define eleconfig_succ "要素模型写入成功"
  51. #define eleconfig_fail "要素模型写入失败"
  52. #define gbk_succ "中文支持库写入成功"
  53. #define gbk_fail "中文支持库写入失败"
  54. extern int16_t CH376_Init(void);
  55. extern rt_err_t HMI_Display(void *Data, rt_size_t length);
  56. /*屏幕显示相应的操作状态*/
  57. static void Status_Display(const char *content, uint16_t value)
  58. {
  59. char *dis_buf = RT_NULL;
  60. dis_buf = rt_malloc(STATUS_DIS_LEN);
  61. rt_memset(dis_buf, '\0', STATUS_DIS_LEN);
  62. if ((value == RT_NULL) && (content != RT_NULL)) //显示文字
  63. {
  64. rt_sprintf(dis_buf, DIS_POS, content);
  65. }
  66. else if ((value != RT_NULL) && (content != RT_NULL)) //显示百分比状态
  67. {
  68. rt_sprintf(dis_buf, DIS_POS_value, content, value);
  69. }
  70. else if ((value != RT_NULL) && (content == RT_NULL)) //显示进度条
  71. {
  72. rt_sprintf(dis_buf, DIS_POS_progress, value);
  73. // LOG_I("%s\r\n",dis_buf);
  74. }
  75. else //切换到BootLoader页面
  76. {
  77. rt_sprintf(dis_buf, pagebootloader);
  78. }
  79. HMI_Display(dis_buf, rt_strlen(dis_buf)); //存入队列
  80. rt_free(dis_buf);
  81. dis_buf = RT_NULL;
  82. }
  83. static void print_progress(size_t cur_size, size_t total_size)
  84. {
  85. static unsigned char progress_sign[100 + 1];
  86. uint8_t i, per = cur_size * 100 / total_size;
  87. if (per > 100)
  88. {
  89. per = 100;
  90. }
  91. for (i = 0; i < 100; i++)
  92. {
  93. if (i < per)
  94. {
  95. progress_sign[i] = '=';
  96. }
  97. else if (per == i)
  98. {
  99. progress_sign[i] = '>';
  100. }
  101. else
  102. {
  103. progress_sign[i] = ' ';
  104. }
  105. }
  106. Status_Display(writingfw, per);
  107. Status_Display(RT_NULL, per);
  108. progress_sign[sizeof(progress_sign) - 1] = '\0';
  109. LOG_I("\033[2A");
  110. LOG_I("Download: [%s] %d%%", progress_sign, per);
  111. }
  112. int usb_read(unsigned char *buffer, size_t length) //
  113. {
  114. int s = 0;
  115. uint32_t RealCount = 0;
  116. s = CH376ByteRead(buffer, length, (PUINT16)&RealCount);
  117. if (s != USB_INT_SUCCESS)
  118. {
  119. mStopIfError(s);
  120. return 0;
  121. }
  122. return RealCount;
  123. }
  124. #include "hc_ctl.h"
  125. static int usb_ota_fw_download(void)
  126. {
  127. int ret = 0, s = 0, resp_status;
  128. uint32_t file_size = 0, length, total_length = 0;
  129. rt_uint8_t *buffer_read = RT_NULL;
  130. const struct fal_partition *dl_part = RT_NULL;
  131. uint8_t filename[20];
  132. if (CH376_Init() != 1)
  133. {
  134. Status_Display(u_fail, RT_NULL);
  135. LOG_E("the usb drive init fail!");
  136. return ret;
  137. }
  138. if (CH376DiskConnect() == USB_INT_SUCCESS) /* 检查U盘是否连接 */
  139. {
  140. Status_Display(u_succ, RT_NULL);
  141. }
  142. else
  143. {
  144. Status_Display(u_fail, RT_NULL);
  145. LOG_E("the usb drive is not read!");
  146. return ret;
  147. }
  148. rt_sprintf((char *)filename, "/QXZ.RBL");
  149. s = CH376FileOpen(filename); /* 打开文件,该文件在根目录下 */
  150. if (s == ERR_MISS_FILE) /* 文件不存在*/
  151. {
  152. Status_Display(notfound_file, RT_NULL);
  153. LOG_E("the ota file is not find!");
  154. return ret;
  155. }
  156. else if (s == USB_INT_SUCCESS)
  157. {
  158. Status_Display(find_file, RT_NULL);
  159. file_size = CH376GetFileSize();
  160. rt_kprintf("File size = %ld\n", file_size);
  161. }
  162. Status_Display(DIS_POS_value, file_size);
  163. /* Get download partition information and erase download partition data */
  164. if ((dl_part = fal_partition_find(DEFAULT_DOWNLOAD_PART)) == RT_NULL)
  165. {
  166. Status_Display(nodownload_part, RT_NULL);
  167. LOG_E("Firmware download failed! Partition (%s) find error!", DEFAULT_DOWNLOAD_PART);
  168. ret = -RT_ERROR;
  169. return ret;
  170. }
  171. LOG_I("Start erase flash (%s) partition!", dl_part->name);
  172. Status_Display(finddownload_part, RT_NULL);
  173. if (fal_partition_erase(dl_part, 0, file_size) < 0)
  174. {
  175. Status_Display(erase_fail, RT_NULL);
  176. LOG_E("Firmware download failed! Partition (%s) erase error!", dl_part->name);
  177. ret = -RT_ERROR;
  178. return ret;
  179. }
  180. Status_Display(erase_succ, RT_NULL);
  181. LOG_I("Erase flash (%s) partition success!", dl_part->name);
  182. buffer_read = rt_malloc(USB_OTA_BUFF_LEN);
  183. if (buffer_read == RT_NULL)
  184. {
  185. Status_Display(malloc_fail, RT_NULL);
  186. LOG_E("No memory for usb ota!");
  187. ret = -RT_ERROR;
  188. return ret;
  189. }
  190. rt_memset(buffer_read, 0x00, USB_OTA_BUFF_LEN);
  191. // list_mem();
  192. Status_Display(startwritfw, RT_NULL);
  193. do
  194. {
  195. length = usb_read(buffer_read, file_size - total_length > USB_OTA_BUFF_LEN ? USB_OTA_BUFF_LEN : file_size - total_length);
  196. if (length > 0)
  197. {
  198. /* Write the data to the corresponding partition address */
  199. if (fal_partition_write(dl_part, total_length, buffer_read, length) < 0)
  200. {
  201. Status_Display(write_error, RT_NULL);
  202. LOG_E("Firmware download failed! Partition (%s) write data error!", dl_part->name);
  203. ret = -RT_ERROR;
  204. goto __exit;
  205. }
  206. total_length += length;
  207. print_progress(total_length, file_size);
  208. // list_mem();
  209. }
  210. else
  211. {
  212. Status_Display(writefail, RT_NULL);
  213. LOG_E("Exit: USB OTA return err (%d)!", length);
  214. ret = -RT_ERROR;
  215. goto __exit;
  216. }
  217. } while (total_length != file_size);
  218. ret = RT_EOK;
  219. if (total_length == file_size)
  220. {
  221. if (buffer_read)
  222. {
  223. rt_free(buffer_read);
  224. buffer_read = RT_NULL;
  225. }
  226. Status_Display(writesucc, RT_NULL);
  227. LOG_I("Download firmware to flash success.");
  228. LOG_I("System now will restart...");
  229. rt_thread_delay(rt_tick_from_millisecond(500));
  230. Status_Display("\0", RT_NULL); //显示状态清空
  231. Status_Display(RT_NULL, RT_NULL); //切换到bootloader页面
  232. /* Reset the device, Start new firmware */
  233. // extern void rt_hw_cpu_reset(void);
  234. // rt_hw_cpu_reset();
  235. extern void RESET_SYSTEM(void);
  236. RESET_SYSTEM();
  237. }
  238. __exit:
  239. if (buffer_read)
  240. {
  241. rt_free(buffer_read);
  242. buffer_read = RT_NULL;
  243. }
  244. return ret;
  245. }
  246. #include "hc_ctl.h"
  247. static int usb_ele_config_download(void)
  248. {
  249. int ret = 0, s = 0, i, resp_status;
  250. uint32_t file_size = 0, length, total_length = 0;
  251. char *buffer_read = RT_NULL;
  252. char *file_buffer = RT_NULL;
  253. char *file_buffer_temp = RT_NULL;
  254. const struct fal_partition *dl_part = RT_NULL;
  255. uint8_t filename[20];
  256. if (CH376_Init() != 1)
  257. {
  258. Status_Display(u_fail, RT_NULL);
  259. LOG_E("the usb drive init fail!");
  260. return ret;
  261. }
  262. if (CH376DiskConnect() == USB_INT_SUCCESS) /* 检查U盘是否连接 */
  263. {
  264. Status_Display(u_succ, RT_NULL);
  265. }
  266. else
  267. {
  268. Status_Display(u_fail, RT_NULL);
  269. LOG_E("the usb drive is not read!");
  270. goto __exit;
  271. }
  272. for (i = 0; i < 4; i++)
  273. { //文件有两个,名称为CONFIG01.C,CONFIG02.C,CONFIG03.C
  274. rt_sprintf((char *)filename, "/CONFIG%02d.C", i + 1);
  275. s = CH376FileOpen(filename); /* 打开文件,该文件在根目录下 */
  276. LOG_I("the opening config file :%s", filename);
  277. if (s == ERR_MISS_FILE) /* 文件不存在*/
  278. {
  279. if (i == 0)
  280. Status_Display(notfound_file_1, RT_NULL);
  281. else if (i == 1)
  282. Status_Display(notfound_file_2, RT_NULL);
  283. else if (i == 2)
  284. Status_Display(notfound_file_3, RT_NULL);
  285. else if (i == 3)
  286. Status_Display(notfound_file_4, RT_NULL);
  287. LOG_I("the config file :%s is not find!", filename);
  288. rt_thread_delay(rt_tick_from_millisecond(2000)); //状态显示维持2s
  289. Status_Display("\0", RT_NULL); //显示状态清空
  290. goto __exit;
  291. }
  292. else if (s == USB_INT_SUCCESS)
  293. {
  294. if (i == 0)
  295. Status_Display(find_file_1, RT_NULL);
  296. else if (i == 1)
  297. Status_Display(find_file_2, RT_NULL);
  298. else if (i == 2)
  299. Status_Display(find_file_3, RT_NULL);
  300. else if (i == 3)
  301. Status_Display(find_file_4, RT_NULL);
  302. file_size = CH376GetFileSize();
  303. LOG_I("File size = %ld\n", file_size);
  304. if (file_size == 0)
  305. {
  306. ret = -RT_ERROR;
  307. Status_Display(file_size_0, RT_NULL); //屏幕显示模型文件超过限定大小
  308. LOG_I("File size is 0");
  309. goto __exit;
  310. }
  311. if (file_size > 6 * 1024)
  312. {
  313. ret = -RT_ERROR;
  314. Status_Display(file_size_i, RT_NULL); //屏幕显示模型文件超过限定大小
  315. LOG_I("File size to large");
  316. goto __exit;
  317. } //文件过大直接退出
  318. }
  319. Status_Display(DIS_POS_value, file_size);
  320. file_buffer = (char *)rt_malloc(file_size);
  321. if (file_buffer == RT_NULL)
  322. {
  323. Status_Display(malloc_fail, RT_NULL);
  324. LOG_E("No memory for file_buffer!");
  325. ret = -RT_ERROR;
  326. goto __exit;
  327. }
  328. buffer_read = (char *)rt_malloc(USB_OTA_BUFF_LEN);
  329. if (buffer_read == RT_NULL)
  330. {
  331. Status_Display(malloc_fail, RT_NULL);
  332. LOG_E("No memory for buffer_read!");
  333. ret = -RT_ERROR;
  334. goto __exit;
  335. }
  336. rt_memset(buffer_read, 0, USB_OTA_BUFF_LEN);
  337. rt_memset(file_buffer, 0, file_size);
  338. file_buffer_temp = file_buffer;
  339. total_length = 0; //清零文件读取位置,准备再次读取
  340. do
  341. {
  342. length = usb_read((uint8_t *)buffer_read, file_size - total_length > USB_OTA_BUFF_LEN ? USB_OTA_BUFF_LEN : file_size - total_length);
  343. if (length > 0)
  344. {
  345. rt_memcpy(file_buffer_temp, buffer_read, length);
  346. total_length += length;
  347. file_buffer_temp += length;
  348. }
  349. else
  350. {
  351. Status_Display(writefail, RT_NULL);
  352. LOG_E("Exit: USB CONFIG return err (%d)!", length);
  353. ret = -RT_ERROR;
  354. break;
  355. }
  356. } while (total_length != file_size);
  357. CH376FileClose(0); //关闭文件
  358. //要素模型下发解析
  359. if (cJSON_to_struct_array((char *)file_buffer) == 1)
  360. {
  361. Status_Display(eleconfig_succ, RT_NULL);
  362. LOG_I("ELEMENT CONFIG SUCUCC");
  363. rt_thread_delay(rt_tick_from_millisecond(1000)); //状态显示维持2s
  364. }
  365. else
  366. {
  367. Status_Display(eleconfig_fail, RT_NULL);
  368. LOG_I("ELEMENT CONFIG FAIL");
  369. rt_thread_delay(rt_tick_from_millisecond(1000)); //状态显示维持2s
  370. }
  371. if (file_buffer)
  372. {
  373. rt_free(file_buffer);
  374. file_buffer = RT_NULL;
  375. } //每个文件解析完成后先释放
  376. if (buffer_read)
  377. {
  378. rt_free(buffer_read);
  379. buffer_read = RT_NULL;
  380. }
  381. }
  382. rt_thread_delay(rt_tick_from_millisecond(2000)); //状态显示维持2s
  383. Status_Display("\0", RT_NULL); //显示状态清空
  384. ret = RT_EOK;
  385. __exit:
  386. return ret;
  387. }
  388. static int usb_ch_config(void)
  389. {
  390. int ret = 0, s = 0, resp_status;
  391. uint32_t file_size = 0, length, total_length = 0;
  392. char *buffer_read = RT_NULL;
  393. char *file_buffer = RT_NULL;
  394. char *file_buffer_temp = RT_NULL;
  395. const struct fal_partition *dl_part = RT_NULL;
  396. uint8_t filename[20];
  397. if (CH376_Init() != 1)
  398. {
  399. Status_Display(u_fail, RT_NULL);
  400. LOG_E("the usb drive init fail!");
  401. return ret;
  402. }
  403. if (CH376DiskConnect() == USB_INT_SUCCESS) /* 检查U盘是否连接 */
  404. {
  405. Status_Display(u_succ, RT_NULL);
  406. }
  407. else
  408. {
  409. Status_Display(u_fail, RT_NULL);
  410. LOG_E("the usb drive is not read!");
  411. goto __exit;
  412. }
  413. rt_sprintf((char *)filename, "/CONFIGCH.C");
  414. s = CH376FileOpen(filename); /* 打开文件,该文件在根目录下 */
  415. LOG_I("the opening config file :%s", filename);
  416. if (s == ERR_MISS_FILE) /* 文件不存在*/
  417. {
  418. Status_Display(notfound_file, RT_NULL);
  419. LOG_I("the CONFIG file :%s is not find!", filename);
  420. goto __exit;
  421. }
  422. else if (s == USB_INT_SUCCESS)
  423. {
  424. Status_Display(find_file, RT_NULL);
  425. file_size = CH376GetFileSize();
  426. LOG_I("File size = %ld\n", file_size);
  427. if (file_size > 6 * 1024)
  428. {
  429. ret = -RT_ERROR;
  430. Status_Display(file_size_i, RT_NULL); //屏幕显示模型文件超过限定大小
  431. LOG_I("File size to large");
  432. goto __exit;
  433. } //文件过大直接退出
  434. }
  435. Status_Display(DIS_POS_value, file_size);
  436. file_buffer = rt_malloc(file_size);
  437. if (file_buffer == RT_NULL)
  438. {
  439. Status_Display(malloc_fail, RT_NULL);
  440. LOG_E("No memory for file_buffer!");
  441. ret = -RT_ERROR;
  442. goto __exit;
  443. }
  444. buffer_read = rt_malloc(USB_OTA_BUFF_LEN);
  445. if (buffer_read == RT_NULL)
  446. {
  447. Status_Display(malloc_fail, RT_NULL);
  448. LOG_E("No memory for buffer_read!");
  449. ret = -RT_ERROR;
  450. goto __exit;
  451. }
  452. rt_memset(buffer_read, 0, USB_OTA_BUFF_LEN);
  453. rt_memset(file_buffer, 0, file_size);
  454. file_buffer_temp = file_buffer;
  455. total_length = 0; //清零文件读取位置,准备再次读取
  456. do
  457. {
  458. length = usb_read((uint8_t *)buffer_read, file_size - total_length > USB_OTA_BUFF_LEN ? USB_OTA_BUFF_LEN : file_size - total_length);
  459. if (length > 0)
  460. {
  461. rt_memcpy(file_buffer_temp, buffer_read, length);
  462. total_length += length;
  463. file_buffer_temp += length;
  464. }
  465. else
  466. {
  467. Status_Display(writefail, RT_NULL);
  468. LOG_E("Exit: USB CONFIG return err (%d)!", length);
  469. ret = -RT_ERROR;
  470. break;
  471. }
  472. } while (total_length != file_size);
  473. CH376FileClose(0); //关闭文件
  474. //通道配置解析保存
  475. if (ch_config_parse((char *)file_buffer) == 1)
  476. {
  477. Status_Display(ch_config_succ, RT_NULL); //显示配置通道成功
  478. }
  479. else
  480. {
  481. Status_Display(ch_config_fail, RT_NULL); //显示配置通道失败
  482. }
  483. rt_thread_delay(rt_tick_from_millisecond(2000)); //状态显示维持2s
  484. if (buffer_read)
  485. {
  486. rt_free(buffer_read);
  487. buffer_read = RT_NULL;
  488. }
  489. if (file_buffer)
  490. {
  491. rt_free(file_buffer);
  492. file_buffer = RT_NULL;
  493. }
  494. ret = RT_EOK;
  495. Status_Display(reboot_5s, RT_NULL); //显示配置通道成功
  496. rt_thread_delay(rt_tick_from_millisecond(5000));
  497. Status_Display(RT_NULL, RT_NULL); //切换到bootloader页面
  498. Status_Display("\0", RT_NULL); //显示状态清空
  499. extern void RESET_SYSTEM(void);
  500. extern uint8_t reflash_elem(void);
  501. extern uint8_t bak_para(uint8_t type, uint8_t num);
  502. rt_kprintf("usb up ch bak in here\n\n");
  503. //bak_para(One, ch_bak_part);
  504. Mark_up_elem();
  505. Alarm_report(12 , 0 , 0);
  506. RESET_SYSTEM();
  507. __exit:
  508. return ret;
  509. }
  510. #define ZK_DOWNLOAD_PART "zk_area"
  511. // gbk数组文件写入
  512. static int usb_gbk_download(void)
  513. {
  514. int ret = 0, s = 0, resp_status;
  515. uint32_t file_size = 0, length, total_length = 0;
  516. rt_uint8_t *buffer_read = RT_NULL;
  517. const struct fal_partition *dl_part = RT_NULL;
  518. uint8_t filename[20];
  519. if (CH376_Init() != 1)
  520. {
  521. Status_Display(u_fail, RT_NULL);
  522. LOG_E("the usb drive init fail!");
  523. return ret;
  524. }
  525. if (CH376DiskConnect() == USB_INT_SUCCESS) /* 检查U盘是否连接 */
  526. {
  527. Status_Display(u_succ, RT_NULL);
  528. }
  529. else
  530. {
  531. Status_Display(u_fail, RT_NULL);
  532. LOG_E("the usb drive is not read!");
  533. goto __exit;
  534. }
  535. rt_sprintf((char *)filename, "/HEXC.BIN");
  536. s = CH376FileOpen(filename); /* 打开文件,该文件在根目录下 */
  537. if (s == ERR_MISS_FILE) /* 文件不存在*/
  538. {
  539. Status_Display(notfound_file, RT_NULL);
  540. LOG_E("the ota file is not find!");
  541. goto __exit;
  542. }
  543. else if (s == USB_INT_SUCCESS)
  544. {
  545. Status_Display(find_file, RT_NULL);
  546. file_size = CH376GetFileSize();
  547. rt_kprintf("File size = %ld\n", file_size);
  548. }
  549. Status_Display(DIS_POS_value, file_size);
  550. /* Get download partition information and erase download partition data */
  551. if ((dl_part = fal_partition_find("zk_area")) == RT_NULL)
  552. {
  553. Status_Display(nodownload_part, RT_NULL);
  554. LOG_E("Firmware download failed! Partition (%s) find error!", "fm_area");
  555. ret = -RT_ERROR;
  556. goto __exit;
  557. }
  558. LOG_I("Start erase flash (%s) partition!", dl_part->name);
  559. Status_Display(finddownload_part, RT_NULL);
  560. if (fal_partition_erase(dl_part, 0, file_size) < 0)
  561. {
  562. Status_Display(erase_fail, RT_NULL);
  563. LOG_E("Firmware download failed! Partition (%s) erase error!", dl_part->name);
  564. ret = -RT_ERROR;
  565. goto __exit;
  566. }
  567. Status_Display(erase_succ, RT_NULL);
  568. LOG_I("Erase flash (%s) partition success!", dl_part->name);
  569. buffer_read = rt_malloc(USB_OTA_BUFF_LEN);
  570. if (buffer_read == RT_NULL)
  571. {
  572. Status_Display(malloc_fail, RT_NULL);
  573. LOG_E("No memory for usb ota!");
  574. ret = -RT_ERROR;
  575. goto __exit;
  576. }
  577. rt_memset(buffer_read, 0x00, USB_OTA_BUFF_LEN);
  578. // list_mem();
  579. Status_Display(startwritfw, RT_NULL);
  580. do
  581. {
  582. length = usb_read(buffer_read, file_size - total_length > USB_OTA_BUFF_LEN ? USB_OTA_BUFF_LEN : file_size - total_length);
  583. if (length > 0)
  584. {
  585. /* Write the data to the corresponding partition address */
  586. if (fal_partition_write(dl_part, total_length, buffer_read, length) < 0)
  587. {
  588. Status_Display(write_error, RT_NULL);
  589. LOG_E("Firmware download failed! Partition (%s) write data error!", dl_part->name);
  590. ret = -RT_ERROR;
  591. goto __exit;
  592. }
  593. total_length += length;
  594. print_progress(total_length, file_size);
  595. // list_mem();
  596. }
  597. else
  598. {
  599. Status_Display(writefail, RT_NULL);
  600. LOG_E("Exit: USB OTA return err (%d)!", length);
  601. ret = -RT_ERROR;
  602. goto __exit;
  603. }
  604. } while (total_length != file_size);
  605. ret = RT_EOK;
  606. if (total_length == file_size)
  607. {
  608. Status_Display(gbk_succ, RT_NULL);
  609. LOG_I("GBK_BIN write to flash success.");
  610. }
  611. else
  612. {
  613. Status_Display(gbk_fail, RT_NULL);
  614. LOG_I("GBK_BIN write to flash failed.");
  615. }
  616. rt_thread_delay(rt_tick_from_millisecond(2000)); //状态显示维持2s
  617. Status_Display("\0", RT_NULL); //显示状态清空
  618. __exit:
  619. if (buffer_read)
  620. {
  621. rt_free(buffer_read);
  622. buffer_read = RT_NULL;
  623. }
  624. return ret;
  625. }
  626. void usb_ota(uint8_t argc, char **argv)
  627. {
  628. if (argc == 0)
  629. usb_ota_fw_download();
  630. else if (argc == 1)
  631. usb_ele_config_download();
  632. else if (argc == 2)
  633. usb_ch_config();
  634. else if (argc == 3)
  635. usb_gbk_download();
  636. }
  637. #if defined(RT_USING_FINSH) && defined(FINSH_USING_MSH)
  638. #include <finsh.h>
  639. MSH_CMD_EXPORT(usb_ota, Use HTTP to download the firmware);
  640. MSH_CMD_EXPORT(usb_ota_fw_download, Use HTTP to download the firmware);
  641. MSH_CMD_EXPORT(usb_ele_config_download, Use HTTP to download the firmware);
  642. MSH_CMD_EXPORT(usb_ch_config, Use HTTP to download the firmware);
  643. MSH_CMD_EXPORT(usb_gbk_download, Use HTTP to download the firmware);
  644. #endif /* defined(RT_USING_FINSH) && defined(FINSH_USING_MSH) */