当前页面为 开发中 版本,查看特定版本的文档,请在页面左下角的下拉菜单中进行选择。

BLE PRF SAMPLE

1 功能概述

此项目演示 BLE 从机和 2.4G 同时工作的双模场景,其在蓝牙从机 BLE Peripheral HR 例程的基础上增加了 PRF 2.4G 相关功能。

2 环境要求

  • board: pan107x evb

  • uart(option): 用来显示串口log(波特率921600,选项8n1

  • 手机app nrf connect

3 编译和烧录

例程位置:<home>\nimble\samples\solutions\ble_prf_sample\keil_107x

使用keil打开项目进行编译烧录。

4 演示说明

  1. 烧录完成后,设备复位会显示上电log,上电后的log如下:

    Try to load HW calibration data.. DONE.
    - Chip Info         : 0x1
    - Chip CP Version   : 255
    - Chip FT Version   : 6
    - Chip MAC Address  : E11000000FBC
    - Chip UID          : 090001465454455354
    - Chip Flash UID    : 4250315A3538380B006ED94356031578
    - Chip Flash Size   : 512 KB
    [I] app started
    [I] ble stack init start...
    [I] Spark Controller Init Start.
    [I] BLE Heap size:3844 B, remain:64 B
    [I] Spark Controller Init OK.
    [I] ble host init ok -> ble thread start...
    [I] registered service 0x180a with handle=1
    [I]   registering characteristic 0x2a24 with def_handle=2 val_handle=3
    [I]   registering characteristic 0x2a25 with def_handle=4 val_handle=5
    [I]   registering characteristic 0x2a27 with def_handle=6 val_handle=7
    [I]   registering characteristic 0x2a26 with def_handle=8 val_handle=9
    [I]   registering characteristic 0x2a28 with def_handle=10 val_handle=11
    [I]   registering characteristic 0x2a29 with def_handle=12 val_handle=13
    [I]   registering characteristic 0x2a23 with def_handle=14 val_handle=15
    [I] registered service 0x180d with handle=16
    [I]   registering characteristic 0x2a37 with def_handle=17 val_handle=18
    [I]   registering characteristic 0x2a38 with def_handle=20 val_handle=21
    [W] Device Id Address: E1 10 00 00 0F BC
    [I] BLE adv start...
    [I] tx done
    [I] tx done
    [I] tx done
    [I] tx done
    [I] tx done
    [I] tx done
    [I] tx done
    [I] tx done
    [I] tx done
    

    “tx done”是2.4g发送完一包后的打印,例程默认每隔500ms发送一次,偶尔会出现1s打印一次。这个是由于蓝牙和2.4g碰撞到一起的时候蓝牙优先级高,此时蓝牙工作2.4g不工作,2.4g主要在蓝牙空闲的时候工作。

  2. 使用手机 nrf connect 扫描蓝牙设备名称ble_hr并且连接

    image

    nrf connect连接ble_hr

    蓝牙广播和连接的同时也能发送2.4g包。

5 开发使用说明

5.1ble和prf初始化

  1. 双模例程必须使能双模的宏”PRF_BLE_DUAL_MODE”,目前宏定义在keil工程的Options->C/C++下选项Preprocessor Symbols中。

  2. 2.4g和BLE初始化代码如下:

	pan_ant_init();

	extern uint32_t BB_UsToTick(uint32_t us);

	uint32_t tick = (*(volatile uint32_t *)0x50020014);
	prf_tx.interval      = BB_UsToTick(500000);			//prf interval 500ms
	prf_tx.slot_duration = 6;							//prf work duration 6 * 1.25ms
	prf_tx.before_point  = 0;
	prf_tx.anchor_point  = tick;
	prf_tx.AntStartCback = prf_event_handler;			//prf event cb
	prf_tx.AntStopCback  = NULL;
	prf_tx.priority      = 0;							//priority lowest 0,1,2; 2 highest
	pan_ant_create(&prf_tx);

	panchip_prf_init(&tx_config);
	panchip_prf_set_chn(tx_config.rf_channel);

	/*adr match bit */
	PRI_RF_SetAddrMatchBit(PRI_RF, 0);
	panchip_prf_set_data(&tx_payload);
	panchip_prf_ble_resume();

	/* start advertising */
	app_ble_advertise_start();

2.4g和蓝牙广播初始化无先后顺序,例程默认是先初始化2.4g,然后初始化广播。

  1. 需要定义一个定时事件的结构体”ab_event_node_t”,包含定时间隔和定时回调等参数。

  2. 首先调用”pan_ant_init()”接口,初始化定时事件调度的内存空间和回调函数。然后注册结构体”pan_ant_create(&prf_tx)”,将2.4g事件插入到蓝牙事件的间隙中。

  3. 结构体中需要填写几个关键参数,结构体定义如下:

    typedef struct{
    	uint32_t           interval;       /* unit: tick (32us) */
    	uint32_t           slot_duration;  /* event max duration  unit: 1.25ms */
    	uint32_t           before_point;   /* unit: tick (32us) */
    	uint32_t           anchor_point;   /* 32k current count unit: tick (32us) */
    	void               *event;         /* ab_event_come_t    event;*/
    	uint32_t           reserve;
    	uint8_t            priority;       /* priority lowest 0,1,2; 2 highest*/
    	uint8_t            dy_scan;
    
    	AntStartCback_t    AntStartCback;
    	AntStartCback_t    AntStopCback;
    }ab_event_node_t;
    
  4. 注册完2.4g事件后就可以启动2.4g收发了,例程在500ms定时回调中启动2.4g发射,2.4g发完后会有tx中断。同理rx也是如此。

5.2ble和prf切换说明

目前例程ble和2.4g切换在底层接口已经实现好了,对用户来说是无感的。在此简单说明一下:

  1. 2.4g和蓝牙切换依赖于panchip_switch_prf接口,控制”pan_prf_config_t”结构体的成员变量中”prf_chip_mode_sel_t”如下

	typedef enum {
		PRF_CHIP_MODE_SEL_BLE          = 1,
		PRF_CHIP_MODE_SEL_XN297        = 2,
		PRF_CHIP_MODE_SEL_NRF		   = 3,
	}prf_chip_mode_sel_t;

如果工作在蓝牙模式则配成1,如果工作在2.4g模式则配成2或3。

  1. 2.4g和蓝牙共同使用同一个中断”LL_IRQHandler”,中断处理都是独立的。2.4g定时回调中切换工作模式到2.4g模式,2.4g产生中断后,即trx结束后切换工作模式到蓝牙模式。

    2.4g中断服务函数:

    uint8_t panchip_prf_ble_handler(void)
    

    蓝牙中断服务函数:

    PAN_BLE_STACK_API void pan_ble_irq(void);
    

6 RAM/Flash资源使用情况

PAN107x:

Flash Size: 130.16k
RAM Size:  39.11 k