Contiki-NG
prop-mode.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2018, Texas Instruments Incorporated - http://www.ti.com/
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the copyright holder nor the names of its
14  * contributors may be used to endorse or promote products derived
15  * from this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
20  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
21  * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
22  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
28  * OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30 /**
31  * \addtogroup cc13xx-cc26xx-rf
32  * @{
33  *
34  * \defgroup cc13xx-cc26xx-rf-prop Prop-mode driver for CC13xx/CC26xx
35  *
36  * @{
37  *
38  * \file
39  * Implementation of the CC13xx/CC26xx prop-mode NETSTACK_RADIO driver.
40  * \author
41  * Edvard Pettersen <e.pettersen@ti.com>
42  */
43 /*---------------------------------------------------------------------------*/
44 #include "contiki.h"
45 #include "net/packetbuf.h"
46 #include "net/netstack.h"
47 #include "sys/energest.h"
48 #include "sys/clock.h"
49 #include "sys/rtimer.h"
50 #include "sys/cc.h"
51 #include "dev/watchdog.h"
52 /*---------------------------------------------------------------------------*/
53 /* RF Core Mailbox API */
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)
60 
61 #include <ti/drivers/rf/RF.h>
62 #include DeviceFamily_constructPath(inc/hw_rfc_dbell.h)
63 #include DeviceFamily_constructPath(driverlib/rfc.h)
64 /*---------------------------------------------------------------------------*/
65 /* Platform RF dev */
66 #include "rf/rf.h"
67 #include "rf/dot-15-4g.h"
68 #include "rf/sched.h"
69 #include "rf/data-queue.h"
70 #include "rf/tx-power.h"
71 #include "rf/settings.h"
72 #include "rf/rat.h"
73 #include "rf/radio-mode.h"
74 /*---------------------------------------------------------------------------*/
75 #include <stdint.h>
76 #include <string.h>
77 #include <stdio.h>
78 #include <stdbool.h>
79 #include <assert.h>
80 /*---------------------------------------------------------------------------*/
81 /* Log configuration */
82 #include "sys/log.h"
83 #define LOG_MODULE "Radio"
84 #define LOG_LEVEL LOG_LEVEL_NONE
85 /*---------------------------------------------------------------------------*/
86 #undef CLAMP
87 #define CLAMP(v, vmin, vmax) (MAX(MIN(v, vmax), vmin))
88 /*---------------------------------------------------------------------------*/
89 /* Configuration parameters */
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
95 /*---------------------------------------------------------------------------*/
96 /* Used for checking result of CCA_REQ command */
97 typedef enum {
98  CCA_STATE_IDLE = 0,
99  CCA_STATE_BUSY = 1,
100  CCA_STATE_INVALID = 2
101 } cca_state_t;
102 /*---------------------------------------------------------------------------*/
103 #if MAC_CONF_WITH_TSCH
104 static volatile uint8_t is_receiving_packet;
105 #endif
106 /*---------------------------------------------------------------------------*/
107 /* Defines and variables related to the .15.4g PHY HDR */
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
112 
113 /* PHY HDR bits */
114 #define DOT_4G_PHR_CRC16 0x10
115 #define DOT_4G_PHR_DW 0x08
116 
117 #if PROP_MODE_USE_CRC16
118 /* CRC16 */
119 #define DOT_4G_PHR_CRC_BIT DOT_4G_PHR_CRC16
120 #define CRC_LEN 2
121 #else
122 /* CRC32 */
123 #define DOT_4G_PHR_CRC_BIT 0
124 #define CRC_LEN 4
125 #endif /* PROP_MODE_USE_CRC16 */
126 
127 #if PROP_MODE_DYN_WHITENER
128 #define DOT_4G_PHR_DW_BIT DOT_4G_PHR_DW
129 #else
130 #define DOT_4G_PHR_DW_BIT 0
131 #endif
132 /*---------------------------------------------------------------------------*/
133 /*
134  * The maximum number of bytes this driver can accept from the MAC layer for
135  * transmission or will deliver to the MAC layer after reception. Includes
136  * the MAC header and payload, but not the CRC.
137  *
138  * Unlike typical 2.4GHz radio drivers, this driver supports the .15.4g
139  * 32-bit CRC option.
140  *
141  * This radio hardware is perfectly happy to transmit frames longer than 127
142  * bytes, which is why it's OK to end up transmitting 125 payload bytes plus
143  * a 4-byte CRC.
144  *
145  * In the future we can change this to support transmission of long frames,
146  * for example as per .15.4g. the size of the TX and RX buffers would need
147  * adjusted accordingly.
148  */
149 #define MAX_PAYLOAD_LEN 125
150 /*---------------------------------------------------------------------------*/
151 /* How long to wait for the RF to enter RX in rf_cmd_ieee_rx */
152 #define TIMEOUT_ENTER_RX_WAIT (RTIMER_SECOND >> 10)
153 
154 /* How long to wait for the rx read entry to become ready */
155 #define TIMEOUT_DATA_ENTRY_BUSY (RTIMER_SECOND / 250)
156 /*---------------------------------------------------------------------------*/
157 /*
158  * Offset of the end of SFD when compared to the radio HW-generated timestamp.
159  */
160 #define RAT_TIMESTAMP_OFFSET USEC_TO_RAT(RADIO_PHY_HEADER_LEN * RADIO_BYTE_AIR_TIME - 270)
161 /*---------------------------------------------------------------------------*/
162 /* TX buf configuration */
163 #define TX_BUF_HDR_LEN 2
164 #define TX_BUF_PAYLOAD_LEN 180
165 
166 #define TX_BUF_SIZE (TX_BUF_HDR_LEN + TX_BUF_PAYLOAD_LEN)
167 /*---------------------------------------------------------------------------*/
168 /* Size of the Length field in Data Entry, two bytes in this case */
169 typedef uint16_t lensz_t;
170 
171 #define FRAME_OFFSET sizeof(lensz_t)
172 #define FRAME_SHAVE 6 /**< RSSI (1) + Timestamp (4) + Status (1) */
173 /*---------------------------------------------------------------------------*/
174 /* Constants used when calculating the LQI from the RSSI */
175 #define RX_SENSITIVITY_DBM -110
176 #define RX_SATURATION_DBM 10
177 #define ED_MIN_DBM_ABOVE_RX_SENSITIVITY 10
178 #define ED_MAX 0xFF
179 
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
182 /*---------------------------------------------------------------------------*/
183 /* RF Core typedefs */
184 typedef rfc_propRxOutput_t rx_output_t;
185 
186 typedef struct {
187  /* RF driver */
188  RF_Handle rf_handle;
189 
190  /* Are we currently in poll mode? */
191  bool poll_mode;
192 
193  /* RAT Overflow Upkeep */
194  struct {
195  struct ctimer overflow_timer;
196  rtimer_clock_t last_overflow;
197  volatile uint32_t overflow_count;
198  } rat;
199 
200  bool (* rx_is_active)(void);
201 
202  /* Outgoing frame buffer */
203  uint8_t tx_buf[TX_BUF_SIZE] CC_ALIGN(4);
204 
205  /* RX Statistics struct */
206  rx_output_t rx_stats;
207 
208  /* RSSI Threshold */
209  int8_t rssi_threshold;
210  uint16_t channel;
211 
212  /* Indicates RF is supposed to be on or off */
213  uint8_t rf_is_on;
214  /* Enable/disable CCA before sending */
215  bool send_on_cca;
216 
217  /* Last RX operation stats */
218  struct {
219  int8_t rssi;
220  uint8_t corr_lqi;
221  uint32_t timestamp;
222  } last;
223 } prop_radio_t;
224 
225 static prop_radio_t prop_radio;
226 
227 /*---------------------------------------------------------------------------*/
228 /* Convenience macros for more succinct access of RF commands */
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
233 
234 /* Convenience macros for volatile access with the RF commands */
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)
239 /*---------------------------------------------------------------------------*/
240 static inline bool
241 tx_is_active(void)
242 {
243  return v_cmd_tx.status == ACTIVE;
244 }
245 /*---------------------------------------------------------------------------*/
246 static inline bool
247 rx_is_active(void)
248 {
249  return v_cmd_rx.status == ACTIVE;
250 }
251 /*---------------------------------------------------------------------------*/
252 static int channel_clear(void);
253 static int on(void);
254 static int off(void);
255 static rf_result_t set_channel_force(uint16_t channel);
256 /*---------------------------------------------------------------------------*/
257 static void
258 init_rf_params(void)
259 {
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;
264 
265  cmd_tx.numHdrBits = DOT_4G_PHR_NUM_BYTES * 8;
266  cmd_tx.syncWord = DOT_4G_SYNCWORD;
267 
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;
275 }
276 /*---------------------------------------------------------------------------*/
277 static int8_t
278 get_rssi(void)
279 {
280  rf_result_t res;
281  bool stop_rx = false;
282  int8_t rssi = RF_GET_RSSI_ERROR_VAL;
283 
284  /* RX is required to be running in order to do a RSSI measurement */
285  if(!rx_is_active()) {
286  /* If RX is not pending, i.e. soon to be running, schedule the RX command */
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");
291  return res;
292  }
293 
294  /* We only stop RX if we had to schedule it */
295  stop_rx = true;
296  }
297 
298  /* Make sure RX is running before we continue, unless we timeout and fail */
299  RTIMER_BUSYWAIT_UNTIL(rx_is_active(), TIMEOUT_ENTER_RX_WAIT);
300 
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;
304  }
305  }
306 
307  /* Perform the RSSI measurement */
308  rssi = RF_getRssi(prop_radio.rf_handle);
309 
310  if(stop_rx) {
311  netstack_stop_rx();
312  }
313 
314  return rssi;
315 }
316 /*---------------------------------------------------------------------------*/
317 static uint8_t
318 get_channel(void)
319 {
320  uint32_t freq_khz = v_cmd_fs.frequency * 1000;
321 
322  /*
323  * For some channels, fractFreq * 1000 / 65536 will return 324.99xx.
324  * Casting the result to uint32_t will truncate decimals resulting in the
325  * function returning channel - 1 instead of channel. Thus, we do a quick
326  * positive integer round up.
327  */
328  freq_khz += (((v_cmd_fs.fractFreq * 1000) + 65535) / 65536);
329 
330  return (uint8_t)((freq_khz - DOT_15_4G_CHAN0_FREQ) / DOT_15_4G_FREQ_SPACING);
331 }
332 /*---------------------------------------------------------------------------*/
333 static rf_result_t
334 set_channel(uint16_t channel)
335 {
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;
340  }
341 
342  if(channel == prop_radio.channel) {
343  /* We are already calibrated to this channel */
344  return RF_RESULT_OK;
345  }
346 
347  return set_channel_force(channel);
348 }
349 /*---------------------------------------------------------------------------*/
350 /* Sets the given channel without checking if it is a valid channel number. */
351 static rf_result_t
352 set_channel_force(uint16_t channel)
353 {
354  rf_result_t res;
355 
356  if(prop_radio.rf_is_on) {
357  /* Force RAT and RTC resync */
358  rf_restart_rat();
359  }
360 
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);
364 
365  LOG_DBG("Set channel to %d, frequency 0x%04X.0x%04X (%lu)\n",
366  (int)channel, freq, frac, new_freq);
367 
368  v_cmd_fs.frequency = freq;
369  v_cmd_fs.fractFreq = frac;
370 
371  res = netstack_sched_fs();
372 
373  if(res != RF_RESULT_OK) {
374  return res;
375  }
376 
377  prop_radio.channel = channel;
378  return RF_RESULT_OK;
379 }
380 /*---------------------------------------------------------------------------*/
381 static uint8_t
382 calculate_lqi(int8_t rssi)
383 {
384  /*
385  * Note : Currently the LQI value is simply the energy detect measurement.
386  * A more accurate value could be derived by using the correlation
387  * value along with the RSSI value.
388  */
389  rssi = CLAMP(rssi, ED_RF_POWER_MIN_DBM, ED_RF_POWER_MAX_DBM);
390 
391  /*
392  * Create energy detect measurement by normalizing and scaling RF power level.
393  * Note : The division operation below is designed for maximum accuracy and
394  * best granularity. This is done by grouping the math operations to
395  * compute the entire numerator before doing any division.
396  */
397  return (ED_MAX * (rssi - ED_RF_POWER_MIN_DBM)) / (ED_RF_POWER_MAX_DBM - ED_RF_POWER_MIN_DBM);
398 }
399 /*---------------------------------------------------------------------------*/
400 static void
401 set_send_on_cca(bool enable)
402 {
403  prop_radio.send_on_cca = enable;
404 }
405 /*---------------------------------------------------------------------------*/
406 static int
407 prepare(const void *payload, unsigned short payload_len)
408 {
409  if(payload_len > TX_BUF_PAYLOAD_LEN || payload_len > MAX_PAYLOAD_LEN) {
410  return RADIO_TX_ERR;
411  }
412 
413  memcpy(prop_radio.tx_buf + TX_BUF_HDR_LEN, payload, payload_len);
414  return 0;
415 }
416 /*---------------------------------------------------------------------------*/
417 static int
418 transmit(unsigned short transmit_len)
419 {
420  rf_result_t res;
421 
422  if(transmit_len > MAX_PAYLOAD_LEN) {
423  LOG_ERR("Too long\n");
424  return RADIO_TX_ERR;
425  }
426 
427  if(tx_is_active()) {
428  LOG_ERR("A transmission is already active\n");
429  return RADIO_TX_ERR;
430  }
431 
432  if(prop_radio.send_on_cca && !channel_clear()) {
433  LOG_WARN("Channel is not clear for transmission\n");
434  return RADIO_TX_COLLISION;
435  }
436 
437  /* Length in .15.4g PHY HDR. Includes the CRC but not the HDR itself */
438  const uint16_t total_length = transmit_len + CRC_LEN;
439  /*
440  * Prepare the .15.4g PHY header
441  * MS=0, Length MSBits=0, DW and CRC configurable
442  * Total length = transmit_len (payload) + CRC length
443  *
444  * The Radio will flip the bits around, so tx_buf[0] must have the length
445  * LSBs (PHR[15:8] and tx_buf[1] will have PHR[7:0]
446  */
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;
449 
450  /* pktLen: Total number of bytes in the TX buffer, including the header if
451  * one exists, but not including the CRC (which is not present in the buffer) */
452  v_cmd_tx.pktLen = transmit_len + DOT_4G_PHR_NUM_BYTES;
453  v_cmd_tx.pPkt = prop_radio.tx_buf;
454 
455  res = netstack_sched_prop_tx(transmit_len);
456 
457  if(res != RF_RESULT_OK) {
458  LOG_WARN("Channel is not clear for transmission\n");
459  return RADIO_TX_ERR;
460  }
461 
462  return (res == RF_RESULT_OK)
463  ? RADIO_TX_OK
464  : RADIO_TX_ERR;
465 }
466 /*---------------------------------------------------------------------------*/
467 static int
468 send(const void *payload, unsigned short payload_len)
469 {
470  prepare(payload, payload_len);
471  return transmit(payload_len);
472 }
473 /*---------------------------------------------------------------------------*/
474 static int
475 read(void *buf, unsigned short buf_len)
476 {
477  volatile data_entry_t *data_entry = data_queue_current_entry();
478 
479  /* Only wait if the Radio is accessing the entry */
480  const rtimer_clock_t t0 = RTIMER_NOW();
481  while((data_entry->status == DATA_ENTRY_BUSY) &&
482  RTIMER_CLOCK_LT(RTIMER_NOW(), t0 + TIMEOUT_DATA_ENTRY_BUSY));
483 
484 #if MAC_CONF_WITH_TSCH
485  /* Make sure the flag is reset */
486  is_receiving_packet = 0;
487 #endif
488 
489  if(data_entry->status != DATA_ENTRY_FINISHED) {
490  /* No available data */
491  return 0;
492  }
493 
494  /*
495  * lensz bytes (2) in the data entry are the length of the received frame.
496  * Data frame is on the following format:
497  * Length (2) + Payload (N) + RSSI (1) + Timestamp (4) + Status (1)
498  * Data frame DOES NOT contain the following:
499  * no Header/PHY bytes
500  * no appended Received CRC bytes
501  * Visual representation of frame format:
502  *
503  * +---------+---------+--------+-----------+--------+
504  * | 2 bytes | N bytes | 1 byte | 4 bytes | 1 byte |
505  * +---------+---------+--------+-----------+--------+
506  * | Length | Payload | RSSI | Timestamp | Status |
507  * +---------+---------+--------+-----------+--------+
508  *
509  * Length bytes equal total length of entire frame excluding itself,
510  * Length = N + RSSI (1) + Timestamp (4) + Status (1)
511  * = N + 6
512  * N = Length - 6
513  */
514  uint8_t *const frame_ptr = (uint8_t *)&data_entry->data;
515  const lensz_t frame_len = *(lensz_t *)frame_ptr;
516 
517  /* Sanity check that Frame is at least Frame Shave bytes long */
518  if(frame_len < FRAME_SHAVE) {
519  LOG_ERR("Received frame is too short, len=%d\n", frame_len);
520 
521  data_queue_release_entry();
522  return 0;
523  }
524 
525  const uint8_t *payload_ptr = frame_ptr + sizeof(lensz_t);
526  const unsigned short payload_len = (unsigned short)(frame_len - FRAME_SHAVE);
527 
528  /* Sanity check that Payload fits in Buffer */
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);
532 
533  data_queue_release_entry();
534  return 0;
535  }
536 
537  memcpy(buf, payload_ptr, payload_len);
538 
539  /* RSSI stored after payload */
540  prop_radio.last.rssi = (int8_t)payload_ptr[payload_len];
541  /* LQI calculated from RSSI */
542  prop_radio.last.corr_lqi = calculate_lqi(prop_radio.last.rssi);
543  /* Timestamp stored RSSI (1) bytes after payload. */
544  uint32_t rat_ticks;
545  memcpy(&rat_ticks, payload_ptr + payload_len + 1, 4);
546 
547  /* Correct timestamp so that it refers to the end of the SFD */
548  prop_radio.last.timestamp = rat_to_timestamp(rat_ticks, RAT_TIMESTAMP_OFFSET);
549 
550  if(!prop_radio.poll_mode) {
551  /* Not in poll mode: packetbuf should not be accessed in interrupt context. */
552  /* In poll mode, the last packet RSSI and link quality can be obtained through */
553  /* RADIO_PARAM_LAST_RSSI and RADIO_PARAM_LAST_LINK_QUALITY */
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);
556  }
557 
558  data_queue_release_entry();
559  return (int)payload_len;
560 }
561 /*---------------------------------------------------------------------------*/
562 static uint8_t
563 cca_request(void)
564 {
565  const int8_t rssi = get_rssi();
566 
567  if(rssi == RF_GET_RSSI_ERROR_VAL) {
568  return CCA_STATE_INVALID;
569  }
570 
571  return (rssi < prop_radio.rssi_threshold)
572  ? CCA_STATE_IDLE
573  : CCA_STATE_BUSY;
574 }
575 /*---------------------------------------------------------------------------*/
576 static int
577 channel_clear(void)
578 {
579  if(tx_is_active()) {
580  LOG_ERR("Channel clear called while in TX\n");
581  return 0;
582  }
583 
584  const uint8_t cca_state = cca_request();
585 
586  /* Channel is clear if CCA state is IDLE */
587  return cca_state == CCA_STATE_IDLE;
588 }
589 /*---------------------------------------------------------------------------*/
590 static int
591 receiving_packet(void)
592 {
593  if(!prop_radio.rf_is_on) {
594  return 0;
595  }
596 
597 #if MAC_CONF_WITH_TSCH
598  /*
599  * Under TSCH operation, we rely on "hints" from the MDMSOFT interrupt
600  * flag. This flag is set by the radio upon sync word detection, but it is
601  * not cleared automatically by hardware. We store state in a variable after
602  * first call. The assumption is that the TSCH code will keep calling us
603  * until frame reception has completed, at which point we can clear MDMSOFT.
604  */
605  if(!is_receiving_packet) {
606  /* Look for the modem synchronization word detection interrupt flag.
607  * This flag is raised when the synchronization word is received.
608  */
609  if(HWREG(RFC_DBELL_BASE + RFC_DBELL_O_RFHWIFG) & RFC_DBELL_RFHWIFG_MDMSOFT) {
610  is_receiving_packet = 1;
611  }
612  } else {
613  /* After the start of the packet: reset the Rx flag once the channel gets clear */
614  is_receiving_packet = (cca_request() == CCA_STATE_BUSY);
615  if(!is_receiving_packet) {
616  /* Clear the modem sync flag */
617  RFCHwIntClear(RFC_DBELL_RFHWIFG_MDMSOFT);
618  }
619  }
620 
621  return is_receiving_packet;
622 #else
623  /*
624  * Under CSMA operation, there is no immediately straightforward logic as to
625  * when it's OK to clear the MDMSOFT interrupt flag:
626  *
627  * - We cannot re-use the same logic as above, since CSMA may bail out of
628  * frame TX immediately after a single call this function here. In this
629  * scenario, is_receiving_packet would remain equal to one and we would
630  * therefore erroneously signal ongoing RX in subsequent calls to this
631  * function here, even _after_ reception has completed.
632  * - We can neither clear inside read_frame() nor inside the RX frame
633  * interrupt handler (remember, we are not in poll mode under CSMA),
634  * since we risk clearing MDMSOFT after we have seen a sync word for the
635  * _next_ frame. If this happens, this function here would incorrectly
636  * return 0 during RX of this next frame.
637  *
638  * So to avoid a very convoluted logic of how to handle MDMSOFT, we simply
639  * perform a clear channel assessment here: We interpret channel activity
640  * as frame reception.
641  */
642 
643  if(cca_request() == CCA_STATE_BUSY) {
644  return 1;
645  }
646 
647  return 0;
648 
649 #endif
650 }
651 /*---------------------------------------------------------------------------*/
652 static int
653 pending_packet(void)
654 {
655  const data_entry_t *const read_entry = data_queue_current_entry();
656  volatile const data_entry_t *curr_entry = read_entry;
657 
658  int num_pending = 0;
659 
660  /* Go through RX Circular buffer and check their status */
661  do {
662  const uint8_t status = curr_entry->status;
663  if((status == DATA_ENTRY_FINISHED) ||
664  (status == DATA_ENTRY_BUSY)) {
665  num_pending += 1;
666  }
667 
668  /* Stop when we have looped the circular buffer */
669  curr_entry = (data_entry_t *)curr_entry->pNextEntry;
670  } while(curr_entry != read_entry);
671 
672  if(num_pending > 0 && !prop_radio.poll_mode) {
673  process_poll(&rf_sched_process);
674  }
675 
676  /* If we didn't find an entry at status finished, no frames are pending */
677  return num_pending;
678 }
679 /*---------------------------------------------------------------------------*/
680 static int
681 on(void)
682 {
683  rf_result_t res;
684 
685  if(prop_radio.rf_is_on) {
686  LOG_WARN("Radio is already on\n");
687  return RF_RESULT_OK;
688  }
689 
690  data_queue_reset();
691 
692  res = netstack_sched_rx(true);
693 
694  if(res != RF_RESULT_OK) {
695  return RF_RESULT_ERROR;
696  }
697 
698  prop_radio.rf_is_on = true;
699  return RF_RESULT_OK;
700 }
701 /*---------------------------------------------------------------------------*/
702 static int
703 off(void)
704 {
705  if(!prop_radio.rf_is_on) {
706  LOG_WARN("Radio is already off\n");
707  return RF_RESULT_OK;
708  }
709 
710  rf_yield();
711 
712  prop_radio.rf_is_on = false;
713  return RF_RESULT_OK;
714 }
715 /*---------------------------------------------------------------------------*/
716 static radio_result_t
717 get_value(radio_param_t param, radio_value_t *value)
718 {
719  rf_result_t res;
720 
721  if(!value) {
723  }
724 
725  switch(param) {
727  /* On / off */
728  *value = (prop_radio.rf_is_on)
731 
732  return RADIO_RESULT_OK;
733 
734  case RADIO_PARAM_CHANNEL:
735  *value = (radio_value_t)get_channel();
736  return RADIO_RESULT_OK;
737 
738  /* RX mode */
739  case RADIO_PARAM_RX_MODE:
740  *value = 0;
741  if(prop_radio.poll_mode) {
743  }
744  return RADIO_RESULT_OK;
745 
746  /* TX mode */
747  case RADIO_PARAM_TX_MODE:
748  *value = 0;
749  return RADIO_RESULT_OK;
750 
751  case RADIO_PARAM_TXPOWER:
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))
757 
759  *value = prop_radio.rssi_threshold;
760  return RADIO_RESULT_OK;
761 
762  case RADIO_PARAM_RSSI:
763  *value = get_rssi();
764  return (*value == RF_GET_RSSI_ERROR_VAL)
766  : RADIO_RESULT_OK;
767 
769  *value = DOT_15_4G_CHAN_MIN;
770  return RADIO_RESULT_OK;
771 
773  *value = DOT_15_4G_CHAN_MAX;
774  return RADIO_RESULT_OK;
775 
777  *value = (radio_value_t)tx_power_min(rf_tx_power_table);
778  return RADIO_RESULT_OK;
779 
781  *value = (radio_value_t)tx_power_max(rf_tx_power_table, rf_tx_power_table_size);
782  return RADIO_RESULT_OK;
783 
784  case RADIO_CONST_MAX_PAYLOAD_LEN:
785  *value = (radio_value_t)MAX_PAYLOAD_LEN;
786  return RADIO_RESULT_OK;
787 
788  default:
790  }
791 }
792 /*---------------------------------------------------------------------------*/
793 static radio_result_t
794 set_value(radio_param_t param, radio_value_t value)
795 {
796  rf_result_t res;
797 
798  switch(param) {
800 
801  if(value == RADIO_POWER_MODE_ON) {
802  return (on() == RF_RESULT_OK)
805  } else if(value == RADIO_POWER_MODE_OFF) {
806  off();
807  return RADIO_RESULT_OK;
808  }
809 
811 
812  case RADIO_PARAM_CHANNEL:
813  res = set_channel((uint16_t)value);
814  return (res == RF_RESULT_OK)
817 
818  case RADIO_PARAM_TXPOWER:
819  if(!tx_power_in_range((int8_t)value, rf_tx_power_table, rf_tx_power_table_size)) {
821  }
822  res = rf_set_tx_power(prop_radio.rf_handle, rf_tx_power_table, (int8_t)value);
823  return (res == RF_RESULT_OK)
826 
827  /* RX Mode */
828  case RADIO_PARAM_RX_MODE:
829  if(value & ~(RADIO_RX_MODE_POLL_MODE)) {
831  }
832 
833  const bool old_poll_mode = prop_radio.poll_mode;
834  prop_radio.poll_mode = (value & RADIO_RX_MODE_POLL_MODE) != 0;
835  if(old_poll_mode == prop_radio.poll_mode) {
836  return RADIO_RESULT_OK;
837  }
838  if(!prop_radio.rf_is_on) {
839  return RADIO_RESULT_OK;
840  }
841 
842  netstack_stop_rx();
843  res = netstack_sched_rx(false);
844  return (res == RF_RESULT_OK)
847 
848  /* TX Mode */
849  case RADIO_PARAM_TX_MODE:
850  if(value & ~(RADIO_TX_MODE_SEND_ON_CCA)) {
852  }
853  set_send_on_cca((value & RADIO_TX_MODE_SEND_ON_CCA) != 0);
854  return RADIO_RESULT_OK;
855 
857  prop_radio.rssi_threshold = (int8_t)value;
858  return RADIO_RESULT_OK;
859 
860  default:
862  }
863 }
864 /*---------------------------------------------------------------------------*/
865 static radio_result_t
866 get_object(radio_param_t param, void *dest, size_t size)
867 {
868  if(!dest) {
870  }
871 
872  switch(param) {
873  /* Last packet timestamp */
875  if(size != sizeof(rtimer_clock_t)) {
877  }
878 
879  *(rtimer_clock_t *)dest = prop_radio.last.timestamp;
880 
881  return RADIO_RESULT_OK;
882 
883  default:
885  }
886 }
887 /*---------------------------------------------------------------------------*/
888 static radio_result_t
889 set_object(radio_param_t param, const void *src, size_t size)
890 {
892 }
893 /*---------------------------------------------------------------------------*/
894 static int
895 init(void)
896 {
897  RF_Params rf_params;
898  RF_TxPowerTable_Value tx_power_value;
899  RF_Stat rf_stat;
900 
901  prop_radio.rx_is_active = rx_is_active;
902 
903  radio_mode = (simplelink_radio_mode_t *)&prop_radio;
904 
905  if(prop_radio.rf_handle) {
906  LOG_WARN("Radio is already initialized\n");
907  return RF_RESULT_OK;
908  }
909 
910  /* RX is off */
911  prop_radio.rf_is_on = false;
912 
913  /* Set configured RSSI threshold */
914  prop_radio.rssi_threshold = PROP_MODE_CCA_RSSI_THRESHOLD;
915 
916  init_rf_params();
917 
918  /* Init RF params and specify non-default params */
919  RF_Params_init(&rf_params);
920  rf_params.nInactivityTimeout = RF_CONF_INACTIVITY_TIMEOUT;
921 
922  /* Open RF Driver */
923  prop_radio.rf_handle = netstack_open(&rf_params);
924 
925  if(prop_radio.rf_handle == NULL) {
926  LOG_ERR("Unable to open RF driver during initialization\n");
927  return RF_RESULT_ERROR;
928  }
929 
930  set_channel_force(IEEE802154_DEFAULT_CHANNEL);
931 
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);
937  } else {
938  LOG_WARN("Setting TX power to %d dBm failed, stat=0x%02X", RF_TXPOWER_DBM, rf_stat);
939  }
940  } else {
941  LOG_WARN("Unable to find TX power %d dBm in the TX power table\n", RF_TXPOWER_DBM);
942  }
943 
944  ENERGEST_ON(ENERGEST_TYPE_LISTEN);
945 
946  /* Start RAT overflow upkeep */
947  rat_init();
948 
949  /* Start RF process */
950  process_start(&rf_sched_process, NULL);
951 
952  return RF_RESULT_OK;
953 }
954 /*---------------------------------------------------------------------------*/
955 const struct radio_driver prop_mode_driver = {
956  init,
957  prepare,
958  transmit,
959  send,
960  read,
964  on,
965  off,
966  get_value,
967  set_value,
968  get_object,
969  set_object,
970 };
971 /*---------------------------------------------------------------------------*/
972 /**
973  * @}
974  * @}
975  */
radio_result_t(* get_object)(radio_param_t param, void *dest, size_t size)
Get a radio parameter object.
Definition: radio.h:762
int(* prepare)(const void *payload, unsigned short payload_len)
Prepare the radio with a packet to be sent.
Definition: radio.h:572
Header file of TX power functionality of CC13xx/CC26xx.
The parameter is not supported.
Definition: radio.h:473
static uint8_t rf_is_on(void)
Checks whether the RFC domain is accessible and the RFC is in IEEE RX.
Definition: ieee-mode.c:267
#define FRAME_SHAVE
RSSI (1) + Timestamp (4) + Status (1)
Definition: prop-mode.c:172
Header file for the energy estimation mechanism
TX failed due to a collision.
Definition: radio.h:503
The maximum transmission power in dBm.
Definition: radio.h:318
Received signal strength indicator in dBm.
Definition: radio.h:218
int(* receiving_packet)(void)
Check if the radio driver is currently receiving a packet.
Definition: radio.h:676
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.
Definition: radio.h:748
int(* pending_packet)(void)
Check if a packet has been received and is available in the radio driver&#39;s buffers.
Definition: radio.h:689
The structure of a Contiki-NG radio device driver.
Definition: radio.h:526
#define RTIMER_BUSYWAIT_UNTIL(cond, max_time)
Busy-wait until a condition for at most max_time.
Definition: rtimer.h:211
static void set_channel(uint8_t channel)
Set the current operating channel.
Definition: cc2538-rf.c:175
Channel used for radio communication.
Definition: radio.h:134
The value argument was incorrect.
Definition: radio.h:474
The parameter was set/read successfully.
Definition: radio.h:472
int(* channel_clear)(void)
Perform a Clear-Channel Assessment (CCA) to find out if there is a packet in the air or not...
Definition: radio.h:664
int radio_value_t
Each radio has a set of parameters that designate the current configuration and state of the radio...
Definition: radio.h:88
An error occurred when getting/setting the parameter, but the arguments were otherwise correct...
Definition: radio.h:480
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...
Definition: radio.h:180
#define IEEE802154_DEFAULT_CHANNEL
The default channel for IEEE 802.15.4 networks.
Definition: mac.h:52
#define RTIMER_NOW()
Get the current clock time.
Definition: rtimer.h:185
Clear channel assessment threshold in dBm.
Definition: radio.h:205
Header file of the CC13xx/CC26xx RF scheduler.
int(* send)(const void *payload, unsigned short payload_len)
Prepare & transmit a packet.
Definition: radio.h:623
int(* transmit)(unsigned short transmit_len)
Send the packet that has previously been prepared.
Definition: radio.h:611
void process_poll(struct process *p)
Request a process to be polled.
Definition: process.c:371
int(* off)(void)
Turn the radio off.
Definition: radio.h:721
#define RF_CONF_INACTIVITY_TIMEOUT
2 ms
The lowest radio channel number.
Definition: radio.h:303
Radio receiver mode determines if the radio has address filter (RADIO_RX_MODE_ADDRESS_FILTER) and aut...
Definition: radio.h:173
Header file for the real-time timer module.
The highest radio channel number.
Definition: radio.h:308
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...
Definition: radio.h:119
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.
Definition: radio.h:466
int(* init)(void)
Initialise the radio hardware.
Definition: radio.h:547
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.
Definition: radio.h:453
The minimum transmission power in dBm.
Definition: radio.h:313
Radio powered on and able to receive frames.
Definition: radio.h:392
int(* read)(void *buf, unsigned short buf_len)
Read a received packet into a buffer.
Definition: radio.h:647
Transmission power in dBm.
Definition: radio.h:192
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.
Definition: radio.h:733
Default definitions of C compiler quirk work-arounds.
Last packet timestamp, of type rtimer_clock_t.
Definition: radio.h:278
An error occurred during transmission.
Definition: radio.h:498
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.
Definition: radio.h:779
Radio powered off and in the lowest possible power consumption state.
Definition: radio.h:387
TX was successful and where an ACK was requested one was received.
Definition: radio.h:490
int(* on)(void)
Turn the radio on.
Definition: radio.h:703
void process_start(struct process *p, process_data_t data)
Start a process.
Definition: process.c:99
static uint8_t get_channel()
Get the current operating channel.
Definition: cc2538-rf.c:165