BLE5 support for CC2640R2

This commit is contained in:
Jinyan BAI 2018-09-11 12:45:29 +01:00
parent 3ece31be30
commit 9406f5b5d9
3 changed files with 389 additions and 7 deletions

View File

@ -1,5 +1,6 @@
/*
* Copyright (c) 2017, Graz University of Technology
* Copyright (c) 2018, University of Bristol - http://www.bristol.ac.uk/
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
@ -34,6 +35,7 @@
*
* \author
* Michael Spoerk <michael.spoerk@tugraz.at>
* Jinyan BAI <onefreebjy@outlook.com>
*/
/*---------------------------------------------------------------------------*/
@ -63,6 +65,9 @@
#include <string.h>
#include "rf-core/ble-hal/rf-ble-cmd.h"
#if RADIO_CONF_BLE5
#include "rf_patches/rf_patch_cpe_bt5.h"
#endif
/*---------------------------------------------------------------------------*/
#include "sys/log.h"
#define LOG_MODULE "BLE-RADIO"
@ -117,9 +122,15 @@ ticks_to_unit(rtimer_clock_t value, uint32_t unit)
return (uint32_t)temp;
}
/*---------------------------------------------------------------------------*/
#if RADIO_CONF_BLE5
#define CMD_BUFFER_SIZE 28
#define PARAM_BUFFER_SIZE 48
#define OUTPUT_BUFFER_SIZE 24
#else
#define CMD_BUFFER_SIZE 24
#define PARAM_BUFFER_SIZE 36
#define OUTPUT_BUFFER_SIZE 24
#endif
/*---------------------------------------------------------------------------*/
/* ADVERTISING data structures */
#define ADV_RX_BUFFERS_OVERHEAD 8
@ -358,11 +369,23 @@ on(void)
oscillators_request_hf_xosc();
if(!rf_core_is_accessible()) {
/* boot the rf core */
if(rf_core_boot() != RF_CORE_CMD_OK) {
LOG_ERR("ble_controller_reset() could not boot rf-core\n");
return BLE_RESULT_ERROR;
}
/* boot and apply Bluetooth 5 Patch */
if(rf_core_power_up() != RF_CORE_CMD_OK) {
LOG_ERR("rf_core_boot: rf_core_power_up() failed\n");
rf_core_power_down();
return RF_CORE_CMD_ERROR;
}
#if RADIO_CONF_BLE5
/* Apply Bluetooth 5 patch, if applicable */
rf_patch_cpe_bt5();
#endif
if(rf_core_start_rat() != RF_CORE_CMD_OK) {
LOG_ERR("rf_core_boot: rf_core_start_rat() failed\n");
rf_core_power_down();
return RF_CORE_CMD_ERROR;
}
rf_core_setup_interrupts(0);
oscillators_switch_to_hf_xosc();
@ -845,7 +868,11 @@ connection_rx(ble_conn_param_t *param)
while(RX_ENTRY_STATUS(param->rx_queue_current) == DATA_ENTRY_FINISHED) {
rx_data = RX_ENTRY_DATA_PTR(param->rx_queue_current);
#if RADIO_CONF_BLE5
len = RX_ENTRY_DATA_LENGTH(param->rx_queue_current) - 7 - 2; /* last 9 bytes are status, timestamp, ... */
#else
len = RX_ENTRY_DATA_LENGTH(param->rx_queue_current) - 6 - 2; /* last 8 bytes are status, timestamp, ... */
#endif
channel = (rx_data[len + 3] & 0x3F);
frame_type = rx_data[0] & 0x03;
more_data = (rx_data[0] & 0x10) >> 4;

View File

@ -1,5 +1,6 @@
/*
* Copyright (c) 2017, Graz University of Technology
* Copyright (c) 2018, University of Bristol - http://www.bristol.ac.uk/
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
@ -36,6 +37,7 @@
*
* \author
* Michael Spoerk <michael.spoerk@tugraz.at>
* Jinyan BAI <onefreebjy@outlook.com>
*/
/*---------------------------------------------------------------------------*/
#include "contiki.h"
@ -57,6 +59,62 @@ static uint16_t tx_power = 0x3161; /* 0 dBm */
/*static uint16_t tx_power = 0x0CCB; / * -15 dBm * / */
/*---------------------------------------------------------------------------*/
/* BLE overrides */
#if RADIO_CONF_BLE5
uint32_t ble_overrides_common[] =
{
/* Rx: Set LNA IB trim value based on the selected defaultPhy.mainMode setting. (NOTE: The value 0x8 is a placeholder. The value to use should be set during run-time by radio driver function.) */
ADI_HALFREG_OVERRIDE(0,4,0xF,0x8),
/* Rx: Set LNA IB offset used for automatic software compensation to 0 */
(uint32_t)0x00008883,
/* Synth: Use 24 MHz crystal, enable extra PLL filtering */
(uint32_t)0x02010403,
/* Synth: Set fine top and bottom code to 127 and 0 */
HW_REG_OVERRIDE(0x4020, 0x7F00),
/* Synth: Configure faster calibration */
HW32_ARRAY_OVERRIDE(0x4004, 1),
/* Synth: Configure faster calibration */
(uint32_t)0x1C0C0618,
/* Synth: Configure faster calibration */
(uint32_t)0xC00401A1,
/* Synth: Configure faster calibration */
(uint32_t)0x21010101,
/* Synth: Configure faster calibration */
(uint32_t)0xC0040141,
/* Synth: Configure faster calibration */
(uint32_t)0x00214AD3,
/* Synth: Decrease synth programming time-out by 90 us (0x0298 RAT ticks = 166 us) */
(uint32_t)0x02980243,
/* Bluetooth 5: Set correct total clock accuracy for received AuxPtr assuming local sleep clock of 50 ppm */
(uint32_t)0x0E490823,
/* override_frontend_id.xml */
(uint32_t)0xFFFFFFFF,
};
uint32_t ble_overrides_1Mbps[] =
{
/* Rx: Set LNA IB trim to normal trim value. (NOTE: The value 0x8 is a placeholder. The value to use should be set during run-time by radio driver function.) */
ADI_HALFREG_OVERRIDE(0,4,0xF,0x8),
/* Rx: Configure AGC to use gain table for improved performance */
HW_REG_OVERRIDE(0x6084, 0x05F8),
(uint32_t)0xFFFFFFFF,
};
uint32_t ble_overrides_2Mbps[] =
{
/* Rx: Set LNA IB trim to normal trim value. (NOTE: The value 0x8 is a placeholder. The value to use should be set during run-time by radio driver function.) */
ADI_HALFREG_OVERRIDE(0,4,0xF,0x8),
(uint32_t)0xFFFFFFFF,
};
uint32_t ble_overrides_coded[] =
{
/* Rx: Set LNA IB trim to 0xF (maximum) */
ADI_HALFREG_OVERRIDE(0,4,0xF,0xF),
/* Rx: Override AGC target gain to improve performance */
HW_REG_OVERRIDE(0x6088, 0x0018),
(uint32_t)0xFFFFFFFF,
};
#else
static uint32_t ble_overrides[] = {
0x00364038, /* Synth: Set RTRIM (POTAILRESTRIM) to 6 */
0x000784A3, /* Synth: Set FREF = 3.43 MHz (24 MHz / 7) */
@ -67,6 +125,7 @@ static uint32_t ble_overrides[] = {
0x008F88B3, /* GPIO mode: https://e2e.ti.com/support/wireless_connectivity/proprietary_sub_1_ghz_simpliciti/f/156/t/488244?*/
0xFFFFFFFF, /* End of override list */
};
#endif
/*---------------------------------------------------------------------------*/
unsigned short
rf_ble_cmd_send(uint8_t *command)
@ -97,14 +156,29 @@ rf_ble_cmd_wait(uint8_t *command)
unsigned short
rf_ble_cmd_setup_ble_mode(void)
{
#if RADIO_CONF_BLE5
rfc_CMD_BLE5_RADIO_SETUP_t cmd;
/* Create radio setup command */
rf_core_init_radio_op((rfc_radioOp_t *)&cmd, sizeof(cmd), CMD_BLE5_RADIO_SETUP);
cmd.startTrigger.bEnaCmd = 0;
cmd.defaultPhy.mainMode = 1;
cmd.defaultPhy.coding = 1;
cmd.pRegOverrideCommon = ble_overrides_common;
cmd.pRegOverride1Mbps = ble_overrides_1Mbps;
cmd.pRegOverride2Mbps = ble_overrides_2Mbps;
cmd.pRegOverrideCoded = ble_overrides_coded;
#else
rfc_CMD_RADIO_SETUP_t cmd;
/* Create radio setup command */
rf_core_init_radio_op((rfc_radioOp_t *)&cmd, sizeof(cmd), CMD_RADIO_SETUP);
cmd.txPower = tx_power;
cmd.pRegOverride = ble_overrides;
cmd.mode = 0;
cmd.pRegOverride = ble_overrides;
#endif
cmd.txPower = tx_power;
/* Send Radio setup to RF Core */
if(rf_ble_cmd_send((uint8_t *)&cmd) != RF_BLE_CMD_OK) {
@ -116,14 +190,26 @@ rf_ble_cmd_setup_ble_mode(void)
}
/*---------------------------------------------------------------------------*/
/* ADVERTISING functions */
/*---------------------------------------------------------------------------*/
void
rf_ble_cmd_create_adv_cmd(uint8_t *command, uint8_t channel,
uint8_t *param, uint8_t *output)
{
#if RADIO_CONF_BLE5
rfc_CMD_BLE5_ADV_t *c = (rfc_CMD_BLE5_ADV_t *)command;
memset(c, 0x00, sizeof(rfc_CMD_BLE5_ADV_t));
c->commandNo = CMD_BLE5_ADV;
c->rangeDelay = 0;
c->txPower = tx_power;
#else
rfc_CMD_BLE_ADV_t *c = (rfc_CMD_BLE_ADV_t *)command;
memset(c, 0x00, sizeof(rfc_CMD_BLE_ADV_t));
c->commandNo = CMD_BLE_ADV;
#endif
c->condition.rule = COND_NEVER;
c->whitening.bOverride = 0;
c->channel = channel;
@ -162,12 +248,113 @@ rf_ble_cmd_create_adv_params(uint8_t *param, dataQueue_t *rx_queue,
p->endTrigger.triggerType = TRIG_NEVER;
}
/*---------------------------------------------------------------------------*/
/* INITIATOR functions */
/*---------------------------------------------------------------------------*/
void
rf_ble_cmd_create_initiator_cmd(uint8_t *cmd, uint8_t channel, uint8_t *params,
uint8_t *output, uint32_t start_time)
{
#if RADIO_CONF_BLE5
rfc_CMD_BLE5_INITIATOR_t *c = (rfc_CMD_BLE5_INITIATOR_t *)cmd;
memset(c, 0x00, sizeof(rfc_CMD_BLE5_INITIATOR_t));
c->commandNo = CMD_BLE5_INITIATOR;
c->condition.rule = COND_NEVER;
c->whitening.bOverride = 0;
c->channel = channel;
c->pParams = (rfc_ble5InitiatorPar_t *)params;
c->startTrigger.triggerType = TRIG_ABSTIME;
c->startTime = start_time;
c->pOutput = (rfc_ble5ScanInitOutput_t *)output;
c->txPower = tx_power;
c->rangeDelay = 0;
#else
rfc_CMD_BLE_INITIATOR_t *c = (rfc_CMD_BLE_INITIATOR_t *)cmd;
memset(c, 0x00, sizeof(rfc_CMD_BLE_INITIATOR_t));
c->commandNo = CMD_BLE_INITIATOR;
c->condition.rule = COND_NEVER;
c->whitening.bOverride = 0;
c->channel = channel;
c->pParams = (rfc_bleInitiatorPar_t *)params;
c->startTrigger.triggerType = TRIG_ABSTIME;
c->startTime = start_time;
c->pOutput = (rfc_bleInitiatorOutput_t *)output;
#endif
}
/*---------------------------------------------------------------------------*/
void
rf_ble_cmd_create_initiator_params(uint8_t *param, dataQueue_t *rx_queue,
uint32_t initiator_time,
ble_addr_type_t own_addr_type, uint8_t *own_addr,
ble_addr_type_t peer_addr_type, uint8_t *peer_addr,
uint32_t connect_time, uint8_t *conn_req_data)
{
#if RADIO_CONF_BLE5
rfc_ble5InitiatorPar_t *p = (rfc_ble5InitiatorPar_t *)param;
p->backoffPar.bLastSucceeded = 0;
p->backoffPar.bLastFailed = 0;
p->maxWaitTimeForAuxCh = 0;
p->rxStartTime = 0;
p->rxListenTime = 0;
#else
rfc_bleInitiatorPar_t *p = (rfc_bleInitiatorPar_t *)param;
#endif
p->pRxQ = rx_queue;
p->rxConfig.bAutoFlushIgnored = 1;
p->rxConfig.bAutoFlushCrcErr = 0;
p->rxConfig.bAutoFlushEmpty = 1;
p->rxConfig.bIncludeLenByte = 1;
p->rxConfig.bIncludeCrc = 0;
p->rxConfig.bAppendRssi = 1;
p->rxConfig.bAppendStatus = 1;
p->rxConfig.bAppendTimestamp = 1;
/* p->initConfig.bUseWhiteList = 0; */
p->initConfig.bUseWhiteList = 1;
p->initConfig.bDynamicWinOffset = 0;
p->initConfig.deviceAddrType = own_addr_type;
p->initConfig.peerAddrType = peer_addr_type;
p->initConfig.bStrictLenFilter = 1;
p->connectReqLen = 22;
p->pConnectReqData = conn_req_data;
p->pDeviceAddress = (uint16_t *)own_addr;
p->pWhiteList = (rfc_bleWhiteListEntry_t *)peer_addr;
p->connectTime = connect_time;
p->timeoutTrigger.triggerType = TRIG_REL_START;
p->timeoutTime = initiator_time;
p->endTrigger.triggerType = TRIG_NEVER;
}
/*---------------------------------------------------------------------------*/
/* CONNECTION slave functions */
/*---------------------------------------------------------------------------*/
void
rf_ble_cmd_create_slave_cmd(uint8_t *cmd, uint8_t channel, uint8_t *params,
uint8_t *output, uint32_t start_time)
{
#if RADIO_CONF_BLE5
rfc_CMD_BLE5_SLAVE_t *c = (rfc_CMD_BLE5_SLAVE_t *)cmd;
memset(c, 0x00, sizeof(rfc_CMD_BLE5_SLAVE_t));
c->commandNo = CMD_BLE5_SLAVE;
c->condition.rule = COND_NEVER;
c->whitening.bOverride = 0;
c->channel = channel;
c->pParams = (rfc_ble5SlavePar_t *)params;
c->startTrigger.triggerType = TRIG_ABSTIME;
c->startTime = start_time;
c->pOutput = (rfc_bleMasterSlaveOutput_t *)output;
c->phyMode.mainMode = 1;
c->phyMode.coding = 1;
c->txPower = tx_power;
c->rangeDelay = 0;
#else
rfc_CMD_BLE_SLAVE_t *c = (rfc_CMD_BLE_SLAVE_t *)cmd;
memset(c, 0x00, sizeof(rfc_CMD_BLE_SLAVE_t));
@ -181,6 +368,7 @@ rf_ble_cmd_create_slave_cmd(uint8_t *cmd, uint8_t channel, uint8_t *params,
c->startTrigger.pastTrig = 0;
c->startTime = start_time;
c->pOutput = (rfc_bleMasterSlaveOutput_t *)output;
#endif
}
/*---------------------------------------------------------------------------*/
void
@ -190,8 +378,13 @@ rf_ble_cmd_create_slave_params(uint8_t *params, dataQueue_t *rx_queue,
uint8_t crc_init_2, uint32_t win_size,
uint32_t window_widening, uint8_t first_packet)
{
#if RADIO_CONF_BLE5
rfc_ble5SlavePar_t *p = (rfc_ble5SlavePar_t *)params;
p->maxRxPktLen = 255;
p->maxLenLowRate = 0;
#else
rfc_bleSlavePar_t *p = (rfc_bleSlavePar_t *)params;
#endif
p->pRxQ = rx_queue;
p->pTxQ = tx_queue;
p->rxConfig.bAutoFlushIgnored = 1;
@ -230,6 +423,91 @@ rf_ble_cmd_create_slave_params(uint8_t *params, dataQueue_t *rx_queue,
p->endTrigger.triggerType = TRIG_NEVER;
}
/*---------------------------------------------------------------------------*/
/* CONNECTION master functions */
/*---------------------------------------------------------------------------*/
void
rf_ble_cmd_create_master_cmd(uint8_t *cmd, uint8_t channel, uint8_t *params,
uint8_t *output, uint32_t start_time)
{
#if RADIO_CONF_BLE5
rfc_CMD_BLE5_MASTER_t *c = (rfc_CMD_BLE5_MASTER_t *)cmd;
memset(c, 0x00, sizeof(rfc_CMD_BLE5_MASTER_t));
c->commandNo = CMD_BLE5_MASTER;
c->condition.rule = COND_NEVER;
c->whitening.bOverride = 0;
c->channel = channel;
c->pParams = (rfc_ble5MasterPar_t *)params;
c->startTrigger.triggerType = TRIG_ABSTIME;
c->startTime = start_time;
c->pOutput = (rfc_bleMasterSlaveOutput_t *)output;
c->phyMode.mainMode = 1;
c->phyMode.coding = 1;
c->txPower = tx_power;
c->rangeDelay = 0;
#else
rfc_CMD_BLE_MASTER_t *c = (rfc_CMD_BLE_MASTER_t *)cmd;
memset(c, 0x00, sizeof(rfc_CMD_BLE_MASTER_t));
c->commandNo = CMD_BLE_MASTER;
c->condition.rule = COND_NEVER;
c->whitening.bOverride = 0;
c->channel = channel;
c->pParams = (rfc_bleMasterPar_t *)params;
c->startTrigger.triggerType = TRIG_ABSTIME;
c->startTime = start_time;
c->pOutput = (rfc_bleMasterSlaveOutput_t *)output;
#endif
}
/*---------------------------------------------------------------------------*/
void
rf_ble_cmd_create_master_params(uint8_t *params, dataQueue_t *rx_queue,
dataQueue_t *tx_queue, uint32_t access_address,
uint8_t crc_init_0, uint8_t crc_init_1,
uint8_t crc_init_2, uint8_t first_packet)
{
#if RADIO_CONF_BLE5
rfc_ble5MasterPar_t *p = (rfc_ble5MasterPar_t *)params;
p->maxRxPktLen = 255;
p->maxLenLowRate = 0;
#else
rfc_bleMasterPar_t *p = (rfc_bleMasterPar_t *)params;
#endif
p->pRxQ = rx_queue;
p->pTxQ = tx_queue;
p->rxConfig.bAutoFlushIgnored = 1;
p->rxConfig.bAutoFlushCrcErr = 1;
p->rxConfig.bAutoFlushEmpty = 1;
p->rxConfig.bIncludeLenByte = 1;
p->rxConfig.bIncludeCrc = 0;
p->rxConfig.bAppendRssi = 1;
p->rxConfig.bAppendStatus = 1;
p->rxConfig.bAppendTimestamp = 1;
if(first_packet) {
/* set parameters for first packet according to TI Technical Reference Manual */
p->seqStat.lastRxSn = 1;
p->seqStat.lastTxSn = 1;
p->seqStat.nextTxSn = 0;
p->seqStat.bFirstPkt = 1;
p->seqStat.bAutoEmpty = 0;
p->seqStat.bLlCtrlTx = 0;
p->seqStat.bLlCtrlAckRx = 0;
p->seqStat.bLlCtrlAckPending = 0;
}
p->maxPkt = 12;
p->accessAddress = access_address;
p->crcInit0 = crc_init_0;
p->crcInit1 = crc_init_1;
p->crcInit2 = crc_init_2;
p->endTrigger.triggerType = TRIG_REL_START;
p->endTime = (uint32_t)15 * 4000; /* a connection event must end after 10 ms */
}
/*---------------------------------------------------------------------------*/
/* DATA queue functions */
/*---------------------------------------------------------------------------*/
unsigned short

View File

@ -106,6 +106,45 @@ void rf_ble_cmd_create_adv_params(uint8_t *param, dataQueue_t *rx_queue,
uint8_t scan_resp_data_len, uint8_t *scan_resp_data,
ble_addr_type_t own_addr_type, uint8_t *own_addr);
/*---------------------------------------------------------------------------*/
/**
* \brief Creates a BLE radio command structure that sets up
* BLE initiation event when sent to the radio core
* \param cmd A pointer to command that is created
* \param channel The BLE data channel used for the connection event
* \param param A pointer to the radio command parameters
* \param output A pointer to the radio command output
* \param start_time
* The time in rf_core_ticks when the connection event will start
*/
void rf_ble_cmd_create_initiator_cmd(uint8_t *cmd, uint8_t channel, uint8_t *params,
uint8_t *output, uint32_t start_time);
/*---------------------------------------------------------------------------*/
/**
* \brief Creates BLE radio command parameters that are used to set up
* BLE initiation event on the radio core
* \param param A pointer to parameter structure that is created
* \param rx_queue A pointer to the RX queue that is used
* \param initiator_window
* T
* \param own_addr_type
* Either BLE_ADDR_TYPE_PUBLIC or BLE_ADDR_TYPE_RANDOM
* \param own_addr A pointer to the device address that is used as own address
* \param peer_addr_type
* Either BLE_ADDR_TYPE_PUBLIC or BLE_ADDR_TYPE_RANDOM
* \param peer_addr A pointer to the device address that is used as peer address
* \param connect_time
The first possible start time of the first connection event
* \param conn_req_data A pointer to the connect request data
*/
void rf_ble_cmd_create_initiator_params(uint8_t *param, dataQueue_t *rx_queue,
uint32_t initiator_window,
ble_addr_type_t own_addr_type, uint8_t *own_addr,
ble_addr_type_t peer_addr_type, uint8_t *peer_addr,
uint32_t connect_time,
uint8_t *conn_req_data);
/*---------------------------------------------------------------------------*/
/**
* \brief Creates a BLE radio command structure that sets up a single
@ -148,6 +187,44 @@ void rf_ble_cmd_create_slave_params(uint8_t *param, dataQueue_t *rx_queue,
uint8_t crc_init_2, uint32_t win_size,
uint32_t window_widening, uint8_t first_packet);
/*---------------------------------------------------------------------------*/
/**
* \brief Creates a BLE radio command structure that sets up
* BLE connection event when sent to the radio core
* \param cmd A pointer to command that is created
* \param channel The BLE data channel used for the connection event
* \param param A pointer to the radio command parameters
* \param output A pointer to the radio command output
* \param start_time
* The time in rf_core_ticks when the connection event will start
*/
void rf_ble_cmd_create_master_cmd(uint8_t *cmd, uint8_t channel, uint8_t *params,
uint8_t *output, uint32_t start_time);
/*---------------------------------------------------------------------------*/
/**
* \brief Creates BLE radio command parameters that are used to set up
* BLE connection event on the radio core
* \param param A pointer to parameter structure that is created
* \param rx_queue A pointer to the RX queue that is used
* \param tx_queue A pointer to the TX queue that is used
* \param access_address
* The access address of the used BLE connection
* \param crc_init_0
* Part of the initialization of the CRC checksum
* \param crc_init_1
* Part of the initialization of the CRC checksum
* \param crc_init_2
* Part of the initialization of the CRC checksum
* \param first_packet
* 1 for the first packet of the BLE connection so that the
* connection is properly initialized
*/
void rf_ble_cmd_create_master_params(uint8_t *params, dataQueue_t *rx_queue,
dataQueue_t *tx_queue, uint32_t access_address,
uint8_t crc_init_0, uint8_t crc_init_1,
uint8_t crc_init_2, uint8_t first_packet);
/*---------------------------------------------------------------------------*/
/**
* \brief Adds a data buffer to a BLE transmission queue