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

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

  1. 打开项目文件。

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

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

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

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

测试验证

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

测试阶段

  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: 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模式配置,header设置ppt_set_tx_header(),并准备待发送的数据ppt_push_tx_data()

备注

PRX角色还演示了对2402MHz到2480MHz共79个信道做PSD信道扫描的过程。

主循环

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