Contiki-NG
Loading...
Searching...
No Matches
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 <inttypes.h>
76#include <stdint.h>
77#include <string.h>
78#include <stdio.h>
79#include <stdbool.h>
80#include <assert.h>
81/*---------------------------------------------------------------------------*/
82/* Log configuration */
83#include "sys/log.h"
84#define LOG_MODULE "Radio"
85#define LOG_LEVEL LOG_LEVEL_NONE
86/*---------------------------------------------------------------------------*/
87#undef CLAMP
88#define CLAMP(v, vmin, vmax) (MAX(MIN(v, vmax), vmin))
89/*---------------------------------------------------------------------------*/
90/* Configuration parameters */
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
96/*---------------------------------------------------------------------------*/
97/* Used for checking result of CCA_REQ command */
98typedef enum {
99 CCA_STATE_IDLE = 0,
100 CCA_STATE_BUSY = 1,
101 CCA_STATE_INVALID = 2
102} cca_state_t;
103/*---------------------------------------------------------------------------*/
104#if MAC_CONF_WITH_TSCH
105static volatile uint8_t is_receiving_packet;
106#endif
107/*---------------------------------------------------------------------------*/
108/* Defines and variables related to the .15.4g PHY HDR */
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, which defines 2047 as the maximum frame size.
147 * The size of the TX and RX buffers would need to be 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/*---------------------------------------------------------------------------*/
155/*
156 * Offset of the end of SFD when compared to the radio HW-generated timestamp.
157 */
158#define RAT_TIMESTAMP_OFFSET USEC_TO_RAT(RADIO_PHY_HEADER_LEN * RADIO_BYTE_AIR_TIME - 270)
159/*---------------------------------------------------------------------------*/
160/* TX buf configuration */
161#define TX_BUF_HDR_LEN 2
162#define TX_BUF_PAYLOAD_LEN 180
163
164#define TX_BUF_SIZE (TX_BUF_HDR_LEN + TX_BUF_PAYLOAD_LEN)
165/*---------------------------------------------------------------------------*/
166/* Size of the Length field in Data Entry, two bytes in this case */
167typedef uint16_t lensz_t;
168
169#define FRAME_OFFSET sizeof(lensz_t)
170#define FRAME_SHAVE 6 /**< RSSI (1) + Timestamp (4) + Status (1) */
171/*---------------------------------------------------------------------------*/
172/* Constants used when calculating the LQI from the RSSI */
173#define RX_SENSITIVITY_DBM -110
174#define RX_SATURATION_DBM 10
175#define ED_MIN_DBM_ABOVE_RX_SENSITIVITY 10
176#define ED_MAX 0xFF
177
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
180/*---------------------------------------------------------------------------*/
181/* RF Core typedefs */
182typedef rfc_propRxOutput_t rx_output_t;
183
184typedef struct {
185 /* RF driver */
186 RF_Handle rf_handle;
187
188 /* Are we currently in poll mode? */
189 bool poll_mode;
190
191 /* RAT Overflow Upkeep */
192 struct {
193 struct ctimer overflow_timer;
194 rtimer_clock_t last_overflow;
195 volatile uint32_t overflow_count;
196 } rat;
197
198 bool (* rx_is_active)(void);
199
200 /* Outgoing frame buffer */
201 uint8_t tx_buf[TX_BUF_SIZE] CC_ALIGN(4);
202
203 /* RX Statistics struct */
204 rx_output_t rx_stats;
205
206 /* RSSI Threshold */
207 int8_t rssi_threshold;
208 uint16_t channel;
209
210 /* Indicates RF is supposed to be on or off */
211 uint8_t rf_is_on;
212 /* Enable/disable CCA before sending */
213 bool send_on_cca;
214
215 /* Last RX operation stats */
216 struct {
217 int8_t rssi;
218 uint8_t corr_lqi;
219 uint32_t timestamp;
220 } last;
221} prop_radio_t;
222
223static prop_radio_t prop_radio;
224
225/*---------------------------------------------------------------------------*/
226/* Convenience macros for more succinct access of RF commands */
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
231
232/* Convenience macros for volatile access with the RF commands */
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)
237/*---------------------------------------------------------------------------*/
238static inline bool
239tx_is_active(void)
240{
241 return v_cmd_tx.status == ACTIVE;
242}
243/*---------------------------------------------------------------------------*/
244static inline bool
245rx_is_active(void)
246{
247 return v_cmd_rx.status == ACTIVE;
248}
249/*---------------------------------------------------------------------------*/
250static int channel_clear(void);
251static int on(void);
252static int off(void);
253static rf_result_t set_channel_force(uint16_t channel);
254/*---------------------------------------------------------------------------*/
255static void
256init_rf_params(void)
257{
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;
262
263 cmd_tx.numHdrBits = DOT_4G_PHR_NUM_BYTES * 8;
264 cmd_tx.syncWord = DOT_4G_SYNCWORD;
265
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;
273}
274/*---------------------------------------------------------------------------*/
275static int8_t
276get_rssi(void)
277{
278 rf_result_t res;
279 bool stop_rx = false;
280 int8_t rssi = RF_GET_RSSI_ERROR_VAL;
281
282 /* RX is required to be running in order to do a RSSI measurement */
283 if(!rx_is_active()) {
284 /* If RX is not pending, i.e. soon to be running, schedule the RX command */
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");
289 return res;
290 }
291
292 /* We only stop RX if we had to schedule it */
293 stop_rx = true;
294 }
295
296 /* Make sure RX is running before we continue, unless we timeout and fail */
297 RTIMER_BUSYWAIT_UNTIL(rx_is_active(), TIMEOUT_ENTER_RX_WAIT);
298
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;
302 }
303 }
304
305 /* Perform the RSSI measurement */
306 rssi = RF_getRssi(prop_radio.rf_handle);
307
308 if(stop_rx) {
309 netstack_stop_rx();
310 }
311
312 return rssi;
313}
314/*---------------------------------------------------------------------------*/
315static uint8_t
316get_channel(void)
317{
318 uint32_t freq_khz = v_cmd_fs.frequency * 1000;
319
320 /*
321 * For some channels, fractFreq * 1000 / 65536 will return 324.99xx.
322 * Casting the result to uint32_t will truncate decimals resulting in the
323 * function returning channel - 1 instead of channel. Thus, we do a quick
324 * positive integer round up.
325 */
326 freq_khz += (((v_cmd_fs.fractFreq * 1000) + 65535) / 65536);
327
328 return (uint8_t)((freq_khz - DOT_15_4G_CHAN0_FREQ) / DOT_15_4G_FREQ_SPACING);
329}
330/*---------------------------------------------------------------------------*/
331static rf_result_t
332set_channel(uint16_t channel)
333{
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;
338 }
339
340 if(channel == prop_radio.channel) {
341 /* We are already calibrated to this channel */
342 return RF_RESULT_OK;
343 }
344
345 return set_channel_force(channel);
346}
347/*---------------------------------------------------------------------------*/
348/* Sets the given channel without checking if it is a valid channel number. */
349static rf_result_t
350set_channel_force(uint16_t channel)
351{
352 rf_result_t res;
353
354 if(prop_radio.rf_is_on) {
355 /* Force RAT and RTC resync */
356 rf_restart_rat();
357 }
358
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);
362
363 LOG_DBG("Set channel to %d, frequency 0x%04X.0x%04X (%" PRIu32 ")\n",
364 (int)channel, freq, frac, new_freq);
365
366 v_cmd_fs.frequency = freq;
367 v_cmd_fs.fractFreq = frac;
368
369 res = netstack_sched_fs();
370
371 if(res != RF_RESULT_OK) {
372 return res;
373 }
374
375 prop_radio.channel = channel;
376 return RF_RESULT_OK;
377}
378/*---------------------------------------------------------------------------*/
379static uint8_t
380calculate_lqi(int8_t rssi)
381{
382 /*
383 * Note : Currently the LQI value is simply the energy detect measurement.
384 * A more accurate value could be derived by using the correlation
385 * value along with the RSSI value.
386 */
387 rssi = CLAMP(rssi, ED_RF_POWER_MIN_DBM, ED_RF_POWER_MAX_DBM);
388
389 /*
390 * Create energy detect measurement by normalizing and scaling RF power level.
391 * Note : The division operation below is designed for maximum accuracy and
392 * best granularity. This is done by grouping the math operations to
393 * compute the entire numerator before doing any division.
394 */
395 return (ED_MAX * (rssi - ED_RF_POWER_MIN_DBM)) / (ED_RF_POWER_MAX_DBM - ED_RF_POWER_MIN_DBM);
396}
397/*---------------------------------------------------------------------------*/
398static void
399set_send_on_cca(bool enable)
400{
401 prop_radio.send_on_cca = enable;
402}
403/*---------------------------------------------------------------------------*/
404static int
405prepare(const void *payload, unsigned short payload_len)
406{
407 if(payload_len > TX_BUF_PAYLOAD_LEN || payload_len > MAX_PAYLOAD_LEN) {
408 return RADIO_TX_ERR;
409 }
410
411 memcpy(prop_radio.tx_buf + TX_BUF_HDR_LEN, payload, payload_len);
412 return 0;
413}
414/*---------------------------------------------------------------------------*/
415static int
416transmit(unsigned short transmit_len)
417{
418 rf_result_t res;
419
420 if(transmit_len > MAX_PAYLOAD_LEN) {
421 LOG_ERR("Too long\n");
422 return RADIO_TX_ERR;
423 }
424
425 if(tx_is_active()) {
426 LOG_ERR("A transmission is already active\n");
427 return RADIO_TX_ERR;
428 }
429
430 if(prop_radio.send_on_cca && !channel_clear()) {
431 LOG_WARN("Channel is not clear for transmission\n");
432 return RADIO_TX_COLLISION;
433 }
434
435 /* Length in .15.4g PHY HDR. Includes the CRC but not the HDR itself */
436 const uint16_t total_length = transmit_len + CRC_LEN;
437 /*
438 * Prepare the .15.4g PHY header
439 * MS=0, Length MSBits=0, DW and CRC configurable
440 * Total length = transmit_len (payload) + CRC length
441 *
442 * The Radio will flip the bits around, so tx_buf[0] must have the length
443 * LSBs (PHR[15:8] and tx_buf[1] will have PHR[7:0]
444 */
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;
447
448 /* pktLen: Total number of bytes in the TX buffer, including the header if
449 * one exists, but not including the CRC (which is not present in the buffer) */
450 v_cmd_tx.pktLen = transmit_len + DOT_4G_PHR_NUM_BYTES;
451 v_cmd_tx.pPkt = prop_radio.tx_buf;
452
453 res = netstack_sched_prop_tx(transmit_len);
454
455 if(res != RF_RESULT_OK) {
456 LOG_WARN("Channel is not clear for transmission\n");
457 return RADIO_TX_ERR;
458 }
459
460 return (res == RF_RESULT_OK)
462 : RADIO_TX_ERR;
463}
464/*---------------------------------------------------------------------------*/
465static int
466send(const void *payload, unsigned short payload_len)
467{
468 prepare(payload, payload_len);
469 return transmit(payload_len);
470}
471/*---------------------------------------------------------------------------*/
472static int
473read(void *buf, unsigned short buf_len)
474{
475 volatile data_entry_t *data_entry = data_queue_current_entry();
476
477 /* Only wait if the Radio is accessing the entry */
478 const rtimer_clock_t t0 = RTIMER_NOW();
479 while((data_entry->status == DATA_ENTRY_BUSY) &&
480 RTIMER_CLOCK_LT(RTIMER_NOW(), t0 + RADIO_FRAME_DURATION(MAX_PAYLOAD_LEN)));
481
482#if MAC_CONF_WITH_TSCH
483 /* Make sure the flag is reset */
484 is_receiving_packet = 0;
485#endif
486
487 if(data_entry->status != DATA_ENTRY_FINISHED) {
488 /* No available data */
489 return 0;
490 }
491
492 /*
493 * lensz bytes (2) in the data entry are the length of the received frame.
494 * Data frame is on the following format:
495 * Length (2) + Payload (N) + RSSI (1) + Timestamp (4) + Status (1)
496 * Data frame DOES NOT contain the following:
497 * no Header/PHY bytes
498 * no appended Received CRC bytes
499 * Visual representation of frame format:
500 *
501 * +---------+---------+--------+-----------+--------+
502 * | 2 bytes | N bytes | 1 byte | 4 bytes | 1 byte |
503 * +---------+---------+--------+-----------+--------+
504 * | Length | Payload | RSSI | Timestamp | Status |
505 * +---------+---------+--------+-----------+--------+
506 *
507 * Length bytes equal total length of entire frame excluding itself,
508 * Length = N + RSSI (1) + Timestamp (4) + Status (1)
509 * = N + 6
510 * N = Length - 6
511 */
512 uint8_t *const frame_ptr = (uint8_t *)&data_entry->data;
513 const lensz_t frame_len = *(lensz_t *)frame_ptr;
514
515 /* Sanity check that Frame is at least Frame Shave bytes long */
516 if(frame_len < FRAME_SHAVE) {
517 LOG_ERR("Received frame is too short, len=%d\n", frame_len);
518
519 data_queue_release_entry();
520 return 0;
521 }
522
523 const uint8_t *payload_ptr = frame_ptr + sizeof(lensz_t);
524 const unsigned short payload_len = (unsigned short)(frame_len - FRAME_SHAVE);
525
526 /* Sanity check that Payload fits in Buffer */
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);
530
531 data_queue_release_entry();
532 return 0;
533 }
534
535 memcpy(buf, payload_ptr, payload_len);
536
537 /* RSSI stored after payload */
538 prop_radio.last.rssi = (int8_t)payload_ptr[payload_len];
539 /* LQI calculated from RSSI */
540 prop_radio.last.corr_lqi = calculate_lqi(prop_radio.last.rssi);
541 /* Timestamp stored RSSI (1) bytes after payload. */
542 uint32_t rat_ticks;
543 memcpy(&rat_ticks, payload_ptr + payload_len + 1, 4);
544
545 /* Correct timestamp so that it refers to the end of the SFD */
546 prop_radio.last.timestamp = rat_to_timestamp(rat_ticks, RAT_TIMESTAMP_OFFSET);
547
548 if(!prop_radio.poll_mode) {
549 /* Not in poll mode: packetbuf should not be accessed in interrupt context. */
550 /* In poll mode, the last packet RSSI and link quality can be obtained through */
551 /* RADIO_PARAM_LAST_RSSI and RADIO_PARAM_LAST_LINK_QUALITY */
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);
554 }
555
556 data_queue_release_entry();
557 return (int)payload_len;
558}
559/*---------------------------------------------------------------------------*/
560static uint8_t
561cca_request(void)
562{
563 const int8_t rssi = get_rssi();
564
565 if(rssi == RF_GET_RSSI_ERROR_VAL) {
566 return CCA_STATE_INVALID;
567 }
568
569 return (rssi < prop_radio.rssi_threshold)
570 ? CCA_STATE_IDLE
571 : CCA_STATE_BUSY;
572}
573/*---------------------------------------------------------------------------*/
574static int
575channel_clear(void)
576{
577 if(tx_is_active()) {
578 LOG_ERR("Channel clear called while in TX\n");
579 return 0;
580 }
581
582 const uint8_t cca_state = cca_request();
583
584 /* Channel is clear if CCA state is IDLE */
585 return cca_state == CCA_STATE_IDLE;
586}
587/*---------------------------------------------------------------------------*/
588static int
589receiving_packet(void)
590{
591 if(!prop_radio.rf_is_on) {
592 return 0;
593 }
594
595#if MAC_CONF_WITH_TSCH
596 /*
597 * Under TSCH operation, we rely on "hints" from the MDMSOFT interrupt
598 * flag. This flag is set by the radio upon sync word detection, but it is
599 * not cleared automatically by hardware. We store state in a variable after
600 * first call. The assumption is that the TSCH code will keep calling us
601 * until frame reception has completed, at which point we can clear MDMSOFT.
602 */
603 if(!is_receiving_packet) {
604 /* Look for the modem synchronization word detection interrupt flag.
605 * This flag is raised when the synchronization word is received.
606 */
607 if(HWREG(RFC_DBELL_BASE + RFC_DBELL_O_RFHWIFG) & RFC_DBELL_RFHWIFG_MDMSOFT) {
608 is_receiving_packet = 1;
609 }
610 } else {
611 /* After the start of the packet: reset the Rx flag once the channel gets clear */
612 is_receiving_packet = (cca_request() == CCA_STATE_BUSY);
613 if(!is_receiving_packet) {
614 /* Clear the modem sync flag */
615 RFCHwIntClear(RFC_DBELL_RFHWIFG_MDMSOFT);
616 }
617 }
618
619 return is_receiving_packet;
620#else
621 /*
622 * Under CSMA operation, there is no immediately straightforward logic as to
623 * when it's OK to clear the MDMSOFT interrupt flag:
624 *
625 * - We cannot re-use the same logic as above, since CSMA may bail out of
626 * frame TX immediately after a single call this function here. In this
627 * scenario, is_receiving_packet would remain equal to one and we would
628 * therefore erroneously signal ongoing RX in subsequent calls to this
629 * function here, even _after_ reception has completed.
630 * - We can neither clear inside read_frame() nor inside the RX frame
631 * interrupt handler (remember, we are not in poll mode under CSMA),
632 * since we risk clearing MDMSOFT after we have seen a sync word for the
633 * _next_ frame. If this happens, this function here would incorrectly
634 * return 0 during RX of this next frame.
635 *
636 * So to avoid a very convoluted logic of how to handle MDMSOFT, we simply
637 * perform a clear channel assessment here: We interpret channel activity
638 * as frame reception.
639 */
640
641 if(cca_request() == CCA_STATE_BUSY) {
642 return 1;
643 }
644
645 return 0;
646
647#endif
648}
649/*---------------------------------------------------------------------------*/
650static int
651pending_packet(void)
652{
653 const data_entry_t *const read_entry = data_queue_current_entry();
654 volatile const data_entry_t *curr_entry = read_entry;
655
656 int num_pending = 0;
657
658 /* Go through RX Circular buffer and check their status */
659 do {
660 const uint8_t status = curr_entry->status;
661 if((status == DATA_ENTRY_FINISHED) ||
662 (status == DATA_ENTRY_BUSY)) {
663 num_pending += 1;
664 }
665
666 /* Stop when we have looped the circular buffer */
667 curr_entry = (data_entry_t *)curr_entry->pNextEntry;
668 } while(curr_entry != read_entry);
669
670 if(num_pending > 0 && !prop_radio.poll_mode) {
671 process_poll(&rf_sched_process);
672 }
673
674 /* If we didn't find an entry at status finished, no frames are pending */
675 return num_pending;
676}
677/*---------------------------------------------------------------------------*/
678static int
679on(void)
680{
681 rf_result_t res;
682
683 if(prop_radio.rf_is_on) {
684 LOG_WARN("Radio is already on\n");
685 return RF_RESULT_OK;
686 }
687
688 data_queue_reset();
689
690 res = netstack_sched_rx(true);
691
692 if(res != RF_RESULT_OK) {
693 return RF_RESULT_ERROR;
694 }
695
696 prop_radio.rf_is_on = true;
697 return RF_RESULT_OK;
698}
699/*---------------------------------------------------------------------------*/
700static int
701off(void)
702{
703 if(!prop_radio.rf_is_on) {
704 LOG_WARN("Radio is already off\n");
705 return RF_RESULT_OK;
706 }
707
708 rf_yield();
709
710 prop_radio.rf_is_on = false;
711 return RF_RESULT_OK;
712}
713/*---------------------------------------------------------------------------*/
714static radio_result_t
715get_value(radio_param_t param, radio_value_t *value)
716{
717 rf_result_t res;
718
719 if(!value) {
721 }
722
723 switch(param) {
725 /* On / off */
726 *value = (prop_radio.rf_is_on)
729
730 return RADIO_RESULT_OK;
731
733 *value = (radio_value_t)get_channel();
734 return RADIO_RESULT_OK;
735
736 /* RX mode */
738 *value = 0;
739 if(prop_radio.poll_mode) {
741 }
742 return RADIO_RESULT_OK;
743
744 /* TX mode */
746 *value = 0;
747 return RADIO_RESULT_OK;
748
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))
755
757 *value = prop_radio.rssi_threshold;
758 return RADIO_RESULT_OK;
759
760 case RADIO_PARAM_RSSI:
761 *value = get_rssi();
762 return (*value == RF_GET_RSSI_ERROR_VAL)
765
767 *value = prop_radio.last.rssi;
768 return RADIO_RESULT_OK;
769
771 *value = prop_radio.last.corr_lqi;
772 return RADIO_RESULT_OK;
773
775 *value = DOT_15_4G_CHAN_MIN;
776 return RADIO_RESULT_OK;
777
779 *value = DOT_15_4G_CHAN_MAX;
780 return RADIO_RESULT_OK;
781
783 *value = (radio_value_t)tx_power_min(rf_tx_power_table);
784 return RADIO_RESULT_OK;
785
787 *value = (radio_value_t)tx_power_max(rf_tx_power_table, rf_tx_power_table_size);
788 return RADIO_RESULT_OK;
789
790 case RADIO_CONST_MAX_PAYLOAD_LEN:
791 *value = (radio_value_t)MAX_PAYLOAD_LEN;
792 return RADIO_RESULT_OK;
793
794 default:
796 }
797}
798/*---------------------------------------------------------------------------*/
799static radio_result_t
800set_value(radio_param_t param, radio_value_t value)
801{
802 rf_result_t res;
803
804 switch(param) {
806
807 if(value == RADIO_POWER_MODE_ON) {
808 return (on() == RF_RESULT_OK)
811 } else if(value == RADIO_POWER_MODE_OFF) {
812 off();
813 return RADIO_RESULT_OK;
814 }
815
817
819 res = set_channel((uint16_t)value);
820 return (res == RF_RESULT_OK)
823
825 if(!tx_power_in_range((int8_t)value, rf_tx_power_table, rf_tx_power_table_size)) {
827 }
828 res = rf_set_tx_power(prop_radio.rf_handle, rf_tx_power_table, (int8_t)value);
829 return (res == RF_RESULT_OK)
832
833 /* RX Mode */
835 if(value & ~(RADIO_RX_MODE_POLL_MODE)) {
837 }
838
839 const bool old_poll_mode = prop_radio.poll_mode;
840 prop_radio.poll_mode = (value & RADIO_RX_MODE_POLL_MODE) != 0;
841 if(old_poll_mode == prop_radio.poll_mode) {
842 return RADIO_RESULT_OK;
843 }
844 if(!prop_radio.rf_is_on) {
845 return RADIO_RESULT_OK;
846 }
847
848 netstack_stop_rx();
849 res = netstack_sched_rx(false);
850 return (res == RF_RESULT_OK)
853
854 /* TX Mode */
856 if(value & ~(RADIO_TX_MODE_SEND_ON_CCA)) {
858 }
859 set_send_on_cca((value & RADIO_TX_MODE_SEND_ON_CCA) != 0);
860 return RADIO_RESULT_OK;
861
863 prop_radio.rssi_threshold = (int8_t)value;
864 return RADIO_RESULT_OK;
865
866 default:
868 }
869}
870/*---------------------------------------------------------------------------*/
871static radio_result_t
872get_object(radio_param_t param, void *dest, size_t size)
873{
874 if(!dest) {
876 }
877
878 switch(param) {
879 /* Last packet timestamp */
881 if(size != sizeof(rtimer_clock_t)) {
883 }
884
885 *(rtimer_clock_t *)dest = prop_radio.last.timestamp;
886
887 return RADIO_RESULT_OK;
888
889 default:
891 }
892}
893/*---------------------------------------------------------------------------*/
894static radio_result_t
895set_object(radio_param_t param, const void *src, size_t size)
896{
898}
899/*---------------------------------------------------------------------------*/
900static int
901init(void)
902{
903 RF_Params rf_params;
904 RF_TxPowerTable_Value tx_power_value;
905 RF_Stat rf_stat;
906
907 prop_radio.rx_is_active = rx_is_active;
908
909 radio_mode = (simplelink_radio_mode_t *)&prop_radio;
910
911 if(prop_radio.rf_handle) {
912 LOG_WARN("Radio is already initialized\n");
913 return RF_RESULT_OK;
914 }
915
916 /* RX is off */
917 prop_radio.rf_is_on = false;
918
919 /* Set configured RSSI threshold */
920 prop_radio.rssi_threshold = PROP_MODE_CCA_RSSI_THRESHOLD;
921
922 init_rf_params();
923
924 /* Init RF params and specify non-default params */
925 RF_Params_init(&rf_params);
926 rf_params.nInactivityTimeout = RF_CONF_INACTIVITY_TIMEOUT;
927
928 /* Open RF Driver */
929 prop_radio.rf_handle = netstack_open(&rf_params);
930
931 if(prop_radio.rf_handle == NULL) {
932 LOG_ERR("Unable to open RF driver during initialization\n");
933 return RF_RESULT_ERROR;
934 }
935
936 set_channel_force(IEEE802154_DEFAULT_CHANNEL);
937
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);
943 } else {
944 LOG_WARN("Setting TX power to %d dBm failed, stat=0x%02X", RF_TXPOWER_DBM, rf_stat);
945 }
946 } else {
947 LOG_WARN("Unable to find TX power %d dBm in the TX power table\n", RF_TXPOWER_DBM);
948 }
949
950 ENERGEST_ON(ENERGEST_TYPE_LISTEN);
951
952 /* Start RAT overflow upkeep */
953 rat_init();
954
955 /* Start RF process */
956 process_start(&rf_sched_process, NULL);
957
958 return RF_RESULT_OK;
959}
960/*---------------------------------------------------------------------------*/
961const struct radio_driver prop_mode_driver = {
962 init,
963 prepare,
964 transmit,
965 send,
966 read,
970 on,
971 off,
972 get_value,
973 set_value,
976};
977/*---------------------------------------------------------------------------*/
978/**
979 * @}
980 * @}
981 */
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)
Definition prop-mode.c:170
static int read(void *buf, unsigned short buf_len)
Definition prop-mode.c:473
void process_start(struct process *p, process_data_t data)
Start a process.
Definition process.c:107
void process_poll(struct process *p)
Request a process to be polled.
Definition process.c:375
#define RADIO_RX_MODE_POLL_MODE
Enable/disable/get the state of radio driver poll mode operation.
Definition radio.h:461
#define RADIO_TX_MODE_SEND_ON_CCA
Radio TX mode control / retrieval.
Definition radio.h:474
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.
Definition radio.h:88
@ RADIO_RESULT_ERROR
An error occurred when getting/setting the parameter, but the arguments were otherwise correct.
Definition radio.h:488
@ RADIO_RESULT_NOT_SUPPORTED
The parameter is not supported.
Definition radio.h:481
@ RADIO_RESULT_INVALID_VALUE
The value argument was incorrect.
Definition radio.h:482
@ RADIO_RESULT_OK
The parameter was set/read successfully.
Definition radio.h:480
@ RADIO_PARAM_POWER_MODE
When getting the value of this parameter, the radio driver should indicate whether the radio is on or...
Definition radio.h:119
@ RADIO_PARAM_RSSI
Received signal strength indicator in dBm.
Definition radio.h:218
@ RADIO_PARAM_LAST_PACKET_TIMESTAMP
Last packet timestamp, of type rtimer_clock_t.
Definition radio.h:286
@ RADIO_PARAM_LAST_RSSI
The RSSI value of the last received packet.
Definition radio.h:226
@ RADIO_PARAM_RX_MODE
Radio receiver mode determines if the radio has address filter (RADIO_RX_MODE_ADDRESS_FILTER) and aut...
Definition radio.h:173
@ RADIO_PARAM_CHANNEL
Channel used for radio communication.
Definition radio.h:134
@ RADIO_PARAM_LAST_LINK_QUALITY
Link quality indicator of the last received packet.
Definition radio.h:244
@ RADIO_PARAM_TXPOWER
Transmission power in dBm.
Definition radio.h:192
@ RADIO_CONST_CHANNEL_MAX
The highest radio channel number.
Definition radio.h:316
@ RADIO_PARAM_CCA_THRESHOLD
Clear channel assessment threshold in dBm.
Definition radio.h:205
@ RADIO_CONST_TXPOWER_MIN
The minimum transmission power in dBm.
Definition radio.h:321
@ RADIO_CONST_CHANNEL_MIN
The lowest radio channel number.
Definition radio.h:311
@ RADIO_CONST_TXPOWER_MAX
The maximum transmission power in dBm.
Definition radio.h:326
@ RADIO_PARAM_TX_MODE
Radio transmission mode determines if the radio has send on CCA (RADIO_TX_MODE_SEND_ON_CCA) enabled o...
Definition radio.h:180
@ RADIO_POWER_MODE_OFF
Radio powered off and in the lowest possible power consumption state.
Definition radio.h:395
@ RADIO_POWER_MODE_ON
Radio powered on and able to receive frames.
Definition radio.h:400
@ RADIO_TX_COLLISION
TX failed due to a collision.
Definition radio.h:511
@ RADIO_TX_ERR
An error occurred during transmission.
Definition radio.h:506
@ RADIO_TX_OK
TX was successful and where an ACK was requested one was received.
Definition radio.h:498
#define RTIMER_BUSYWAIT_UNTIL(cond, max_time)
Busy-wait until a condition for at most max_time.
Definition rtimer.h:213
#define RTIMER_NOW()
Get the current clock time.
Definition rtimer.h:187
Header file for the logging system.
#define IEEE802154_DEFAULT_CHANNEL
The default channel for IEEE 802.15.4 networks.
Definition mac.h:52
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.
The structure of a Contiki-NG radio device driver.
Definition radio.h:534
radio_result_t(* get_object)(radio_param_t param, void *dest, size_t size)
Get a radio parameter object.
Definition radio.h:770
int(* prepare)(const void *payload, unsigned short payload_len)
Prepare the radio with a packet to be sent.
Definition radio.h:580
radio_result_t(* set_value)(radio_param_t param, radio_value_t value)
Set a radio parameter value.
Definition radio.h:756
int(* off)(void)
Turn the radio off.
Definition radio.h:729
int(* init)(void)
Initialise the radio hardware.
Definition radio.h:555
int(* send)(const void *payload, unsigned short payload_len)
Prepare & transmit a packet.
Definition radio.h:631
int(* receiving_packet)(void)
Check if the radio driver is currently receiving a packet.
Definition radio.h:684
radio_result_t(* set_object)(radio_param_t param, const void *src, size_t size)
Set a radio parameter object.
Definition radio.h:787
int(* on)(void)
Turn the radio on.
Definition radio.h:711
int(* transmit)(unsigned short transmit_len)
Send the packet that has previously been prepared.
Definition radio.h:619
int(* pending_packet)(void)
Check if a packet has been received and is available in the radio driver's buffers.
Definition radio.h:697
radio_result_t(* get_value)(radio_param_t param, radio_value_t *value)
Get a radio parameter value.
Definition radio.h:741
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:672
Header file of TX power functionality of CC13xx/CC26xx.