app如何通过多种固件下载器实现OTA升级

前言:前边讲过stm32通用bootloader的实现方法,没有看过的,可以参考这一篇文章:stm32通用bootloader——fota,这次将在上篇bootloader的基础上,介绍app如何通过多种固件下载器实现ota升级。
此项目硬件使用的是stm32f429开发板,代码全部使用rt-thread studio搭积木的方式实现,仅仅改动了几行代码,开发效率非常高。此项目的地址:https://gitee.com/aladdin-wang/rt-fota-stm32l431.git
使用到的软件包和组件:
在这里插入图片描述
1.准备工作
1.1 新建工程
由于此项目使用的esp8266需要一个串口,我使用的是uart2,所以需要还需要配置uart2:
增加uart接收缓冲区大小:
1.2 打开fal和at device软件包
配置fal软件包
配置sfud组件
配置spi
配置fal_cfg.h
1#ifndef_fal_cfg_h_ 2#define_fal_cfg_h_ 3 4#include 5#include 6 7#defineflash_size_granularity_16k(4*16*1024) 8#defineflash_size_granularity_64k(64*1024) 9#defineflash_size_granularity_128k(7*128*1024) 10 11#definestm32_flash_start_adress_16kstm32_flash_start_adress 12#definestm32_flash_start_adress_64k(stm32_flash_start_adress_16k+flash_size_granularity_16k) 13#definestm32_flash_start_adress_128k(stm32_flash_start_adress_64k+flash_size_granularity_64k) 14/*=====================flashdeviceconfiguration=========================*/ 15externconststructfal_flash_devstm32_onchip_flash_16k; 16externconststructfal_flash_devstm32_onchip_flash_64k; 17externconststructfal_flash_devstm32_onchip_flash_128k; 18externstructfal_flash_devnor_flash0; 19 20/*flashdevicetable*/ 21#definefal_flash_dev_table 22{ 23&stm32_onchip_flash_16k, 24&stm32_onchip_flash_64k, 25&stm32_onchip_flash_128k, 26&nor_flash0, 27} 28/*======================partitionconfiguration==========================*/ 29#ifdeffal_part_has_table_cfg 30/*partitiontable*/ 31#definefal_part_table 32{ 33{fal_part_magic_wrod,bootloader,onchip_flash_16k,0,flash_size_granularity_16k,0}, 34{fal_part_magic_wrod,param,onchip_flash_64k,0,flash_size_granularity_64k,0}, 35{fal_part_magic_wrod,app,onchip_flash_128k,0,flash_size_granularity_128k,0}, 36{fal_part_magic_wrod,ef,w25q128,0,1024*1024,0}, 37{fal_part_magic_wrod,download,w25q128,1024*1024,512*1024,0}, 38{fal_part_magic_wrod,factory,w25q128,(1024+512)*1024,512*1024,0}, 39} 40#endif/*fal_part_has_table_cfg*/ 41 42#endif/*_fal_cfg_h_*/
初始化spi flash和fal软件包
1#include 2#includespi_flash.h 3#includespi_flash_sfud.h 4#includedrv_spi.h 5 6#ifdefined(rt_using_sfud) 7staticintrt_hw_spi_flash_init(void) 8{ 9__hal_rcc_gpiof_clk_enable(); 10rt_hw_spi_device_attach(spi5,spi50,gpiof,gpio_pin_6); 11 12if(rt_null==rt_sfud_flash_probe(w25q128,spi50)) 13{ 14return-rt_error; 15} 16 17returnrt_eok; 18} 19init_component_export(rt_hw_spi_flash_init); 20#endif1intfs_init(void) 2{ 3/*partitioninitialized*/ 4fal_init(); 5return0; 6} 7init_env_export(fs_init);
配置at device软件包
1.3 配置中断重定向
1/** 2*functionota_app_vtor_reconfig 3*descriptionsetvectortablebaselocationtothestartaddrofapp(rt_app_part_addr). 4*/ 5staticintota_app_vtor_reconfig(void) 6{ 7#definenvic_vtor_mask0x3fffff80 8/*setthevectortablebaselocationbyuserapplicationfirmwaredefinition*/ 9scb->vtor=0x8020000&nvic_vtor_mask; 10 11return0; 12} 13init_board_export(ota_app_vtor_reconfig);
烧录bootloader:
bootloader的制作方法请参考官方的教程https://www.rt-thread.org/document/site/application-note/system/rtboot/an0028-rtboot/或者stm32通用bootloader——fota
2.阿里云物联网平台ota
注册 linkplatform 平台
创建产品
产品详情:
添加设备
添加自定义topic
配置ali iotkit软件包
将刚才新建的阿里云设备信息填写到配置信息里:
将软件包的示例mqtt-example.c和ota_mqtt-example.c拷贝到applications目录备用
配置mbedtls软件包
更改ota_mqtt-example.c中的部分代码:
1staticint_ota_mqtt_client(void) 2{ 3#defineota_buf_len(16385) 4#definedefault_download_partdownload 5intrc=0,ota_over=0; 6void*pclient=null,*h_ota=null; 7iotx_conn_info_ptpconn_info; 8iotx_mqtt_param_tmqtt_params; 9 10//file*fp; 11staticcharbuf_ota[ota_buf_len]; 12conststructfal_partition*dl_part=rt_null; 13 14//if(null==(fp=fopen(ota.bin,wb+))){ 15//example_trace(openfilefailed); 16//gotodo_exit; 17//} 18 19/**host_name; 37mqtt_params.client_id=pconn_info->client_id; 38mqtt_params.username=pconn_info->username; 39mqtt_params.password=pconn_info->password; 40mqtt_params.pub_key=pconn_info->pub_key; 41 42mqtt_params.request_timeout_ms=2000; 43mqtt_params.clean_session=0; 44mqtt_params.keepalive_interval_ms=60000; 45mqtt_params.read_buf_size=ota_mqtt_msglen; 46mqtt_params.write_buf_size=ota_mqtt_msglen; 47 48mqtt_params.handle_event.h_fp=event_handle; 49mqtt_params.handle_event.pcontext=null; 50 51/*constructamqttclientwithspecifyparameter*/ 52pclient=iot_mqtt_construct(&mqtt_params); 53if(null==pclient){ 54example_trace(mqttconstructfailed); 55rc=-1; 56gotodo_exit; 57} 58h_ota=iot_ota_init(g_product_key,g_device_name,pclient); 59if(null==h_ota){ 60rc=-1; 61example_trace(initializeotafailed); 62gotodo_exit; 63} 64 65 66do{ 67uint32_tfirmware_valid; 68 69example_trace(waitotaupgradecommand....); 70 71/*handlethemqttpacketreceivedfromtcporsslconnection*/ 72iot_mqtt_yield(pclient,200); 73 74if(iot_ota_isfetching(h_ota)){ 75uint32_tlast_percent=0,percent=0; 76charmd5sum[33]; 77charversion[128]={0}; 78uint32_tlen,size_downloaded,size_file; 79iot_ota_ioctl(h_ota,iot_otag_file_size,&size_file,4); 80/*getdownloadpartitioninformationanderasedownloadpartitiondata*/ 81if((dl_part=fal_partition_find(default_download_part))==rt_null) 82{ 83log_e(firmwaredownloadfailed!partition(%s)finderror!,download); 84rc=-1; 85gotodo_exit; 86} 87 88log_i(starteraseflash(%s)partition!,dl_part->name); 89 90if(fal_partition_erase(dl_part,0,size_file)name); 93rc=-1; 94gotodo_exit; 95} 96log_i(eraseflash(%s)partitionsuccess!,dl_part->name); 97 98rt_uint32_tcontent_pos=0,content_write_sz; 99 100do{ 101 102len=iot_ota_fetchyield(h_ota,buf_ota,ota_buf_len,1); 103if(len>0){ 104content_write_sz=fal_partition_write(dl_part,content_pos,(uint8_t*)buf_ota,len); 105if(content_write_sz!=len) 106{ 107log_i(writeotadatatofilefailed); 108 109iot_ota_reportprogress(h_ota,iot_otap_burn_failed,rt_null); 110 111gotodo_exit; 112} 113else 114{ 115content_pos=content_pos+len; 116log_i(receive%dbytes,totalrecieve:%dbytes,content_pos,size_file); 117} 118}else{ 119iot_ota_reportprogress(h_ota,iot_otap_fetch_failed,null); 120example_trace(otafetchfail); 121} 122 123/*getotainformation*/ 124iot_ota_ioctl(h_ota,iot_otag_fetched_size,&size_downloaded,4); 125iot_ota_ioctl(h_ota,iot_otag_file_size,&size_file,4); 126 127last_percent=percent; 128percent=(size_downloaded*100)/size_file; 129if(percent-last_percent>0){ 130iot_ota_reportprogress(h_ota,percent,null); 131} 132iot_mqtt_yield(pclient,100); 133}while(!iot_ota_isfetchfinish(h_ota)); 134 135iot_ota_ioctl(h_ota,iot_otag_md5sum,md5sum,33); 136iot_ota_ioctl(h_ota,iot_otag_version,version,128); 137iot_ota_ioctl(h_ota,iot_otag_check_firmware,&firmware_valid,4); 138if(0==firmware_valid){ 139example_trace(thefirmwareisinvalid); 140}else{ 141example_trace(thefirmwareisvalid); 142iot_ota_reportversion(h_ota,version); 143 144log_i(downloadfirmwaretoflashsuccess.); 145log_i(systemnowwillrestart...); 146 147hal_sleepms(1000); 148 149/*resetthedevice,startnewfirmware*/ 150externvoidrt_hw_cpu_reset(void); 151rt_hw_cpu_reset(); 152} 153 154ota_over=1; 155} 156hal_sleepms(2000); 157}while(!ota_over); 158 159hal_sleepms(1000); 160 161do_exit: 162 163if(null!=h_ota){ 164iot_ota_deinit(h_ota); 165} 166 167if(null!=pclient){ 168iot_mqtt_destroy(&pclient); 169} 170 171returnrc; 172}
编译工程,将bin文件上传到阿里云:
阿里云不支持rbl格式的文件,直接将rt_ota_packaging_tool生成的rbl文件后缀改为bin,上传即可。
最后使用ali_ota_sample命令升级:
3.http ota和ymodem ota
配置ota_downloader软件包
如果暂时没有自己的服务器,可以使用mywebserver进行测试:
配置完mywebserver,可以打开浏览器输入ip地址查看:
使用http_ota命令进行http_ota升级:
使用ymodem_ota命令进行ymodem_ota升级:
4.不使用app进行升级
rt-fota集成了ymodem_ota,上电短按恢复出厂设置按钮即可进入rt-fota命令行模式,通过ymodem_ota命令即可进行升级:

力帆轩朗采用的是蒙迪欧式的大嘴设计,车主得意五万到手,已经成为了重庆人民的骄傲
山东电力公司已面启动智能运检体系建设工作
10BASE-T1L使智能现场仪表供电变得无痛
Diodes公司150mA超低压差稳压器支持宽输入电压范围及固定输出电压
8月1日开始施行动力蓄电池回收利用溯源管理暂行规定
app如何通过多种固件下载器实现OTA升级
孙正义:未来30年的人工智能和物联网(深度好文)
铅酸蓄电池的组成部分_铅酸蓄电池的寿命
联锁控制系统的构成、特点、功能介绍
阻容降压X2安规电容有什么作用?
试水新零售 小吉科技联手闪殿打造“天生艺术家”快闪店
工业机器人的结构和组成说明
中兴,只是一个开始,国家层面如何破局?
AOC有源光缆和DAC高速线缆之间的区别是什么
国家大力推进超高清视频产业 激光电视迎来发展新机遇
机器视觉系统的构成/工作原理/功能/优点
2018华为成绩单亮眼 2019迎来行业格局大洗牌
国产平价游戏蓝牙耳机有吗?便宜好用的游戏蓝牙耳机榜单
基于全球性的金融区块链开源项目Zipper介绍
微软在华正式公布Win10 VR头显的PC最低配置