Simple TRX
本示例用两个工程分别扮演 PTX 和 PRX 两个角色,他们之间可以互相发送消息,演示 Radio 的基本使用方法。
环境需求
该示例支持以下开发工具包:
Hardware Platforms |
Board Name |
---|---|
RTL87x2G HDK |
RTL87x2G EVB |
更多要求,请参考 快速入门 。
硬件连线
请参考 快速入门 中的 EVB 接口和模块 。
配置选项
APP 所有需要配置的内容在 samples\ppt\ppt_simple_trx\src\ppt_cfg.h
中。
默认配置为 BLE Advertising 所需的,开发者可以根据实际需求配置。
编译和下载
该示例可以在 SDK 文件夹中找到:
Project file:
samples\ppt\ppt_simple_trx\proj_ptx\rtl87x2g\mdk
samples\ppt\ppt_simple_trx\proj_prx\rtl87x2g\mdk
Project file:
samples\ppt\ppt_simple_trx\proj_ptx\rtl87x2g\gcc
samples\ppt\ppt_simple_trx\proj_prx\rtl87x2g\gcc
编译和运行该示例请遵循以下步骤:
测试验证
将两个示例工程分别烧录到两个 EVB 板后,然后使用 Debug Analyzer
工具获取日志查看运行结果。
测试阶段
PTX EVB 按下 reset 按钮后,将开始模拟 BLE Adv 发送广播,并监听应答。PTX 角色是周期性发包,在空闲时可以进入睡眠模式。 如果成功启动,会打印下面的日志。
[APP] !**ptx: statistics tx 95, rx 70, miss 25(26.315%)
PRX EVB 按下 reset 按钮后,将开始模拟 BLE Scan 监听广播,并发送应答。 如果成功启动,会打印下面的日志。
[APP] !**prx: rx count 1774, int count 2969, rx stack 0x0008, entry 0, hp 0x00, len 15, hs 0x00000000, payload 01 02 01 02 01 01 02 01 05 05 09 32 2E 34 67
注意
因为默认配置是 BLE Advertising Channel 37 信道,即 2402MHz,所以 PTX 可能会听到 BLE 设备应答,PRX 可能会听到 BLE 设备的广播。
可以修改 FREQUENCY
等参数避开 BLE 设备。
代码介绍
本章的主要目的是帮助 APP 开发人员熟悉相关的开发流程。本章将按照以下几个部分进行介绍:
章节 源码路径 中介绍工程和源文件路径。
章节 初始化 中介绍启动流程。
章节 PTX/PRX 流程 中介绍 Radio 初始化和运行流程。
源码路径
Project directory:
samples\ppt\ppt_simple_trx\proj_xxx
Source code directory:
samples\ppt\ppt_simple_trx\src
应用程序项目中的源文件当前被分为几个组,如下所示。
└── Project: ptx(prx)
└── secure_only_app
├── Device includes startup code
├── CMSE Library Non-secure callable lib
├── Lib includes all binary symbol files that user application is built on
├── ROM_NS.lib
├── lowerstack.lib
├── rtl87x2g_sdk.lib
└── rtl87x2g_io.lib
├── Peripheral includes all peripheral drivers and module code used by the application
├── ppt driver includes radio driver
├── ppt_driver.c includes hw registers operation
└── ppt_simple.c includes hw management and sw wrapper
└── APP includes the ble_peripheral user application implementation
├── main_ns.c includes the io, os and platform initialization
├── app_task.c includes app task initialization and main loop
├── ppt_cfg.c includes the radio configurations
└── ppt_ptx(prx).c includes ptx and prx operations and interrupt routine
初始化
当 EVB 板启动并且芯片被重置时, main
函数将被调用,它执行以下初始化函数:
int main(void)
{
DBG_DIRECT("Non-Secure World: main");
#if DLPS_EN
pwr_mgr_init();
#endif
app_task_init();
/* Start scheduler. */
os_sched_start();
/* Should not reach here as the scheduler is already started. */
for (; ;)
{
}
}
pwr_mgr_init
用于初始化 DLPS 功耗管理模块。app_task_init
用于初始化 app task,注册 task 主函数app_main_task
。os_sched_start
用于启动 os scheduler。当调度器启动后,app task 的主函数app_main_task
会得到调度,最终调用 PTX/PRX 的主函数ppt_demo
,示例如下。
void app_main_task(void *p_param)
{
uint8_t event;
/* This task calls secure side functions. So allocate a secure context for
* it. */
//must locate at the first line
os_alloc_secure_ctx(configMINIMAL_SECURE_STACK_SIZE);
/* avoid ppt initialization conflict with ble psd procedure */
os_delay(1000);
void ppt_demo(void);
ppt_demo();
while (1);
}
PTX/PRX 流程
ppt_demo
会初始化并配置 Radio,然后启动 PTX 或者 PRX 的循环。
初始化和配置
ppt_cfg
会初始化并配置 Radio,包括频率、PHY 类型、帧格式、CRC 参数、whitening 参数、access address 和发射功率等。ppt_reg_handler()
注册 Radio 的中断处理函数。配置 PTX 和 PRX 模式,设置 packet header
ppt_set_tx_header()
,并准备待发送的数据ppt_push_tx_data()
。
备注
PRX 角色还演示了对 2402 MHz 到 2480 MHz 共 79 个信道做 PSD 信道扫描的过程,示例如下。
/*
* psd sample code
* get the channel rssi of different frequencies from 2402 to 2480 MHz
*/
for (uint8_t loop = 0; loop < 79; loop++)
{
ppt_psd_mode_ext_t param =
{
{
.chann_start = loop,
.chann_stop = loop,
.chann_step = 1,
.mode = 0,
.timeout = PSD_TIMEOUT_DEFAULT
}
};
ppt_set_psd_mode_ext(¶m);
ppt_enable_psd(NULL);
}
for (uint8_t loop = 0; loop < 79; loop++)
{
int16_t rssi = ppt_get_psd_result(loop);
DBG_DIRECT("PSD: freq %dMHz rssi = %ddBm", 2402 + loop, rssi);
}
主循环
PTX 会循环发送数据,流程如下:
准备数据。
使能 PTX,并等待完成。
修改数据内容和长度。
如果是 periodic 模式,则延时 100ms,间隔发送。
返回步骤 1。
PRX 会循环监听,流程如下:
如果是 oneshot 模式,流程如下:
使能 PRX,并等待完成。
延时 10ms,让 platform 有时间输出 log。
返回步骤 1。
如果是 continuous 模式,PRX 会持续使能,流程如下:
使能 PRX,不等待完成。
延时 10ms,让 platform 有时间输出 log。
返回步骤 2。
Radio 中断处理
在使能 PTX 或 PRX 后,Radio 会运行并触发相应的中断,通过 ppt_reg_handler()
注册的中断函数会得到调用。
中断的类型可以参考 中断。
PTX 会处理
tx_int
。如果打开 ACK 模式,还会处理
rx_int
。如果打开 periodic 模式,会处理
tx_early_int
。当 periodic 模式的重传次数达到要求后,会在 TRX 中断里关闭 PTX,然后需要再处理kill_ptx_int
。
PRX 会处理
rx_int
。如果打开 ACK 模式,还会处理
tx_int
。