usb_ota.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. #define LOG_TAG "usb_ota" // 该模块对应的标签。不定义时,默认:NO_TAG
  2. #define LOG_LVL LOG_LVL_DBG // 该模块对应的日志输出级别。不定义时,默认:调试级别
  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 <rt_fota.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 u_succ "U盘检测成功"
  18. #define u_fail "U盘检测失败"
  19. #define find_file "检测到升级文件"
  20. #define notfound_file "未找到升级文件"
  21. #define filesize "升级文件大小:"
  22. #define startwritfw "准备写入固件"
  23. #define writingfw "正在写入固件"
  24. #define finddownload_part "找到固件分区,准备格式化"
  25. #define nodownload_part "未找到固件分区"
  26. #define erase_fail "固件分区格式化失败"
  27. #define erase_succ "固件分区格式化成功"
  28. #define malloc_fail "分配内存失败"
  29. #define write_error "固件写入错误"
  30. #define writefail "固件写入失败"
  31. #define writesucc "固件写入成功"
  32. static void print_progress(size_t cur_size, size_t total_size)
  33. {
  34. static unsigned char progress_sign[100 + 1];
  35. uint8_t i, per = cur_size * 100 / total_size;
  36. if (per > 100)
  37. {
  38. per = 100;
  39. }
  40. for (i = 0; i < 100; i++)
  41. {
  42. if (i < per)
  43. {
  44. progress_sign[i] = '=';
  45. }
  46. else if (per == i)
  47. {
  48. progress_sign[i] = '>';
  49. }
  50. else
  51. {
  52. progress_sign[i] = ' ';
  53. }
  54. }
  55. fota_progress_bar_send(per);
  56. progress_sign[sizeof(progress_sign) - 1] = '\0';
  57. log_i("\033[2A");
  58. log_i("Download: [%s] %d%%", progress_sign, per);
  59. }
  60. int usb_read(unsigned char *buffer, size_t length)//
  61. {
  62. int s = 0;
  63. uint32_t RealCount = 0;
  64. s = CH376ByteRead(buffer, length, (PUINT16 ) &RealCount);
  65. if (s != USB_INT_SUCCESS)
  66. {
  67. mStopIfError(s);
  68. return 0;
  69. }
  70. return RealCount;
  71. }
  72. static int usb_ota_fw_download(void)
  73. {
  74. int ret = 0,s = 0, resp_status;
  75. uint32_t file_size = 0, length, total_length = 0;
  76. rt_uint8_t *buffer_read = RT_NULL;
  77. const struct fal_partition * dl_part = RT_NULL;
  78. uint8_t filename[20];
  79. if(CH376_Init() != 1)
  80. {
  81. fota_status_send(u_fail, RT_NULL);
  82. LOG_E("the usb drive init fail! > usb_ota_fw_download");
  83. goto __exit;
  84. }
  85. fota_status_send(u_succ, sizeof(u_succ));
  86. rt_sprintf((char *)filename,"/QXZ.RBL");
  87. s = CH376FileOpen(filename); /* 打开文件,该文件在根目录下 */
  88. if(s == ERR_MISS_FILE) /* 文件不存在*/
  89. {
  90. fota_status_send(notfound_file, sizeof(notfound_file));
  91. log_e("the ota file is not find!");
  92. goto __exit;
  93. }
  94. else if (s == USB_INT_SUCCESS) {
  95. fota_status_send(find_file, sizeof(find_file));
  96. file_size = CH376GetFileSize();
  97. rt_kprintf( "File size = %ld\n", file_size);
  98. }
  99. /* Get download partition information and erase download partition data */
  100. if ((dl_part = fal_partition_find(DEFAULT_DOWNLOAD_PART)) == RT_NULL)
  101. {
  102. fota_status_send(nodownload_part,sizeof(nodownload_part));
  103. log_e("Firmware download failed! Partition (%s) find error!", DEFAULT_DOWNLOAD_PART);
  104. ret = -RT_ERROR;
  105. goto __exit;
  106. }
  107. LOG_I("Start erase flash (%s) partition!", dl_part->name);
  108. fota_status_send(finddownload_part, sizeof(finddownload_part));
  109. if (fal_partition_erase(dl_part, 0, file_size) < 0)
  110. {
  111. fota_status_send(erase_fail,sizeof(erase_fail));
  112. log_e("Firmware download failed! Partition (%s) erase error!", dl_part->name);
  113. ret = -RT_ERROR;
  114. goto __exit;
  115. }
  116. fota_status_send(erase_succ, sizeof(erase_succ));
  117. LOG_I("Erase flash (%s) partition success!", dl_part->name);
  118. buffer_read = rt_malloc(USB_OTA_BUFF_LEN);
  119. if (buffer_read == RT_NULL)
  120. {
  121. fota_status_send(malloc_fail, sizeof(malloc_fail));
  122. log_e("No memory for usb ota!");
  123. ret = -RT_ERROR;
  124. goto __exit;
  125. }
  126. rt_memset(buffer_read, 0x00, USB_OTA_BUFF_LEN);
  127. fota_status_send(startwritfw, sizeof(startwritfw));
  128. do
  129. {
  130. length = usb_read(buffer_read, file_size - total_length > USB_OTA_BUFF_LEN ?
  131. USB_OTA_BUFF_LEN : file_size - total_length);
  132. if (length > 0)
  133. {
  134. /* Write the data to the corresponding partition address */
  135. if (fal_partition_write(dl_part, total_length, buffer_read, length) < 0)
  136. {
  137. fota_status_send(write_error, sizeof(write_error));
  138. log_e("Firmware download failed! Partition (%s) write data error!", dl_part->name);
  139. ret = -RT_ERROR;
  140. goto __exit;
  141. }
  142. total_length += length;
  143. print_progress(total_length, file_size);
  144. }
  145. else
  146. {
  147. fota_status_send(writefail, sizeof(writefail));
  148. log_e("Exit: USB OTA return err (%d)!", length);
  149. ret = -RT_ERROR;
  150. goto __exit;
  151. }
  152. } while(total_length != file_size);
  153. ret = RT_EOK;
  154. if (total_length == file_size)
  155. {
  156. if (buffer_read)
  157. {rt_free(buffer_read);buffer_read = RT_NULL;}
  158. fota_status_send(writesucc, sizeof(writesucc));
  159. log_i("Download firmware to flash success.");
  160. log_i("System now will restart...");
  161. rt_thread_delay(rt_tick_from_millisecond(500));
  162. fota_status_send("\0", sizeof("\0"));//显示状态清空
  163. /* Reset the device, Start new firmware */
  164. // extern void rt_hw_cpu_reset(void);
  165. // rt_hw_cpu_reset();
  166. firstime_clone_df();//第一次烧写程序,出厂备份区保存固件
  167. upgrade();//向mcuapp区写入数据
  168. }
  169. __exit:
  170. if (buffer_read != RT_NULL)rt_free(buffer_read);
  171. return ret;
  172. }
  173. void usb_ota(uint8_t argc, char **argv)
  174. {
  175. usb_ota_fw_download();
  176. }
  177. #if defined(RT_USING_FINSH) && defined(FINSH_USING_MSH)
  178. #include <finsh.h>
  179. MSH_CMD_EXPORT(usb_ota, Use HTTP to download the firmware);
  180. #endif /* defined(RT_USING_FINSH) && defined(FINSH_USING_MSH) */