Simple TRX

本示例用两个工程分别扮演 PTX 和 PRX 两个角色,他们之间可以互相发送消息,演示 Radio 的基本使用方法。

环境需求

该示例支持以下开发工具包:

开发套件

Hardware Platforms

Board Name

RTL8752H HDK

RTL8752H EVB

更多要求,请参考 快速入门

硬件连线

请参考 快速入门 中的 RTL8752H EVB 接口和模块

配置选项

APP 所有需要配置的内容在 src\app\ppt_simple_trx\ppt_cfg.h 中。 默认配置为 BLE Advertising 所需的,开发者可以根据实际需求配置。

编译和下载

该示例可以在 SDK 文件夹中找到:

Project file:

board\evb\ppt_simple_ptx\mdk
board\evb\ppt_simple_prx\mdk

Project file:

board\evb\ppt_simple_ptx\gcc
board\evb\ppt_simple_ptx\gcc

编译和运行该示例请遵循以下步骤:

  1. 打开项目文件。

  2. 要编译目标文件,请参考 快速入门编译 APP Image 中列出的步骤。

  3. 编译成功后,项目文件所在目录的子目录 bin 下 会生成 app bin 文件 app_MP_sdk_xxx.bin

  4. 要下载 app bin 到 EVB 板,请参考 快速入门MP Tool 中列出的步骤。

  5. 在 EVB 板上按下 reset 按钮,程序将开始运行。

测试验证

将两个示例工程分别烧录到两个 EVB 板后,然后使用 Debug Analyzer 工具获取日志查看运行结果。

测试阶段

  1. PTX EVB 按下 reset 按钮后,将开始模拟 BLE Adv 发送广播,并监听应答。PTX 角色是周期性发包,在空闲时可以进入睡眠模式。 如果成功启动,会打印下面的日志。

    [APP] !**ptx: statistics tx 95, rx 70, miss 25(26.315%)
    
  2. 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 开发人员熟悉相关的开发流程。本章将按照以下几个部分进行介绍:

源码路径

  • Project directory: board\evb\ppt_simple_ptx

  • Source code directory: src\app\ppt_simple_trx

应用程序项目中的源文件当前被分为几个组,如下所示。

└── Project: ptx(prx)
    └── secure_only_app
        ├── cmsis                    includes startup code
        ├── Lib                      includes all binary symbol files that user application is built on
            ├── ROM_NS.lib
            ├── lowerstack.lib
            └── rtl8752h_sdk.lib
        ├── platform                 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
            ├── overlay_mgr.c        includes image section management
            ├── 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)
{
     extern uint32_t random_seed_value;
     srand(random_seed_value);

     board_init();
     pwr_mgr_init();
     task_init();
     os_sched_start();

     return 0;
}
  • board_init() 用于初始化 IO 模块。

  • pwr_mgr_init() 用于初始化 DLPS 功耗管理模块。

  • 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)
{
     /* avoid conflict with ble psd procedure */
     os_delay(1000);

     while (1)
     {
         void ppt_demo(void);
         ppt_demo();
     }
}

PTX/PRX 流程

ppt_demo() 会初始化并配置 Radio,然后启动 PTX 或者 PRX。

初始化和配置

  • ppt_cfg() 会初始化并配置 Radio,包括频率、PHY 类型、帧格式、CRC 参数、whitening 参数、access address 和发射功率等。

  • ppt_reg_handler() 注册 Radio 的中断处理函数。

  • PTX 和 PRX 模式配置,header 设置 ppt_set_tx_header(),并准备待发送的数据 ppt_push_tx_data()

    备注

    PRX 角色还演示了对 2402MHz 到 2480MHz 共 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(&param);
        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 会循环发送数据,流程如下:

    1. 准备数据。

    2. 使能 PTX,并等待完成。

    3. 修改数据内容和长度。

    4. 如果是 periodic 模式,则延时 100ms,间隔发送。

    5. 返回步骤 1。

  • PRX 会循环监听,流程如下:

    • 如果是 oneshot 模式,流程如下:

      1. 使能 PRX,并等待完成。

      2. 延时 10ms,让 platform 有时间输出 log。

      3. 返回步骤 1。

    • 如果是 continuous 模式,PRX 会持续使能,流程如下:

      1. 使能 PRX,不等待完成。

      2. 延时 10ms,让 platform 有时间输出 log。

      3. 返回步骤 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