当前页面为 开发中 版本,查看特定版本的文档,请在页面左下角的下拉菜单中进行选择。

Bluetooth: Peripheral OTA

1 功能概述

此项目演示了蓝牙从机OTA的功能,该工程在”peripheral”从机例程的基础上增加了OTA的SMP服务,可以通过“nrf connect” APP对设备进行软件升级。

2 环境要求

  • board: 支持 BLE 的蓝牙设备

  • uart(option): 用来显示串口log

  • 测试软件: nRF Connect V4.24.3

  • 烧录工具:j-link或panlink

3 编译和烧录

例程位置:zephyr\samples_panchip\bluetooth\peripheral_ota

使用 ZAL 工具可以对其进行编译、烧录、打开 VS Code 调试等操作。关于 ZAL 工具的详细介绍请参考:Zephyr APP Launcher 工具介绍

4 演示说明

  1. 烧录完成后,设备自动启动蓝牙广播,可以在手机APP nRF Connect或抓包工具上获取如下信息:

    • Service UUID: 0x1805, 0x180D, 0x180F

    • Device Name: Zephyr Peripheral Sample Long Name

    如下图所示:

    image

    使用nRF Connect搜索蓝牙广播

  2. 当手机APP nRF Connect连接成功后,APP显示如下:

    image

    蓝牙成功连接

    SMP服务就是OTA的服务。

  3. 修改工程”peripheral ota“中蓝牙设备名称,如下图所示:

    image

    修改蓝牙广播名称

    使用 ZAL 工具进行编译,在编译目录中将编译后的固件”zephyr.signed.bin”拷贝到手机上,编译后的固件位置如下图所示:

    image

    编译后的zephyr固件

  4. 点击手机右上角的“DFU”按钮,进入OTA文件选择界面,如下图所示:

    image

    选择待升级的Image文件

    选择刚刚生成的OTA文件“zephyr.signed.bin”。

    选择OTA的模式:

    image

    选择OTA模式

  5. 开始OTA

    选好文件后就开始OTA了,OTA支持断点续传功能。

    image

    开始OTA

  6. OTA结束

    OTA文件传输完成后,芯片自动复位重启。使用手机APP nRF Connect扫描设备的广播信息,此时设备的名字已经改变。

    如下图所示:

    image

    OTA完成

    OTA升级固件成功。

5 OTA功能移植

如果其他工程想要使用OTA功能,需要执行以下三个步骤:

  1. 在“prj.conf”文件中添加CONFIG配置

    ######################  OTA CONFIG  ###########################
    # Allow for large Bluetooth data packets.
    CONFIG_BT_L2CAP_TX_MTU=252
    CONFIG_BT_BUF_ACL_RX_SIZE=256
    
    # Enable the Bluetooth (unauthenticated) and shell mcumgr transports.
    CONFIG_MCUMGR_SMP_BT=y
    CONFIG_MCUMGR_SMP_BT_AUTHEN=n
    
    # Enable the LittleFS file system.
    CONFIG_FILE_SYSTEM=y
    CONFIG_FILE_SYSTEM_LITTLEFS=y
    
    # Add 256 bytes to accommodate upload command (lfs_stat overflows)
    CONFIG_SYSTEM_WORKQUEUE_STACK_SIZE=2304
    
    # Enable mcumgr.
    CONFIG_MCUMGR=y
    
    # Ensure an MCUboot-compatible binary is generated.
    CONFIG_BOOTLOADER_MCUBOOT=y
    
    # Required by the `taskstat` command.
    CONFIG_THREAD_MONITOR=y
    
    # Enable statistics and statistic names.
    CONFIG_STATS=y
    CONFIG_STATS_NAMES=y
    
    # Enable most core commands.
    CONFIG_MCUMGR_CMD_IMG_MGMT=y
    CONFIG_MCUMGR_CMD_OS_MGMT=y
    CONFIG_MCUMGR_CMD_STAT_MGMT=y
    ###########################################################################
    
  2. 在“main.c”文件中包含OTA相关的头文件

    /******************  OTA INCLUDE *******************/
    #ifdef CONFIG_MCUMGR_CMD_OS_MGMT
    #include "os_mgmt/os_mgmt.h"
    #endif
    #ifdef CONFIG_MCUMGR_CMD_IMG_MGMT
    #include "img_mgmt/img_mgmt.h"
    #endif
    #ifdef CONFIG_MCUMGR_CMD_STAT_MGMT
    #include "stat_mgmt/stat_mgmt.h"
    #endif
    #ifdef CONFIG_MCUMGR_SMP_BT
    #include <mgmt/mcumgr/smp_bt.h>
    #endif
    /*****************************************************/
    
  3. 在“main()”函数中添加OTA相关初始化代码

    	err = bt_enable(NULL);
    	if (err) {
    		printk("Bluetooth init failed (err %d)\n", err);
    		return;
    	}
    
    	bt_ready();
    
    /******************  OTA SERVICE  *******************/
    #ifdef CONFIG_MCUMGR_CMD_OS_MGMT
    	os_mgmt_register_group();
    #endif
    #ifdef CONFIG_MCUMGR_CMD_IMG_MGMT
    	img_mgmt_register_group();
    #endif
    #ifdef CONFIG_MCUMGR_CMD_STAT_MGMT
    	stat_mgmt_register_group();
    #endif
    #ifdef CONFIG_MCUMGR_SMP_BT
    	smp_bt_register();
    #endif
    /*****************************************************/
    

在新的工程添加以上三部分代码后,新的工程也支持OTA功能了。

6 RAM/Flash资源使用情况

Memory region         Used Size  Region Size  %age Used
FLASH:      123040 B       384 KB     31.29%
SRAM:       37656 B        50 KB     73.55%