RW007 高速WiFi模块 开启 BLE 主机通信功能示例

发布于 2021-04-10 18:33:47

RW007 高速WiFi模块 开启 BLE 主机通信功能示例

由于RW007模块默认没有开启BLE功能,使用BLE功能,需要根据下面的步骤进行操作。

步骤1:更新 RW007 模块的固件

由于默认RW007模块的固件不支持BLE主机功能,所以需要使用相关的升级工具进行升级。具体操作请参考附件中对应的文档进行操作。

步骤2:更新主机的 RW007软件驱动包

由于v2.0.1 版本的RW007软件驱动包中,不包括BLE功能,所以需要手动更新RW007的软件驱动包,请使用提供的软件驱动包替换到主机软件包的目录下。

步骤3:测试RW007模块的BLE功能

根据前面的操作步骤,确保RW007模块自身的固件已经更新和RW007驱动软件包已更新替换到使用的工程中,在提供的软件驱动包中,已经包含了部分 BLE功能的测试命令,可以在串口调试中通过msh命令,进行调试。

msh />rw007_ble
[rw007_ble command]

rw007_ble help
rw007_ble init central/peripheral                           Note: init ble mode
rw007_ble get_addr                                          Note: get ble address
rw007_ble update_params                                     Note: update connect parameters(no support)
rw007_ble scan                                              Note: scan ble slave
rw007_ble stop_scan                                         Note: stop scan
rw007_ble connect xx:xx:xx:xx:xx:xx                         Note: use slave addr to connect
rw007_ble disconnect [conn_handle]                          Note: disconnect slave
rw007_ble get_server                                        Note: discover all server(no support)
rw007_ble get_char                                          Note: discover all description(no support)
rw007_ble get_disc                                          Note: discover all characteristic(no support)
rw007_ble mtu_exch                                          Note: ble mtu exchange(no support)
rw007_ble notify                                            Note: enable ble notify(no support)
rw007_ble write                                             Note: ble write data(no support)
rw007_ble read                                              Note: ble read data(no support)
rw007_ble notify_change [conn_handle] [char_value] [UUID]   Note: ble notify configure by uuid
rw007_ble write_uuid [conn_handle] [UUID] [data]            Note: ble write data by uuid
rw007_ble read_uuid                                         Note: ble read data by uuid(no support)

msh />

如上可知,有部分BLE功能暂时还没支持的,下面将详细说一说BLE测试命令的使用。

BLE 功能初始化

BLE 功能初始化函数,使用 BLE功能必须调用。

  • 1.RW007BLE 设备初始化为主机设备。
rw007_ble init central

测试示例

msh />rw007_ble init central
122 - ble_cmd_init
start ble central
msh />
  • 2.RW007BLE 设备初始化为从机设备

注意:目前不支持使用从机模式。

rw007_ble init peripheral

BLE 功能获取本机的BLE设备地址

由于BLE设备的地址分为public公共地址和random随机地址。

rw007_ble get_addr

测试示例:

msh />rw007_ble get_addr
273 - ble_cmd_get_addr
msh />resp_type: 0, len: 12
RW007_BLE_RSP_TYPE_ADDR_GET
public_id_addr = 48:00:42:8c:47:c9
random_id_addr = 00:04:5c:43:00:00

BLE 功能扫描设备

目前RW007BLE 功能支持通过 scan命令,扫描周围的BLE设备。

  • 1.执行扫描周围BLE设备
rw007_ble scan

测试示例:

msh />rw007_ble scan
176 - ble_cmd_scan
msh />resp_type: 1, len: 65
RW007_BLE_NTF_TYPE_DISCOVER
received advertisement; event_type=0 rssi=-60 addr_type=0 addr=4c:ed:fb:00:04:b1
resp_type: 1, len: 56

这里的 addr地址内容可以为后面连接设备使用,由于目前没有直接显示对应 BLE 设备的名称,需要先人工确定 BLE 设备的地址。

  • 2.停止扫描周围BLE设备
    该指令用于 RW007模块正在执行scan扫描操作过程中,马上停止扫描的动作,可以执行此命令。
rw007_ble stop_scan

BLE 功能设备连接

目前 RW007 BLE 功能设备支持通过 BLE设备的类mac地址来连接设备,该命令仅支持主机模式去连接从机设备的类mac地址,连接成功后会有对应的conn_handle值,这个conn_handle值在其他功能沿用。

rw007_ble connect xx:xx:xx:xx:xx:xx

测试示例:

msh />rw007_ble connect 4c:ed:fb:00:04:b1
198 - ble_cmd_connect
str_addr: 4c:ed:fb:00:04:b1
mac addr: b1: 4: 0:fb:ed:4c
msh />resp_type: 1, len: 52
RW007_BLE_NTF_TYPE_CONNECT
type: 0, status: 0, conn_handle: 1

resp_type: 1, len: 44
RW007_BLE_NTF_TYPE_CONNECT_DESC
our_id_addr: c9:47:8c:42:00:48
peer_id_addr: b1:04:00:fb:ed:4c

BLE 功能设备断开连接

目前RW007BLE 功能设备支持主动断开从设备的连接。

rw007_ble disconnect [conn_handle]

其中 conn_handleconnect连接时的需要一致。

msh />rw007_ble disconnect 1
341 - ble_cmd_disconnect
msh />resp_type: 1, len: 52
RW007_BLE_NTF_TYPE_DISCONN

BLE 功能写数据

目前RW007BLE 功能写数据,支持通过指定UUID来写对应的数据。

rw007_ble write_uuid [conn_handle] [UUID] [data]

指令说明:[conn_handle]RW007连接设备时生成的对应值,[UUID]为对应 BLE设备的特征参数,[data]为需要发送的数据。

测试示例:

msh />rw007_ble write_uuid 1 ffe1 Hello,RT-Thread....
402 - ble_cmd_gattc_write_by_uuid
write conn_hanle:1 uuid:ffe1 data:Hello,RT-Thread....
write rc:0
msh />

figure1.png

BLE 功能更新notify 参数

目前 RW007 BLE 功能支持修改接收 notify参数。

rw007_ble notify_change [conn_handle] [char_value] [UUID]

参数说明:

[conn_handle]RW007连接设备时生成的对应值

[char_value] 为配置参数,具体如下

0:disable indication&notification
1:enable notification,disable indication
2:enable indication, disable notification

[UUID]为对应 BLE设备的特征参数值。

BLE 功能关于广播包类型的说明

目前 RW007 BLE 功能中可以关于广播包 event的类型可以分为下面几种。

/* Advertising report */
#define BLE_HCI_ADV_RPT_EVTYPE_ADV_IND      (0)
#define BLE_HCI_ADV_RPT_EVTYPE_DIR_IND      (1)
#define BLE_HCI_ADV_RPT_EVTYPE_SCAN_IND     (2)
#define BLE_HCI_ADV_RPT_EVTYPE_NONCONN_IND  (3)
#define BLE_HCI_ADV_RPT_EVTYPE_SCAN_RSP     (4)

广播包结构如下:

广播包结构描述:
struct ble_gap_disc_desc {
    uint8_t event_type;
    uint8_t length_data;
    ble_addr_t addr;
    int8_t rssi;
    uint8_t *data;

    /***
     * LE direct advertising report fields; direct_addr is BLE_ADDR_ANY if
     * direct address fields are not present.
     */
    ble_addr_t direct_addr;
};

通过event_type区分出不同的广播包类型,所有类型分为如下几种:
/* Advertising report */
0 : 普通广播数据包
1 : 直接广播包 
2 : 扫描请求包 
3 : 不可连接广播指示
4 : 扫描响应数据包

因此,在设备的scan扫描回应中可以通过对应的event_type来判定是哪种广播包的类型。

RW007的驱动代码中,可以通过下面的event_type来判断是哪种类型的广播包,可以让用户自行实现对应功能。

case RW007_BLE_NTF_TYPE_DISC:
        {
            rt_kprintf("RW007_BLE_NTF_TYPE_DISCOVER\n");
            ....
            switch(event_type)
            {
                case BLE_HCI_ADV_RPT_EVTYPE_ADV_IND:

                    break;
                case BLE_HCI_ADV_RPT_EVTYPE_DIR_IND:

                    break;
                case BLE_HCI_ADV_RPT_EVTYPE_SCAN_IND:

                    break;
                case BLE_HCI_ADV_RPT_EVTYPE_NONCONN_IND:

                    break;
                case BLE_HCI_ADV_RPT_EVTYPE_SCAN_RSP:

                    break;
            }
            ....
        }

测试示例:

  • 1.初始化 RW007BLE 功能为主机模式。
msh /> rw007_ble init central
263 - ble_cmd_init
start ble central
msh />
  • 2.执行BLE扫描指令
msh />rw007_ble scan 
318 - ble_cmd_scan
msh />resp_type: 1, len: 83
RW007_BLE_NTF_TYPE_DISCOVER
received advertisement; event_type=3 rssi=-56 addr_type=1 addr=41:ef:77:50:2b:29
mfg_data = 1e ff 06 00 01 09 20 02 5b 5d cd 33 a4 8c c7 c5 36 ac de 12 ab 17 87 89 e1 84 e9 19 ec c4 ad 

resp_type: 1, len: 83
RW007_BLE_NTF_TYPE_DISCOVER
received advertisement; event_type=3 rssi=-65 addr_type=1 addr=3c:2e:33:ea:8d:aa
mfg_data = 1e ff 06 00 01 09 20 02 28 4c cf 9b a7 f2 7d f6 c7 7e d9 54 ac dd 91 a6 ee 0d 22 43 e1 8f e3 

resp_type: 1, len: 83
RW007_BLE_NTF_TYPE_DISCOVER
received advertisement; event_type=3 rssi=-59 addr_type=1 addr=42:18:ab:80:5c:11
mfg_data = 1e ff 06 00 01 09 20 02 eb bf cf 15 97 aa 50 7f a4 57 25 d9 21 35 71 eb 6f aa a1 56 5c d5 e2 
  • 3.观察event_type=可以确定是哪种广播包类型。
RW007_BLE_NTF_TYPE_DISCOVER
received advertisement; event_type=0 rssi=-64 addr_type=1 addr=61:63:46:c3:49:99
mfg_data = 02 01 1a 02 0a 0c 0b ff 4c 00 10 06 00 19 1d 6d 63 18 

resp_type: 1, len: 52
RW007_BLE_NTF_TYPE_DISCOVER
received advertisement; event_type=4 rssi=-64 addr_type=1 addr=61:63:46:c3:49:99
mfg_data = 

resp_type: 1, len: 73
RW007_BLE_NTF_TYPE_DISCOVER
received advertisement; event_type=0 rssi=-73 addr_type=0 addr=91:21:52:00:4e:b8
mfg_data = 14 ff 4c 00 07 0f 00 02 20 91 21 52 00 4e b8 f5 58 58 3c 39 00 

以上的代码可以在RW007的软件驱动包中找到对应的位置,想深入了解的可以仔细阅读。

注意事项:

  • 1.请确保使用的RW007模块带BLE功能。
  • 2.RW007BLE 功能是基于SPI模式的,请确保RW007模块自身的版本为 2.0.2以上的版本。

附件:
链接:https://pan.baidu.com/s/1K2S5OWpPeGTJXkKC-_TV9A
提取码:5v5v

1 条评论

发布
问题