17. 无线串口
📝本节您将学习天巧星板载 2.4G 无线串口模块的使用方法,实现两块开发板之间或开发板与电脑之间的无线数据通信。
🏆本章目标
1️⃣ 了解 2.4G 无线串口模块的工作原理
2️⃣ 配置 UART7 与无线模块通信
3️⃣ 实现无线数据的透明传输
4️⃣ 掌握连接状态检测和 AT 指令控制
17.1 无线串口模块介绍
天巧星板载一个 2.4GHz 无线串口模块,通过 UART7 与 MCU 连接。该模块实现透明传输——MCU 通过串口发送的数据会自动通过无线射频发送到配对的另一端,对方收到的数据也会通过串口回传给 MCU。
从软件角度看,无线串口等同于一根"无形的串口线"——收发逻辑与有线串口完全一致。
基本参数
| 参数 | 规格 |
|---|---|
| 工作频段 | 2.4GHz ISM |
| 通信方式 | 透明传输(串口透传) |
| 串口接口 | UART7,PB17(TX) / PB18(RX) |
| 波特率 | 115200 baud(固定) |
| 连接状态引脚 | PB23(高电平 = 已连接) |
| 配对方式 | 自动配对(同一批次模块出厂已配对) |
硬件连接
MCU UART7 TX (PB17) ──→ 无线模块 RX
MCU UART7 RX (PB18) ←── 无线模块 TX
MCU GPIO (PB23) ←── 无线模块 LINK(连接状态)1
2
3
2
3
17.2 SysConfig 配置
UART7 配置
| 参数 | 设置值 | 说明 |
|---|---|---|
| Name | UART_WIRELESS | 实例名称 |
| UART Module | UART7 | |
| Baud Rate | 115200 | 与模块匹配 |
| Word Length | 8 Bits | |
| Parity | None | |
| Stop Bits | 1 | |
| Direction | TX and RX | |
| TX Pin | PB17 | |
| RX Pin | PB18 | |
| RX Interrupt | Enable | 接收中断 |
连接状态 GPIO 配置
| 参数 | 设置值 | 说明 |
|---|---|---|
| Name | GPIO_WIRELESS_LINK | |
| Port | PORTB | |
| Pin | 23 | |
| Direction | Input | |
| Internal Resistor | Pull-Down | 默认低(未连接) |
17.3 驱动实现
17.3.1 基础收发
c
#include "ti_msp_dl_config.h"
#include <string.h>
// 无线串口发送单字节
void wireless_send_byte(uint8_t data)
{
while (DL_UART_Main_isBusy(UART_WIRELESS_INST));
DL_UART_Main_transmitData(UART_WIRELESS_INST, data);
}
// 无线串口发送字符串
void wireless_send_string(const char *str)
{
while (*str) {
wireless_send_byte(*str++);
}
}
// 无线串口发送数据块
void wireless_send_data(uint8_t *data, uint16_t len)
{
for (uint16_t i = 0; i < len; i++) {
wireless_send_byte(data[i]);
}
}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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
17.3.2 接收缓冲区
c
#define WIRELESS_RX_BUF_SIZE 128
static volatile uint8_t rx_buffer[WIRELESS_RX_BUF_SIZE];
static volatile uint16_t rx_write_idx = 0;
static volatile uint16_t rx_read_idx = 0;
// 获取接收缓冲区中的数据量
uint16_t wireless_available(void)
{
return (rx_write_idx - rx_read_idx + WIRELESS_RX_BUF_SIZE) % WIRELESS_RX_BUF_SIZE;
}
// 读取一个字节(非阻塞,需先检查 available)
uint8_t wireless_read_byte(void)
{
uint8_t data = rx_buffer[rx_read_idx];
rx_read_idx = (rx_read_idx + 1) % WIRELESS_RX_BUF_SIZE;
return data;
}
// 读取指定长度数据
uint16_t wireless_read(uint8_t *buf, uint16_t max_len)
{
uint16_t count = 0;
while (wireless_available() > 0 && count < max_len) {
buf[count++] = wireless_read_byte();
}
return count;
}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
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
17.3.3 中断接收
c
// UART7 中断服务函数
void UART7_IRQHandler(void)
{
switch (DL_UART_Main_getPendingInterrupt(UART_WIRELESS_INST)) {
case DL_UART_MAIN_IIDX_RX:
{
uint8_t data = DL_UART_Main_receiveData(UART_WIRELESS_INST);
uint16_t next_idx = (rx_write_idx + 1) % WIRELESS_RX_BUF_SIZE;
if (next_idx != rx_read_idx) { // 缓冲区未满
rx_buffer[rx_write_idx] = data;
rx_write_idx = next_idx;
}
break;
}
default:
break;
}
}1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
17.3.4 连接状态检测
c
// 检测无线连接状态(PB23)
// 返回 1 = 已连接, 0 = 未连接
uint8_t wireless_is_linked(void)
{
return (DL_GPIO_readPins(GPIO_WIRELESS_LINK_PORT,
GPIO_WIRELESS_LINK_PIN_PIN) != 0) ? 1 : 0;
}1
2
3
4
5
6
7
2
3
4
5
6
7
连接状态说明
PB23 引脚由无线模块驱动:
- 高电平:模块已与对端建立连接,数据可正常收发
- 低电平:模块未连接或对端不在范围内
出厂固件中对连接状态做了约 3 秒的软件滤波,避免瞬间断连误判。
17.4 AT 指令控制
无线模块支持简单的 AT 指令进行配置:
| 指令 | 功能 | 响应 |
|---|---|---|
AT_RF=ON\r\n | 开启射频(启动无线通信) | 模块开始搜索配对 |
AT_RF=OFF\r\n | 关闭射频(关闭无线通信) | 模块停止通信,降低功耗 |
c
// 开启无线射频
void wireless_rf_on(void)
{
wireless_send_string("AT_RF=ON\r\n");
}
// 关闭无线射频
void wireless_rf_off(void)
{
wireless_send_string("AT_RF=OFF\r\n");
}1
2
3
4
5
6
7
8
9
10
11
2
3
4
5
6
7
8
9
10
11
注意
AT 指令需要在模块未进入数据透传状态时发送。通常在初始化阶段发送。模块上电后默认开启射频,一般无需手动发送 AT_RF=ON。
17.5 实验一:无线回显
天巧星通过无线模块接收数据,原样转发回去(类似有线串口回显):
c
#include "ti_msp_dl_config.h"
#include <stdio.h>
void uart_send_string(const char *str)
{
while (*str) {
while (DL_UART_Main_isBusy(UART_0_INST));
DL_UART_Main_transmitData(UART_0_INST, *str++);
}
}
int main(void)
{
SYSCFG_DL_init();
NVIC_EnableIRQ(UART_WIRELESS_INT_IRQN);
uart_send_string("Wireless echo ready.\r\n");
char buf[64];
while (1) {
// 检测连接状态
if (wireless_is_linked()) {
// 有数据则回显
while (wireless_available() > 0) {
uint8_t ch = wireless_read_byte();
wireless_send_byte(ch); // 无线回发
// 同时在调试串口显示
sprintf(buf, "RX: 0x%02X ('%c')\r\n", ch, ch);
uart_send_string(buf);
}
}
delay_cycles(800000); // 10ms 轮询
}
}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
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
17.6 实验二:无线控制 LED
对端发送 '1' 点亮 LED,发送 '0' 熄灭 LED:
c
#include "ti_msp_dl_config.h"
int main(void)
{
SYSCFG_DL_init();
NVIC_EnableIRQ(UART_WIRELESS_INT_IRQN);
while (1) {
if (wireless_available() > 0) {
uint8_t cmd = wireless_read_byte();
switch (cmd) {
case '1':
DL_GPIO_clearPins(GPIO_LED_PORT, GPIO_LED_PIN_PIN); // LED亮
wireless_send_string("LED ON\r\n");
break;
case '0':
DL_GPIO_setPins(GPIO_LED_PORT, GPIO_LED_PIN_PIN); // LED灭
wireless_send_string("LED OFF\r\n");
break;
}
}
delay_cycles(800000);
}
}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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
17.7 实验三:无线数据上报
定时通过无线串口发送传感器数据:
c
#include "ti_msp_dl_config.h"
#include <stdio.h>
int main(void)
{
SYSCFG_DL_init();
NVIC_EnableIRQ(UART_WIRELESS_INT_IRQN);
char buf[64];
uint32_t count = 0;
while (1) {
if (wireless_is_linked()) {
// 假设已读取 ADC 值
uint16_t adc_val = 2048; // 实际替换为真实采集值
sprintf(buf, "T:%lu ADC:%u\r\n", count++, adc_val);
wireless_send_string(buf);
}
delay_cycles(80000000); // 1 秒发送一次(80MHz)
}
}1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
17.8 调试串口与无线串口协同
天巧星有两个独立串口通道:
- UART0(PA10/PA11)→ CH340 USB → 电脑(调试输出)
- UART7(PB17/PB18)→ 无线模块 → 远程设备
两者互不干扰,可以同时使用:UART0 输出调试日志,UART7 收发业务数据。
c
// 调试串口输出(本地,USB连电脑)
uart_send_string("Debug: received wireless data\r\n");
// 无线串口输出(远程,射频发送)
wireless_send_string("ACK\r\n");1
2
3
4
5
2
3
4
5
17.9 知识总结
| 内容 | 说明 |
|---|---|
| 模块类型 | 2.4GHz 无线串口透传 |
| 通信接口 | UART7,115200 baud |
| 引脚 | PB17(TX) / PB18(RX) / PB23(LINK) |
| 使用方式 | 等同于有线串口,透明传输 |
| 连接检测 | PB23 高电平 = 已连接 |
| AT 指令 | AT_RF=ON/OFF 控制射频开关 |
| 配对 | 出厂已配对,即插即用 |
下一章将学习使用 PWM 驱动天巧星板载的无源蜂鸣器,实现音调控制和旋律播放。