前言
非常感谢基于安谋科技STAR-MC1的全志XR806 Wi-Fi+BLE开源鸿蒙开发板试用活动,并获得开发板试用。
XR806不仅硬件功能多,XR806也提供了功能极其丰富的SDK,几天体验下来非常容易上手。常见的功能几乎都有相应的cmd或demo实现,HAL也做得非常全面,非常适合快速开发。这一点超级好评!本文章要实现的无线OTA也基于该SDK。
环境搭建
本实验主机环境为ubuntu,并基于freertos sdk开发,这里不详述,全志在线以及其他文章有很多介绍了。 https://xr806.docs.aw-ol.com/
无线OTA简述
OTA全称空中下载技术,及通过网络在线更新软件。当产品发布以后,若能支持网络升级,那将省去不少人力。所以OTA对于物联网产品是十分重要的功能。XR806 SDK中提供了两种升级方式:通过文件系统升级;通过http网络升级。同时也提供了完整的分区备份/校验/加密等功能,使得升级功能不容易被入侵。
无线OTA实验步骤
hello\_demo工程位于project/demo/hello\_demo。该工程虽然只在main.c中打印了"hello world",看似功能简单。但它其实还支持许多cmd,位于project/demo/hello\_demo/command.c:
static const struct cmd_data g_main_cmds[] = {
#if PRJCONF_NET_EN
{ "net", cmd_net_exec, CMD_DESC("network command") },
#endif
{ "mem", cmd_mem_exec, CMD_DESC("memory command") },
{ "heap", cmd_heap_exec, CMD_DESC("heap use information command") },
{ "thread", cmd_thread_exec, CMD_DESC("thread information command") },
{ "upgrade", cmd_upgrade_exec, CMD_DESC("upgrade command") },
{ "reboot", cmd_reboot_exec, CMD_DESC("reboot command") },
#ifdef CONFIG_OTA
{ "ota", cmd_ota_exec, CMD_DESC("over the airtechnology upgrade commands") },
#endif
{ "pm", cmd_pm_exec, CMD_DESC("power management command") },
{ "efpg", cmd_efpg_exec, CMD_DESC("efpg command") },
{ "help", cmd_main_help_exec, CMD_DESC(CMD_HELP_DESC) },
};
我们需要在menuconfig中使能OTA和NET(默认已经全部支持)。所以hello\_demo工程默认是支持http网络以及OTA功能,基础条件都具备,接下来看看该如何使用。
1. 使用phoenixMC工具编译烧录hello工程
sudo ./phoenixMC -i ../out/xr_system.img
烧录完成:
打开串口查看日志:
sudo minicom -D /dev/ttyUSB0 -b 115200
2. 修改hello工程,编译生成即将要升级的固件
修改hello.c,将打印修改为“OTA OK!”
int main(void)
{
platform_init();
while (1) {
OS_Sleep(10);
printf("OTA OK! @ %u sec\n", OS_GetTicks()); //修改打印
}
return 0;
}
重新编译
yhz@lab:~/work/misc/xr806_sdk$ make build
yhz@lab:~/work/misc/xr806_sdk$ md5sum out/xr_system.img
6e79ddbdad10928c16a50805c2ca0f1c out/xr_system.img
3. 使用net cmd命令连接wifi
Set STA mode:
$ net mode sta
<ACK> 200 OK
[net INF] no need to switch wlan mode 0
Enable STA:
$ net sta enable
<ACK> 200 OK
Config SAT:
$ net sta config dmesg-4g 88880000 //wifi名称和密码
<ACK> 200 OK
Connect wifi:
$ net sta connect
<ACK> 200 OK
en1: Trying to associate with 34:ca:81:89:da:fc (SSID='dmesg-4g' freq=2462 MHz)
en1: WPA: Key negotiation completed with 34:ca:81:89:da:fc [PTK=CCMP GTK=CCMP]
en1: CTRL-EVENT-CONNECTED - Connection to 34:ca:81:89:da:fc completed [id=0 id_str=]
[net INF] msg <wlan connected>
[net INF] netif is link up
[net INF] start DHCP...
WAR drop=1135, fctl=0x00d0.
[net INF] netif (IPv4) is up
[net INF] address: 192.168.10.5
[net INF] gateway: 192.168.10.1
[net INF] netmask: 255.255.255.0
[net INF] msg <network up>
Check:
$ net ifconfig status
<ACK> 200 en1 up, address:192.168.10.5 gateway:192.168.10.1 netmask:255.255.255.0
$ net ping 192.168.10.11
<ACK> 200 OK
PING 192.168.10.11 108 bytes of data.
100 bytes from 192.168.10.11: icmp_seq=1 time=9 ms
100 bytes from 192.168.10.11: icmp_seq=2 time=6 ms
100 bytes from 192.168.10.11: icmp_seq=3 time=14 ms
--- 192.168.10.11 ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 3029ms
rtt min/avg/max/mdev = 6/9/14/8 ms
至此,wifi配置完成,xr806已成功联网。接下来配置镜像服务器。
4. 使用apache2搭建http服务器
sudo apt-get update
sudo apt install apache2
sudo ufw app list
sudo ufw allow 'Apache'
sudo systemctl status apache2
整个搭建过程很简单。apache2的默认文件位置位于/var/www/html/目录下。
给该文件夹增加权限:
sudo chmod 777 -R /var/www/html/
这样服务器环境就搭建好了,将待OTA的镜像放到/var/www/html/目录下:
yhz@lab:~/work/misc/xr806_sdk$ cp out/xr_system.img /var/www/html/
yhz@lab:~/work/misc/xr806_sdk$
yhz@lab:~/work/misc/xr806_sdk$ md5sum /var/www/html/xr_system.img
6e79ddbdad10928c16a50805c2ca0f1c /var/www/html/xr_system.img
5. 开始OTA
在第三步中我们已经连接了wifi,此时xr806可以从服务器获取新的镜像。使用ota命令开始升级:
$ ota http http://192.168.10.11/xr_system.img
<ACK> 200 OK
OTA: erase flash...
OTA: start loading image...
Hello world! @ 41063083 sec
OTA: loading image (50 KB)...
OTA: loading image (101 KB)...
OTA: loading image (150 KB)...
OTA: loading image (200 KB)...
OTA: loading image (251 KB)...
OTA: loading image (300 KB)...
OTA: loading image (350 KB)...
OTA: loading image (401 KB)...
OTA: loading image (450 KB)...
OTA: loading image (500 KB)...
OTA: loading image (551 KB)...
OTA: loading image (600 KB)...
OTA: loading image (651 KB)...
OTA: loading image (701 KB)...
OTA: loading image (750 KB)...
OTA: loading image (801 KB)...
OTA: loading image (850 KB)...
OTA: loading image (901 KB)...
OTA: loading image (951 KB)...
OTA: loading image (1000 KB)...
OTA: finish loading image(0x000fab60)
OTA: finish checkinuse default flash chip mJedec 0x0
[FD I]: mode: 0x10, freq: 96000000Hz, drv: 0
[FD I]: jedec: 0x0, suspend_support: 1
mode select:e
wlan information
===================================================
firmware:
version : R0-XR_C07.08.52.67_ULP_R_02.132 Jan 10 2023 19:14:11-Y02.132
buffer : 8
driver:
version : XR_V02.06.10
mac address:
in use : 0c:69:88:39:e6:01
in use : 0c:69:88:39:e6:02
====================================================================
wlan mode:a
platform information
===============================================
XR806 SDK v1.2.2 Oct 24 2023 00:13:09 62800400
heap space [0x216014, 0x24bc00), size 220140
cpu clock 160000000 Hz
HF clock 40000000 Hz
sdk option:
XIP : enable
INT LF OSC : enable
INT LDO : select
INT LDO / EXT PWR: enable
SIP flash : enable
mac address:
efuse : 80:74:84:05:b2:56
in use : 0c:69:88:39:e6:01
====================================================================
$
$ OTA OK! @ 10165 sec
$ OTA OK! @ 20165 sec
至此,OTA升级完毕。
感慨一下,xr806的硬件和SDK功能很强大,开发起来真的很快~~~!