48 #include "sys/clock.h" 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) 83 #define LOG_MODULE "Radio" 84 #define LOG_LEVEL LOG_LEVEL_NONE 87 #define CLAMP(v, vmin, vmax) (MAX(MIN(v, vmax), vmin)) 90 #define PROP_MODE_DYN_WHITENER PROP_MODE_CONF_DW 91 #define PROP_MODE_USE_CRC16 PROP_MODE_CONF_USE_CRC16 92 #define PROP_MODE_CENTER_FREQ PROP_MODE_CONF_CENTER_FREQ 93 #define PROP_MODE_LO_DIVIDER PROP_MODE_CONF_LO_DIVIDER 94 #define PROP_MODE_CCA_RSSI_THRESHOLD PROP_MODE_CONF_CCA_RSSI_THRESHOLD 100 CCA_STATE_INVALID = 2
103 #if MAC_CONF_WITH_TSCH 104 static volatile uint8_t is_receiving_packet;
108 #define DOT_4G_MAX_FRAME_LEN 2047 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) 155 #define TIMEOUT_DATA_ENTRY_BUSY (RTIMER_SECOND / 250) 160 #define RAT_TIMESTAMP_OFFSET USEC_TO_RAT(RADIO_PHY_HEADER_LEN * RADIO_BYTE_AIR_TIME - 270) 163 #define TX_BUF_HDR_LEN 2 164 #define TX_BUF_PAYLOAD_LEN 180 166 #define TX_BUF_SIZE (TX_BUF_HDR_LEN + TX_BUF_PAYLOAD_LEN) 169 typedef uint16_t lensz_t;
171 #define FRAME_OFFSET sizeof(lensz_t) 172 #define FRAME_SHAVE 6 175 #define RX_SENSITIVITY_DBM -110 176 #define RX_SATURATION_DBM 10 177 #define ED_MIN_DBM_ABOVE_RX_SENSITIVITY 10 180 #define ED_RF_POWER_MIN_DBM (RX_SENSITIVITY_DBM + ED_MIN_DBM_ABOVE_RX_SENSITIVITY) 181 #define ED_RF_POWER_MAX_DBM RX_SATURATION_DBM 184 typedef rfc_propRxOutput_t rx_output_t;
195 struct ctimer overflow_timer;
196 rtimer_clock_t last_overflow;
197 volatile uint32_t overflow_count;
200 bool (* rx_is_active)(void);
203 uint8_t tx_buf[TX_BUF_SIZE] CC_ALIGN(4);
206 rx_output_t rx_stats;
209 int8_t rssi_threshold;
225 static prop_radio_t prop_radio;
229 #define cmd_radio_setup rf_cmd_prop_radio_div_setup 230 #define cmd_fs rf_cmd_prop_fs 231 #define cmd_tx rf_cmd_prop_tx_adv 232 #define cmd_rx rf_cmd_prop_rx_adv 235 #define v_cmd_radio_setup CC_ACCESS_NOW(rfc_CMD_PROP_RADIO_DIV_SETUP_t, rf_cmd_prop_radio_div_setup) 236 #define v_cmd_fs CC_ACCESS_NOW(rfc_CMD_FS_t, rf_cmd_prop_fs) 237 #define v_cmd_tx CC_ACCESS_NOW(rfc_CMD_PROP_TX_ADV_t, rf_cmd_prop_tx_adv) 238 #define v_cmd_rx CC_ACCESS_NOW(rfc_CMD_PROP_RX_ADV_t, rf_cmd_prop_rx_adv) 243 return v_cmd_tx.status == ACTIVE;
249 return v_cmd_rx.status == ACTIVE;
254 static int off(
void);
255 static rf_result_t set_channel_force(uint16_t channel);
260 cmd_radio_setup.config.frontEndMode = RF_SUB_1_GHZ_FRONT_END_MODE;
261 cmd_radio_setup.config.biasMode = RF_SUB_1_GHZ_BIAS_MODE;
262 cmd_radio_setup.centerFreq = PROP_MODE_CENTER_FREQ;
263 cmd_radio_setup.loDivider = PROP_MODE_LO_DIVIDER;
265 cmd_tx.numHdrBits = DOT_4G_PHR_NUM_BYTES * 8;
266 cmd_tx.syncWord = DOT_4G_SYNCWORD;
268 cmd_rx.syncWord0 = DOT_4G_SYNCWORD;
269 cmd_rx.syncWord1 = 0x00000000;
270 cmd_rx.maxPktLen = DOT_4G_MAX_FRAME_LEN - DOT_4G_LEN_OFFSET;
271 cmd_rx.hdrConf.numHdrBits = DOT_4G_PHR_NUM_BYTES * 8;
272 cmd_rx.lenOffset = DOT_4G_LEN_OFFSET;
273 cmd_rx.pQueue = data_queue_init(
sizeof(lensz_t));
274 cmd_rx.pOutput = (uint8_t *)&prop_radio.rx_stats;
281 bool stop_rx =
false;
282 int8_t rssi = RF_GET_RSSI_ERROR_VAL;
285 if(!rx_is_active()) {
287 if(v_cmd_rx.status != PENDING) {
288 res = netstack_sched_rx(
false);
289 if(res != RF_RESULT_OK) {
290 LOG_ERR(
"RSSI measurement failed to schedule RX\n");
301 if(!rx_is_active()) {
302 LOG_ERR(
"RSSI measurement failed to turn on RX, RX status=0x%04X\n", v_cmd_rx.status);
303 return RF_RESULT_ERROR;
308 rssi = RF_getRssi(prop_radio.rf_handle);
320 uint32_t freq_khz = v_cmd_fs.frequency * 1000;
328 freq_khz += (((v_cmd_fs.fractFreq * 1000) + 65535) / 65536);
330 return (uint8_t)((freq_khz - DOT_15_4G_CHAN0_FREQ) / DOT_15_4G_FREQ_SPACING);
336 if(!dot_15_4g_chan_in_range(channel)) {
337 LOG_WARN(
"Supplied hannel %d is illegal, defaults to %d\n",
338 (
int)channel, DOT_15_4G_DEFAULT_CHAN);
339 channel = DOT_15_4G_DEFAULT_CHAN;
342 if(channel == prop_radio.channel) {
347 return set_channel_force(channel);
352 set_channel_force(uint16_t channel)
356 if(prop_radio.rf_is_on) {
361 const uint32_t new_freq = dot_15_4g_freq(channel);
362 const uint16_t freq = (uint16_t)(new_freq / 1000);
363 const uint16_t frac = (uint16_t)(((new_freq - (freq * 1000)) * 0x10000) / 1000);
365 LOG_DBG(
"Set channel to %d, frequency 0x%04X.0x%04X (%lu)\n",
366 (
int)channel, freq, frac, new_freq);
368 v_cmd_fs.frequency = freq;
369 v_cmd_fs.fractFreq = frac;
371 res = netstack_sched_fs();
373 if(res != RF_RESULT_OK) {
377 prop_radio.channel = channel;
382 calculate_lqi(int8_t rssi)
389 rssi = CLAMP(rssi, ED_RF_POWER_MIN_DBM, ED_RF_POWER_MAX_DBM);
397 return (ED_MAX * (rssi - ED_RF_POWER_MIN_DBM)) / (ED_RF_POWER_MAX_DBM - ED_RF_POWER_MIN_DBM);
401 set_send_on_cca(
bool enable)
403 prop_radio.send_on_cca = enable;
407 prepare(
const void *payload,
unsigned short payload_len)
409 if(payload_len > TX_BUF_PAYLOAD_LEN || payload_len > MAX_PAYLOAD_LEN) {
413 memcpy(prop_radio.tx_buf + TX_BUF_HDR_LEN, payload, payload_len);
418 transmit(
unsigned short transmit_len)
422 if(transmit_len > MAX_PAYLOAD_LEN) {
423 LOG_ERR(
"Too long\n");
428 LOG_ERR(
"A transmission is already active\n");
433 LOG_WARN(
"Channel is not clear for transmission\n");
438 const uint16_t total_length = transmit_len + CRC_LEN;
447 prop_radio.tx_buf[0] = ((total_length >> 0) & 0xFF);
448 prop_radio.tx_buf[1] = ((total_length >> 8) & 0xFF) + DOT_4G_PHR_DW_BIT + DOT_4G_PHR_CRC_BIT;
452 v_cmd_tx.pktLen = transmit_len + DOT_4G_PHR_NUM_BYTES;
453 v_cmd_tx.pPkt = prop_radio.tx_buf;
455 res = netstack_sched_prop_tx(transmit_len);
457 if(res != RF_RESULT_OK) {
458 LOG_WARN(
"Channel is not clear for transmission\n");
462 return (res == RF_RESULT_OK)
468 send(
const void *payload,
unsigned short payload_len)
475 read(
void *buf,
unsigned short buf_len)
477 volatile data_entry_t *data_entry = data_queue_current_entry();
481 while((data_entry->status == DATA_ENTRY_BUSY) &&
482 RTIMER_CLOCK_LT(
RTIMER_NOW(), t0 + TIMEOUT_DATA_ENTRY_BUSY));
484 #if MAC_CONF_WITH_TSCH 486 is_receiving_packet = 0;
489 if(data_entry->status != DATA_ENTRY_FINISHED) {
514 uint8_t *
const frame_ptr = (uint8_t *)&data_entry->data;
515 const lensz_t frame_len = *(lensz_t *)frame_ptr;
519 LOG_ERR(
"Received frame is too short, len=%d\n", frame_len);
521 data_queue_release_entry();
525 const uint8_t *payload_ptr = frame_ptr +
sizeof(lensz_t);
526 const unsigned short payload_len = (
unsigned short)(frame_len -
FRAME_SHAVE);
529 if(payload_len > buf_len) {
530 LOG_ERR(
"Payload of received frame is too large for local buffer, len=%d buf_len=%d\n",
531 payload_len, buf_len);
533 data_queue_release_entry();
537 memcpy(buf, payload_ptr, payload_len);
540 prop_radio.last.rssi = (int8_t)payload_ptr[payload_len];
542 prop_radio.last.corr_lqi = calculate_lqi(prop_radio.last.rssi);
545 memcpy(&rat_ticks, payload_ptr + payload_len + 1, 4);
548 prop_radio.last.timestamp = rat_to_timestamp(rat_ticks, RAT_TIMESTAMP_OFFSET);
550 if(!prop_radio.poll_mode) {
554 packetbuf_set_attr(PACKETBUF_ATTR_RSSI, (packetbuf_attr_t)prop_radio.last.rssi);
555 packetbuf_set_attr(PACKETBUF_ATTR_LINK_QUALITY, (packetbuf_attr_t)prop_radio.last.corr_lqi);
558 data_queue_release_entry();
559 return (
int)payload_len;
565 const int8_t rssi = get_rssi();
567 if(rssi == RF_GET_RSSI_ERROR_VAL) {
568 return CCA_STATE_INVALID;
571 return (rssi < prop_radio.rssi_threshold)
580 LOG_ERR(
"Channel clear called while in TX\n");
584 const uint8_t cca_state = cca_request();
587 return cca_state == CCA_STATE_IDLE;
593 if(!prop_radio.rf_is_on) {
597 #if MAC_CONF_WITH_TSCH 605 if(!is_receiving_packet) {
609 if(HWREG(RFC_DBELL_BASE + RFC_DBELL_O_RFHWIFG) & RFC_DBELL_RFHWIFG_MDMSOFT) {
610 is_receiving_packet = 1;
614 is_receiving_packet = (cca_request() == CCA_STATE_BUSY);
615 if(!is_receiving_packet) {
617 RFCHwIntClear(RFC_DBELL_RFHWIFG_MDMSOFT);
621 return is_receiving_packet;
643 if(cca_request() == CCA_STATE_BUSY) {
655 const data_entry_t *
const read_entry = data_queue_current_entry();
656 volatile const data_entry_t *curr_entry = read_entry;
662 const uint8_t status = curr_entry->status;
663 if((status == DATA_ENTRY_FINISHED) ||
664 (status == DATA_ENTRY_BUSY)) {
669 curr_entry = (data_entry_t *)curr_entry->pNextEntry;
670 }
while(curr_entry != read_entry);
672 if(num_pending > 0 && !prop_radio.poll_mode) {
685 if(prop_radio.rf_is_on) {
686 LOG_WARN(
"Radio is already on\n");
692 res = netstack_sched_rx(
true);
694 if(res != RF_RESULT_OK) {
695 return RF_RESULT_ERROR;
698 prop_radio.rf_is_on =
true;
705 if(!prop_radio.rf_is_on) {
706 LOG_WARN(
"Radio is already off\n");
712 prop_radio.rf_is_on =
false;
728 *value = (prop_radio.rf_is_on)
741 if(prop_radio.poll_mode) {
752 res = rf_get_tx_power(prop_radio.rf_handle, rf_tx_power_table, (int8_t *)&value);
753 return ((res == RF_RESULT_OK) &&
754 (*value != RF_TxPowerTable_INVALID_DBM))
759 *value = prop_radio.rssi_threshold;
764 return (*value == RF_GET_RSSI_ERROR_VAL)
769 *value = DOT_15_4G_CHAN_MIN;
773 *value = DOT_15_4G_CHAN_MAX;
781 *value = (
radio_value_t)tx_power_max(rf_tx_power_table, rf_tx_power_table_size);
784 case RADIO_CONST_MAX_PAYLOAD_LEN:
802 return (
on() == RF_RESULT_OK)
814 return (res == RF_RESULT_OK)
819 if(!tx_power_in_range((int8_t)value, rf_tx_power_table, rf_tx_power_table_size)) {
822 res = rf_set_tx_power(prop_radio.rf_handle, rf_tx_power_table, (int8_t)value);
823 return (res == RF_RESULT_OK)
833 const bool old_poll_mode = prop_radio.poll_mode;
835 if(old_poll_mode == prop_radio.poll_mode) {
838 if(!prop_radio.rf_is_on) {
843 res = netstack_sched_rx(
false);
844 return (res == RF_RESULT_OK)
857 prop_radio.rssi_threshold = (int8_t)value;
866 get_object(radio_param_t param,
void *dest,
size_t size)
875 if(size !=
sizeof(rtimer_clock_t)) {
879 *(rtimer_clock_t *)dest = prop_radio.last.timestamp;
889 set_object(radio_param_t param,
const void *src,
size_t size)
898 RF_TxPowerTable_Value tx_power_value;
901 prop_radio.rx_is_active = rx_is_active;
903 radio_mode = (simplelink_radio_mode_t *)&prop_radio;
905 if(prop_radio.rf_handle) {
906 LOG_WARN(
"Radio is already initialized\n");
911 prop_radio.rf_is_on =
false;
914 prop_radio.rssi_threshold = PROP_MODE_CCA_RSSI_THRESHOLD;
919 RF_Params_init(&rf_params);
923 prop_radio.rf_handle = netstack_open(&rf_params);
925 if(prop_radio.rf_handle == NULL) {
926 LOG_ERR(
"Unable to open RF driver during initialization\n");
927 return RF_RESULT_ERROR;
932 tx_power_value = RF_TxPowerTable_findValue(rf_tx_power_table, RF_TXPOWER_DBM);
933 if(tx_power_value.rawValue != RF_TxPowerTable_INVALID_VALUE) {
934 rf_stat = RF_setTxPower(prop_radio.rf_handle, tx_power_value);
935 if(rf_stat == RF_StatSuccess) {
936 LOG_INFO(
"TX power configured to %d dBm\n", RF_TXPOWER_DBM);
938 LOG_WARN(
"Setting TX power to %d dBm failed, stat=0x%02X", RF_TXPOWER_DBM, rf_stat);
941 LOG_WARN(
"Unable to find TX power %d dBm in the TX power table\n", RF_TXPOWER_DBM);
944 ENERGEST_ON(ENERGEST_TYPE_LISTEN);
radio_result_t(* get_object)(radio_param_t param, void *dest, size_t size)
Get a radio parameter object.
Header file with descriptors for the various modes of operation defined in IEEE 802.15.4g.
int(* prepare)(const void *payload, unsigned short payload_len)
Prepare the radio with a packet to be sent.
Header file of TX power functionality of CC13xx/CC26xx.
The parameter is not supported.
static uint8_t rf_is_on(void)
Checks whether the RFC domain is accessible and the RFC is in IEEE RX.
#define FRAME_SHAVE
RSSI (1) + Timestamp (4) + Status (1)
Header file for the energy estimation mechanism
TX failed due to a collision.
The maximum transmission power in dBm.
Received signal strength indicator in dBm.
int(* receiving_packet)(void)
Check if the radio driver is currently receiving a packet.
Header file of the generic radio mode API.
radio_result_t(* set_value)(radio_param_t param, radio_value_t value)
Set a radio parameter value.
int(* pending_packet)(void)
Check if a packet has been received and is available in the radio driver's buffers.
The structure of a Contiki-NG radio device driver.
#define RTIMER_BUSYWAIT_UNTIL(cond, max_time)
Busy-wait until a condition for at most max_time.
static void set_channel(uint8_t channel)
Set the current operating channel.
Channel used for radio communication.
The value argument was incorrect.
The parameter was set/read successfully.
int(* channel_clear)(void)
Perform a Clear-Channel Assessment (CCA) to find out if there is a packet in the air or not...
int radio_value_t
Each radio has a set of parameters that designate the current configuration and state of the radio...
An error occurred when getting/setting the parameter, but the arguments were otherwise correct...
Header file of the CC13xx/CC26xx RAT timer handler.
Radio transmission mode determines if the radio has send on CCA (RADIO_TX_MODE_SEND_ON_CCA) enabled o...
#define IEEE802154_DEFAULT_CHANNEL
The default channel for IEEE 802.15.4 networks.
#define RTIMER_NOW()
Get the current clock time.
Clear channel assessment threshold in dBm.
Header file of the CC13xx/CC26xx RF scheduler.
int(* send)(const void *payload, unsigned short payload_len)
Prepare & transmit a packet.
int(* transmit)(unsigned short transmit_len)
Send the packet that has previously been prepared.
void process_poll(struct process *p)
Request a process to be polled.
int(* off)(void)
Turn the radio off.
#define RF_CONF_INACTIVITY_TIMEOUT
2 ms
The lowest radio channel number.
Radio receiver mode determines if the radio has address filter (RADIO_RX_MODE_ADDRESS_FILTER) and aut...
Header file for the real-time timer module.
The highest radio channel number.
Header file of the CC13xx/CC26xx RF data queue.
When getting the value of this parameter, the radio driver should indicate whether the radio is on or...
enum radio_result_e radio_result_t
Radio return values when setting or getting radio parameters.
#define RADIO_TX_MODE_SEND_ON_CCA
Radio TX mode control / retrieval.
int(* init)(void)
Initialise the radio hardware.
Header file of RF settings for CC13xx/CC26xx.
#define RADIO_RX_MODE_POLL_MODE
Enable/disable/get the state of radio driver poll mode operation.
The minimum transmission power in dBm.
Radio powered on and able to receive frames.
int(* read)(void *buf, unsigned short buf_len)
Read a received packet into a buffer.
Transmission power in dBm.
Header file of common CC13xx/CC26xx RF functionality.
Header file for the Packet buffer (packetbuf) management
Include file for the Contiki low-layer network stack (NETSTACK)
radio_result_t(* get_value)(radio_param_t param, radio_value_t *value)
Get a radio parameter value.
Default definitions of C compiler quirk work-arounds.
Last packet timestamp, of type rtimer_clock_t.
An error occurred during transmission.
Header file for the logging system
radio_result_t(* set_object)(radio_param_t param, const void *src, size_t size)
Set a radio parameter object.
Radio powered off and in the lowest possible power consumption state.
TX was successful and where an ACK was requested one was received.
int(* on)(void)
Turn the radio on.
void process_start(struct process *p, process_data_t data)
Start a process.
static uint8_t get_channel()
Get the current operating channel.