51#include "dev/watchdog.h"
54#include <ti/devices/DeviceFamily.h>
55#include DeviceFamily_constructPath(driverlib/rf_mailbox.h)
56#include DeviceFamily_constructPath(driverlib/rf_common_cmd.h)
57#include DeviceFamily_constructPath(driverlib/rf_data_entry.h)
58#include DeviceFamily_constructPath(driverlib/rf_prop_cmd.h)
59#include DeviceFamily_constructPath(driverlib/rf_prop_mailbox.h)
61#include <ti/drivers/rf/RF.h>
62#include DeviceFamily_constructPath(inc/hw_rfc_dbell.h)
63#include DeviceFamily_constructPath(driverlib/rfc.h)
84#define LOG_MODULE "Radio"
85#define LOG_LEVEL LOG_LEVEL_NONE
88#define CLAMP(v, vmin, vmax) (MAX(MIN(v, vmax), vmin))
91#define PROP_MODE_DYN_WHITENER PROP_MODE_CONF_DW
92#define PROP_MODE_USE_CRC16 PROP_MODE_CONF_USE_CRC16
93#define PROP_MODE_CENTER_FREQ PROP_MODE_CONF_CENTER_FREQ
94#define PROP_MODE_LO_DIVIDER PROP_MODE_CONF_LO_DIVIDER
95#define PROP_MODE_CCA_RSSI_THRESHOLD PROP_MODE_CONF_CCA_RSSI_THRESHOLD
101 CCA_STATE_INVALID = 2
104#if MAC_CONF_WITH_TSCH
105static volatile uint8_t is_receiving_packet;
109#define DOT_4G_PHR_NUM_BYTES 2
110#define DOT_4G_LEN_OFFSET 0xFC
111#define DOT_4G_SYNCWORD 0x0055904E
114#define DOT_4G_PHR_CRC16 0x10
115#define DOT_4G_PHR_DW 0x08
117#if PROP_MODE_USE_CRC16
119#define DOT_4G_PHR_CRC_BIT DOT_4G_PHR_CRC16
123#define DOT_4G_PHR_CRC_BIT 0
127#if PROP_MODE_DYN_WHITENER
128#define DOT_4G_PHR_DW_BIT DOT_4G_PHR_DW
130#define DOT_4G_PHR_DW_BIT 0
149#define MAX_PAYLOAD_LEN 125
152#define TIMEOUT_ENTER_RX_WAIT (RTIMER_SECOND >> 10)
158#define RAT_TIMESTAMP_OFFSET USEC_TO_RAT(RADIO_PHY_HEADER_LEN * RADIO_BYTE_AIR_TIME - 270)
161#define TX_BUF_HDR_LEN 2
162#define TX_BUF_PAYLOAD_LEN 180
164#define TX_BUF_SIZE (TX_BUF_HDR_LEN + TX_BUF_PAYLOAD_LEN)
167typedef uint16_t lensz_t;
169#define FRAME_OFFSET sizeof(lensz_t)
173#define RX_SENSITIVITY_DBM -110
174#define RX_SATURATION_DBM 10
175#define ED_MIN_DBM_ABOVE_RX_SENSITIVITY 10
178#define ED_RF_POWER_MIN_DBM (RX_SENSITIVITY_DBM + ED_MIN_DBM_ABOVE_RX_SENSITIVITY)
179#define ED_RF_POWER_MAX_DBM RX_SATURATION_DBM
182typedef rfc_propRxOutput_t rx_output_t;
193 struct ctimer overflow_timer;
194 rtimer_clock_t last_overflow;
195 volatile uint32_t overflow_count;
198 bool (* rx_is_active)(void);
201 uint8_t tx_buf[TX_BUF_SIZE] CC_ALIGN(4);
204 rx_output_t rx_stats;
207 int8_t rssi_threshold;
223static prop_radio_t prop_radio;
227#define cmd_radio_setup rf_cmd_prop_radio_div_setup
228#define cmd_fs rf_cmd_prop_fs
229#define cmd_tx rf_cmd_prop_tx_adv
230#define cmd_rx rf_cmd_prop_rx_adv
233#define v_cmd_radio_setup CC_ACCESS_NOW(rfc_CMD_PROP_RADIO_DIV_SETUP_t, rf_cmd_prop_radio_div_setup)
234#define v_cmd_fs CC_ACCESS_NOW(rfc_CMD_FS_t, rf_cmd_prop_fs)
235#define v_cmd_tx CC_ACCESS_NOW(rfc_CMD_PROP_TX_ADV_t, rf_cmd_prop_tx_adv)
236#define v_cmd_rx CC_ACCESS_NOW(rfc_CMD_PROP_RX_ADV_t, rf_cmd_prop_rx_adv)
241 return v_cmd_tx.status == ACTIVE;
247 return v_cmd_rx.status == ACTIVE;
250static int channel_clear(
void);
253static rf_result_t set_channel_force(uint16_t channel);
258 cmd_radio_setup.config.frontEndMode = RF_SUB_1_GHZ_FRONT_END_MODE;
259 cmd_radio_setup.config.biasMode = RF_SUB_1_GHZ_BIAS_MODE;
260 cmd_radio_setup.centerFreq = PROP_MODE_CENTER_FREQ;
261 cmd_radio_setup.loDivider = PROP_MODE_LO_DIVIDER;
263 cmd_tx.numHdrBits = DOT_4G_PHR_NUM_BYTES * 8;
264 cmd_tx.syncWord = DOT_4G_SYNCWORD;
266 cmd_rx.syncWord0 = DOT_4G_SYNCWORD;
267 cmd_rx.syncWord1 = 0x00000000;
268 cmd_rx.maxPktLen = RADIO_PHY_OVERHEAD + MAX_PAYLOAD_LEN;
269 cmd_rx.hdrConf.numHdrBits = DOT_4G_PHR_NUM_BYTES * 8;
270 cmd_rx.lenOffset = DOT_4G_LEN_OFFSET;
271 cmd_rx.pQueue = data_queue_init(
sizeof(lensz_t));
272 cmd_rx.pOutput = (uint8_t *)&prop_radio.rx_stats;
279 bool stop_rx =
false;
280 int8_t rssi = RF_GET_RSSI_ERROR_VAL;
283 if(!rx_is_active()) {
285 if(v_cmd_rx.status != PENDING) {
286 res = netstack_sched_rx(
false);
287 if(res != RF_RESULT_OK) {
288 LOG_ERR(
"RSSI measurement failed to schedule RX\n");
299 if(!rx_is_active()) {
300 LOG_ERR(
"RSSI measurement failed to turn on RX, RX status=0x%04X\n", v_cmd_rx.status);
301 return RF_RESULT_ERROR;
306 rssi = RF_getRssi(prop_radio.rf_handle);
318 uint32_t freq_khz = v_cmd_fs.frequency * 1000;
326 freq_khz += (((v_cmd_fs.fractFreq * 1000) + 65535) / 65536);
328 return (uint8_t)((freq_khz - DOT_15_4G_CHAN0_FREQ) / DOT_15_4G_FREQ_SPACING);
334 if(!dot_15_4g_chan_in_range(channel)) {
335 LOG_WARN(
"Supplied hannel %d is illegal, defaults to %d\n",
336 (
int)channel, DOT_15_4G_DEFAULT_CHAN);
337 channel = DOT_15_4G_DEFAULT_CHAN;
340 if(channel == prop_radio.channel) {
345 return set_channel_force(channel);
350set_channel_force(uint16_t channel)
354 if(prop_radio.rf_is_on) {
359 const uint32_t new_freq = dot_15_4g_freq(channel);
360 const uint16_t freq = (uint16_t)(new_freq / 1000);
361 const uint16_t frac = (uint16_t)(((new_freq - (freq * 1000)) * 0x10000) / 1000);
363 LOG_DBG(
"Set channel to %d, frequency 0x%04X.0x%04X (%" PRIu32
")\n",
364 (
int)channel, freq, frac, new_freq);
366 v_cmd_fs.frequency = freq;
367 v_cmd_fs.fractFreq = frac;
369 res = netstack_sched_fs();
371 if(res != RF_RESULT_OK) {
375 prop_radio.channel = channel;
380calculate_lqi(int8_t rssi)
387 rssi = CLAMP(rssi, ED_RF_POWER_MIN_DBM, ED_RF_POWER_MAX_DBM);
395 return (ED_MAX * (rssi - ED_RF_POWER_MIN_DBM)) / (ED_RF_POWER_MAX_DBM - ED_RF_POWER_MIN_DBM);
399set_send_on_cca(
bool enable)
401 prop_radio.send_on_cca = enable;
405prepare(
const void *payload,
unsigned short payload_len)
407 if(payload_len > TX_BUF_PAYLOAD_LEN || payload_len > MAX_PAYLOAD_LEN) {
411 memcpy(prop_radio.tx_buf + TX_BUF_HDR_LEN, payload, payload_len);
416transmit(
unsigned short transmit_len)
420 if(transmit_len > MAX_PAYLOAD_LEN) {
421 LOG_ERR(
"Too long\n");
426 LOG_ERR(
"A transmission is already active\n");
430 if(prop_radio.send_on_cca && !channel_clear()) {
431 LOG_WARN(
"Channel is not clear for transmission\n");
436 const uint16_t total_length = transmit_len + CRC_LEN;
445 prop_radio.tx_buf[0] = ((total_length >> 0) & 0xFF);
446 prop_radio.tx_buf[1] = ((total_length >> 8) & 0xFF) + DOT_4G_PHR_DW_BIT + DOT_4G_PHR_CRC_BIT;
450 v_cmd_tx.pktLen = transmit_len + DOT_4G_PHR_NUM_BYTES;
451 v_cmd_tx.pPkt = prop_radio.tx_buf;
453 res = netstack_sched_prop_tx(transmit_len);
455 if(res != RF_RESULT_OK) {
456 LOG_WARN(
"Channel is not clear for transmission\n");
460 return (res == RF_RESULT_OK)
466send(
const void *payload,
unsigned short payload_len)
468 prepare(payload, payload_len);
469 return transmit(payload_len);
473read(
void *buf,
unsigned short buf_len)
475 volatile data_entry_t *data_entry = data_queue_current_entry();
479 while((data_entry->status == DATA_ENTRY_BUSY) &&
480 RTIMER_CLOCK_LT(
RTIMER_NOW(), t0 + RADIO_FRAME_DURATION(MAX_PAYLOAD_LEN)));
482#if MAC_CONF_WITH_TSCH
484 is_receiving_packet = 0;
487 if(data_entry->status != DATA_ENTRY_FINISHED) {
512 uint8_t *
const frame_ptr = (uint8_t *)&data_entry->data;
513 const lensz_t frame_len = *(lensz_t *)frame_ptr;
517 LOG_ERR(
"Received frame is too short, len=%d\n", frame_len);
519 data_queue_release_entry();
523 const uint8_t *payload_ptr = frame_ptr +
sizeof(lensz_t);
524 const unsigned short payload_len = (
unsigned short)(frame_len -
FRAME_SHAVE);
527 if(payload_len > buf_len) {
528 LOG_ERR(
"Payload of received frame is too large for local buffer, len=%d buf_len=%d\n",
529 payload_len, buf_len);
531 data_queue_release_entry();
535 memcpy(buf, payload_ptr, payload_len);
538 prop_radio.last.rssi = (int8_t)payload_ptr[payload_len];
540 prop_radio.last.corr_lqi = calculate_lqi(prop_radio.last.rssi);
543 memcpy(&rat_ticks, payload_ptr + payload_len + 1, 4);
546 prop_radio.last.timestamp = rat_to_timestamp(rat_ticks, RAT_TIMESTAMP_OFFSET);
548 if(!prop_radio.poll_mode) {
552 packetbuf_set_attr(PACKETBUF_ATTR_RSSI, (packetbuf_attr_t)prop_radio.last.rssi);
553 packetbuf_set_attr(PACKETBUF_ATTR_LINK_QUALITY, (packetbuf_attr_t)prop_radio.last.corr_lqi);
556 data_queue_release_entry();
557 return (
int)payload_len;
563 const int8_t rssi = get_rssi();
565 if(rssi == RF_GET_RSSI_ERROR_VAL) {
566 return CCA_STATE_INVALID;
569 return (rssi < prop_radio.rssi_threshold)
578 LOG_ERR(
"Channel clear called while in TX\n");
582 const uint8_t cca_state = cca_request();
585 return cca_state == CCA_STATE_IDLE;
589receiving_packet(
void)
591 if(!prop_radio.rf_is_on) {
595#if MAC_CONF_WITH_TSCH
603 if(!is_receiving_packet) {
607 if(HWREG(RFC_DBELL_BASE + RFC_DBELL_O_RFHWIFG) & RFC_DBELL_RFHWIFG_MDMSOFT) {
608 is_receiving_packet = 1;
612 is_receiving_packet = (cca_request() == CCA_STATE_BUSY);
613 if(!is_receiving_packet) {
615 RFCHwIntClear(RFC_DBELL_RFHWIFG_MDMSOFT);
619 return is_receiving_packet;
641 if(cca_request() == CCA_STATE_BUSY) {
653 const data_entry_t *
const read_entry = data_queue_current_entry();
654 volatile const data_entry_t *curr_entry = read_entry;
660 const uint8_t status = curr_entry->status;
661 if((status == DATA_ENTRY_FINISHED) ||
662 (status == DATA_ENTRY_BUSY)) {
667 curr_entry = (data_entry_t *)curr_entry->pNextEntry;
668 }
while(curr_entry != read_entry);
670 if(num_pending > 0 && !prop_radio.poll_mode) {
683 if(prop_radio.rf_is_on) {
684 LOG_WARN(
"Radio is already on\n");
690 res = netstack_sched_rx(
true);
692 if(res != RF_RESULT_OK) {
693 return RF_RESULT_ERROR;
696 prop_radio.rf_is_on =
true;
703 if(!prop_radio.rf_is_on) {
704 LOG_WARN(
"Radio is already off\n");
710 prop_radio.rf_is_on =
false;
726 *value = (prop_radio.rf_is_on)
739 if(prop_radio.poll_mode) {
750 res = rf_get_tx_power(prop_radio.rf_handle, rf_tx_power_table, (int8_t *)&value);
751 return ((res == RF_RESULT_OK) &&
752 (*value != RF_TxPowerTable_INVALID_DBM))
757 *value = prop_radio.rssi_threshold;
762 return (*value == RF_GET_RSSI_ERROR_VAL)
767 *value = prop_radio.last.rssi;
771 *value = prop_radio.last.corr_lqi;
775 *value = DOT_15_4G_CHAN_MIN;
779 *value = DOT_15_4G_CHAN_MAX;
787 *value = (
radio_value_t)tx_power_max(rf_tx_power_table, rf_tx_power_table_size);
790 case RADIO_CONST_MAX_PAYLOAD_LEN:
808 return (on() == RF_RESULT_OK)
820 return (res == RF_RESULT_OK)
825 if(!tx_power_in_range((int8_t)value, rf_tx_power_table, rf_tx_power_table_size)) {
828 res = rf_set_tx_power(prop_radio.rf_handle, rf_tx_power_table, (int8_t)value);
829 return (res == RF_RESULT_OK)
839 const bool old_poll_mode = prop_radio.poll_mode;
841 if(old_poll_mode == prop_radio.poll_mode) {
844 if(!prop_radio.rf_is_on) {
849 res = netstack_sched_rx(
false);
850 return (res == RF_RESULT_OK)
863 prop_radio.rssi_threshold = (int8_t)value;
872get_object(radio_param_t param,
void *dest,
size_t size)
881 if(size !=
sizeof(rtimer_clock_t)) {
885 *(rtimer_clock_t *)dest = prop_radio.last.timestamp;
895set_object(radio_param_t param,
const void *src,
size_t size)
904 RF_TxPowerTable_Value tx_power_value;
907 prop_radio.rx_is_active = rx_is_active;
909 radio_mode = (simplelink_radio_mode_t *)&prop_radio;
911 if(prop_radio.rf_handle) {
912 LOG_WARN(
"Radio is already initialized\n");
917 prop_radio.rf_is_on =
false;
920 prop_radio.rssi_threshold = PROP_MODE_CCA_RSSI_THRESHOLD;
925 RF_Params_init(&rf_params);
929 prop_radio.rf_handle = netstack_open(&rf_params);
931 if(prop_radio.rf_handle == NULL) {
932 LOG_ERR(
"Unable to open RF driver during initialization\n");
933 return RF_RESULT_ERROR;
938 tx_power_value = RF_TxPowerTable_findValue(rf_tx_power_table, RF_TXPOWER_DBM);
939 if(tx_power_value.rawValue != RF_TxPowerTable_INVALID_VALUE) {
940 rf_stat = RF_setTxPower(prop_radio.rf_handle, tx_power_value);
941 if(rf_stat == RF_StatSuccess) {
942 LOG_INFO(
"TX power configured to %d dBm\n", RF_TXPOWER_DBM);
944 LOG_WARN(
"Setting TX power to %d dBm failed, stat=0x%02X", RF_TXPOWER_DBM, rf_stat);
947 LOG_WARN(
"Unable to find TX power %d dBm in the TX power table\n", RF_TXPOWER_DBM);
950 ENERGEST_ON(ENERGEST_TYPE_LISTEN);
Default definitions of C compiler quirk work-arounds.
Header file of the CC13xx/CC26xx RF data queue.
Header file for the energy estimation mechanism.
#define RF_CONF_INACTIVITY_TIMEOUT
2 ms
#define FRAME_SHAVE
RSSI (1) + Timestamp (4) + Status (1)
static uint8_t get_channel()
Get the current operating channel.
static void set_channel(uint8_t channel)
Set the current operating channel.
void process_start(struct process *p, process_data_t data)
Start a process.
void process_poll(struct process *p)
Request a process to be polled.
#define RADIO_RX_MODE_POLL_MODE
Enable/disable/get the state of radio driver poll mode operation.
#define RADIO_TX_MODE_SEND_ON_CCA
Radio TX mode control / retrieval.
enum radio_result_e radio_result_t
Radio return values when setting or getting radio parameters.
int radio_value_t
Each radio has a set of parameters that designate the current configuration and state of the radio.
@ RADIO_RESULT_ERROR
An error occurred when getting/setting the parameter, but the arguments were otherwise correct.
@ RADIO_RESULT_NOT_SUPPORTED
The parameter is not supported.
@ RADIO_RESULT_INVALID_VALUE
The value argument was incorrect.
@ RADIO_RESULT_OK
The parameter was set/read successfully.
@ RADIO_PARAM_POWER_MODE
When getting the value of this parameter, the radio driver should indicate whether the radio is on or...
@ RADIO_PARAM_RSSI
Received signal strength indicator in dBm.
@ RADIO_PARAM_LAST_PACKET_TIMESTAMP
Last packet timestamp, of type rtimer_clock_t.
@ RADIO_PARAM_LAST_RSSI
The RSSI value of the last received packet.
@ RADIO_PARAM_RX_MODE
Radio receiver mode determines if the radio has address filter (RADIO_RX_MODE_ADDRESS_FILTER) and aut...
@ RADIO_PARAM_CHANNEL
Channel used for radio communication.
@ RADIO_PARAM_LAST_LINK_QUALITY
Link quality indicator of the last received packet.
@ RADIO_PARAM_TXPOWER
Transmission power in dBm.
@ RADIO_CONST_CHANNEL_MAX
The highest radio channel number.
@ RADIO_PARAM_CCA_THRESHOLD
Clear channel assessment threshold in dBm.
@ RADIO_CONST_TXPOWER_MIN
The minimum transmission power in dBm.
@ RADIO_CONST_CHANNEL_MIN
The lowest radio channel number.
@ RADIO_CONST_TXPOWER_MAX
The maximum transmission power in dBm.
@ RADIO_PARAM_TX_MODE
Radio transmission mode determines if the radio has send on CCA (RADIO_TX_MODE_SEND_ON_CCA) enabled o...
@ RADIO_POWER_MODE_OFF
Radio powered off and in the lowest possible power consumption state.
@ RADIO_POWER_MODE_ON
Radio powered on and able to receive frames.
@ RADIO_TX_COLLISION
TX failed due to a collision.
@ RADIO_TX_ERR
An error occurred during transmission.
@ RADIO_TX_OK
TX was successful and where an ACK was requested one was received.
static uint8_t rf_is_on(void)
Checks whether the RFC domain is accessible and the RFC is in IEEE RX.
#define RTIMER_BUSYWAIT_UNTIL(cond, max_time)
Busy-wait until a condition for at most max_time.
#define RTIMER_NOW()
Get the current clock time.
Header file for the logging system.
#define IEEE802154_DEFAULT_CHANNEL
The default channel for IEEE 802.15.4 networks.
Include file for the Contiki low-layer network stack (NETSTACK)
Header file for the Packet buffer (packetbuf) management.
Header file of the generic radio mode API.
Header file of the CC13xx/CC26xx RAT timer handler.
Header file of common CC13xx/CC26xx RF functionality.
Header file for the real-time timer module.
Header file of the CC13xx/CC26xx RF scheduler.
Header file of RF settings for CC13xx/CC26xx.
Header file with descriptors for the various modes of operation defined in IEEE 802....
The structure of a Contiki-NG radio device driver.
radio_result_t(* get_object)(radio_param_t param, void *dest, size_t size)
Get a radio parameter object.
int(* read)(void *buf, unsigned short buf_len)
Read a received packet into a buffer.
int(* prepare)(const void *payload, unsigned short payload_len)
Prepare the radio with a packet to be sent.
radio_result_t(* set_value)(radio_param_t param, radio_value_t value)
Set a radio parameter value.
int(* off)(void)
Turn the radio off.
int(* init)(void)
Initialise the radio hardware.
int(* send)(const void *payload, unsigned short payload_len)
Prepare & transmit a packet.
int(* receiving_packet)(void)
Check if the radio driver is currently receiving a packet.
radio_result_t(* set_object)(radio_param_t param, const void *src, size_t size)
Set a radio parameter object.
int(* on)(void)
Turn the radio on.
int(* transmit)(unsigned short transmit_len)
Send the packet that has previously been prepared.
int(* pending_packet)(void)
Check if a packet has been received and is available in the radio driver's buffers.
radio_result_t(* get_value)(radio_param_t param, radio_value_t *value)
Get a radio parameter value.
int(* channel_clear)(void)
Perform a Clear-Channel Assessment (CCA) to find out if there is a packet in the air or not.
Header file of TX power functionality of CC13xx/CC26xx.