usb_ota.c 19 KB

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