Contiki-NG
ieee-mode.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2014, 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 /**
32  * \addtogroup rf-core
33  * @{
34  *
35  * \defgroup rf-core-ieee CC13xx/CC26xx IEEE mode driver
36  *
37  * @{
38  *
39  * \file
40  * Implementation of the CC13xx/CC26xx IEEE mode NETSTACK_RADIO driver
41  */
42 /*---------------------------------------------------------------------------*/
43 #include "contiki.h"
44 #include "dev/radio.h"
45 #include "dev/cc26xx-uart.h"
46 #include "dev/oscillators.h"
47 #include "net/packetbuf.h"
48 #include "net/linkaddr.h"
49 #include "net/netstack.h"
50 #include "sys/energest.h"
51 #include "sys/clock.h"
52 #include "sys/rtimer.h"
53 #include "sys/ctimer.h"
54 #include "sys/cc.h"
55 #include "lpm.h"
56 #include "ti-lib.h"
57 #include "rf-core/rf-core.h"
58 #include "rf-core/rf-switch.h"
59 #include "rf-core/rf-ble.h"
60 /*---------------------------------------------------------------------------*/
61 /* RF core and RF HAL API */
62 #include "hw_rfc_dbell.h"
63 #include "hw_rfc_pwr.h"
64 /*---------------------------------------------------------------------------*/
65 /* RF Core Mailbox API */
66 #include "rf-core/api/ieee_cmd.h"
67 #include "rf-core/api/ieee_mailbox.h"
68 #include "driverlib/rf_mailbox.h"
69 #include "driverlib/rf_common_cmd.h"
70 #include "driverlib/rf_data_entry.h"
71 /*---------------------------------------------------------------------------*/
72 #include "smartrf-settings.h"
73 /*---------------------------------------------------------------------------*/
74 #include <stdint.h>
75 #include <string.h>
76 #include <stdio.h>
77 #include <stdbool.h>
78 /*---------------------------------------------------------------------------*/
79 #define DEBUG 0
80 #if DEBUG
81 #define PRINTF(...) printf(__VA_ARGS__)
82 #else
83 #define PRINTF(...)
84 #endif
85 
86 /* Configuration to enable/disable auto ACKs in IEEE mode */
87 #ifdef IEEE_MODE_CONF_AUTOACK
88 #define IEEE_MODE_AUTOACK IEEE_MODE_CONF_AUTOACK
89 #else
90 #define IEEE_MODE_AUTOACK 1
91 #endif /* IEEE_MODE_CONF_AUTOACK */
92 
93 /* Configuration to enable/disable frame filtering in IEEE mode */
94 #ifdef IEEE_MODE_CONF_PROMISCOUS
95 #define IEEE_MODE_PROMISCOUS IEEE_MODE_CONF_PROMISCOUS
96 #else
97 #define IEEE_MODE_PROMISCOUS 0
98 #endif /* IEEE_MODE_CONF_PROMISCOUS */
99 
100 #ifdef IEEE_MODE_CONF_RSSI_THRESHOLD
101 #define IEEE_MODE_RSSI_THRESHOLD IEEE_MODE_CONF_RSSI_THRESHOLD
102 #else
103 #define IEEE_MODE_RSSI_THRESHOLD 0xA6
104 #endif /* IEEE_MODE_CONF_RSSI_THRESHOLD */
105 /*---------------------------------------------------------------------------*/
106 #define STATUS_CRC_FAIL 0x80 /* bit 7 */
107 #define STATUS_REJECT_FRAME 0x40 /* bit 6 */
108 #define STATUS_CORRELATION 0x3f /* bits 0-5 */
109 /*---------------------------------------------------------------------------*/
110 /* Data entry status field constants */
111 #define DATA_ENTRY_STATUS_PENDING 0x00 /* Not in use by the Radio CPU */
112 #define DATA_ENTRY_STATUS_ACTIVE 0x01 /* Open for r/w by the radio CPU */
113 #define DATA_ENTRY_STATUS_BUSY 0x02 /* Ongoing r/w */
114 #define DATA_ENTRY_STATUS_FINISHED 0x03 /* Free to use and to free */
115 #define DATA_ENTRY_STATUS_UNFINISHED 0x04 /* Partial RX entry */
116 /*---------------------------------------------------------------------------*/
117 /* RF stats data structure */
118 static uint8_t rf_stats[16] = { 0 };
119 /*---------------------------------------------------------------------------*/
120 /* The size of the RF commands buffer */
121 #define RF_CMD_BUFFER_SIZE 128
122 /*---------------------------------------------------------------------------*/
123 #define RAT_TIMESTAMP_OFFSET_2_4_GHZ 0
124 /*---------------------------------------------------------------------------*/
125 /**
126  * \brief Returns the current status of a running Radio Op command
127  * \param a A pointer with the buffer used to initiate the command
128  * \return The value of the Radio Op buffer's status field
129  *
130  * This macro can be used to e.g. return the status of a previously
131  * initiated background operation, or of an immediate command
132  */
133 #define RF_RADIO_OP_GET_STATUS(a) (((rfc_radioOp_t *)a)->status)
134 /*---------------------------------------------------------------------------*/
135 #define IEEE_MODE_CHANNEL_MIN 11
136 #define IEEE_MODE_CHANNEL_MAX 26
137 /*---------------------------------------------------------------------------*/
138 /* TX Power dBm lookup table - values from SmartRF Studio */
139 typedef struct output_config {
140  radio_value_t dbm;
141  uint16_t tx_power; /* Value for the CMD_RADIO_SETUP.txPower field */
142 } output_config_t;
143 
144 static const output_config_t output_power[] = {
145  { 5, 0x9330 },
146  { 4, 0x9324 },
147  { 3, 0x5a1c },
148  { 2, 0x4e18 },
149  { 1, 0x4214 },
150  { 0, 0x3161 },
151  { -3, 0x2558 },
152  { -6, 0x1d52 },
153  { -9, 0x194e },
154  {-12, 0x144b },
155  {-15, 0x0ccb },
156  {-18, 0x0cc9 },
157  {-21, 0x0cc7 },
158 };
159 
160 #define OUTPUT_CONFIG_COUNT (sizeof(output_power) / sizeof(output_config_t))
161 
162 /* Max and Min Output Power in dBm */
163 #define OUTPUT_POWER_MIN (output_power[OUTPUT_CONFIG_COUNT - 1].dbm)
164 #define OUTPUT_POWER_MAX (output_power[0].dbm)
165 #define OUTPUT_POWER_UNKNOWN 0xFFFF
166 
167 /* Default TX Power - position in output_power[] */
168 static const output_config_t *tx_power_current = &output_power[0];
169 /*---------------------------------------------------------------------------*/
170 static rfc_CMD_IEEE_MOD_FILT_t filter_cmd;
171 /*---------------------------------------------------------------------------*/
172 /*
173  * Buffers used to send commands to the RF core (generic and IEEE commands).
174  * Some of those buffers are re-usable, some are not.
175  *
176  * If you are uncertain, declare a new buffer.
177  */
178 /*
179  * A buffer to send a CMD_IEEE_RX and to subsequently monitor its status
180  * Do not use this buffer for any commands other than CMD_IEEE_RX
181  */
182 static uint8_t cmd_ieee_rx_buf[RF_CMD_BUFFER_SIZE] CC_ALIGN(4);
183 /*---------------------------------------------------------------------------*/
184 #define DATA_ENTRY_LENSZ_NONE 0
185 #define DATA_ENTRY_LENSZ_BYTE 1
186 #define DATA_ENTRY_LENSZ_WORD 2 /* 2 bytes */
187 
188 /* The size of the metadata (excluding the packet length field) */
189 #define RX_BUF_METADATA_SIZE \
190  (2 * RF_CORE_RX_BUF_INCLUDE_CRC \
191  + RF_CORE_RX_BUF_INCLUDE_RSSI \
192  + RF_CORE_RX_BUF_INCLUDE_CORR \
193  + 4 * RF_CORE_RX_BUF_INCLUDE_TIMESTAMP)
194 
195 /* The offset of the packet length in a rx buffer */
196 #define RX_BUF_LENGTH_OFFSET sizeof(rfc_dataEntry_t)
197 /* The offset of the packet data in a rx buffer */
198 #define RX_BUF_DATA_OFFSET (RX_BUF_LENGTH_OFFSET + 1)
199 
200 #define RX_BUF_SIZE (RX_BUF_DATA_OFFSET \
201  + NETSTACK_RADIO_MAX_PAYLOAD_LEN \
202  + RX_BUF_METADATA_SIZE)
203 
204 /* Four receive buffers entries with room for 1 IEEE802.15.4 frame in each */
205 static uint8_t rx_buf_0[RX_BUF_SIZE] CC_ALIGN(4);
206 static uint8_t rx_buf_1[RX_BUF_SIZE] CC_ALIGN(4);
207 static uint8_t rx_buf_2[RX_BUF_SIZE] CC_ALIGN(4);
208 static uint8_t rx_buf_3[RX_BUF_SIZE] CC_ALIGN(4);
209 
210 /* The RX Data Queue */
211 static dataQueue_t rx_data_queue = { 0 };
212 
213 /* Receive entry pointer to keep track of read items */
214 volatile static uint8_t *rx_read_entry;
215 /*---------------------------------------------------------------------------*/
216 /* The outgoing frame buffer */
217 #define TX_BUF_PAYLOAD_LEN 180
218 #define TX_BUF_HDR_LEN 2
219 
220 static uint8_t tx_buf[TX_BUF_HDR_LEN + TX_BUF_PAYLOAD_LEN] CC_ALIGN(4);
221 /*---------------------------------------------------------------------------*/
222 #ifdef IEEE_MODE_CONF_BOARD_OVERRIDES
223 #define IEEE_MODE_BOARD_OVERRIDES IEEE_MODE_CONF_BOARD_OVERRIDES
224 #else
225 #define IEEE_MODE_BOARD_OVERRIDES
226 #endif
227 /*---------------------------------------------------------------------------*/
228 /* Overrides for IEEE 802.15.4, differential mode */
229 static uint32_t ieee_overrides[] = {
230  0x00354038, /* Synth: Set RTRIM (POTAILRESTRIM) to 5 */
231  0x4001402D, /* Synth: Correct CKVD latency setting (address) */
232  0x00608402, /* Synth: Correct CKVD latency setting (value) */
233 // 0x4001405D, /* Synth: Set ANADIV DIV_BIAS_MODE to PG1 (address) */
234 // 0x1801F800, /* Synth: Set ANADIV DIV_BIAS_MODE to PG1 (value) */
235  0x000784A3, /* Synth: Set FREF = 3.43 MHz (24 MHz / 7) */
236  0xA47E0583, /* Synth: Set loop bandwidth after lock to 80 kHz (K2) */
237  0xEAE00603, /* Synth: Set loop bandwidth after lock to 80 kHz (K3, LSB) */
238  0x00010623, /* Synth: Set loop bandwidth after lock to 80 kHz (K3, MSB) */
239  0x002B50DC, /* Adjust AGC DC filter */
240  0x05000243, /* Increase synth programming timeout */
241  0x002082C3, /* Increase synth programming timeout */
242  IEEE_MODE_BOARD_OVERRIDES
243  0xFFFFFFFF, /* End of override list */
244 };
245 /*---------------------------------------------------------------------------*/
246 static int on(void);
247 static int off(void);
248 /*---------------------------------------------------------------------------*/
249 /**
250  * \brief Checks whether the RFC domain is accessible and the RFC is in IEEE RX
251  * \return 1: RFC in RX mode (and therefore accessible too). 0 otherwise
252  */
253 static uint8_t
254 rf_is_on(void)
255 {
256  if(!rf_core_is_accessible()) {
257  return 0;
258  }
259 
260  return RF_RADIO_OP_GET_STATUS(cmd_ieee_rx_buf) == RF_CORE_RADIO_OP_STATUS_ACTIVE;
261 }
262 /*---------------------------------------------------------------------------*/
263 /**
264  * \brief Check the RF's TX status
265  * \return 1 RF is transmitting
266  * \return 0 RF is not transmitting
267  *
268  * TX mode may be triggered either by a CMD_IEEE_TX or by the automatic
269  * transmission of an ACK frame.
270  */
271 static uint8_t
273 {
274  uint32_t cmd_status;
275  rfc_CMD_IEEE_CCA_REQ_t cmd;
276 
277  /* If we are off, we are not in TX */
278  if(!rf_core_is_accessible()) {
279  return 0;
280  }
281 
282  memset(&cmd, 0x00, sizeof(cmd));
283 
284  cmd.commandNo = CMD_IEEE_CCA_REQ;
285 
286  if(rf_core_send_cmd((uint32_t)&cmd, &cmd_status) == RF_CORE_CMD_ERROR) {
287  PRINTF("transmitting: CMDSTA=0x%08lx\n", cmd_status);
288  return 0;
289  }
290 
291  if((cmd.currentRssi == RF_CORE_CMD_CCA_REQ_RSSI_UNKNOWN) &&
292  (cmd.ccaInfo.ccaEnergy == RF_CORE_CMD_CCA_REQ_CCA_STATE_BUSY)) {
293  return 1;
294  }
295 
296  return 0;
297 }
298 /*---------------------------------------------------------------------------*/
299 /**
300  * \brief Returns CCA information
301  * \return RF_CORE_GET_CCA_INFO_ERROR if the RF was not on
302  * \return On success, the return value is formatted as per the ccaInfo field
303  * of CMD_IEEE_CCA_REQ
304  *
305  * It is the caller's responsibility to make sure the RF is on. This function
306  * will return RF_CORE_GET_CCA_INFO_ERROR if the RF is off
307  *
308  * This function will in fact wait for a valid CCA state
309  */
310 static uint8_t
312 {
313  uint32_t cmd_status;
314  rfc_CMD_IEEE_CCA_REQ_t cmd;
315 
316  if(!rf_is_on()) {
317  PRINTF("get_cca_info: Not on\n");
318  return RF_CORE_GET_CCA_INFO_ERROR;
319  }
320 
321  memset(&cmd, 0x00, sizeof(cmd));
322  cmd.ccaInfo.ccaState = RF_CORE_CMD_CCA_REQ_CCA_STATE_INVALID;
323 
324  while(cmd.ccaInfo.ccaState == RF_CORE_CMD_CCA_REQ_CCA_STATE_INVALID) {
325  memset(&cmd, 0x00, sizeof(cmd));
326  cmd.commandNo = CMD_IEEE_CCA_REQ;
327 
328  if(rf_core_send_cmd((uint32_t)&cmd, &cmd_status) == RF_CORE_CMD_ERROR) {
329  PRINTF("get_cca_info: CMDSTA=0x%08lx\n", cmd_status);
330 
331  return RF_CORE_GET_CCA_INFO_ERROR;
332  }
333  }
334 
335  /* We have a valid CCA state. Return the CCA Info */
336  return *((uint8_t *)&cmd.ccaInfo);
337 }
338 /*---------------------------------------------------------------------------*/
339 /**
340  * \brief Reads the current signal strength (RSSI)
341  * \return The current RSSI in dBm or CMD_GET_RSSI_UNKNOWN
342  *
343  * This function reads the current RSSI on the currently configured
344  * channel.
345  */
346 static radio_value_t
347 get_rssi(void)
348 {
349  uint32_t cmd_status;
350  uint8_t was_off = 0;
351  rfc_CMD_IEEE_CCA_REQ_t cmd;
352 
353  /* If we are off, turn on first */
354  if(!rf_is_on()) {
355  was_off = 1;
356  if(on() != RF_CORE_CMD_OK) {
357  PRINTF("get_rssi: on() failed\n");
358  return RF_CORE_CMD_CCA_REQ_RSSI_UNKNOWN;
359  }
360  }
361 
362  memset(&cmd, 0x00, sizeof(cmd));
363  cmd.ccaInfo.ccaEnergy = RF_CORE_CMD_CCA_REQ_CCA_STATE_INVALID;
364 
365  while(cmd.ccaInfo.ccaEnergy == RF_CORE_CMD_CCA_REQ_CCA_STATE_INVALID) {
366  memset(&cmd, 0x00, sizeof(cmd));
367  cmd.commandNo = CMD_IEEE_CCA_REQ;
368 
369  if(rf_core_send_cmd((uint32_t)&cmd, &cmd_status) == RF_CORE_CMD_ERROR) {
370  PRINTF("get_rssi: CMDSTA=0x%08lx\n", cmd_status);
371 
372  /* Make sure to return RSSI unknown */
373  cmd.currentRssi = RF_CORE_CMD_CCA_REQ_RSSI_UNKNOWN;
374  break;
375  }
376  }
377 
378  /* If we were off, turn back off */
379  if(was_off) {
380  off();
381  }
382 
383  return cmd.currentRssi;
384 }
385 /*---------------------------------------------------------------------------*/
386 /* Returns the current TX power in dBm */
387 static radio_value_t
388 get_tx_power(void)
389 {
390  return tx_power_current->dbm;
391 }
392 /*---------------------------------------------------------------------------*/
393 /*
394  * Set TX power to 'at least' power dBm
395  * This works with a lookup table. If the value of 'power' does not exist in
396  * the lookup table, TXPOWER will be set to the immediately higher available
397  * value
398  */
399 static void
400 set_tx_power(radio_value_t power)
401 {
402  uint32_t cmd_status;
403  int i;
404  rfc_CMD_SET_TX_POWER_t cmd;
405 
406  /* First, find the correct setting and save it */
407  for(i = OUTPUT_CONFIG_COUNT - 1; i >= 0; --i) {
408  if(power <= output_power[i].dbm) {
409  tx_power_current = &output_power[i];
410  break;
411  }
412  }
413 
414  /*
415  * If the core is not accessible, the new setting will be applied next
416  * time we send CMD_RADIO_SETUP, so we don't need to do anything further.
417  * If the core is accessible, we can apply the new setting immediately with
418  * CMD_SET_TX_POWER
419  */
420  if(rf_core_is_accessible() == RF_CORE_NOT_ACCESSIBLE) {
421  return;
422  }
423 
424  memset(&cmd, 0x00, sizeof(cmd));
425  cmd.commandNo = CMD_SET_TX_POWER;
426  cmd.txPower = output_power[i].tx_power;
427 
428  if(rf_core_send_cmd((uint32_t)&cmd, &cmd_status) == RF_CORE_CMD_ERROR) {
429  PRINTF("set_tx_power: CMDSTA=0x%08lx\n", cmd_status);
430  }
431 }
432 /*---------------------------------------------------------------------------*/
433 static uint8_t
434 rf_radio_setup()
435 {
436  uint32_t cmd_status;
437  rfc_CMD_RADIO_SETUP_t cmd;
438 
439  rf_switch_select_path(RF_SWITCH_PATH_2_4GHZ);
440 
441  /* Create radio setup command */
442  rf_core_init_radio_op((rfc_radioOp_t *)&cmd, sizeof(cmd), CMD_RADIO_SETUP);
443 
444  cmd.txPower = tx_power_current->tx_power;
445  cmd.pRegOverride = ieee_overrides;
446  cmd.config.frontEndMode = RF_CORE_RADIO_SETUP_FRONT_END_MODE;
447  cmd.config.biasMode = RF_CORE_RADIO_SETUP_BIAS_MODE;
448  cmd.mode = 1;
449 
450  /* Send Radio setup to RF Core */
451  if(rf_core_send_cmd((uint32_t)&cmd, &cmd_status) != RF_CORE_CMD_OK) {
452  PRINTF("rf_radio_setup: CMD_RADIO_SETUP, CMDSTA=0x%08lx, status=0x%04x\n",
453  cmd_status, cmd.status);
454  return RF_CORE_CMD_ERROR;
455  }
456 
457  /* Wait until radio setup is done */
458  if(rf_core_wait_cmd_done(&cmd) != RF_CORE_CMD_OK) {
459  PRINTF("rf_radio_setup: CMD_RADIO_SETUP wait, CMDSTA=0x%08lx, status=0x%04x\n",
460  cmd_status, cmd.status);
461  return RF_CORE_CMD_ERROR;
462  }
463 
464  return RF_CORE_CMD_OK;
465 }
466 /*---------------------------------------------------------------------------*/
467 /**
468  * \brief Set up radio in IEEE802.15.4 RX mode
469  *
470  * \return RF_CORE_CMD_OK Succeeded
471  * \return RF_CORE_CMD_ERROR Failed
472  *
473  * This function assumes that cmd_ieee_rx_buf has been previously populated
474  * with correct values. This can be done through init_rf_params (sets defaults)
475  * or through Contiki's extended RF API (set_value, set_object)
476  */
477 static uint8_t
479 {
480  uint32_t cmd_status;
481  int ret;
482 
483  ret = rf_core_send_cmd((uint32_t)cmd_ieee_rx_buf, &cmd_status);
484 
485  if(ret != RF_CORE_CMD_OK) {
486  PRINTF("rf_cmd_ieee_rx: ret=%d, CMDSTA=0x%08lx, status=0x%04x\n",
487  ret, cmd_status, RF_RADIO_OP_GET_STATUS(cmd_ieee_rx_buf));
488  return RF_CORE_CMD_ERROR;
489  }
490 
491  RTIMER_BUSYWAIT_UNTIL(RF_RADIO_OP_GET_STATUS(cmd_ieee_rx_buf) == RF_CORE_RADIO_OP_STATUS_ACTIVE,
492  RF_CORE_ENTER_RX_TIMEOUT);
493 
494  /* Wait to enter RX */
495  if(RF_RADIO_OP_GET_STATUS(cmd_ieee_rx_buf) != RF_CORE_RADIO_OP_STATUS_ACTIVE) {
496  PRINTF("rf_cmd_ieee_rx: CMDSTA=0x%08lx, status=0x%04x\n",
497  cmd_status, RF_RADIO_OP_GET_STATUS(cmd_ieee_rx_buf));
498  return RF_CORE_CMD_ERROR;
499  }
500 
501  return ret;
502 }
503 /*---------------------------------------------------------------------------*/
504 static void
505 init_rx_buffers(void)
506 {
507  rfc_dataEntry_t *entry;
508 
509  entry = (rfc_dataEntry_t *)rx_buf_0;
510  entry->pNextEntry = rx_buf_1;
511  entry->config.lenSz = DATA_ENTRY_LENSZ_BYTE;
512  entry->length = sizeof(rx_buf_0) - sizeof(*entry);
513 
514  entry = (rfc_dataEntry_t *)rx_buf_1;
515  entry->pNextEntry = rx_buf_2;
516  entry->config.lenSz = DATA_ENTRY_LENSZ_BYTE;
517  entry->length = sizeof(rx_buf_0) - sizeof(*entry);
518 
519  entry = (rfc_dataEntry_t *)rx_buf_2;
520  entry->pNextEntry = rx_buf_3;
521  entry->config.lenSz = DATA_ENTRY_LENSZ_BYTE;
522  entry->length = sizeof(rx_buf_0) - sizeof(*entry);
523 
524  entry = (rfc_dataEntry_t *)rx_buf_3;
525  entry->pNextEntry = rx_buf_0;
526  entry->config.lenSz = DATA_ENTRY_LENSZ_BYTE;
527  entry->length = sizeof(rx_buf_0) - sizeof(*entry);
528 }
529 /*---------------------------------------------------------------------------*/
530 static void
531 init_rf_params(void)
532 {
533  rfc_CMD_IEEE_RX_t *cmd = (rfc_CMD_IEEE_RX_t *)cmd_ieee_rx_buf;
534 
535  memset(cmd_ieee_rx_buf, 0x00, RF_CMD_BUFFER_SIZE);
536 
537  cmd->commandNo = CMD_IEEE_RX;
538  cmd->status = RF_CORE_RADIO_OP_STATUS_IDLE;
539  cmd->pNextOp = NULL;
540  cmd->startTime = 0x00000000;
541  cmd->startTrigger.triggerType = TRIG_NOW;
542  cmd->condition.rule = COND_NEVER;
543  cmd->channel = IEEE802154_DEFAULT_CHANNEL;
544 
545  cmd->rxConfig.bAutoFlushCrc = 1;
546  cmd->rxConfig.bAutoFlushIgn = 0;
547  cmd->rxConfig.bIncludePhyHdr = 0;
548  cmd->rxConfig.bIncludeCrc = RF_CORE_RX_BUF_INCLUDE_CRC;
549  cmd->rxConfig.bAppendRssi = RF_CORE_RX_BUF_INCLUDE_RSSI;
550  cmd->rxConfig.bAppendCorrCrc = RF_CORE_RX_BUF_INCLUDE_CORR;
551  cmd->rxConfig.bAppendSrcInd = 0;
552  cmd->rxConfig.bAppendTimestamp = RF_CORE_RX_BUF_INCLUDE_TIMESTAMP;
553 
554  cmd->pRxQ = &rx_data_queue;
555  cmd->pOutput = (rfc_ieeeRxOutput_t *)rf_stats;
556 
557 #if IEEE_MODE_PROMISCOUS
558  cmd->frameFiltOpt.frameFiltEn = 0;
559 #else
560  cmd->frameFiltOpt.frameFiltEn = 1;
561 #endif
562 
563  cmd->frameFiltOpt.frameFiltStop = 1;
564 
565 #if IEEE_MODE_AUTOACK
566  cmd->frameFiltOpt.autoAckEn = 1;
567 #else
568  cmd->frameFiltOpt.autoAckEn = 0;
569 #endif
570 
571  cmd->frameFiltOpt.slottedAckEn = 0;
572  cmd->frameFiltOpt.autoPendEn = 0;
573  cmd->frameFiltOpt.defaultPend = 0;
574  cmd->frameFiltOpt.bPendDataReqOnly = 0;
575  cmd->frameFiltOpt.bPanCoord = 0;
576  cmd->frameFiltOpt.maxFrameVersion = 2;
577  cmd->frameFiltOpt.bStrictLenFilter = 0;
578 
579  /* Receive all frame types */
580  cmd->frameTypes.bAcceptFt0Beacon = 1;
581  cmd->frameTypes.bAcceptFt1Data = 1;
582  cmd->frameTypes.bAcceptFt2Ack = 1;
583  cmd->frameTypes.bAcceptFt3MacCmd = 1;
584  cmd->frameTypes.bAcceptFt4Reserved = 1;
585  cmd->frameTypes.bAcceptFt5Reserved = 1;
586  cmd->frameTypes.bAcceptFt6Reserved = 1;
587  cmd->frameTypes.bAcceptFt7Reserved = 1;
588 
589  /* Configure CCA settings */
590  cmd->ccaOpt.ccaEnEnergy = 1;
591  cmd->ccaOpt.ccaEnCorr = 1;
592  cmd->ccaOpt.ccaEnSync = 1;
593  cmd->ccaOpt.ccaCorrOp = 1;
594  cmd->ccaOpt.ccaSyncOp = 0;
595  cmd->ccaOpt.ccaCorrThr = 3;
596 
597  cmd->ccaRssiThr = IEEE_MODE_RSSI_THRESHOLD;
598 
599  cmd->numExtEntries = 0x00;
600  cmd->numShortEntries = 0x00;
601  cmd->pExtEntryList = 0;
602  cmd->pShortEntryList = 0;
603 
604  cmd->endTrigger.triggerType = TRIG_NEVER;
605  cmd->endTime = 0x00000000;
606 
607  /* set address filter command */
608  filter_cmd.commandNo = CMD_IEEE_MOD_FILT;
609  memcpy(&filter_cmd.newFrameFiltOpt, &cmd->frameFiltOpt, sizeof(cmd->frameFiltOpt));
610  memcpy(&filter_cmd.newFrameTypes, &cmd->frameTypes, sizeof(cmd->frameTypes));
611 }
612 /*---------------------------------------------------------------------------*/
613 static int
614 rx_on(void)
615 {
616  int ret;
617 
618  /* Get status of running IEEE_RX (if any) */
619  if(rf_is_on()) {
620  PRINTF("rx_on: We were on. PD=%u, RX=0x%04x \n", rf_core_is_accessible(),
621  RF_RADIO_OP_GET_STATUS(cmd_ieee_rx_buf));
622  return RF_CORE_CMD_OK;
623  }
624 
625  /* Put CPE in RX using the currently configured parameters */
626  ret = rf_cmd_ieee_rx();
627 
628  if(ret) {
629  ENERGEST_ON(ENERGEST_TYPE_LISTEN);
630  }
631 
632  return ret;
633 }
634 /*---------------------------------------------------------------------------*/
635 static int
636 rx_off(void)
637 {
638  uint32_t cmd_status;
639  int ret;
640 
641  /* If we are off, do nothing */
642  if(!rf_is_on()) {
643  return RF_CORE_CMD_OK;
644  }
645 
646  /* Wait for ongoing ACK TX to finish */
647  RTIMER_BUSYWAIT_UNTIL(!transmitting(), RF_CORE_TX_FINISH_TIMEOUT);
648 
649  /* Send a CMD_ABORT command to RF Core */
650  if(rf_core_send_cmd(CMDR_DIR_CMD(CMD_ABORT), &cmd_status) != RF_CORE_CMD_OK) {
651  PRINTF("RX off: CMD_ABORT status=0x%08lx\n", cmd_status);
652  /* Continue nonetheless */
653  }
654 
655  RTIMER_BUSYWAIT_UNTIL(!rf_is_on(), RF_CORE_TURN_OFF_TIMEOUT);
656 
657  if(RF_RADIO_OP_GET_STATUS(cmd_ieee_rx_buf) == IEEE_DONE_STOPPED ||
658  RF_RADIO_OP_GET_STATUS(cmd_ieee_rx_buf) == IEEE_DONE_ABORT) {
659  /* Stopped gracefully */
660  ret = RF_CORE_CMD_OK;
661  } else {
662  PRINTF("RX off: BG status=0x%04x\n", RF_RADIO_OP_GET_STATUS(cmd_ieee_rx_buf));
663  ret = RF_CORE_CMD_ERROR;
664  }
665 
666  ENERGEST_OFF(ENERGEST_TYPE_LISTEN);
667 
668  return ret;
669 }
670 /*---------------------------------------------------------------------------*/
671 static uint8_t
672 request(void)
673 {
674  /*
675  * We rely on the RDC layer to turn us on and off. Thus, if we are on we
676  * will only allow sleep, standby otherwise
677  */
678  if(rf_is_on()) {
679  return LPM_MODE_SLEEP;
680  }
681 
682  return LPM_MODE_MAX_SUPPORTED;
683 }
684 /*---------------------------------------------------------------------------*/
685 LPM_MODULE(cc26xx_rf_lpm_module, request, NULL, NULL, LPM_DOMAIN_NONE);
686 /*---------------------------------------------------------------------------*/
687 static void
688 soft_off(void)
689 {
690  uint32_t cmd_status;
691  volatile rfc_radioOp_t *cmd = rf_core_get_last_radio_op();
692 
693  if(!rf_core_is_accessible()) {
694  return;
695  }
696 
697  PRINTF("soft_off: Aborting 0x%04x, Status=0x%04x\n", cmd->commandNo,
698  cmd->status);
699 
700  /* Send a CMD_ABORT command to RF Core */
701  if(rf_core_send_cmd(CMDR_DIR_CMD(CMD_ABORT), &cmd_status) != RF_CORE_CMD_OK) {
702  PRINTF("soft_off: CMD_ABORT status=0x%08lx\n", cmd_status);
703  return;
704  }
705 
706  RTIMER_BUSYWAIT_UNTIL((cmd->status & RF_CORE_RADIO_OP_MASKED_STATUS) !=
707  RF_CORE_RADIO_OP_MASKED_STATUS_RUNNING, RF_CORE_TURN_OFF_TIMEOUT);
708 }
709 /*---------------------------------------------------------------------------*/
710 static uint8_t
711 soft_on(void)
712 {
713  if(rf_radio_setup() != RF_CORE_CMD_OK) {
714  PRINTF("on: radio_setup() failed\n");
715  return RF_CORE_CMD_ERROR;
716  }
717 
718  return rx_on();
719 }
720 /*---------------------------------------------------------------------------*/
721 static const rf_core_primary_mode_t mode_ieee = {
722  soft_off,
723  soft_on,
724  rf_is_on,
725  RAT_TIMESTAMP_OFFSET_2_4_GHZ
726 };
727 /*---------------------------------------------------------------------------*/
728 static int
729 init(void)
730 {
731  lpm_register_module(&cc26xx_rf_lpm_module);
732 
734 
735  /* Initialise RX buffers */
736  memset(rx_buf_0, 0, RX_BUF_SIZE);
737  memset(rx_buf_1, 0, RX_BUF_SIZE);
738  memset(rx_buf_2, 0, RX_BUF_SIZE);
739  memset(rx_buf_3, 0, RX_BUF_SIZE);
740 
741  /* Set of RF Core data queue. Circular buffer, no last entry */
742  rx_data_queue.pCurrEntry = rx_buf_0;
743 
744  rx_data_queue.pLastEntry = NULL;
745 
746  /* Initialize current read pointer to first element (used in ISR) */
747  rx_read_entry = rx_buf_0;
748 
749  /* Populate the RF parameters data structure with default values */
750  init_rf_params();
751 
752  if(on() != RF_CORE_CMD_OK) {
753  PRINTF("init: on() failed\n");
754  return RF_CORE_CMD_ERROR;
755  }
756 
757  ENERGEST_ON(ENERGEST_TYPE_LISTEN);
758 
759  rf_core_primary_mode_register(&mode_ieee);
760 
762 
763  process_start(&rf_core_process, NULL);
764  return 1;
765 }
766 /*---------------------------------------------------------------------------*/
767 static int
768 prepare(const void *payload, unsigned short payload_len)
769 {
770  int len = MIN(payload_len, TX_BUF_PAYLOAD_LEN);
771 
772  memcpy(&tx_buf[TX_BUF_HDR_LEN], payload, len);
773  return 0;
774 }
775 /*---------------------------------------------------------------------------*/
776 static int
777 transmit(unsigned short transmit_len)
778 {
779  int ret;
780  uint8_t was_off = 0;
781  uint32_t cmd_status;
782  uint16_t stat;
783  uint8_t tx_active = 0;
784  rtimer_clock_t t0;
785  volatile rfc_CMD_IEEE_TX_t cmd;
786 
787  if(!rf_is_on()) {
788  was_off = 1;
789  if(on() != RF_CORE_CMD_OK) {
790  PRINTF("transmit: on() failed\n");
791  return RADIO_TX_ERR;
792  }
793  }
794 
795  /*
796  * We are certainly not TXing a frame as a result of CMD_IEEE_TX, but we may
797  * be in the process of TXing an ACK. In that case, wait for the TX to finish
798  * or return after approx TX_WAIT_TIMEOUT
799  */
800  t0 = RTIMER_NOW();
801 
802  do {
803  tx_active = transmitting();
804  } while(tx_active == 1 &&
805  (RTIMER_CLOCK_LT(RTIMER_NOW(), t0 + RF_CORE_TX_TIMEOUT)));
806 
807  if(tx_active) {
808  PRINTF("transmit: Already TXing and wait timed out\n");
809 
810  if(was_off) {
811  off();
812  }
813 
814  return RADIO_TX_COLLISION;
815  }
816 
817  /* Send the CMD_IEEE_TX command */
818  rf_core_init_radio_op((rfc_radioOp_t *)&cmd, sizeof(cmd), CMD_IEEE_TX);
819 
820  cmd.payloadLen = transmit_len;
821  cmd.pPayload = &tx_buf[TX_BUF_HDR_LEN];
822 
823  cmd.startTime = 0;
824  cmd.startTrigger.triggerType = TRIG_NOW;
825 
826  /* Enable the LAST_FG_COMMAND_DONE interrupt, which will wake us up */
827  rf_core_cmd_done_en(true);
828 
829  ret = rf_core_send_cmd((uint32_t)&cmd, &cmd_status);
830 
831  if(ret) {
832  /* If we enter here, TX actually started */
833  ENERGEST_SWITCH(ENERGEST_TYPE_LISTEN, ENERGEST_TYPE_TRANSMIT);
834 
835  /* Idle away while the command is running */
836  while((cmd.status & RF_CORE_RADIO_OP_MASKED_STATUS)
837  == RF_CORE_RADIO_OP_MASKED_STATUS_RUNNING) {
838  /* Note: for now sleeping while Tx'ing in polling mode is disabled.
839  * To enable it:
840  * 1) make the `lpm_sleep()` call here unconditional;
841  * 2) change the radio ISR priority to allow radio ISR to interrupt rtimer ISR.
842  */
843  if(!rf_core_poll_mode) {
844  lpm_sleep();
845  }
846  }
847 
848  stat = cmd.status;
849 
850  if(stat == RF_CORE_RADIO_OP_STATUS_IEEE_DONE_OK) {
851  /* Sent OK */
852  ret = RADIO_TX_OK;
853  } else {
854  /* Operation completed, but frame was not sent */
855  PRINTF("transmit: ret=%d, CMDSTA=0x%08lx, status=0x%04x\n", ret,
856  cmd_status, stat);
857  ret = RADIO_TX_ERR;
858  }
859  } else {
860  /* Failure sending the CMD_IEEE_TX command */
861  PRINTF("transmit: ret=%d, CMDSTA=0x%08lx, status=0x%04x\n",
862  ret, cmd_status, cmd.status);
863 
864  ret = RADIO_TX_ERR;
865  }
866 
867  /*
868  * Update ENERGEST state here, before a potential call to off(), which
869  * will correctly update it if required.
870  */
871  ENERGEST_SWITCH(ENERGEST_TYPE_TRANSMIT, ENERGEST_TYPE_LISTEN);
872 
873  /*
874  * Disable LAST_FG_COMMAND_DONE interrupt. We don't really care about it
875  * except when we are transmitting
876  */
878 
879  if(was_off) {
880  off();
881  }
882 
883  return ret;
884 }
885 /*---------------------------------------------------------------------------*/
886 static int
887 send(const void *payload, unsigned short payload_len)
888 {
889  prepare(payload, payload_len);
890  return transmit(payload_len);
891 }
892 /*---------------------------------------------------------------------------*/
893 static void
894 release_data_entry(void)
895 {
896  rfc_dataEntryGeneral_t *entry = (rfc_dataEntryGeneral_t *)rx_read_entry;
897 
898  /* Clear the length byte */
899  rx_read_entry[8] = 0;
900 
901  /* Set status to 0 "Pending" in element */
902  entry->status = DATA_ENTRY_STATUS_PENDING;
903  rx_read_entry = entry->pNextEntry;
904 }
905 /*---------------------------------------------------------------------------*/
906 static int
907 read_frame(void *buf, unsigned short buf_len)
908 {
909  int len = 0;
910  rfc_dataEntryGeneral_t *entry = (rfc_dataEntryGeneral_t *)rx_read_entry;
911  uint32_t rat_timestamp;
912 
913  /* wait for entry to become finished */
914  rtimer_clock_t t0 = RTIMER_NOW();
915  while(entry->status == DATA_ENTRY_STATUS_BUSY
916  && RTIMER_CLOCK_LT(RTIMER_NOW(), t0 + (RTIMER_SECOND / 250)));
917 
918  if(entry->status != DATA_ENTRY_STATUS_FINISHED) {
919  /* No available data */
920  return 0;
921  }
922 
923  len = rx_read_entry[RX_BUF_LENGTH_OFFSET];
924  if(len <= RX_BUF_METADATA_SIZE) {
925  PRINTF("RF: too short!");
926 
927  release_data_entry();
928  return 0;
929  }
930 
931  len -= RX_BUF_METADATA_SIZE;
932  if(len > buf_len) {
933  PRINTF("RF: too long\n");
934 
935  release_data_entry();
936  return 0;
937  }
938 
939  memcpy(buf, (uint8_t *)rx_read_entry + RX_BUF_DATA_OFFSET, len);
940 
941  rf_core_last_rssi = (int8_t)rx_read_entry[RX_BUF_DATA_OFFSET + len];
942  rf_core_last_corr_lqi = (uint8_t)rx_read_entry[RX_BUF_DATA_OFFSET + len + 1] & STATUS_CORRELATION;
943 
944  /* get the timestamp */
945  memcpy(&rat_timestamp, (uint8_t *)rx_read_entry + RX_BUF_DATA_OFFSET + len + 2, 4);
946 
947  rf_core_last_packet_timestamp = rf_core_convert_rat_to_rtimer(rat_timestamp);
948 
949  if(!rf_core_poll_mode) {
950  /* Not in poll mode: packetbuf should not be accessed in interrupt context.
951  * In poll mode, the last packet RSSI and link quality can be obtained through
952  * RADIO_PARAM_LAST_RSSI and RADIO_PARAM_LAST_LINK_QUALITY */
953  packetbuf_set_attr(PACKETBUF_ATTR_RSSI, rf_core_last_rssi);
954  packetbuf_set_attr(PACKETBUF_ATTR_LINK_QUALITY, rf_core_last_corr_lqi);
955  }
956 
957  release_data_entry();
958 
959  return len;
960 }
961 /*---------------------------------------------------------------------------*/
962 static int
963 channel_clear(void)
964 {
965  uint8_t was_off = 0;
966  uint8_t cca_info;
967  int ret = RF_CORE_CCA_CLEAR;
968 
969  /*
970  * If we are in the middle of a BLE operation, we got called by ContikiMAC
971  * from within an interrupt context. Indicate a clear channel
972  */
973  if(rf_ble_is_active() == RF_BLE_ACTIVE) {
974  PRINTF("channel_clear: Interrupt context but BLE in progress\n");
975  return RF_CORE_CCA_CLEAR;
976  }
977 
978  if(rf_is_on()) {
979  /*
980  * Wait for potential leftover ACK still being sent.
981  * Strictly speaking, if we are TXing an ACK then the channel is not clear.
982  * However, channel_clear is only ever called to determine whether there is
983  * someone else's packet in the air, not ours.
984  *
985  * We could probably even simply return that the channel is clear
986  */
987  RTIMER_BUSYWAIT_UNTIL(!transmitting(), RF_CORE_TX_FINISH_TIMEOUT);
988  } else {
989  was_off = 1;
990  if(on() != RF_CORE_CMD_OK) {
991  PRINTF("channel_clear: on() failed\n");
992  if(was_off) {
993  off();
994  }
995  return RF_CORE_CCA_CLEAR;
996  }
997  }
998 
999  cca_info = get_cca_info();
1000 
1001  if(cca_info == RF_CORE_GET_CCA_INFO_ERROR) {
1002  PRINTF("channel_clear: CCA error\n");
1003  ret = RF_CORE_CCA_CLEAR;
1004  } else {
1005  /*
1006  * cca_info bits 1:0 - ccaStatus
1007  * Return 1 (clear) if idle or invalid.
1008  */
1009  ret = (cca_info & 0x03) != RF_CORE_CMD_CCA_REQ_CCA_STATE_BUSY;
1010  }
1011 
1012  if(was_off) {
1013  off();
1014  }
1015 
1016  return ret;
1017 }
1018 /*---------------------------------------------------------------------------*/
1019 static int
1020 receiving_packet(void)
1021 {
1022  uint8_t cca_info;
1023 
1024  /*
1025  * If we are in the middle of a BLE operation, we got called by ContikiMAC
1026  * from within an interrupt context. We are not receiving
1027  */
1028  if(rf_ble_is_active() == RF_BLE_ACTIVE) {
1029  PRINTF("receiving_packet: Interrupt context but BLE in progress\n");
1030  return 0;
1031  }
1032 
1033  /* If we are off, we are not receiving */
1034  if(!rf_is_on()) {
1035  PRINTF("receiving_packet: We were off\n");
1036  return 0;
1037  }
1038 
1039  /* If we are transmitting (can only be an ACK here), we are not receiving */
1040  if(transmitting()) {
1041  PRINTF("receiving_packet: We were TXing\n");
1042  return 0;
1043  }
1044 
1045  cca_info = get_cca_info();
1046 
1047  /* If we can't read CCA info, return "not receiving" */
1048  if(cca_info == RF_CORE_GET_CCA_INFO_ERROR) {
1049  return 0;
1050  }
1051 
1052  /* If sync has been seen, return 1 (receiving) */
1053  if(cca_info & RF_CORE_CMD_CCA_REQ_CCA_SYNC_BUSY) {
1054  return 1;
1055  }
1056 
1057  return 0;
1058 }
1059 /*---------------------------------------------------------------------------*/
1060 static int
1061 pending_packet(void)
1062 {
1063  volatile rfc_dataEntry_t *entry = (rfc_dataEntry_t *)rx_data_queue.pCurrEntry;
1064  int rv = 0;
1065 
1066  /* Go through all RX buffers and check their status */
1067  do {
1068  if(entry->status == DATA_ENTRY_STATUS_FINISHED
1069  || entry->status == DATA_ENTRY_STATUS_BUSY) {
1070  rv = 1;
1071  if(!rf_core_poll_mode) {
1072  process_poll(&rf_core_process);
1073  }
1074  }
1075 
1076  entry = (rfc_dataEntry_t *)entry->pNextEntry;
1077  } while(entry != (rfc_dataEntry_t *)rx_data_queue.pCurrEntry);
1078 
1079  /* If we didn't find an entry at status finished, no frames are pending */
1080  return rv;
1081 }
1082 /*---------------------------------------------------------------------------*/
1083 static int
1084 on(void)
1085 {
1086  /*
1087  * If we are in the middle of a BLE operation, we got called by ContikiMAC
1088  * from within an interrupt context. Abort, but pretend everything is OK.
1089  */
1090  if(rf_ble_is_active() == RF_BLE_ACTIVE) {
1091  PRINTF("on: Interrupt context but BLE in progress\n");
1092  return RF_CORE_CMD_OK;
1093  }
1094 
1095  /*
1096  * Request the HF XOSC as the source for the HF clock. Needed before we can
1097  * use the FS. This will only request, it will _not_ perform the switch.
1098  */
1100 
1101  if(rf_is_on()) {
1102  PRINTF("on: We were on. PD=%u, RX=0x%04x \n", rf_core_is_accessible(),
1103  RF_RADIO_OP_GET_STATUS(cmd_ieee_rx_buf));
1104  return RF_CORE_CMD_OK;
1105  }
1106 
1107  init_rx_buffers();
1108 
1109  /*
1110  * Trigger a switch to the XOSC, so that we can subsequently use the RF FS
1111  * This will block until the XOSC is actually ready, but give how we
1112  * requested it early on, this won't be too long a wait.
1113  * This should be done before starting the RAT.
1114  */
1116 
1117  if(rf_core_boot() != RF_CORE_CMD_OK) {
1118  PRINTF("on: rf_core_boot() failed\n");
1119  return RF_CORE_CMD_ERROR;
1120  }
1121 
1123 
1124  if(rf_radio_setup() != RF_CORE_CMD_OK) {
1125  PRINTF("on: radio_setup() failed\n");
1126  return RF_CORE_CMD_ERROR;
1127  }
1128 
1129  return rx_on();
1130 }
1131 /*---------------------------------------------------------------------------*/
1132 static int
1133 off(void)
1134 {
1135  /*
1136  * If we are in the middle of a BLE operation, we got called by ContikiMAC
1137  * from within an interrupt context. Abort, but pretend everything is OK.
1138  */
1139  if(rf_ble_is_active() == RF_BLE_ACTIVE) {
1140  PRINTF("off: Interrupt context but BLE in progress\n");
1141  return RF_CORE_CMD_OK;
1142  }
1143 
1144  RTIMER_BUSYWAIT_UNTIL(!transmitting(), RF_CORE_TX_FINISH_TIMEOUT);
1145 
1146  /* stopping the rx explicitly results in lower sleep-mode power usage */
1147  rx_off();
1149 
1150  ENERGEST_OFF(ENERGEST_TYPE_LISTEN);
1151 
1152 #if !CC2650_FAST_RADIO_STARTUP
1153  /* Switch HF clock source to the RCOSC to preserve power.
1154  * This must be done after stopping RAT.
1155  */
1157 #endif
1158 
1159  /* We pulled the plug, so we need to restore the status manually */
1160  ((rfc_CMD_IEEE_RX_t *)cmd_ieee_rx_buf)->status = RF_CORE_RADIO_OP_STATUS_IDLE;
1161 
1162  /*
1163  * Just in case there was an ongoing RX (which started after we begun the
1164  * shutdown sequence), we don't want to leave the buffer in state == ongoing
1165  */
1166  if(((rfc_dataEntry_t *)rx_buf_0)->status == DATA_ENTRY_STATUS_BUSY) {
1167  ((rfc_dataEntry_t *)rx_buf_0)->status = DATA_ENTRY_STATUS_PENDING;
1168  }
1169  if(((rfc_dataEntry_t *)rx_buf_1)->status == DATA_ENTRY_STATUS_BUSY) {
1170  ((rfc_dataEntry_t *)rx_buf_1)->status = DATA_ENTRY_STATUS_PENDING;
1171  }
1172  if(((rfc_dataEntry_t *)rx_buf_2)->status == DATA_ENTRY_STATUS_BUSY) {
1173  ((rfc_dataEntry_t *)rx_buf_2)->status = DATA_ENTRY_STATUS_PENDING;
1174  }
1175  if(((rfc_dataEntry_t *)rx_buf_3)->status == DATA_ENTRY_STATUS_BUSY) {
1176  ((rfc_dataEntry_t *)rx_buf_3)->status = DATA_ENTRY_STATUS_PENDING;
1177  }
1178 
1179  return RF_CORE_CMD_OK;
1180 }
1181 /*---------------------------------------------------------------------------*/
1182 /* Enable or disable CCA before sending */
1183 static radio_result_t
1184 set_send_on_cca(uint8_t enable)
1185 {
1186  if(enable) {
1187  /* this driver does not have support for CCA on Tx */
1188  return RADIO_RESULT_NOT_SUPPORTED;
1189  }
1190  return RADIO_RESULT_OK;
1191 }
1192 /*---------------------------------------------------------------------------*/
1193 static radio_result_t
1194 get_value(radio_param_t param, radio_value_t *value)
1195 {
1196  rfc_CMD_IEEE_RX_t *cmd = (rfc_CMD_IEEE_RX_t *)cmd_ieee_rx_buf;
1197 
1198  if(!value) {
1199  return RADIO_RESULT_INVALID_VALUE;
1200  }
1201 
1202  switch(param) {
1203  case RADIO_PARAM_POWER_MODE:
1204  /* On / off */
1205  *value = rf_is_on() ? RADIO_POWER_MODE_ON : RADIO_POWER_MODE_OFF;
1206  return RADIO_RESULT_OK;
1207  case RADIO_PARAM_CHANNEL:
1208  *value = (radio_value_t)cmd->channel;
1209  return RADIO_RESULT_OK;
1210  case RADIO_PARAM_PAN_ID:
1211  *value = (radio_value_t)cmd->localPanID;
1212  return RADIO_RESULT_OK;
1213  case RADIO_PARAM_16BIT_ADDR:
1214  *value = (radio_value_t)cmd->localShortAddr;
1215  return RADIO_RESULT_OK;
1216  case RADIO_PARAM_RX_MODE:
1217  *value = 0;
1218  if(cmd->frameFiltOpt.frameFiltEn) {
1219  *value |= RADIO_RX_MODE_ADDRESS_FILTER;
1220  }
1221  if(cmd->frameFiltOpt.autoAckEn) {
1222  *value |= RADIO_RX_MODE_AUTOACK;
1223  }
1224  if(rf_core_poll_mode) {
1225  *value |= RADIO_RX_MODE_POLL_MODE;
1226  }
1227 
1228  return RADIO_RESULT_OK;
1229  case RADIO_PARAM_TX_MODE:
1230  *value = 0;
1231  return RADIO_RESULT_OK;
1232  case RADIO_PARAM_TXPOWER:
1233  *value = get_tx_power();
1234  return RADIO_RESULT_OK;
1235  case RADIO_PARAM_CCA_THRESHOLD:
1236  *value = cmd->ccaRssiThr;
1237  return RADIO_RESULT_OK;
1238  case RADIO_PARAM_RSSI:
1239  *value = get_rssi();
1240 
1241  if(*value == RF_CORE_CMD_CCA_REQ_RSSI_UNKNOWN) {
1242  return RADIO_RESULT_ERROR;
1243  } else {
1244  return RADIO_RESULT_OK;
1245  }
1246  case RADIO_CONST_CHANNEL_MIN:
1247  *value = IEEE_MODE_CHANNEL_MIN;
1248  return RADIO_RESULT_OK;
1249  case RADIO_CONST_CHANNEL_MAX:
1250  *value = IEEE_MODE_CHANNEL_MAX;
1251  return RADIO_RESULT_OK;
1252  case RADIO_CONST_TXPOWER_MIN:
1253  *value = OUTPUT_POWER_MIN;
1254  return RADIO_RESULT_OK;
1255  case RADIO_CONST_TXPOWER_MAX:
1256  *value = OUTPUT_POWER_MAX;
1257  return RADIO_RESULT_OK;
1258  case RADIO_PARAM_LAST_RSSI:
1259  *value = rf_core_last_rssi;
1260  return RADIO_RESULT_OK;
1261  case RADIO_PARAM_LAST_LINK_QUALITY:
1262  *value = rf_core_last_corr_lqi;
1263  return RADIO_RESULT_OK;
1264  case RADIO_CONST_PHY_OVERHEAD:
1265  *value = (radio_value_t)RADIO_PHY_OVERHEAD;
1266  return RADIO_RESULT_OK;
1267  case RADIO_CONST_BYTE_AIR_TIME:
1268  *value = (radio_value_t)RADIO_BYTE_AIR_TIME;
1269  return RADIO_RESULT_OK;
1270  case RADIO_CONST_DELAY_BEFORE_TX:
1271  *value = (radio_value_t)RADIO_DELAY_BEFORE_TX;
1272  return RADIO_RESULT_OK;
1273  case RADIO_CONST_DELAY_BEFORE_RX:
1274  *value = (radio_value_t)RADIO_DELAY_BEFORE_RX;
1275  return RADIO_RESULT_OK;
1276  case RADIO_CONST_DELAY_BEFORE_DETECT:
1277  *value = (radio_value_t)RADIO_DELAY_BEFORE_DETECT;
1278  return RADIO_RESULT_OK;
1279  default:
1280  return RADIO_RESULT_NOT_SUPPORTED;
1281  }
1282 }
1283 /*---------------------------------------------------------------------------*/
1284 static radio_result_t
1285 set_value(radio_param_t param, radio_value_t value)
1286 {
1287  radio_result_t rv = RADIO_RESULT_OK;
1288  rfc_CMD_IEEE_RX_t *cmd = (rfc_CMD_IEEE_RX_t *)cmd_ieee_rx_buf;
1289  uint8_t old_poll_mode;
1290 
1291  switch(param) {
1292  case RADIO_PARAM_POWER_MODE:
1293  if(value == RADIO_POWER_MODE_ON) {
1294  if(on() != RF_CORE_CMD_OK) {
1295  PRINTF("set_value: on() failed (1)\n");
1296  return RADIO_RESULT_ERROR;
1297  }
1298  return RADIO_RESULT_OK;
1299  }
1300  if(value == RADIO_POWER_MODE_OFF) {
1301  off();
1302  return RADIO_RESULT_OK;
1303  }
1304  return RADIO_RESULT_INVALID_VALUE;
1305  case RADIO_PARAM_CHANNEL:
1306  if(value < IEEE_MODE_CHANNEL_MIN ||
1307  value > IEEE_MODE_CHANNEL_MAX) {
1308  return RADIO_RESULT_INVALID_VALUE;
1309  }
1310 
1311  /* Note: this return may lead to long periods when RAT and RTC are not resynchronized */
1312  if(cmd->channel == (uint8_t)value) {
1313  /* We already have that very same channel configured.
1314  * Nothing to do here. */
1315  return RADIO_RESULT_OK;
1316  }
1317 
1318  cmd->channel = (uint8_t)value;
1319  break;
1320  case RADIO_PARAM_PAN_ID:
1321  cmd->localPanID = (uint16_t)value;
1322  break;
1323  case RADIO_PARAM_16BIT_ADDR:
1324  cmd->localShortAddr = (uint16_t)value;
1325  break;
1326  case RADIO_PARAM_RX_MODE:
1327  {
1328  if(value & ~(RADIO_RX_MODE_ADDRESS_FILTER |
1329  RADIO_RX_MODE_AUTOACK | RADIO_RX_MODE_POLL_MODE)) {
1330  return RADIO_RESULT_INVALID_VALUE;
1331  }
1332 
1333  cmd->frameFiltOpt.frameFiltEn = (value & RADIO_RX_MODE_ADDRESS_FILTER) != 0;
1334  cmd->frameFiltOpt.frameFiltStop = 1;
1335  cmd->frameFiltOpt.autoAckEn = (value & RADIO_RX_MODE_AUTOACK) != 0;
1336  cmd->frameFiltOpt.slottedAckEn = 0;
1337  cmd->frameFiltOpt.autoPendEn = 0;
1338  cmd->frameFiltOpt.defaultPend = 0;
1339  cmd->frameFiltOpt.bPendDataReqOnly = 0;
1340  cmd->frameFiltOpt.bPanCoord = 0;
1341  cmd->frameFiltOpt.bStrictLenFilter = 0;
1342 
1343  old_poll_mode = rf_core_poll_mode;
1344  rf_core_poll_mode = (value & RADIO_RX_MODE_POLL_MODE) != 0;
1345  if(rf_core_poll_mode == old_poll_mode) {
1346  uint32_t cmd_status;
1347 
1348  /* do not turn the radio on and off, just send an update command */
1349  memcpy(&filter_cmd.newFrameFiltOpt, &cmd->frameFiltOpt, sizeof(cmd->frameFiltOpt));
1350 
1351  if(rf_core_send_cmd((uint32_t)&filter_cmd, &cmd_status) == RF_CORE_CMD_ERROR) {
1352  PRINTF("setting address filter failed: CMDSTA=0x%08lx\n", cmd_status);
1353  return RADIO_RESULT_ERROR;
1354  }
1355  return RADIO_RESULT_OK;
1356  }
1357  break;
1358  }
1359 
1360  case RADIO_PARAM_TX_MODE:
1361  if(value & ~(RADIO_TX_MODE_SEND_ON_CCA)) {
1362  return RADIO_RESULT_INVALID_VALUE;
1363  }
1364  return set_send_on_cca((value & RADIO_TX_MODE_SEND_ON_CCA) != 0);
1365 
1366  case RADIO_PARAM_TXPOWER:
1367  if(value < OUTPUT_POWER_MIN || value > OUTPUT_POWER_MAX) {
1368  return RADIO_RESULT_INVALID_VALUE;
1369  }
1370 
1371  set_tx_power(value);
1372 
1373  return RADIO_RESULT_OK;
1374 
1375  case RADIO_PARAM_CCA_THRESHOLD:
1376  cmd->ccaRssiThr = (int8_t)value;
1377  break;
1378 
1379  default:
1380  return RADIO_RESULT_NOT_SUPPORTED;
1381  }
1382 
1383  /* If off, the new configuration will be applied the next time radio is started */
1384  if(!rf_is_on()) {
1385  return RADIO_RESULT_OK;
1386  }
1387 
1388  /* If we reach here we had no errors. Apply new settings */
1389  if(rx_off() != RF_CORE_CMD_OK) {
1390  PRINTF("set_value: rx_off() failed\n");
1391  rv = RADIO_RESULT_ERROR;
1392  }
1393 
1394  /* Restart the radio timer (RAT).
1395  This causes resynchronization between RAT and RTC: useful for TSCH. */
1396  if(rf_core_restart_rat() == RF_CORE_CMD_OK) {
1398  }
1399 
1400  if(rx_on() != RF_CORE_CMD_OK) {
1401  PRINTF("set_value: rx_on() failed\n");
1402  rv = RADIO_RESULT_ERROR;
1403  }
1404 
1405  return rv;
1406 }
1407 /*---------------------------------------------------------------------------*/
1408 static radio_result_t
1409 get_object(radio_param_t param, void *dest, size_t size)
1410 {
1411  uint8_t *target;
1412  uint8_t *src;
1413  int i;
1414  rfc_CMD_IEEE_RX_t *cmd = (rfc_CMD_IEEE_RX_t *)cmd_ieee_rx_buf;
1415 
1416  if(param == RADIO_PARAM_64BIT_ADDR) {
1417  if(size != 8 || !dest) {
1418  return RADIO_RESULT_INVALID_VALUE;
1419  }
1420 
1421  target = dest;
1422  src = (uint8_t *)(&cmd->localExtAddr);
1423 
1424  for(i = 0; i < 8; i++) {
1425  target[i] = src[7 - i];
1426  }
1427 
1428  return RADIO_RESULT_OK;
1429  }
1430 
1431  if(param == RADIO_PARAM_LAST_PACKET_TIMESTAMP) {
1432  if(size != sizeof(rtimer_clock_t) || !dest) {
1433  return RADIO_RESULT_INVALID_VALUE;
1434  }
1435  *(rtimer_clock_t *)dest = rf_core_last_packet_timestamp;
1436 
1437  return RADIO_RESULT_OK;
1438  }
1439 
1440  return RADIO_RESULT_NOT_SUPPORTED;
1441 }
1442 /*---------------------------------------------------------------------------*/
1443 static radio_result_t
1444 set_object(radio_param_t param, const void *src, size_t size)
1445 {
1446  radio_result_t rv = RADIO_RESULT_OK;
1447  int i;
1448  uint8_t *dst;
1449  rfc_CMD_IEEE_RX_t *cmd = (rfc_CMD_IEEE_RX_t *)cmd_ieee_rx_buf;
1450 
1451  if(param == RADIO_PARAM_64BIT_ADDR) {
1452  if(size != 8 || !src) {
1453  return RADIO_RESULT_INVALID_VALUE;
1454  }
1455 
1456  dst = (uint8_t *)(&cmd->localExtAddr);
1457 
1458  for(i = 0; i < 8; i++) {
1459  dst[i] = ((uint8_t *)src)[7 - i];
1460  }
1461 
1462  /* If off, the new configuration will be applied the next time radio is started */
1463  if(!rf_is_on()) {
1464  return RADIO_RESULT_OK;
1465  }
1466 
1467  if(rx_off() != RF_CORE_CMD_OK) {
1468  PRINTF("set_object: rx_off() failed\n");
1469  rv = RADIO_RESULT_ERROR;
1470  }
1471 
1472  if(rx_on() != RF_CORE_CMD_OK) {
1473  PRINTF("set_object: rx_on() failed\n");
1474  rv = RADIO_RESULT_ERROR;
1475  }
1476 
1477  return rv;
1478  }
1479  return RADIO_RESULT_NOT_SUPPORTED;
1480 }
1481 /*---------------------------------------------------------------------------*/
1482 const struct radio_driver ieee_mode_driver = {
1483  init,
1484  prepare,
1485  transmit,
1486  send,
1487  read_frame,
1488  channel_clear,
1491  on,
1492  off,
1493  get_value,
1494  set_value,
1495  get_object,
1496  set_object,
1497 };
1498 /*---------------------------------------------------------------------------*/
1499 /**
1500  * @}
1501  * @}
1502  */
radio_result_t(* get_object)(radio_param_t param, void *dest, size_t size)
Get a radio parameter object.
Definition: radio.h:290
void oscillators_request_hf_xosc(void)
Requests the HF XOSC as the source for the HF clock, but does not perform the actual switch...
Definition: oscillators.c:94
static uint8_t transmitting(void)
Check the RF&#39;s TX status.
Definition: ieee-mode.c:272
int(* prepare)(const void *payload, unsigned short payload_len)
Prepare the radio with a packet to be sent.
Definition: radio.h:252
static uint8_t rf_cmd_ieee_rx()
Set up radio in IEEE802.15.4 RX mode.
Definition: ieee-mode.c:478
Header file with macros which rename TI CC26xxware functions.
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:254
Header file for the energy estimation mechanism
rfc_radioOp_t * rf_core_get_last_radio_op()
Returns a pointer to the most recent proto-dependent Radio Op.
Definition: rf-core.c:534
Header file for the radio API
Header file for the link-layer address representation
void rf_core_power_down()
Disable RFCORE clock domain in the MCU VD and turn off the RFCORE PD.
Definition: rf-core.c:372
int(* receiving_packet)(void)
Check if the radio driver is currently receiving a packet.
Definition: radio.h:268
radio_result_t(* set_value)(radio_param_t param, radio_value_t value)
Set a radio parameter value.
Definition: radio.h:283
int(* pending_packet)(void)
Check if the radio driver has just received a packet.
Definition: radio.h:271
uint8_t rf_core_set_modesel()
Initialise RF APIs in the RF core.
Definition: rf-core.c:410
The structure of a device driver for a radio in Contiki.
Definition: radio.h:247
#define RTIMER_BUSYWAIT_UNTIL(cond, max_time)
Busy-wait until a condition for at most max_time.
Definition: rtimer.h:202
void lpm_sleep(void)
Enter sleep mode.
Definition: lpm.c:376
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:265
Header file for the CC13xx/CC26xx RF core driver.
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
#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:160
Header file for the CC13xx/CC26xx oscillator control.
uint32_t rf_core_convert_rat_to_rtimer(uint32_t rat_timestamp)
Convert from RAT timestamp to rtimer ticks.
Definition: rf-core.c:652
void oscillators_switch_to_hf_rc(void)
Switches MF and HF clock source to be the HF RC OSC.
Definition: oscillators.c:134
Header file for the callback timer
void rf_core_primary_mode_register(const rf_core_primary_mode_t *mode)
Register a primary mode for radio operation.
Definition: rf-core.c:549
int(* send)(const void *payload, unsigned short payload_len)
Prepare & transmit a packet.
Definition: radio.h:258
A data strcuture representing the radio&#39;s primary mode of operation.
Definition: rf-core.h:125
int(* transmit)(unsigned short transmit_len)
Send the packet that has previously been prepared.
Definition: radio.h:255
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:277
Header file for the real-time timer module.
uint8_t rf_core_rat_init(void)
Initialize the RAT to RTC conversion machinery.
Definition: rf-core.c:577
static radio_value_t get_rssi(void)
Reads the current signal strength (RSSI)
Definition: ieee-mode.c:347
void rf_core_cmd_done_dis(void)
Disable the LAST_CMD_DONE and LAST_FG_CMD_DONE interrupts.
Definition: rf-core.c:527
void rf_core_init_radio_op(rfc_radioOp_t *op, uint16_t len, uint16_t command)
Prepare a buffer to host a Radio Op.
Definition: rf-core.c:540
#define RADIO_RX_MODE_ADDRESS_FILTER
The radio reception mode controls address filtering and automatic transmission of acknowledgements in...
Definition: radio.h:214
uint8_t rf_ble_is_active()
Check whether the BLE beacond is currently active.
Definition: rf-ble.c:262
Header file for the CC13xx/CC26xx BLE driver.
Header file with definitions related to RF switch support.
#define RADIO_TX_MODE_SEND_ON_CCA
The radio transmission mode controls whether transmissions should be done using clear channel assessm...
Definition: radio.h:226
static uint8_t get_cca_info(void)
Returns CCA information.
Definition: ieee-mode.c:311
uint8_t rf_core_is_accessible()
Check whether the RF core is accessible.
Definition: rf-core.c:145
Header file for the CC13xx/CC26xx UART driver.
void oscillators_switch_to_hf_xosc(void)
Performs the switch to the XOSC.
Definition: oscillators.c:116
uint_fast8_t rf_core_send_cmd(uint32_t cmd, uint32_t *status)
Sends a command to the RF core.
Definition: rf-core.c:154
Header file for the Packet buffer (packetbuf) management
#define LPM_MODULE(n, m, s, w, l)
Declare a variable to be used in order to get notifications from LPM.
Definition: lpm.h:92
void rf_core_setup_interrupts(void)
Setup RF core interrupts.
Definition: rf-core.c:479
Include file for the Contiki low-layer network stack (NETSTACK)
uint8_t rf_core_restart_rat(void)
Restart the CM0 RAT.
Definition: rf-core.c:460
radio_result_t(* get_value)(radio_param_t param, radio_value_t *value)
Get a radio parameter value.
Definition: radio.h:280
Default definitions of C compiler quirk work-arounds.
uint8_t rf_core_boot()
Boot the RF Core.
Definition: rf-core.c:438
radio_result_t(* set_object)(radio_param_t param, const void *src, size_t size)
Set a radio parameter object.
Definition: radio.h:296
void rf_core_cmd_done_en(bool fg)
Enable interrupt on command done.
Definition: rf-core.c:513
uint8_t rf_core_check_rat_overflow(void)
Check if RAT overflow has occured and increment the overflow counter if so.
Definition: rf-core.c:588
int(* on)(void)
Turn the radio on.
Definition: radio.h:274
void process_start(struct process *p, process_data_t data)
Start a process.
Definition: process.c:99
void lpm_register_module(lpm_registered_module_t *module)
Register a module for LPM notifications.
Definition: lpm.c:539
uint_fast8_t rf_core_wait_cmd_done(void *cmd)
Block and wait for a Radio op to complete.
Definition: rf-core.c:220
#define RF_RADIO_OP_GET_STATUS(a)
Returns the current status of a running Radio Op command.
Definition: ieee-mode.c:133