| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214 |
- #define LOG_TAG "usb_ota" // 该模块对应的标签。不定义时,默认:NO_TAG
- #define LOG_LVL LOG_LVL_DBG // 该模块对应的日志输出级别。不定义时,默认:调试级别
- #include <ulog.h> // 必须在 LOG_TAG 与 LOG_LVL 下面
- #include <stdio.h>
- #include <stdint.h>
- #include <stdlib.h>
- #include <rtthread.h>
- #include <finsh.h>
- #include <fal.h>
- #include "sys_config.h"
- #include "CH376HFT.h"
- #include <rt_fota.h>
- #define USB_OTA_BUFF_LEN 8192
- #define USB_OTA_DL_DELAY (10 * RT_TICK_PER_SECOND)
- #define DEFAULT_DOWNLOAD_PART "fm_area"
- #define STATUS_DIS_LEN 100 //显示缓冲区长度
- #define u_succ "U盘检测成功"
- #define u_fail "U盘检测失败"
- #define find_file "检测到升级文件"
- #define notfound_file "未找到升级文件"
- #define filesize "升级文件大小:"
- #define startwritfw "准备写入固件"
- #define writingfw "正在写入固件"
- #define finddownload_part "找到固件分区,准备格式化"
- #define nodownload_part "未找到固件分区"
- #define erase_fail "固件分区格式化失败"
- #define erase_succ "固件分区格式化成功"
- #define malloc_fail "分配内存失败"
- #define write_error "固件写入错误"
- #define writefail "固件写入失败"
- #define writesucc "固件写入成功"
- static void print_progress(size_t cur_size, size_t total_size)
- {
- static unsigned char progress_sign[100 + 1];
- uint8_t i, per = cur_size * 100 / total_size;
- if (per > 100)
- {
- per = 100;
- }
- for (i = 0; i < 100; i++)
- {
- if (i < per)
- {
- progress_sign[i] = '=';
- }
- else if (per == i)
- {
- progress_sign[i] = '>';
- }
- else
- {
- progress_sign[i] = ' ';
- }
- }
- fota_progress_bar_send(per);
- progress_sign[sizeof(progress_sign) - 1] = '\0';
- log_i("\033[2A");
- log_i("Download: [%s] %d%%", progress_sign, per);
- }
- int usb_read(unsigned char *buffer, size_t length)//
- {
- int s = 0;
- uint32_t RealCount = 0;
- s = CH376ByteRead(buffer, length, (PUINT16 ) &RealCount);
- if (s != USB_INT_SUCCESS)
- {
- mStopIfError(s);
- return 0;
- }
- return RealCount;
- }
- static int usb_ota_fw_download(void)
- {
- int ret = 0,s = 0, resp_status;
- uint32_t file_size = 0, length, total_length = 0;
- rt_uint8_t *buffer_read = RT_NULL;
- const struct fal_partition * dl_part = RT_NULL;
- uint8_t filename[20];
- if(CH376_Init() != 1)
- {
- fota_status_send(u_fail, RT_NULL);
- LOG_E("the usb drive init fail! > usb_ota_fw_download");
- goto __exit;
- }
- fota_status_send(u_succ, sizeof(u_succ));
- rt_sprintf((char *)filename,"/QXZ.RBL");
- s = CH376FileOpen(filename); /* 打开文件,该文件在根目录下 */
- if(s == ERR_MISS_FILE) /* 文件不存在*/
- {
- fota_status_send(notfound_file, sizeof(notfound_file));
- log_e("the ota file is not find!");
- goto __exit;
- }
- else if (s == USB_INT_SUCCESS) {
- fota_status_send(find_file, sizeof(find_file));
- file_size = CH376GetFileSize();
- rt_kprintf( "File size = %ld\n", file_size);
- }
- /* Get download partition information and erase download partition data */
- if ((dl_part = fal_partition_find(DEFAULT_DOWNLOAD_PART)) == RT_NULL)
- {
- fota_status_send(nodownload_part,sizeof(nodownload_part));
- log_e("Firmware download failed! Partition (%s) find error!", DEFAULT_DOWNLOAD_PART);
- ret = -RT_ERROR;
- goto __exit;
- }
- LOG_I("Start erase flash (%s) partition!", dl_part->name);
- fota_status_send(finddownload_part, sizeof(finddownload_part));
- if (fal_partition_erase(dl_part, 0, file_size) < 0)
- {
- fota_status_send(erase_fail,sizeof(erase_fail));
- log_e("Firmware download failed! Partition (%s) erase error!", dl_part->name);
- ret = -RT_ERROR;
- goto __exit;
- }
- fota_status_send(erase_succ, sizeof(erase_succ));
- LOG_I("Erase flash (%s) partition success!", dl_part->name);
- buffer_read = rt_malloc(USB_OTA_BUFF_LEN);
- if (buffer_read == RT_NULL)
- {
- fota_status_send(malloc_fail, sizeof(malloc_fail));
- log_e("No memory for usb ota!");
- ret = -RT_ERROR;
- goto __exit;
- }
- rt_memset(buffer_read, 0x00, USB_OTA_BUFF_LEN);
- fota_status_send(startwritfw, sizeof(startwritfw));
- do
- {
- length = usb_read(buffer_read, file_size - total_length > USB_OTA_BUFF_LEN ?
- USB_OTA_BUFF_LEN : file_size - total_length);
- if (length > 0)
- {
- /* Write the data to the corresponding partition address */
- if (fal_partition_write(dl_part, total_length, buffer_read, length) < 0)
- {
- fota_status_send(write_error, sizeof(write_error));
- log_e("Firmware download failed! Partition (%s) write data error!", dl_part->name);
- ret = -RT_ERROR;
- goto __exit;
- }
- total_length += length;
- print_progress(total_length, file_size);
- }
- else
- {
- fota_status_send(writefail, sizeof(writefail));
- log_e("Exit: USB OTA return err (%d)!", length);
- ret = -RT_ERROR;
- goto __exit;
- }
- } while(total_length != file_size);
- ret = RT_EOK;
- if (total_length == file_size)
- {
- if (buffer_read)
- {rt_free(buffer_read);buffer_read = RT_NULL;}
- fota_status_send(writesucc, sizeof(writesucc));
- log_i("Download firmware to flash success.");
- log_i("System now will restart...");
- rt_thread_delay(rt_tick_from_millisecond(500));
- fota_status_send("\0", sizeof("\0"));//显示状态清空
- /* Reset the device, Start new firmware */
- // extern void rt_hw_cpu_reset(void);
- // rt_hw_cpu_reset();
- firstime_clone_df();//第一次烧写程序,出厂备份区保存固件
- upgrade();//向mcuapp区写入数据
- }
- __exit:
- if (buffer_read != RT_NULL)rt_free(buffer_read);
- return ret;
- }
- void usb_ota(uint8_t argc, char **argv)
- {
- usb_ota_fw_download();
- }
- #if defined(RT_USING_FINSH) && defined(FINSH_USING_MSH)
- #include <finsh.h>
- MSH_CMD_EXPORT(usb_ota, Use HTTP to download the firmware);
- #endif /* defined(RT_USING_FINSH) && defined(FINSH_USING_MSH) */
|