LV03-04-天猫蓝牙Mesh开发-02-呼吸灯实现

本文主要是天猫蓝牙Mesh开发——SDK目录解析的相关笔记,若笔记中有错误或者不合适的地方,欢迎批评指正😃。

点击查看使用工具及版本
Windows版本 windows11
Ubuntu版本 Ubuntu22.04的64位版本
VMware® Workstation 16 Pro 16.2.3 build-19376536
终端软件 MobaXterm(Professional Edition v23.0 Build 5042 (license))
点击查看本文参考资料
分类 网址 说明
官方网站 阿里云 阿里云官网主页
阿里生活物联平台 生活物联网平台(飞燕平台)主页
AliGenie 天猫精灵开放平台AliGenie主页
阿里物联网平台 阿里物联网平台主页
Bluetooth 技术网站 蓝牙协议规范什么的可以来这里找
Telink Telink | Chips for a Smarter IoT (telink-semi.com)
Telink中文官网
开发手册 AliOS Things开发指南 AliOS Things开发指南,这里是最新版本,可以直接从官网找到
AliOS Things开发指南 AliOS Things应用开发指南,这里应该是3.3版本的完整开发文档
AliOS Things开发指南(3.0) AliOS Things应用开发指南,这里应该是3.0版本的完整开发文档
生活物联网平台开发文档 生活物联网平台(飞燕平台)开发文档
《设备端开发指南》
Wi-Fi IoT品类定义与功能开发 天猫精灵IoT开放平台——Wi-Fi IoT品类定义与功能开发
硬件平台 mk3080 WiFi开发板 WiFi开发板使用指南-阿里云开发者社区
esp8266开发板 一个教程:ESP8266-NodeMCU开发板详解-太极创客 (taichi-maker.com)
TLSR8258 Datasheet Datasheet for Telink BLE + IEEE802.15.4 MultiStandard Wireless SoC TLSR8258
参考资料 AliOS Things 3.0 应用开发指南 这个只是一篇参考文章,里面是一些环境搭建相关的,可以参考
IP知识百科 - 华为 (huawei.com) IP的一些相关知识点
点击查看相关文件下载
分类 网址 说明
蓝牙规范相关文档 Core Specification 5.2 核心规格 5.2,该规范定义了创建可互操作的Bluetooth 设备所需的技术。
《Core_v5.2.pdf》
Mesh Model(v1.1) 本Bluetooth 规范定义了模型(以及它们所需的状态和消息),这些模型用于在mesh 网络中的节点上执行基本功能,超出了Bluetooth Mesh 配置文件 规范中定义的基础模型。
本规范包括定义跨设备类型标准功能的通用模型,以及支持关键mesh 场景的模型,如照明控制、传感器、时间和场景。
《MshMDL_v1.1.pdf》
Mesh Profile(v1.0.1) 该Bluetooth 规范定义了基本要求,以实现可互操作的mesh 网络解决方案,用于Bluetooth 低能量无线技术。
《MshPRFv1.0.1.pdf》
Mesh Device Properties 本规范包含Bluetooth Mesh 配置文件 和Bluetooth Mesh 模型规范所要求的设备属性的定义。
但是跟之前的有些区别,我主要看的之前的版本:《MMeshDeviceProperties_v1.2.pdf》
GATT Specification Supplement GATT Specification Supplement | Bluetooth® Technology Website。
好像可以在线看:《GATT Specification Supplement》
Assigned Numbers GATT的一些类型定义可以在这里找。
AliOS Things alios-things/AliOS-Things Gitee上的AliOSThings SDK源码仓库
alibaba/AliOS-Things GitHub上的AliOSThings SDK源码仓库
天猫精灵蓝牙Mesh协议栈 alibaba-archive/genie-bt-mesh-stack GitHub上的天猫精灵蓝牙Mesh协议栈源码仓库。
之前是在alibaba/genie-bt-mesh-stack这个仓库。
写笔记的时候最新提交为faf523618a6a2560090fc423222b9db80984bb7a
蓝牙Mesh设备开发指南 阿里云生活服务平台开发手册——蓝牙设备开发一节中的内容

一、GPIO

需要知道的是,这个蓝牙开发板里面其实使用的也是AliOSThings的内核,所以对于GPIO的一些接口函数什么的,跟前面学习AliOSThings的时候是一样的。

1. 官方文档

我们可以看一下官方文档:GPIO · 开发指南 (alicdn.com),这里有对GPIO接口函数的详细说明:

image-20231224192923184

2. 使用实例

2.1 led.c

点击查看详情
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include <stdio.h>
#include <aos/aos.h>
#include <hal/soc/gpio.h>
#include "drivers/8258/gpio_8258.h"

#define LED0_PIN TC825X_GET_PIN_NUM(GPIO_PB4) // RED——PC1
#define LED1_PIN TC825X_GET_PIN_NUM(GPIO_PB5) // GREEN——PC1
#define LED2_PIN TC825X_GET_PIN_NUM(GPIO_PC1) // BLUE——PC1

static gpio_dev_t led_dev[3];

int32_t sys_led_init(void)
{
int32_t ret = 0;
led_dev[0].port = LED0_PIN; /* gpio port config */
led_dev[0].config = OUTPUT_PUSH_PULL;/* set as output mode */
ret = hal_gpio_init(&led_dev[0]); /* configure GPIO with the given settings */
if (ret != 0) {
printf("gpio init error !\n");
}

led_dev[1].port = LED1_PIN; /* gpio port config */
led_dev[1].config = OUTPUT_PUSH_PULL;/* set as output mode */
ret = hal_gpio_init(&led_dev[1]); /* configure GPIO with the given settings */
if (ret != 0) {
printf("gpio init error !\n");
}

led_dev[2].port = LED2_PIN; /* gpio port config */
led_dev[2].config = OUTPUT_PUSH_PULL;/* set as output mode */
ret = hal_gpio_init(&led_dev[2]); /* configure GPIO with the given settings */
if (ret != 0) {
printf("gpio init error !\n");
}

return 0;
}
int application_start(int argc, char **argv)
{
printf("BUILD_TIME:%s\n", __DATE__","__TIME__);
sys_led_init();
while(1)
{
hal_gpio_output_toggle(&led_dev[0]);
hal_gpio_output_toggle(&led_dev[1]);
hal_gpio_output_toggle(&led_dev[2]);
aos_msleep(1000);
}
return 0;
}

2.2 led.mk

点击查看详情
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
NAME := led

$(NAME)_COMPONENTS += genie_app bluetooth.bt_host bluetooth.bt_mesh yloop cli

$(NAME)_INCLUDES += ../../../../genie_app \
../../../../genie_app/base \
../../../../genie_app/bluetooth/host \
../../../../genie_app/bluetooth/mesh

$(NAME)_SOURCES := led.c

ifeq ($(HOST_MCU_FAMILY),ch6121)
bt_host_tinycrypt = 0
else ifeq ($(HOST_MCU_FAMILY),tg7100b)
bt_host_tinycrypt = 0
else
ble = 1
bt_mesh = 1
endif
en_bt_smp = 1

# Host configurations
GLOBAL_DEFINES += CONFIG_BLUETOOTH
ifeq ($(HOST_MCU_FAMILY),ch6121)
GLOBAL_DEFINES += CONFIG_BT_RX_BUF_LEN=252
GLOBAL_DEFINES += CONFIG_BT_L2CAP_TX_MTU=244
GLOBAL_DEFINES += CONFIG_BT_MESH_ADV_BLOCK
else ifeq ($(HOST_MCU_FAMILY),tg7100b)
GLOBAL_DEFINES += CONFIG_BT_RX_BUF_LEN=252
GLOBAL_DEFINES += CONFIG_BT_L2CAP_TX_MTU=244
GLOBAL_DEFINES += CONFIG_BT_MESH_ADV_BLOCK
else
GLOBAL_DEFINES += CONFIG_BT_SMP
GLOBAL_DEFINES += CONFIG_BT_HCI_VS_EXT
GLOBAL_DEFINES += CONFIG_BT_TINYCRYPT_ECC
endif
GLOBAL_DEFINES += CONFIG_BT_CONN
#GLOBAL_DEFINES += CONFIG_BT_CENTRAL
GLOBAL_DEFINES += CONFIG_BT_PERIPHERAL
#GLOBAL_DEFINES += CONFIG_BLE_50


# Mesh function select
GLOBAL_DEFINES += CONFIG_BT_MESH
GLOBAL_DEFINES += CONFIG_BT_MESH_PROV
GLOBAL_DEFINES += CONFIG_BT_MESH_PB_ADV
GLOBAL_DEFINES += CONFIG_BT_MESH_PB_GATT
GLOBAL_DEFINES += CONFIG_BT_MESH_PROXY
GLOBAL_DEFINES += CONFIG_BT_MESH_GATT_PROXY
GLOBAL_DEFINES += CONFIG_BT_MESH_RELAY
#GLOBAL_DEFINES += CONFIG_BT_MESH_FRIEND
#GLOBAL_DEFINES += CONFIG_BT_MESH_LOW_POWER
#GLOBAL_DEFINES += CONFIG_BT_MESH_SHELL
#GLOBAL_DEFINES += CONFIG_BT_MESH_BQB
#GLOBAL_DEFINES += CONFIG_BT_MESH_IV_UPDATE_TEST

# Mesh foundation model select
GLOBAL_DEFINES += CONFIG_BT_MESH_CFG_SRV
GLOBAL_DEFINES += CONFIG_BT_MESH_HEALTH_SRV

# Mesh debug message enable
#GLOBAL_DEFINES += USE_BT_MESH_CUSTOM_LOG
GLOBAL_DEFINES += GENIE_DEBUG_COLOR
GLOBAL_DEFINES += MESH_DEBUG_RX
GLOBAL_DEFINES += MESH_DEBUG_TX
GLOBAL_DEFINES += MESH_DEBUG_PROV

#GLOBAL_DEFINES += CONFIG_INIT_STACKS
#GLOBAL_DEFINES += CONFIG_PRINTK

GLOBAL_INCLUDES += ../

GLOBAL_DEFINES += PROJECT_SW_VERSION=0x00010000
GLOBAL_DEFINES += PROJECT_SECRET_KEY=\"00112233445566770011223344556677\"

MESH_MODEL_GEN_ONOFF_SRV = 1
MESH_MODEL_DIABLE_TRANS = 1
#MESH_MODEL_LIGHTNESS_SRV = 1
#MESH_MODEL_CTL_SRV = 1
#MESH_MODEL_VENDOR_SRV = 1

ALI_SIMPLE_MODLE = 1

GLOBAL_DEFINES += GENIE_OLD_AUTH

3. 编译工程

1
aos make bluetooth.led@tc825x

4. 烧录程序

image-20231224194249860

5. 实验现象

串口会有以下打印信息:

image-20231224194329068

然后我们会看到三个灯同时间隔1秒闪烁。

二、PWM

1. 官方文档

文档接口说明在这里:PWM · 开发指南 (alicdn.com)

image-20231224194453787

2. 使用实例

2.1 pwm.c

点击查看详情
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#include <stdio.h>
#include <aos/aos.h>
#include <hal/soc/gpio.h>
#include <hal/soc/pwm.h>
#include "drivers/8258/gpio_8258.h"

#define BLUE_LED_PWM
#define LED0_PIN TC825X_GET_PIN_NUM(GPIO_PB4) // RED——PC1
#define LED1_PIN TC825X_GET_PIN_NUM(GPIO_PB5) // GREEN——PC1
#define LED2_PIN TC825X_GET_PIN_NUM(GPIO_PC1) // BLUE——PC1

static gpio_dev_t led_dev[3] = {0};

int32_t sys_led_init(void)
{
int32_t ret = 0;
led_dev[0].port = LED0_PIN; /* gpio port config */
led_dev[0].config = OUTPUT_PUSH_PULL;/* set as output mode */
ret = hal_gpio_init(&led_dev[0]); /* configure GPIO with the given settings */
if (ret != 0) {
printf("gpio init error !\n");
}

led_dev[1].port = LED1_PIN; /* gpio port config */
led_dev[1].config = OUTPUT_PUSH_PULL;/* set as output mode */
ret = hal_gpio_init(&led_dev[1]); /* configure GPIO with the given settings */
if (ret != 0) {
printf("gpio init error !\n");
}
#ifndef BLUE_LED_PWM
led_dev[2].port = LED2_PIN; /* gpio port config */
led_dev[2].config = OUTPUT_PUSH_PULL;/* set as output mode */
ret = hal_gpio_init(&led_dev[2]); /* configure GPIO with the given settings */
if (ret != 0) {
printf("gpio init error !\n");
}
#endif
printf("++++++++++ led init! ++++++++++\n");
return ret;
}

#ifdef BLUE_LED_PWM
void hal_pwm_app_dynamic_out(void)
{
int cnt;
int32_t ret;
pwm_dev_t pwm = {0};

printf("hal_pwm_app_dynamic_out start\r\n");

pwm.port = LED2_PIN;
pwm.config.freq = 1000;
pwm.config.duty_cycle = 0.00;
pwm.priv = NULL;

ret = hal_pwm_init(&pwm);
if(ret){
printf("hal_pwm_init fail,ret:%d\r\n",ret);
return;
}

hal_pwm_start(&pwm);

cnt = 10;
while (cnt--) {
printf("duty_cycle count up cnt=%d\r\n", cnt);
pwm_config_t para = {0.000, 1000};
for (int i = 0; i < 100; i++) {
para.duty_cycle += 0.01;
aos_msleep(10);
hal_pwm_para_chg(&pwm, para);
}

printf("duty_cycle count down cnt=%d\r\n", cnt);

para.duty_cycle = 1.0;
para.freq = 1000;
for (int i = 0; i < 100; i++) {
para.duty_cycle -= 0.01;
aos_msleep(10);
hal_pwm_para_chg(&pwm, para);
}
}

hal_pwm_stop(&pwm);

hal_pwm_finalize(&pwm);

printf("hal_pwm_app_dynamic_out end!\r\n");
}
#endif

int application_start(int argc, char **argv)
{
printf("BUILD_TIME:%s\n", __DATE__","__TIME__);
sys_led_init();
#ifdef BLUE_LED_PWM
hal_pwm_app_dynamic_out();
#endif
while(1)
{
hal_gpio_output_toggle(&led_dev[0]);
aos_msleep(1000);
}
return 0;
}

2.2 pwm.mk

点击查看详情
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
NAME := pwm

$(NAME)_COMPONENTS += genie_app bluetooth.bt_host bluetooth.bt_mesh yloop cli

$(NAME)_INCLUDES += ../../../../genie_app \
../../../../genie_app/base \
../../../../genie_app/bluetooth/host \
../../../../genie_app/bluetooth/mesh

$(NAME)_SOURCES := pwm.c

ifeq ($(HOST_MCU_FAMILY),ch6121)
bt_host_tinycrypt = 0
else ifeq ($(HOST_MCU_FAMILY),tg7100b)
bt_host_tinycrypt = 0
else
ble = 1
bt_mesh = 1
endif
en_bt_smp = 1

# Host configurations
GLOBAL_DEFINES += CONFIG_BLUETOOTH
ifeq ($(HOST_MCU_FAMILY),ch6121)
GLOBAL_DEFINES += CONFIG_BT_RX_BUF_LEN=252
GLOBAL_DEFINES += CONFIG_BT_L2CAP_TX_MTU=244
GLOBAL_DEFINES += CONFIG_BT_MESH_ADV_BLOCK
else ifeq ($(HOST_MCU_FAMILY),tg7100b)
GLOBAL_DEFINES += CONFIG_BT_RX_BUF_LEN=252
GLOBAL_DEFINES += CONFIG_BT_L2CAP_TX_MTU=244
GLOBAL_DEFINES += CONFIG_BT_MESH_ADV_BLOCK
else
GLOBAL_DEFINES += CONFIG_BT_SMP
GLOBAL_DEFINES += CONFIG_BT_HCI_VS_EXT
GLOBAL_DEFINES += CONFIG_BT_TINYCRYPT_ECC
endif
GLOBAL_DEFINES += CONFIG_BT_CONN
#GLOBAL_DEFINES += CONFIG_BT_CENTRAL
GLOBAL_DEFINES += CONFIG_BT_PERIPHERAL
#GLOBAL_DEFINES += CONFIG_BLE_50


# Mesh function select
GLOBAL_DEFINES += CONFIG_BT_MESH
GLOBAL_DEFINES += CONFIG_BT_MESH_PROV
GLOBAL_DEFINES += CONFIG_BT_MESH_PB_ADV
GLOBAL_DEFINES += CONFIG_BT_MESH_PB_GATT
GLOBAL_DEFINES += CONFIG_BT_MESH_PROXY
GLOBAL_DEFINES += CONFIG_BT_MESH_GATT_PROXY
GLOBAL_DEFINES += CONFIG_BT_MESH_RELAY
#GLOBAL_DEFINES += CONFIG_BT_MESH_FRIEND
#GLOBAL_DEFINES += CONFIG_BT_MESH_LOW_POWER
#GLOBAL_DEFINES += CONFIG_BT_MESH_SHELL
#GLOBAL_DEFINES += CONFIG_BT_MESH_BQB
#GLOBAL_DEFINES += CONFIG_BT_MESH_IV_UPDATE_TEST

# Mesh foundation model select
GLOBAL_DEFINES += CONFIG_BT_MESH_CFG_SRV
GLOBAL_DEFINES += CONFIG_BT_MESH_HEALTH_SRV

# Mesh debug message enable
#GLOBAL_DEFINES += USE_BT_MESH_CUSTOM_LOG
GLOBAL_DEFINES += GENIE_DEBUG_COLOR
GLOBAL_DEFINES += MESH_DEBUG_RX
GLOBAL_DEFINES += MESH_DEBUG_TX
GLOBAL_DEFINES += MESH_DEBUG_PROV

#GLOBAL_DEFINES += CONFIG_INIT_STACKS
#GLOBAL_DEFINES += CONFIG_PRINTK

GLOBAL_INCLUDES += ../

GLOBAL_DEFINES += PROJECT_SW_VERSION=0x00010000
GLOBAL_DEFINES += PROJECT_SECRET_KEY=\"00112233445566770011223344556677\"

MESH_MODEL_GEN_ONOFF_SRV = 1
MESH_MODEL_DIABLE_TRANS = 1
#MESH_MODEL_LIGHTNESS_SRV = 1
#MESH_MODEL_CTL_SRV = 1
#MESH_MODEL_VENDOR_SRV = 1

ALI_SIMPLE_MODLE = 1

GLOBAL_DEFINES += GENIE_OLD_AUTH

3. 编译工程

1
aos make bluetooth.pwm@tc825x

4. 烧录程序

image-20231224202057014

5. 实验现象

image-20231224202202551

蓝灯会由亮变暗,再由暗变量重复10次,然后红灯开始闪烁。