Contiki-NG
cc1200.c
1 /*
2  * Copyright (c) 2015, Weptech elektronik GmbH Germany
3  * http://www.weptech.de
4  *
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in the
14  * documentation and/or other materials provided with the distribution.
15  * 3. Neither the name of the copyright holder nor the names of its
16  * contributors may be used to endorse or promote products derived
17  * from this software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
22  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
23  * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
24  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
25  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
28  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
30  * OF THE POSSIBILITY OF SUCH DAMAGE.
31  *
32  * This file is part of the Contiki operating system.
33  */
34 
35 #include "dev/radio/cc1200/cc1200-const.h"
36 #include "dev/radio/cc1200/cc1200-conf.h"
37 #include "dev/radio/cc1200/cc1200-arch.h"
38 #include "dev/radio/cc1200/cc1200-rf-cfg.h"
39 
40 #include "net/netstack.h"
41 #include "net/packetbuf.h"
42 #include "dev/watchdog.h"
43 #include "sys/energest.h"
44 
45 #include "dev/leds.h"
46 
47 #include <string.h>
48 #include <stdio.h>
49 
50 static int16_t rssi;
51 static rtimer_clock_t sfd_timestamp = 0;
52 
53 /*---------------------------------------------------------------------------*/
54 /* Various implementation specific defines */
55 /*---------------------------------------------------------------------------*/
56 /*
57  * The debug level to use
58  * - 0: No output at all
59  * - 1: Print errors (unrecoverable)
60  * - 2: Print errors + warnings (recoverable errors)
61  * - 3: Print errors + warnings + information (what's going on...)
62  */
63 #define DEBUG_LEVEL 0
64 /*
65  * RF test mode. Blocks inside "configure()".
66  * - Set this parameter to 1 in order to produce an modulated carrier (PN9)
67  * - Set this parameter to 2 in order to produce an unmodulated carrier
68  * - Set this parameter to 3 in order to switch to rx synchronous mode
69  * The channel is set according to CC1200_DEFAULT_CHANNEL
70  */
71 #ifndef CC1200_RF_TESTMODE
72 #define CC1200_RF_TESTMODE 0
73 #endif
74 
75 #if CC1200_RF_TESTMODE
76 #undef CC1200_RF_CFG
77 #if CC1200_RF_TESTMODE == 1
78 #define CC1200_RF_CFG cc1200_802154g_863_870_fsk_50kbps
79 #elif CC1200_RF_TESTMODE == 2
80 #define CC1200_RF_CFG cc1200_802154g_863_870_fsk_50kbps
81 #elif CC1200_RF_TESTMODE == 3
82 #define CC1200_RF_CFG cc1200_802154g_863_870_fsk_50kbps
83 #endif
84 #endif
85 /*
86  * When set, a software buffer is used to store outgoing packets before copying
87  * to Tx FIFO. This enabled sending packets larger than the FIFO. When unset,
88  * no buffer is used; instead, the payload is copied directly to the Tx FIFO.
89  * This is requried by TSCH, for shorter and more predictable delay in the Tx
90  * chain. This, however, restircts the payload length to the Tx FIFO size.
91  */
92 #define CC1200_WITH_TX_BUF (!MAC_CONF_WITH_TSCH)
93 /*
94  * Set this parameter to 1 in order to use the MARC_STATE register when
95  * polling the chips's status. Else use the status byte returned when sending
96  * a NOP strobe.
97  *
98  * TODO: Option to be removed upon approval of the driver
99  */
100 #define STATE_USES_MARC_STATE 0
101 /*
102  * Set this parameter to 1 in order to speed up transmission by
103  * sending a FSTXON strobe before filling the FIFO.
104  *
105  * TODO: Option to be removed upon approval of the driver
106  */
107 #if MAC_CONF_WITH_TSCH
108 #define USE_SFSTXON 0
109 #else /* MAC_CONF_WITH_TSCH */
110 #define USE_SFSTXON 1
111 #endif /* MAC_CONF_WITH_TSCH */
112 /*---------------------------------------------------------------------------*/
113 /* Phy header length */
114 #if CC1200_802154G
115 /* Phy header = 2 byte */
116 #define PHR_LEN 2
117 #else
118 /* Phy header = length byte = 1 byte */
119 #define PHR_LEN 1
120 #endif /* #if CC1200_802154G */
121 /*---------------------------------------------------------------------------*/
122 /* Size of appendix (rssi + lqi) appended to the rx pkt */
123 #define APPENDIX_LEN 2
124 /*---------------------------------------------------------------------------*/
125 /* Verify payload length */
126 /*---------------------------------------------------------------------------*/
127 #if CC1200_802154G
128 #if CC1200_USE_GPIO2
129 #if CC1200_MAX_PAYLOAD_LEN > (2048 - PHR_LEN)
130 #error Payload length not supported by this driver
131 #endif
132 #else
133 #if CC1200_MAX_PAYLOAD_LEN > (CC1200_FIFO_SIZE - PHR_LEN)
134 /* PHR_LEN = 2 -> we can only place 126 payload bytes bytes in the FIFO */
135 #error Payload length not supported without GPIO2
136 #endif
137 #endif /* #if CC1200_USE_GPIO2 */
138 #else /* #if CC1200_802154G */
139 #if CC1200_MAX_PAYLOAD_LEN > (CC1200_FIFO_SIZE - PHR_LEN)
140 /* PHR_LEN = 1 -> we can only place 127 payload bytes bytes in the FIFO */
141 #error Payload length not supported without enabling 802.15.4g mode
142 #endif
143 #endif /* #if CC1200_802154G */
144 /*---------------------------------------------------------------------------*/
145 /* Main driver configurations settings. Don't touch! */
146 /*---------------------------------------------------------------------------*/
147 #if CC1200_USE_GPIO2
148 /* Use GPIO2 as RX / TX FIFO threshold indicator pin */
149 #define GPIO2_IOCFG CC1200_IOCFG_RXFIFO_THR
150 /* This is the FIFO threshold we use */
151 #if MAC_CONF_WITH_TSCH
152 #if CC1200_802154G
153 #define FIFO_THRESHOLD 1
154 #else
155 #define FIFO_THRESHOLD 0
156 #endif
157 #else /* MAC_CONF_WITH_TSCH */
158 #define FIFO_THRESHOLD 32
159 #endif /* MAC_CONF_WITH_TSCH */
160 /* Turn on RX after packet reception */
161 #define RXOFF_MODE_RX 1
162 /* Let the CC1200 append RSSI + LQI */
163 #define APPEND_STATUS 1
164 #else
165 /* Arbitrary configuration for GPIO2 */
166 #define GPIO2_IOCFG CC1200_IOCFG_MARC_2PIN_STATUS_0
167 #if (CC1200_MAX_PAYLOAD_LEN <= (CC1200_FIFO_SIZE - PHR_LEN - APPENDIX_LEN))
168 /*
169  * Read out RX FIFO at the end of the packet (GPIO0 falling edge). RX restarts
170  * automatically
171  */
172 #define RXOFF_MODE_RX 1
173 /* Let the CC1200 append RSSI + LQI */
174 #define APPEND_STATUS 1
175 #else
176 /*
177  * Read out RX FIFO at the end of the packet (GPIO0 falling edge). RX has
178  * to be started manually in this case
179  */
180 #define RXOFF_MODE_RX 0
181 /* No space for appendix in the RX FIFO. Read it out by hand */
182 #define APPEND_STATUS 0
183 #endif /* #if CC1200_MAX_PAYLOAD_LEN <= 125 */
184 #endif /* #if CC1200_USE_GPIO2 */
185 
186 /* Read out packet on falling edge of GPIO0 */
187 #define GPIO0_IOCFG CC1200_IOCFG_PKT_SYNC_RXTX
188 /* Arbitrary configuration for GPIO3 */
189 #define GPIO3_IOCFG CC1200_IOCFG_MARC_2PIN_STATUS_0
190 /* Turn on RX automatically after TX */
191 #define TXOFF_MODE_RX 1
192 #if APPEND_STATUS
193 /* CC1200 places two bytes in the RX FIFO */
194 #define CC_APPENDIX_LEN 2
195 #else
196 /* CC1200 doesn't add appendix to RX FIFO */
197 #define CC_APPENDIX_LEN 0
198 #endif /* #if APPEND_STATUS */
199 /*---------------------------------------------------------------------------*/
200 /* RF configuration */
201 /*---------------------------------------------------------------------------*/
202 /* Import the rf configuration set by CC1200_RF_CFG */
203 extern const cc1200_rf_cfg_t CC1200_RF_CFG;
204 /*---------------------------------------------------------------------------*/
205 /* This defines the way we calculate the frequency registers */
206 /*---------------------------------------------------------------------------*/
207 /* XTAL frequency in kHz */
208 #define XTAL_FREQ_KHZ 40000
209 /*
210  * Divider + multiplier for calculation of FREQ registers
211  * f * 2^16 * 4 / 40000 = f * 2^12 / 625 (no overflow up to frequencies of
212  * 1048.576 MHz using uint32_t)
213  */
214 #define LO_DIVIDER 4
215 #if (XTAL_FREQ_KHZ == 40000) && (LO_DIVIDER == 4)
216 #define FREQ_DIVIDER 625
217 #define FREQ_MULTIPLIER 4096
218 #else
219 #error Invalid settings for frequency calculation
220 #endif
221 /*---------------------------------------------------------------------------*/
222 #if STATE_USES_MARC_STATE
223 /* We use the MARC_STATE register to poll the chip's status */
224 #define STATE_IDLE CC1200_MARC_STATE_IDLE
225 #define STATE_RX CC1200_MARC_STATE_RX
226 #define STATE_TX CC1200_MARC_STATE_TX
227 #define STATE_RX_FIFO_ERROR CC1200_MARC_STATE_RX_FIFO_ERR
228 #define STATE_TX_FIFO_ERROR CC1200_MARC_STATE_TX_FIFO_ERR
229 #else
230 /* We use the status byte read out using a NOP strobe */
231 #define STATE_IDLE CC1200_STATUS_BYTE_IDLE
232 #define STATE_RX CC1200_STATUS_BYTE_RX
233 #define STATE_TX CC1200_STATUS_BYTE_TX
234 #define STATE_FSTXON CC1200_STATUS_BYTE_FSTXON
235 #define STATE_CALIBRATE CC1200_STATUS_BYTE_CALIBRATE
236 #define STATE_SETTLING CC1200_STATUS_BYTE_SETTLING
237 #define STATE_RX_FIFO_ERR CC1200_STATUS_BYTE_RX_FIFO_ERR
238 #define STATE_TX_FIFO_ERR CC1200_STATUS_BYTE_TX_FIFO_ERR
239 #endif /* #if STATE_USES_MARC_STATE */
240 /*---------------------------------------------------------------------------*/
241 /* Return values for addr_check_auto_ack() */
242 /*---------------------------------------------------------------------------*/
243 /* Frame cannot be parsed / is to short */
244 #define INVALID_FRAME 0
245 /* Address check failed */
246 #define ADDR_CHECK_FAILED 1
247 /* Address check succeeded */
248 #define ADDR_CHECK_OK 2
249 /* Address check succeeded and ACK was send */
250 #define ADDR_CHECK_OK_ACK_SEND 3
251 /*---------------------------------------------------------------------------*/
252 /* Return values for set_channel() */
253 /*---------------------------------------------------------------------------*/
254 /* Channel update was performed */
255 #define CHANNEL_UPDATE_SUCCEEDED 0
256 /* Busy, channel update postponed */
257 #define CHANNEL_UPDATE_POSTPONED 1
258 /* Invalid channel */
259 #define CHANNEL_OUT_OF_LIMITS 2
260 /*---------------------------------------------------------------------------*/
261 /* Various flags indicating the operating state of the radio. See rf_flags */
262 /*---------------------------------------------------------------------------*/
263 /* Radio was initialized (= init() was called) */
264 #define RF_INITIALIZED 0x01
265 /* The radio is on (= not in standby) */
266 #define RF_ON 0x02
267 /* An incoming packet was detected (at least payload length was received */
268 #define RF_RX_PROCESSING_PKT 0x04
269 /* TX is ongoing */
270 #define RF_TX_ACTIVE 0x08
271 /* Channel update required */
272 #define RF_UPDATE_CHANNEL 0x10
273 /* SPI was locked when calling RX interrupt, let the pollhandler do the job */
274 #define RF_POLL_RX_INTERRUPT 0x20
275 /* Ongoing reception */
276 #define RF_RX_ONGOING 0x40
277 /* Force calibration in case we don't use CC1200 AUTOCAL + timeout */
278 #if !CC1200_AUTOCAL
279 #if CC1200_CAL_TIMEOUT_SECONDS
280 #define RF_FORCE_CALIBRATION 0x40
281 #endif
282 #endif
283 /*---------------------------------------------------------------------------*/
284 /* Length of 802.15.4 ACK. We discard packets with a smaller size */
285 #define ACK_LEN 3
286 /*---------------------------------------------------------------------------*/
287 /* This is the way we handle the LEDs */
288 /*---------------------------------------------------------------------------*/
289 #ifdef CC1200_TX_LEDS
290 #define TX_LEDS_ON() leds_on(CC1200_TX_LEDS)
291 #define TX_LEDS_OFF() leds_off(CC1200_TX_LEDS)
292 #else
293 #define TX_LEDS_ON()
294 #define TX_LEDS_OFF()
295 #endif /* #ifdef CC1200_TX_LEDS */
296 
297 #ifdef CC1200_RX_LEDS
298 #define RX_LEDS_ON() leds_on(CC1200_RX_LEDS)
299 #define RX_LEDS_OFF() leds_off(CC1200_RX_LEDS)
300 #else
301 #define RX_LEDS_ON()
302 #define RX_LEDS_OFF()
303 #endif /* #ifdef CC1200_RX_LEDS */
304 /*---------------------------------------------------------------------------*/
305 /*
306  * We have to prevent duplicate SPI access.
307  * We therefore LOCK SPI in order to prevent the rx interrupt to
308  * interfere.
309  */
310 #define LOCK_SPI() do { spi_locked++; } while(0)
311 #define SPI_IS_LOCKED() (spi_locked != 0)
312 #define RELEASE_SPI() do { spi_locked--; } while(0)
313 
314 /*---------------------------------------------------------------------------*/
315 #if CC1200_USE_GPIO2
316 /* Configure GPIO interrupts. GPIO0: falling, GPIO2: rising edge */
317 #define SETUP_GPIO_INTERRUPTS() \
318  do { \
319  cc1200_arch_gpio0_setup_irq(0); \
320  cc1200_arch_gpio2_setup_irq(1); \
321  } while(0)
322 #define ENABLE_GPIO_INTERRUPTS() \
323  do { \
324  cc1200_arch_gpio0_enable_irq(); \
325  cc1200_arch_gpio2_enable_irq(); \
326  } while(0)
327 #define DISABLE_GPIO_INTERRUPTS() \
328  do { \
329  cc1200_arch_gpio0_disable_irq(); \
330  cc1200_arch_gpio2_disable_irq(); \
331  } while(0)
332 #else
333 #define SETUP_GPIO_INTERRUPTS() cc1200_arch_gpio0_setup_irq(0)
334 #define ENABLE_GPIO_INTERRUPTS() cc1200_arch_gpio0_enable_irq()
335 #define DISABLE_GPIO_INTERRUPTS() cc1200_arch_gpio0_disable_irq()
336 #endif /* #if CC1200_USE_GPIO2 */
337 /*---------------------------------------------------------------------------*/
338 /* Debug macros */
339 /*---------------------------------------------------------------------------*/
340 #if DEBUG_LEVEL > 0
341 /* Show all kind of errors e.g. when passing invalid payload length */
342 #define ERROR(...) printf(__VA_ARGS__)
343 #else
344 #define ERROR(...)
345 #endif
346 
347 #if DEBUG_LEVEL > 0
348 /* This macro is used to check if the radio is in a valid state */
349 #define RF_ASSERT(condition) \
350  do { \
351  if(!(condition)) { \
352  printf("RF: Assertion failed in line %d\n", __LINE__); \
353  } \
354  } while(0)
355 #else
356 #define RF_ASSERT(condition)
357 #endif
358 
359 #if DEBUG_LEVEL > 1
360 /* Show warnings e.g. for FIFO errors */
361 #define WARNING(...) printf(__VA_ARGS__)
362 #else
363 #define WARNING(...)
364 #endif
365 
366 #if DEBUG_LEVEL > 2
367 /* We just print out what's going on */
368 #define INFO(...) printf(__VA_ARGS__)
369 #else
370 #define INFO(...)
371 #endif
372 
373 /* Busy-wait (time-bounded) until the radio reaches a given state */
374 #define RTIMER_BUSYWAIT_UNTIL_STATE(s, t) RTIMER_BUSYWAIT_UNTIL(state() == (s), t)
375 
376 /*---------------------------------------------------------------------------*/
377 /* Variables */
378 /*---------------------------------------------------------------------------*/
379 /* Flag indicating whether non-interrupt routines are using SPI */
380 static volatile uint8_t spi_locked = 0;
381 #if CC1200_WITH_TX_BUF
382 /* Packet buffer for transmission, filled within prepare() */
383 static uint8_t tx_pkt[CC1200_MAX_PAYLOAD_LEN];
384 #endif /* CC1200_WITH_TX_BUF */
385 /* The number of bytes waiting in tx_pkt */
386 static uint16_t tx_pkt_len;
387 /* Number of bytes from tx_pkt left to write to FIFO */
388 uint16_t bytes_left_to_write;
389 /* Packet buffer for reception */
390 static uint8_t rx_pkt[CC1200_MAX_PAYLOAD_LEN + APPENDIX_LEN];
391 /* The number of bytes placed in rx_pkt */
392 static volatile uint16_t rx_pkt_len = 0;
393 /*
394  * The current channel in the range CC1200_RF_CHANNEL_MIN
395  * to CC1200_RF_CHANNEL_MAX
396  */
397 static uint8_t rf_channel;
398 /* The next channel requested */
399 static uint8_t new_rf_channel;
400 /* RADIO_PARAM_RX_MODE. Initialized in init() */
401 static radio_value_t rx_mode_value;
402 /* RADIO_PARAM_RX_MODE. Initialized in init() */
403 static radio_value_t tx_mode_value;
404 /* RADIO_PARAM_TXPOWER in dBm. Initialized in init() */
405 static int8_t txpower;
406 static int8_t new_txpower;
407 /* RADIO_PARAM_CCA_THRESHOLD. Initialized in init() */
408 static int8_t cca_threshold;
409 static int8_t new_cca_threshold;
410 /* The radio drivers state */
411 static uint8_t rf_flags = 0;
412 #if !CC1200_AUTOCAL && CC1200_CAL_TIMEOUT_SECONDS
413 /* Use a timeout to decide when to calibrate */
414 static unsigned long cal_timer;
415 #endif
416 #if CC1200_USE_RX_WATCHDOG
417 /* Timer used for RX watchdog */
418 static struct etimer et;
419 #endif /* #if CC1200_USE_RX_WATCHDOG */
420 /*---------------------------------------------------------------------------*/
421 /* Prototypes for Netstack API radio driver functions */
422 /*---------------------------------------------------------------------------*/
423 /* Init the radio. */
424 static int
425 init(void);
426 /* Prepare and copy PHY header to Tx FIFO */
427 static int
428 copy_header_to_tx_fifo(unsigned short payload_len);
429 /* Prepare the radio with a packet to be sent. */
430 static int
431 prepare(const void *payload, unsigned short payload_len);
432 /* Send the packet that has previously been prepared. */
433 static int
434 transmit(unsigned short payload_len);
435 /* Prepare & transmit a packet. */
436 static int
437 send(const void *payload, unsigned short payload_len);
438 /* Read a received packet into a buffer. */
439 static int
440 read(void *buf, unsigned short bufsize);
441 /*
442  * Perform a Clear-Channel Assessment (CCA) to find out if there is
443  * a packet in the air or not.
444  */
445 static int
446 channel_clear(void);
447 /* Check if the radio driver is currently receiving a packet. */
448 static int
449 receiving_packet(void);
450 /* Check if the radio driver has just received a packet. */
451 static int
452 pending_packet(void);
453 /* Turn the radio on. */
454 static int
455 on(void);
456 /* Turn the radio off. */
457 static int
458 off(void);
459 /* Get a radio parameter value. */
460 static radio_result_t
461 get_value(radio_param_t param, radio_value_t *value);
462 /* Set a radio parameter value. */
463 static radio_result_t
464 set_value(radio_param_t param, radio_value_t value);
465 /* Get a radio parameter object. */
466 static radio_result_t
467 get_object(radio_param_t param, void *dest, size_t size);
468 /* Set a radio parameter object. */
469 static radio_result_t
470 set_object(radio_param_t param, const void *src, size_t size);
471 /*---------------------------------------------------------------------------*/
472 /* The radio driver exported to contiki */
473 /*---------------------------------------------------------------------------*/
474 const struct radio_driver cc1200_driver = {
475  init,
476  prepare,
477  transmit,
478  send,
479  read,
483  on,
484  off,
485  get_value,
486  set_value,
487  get_object,
488  set_object
489 };
490 /*---------------------------------------------------------------------------*/
491 /* Prototypes for CC1200 low level function. All of these functions are
492  called by the radio driver functions or the rx interrupt,
493  so there is no need to lock SPI within these functions */
494 /*---------------------------------------------------------------------------*/
495 /* Send a command strobe. */
496 static uint8_t
497 strobe(uint8_t strobe);
498 /* Reset CC1200. */
499 static void
500 reset(void);
501 /* Write a single byte to the specified address. */
502 static uint8_t
503 single_write(uint16_t addr, uint8_t value);
504 /* Read a single byte from the specified address. */
505 static uint8_t
506 single_read(uint16_t addr);
507 /* Write a burst of bytes starting at the specified address. */
508 static void
509 burst_write(uint16_t addr, const uint8_t *data, uint8_t data_len);
510 /* Read a burst of bytes starting at the specified address. */
511 static void
512 burst_read(uint16_t addr, uint8_t *data, uint8_t data_len);
513 /* Write a list of register settings. */
514 static void
515 write_reg_settings(const registerSetting_t *reg_settings,
516  uint16_t sizeof_reg_settings);
517 /* Configure the radio (write basic configuration). */
518 static void
519 configure(void);
520 /* Return the radio's state. */
521 static uint8_t
522 state(void);
523 #if !CC1200_AUTOCAL
524 /* Perform manual calibration. */
525 static void
526 calibrate(void);
527 #endif
528 /* Enter IDLE state. */
529 static void
530 idle(void);
531 /* Enter RX state. */
532 static void
533 idle_calibrate_rx(void);
534 /* Restart RX from within RX interrupt. */
535 static void
536 rx_rx(void);
537 /* Fill TX FIFO (if not already done), start TX and wait for TX to complete (blocking!). */
538 static int
539 idle_tx_rx(const uint8_t *payload, uint16_t payload_len);
540 /* Update TX power */
541 static void
542 update_txpower(int8_t txpower_dbm);
543 /* Update CCA threshold */
544 static void
545 update_cca_threshold(int8_t threshold_dbm);
546 /* Calculate FREQ register from channel */
547 static uint32_t
548 calculate_freq(uint8_t channel);
549 /* Update rf channel if possible, else postpone it (-> pollhandler). */
550 static int
551 set_channel(uint8_t channel);
552 /* Validate address and send ACK if requested. */
553 static int
554 addr_check_auto_ack(uint8_t *frame, uint16_t frame_len);
555 /*---------------------------------------------------------------------------*/
556 /* Handle tasks left over from rx interrupt or because SPI was locked */
557 static void pollhandler(void);
558 /*---------------------------------------------------------------------------*/
559 PROCESS(cc1200_process, "CC1200 driver");
560 /*---------------------------------------------------------------------------*/
561 PROCESS_THREAD(cc1200_process, ev, data)
562 {
563 
564  PROCESS_POLLHANDLER(pollhandler());
565 
566  PROCESS_BEGIN();
567 
568 #if CC1200_USE_RX_WATCHDOG
569  while(1) {
570 
571  if((rf_flags & (RF_ON | RF_TX_ACTIVE)) == RF_ON) {
572 
574  etimer_reset(&et);
575 
576  /*
577  * We are on and not in TX. As every function of this driver
578  * assures that we are in RX mode
579  * (using RTIMER_BUSYWAIT_UNTIL_STATE(STATE_RX, ...) construct) in
580  * either rx_rx(), idle_calibrate_rx() or transmit(),
581  * something probably went wrong in the rx interrupt handler
582  * if we are not in RX at this point.
583  */
584 
585  if(cc1200_arch_gpio0_read_pin() == 0) {
586 
587  /*
588  * GPIO de-asserts as soon as we leave RX for what reason ever. No
589  * reason to check RX as long as it is asserted (we are receiving a
590  * packet). We should never interfere with the rx interrupt if we
591  * check GPIO0 in advance...
592  */
593 
594  LOCK_SPI();
595  if(state() != STATE_RX) {
596  WARNING("RF: RX watchdog triggered!\n");
597  rx_rx();
598  }
599  RELEASE_SPI();
600 
601  }
602 
603  } else {
604  PROCESS_YIELD();
605  }
606 
607  }
608 #endif /* #if CC1200_USE_RX_WATCHDOG */
609 
610  PROCESS_YIELD_UNTIL(ev == PROCESS_EVENT_EXIT);
611 
612  PROCESS_END();
613 
614 }
615 /*---------------------------------------------------------------------------*/
616 /* Handle tasks left over from rx interrupt or because SPI was locked */
617 static void
618 pollhandler(void)
619 {
620 
621  if((rf_flags & (RF_ON + RF_POLL_RX_INTERRUPT)) ==
622  (RF_ON + RF_POLL_RX_INTERRUPT)) {
623  cc1200_rx_interrupt();
624  }
625 
626  if(rf_flags & RF_UPDATE_CHANNEL) {
627  /* We couldn't set the channel because we were busy. Try again now. */
628  set_channel(new_rf_channel);
629  }
630 
631  if((rx_mode_value & RADIO_RX_MODE_POLL_MODE) == 0 && rx_pkt_len > 0) {
632 
633  int len;
634 
635  /*
636  * We received a valid packet. CRC was checked before,
637  * address filtering was performed (if configured)
638  * and ACK was send (if configured)
639  */
640 
641  packetbuf_clear();
643 
644  if(len > 0) {
646  NETSTACK_MAC.input();
647  }
648 
649  }
650 
651 }
652 /*---------------------------------------------------------------------------*/
653 /*---------------------------------------------------------------------------*/
654 /*
655  * Netstack API radio driver functions
656  */
657 /*---------------------------------------------------------------------------*/
658 /*---------------------------------------------------------------------------*/
659 
660 /* Initialize radio. */
661 static int
662 init(void)
663 {
664 
665  INFO("RF: Init (%s)\n", CC1200_RF_CFG.cfg_descriptor);
666 
667  if(!(rf_flags & RF_INITIALIZED)) {
668 
669  LOCK_SPI();
670 
671  /* Perform low level initialization */
672  cc1200_arch_init();
673 
674  /* Configure GPIO interrupts */
675  SETUP_GPIO_INTERRUPTS();
676 
677  /* Write initial configuration */
678  configure();
679 
680  /* Enable address filtering + auto ack */
682 
683  /* Enable CCA */
684  tx_mode_value = (RADIO_TX_MODE_SEND_ON_CCA);
685 
686  /* Set output power */
687  new_txpower = CC1200_RF_CFG.max_txpower;
688  update_txpower(new_txpower);
689 
690  /* Adjust CAA threshold */
691  new_cca_threshold = CC1200_RF_CFG.cca_threshold;
692  update_cca_threshold(new_cca_threshold);
693 
694  process_start(&cc1200_process, NULL);
695 
696  /* We are on + initialized at this point */
697  rf_flags |= (RF_INITIALIZED + RF_ON);
698 
699  RELEASE_SPI();
700 
701  /* Set default channel. This will also force initial calibration! */
702  set_channel(CC1200_DEFAULT_CHANNEL);
703 
704  /*
705  * We have to call off() before on() because on() relies on the
706  * configuration of the GPIO0 pin
707  */
708  off();
709  }
710 
711  return 1;
712 
713 }
714 /*---------------------------------------------------------------------------*/
715 /* Prepare the radio with a packet to be sent. */
716 static int
717 prepare(const void *payload, unsigned short payload_len)
718 {
719 
720  INFO("RF: Prepare (%d)\n", payload_len);
721 
722  if((payload_len < ACK_LEN) ||
723  (payload_len > CC1200_MAX_PAYLOAD_LEN)) {
724  ERROR("RF: Invalid payload length!\n");
725  return RADIO_TX_ERR;
726  }
727 
728  tx_pkt_len = payload_len;
729 
730 #if CC1200_WITH_TX_BUF
731  /* Copy payload to buffer, will be sent later */
732  memcpy(tx_pkt, payload, tx_pkt_len);
733 #else /* CC1200_WITH_TX_BUF */
734 #if (CC1200_MAX_PAYLOAD_LEN > (CC1200_FIFO_SIZE - PHR_LEN))
735 #error CC1200 max payload too large
736 #else
737  /* Copy header and payload directly to Radio Tx FIFO (127 bytes max) */
738  copy_header_to_tx_fifo(payload_len);
739  burst_write(CC1200_TXFIFO, payload, payload_len);
740 #endif
741 #endif /* CC1200_WITH_TX_BUF */
742 
743  return RADIO_TX_OK;
744 }
745 /*---------------------------------------------------------------------------*/
746 /* Prepare the radio with a packet to be sent. */
747 static int
748 copy_header_to_tx_fifo(unsigned short payload_len)
749 {
750 #if CC1200_802154G
751  /* Prepare PHR for 802.15.4g frames */
752  struct {
753  uint8_t phra;
754  uint8_t phrb;
755  } phr;
756 #if CC1200_802154G_CRC16
757  payload_len += 2;
758 #else
759  payload_len += 4;
760 #endif
761  /* Frame length */
762  phr.phrb = (uint8_t)(payload_len & 0x00FF);
763  phr.phra = (uint8_t)((payload_len >> 8) & 0x0007);
764 #if CC1200_802154G_WHITENING
765  /* Enable Whitening */
766  phr.phra |= (1 << 3);
767 #endif /* #if CC1200_802154G_WHITENING */
768 #if CC1200_802154G_CRC16
769  /* FCS type 1, 2 Byte CRC */
770  phr.phra |= (1 << 4);
771 #endif /* #if CC1200_802154G_CRC16 */
772 #endif /* #if CC1200_802154G */
773 
774  idle();
775 
776  rf_flags &= ~RF_RX_PROCESSING_PKT;
777  strobe(CC1200_SFRX);
778  /* Flush TX FIFO */
779  strobe(CC1200_SFTX);
780 
781 #if CC1200_802154G
782  /* Write PHR */
783  burst_write(CC1200_TXFIFO, (uint8_t *)&phr, PHR_LEN);
784 #else
785  /* Write length byte */
786  burst_write(CC1200_TXFIFO, (uint8_t *)&payload_len, PHR_LEN);
787 #endif /* #if CC1200_802154G */
788 
789  return 0;
790 }
791 /*---------------------------------------------------------------------------*/
792 /* Send the packet that has previously been prepared. */
793 static int
794 transmit(unsigned short transmit_len)
795 {
796 
797  uint8_t was_off = 0;
798  int ret = RADIO_TX_OK;
799  int txret;
800 
801  INFO("RF: Transmit (%d)\n", transmit_len);
802 
803  if(transmit_len != tx_pkt_len) {
804  ERROR("RF: TX length mismatch!\n");
805  return RADIO_TX_ERR;
806  }
807 
808  /* TX ongoing. Inhibit channel update & ACK as soon as possible */
809  rf_flags |= RF_TX_ACTIVE;
810 
811  if(!(rf_flags & RF_ON)) {
812  /* Radio is off - turn it on */
813  was_off = 1;
814  on();
815  /* Radio is in RX now (and calibrated...) */
816  }
817 
818  if(tx_mode_value & RADIO_TX_MODE_SEND_ON_CCA) {
819  /* Perform clear channel assessment */
820  if(!channel_clear()) {
821  /* Channel occupied */
822  if(was_off) {
823  off();
824  }
825  rf_flags &= ~RF_TX_ACTIVE;
826  return RADIO_TX_COLLISION;
827  }
828  }
829 
830  /*
831  * Lock SPI here because "on()" and "channel_clear()"
832  * won't work if SPI is locked!
833  */
834  LOCK_SPI();
835 
836  /*
837  * Make sure we start from a sane state. idle() also disables
838  * the GPIO interrupt(s).
839  */
840  idle();
841 
842  /* Update output power */
843  if(new_txpower != txpower) {
844  update_txpower(new_txpower);
845  }
846 
847 #if !CC1200_AUTOCAL
848  /* Perform manual calibration unless just turned on */
849  if(!was_off) {
850  calibrate();
851  }
852 #endif
853 
854  /* Send data using TX FIFO */
855 #if CC1200_WITH_TX_BUF
856  txret = idle_tx_rx(tx_pkt, tx_pkt_len);
857 #else /* CC1200_WITH_TX_BUF */
858  txret = idle_tx_rx(NULL, tx_pkt_len);
859 #endif /* CC1200_WITH_TX_BUF */
860  if(txret == RADIO_TX_OK) {
861 
862  /*
863  * TXOFF_MODE is set to RX,
864  * let's wait until we are in RX and turn on the GPIO IRQs
865  * again as they were turned off in idle()
866  */
867 
868  RTIMER_BUSYWAIT_UNTIL_STATE(STATE_RX,
869  CC1200_RF_CFG.tx_rx_turnaround);
870 
871  ENABLE_GPIO_INTERRUPTS();
872 
873  } else {
874 
875  /*
876  * Something went wrong during TX, idle_tx_rx() returns in IDLE
877  * state in this case.
878  * Turn on RX again unless we turn off anyway
879  */
880 
881  ret = RADIO_TX_ERR;
882  if(!was_off) {
883 #ifdef RF_FORCE_CALIBRATION
884  rf_flags |= RF_FORCE_CALIBRATION;
885 #endif
886  idle_calibrate_rx();
887  }
888  }
889 
890  /* Release SPI here because "off()" won't work if SPI is locked! */
891  RELEASE_SPI();
892 
893  if(was_off) {
894  off();
895  }
896 
897  /* TX completed */
898  rf_flags &= ~RF_TX_ACTIVE;
899 
900  return ret;
901 
902 }
903 /*---------------------------------------------------------------------------*/
904 /* Prepare & transmit a packet. */
905 static int
906 send(const void *payload, unsigned short payload_len)
907 {
908 
909  int ret;
910 
911  INFO("RF: Send (%d)\n", payload_len);
912 
913  /* payload_len checked within prepare() */
914  if((ret = prepare(payload, payload_len)) == RADIO_TX_OK) {
915  ret = transmit(payload_len);
916  }
917 
918  return ret;
919 
920 }
921 /*---------------------------------------------------------------------------*/
922 /* Read a received packet into a buffer. */
923 static int
924 read(void *buf, unsigned short buf_len)
925 {
926 
927  int len = 0;
928 
929  if(rx_pkt_len > 0) {
930 
931  rssi = (int8_t)rx_pkt[rx_pkt_len - 2] + (int)CC1200_RF_CFG.rssi_offset;
932  /* CRC is already checked */
933  uint8_t crc_lqi = rx_pkt[rx_pkt_len - 1];
934 
935  len = rx_pkt_len - APPENDIX_LEN;
936 
937  if(len > buf_len) {
938 
939  ERROR("RF: Failed to read packet (too big)!\n");
940 
941  } else {
942 
943  INFO("RF: Read (%d bytes, %d dBm)\n", len, rssi);
944 
945  memcpy((void *)buf, (const void *)rx_pkt, len);
946 
947  /* Release rx_pkt */
948  rx_pkt_len = 0;
949 
950  packetbuf_set_attr(PACKETBUF_ATTR_RSSI, rssi);
951  /* Mask out CRC bit */
952  packetbuf_set_attr(PACKETBUF_ATTR_LINK_QUALITY,
953  crc_lqi & ~(1 << 7));
954  }
955 
956  }
957 
958  return len;
959 
960 }
961 /*---------------------------------------------------------------------------*/
962 /*
963  * Perform a Clear-Channel Assessment (CCA) to find out if there is a
964  * packet in the air or not.
965  */
966 static int
967 channel_clear(void)
968 {
969 
970  uint8_t cca, was_off = 0;
971 
972  if(SPI_IS_LOCKED()) {
973  /* Probably locked in rx interrupt. Return "channel occupied" */
974  return 0;
975  }
976 
977  if(!(rf_flags & RF_ON)) {
978  /* We are off */
979  was_off = 1;
980  on();
981  }
982 
983  LOCK_SPI();
984 
985  RF_ASSERT(state() == STATE_RX);
986 
987  /*
988  * At this point we should be in RX. If GPIO0 is set, we are currently
989  * receiving a packet, no need to check the RSSI. Or is there any situation
990  * when we want access the channel even if we are currently receiving a
991  * packet???
992  */
993 
994  if(cc1200_arch_gpio0_read_pin() == 1) {
995  /* Channel occupied */
996  INFO("RF: CCA (0)\n");
997  cca = 0;
998  } else {
999 
1000  uint8_t rssi0;
1001 
1002  /* Update CCA threshold */
1003  if(new_cca_threshold != cca_threshold) {
1004  update_cca_threshold(new_cca_threshold);
1005  }
1006 
1007  /* Wait for CARRIER_SENSE_VALID signal */
1008  RTIMER_BUSYWAIT_UNTIL(((rssi0 = single_read(CC1200_RSSI0))
1009  & CC1200_CARRIER_SENSE_VALID),
1010  RTIMER_SECOND / 100);
1011  RF_ASSERT(rssi0 & CC1200_CARRIER_SENSE_VALID);
1012 
1013  if(rssi0 & CC1200_CARRIER_SENSE) {
1014  /* Channel occupied */
1015  INFO("RF: CCA (0)\n");
1016  cca = 0;
1017  } else {
1018  /* Channel clear */
1019  INFO("RF: CCA (1)\n");
1020  cca = 1;
1021  }
1022 
1023  }
1024 
1025  RELEASE_SPI();
1026 
1027  if(was_off) {
1028  off();
1029  }
1030 
1031  return cca;
1032 
1033 }
1034 /*---------------------------------------------------------------------------*/
1035 /*
1036  * Check if the radio driver is currently receiving a packet.
1037  *
1038  * CSMA uses this function
1039  * - to detect a collision before transmit()
1040  * - to detect an incoming ACK
1041  */
1042 static int
1043 receiving_packet(void)
1044 {
1045 
1046  int ret = 0;
1047 
1048  if((rf_flags & (RF_ON | RF_TX_ACTIVE)) == RF_ON) {
1049  /* We are on and not in TX */
1050  if((cc1200_arch_gpio0_read_pin() == 1) || (rx_pkt_len != 0)) {
1051 
1052  /*
1053  * SYNC word found or packet just received. Changing the criteria
1054  * for this event might make it necessary to review the MAC timing
1055  * parameters! Instead of (or in addition to) using GPIO0 we could also
1056  * read out MODEM_STATUS1 (e.g. PQT reached), but this would not change
1057  * the situation at least for CSMA as it uses two "blocking" timers
1058  * (does not perform polling...). Therefore the overall timing
1059  * of the ACK handling wouldn't change. It would just allow to detect an
1060  * incoming packet a little bit earlier and help us with respect to
1061  * collision avoidance (why not use channel_clear()
1062  * at this point?).
1063  */
1064 
1065  ret = 1;
1066 
1067  }
1068  }
1069 
1070  INFO("RF: Receiving (%d)\n", ret);
1071  return ret;
1072 
1073 }
1074 /*---------------------------------------------------------------------------*/
1075 /* Check if the radio driver has just received a packet. */
1076 static int
1077 pending_packet(void)
1078 {
1079  int ret;
1080  ret = ((rx_pkt_len != 0) ? 1 : 0);
1081  if(ret == 0 && !SPI_IS_LOCKED()) {
1082  LOCK_SPI();
1083  ret = (single_read(CC1200_NUM_RXBYTES) > 0);
1084  RELEASE_SPI();
1085  }
1086 
1087  INFO("RF: Pending (%d)\n", ret);
1088  return ret;
1089 
1090 }
1091 /*---------------------------------------------------------------------------*/
1092 /* Turn the radio on. */
1093 static int
1094 on(void)
1095 {
1096 
1097  INFO("RF: On\n");
1098 
1099  /* Don't turn on if we are on already */
1100  if(!(rf_flags & RF_ON)) {
1101 
1102  if(SPI_IS_LOCKED()) {
1103  return 0;
1104  }
1105 
1106  LOCK_SPI();
1107 
1108  /* Wake-up procedure. Wait for GPIO0 to de-assert (CHIP_RDYn) */
1109  cc1200_arch_spi_select();
1110  RTIMER_BUSYWAIT_UNTIL((cc1200_arch_gpio0_read_pin() == 0),
1111  RTIMER_SECOND / 100);
1112  RF_ASSERT((cc1200_arch_gpio0_read_pin() == 0));
1113  cc1200_arch_spi_deselect();
1114 
1115  rf_flags = RF_INITIALIZED;
1116  rf_flags |= RF_ON;
1117 
1118  /* Radio is IDLE now, re-configure GPIO0 (modified inside off()) */
1119  single_write(CC1200_IOCFG0, GPIO0_IOCFG);
1120 
1121  /* Turn on RX */
1122  idle_calibrate_rx();
1123 
1124  RELEASE_SPI();
1125 
1126 #if CC1200_USE_RX_WATCHDOG
1127  PROCESS_CONTEXT_BEGIN(&cc1200_process);
1128  etimer_set(&et, CLOCK_SECOND);
1129  PROCESS_CONTEXT_END(&cc1200_process);
1130 #endif /* #if CC1200_USE_RX_WATCHDOG */
1131 
1132  } else {
1133  INFO("RF: Already on\n");
1134  }
1135 
1136  return 1;
1137 
1138 }
1139 /*---------------------------------------------------------------------------*/
1140 /* Turn the radio off. */
1141 static int
1142 off(void)
1143 {
1144 
1145  INFO("RF: Off\n");
1146 
1147  /* Don't turn off if we are off already */
1148  if(rf_flags & RF_ON) {
1149 
1150  if(SPI_IS_LOCKED()) {
1151  return 0;
1152  }
1153 
1154  LOCK_SPI();
1155 
1156  idle();
1157 
1158  if(single_read(CC1200_NUM_RXBYTES) > 0) {
1159  RELEASE_SPI();
1160  /* In case there is something in the Rx FIFO, read it */
1161  cc1200_rx_interrupt();
1162  if(SPI_IS_LOCKED()) {
1163  return 0;
1164  }
1165  LOCK_SPI();
1166  }
1167 
1168  /*
1169  * As we use GPIO as CHIP_RDYn signal on wake-up / on(),
1170  * we re-configure it for CHIP_RDYn.
1171  */
1172  single_write(CC1200_IOCFG0, CC1200_IOCFG_RXFIFO_CHIP_RDY_N);
1173 
1174  /* Say goodbye ... */
1175  strobe(CC1200_SPWD);
1176 
1177  /* Clear all but the initialized flag */
1178  rf_flags = RF_INITIALIZED;
1179 
1180  RELEASE_SPI();
1181 
1182 #if CC1200_USE_RX_WATCHDOG
1183  etimer_stop(&et);
1184 #endif /* #if CC1200_USE_RX_WATCHDOG */
1185 
1186  } else {
1187  INFO("RF: Already off\n");
1188  }
1189 
1190  return 1;
1191 
1192 }
1193 /*---------------------------------------------------------------------------*/
1194 /**
1195  * \brief Reads the current signal strength (RSSI)
1196  * \return The current RSSI in dBm
1197  *
1198  * This function reads the current RSSI on the currently configured
1199  * channel.
1200  */
1201 static int16_t
1202 get_rssi(void)
1203 {
1204  int16_t rssi0, rssi1;
1205  uint8_t was_off = 0;
1206 
1207  /* If we are off, turn on first */
1208  if(!(rf_flags & RF_ON)) {
1209  was_off = 1;
1210  on();
1211  }
1212 
1213  /* Wait for CARRIER_SENSE_VALID signal */
1214  RTIMER_BUSYWAIT_UNTIL(((rssi0 = single_read(CC1200_RSSI0))
1215  & CC1200_CARRIER_SENSE_VALID),
1216  RTIMER_SECOND / 100);
1217  RF_ASSERT(rssi0 & CC1200_CARRIER_SENSE_VALID);
1218  rssi1 = (int8_t)single_read(CC1200_RSSI1) + (int)CC1200_RF_CFG.rssi_offset;
1219 
1220  /* If we were off, turn back off */
1221  if(was_off) {
1222  off();
1223  }
1224 
1225  return rssi1;
1226 }
1227 /*---------------------------------------------------------------------------*/
1228 /* Get a radio parameter value. */
1229 static radio_result_t
1230 get_value(radio_param_t param, radio_value_t *value)
1231 {
1232 
1233  if(!value) {
1235  }
1236 
1237  switch(param) {
1239 
1240  if(rf_flags & RF_ON) {
1242  } else {
1244  }
1245  return RADIO_RESULT_OK;
1246 
1247  case RADIO_PARAM_CHANNEL:
1248 
1249  *value = (radio_value_t)rf_channel;
1250  return RADIO_RESULT_OK;
1251 
1252  case RADIO_PARAM_PAN_ID:
1254 
1256 
1257  case RADIO_PARAM_RX_MODE:
1258 
1259  *value = (radio_value_t)rx_mode_value;
1260  return RADIO_RESULT_OK;
1261 
1262  case RADIO_PARAM_TX_MODE:
1263 
1264  *value = (radio_value_t)tx_mode_value;
1265  return RADIO_RESULT_OK;
1266 
1267  case RADIO_PARAM_TXPOWER:
1268 
1269  *value = (radio_value_t)txpower;
1270  return RADIO_RESULT_OK;
1271 
1273 
1274  *value = (radio_value_t)cca_threshold;
1275  return RADIO_RESULT_OK;
1276 
1277  case RADIO_PARAM_RSSI:
1278  *value = get_rssi();
1279  return RADIO_RESULT_OK;
1280 
1281  case RADIO_PARAM_LAST_RSSI:
1282  *value = (radio_value_t)rssi;
1283  return RADIO_RESULT_OK;
1284 
1286 
1288 
1290 
1291  *value = (radio_value_t)CC1200_RF_CFG.min_channel;
1292  return RADIO_RESULT_OK;
1293 
1295 
1296  *value = (radio_value_t)CC1200_RF_CFG.max_channel;
1297  return RADIO_RESULT_OK;
1298 
1300 
1301  *value = (radio_value_t)CC1200_CONST_TX_POWER_MIN;
1302  return RADIO_RESULT_OK;
1303 
1305 
1306  *value = (radio_value_t)CC1200_RF_CFG.max_txpower;
1307  return RADIO_RESULT_OK;
1308 
1310 #if CC1200_802154G
1311 #if CC1200_802154G_CRC16
1312  *value = (radio_value_t)4; /* 2 bytes PHR, 2 bytes CRC */
1313 #else
1314  *value = (radio_value_t)6; /* 2 bytes PHR, 4 bytes CRC */
1315 #endif
1316 #else
1317  *value = (radio_value_t)3; /* 1 len byte, 2 bytes CRC */
1318 #endif
1319  return RADIO_RESULT_OK;
1320 
1322  *value = (radio_value_t)8*1000*1000 / CC1200_RF_CFG.bitrate;
1323  return RADIO_RESULT_OK;
1324 
1326  *value = (radio_value_t)CC1200_RF_CFG.delay_before_tx;
1327  return RADIO_RESULT_OK;
1328 
1330  *value = (radio_value_t)CC1200_RF_CFG.delay_before_rx;
1331  return RADIO_RESULT_OK;
1332 
1334  *value = (radio_value_t)CC1200_RF_CFG.delay_before_detect;
1335  return RADIO_RESULT_OK;
1336 
1337  case RADIO_CONST_MAX_PAYLOAD_LEN:
1338  *value = (radio_value_t)CC1200_MAX_PAYLOAD_LEN;
1339  return RADIO_RESULT_OK;
1340 
1341  default:
1342 
1344 
1345  }
1346 
1347 }
1348 /*---------------------------------------------------------------------------*/
1349 /* Set a radio parameter value. */
1350 static radio_result_t
1351 set_value(radio_param_t param, radio_value_t value)
1352 {
1353 
1354  switch(param) {
1356 
1357  if(value == RADIO_POWER_MODE_ON) {
1358  on();
1359  return RADIO_RESULT_OK;
1360  }
1361 
1362  if(value == RADIO_POWER_MODE_OFF) {
1363  off();
1364  return RADIO_RESULT_OK;
1365  }
1366 
1368 
1369  case RADIO_PARAM_CHANNEL:
1370 
1371  if(set_channel(value) == CHANNEL_OUT_OF_LIMITS) {
1373  }
1374 
1375  /*
1376  * We always return OK here even if the channel update was
1377  * postponed. rf_channel is NOT updated in this case until
1378  * the channel update was performed. So reading back
1379  * the channel using get_value() might return the "old" channel
1380  * until the channel was actually changed
1381  */
1382 
1383  return RADIO_RESULT_OK;
1384 
1385  case RADIO_PARAM_PAN_ID:
1387 
1389 
1390  case RADIO_PARAM_RX_MODE:
1391 
1392  rx_mode_value = value;
1393  return RADIO_RESULT_OK;
1394 
1395  case RADIO_PARAM_TX_MODE:
1396 
1397  tx_mode_value = value;
1398  return RADIO_RESULT_OK;
1399 
1400  case RADIO_PARAM_TXPOWER:
1401 
1402  if(value > (radio_value_t)CC1200_RF_CFG.max_txpower) {
1403  value = (radio_value_t)CC1200_RF_CFG.max_txpower;
1404  }
1405 
1406  if(value < (radio_value_t)CC1200_CONST_TX_POWER_MIN) {
1407  value = (radio_value_t)CC1200_CONST_TX_POWER_MIN;
1408  }
1409 
1410  /* We update the output power as soon as we transmit the next packet */
1411  new_txpower = (int8_t)value;
1412  return RADIO_RESULT_OK;
1413 
1415 
1416  if(value > (radio_value_t)CC1200_CONST_CCA_THRESHOLD_MAX) {
1417  value = (radio_value_t)CC1200_CONST_CCA_THRESHOLD_MAX;
1418  }
1419 
1420  if(value < (radio_value_t)CC1200_CONST_CCA_THRESHOLD_MIN) {
1421  value = (radio_value_t)CC1200_CONST_CCA_THRESHOLD_MIN;
1422  }
1423 
1424  /* When to update the threshold? Let's do it in channel_clear() ... */
1425  new_cca_threshold = (int8_t)value;
1426  return RADIO_RESULT_OK;
1427 
1428  case RADIO_PARAM_RSSI:
1430 
1431  default:
1432 
1434 
1435  }
1436 
1437 }
1438 /*---------------------------------------------------------------------------*/
1439 /* Get a radio parameter object. */
1440 static radio_result_t
1441 get_object(radio_param_t param, void *dest, size_t size)
1442 {
1443  if(param == RADIO_PARAM_LAST_PACKET_TIMESTAMP) {
1444  if(size != sizeof(rtimer_clock_t) || !dest) {
1446  }
1447  *(rtimer_clock_t *)dest = sfd_timestamp;
1448  return RADIO_RESULT_OK;
1449  }
1450 
1451 #if MAC_CONF_WITH_TSCH
1452  if(param == RADIO_CONST_TSCH_TIMING) {
1453  if(size != sizeof(uint16_t *) || !dest) {
1455  }
1456  /* Assigned value: a pointer to the TSCH timing in usec */
1457  *(const uint16_t **)dest = CC1200_RF_CFG.tsch_timing;
1458  return RADIO_RESULT_OK;
1459  }
1460 #endif /* MAC_CONF_WITH_TSCH */
1461 
1463 
1464 }
1465 /*---------------------------------------------------------------------------*/
1466 /* Set a radio parameter object. */
1467 static radio_result_t
1468 set_object(radio_param_t param, const void *src, size_t size)
1469 {
1470 
1472 
1473 }
1474 /*---------------------------------------------------------------------------*/
1475 /*---------------------------------------------------------------------------*/
1476 /*
1477  * CC1200 low level functions
1478  */
1479 /*---------------------------------------------------------------------------*/
1480 /*---------------------------------------------------------------------------*/
1481 
1482 /* Send a command strobe. */
1483 static uint8_t
1484 strobe(uint8_t strobe)
1485 {
1486 
1487  uint8_t ret;
1488 
1489  cc1200_arch_spi_select();
1490  ret = cc1200_arch_spi_rw_byte(strobe);
1491  cc1200_arch_spi_deselect();
1492 
1493  return ret;
1494 
1495 }
1496 /*---------------------------------------------------------------------------*/
1497 /* Reset CC1200. */
1498 static void
1499 reset(void)
1500 {
1501 
1502  cc1200_arch_spi_select();
1503  cc1200_arch_spi_rw_byte(CC1200_SRES);
1504  /*
1505  * Here we should wait for SO to go low again.
1506  * As we don't have access to this pin we just wait for 100µs.
1507  */
1508  clock_delay(100);
1509  cc1200_arch_spi_deselect();
1510 
1511 }
1512 /*---------------------------------------------------------------------------*/
1513 /* Write a single byte to the specified address. */
1514 static uint8_t
1515 single_write(uint16_t addr, uint8_t val)
1516 {
1517 
1518  uint8_t ret;
1519 
1520  cc1200_arch_spi_select();
1521  if(CC1200_IS_EXTENDED_ADDR(addr)) {
1522  cc1200_arch_spi_rw_byte(CC1200_EXTENDED_WRITE_CMD);
1523  cc1200_arch_spi_rw_byte((uint8_t)addr);
1524  } else {
1525  cc1200_arch_spi_rw_byte(addr | CC1200_WRITE_BIT);
1526  }
1527  ret = cc1200_arch_spi_rw_byte(val);
1528  cc1200_arch_spi_deselect();
1529 
1530  return ret;
1531 
1532 }
1533 /*---------------------------------------------------------------------------*/
1534 /* Read a single byte from the specified address. */
1535 static uint8_t
1536 single_read(uint16_t addr)
1537 {
1538 
1539  uint8_t ret;
1540 
1541  cc1200_arch_spi_select();
1542  if(CC1200_IS_EXTENDED_ADDR(addr)) {
1543  cc1200_arch_spi_rw_byte(CC1200_EXTENDED_READ_CMD);
1544  cc1200_arch_spi_rw_byte((uint8_t)addr);
1545  } else {
1546  cc1200_arch_spi_rw_byte(addr | CC1200_READ_BIT);
1547  }
1548  ret = cc1200_arch_spi_rw_byte(0);
1549  cc1200_arch_spi_deselect();
1550 
1551  return ret;
1552 
1553 }
1554 /*---------------------------------------------------------------------------*/
1555 /* Write a burst of bytes starting at the specified address. */
1556 static void
1557 burst_write(uint16_t addr, const uint8_t *data, uint8_t data_len)
1558 {
1559 
1560  cc1200_arch_spi_select();
1561  if(CC1200_IS_EXTENDED_ADDR(addr)) {
1562  cc1200_arch_spi_rw_byte(CC1200_EXTENDED_BURST_WRITE_CMD);
1563  cc1200_arch_spi_rw_byte((uint8_t)addr);
1564  } else {
1565  cc1200_arch_spi_rw_byte(addr | CC1200_WRITE_BIT | CC1200_BURST_BIT);
1566  }
1567  cc1200_arch_spi_rw(NULL, data, data_len);
1568  cc1200_arch_spi_deselect();
1569 
1570 }
1571 /*---------------------------------------------------------------------------*/
1572 /* Read a burst of bytes starting at the specified address. */
1573 static void
1574 burst_read(uint16_t addr, uint8_t *data, uint8_t data_len)
1575 {
1576 
1577  cc1200_arch_spi_select();
1578  if(CC1200_IS_EXTENDED_ADDR(addr)) {
1579  cc1200_arch_spi_rw_byte(CC1200_EXTENDED_BURST_READ_CMD);
1580  cc1200_arch_spi_rw_byte((uint8_t)addr);
1581  } else {
1582  cc1200_arch_spi_rw_byte(addr | CC1200_READ_BIT | CC1200_BURST_BIT);
1583  }
1584  cc1200_arch_spi_rw(data, NULL, data_len);
1585  cc1200_arch_spi_deselect();
1586 
1587 }
1588 /*---------------------------------------------------------------------------*/
1589 /* Write a list of register settings. */
1590 static void
1591 write_reg_settings(const registerSetting_t *reg_settings,
1592  uint16_t sizeof_reg_settings)
1593 {
1594 
1595  int i = sizeof_reg_settings / sizeof(registerSetting_t);
1596 
1597  if(reg_settings != NULL) {
1598  while(i--) {
1599  single_write(reg_settings->addr,
1600  reg_settings->val);
1601  reg_settings++;
1602  }
1603  }
1604 
1605 }
1606 /*---------------------------------------------------------------------------*/
1607 /* Configure the radio (write basic configuration). */
1608 static void
1609 configure(void)
1610 {
1611 
1612  uint8_t reg;
1613 #if CC1200_RF_TESTMODE
1614  uint32_t freq;
1615 #endif
1616 
1617  /*
1618  * As we only write registers which are different from the chip's reset
1619  * state, let's assure that the chip is in a clean state
1620  */
1621  reset();
1622 
1623  /* Write the configuration as exported from SmartRF Studio */
1624  write_reg_settings(CC1200_RF_CFG.register_settings,
1625  CC1200_RF_CFG.size_of_register_settings);
1626 
1627  /* Write frequency offset */
1628 #if CC1200_FREQ_OFFSET
1629  /* MSB */
1630  single_write(CC1200_FREQOFF1, (uint8_t)(CC1200_FREQ_OFFSET >> 8));
1631  /* LSB */
1632  single_write(CC1200_FREQOFF0, (uint8_t)(CC1200_FREQ_OFFSET));
1633 #endif
1634 
1635  /* RSSI offset */
1636  single_write(CC1200_AGC_GAIN_ADJUST, (int8_t)CC1200_RF_CFG.rssi_offset);
1637 
1638  /***************************************************************************
1639  * RF test modes needed during hardware development
1640  **************************************************************************/
1641 
1642 #if (CC1200_RF_TESTMODE == 1) || (CC1200_RF_TESTMODE == 2)
1643 
1644  strobe(CC1200_SFTX);
1645  single_write(CC1200_TXFIRST, 0);
1646  single_write(CC1200_TXLAST, 0xFF);
1647  update_txpower(CC1200_CONST_TX_POWER_MAX);
1648  single_write(CC1200_PKT_CFG2, 0x02);
1649  freq = calculate_freq(CC1200_DEFAULT_CHANNEL - CC1200_RF_CFG.min_channel);
1650  single_write(CC1200_FREQ0, ((uint8_t *)&freq)[0]);
1651  single_write(CC1200_FREQ1, ((uint8_t *)&freq)[1]);
1652  single_write(CC1200_FREQ2, ((uint8_t *)&freq)[2]);
1653 
1654  printf("RF: Freq0 0x%02x\n", ((uint8_t *)&freq)[0]);
1655  printf("RF: Freq1 0x%02x\n", ((uint8_t *)&freq)[1]);
1656  printf("RF: Freq2 0x%02x\n", ((uint8_t *)&freq)[2]);
1657 
1658 #if (CC1200_RF_TESTMODE == 1)
1659  single_write(CC1200_SYNC_CFG1, 0xE8);
1660  single_write(CC1200_PREAMBLE_CFG1, 0x00);
1661  single_write(CC1200_MDMCFG1, 0x46);
1662  single_write(CC1200_PKT_CFG0, 0x40);
1663  single_write(CC1200_FS_DIG1, 0x07);
1664  single_write(CC1200_FS_DIG0, 0xAA);
1665  single_write(CC1200_FS_DVC1, 0xFF);
1666  single_write(CC1200_FS_DVC0, 0x17);
1667 #endif
1668 
1669 #if (CC1200_RF_TESTMODE == 2)
1670  single_write(CC1200_SYNC_CFG1, 0xE8);
1671  single_write(CC1200_PREAMBLE_CFG1, 0x00);
1672  single_write(CC1200_MDMCFG1, 0x06);
1673  single_write(CC1200_PA_CFG1, 0x3F);
1674  single_write(CC1200_MDMCFG2, 0x03);
1675  single_write(CC1200_FS_DIG1, 0x07);
1676  single_write(CC1200_FS_DIG0, 0xAA);
1677  single_write(CC1200_FS_DVC0, 0x17);
1678  single_write(CC1200_SERIAL_STATUS, 0x08);
1679 #endif
1680 
1681  strobe(CC1200_STX);
1682 
1683  while(1) {
1684 #if (CC1200_RF_TESTMODE == 1)
1687  leds_off(LEDS_YELLOW);
1688  leds_on(LEDS_RED);
1691  leds_off(LEDS_RED);
1692  leds_on(LEDS_YELLOW);
1693 #else
1696  leds_off(LEDS_GREEN);
1697  leds_on(LEDS_RED);
1700  leds_off(LEDS_RED);
1701  leds_on(LEDS_GREEN);
1702 #endif
1703  }
1704 
1705 #elif (CC1200_RF_TESTMODE == 3)
1706 
1707  /* CS on GPIO3 */
1708  single_write(CC1200_IOCFG3, CC1200_IOCFG_CARRIER_SENSE);
1709  single_write(CC1200_IOCFG2, CC1200_IOCFG_SERIAL_CLK);
1710  single_write(CC1200_IOCFG0, CC1200_IOCFG_SERIAL_RX);
1711  update_cca_threshold(CC1200_RF_CFG.cca_threshold);
1712  freq = calculate_freq(CC1200_DEFAULT_CHANNEL - CC1200_RF_CFG.min_channel);
1713  single_write(CC1200_FREQ0, ((uint8_t *)&freq)[0]);
1714  single_write(CC1200_FREQ1, ((uint8_t *)&freq)[1]);
1715  single_write(CC1200_FREQ2, ((uint8_t *)&freq)[2]);
1716  strobe(CC1200_SRX);
1717 
1718  while(1) {
1719 
1722  leds_off(LEDS_GREEN);
1723  leds_on(LEDS_YELLOW);
1726  leds_off(LEDS_YELLOW);
1727  leds_on(LEDS_GREEN);
1728  clock_delay_usec(1000);
1729 
1730  /* CS on GPIO3 */
1731  if(cc1200_arch_gpio3_read_pin() == 1) {
1732  leds_on(LEDS_RED);
1733  } else {
1734  leds_off(LEDS_RED);
1735  }
1736 
1737  }
1738 
1739 #endif /* #if CC1200_RF_TESTMODE == ... */
1740 
1741  /***************************************************************************
1742  * Set the stuff we need for this driver to work. Don't touch!
1743  **************************************************************************/
1744 
1745  /* GPIOx configuration */
1746  single_write(CC1200_IOCFG3, GPIO3_IOCFG);
1747  single_write(CC1200_IOCFG2, GPIO2_IOCFG);
1748  single_write(CC1200_IOCFG0, GPIO0_IOCFG);
1749 
1750  reg = single_read(CC1200_SETTLING_CFG);
1751  /*
1752  * Turn of auto calibration. This gives us better control
1753  * over the timing (RX/TX & TX /RX turnaround!). We calibrate manually:
1754  * - Upon wake-up (on())
1755  * - Before going to TX (transmit())
1756  * - When setting an new channel (set_channel())
1757  */
1758  reg &= ~(3 << 3);
1759 #if CC1200_AUTOCAL
1760  /* We calibrate when going from idle to RX or TX */
1761  reg |= (1 << 3);
1762 #endif
1763  single_write(CC1200_SETTLING_CFG, reg);
1764 
1765  /* Configure RXOFF_MODE */
1766  reg = single_read(CC1200_RFEND_CFG1);
1767  reg &= ~(3 << 4); /* RXOFF_MODE = IDLE */
1768 #if RXOFF_MODE_RX
1769  reg |= (3 << 4); /* RXOFF_MODE = RX */
1770 #endif
1771  reg |= 0x0F; /* Disable RX timeout */
1772  single_write(CC1200_RFEND_CFG1, reg);
1773 
1774  /* Configure TXOFF_MODE */
1775  reg = single_read(CC1200_RFEND_CFG0);
1776  reg &= ~(3 << 4); /* TXOFF_MODE = IDLE */
1777 #if TXOFF_MODE_RX
1778  reg |= (3 << 4); /* TXOFF_MODE = RX */
1779 #endif
1780  single_write(CC1200_RFEND_CFG0, reg);
1781 
1782  /*
1783  * CCA Mode 0: Always give clear channel indication.
1784  * CCA is done "by hand". Keep in mind: automatic CCA would also
1785  * affect the transmission of the ACK and is not implemented yet!
1786  */
1787 #if CC1200_802154G
1788  single_write(CC1200_PKT_CFG2, (1 << 5));
1789 #else
1790  single_write(CC1200_PKT_CFG2, 0x00);
1791 #endif
1792 
1793  /* Configure appendix */
1794  reg = single_read(CC1200_PKT_CFG1);
1795 #if APPEND_STATUS
1796  reg |= (1 << 0);
1797 #else
1798  reg &= ~(1 << 0);
1799 #endif
1800  single_write(CC1200_PKT_CFG1, reg);
1801 
1802  /* Variable packet length mode */
1803  reg = single_read(CC1200_PKT_CFG0);
1804  reg &= ~(3 << 5);
1805  reg |= (1 << 5);
1806  single_write(CC1200_PKT_CFG0, reg);
1807 
1808 #ifdef FIFO_THRESHOLD
1809  /* FIFO threshold */
1810  single_write(CC1200_FIFO_CFG, FIFO_THRESHOLD);
1811 #endif
1812 
1813 }
1814 /*---------------------------------------------------------------------------*/
1815 /* Return the radio's state. */
1816 static uint8_t
1817 state(void)
1818 {
1819 
1820 #if STATE_USES_MARC_STATE
1821  return single_read(CC1200_MARCSTATE) & 0x1f;
1822 #else
1823  return strobe(CC1200_SNOP) & 0x70;
1824 #endif
1825 
1826 }
1827 /*---------------------------------------------------------------------------*/
1828 #if !CC1200_AUTOCAL
1829 /* Perform manual calibration. */
1830 static void
1831 calibrate(void)
1832 {
1833 
1834 #ifdef RF_FORCE_CALIBRATION
1835  if (!(rf_flags & RF_FORCE_CALIBRATION)
1836  && ((clock_seconds() - cal_timer) < CC1200_CAL_TIMEOUT_SECONDS)) {
1837  /* Timeout not reached, defer calibration... */
1838  return;
1839  }
1840  rf_flags &= ~RF_FORCE_CALIBRATION;
1841 #endif
1842 
1843  INFO("RF: Calibrate\n");
1844 
1845  strobe(CC1200_SCAL);
1846  RTIMER_BUSYWAIT_UNTIL_STATE(STATE_CALIBRATE, RTIMER_SECOND / 100);
1847  RTIMER_BUSYWAIT_UNTIL_STATE(STATE_IDLE, RTIMER_SECOND / 100);
1848 
1849 #if CC1200_CAL_TIMEOUT_SECONDS
1850  cal_timer = clock_seconds();
1851 #endif
1852 
1853 }
1854 #endif
1855 /*---------------------------------------------------------------------------*/
1856 /* Enter IDLE state. */
1857 static void
1858 idle(void)
1859 {
1860 
1861  uint8_t s;
1862 
1863  DISABLE_GPIO_INTERRUPTS();
1864 
1865  TX_LEDS_OFF();
1866  RX_LEDS_OFF();
1867 
1868  ENERGEST_OFF(ENERGEST_TYPE_LISTEN);
1869  ENERGEST_OFF(ENERGEST_TYPE_TRANSMIT);
1870 
1871  s = state();
1872 
1873  if(s == STATE_IDLE) {
1874  return;
1875  } else if(s == STATE_RX_FIFO_ERR) {
1876  WARNING("RF: RX FIFO error!\n");
1877  strobe(CC1200_SFRX);
1878  } else if(s == STATE_TX_FIFO_ERR) {
1879  WARNING("RF: TX FIFO error!\n");
1880  strobe(CC1200_SFTX);
1881  }
1882 
1883  strobe(CC1200_SIDLE);
1884  RTIMER_BUSYWAIT_UNTIL_STATE(STATE_IDLE, RTIMER_SECOND / 100);
1885 
1886 } /* idle(), 21.05.2015 */
1887 /*---------------------------------------------------------------------------*/
1888 /* Enter RX state. */
1889 static void
1890 idle_calibrate_rx(void)
1891 {
1892 
1893  RF_ASSERT(state() == STATE_IDLE);
1894 
1895 #if !CC1200_AUTOCAL
1896  calibrate();
1897 #endif
1898 
1899  rf_flags &= ~RF_RX_PROCESSING_PKT;
1900  strobe(CC1200_SFRX);
1901  strobe(CC1200_SRX);
1902  RTIMER_BUSYWAIT_UNTIL_STATE(STATE_RX, RTIMER_SECOND / 100);
1903 
1904  ENABLE_GPIO_INTERRUPTS();
1905 
1906  ENERGEST_ON(ENERGEST_TYPE_LISTEN);
1907 
1908 }
1909 /*---------------------------------------------------------------------------*/
1910 /* Restart RX from within RX interrupt. */
1911 static void
1912 rx_rx(void)
1913 {
1914 
1915  uint8_t s = state();
1916 
1917  if(s == STATE_IDLE) {
1918  /* Proceed to rx */
1919  } else if(s == STATE_RX_FIFO_ERR) {
1920  WARNING("RF: RX FIFO error!\n");
1921  strobe(CC1200_SFRX);
1922  } else if(s == STATE_TX_FIFO_ERR) {
1923  WARNING("RF: TX FIFO error!\n");
1924  strobe(CC1200_SFTX);
1925  } else {
1926  strobe(CC1200_SIDLE);
1927  RTIMER_BUSYWAIT_UNTIL_STATE(STATE_IDLE,
1928  RTIMER_SECOND / 100);
1929  }
1930 
1931  RX_LEDS_OFF();
1932  rf_flags &= ~RF_RX_PROCESSING_PKT;
1933 
1934  /* Clear pending GPIO interrupts */
1935  ENABLE_GPIO_INTERRUPTS();
1936 
1937  strobe(CC1200_SFRX);
1938  strobe(CC1200_SRX);
1939  RTIMER_BUSYWAIT_UNTIL_STATE(STATE_RX, RTIMER_SECOND / 100);
1940 
1941 }
1942 /*---------------------------------------------------------------------------*/
1943 /* Fill TX FIFO (if not already done), start TX and wait for TX to complete (blocking!). */
1944 static int
1945 idle_tx_rx(const uint8_t *payload, uint16_t payload_len)
1946 {
1947 #if (CC1200_MAX_PAYLOAD_LEN > (CC1200_FIFO_SIZE - PHR_LEN))
1948  uint8_t to_write;
1949  const uint8_t *p;
1950 #endif
1951 
1952  /* Prepare for RX */
1953  rf_flags &= ~RF_RX_PROCESSING_PKT;
1954  strobe(CC1200_SFRX);
1955 
1956  /* Configure GPIO0 to detect TX state */
1957  single_write(CC1200_IOCFG0, CC1200_IOCFG_MARC_2PIN_STATUS_0);
1958 
1959 #if CC1200_WITH_TX_BUF
1960  /* Prepare and write header */
1961  copy_header_to_tx_fifo(payload_len);
1962 
1963  /*
1964  * Fill FIFO with data. If SPI is slow it might make sense
1965  * to divide this process into several chunks.
1966  * The best solution would be to perform TX FIFO refill
1967  * using an interrupt, but we are blocking here (= in TX) anyway...
1968  */
1969 
1970 #if (CC1200_MAX_PAYLOAD_LEN > (CC1200_FIFO_SIZE - PHR_LEN))
1971  to_write = MIN(payload_len, (CC1200_FIFO_SIZE - PHR_LEN));
1972  burst_write(CC1200_TXFIFO, payload, to_write);
1973  bytes_left_to_write = payload_len - to_write;
1974  p = payload + to_write;
1975 #else
1976  burst_write(CC1200_TXFIFO, payload, payload_len);
1977 #endif
1978 #endif /* CC1200_WITH_TX_BUF */
1979 
1980 #if USE_SFSTXON
1981  /* Wait for synthesizer to be ready */
1982  RTIMER_BUSYWAIT_UNTIL_STATE(STATE_FSTXON, RTIMER_SECOND / 100);
1983 #endif
1984 
1985  /* Start TX */
1986  strobe(CC1200_STX);
1987 
1988  /* Wait for TX to start. */
1989  RTIMER_BUSYWAIT_UNTIL((cc1200_arch_gpio0_read_pin() == 1), RTIMER_SECOND / 100);
1990 
1991  /* Turned off at the latest in idle() */
1992  TX_LEDS_ON();
1993 
1994  /* Turned off at the latest in idle() */
1995  ENERGEST_ON(ENERGEST_TYPE_TRANSMIT);
1996 
1997  if((cc1200_arch_gpio0_read_pin() == 0) &&
1998  (single_read(CC1200_NUM_TXBYTES) != 0)) {
1999 
2000  /*
2001  * TX didn't start in time. We also check NUM_TXBYES
2002  * in case we missed the rising edge of the GPIO signal
2003  */
2004 
2005  ERROR("RF: TX doesn't start!\n");
2006 #if (CC1200_MAX_PAYLOAD_LEN > (CC1200_FIFO_SIZE - PHR_LEN))
2007  single_write(CC1200_IOCFG2, GPIO2_IOCFG);
2008 #endif
2009  idle();
2010 
2011  /* Re-configure GPIO0 */
2012  single_write(CC1200_IOCFG0, GPIO0_IOCFG);
2013 
2014  return RADIO_TX_ERR;
2015 
2016  }
2017 
2018 #if (CC1200_MAX_PAYLOAD_LEN > (CC1200_FIFO_SIZE - PHR_LEN)) && CC1200_WITH_TX_BUF
2019  if(bytes_left_to_write != 0) {
2020  rtimer_clock_t t0;
2021  uint8_t s;
2022  t0 = RTIMER_NOW();
2023  do {
2024  if((bytes_left_to_write != 0) &&
2025  (cc1200_arch_gpio2_read_pin() == 0)) {
2026  /* TX TIFO is drained below FIFO_THRESHOLD. Re-fill... */
2027  to_write = MIN(bytes_left_to_write, FIFO_THRESHOLD);
2028  burst_write(CC1200_TXFIFO, p, to_write);
2029  bytes_left_to_write -= to_write;
2030  p += to_write;
2031  t0 += CC1200_RF_CFG.tx_pkt_lifetime;
2032  }
2033  } while((cc1200_arch_gpio0_read_pin() == 1) &&
2034  RTIMER_CLOCK_LT(RTIMER_NOW(), t0 + CC1200_RF_CFG.tx_pkt_lifetime));
2035 
2036  /*
2037  * At this point we either left TX or a timeout occurred. If all went
2038  * well, we are in RX (or at least settling) now.
2039  * If we didn't manage to refill the TX FIFO, an underflow might
2040  * have occur-ed - the radio might be still in TX here!
2041  */
2042 
2043  s = state();
2044  if((s != STATE_RX) && (s != STATE_SETTLING)) {
2045 
2046  /*
2047  * Something bad happened. Wait for radio to enter a
2048  * stable state (in case of an error we are in TX here)
2049  */
2050 
2051  INFO("RF: TX failure!\n");
2052  RTIMER_BUSYWAIT_UNTIL((state() != STATE_TX), RTIMER_SECOND / 100);
2053  /* Re-configure GPIO2 */
2054  single_write(CC1200_IOCFG2, GPIO2_IOCFG);
2055  idle();
2056 
2057  /* Re-configure GPIO0 */
2058  single_write(CC1200_IOCFG0, GPIO0_IOCFG);
2059 
2060  return RADIO_TX_ERR;
2061 
2062  }
2063 
2064  } else {
2065  /* Wait for TX to complete */
2066  RTIMER_BUSYWAIT_UNTIL((cc1200_arch_gpio0_read_pin() == 0),
2067  CC1200_RF_CFG.tx_pkt_lifetime);
2068  }
2069 #else
2070  /* Wait for TX to complete */
2071  RTIMER_BUSYWAIT_UNTIL((cc1200_arch_gpio0_read_pin() == 0),
2072  CC1200_RF_CFG.tx_pkt_lifetime);
2073 #endif
2074 
2075  if(cc1200_arch_gpio0_read_pin() == 1) {
2076  /* TX takes to long - abort */
2077  ERROR("RF: TX takes to long!\n");
2078 #if (CC1200_MAX_PAYLOAD_LEN > (CC1200_FIFO_SIZE - PHR_LEN))
2079  /* Re-configure GPIO2 */
2080  single_write(CC1200_IOCFG2, GPIO2_IOCFG);
2081 #endif
2082  idle();
2083 
2084  /* Re-configure GPIO0 */
2085  single_write(CC1200_IOCFG0, GPIO0_IOCFG);
2086 
2087  return RADIO_TX_ERR;
2088 
2089  }
2090 
2091 #if (CC1200_MAX_PAYLOAD_LEN > (CC1200_FIFO_SIZE - PHR_LEN))
2092  /* Re-configure GPIO2 */
2093  single_write(CC1200_IOCFG2, GPIO2_IOCFG);
2094 #endif
2095 
2096  /* Re-configure GPIO0 */
2097  single_write(CC1200_IOCFG0, GPIO0_IOCFG);
2098 
2099  TX_LEDS_OFF();
2100 
2101  ENERGEST_SWITCH(ENERGEST_TYPE_TRANSMIT, ENERGEST_TYPE_LISTEN);
2102 
2103  return RADIO_TX_OK;
2104 
2105 }
2106 /*---------------------------------------------------------------------------*/
2107 /* Update TX power */
2108 static void
2109 update_txpower(int8_t txpower_dbm)
2110 {
2111 
2112  uint8_t reg = single_read(CC1200_PA_CFG1);
2113 
2114  reg &= ~0x3F;
2115  /* Up to now we don't handle the special power levels PA_POWER_RAMP < 3 */
2116  reg |= ((((txpower_dbm + 18) * 2) - 1) & 0x3F);
2117  single_write(CC1200_PA_CFG1, reg);
2118 
2119  txpower = txpower_dbm;
2120 
2121 }
2122 /*---------------------------------------------------------------------------*/
2123 /* Update CCA threshold */
2124 static void
2125 update_cca_threshold(int8_t threshold_dbm)
2126 {
2127 
2128  single_write(CC1200_AGC_CS_THR, (uint8_t)threshold_dbm);
2129  cca_threshold = threshold_dbm;
2130 
2131 }
2132 /*---------------------------------------------------------------------------*/
2133 /* Calculate FREQ register from channel */
2134 static uint32_t
2135 calculate_freq(uint8_t channel)
2136 {
2137 
2138  uint32_t freq;
2139 
2140  freq = CC1200_RF_CFG.chan_center_freq0 + (channel * CC1200_RF_CFG.chan_spacing) / 1000 /* /1000 because chan_spacing is in Hz */;
2141  freq *= FREQ_MULTIPLIER;
2142  freq /= FREQ_DIVIDER;
2143 
2144  return freq;
2145 
2146 }
2147 /*---------------------------------------------------------------------------*/
2148 /* Update rf channel if possible, else postpone it (->pollhandler) */
2149 static int
2150 set_channel(uint8_t channel)
2151 {
2152 
2153  uint8_t was_off = 0;
2154  uint32_t freq;
2155 
2156  channel %= (CC1200_RF_CFG.max_channel - CC1200_RF_CFG.min_channel + 1);
2157  channel += CC1200_RF_CFG.min_channel;
2158 
2159 #if 0
2160  /*
2161  * We explicitly allow a channel update even if the channel does not change.
2162  * This feature can be used to manually force a calibration.
2163  */
2164  if(channel == rf_channel) {
2165  return rf_channel;
2166  }
2167 #endif
2168 
2169  if(channel < CC1200_RF_CFG.min_channel ||
2170  channel > CC1200_RF_CFG.max_channel) {
2171  /* Invalid channel */
2172  return CHANNEL_OUT_OF_LIMITS;
2173  }
2174 
2175  if(SPI_IS_LOCKED() || (rf_flags & RF_TX_ACTIVE) || receiving_packet()) {
2176 
2177  /* We are busy, postpone channel update */
2178 
2179  new_rf_channel = channel;
2180  rf_flags |= RF_UPDATE_CHANNEL;
2181  process_poll(&cc1200_process);
2182  INFO("RF: Channel update postponed\n");
2183 
2184  return CHANNEL_UPDATE_POSTPONED;
2185 
2186  }
2187  rf_flags &= ~RF_UPDATE_CHANNEL;
2188 
2189  INFO("RF: Channel update (%d)\n", channel);
2190 
2191  if(!(rf_flags & RF_ON)) {
2192  was_off = 1;
2193  on();
2194  }
2195 
2196  LOCK_SPI();
2197 
2198  idle();
2199 
2200  freq = calculate_freq(channel - CC1200_RF_CFG.min_channel);
2201  single_write(CC1200_FREQ0, ((uint8_t *)&freq)[0]);
2202  single_write(CC1200_FREQ1, ((uint8_t *)&freq)[1]);
2203  single_write(CC1200_FREQ2, ((uint8_t *)&freq)[2]);
2204 
2205  rf_channel = channel;
2206 
2207  /* Turn on RX again unless we turn off anyway */
2208  if(!was_off) {
2209 #ifdef RF_FORCE_CALIBRATION
2210  rf_flags |= RF_FORCE_CALIBRATION;
2211 #endif
2212  idle_calibrate_rx();
2213  }
2214 
2215  RELEASE_SPI();
2216 
2217  if(was_off) {
2218  off();
2219  }
2220 
2221  return CHANNEL_UPDATE_SUCCEEDED;
2222 
2223 }
2224 /*---------------------------------------------------------------------------*/
2225 /* Check broadcast address. */
2226 static int
2227 is_broadcast_addr(uint8_t mode, uint8_t *addr)
2228 {
2229 
2230  int i = mode == FRAME802154_SHORTADDRMODE ? 2 : 8;
2231 
2232  while(i-- > 0) {
2233  if(addr[i] != 0xff) {
2234  return 0;
2235  }
2236  }
2237 
2238  return 1;
2239 
2240 }
2241 /*---------------------------------------------------------------------------*/
2242 static int
2243 addr_check_auto_ack(uint8_t *frame, uint16_t frame_len)
2244 {
2245 
2246  frame802154_t info154;
2247 
2248  if(frame802154_parse(frame, frame_len, &info154) != 0) {
2249 
2250  /* We received a valid 802.15.4 frame */
2251 
2252  if(!(rx_mode_value & RADIO_RX_MODE_ADDRESS_FILTER) ||
2253  info154.fcf.frame_type == FRAME802154_ACKFRAME ||
2254  is_broadcast_addr(info154.fcf.dest_addr_mode,
2255  (uint8_t *)&info154.dest_addr) ||
2256  linkaddr_cmp((linkaddr_t *)&info154.dest_addr,
2257  &linkaddr_node_addr)) {
2258 
2259  /*
2260  * Address check succeeded or address filter disabled.
2261  * We send an ACK in case a corresponding data frame
2262  * is received even in promiscuous mode (if auto-ack is
2263  * enabled)!
2264  */
2265 
2266  if((rx_mode_value & RADIO_RX_MODE_AUTOACK) &&
2267  info154.fcf.frame_type == FRAME802154_DATAFRAME &&
2268  info154.fcf.ack_required != 0 &&
2269  (!(rx_mode_value & RADIO_RX_MODE_ADDRESS_FILTER) ||
2270  linkaddr_cmp((linkaddr_t *)&info154.dest_addr,
2271  &linkaddr_node_addr))) {
2272 
2273  /*
2274  * Data frame destined for us & ACK request bit set -> send ACK.
2275  * Make sure the preamble length is configured accordingly as
2276  * MAC timing parameters rely on this!
2277  */
2278 
2279  uint8_t ack[ACK_LEN] = { FRAME802154_ACKFRAME, 0, info154.seq };
2280 
2281 #if (RXOFF_MODE_RX == 1)
2282  /*
2283  * This turns off GPIOx interrupts. Make sure they are turned on
2284  * in rx_rx() later on!
2285  */
2286  idle();
2287 #endif
2288 
2289  prepare((const uint8_t *)ack, ACK_LEN);
2290  idle_tx_rx((const uint8_t *)ack, ACK_LEN);
2291 
2292  /* rx_rx() will follow */
2293 
2294  return ADDR_CHECK_OK_ACK_SEND;
2295 
2296  }
2297 
2298  return ADDR_CHECK_OK;
2299 
2300  } else {
2301 
2302  return ADDR_CHECK_FAILED;
2303 
2304  }
2305 
2306  }
2307 
2308  return INVALID_FRAME;
2309 
2310 }
2311 /*---------------------------------------------------------------------------*/
2312 /*
2313  * The CC1200 interrupt handler: called by the hardware interrupt
2314  * handler, which is defined as part of the cc1200-arch interface.
2315  */
2316 int
2317 cc1200_rx_interrupt(void)
2318 {
2319 
2320  /* The radio's state */
2321  uint8_t s;
2322  /* The number of bytes in the RX FIFO waiting for read-out */
2323  uint8_t num_rxbytes;
2324  /* The payload length read as the first byte from the RX FIFO */
2325  static uint16_t payload_len;
2326  /*
2327  * The number of bytes already read out and placed in the
2328  * intermediate buffer
2329  */
2330  static uint16_t bytes_read;
2331  /*
2332  * We use an intermediate buffer for the packet before
2333  * we pass it to the next upper layer. We also place RSSI +
2334  * LQI in this buffer
2335  */
2336  static uint8_t buf[CC1200_MAX_PAYLOAD_LEN + APPENDIX_LEN];
2337 
2338  /*
2339  * If CC1200_USE_GPIO2 is enabled, we come here either once RX FIFO
2340  * threshold is reached (GPIO2 rising edge)
2341  * or at the end of the packet (GPIO0 falling edge).
2342  */
2343 #if CC1200_USE_GPIO2
2344  int gpio2 = cc1200_arch_gpio2_read_pin();
2345  int gpio0 = cc1200_arch_gpio0_read_pin();
2346  if((rf_flags & RF_RX_ONGOING) == 0 && gpio2 > 0) {
2347  rf_flags |= RF_RX_ONGOING;
2348  sfd_timestamp = RTIMER_NOW();
2349  }
2350  if(gpio0 == 0) {
2351  rf_flags &= ~RF_RX_ONGOING;
2352  }
2353 #endif
2354 
2355  if(SPI_IS_LOCKED()) {
2356 
2357  /*
2358  * SPI is in use. Exit and make sure this
2359  * function is called from the poll handler as soon
2360  * as SPI is available again
2361  */
2362 
2363  rf_flags |= RF_POLL_RX_INTERRUPT;
2364  process_poll(&cc1200_process);
2365  return 1;
2366 
2367  }
2368  rf_flags &= ~RF_POLL_RX_INTERRUPT;
2369 
2370  LOCK_SPI();
2371 
2372  /*
2373  * If CC1200_USE_GPIO2 is enabled, we come here either once RX FIFO
2374  * threshold is reached (GPIO2 rising edge)
2375  * or at the end of the packet (GPIO0 falling edge).
2376  */
2377 
2378  /* Make sure we are in a sane state. Sane means: either RX or IDLE */
2379  s = state();
2380  if((s == STATE_RX_FIFO_ERR) || (s == STATE_TX_FIFO_ERR)) {
2381 
2382  rx_rx();
2383  RELEASE_SPI();
2384  return 0;
2385 
2386  }
2387 
2388  num_rxbytes = single_read(CC1200_NUM_RXBYTES);
2389 
2390  if(num_rxbytes == 0) {
2391 
2392  /*
2393  * This might happen from time to time because
2394  * this function is also called by the pollhandler and / or
2395  * from TWO interrupts which can occur at the same time.
2396  */
2397 
2398  INFO("RF: RX FIFO empty!\n");
2399  RELEASE_SPI();
2400  return 0;
2401 
2402  }
2403 
2404  if(!(rf_flags & RF_RX_PROCESSING_PKT)) {
2405 
2406 #if CC1200_802154G
2407  struct {
2408  uint8_t phra;
2409  uint8_t phrb;
2410  }
2411  phr;
2412 
2413  if(num_rxbytes < PHR_LEN) {
2414 
2415  WARNING("RF: PHR incomplete!\n");
2416  rx_rx();
2417  RELEASE_SPI();
2418  return 0;
2419 
2420  }
2421 
2422  burst_read(CC1200_RXFIFO,
2423  &phr,
2424  PHR_LEN);
2425  payload_len = (phr.phra & 0x07);
2426  payload_len <<= 8;
2427  payload_len += phr.phrb;
2428 
2429  if(phr.phra & (1 << 4)) {
2430  /* CRC16, payload_len += 2 */
2431  payload_len -= 2;
2432  } else {
2433  /* CRC16, payload_len += 4 */
2434  payload_len -= 4;
2435  }
2436 #else
2437  /* Read first byte in RX FIFO (payload length) */
2438  burst_read(CC1200_RXFIFO,
2439  (uint8_t *)&payload_len,
2440  PHR_LEN);
2441 #endif
2442 
2443  if(payload_len < ACK_LEN) {
2444  /* Packet to short. Discard it */
2445  WARNING("RF: Packet too short!\n");
2446  rx_rx();
2447  RELEASE_SPI();
2448  return 0;
2449  }
2450 
2451  if(payload_len > CC1200_MAX_PAYLOAD_LEN) {
2452  /* Packet to long. Discard it */
2453  WARNING("RF: Packet to long!\n");
2454  rx_rx();
2455  RELEASE_SPI();
2456  return 0;
2457  }
2458 
2459  RX_LEDS_ON();
2460  bytes_read = 0;
2461  num_rxbytes -= PHR_LEN;
2462 
2463  rf_flags |= RF_RX_PROCESSING_PKT;
2464 
2465  /* Fall through... */
2466 
2467  }
2468 
2469  if(rf_flags & RF_RX_PROCESSING_PKT) {
2470 
2471  /*
2472  * Read out remaining bytes unless FIFO is empty.
2473  * We have at least num_rxbytes in the FIFO to be read out.
2474  */
2475 
2476  if((num_rxbytes + bytes_read) > (payload_len + CC_APPENDIX_LEN)) {
2477 
2478  /*
2479  * We have a mismatch between the number of bytes in the RX FIFO
2480  * and the payload_len. This would lead to an buffer overflow,
2481  * so we catch this error here.
2482  */
2483 
2484  WARNING("RF: RX length mismatch %d %d %d!\n", num_rxbytes,
2485  bytes_read,
2486  payload_len);
2487  rx_rx();
2488  RELEASE_SPI();
2489  return 0;
2490 
2491  }
2492 
2493  burst_read(CC1200_RXFIFO,
2494  &buf[bytes_read],
2495  num_rxbytes);
2496 
2497  bytes_read += num_rxbytes;
2498  num_rxbytes = 0;
2499 
2500  if(bytes_read == (payload_len + CC_APPENDIX_LEN)) {
2501 
2502  /*
2503  * End of packet. Read appendix (if available), check CRC
2504  * and copy the data from temporary buffer to rx_pkt
2505  * RSSI offset already set using AGC_GAIN_ADJUST.GAIN_ADJUSTMENT
2506  */
2507 
2508 #if APPEND_STATUS
2509  uint8_t crc_lqi = buf[bytes_read - 1];
2510 #else
2511  int8_t rssi = single_read(CC1200_RSSI1);
2512  uint8_t crc_lqi = single_read(CC1200_LQI_VAL);
2513 #endif
2514 
2515  if(!(crc_lqi & (1 << 7))) {
2516  /* CRC error. Drop the packet */
2517  INFO("RF: CRC error!\n");
2518  } else if(rx_pkt_len != 0) {
2519  /* An old packet is pending. Drop the packet */
2520  WARNING("RF: Packet pending!\n");
2521  } else {
2522 
2523  int ret = addr_check_auto_ack(buf, bytes_read);
2524 
2525  if((ret == ADDR_CHECK_OK) ||
2526  (ret == ADDR_CHECK_OK_ACK_SEND)) {
2527 #if APPEND_STATUS
2528  /* RSSI + LQI already read out and placed into buf */
2529 #else
2530  buf[bytes_read++] = (uint8_t)rssi;
2531  buf[bytes_read++] = crc_lqi;
2532 #endif
2533  rx_pkt_len = bytes_read;
2534  memcpy((void *)rx_pkt, buf, rx_pkt_len);
2535  rx_rx();
2536  process_poll(&cc1200_process);
2537  RELEASE_SPI();
2538  return 1;
2539 
2540  } else {
2541  /* Invalid address. Drop the packet */
2542  }
2543 
2544  }
2545 
2546  /* Buffer full, address or CRC check failed */
2547  rx_rx();
2548  RELEASE_SPI();
2549  return 0;
2550 
2551  } /* if (bytes_read == payload_len) */
2552 
2553  }
2554 
2555  RELEASE_SPI();
2556  return 0;
2557 
2558 }
2559 /*---------------------------------------------------------------------------*/
radio_result_t(* get_object)(radio_param_t param, void *dest, size_t size)
Get a radio parameter object.
Definition: radio.h:762
void * packetbuf_dataptr(void)
Get a pointer to the data in the packetbuf.
Definition: packetbuf.c:143
The delay in usec between turning on the radio and it being actually listening (able to hear a preamb...
Definition: radio.h:347
uint8_t dest_addr_mode
2 bit.
Definition: frame802154.h:161
int(* prepare)(const void *payload, unsigned short payload_len)
Prepare the radio with a packet to be sent.
Definition: radio.h:572
#define PROCESS(name, strname)
Declare a process.
Definition: process.h:307
void etimer_stop(struct etimer *et)
Stop a pending event timer.
Definition: etimer.c:243
The parameter is not supported.
Definition: radio.h:473
frame802154_fcf_t fcf
Frame control field.
Definition: frame802154.h:204
#define PROCESS_CONTEXT_END(p)
End a context switch.
Definition: process.h:440
void packetbuf_clear(void)
Clear and reset the packetbuf.
Definition: packetbuf.c:75
static uip_ds6_addr_t * addr
Pointer to a nbr cache entry.
Definition: uip-nd6.c:107
Header file for the energy estimation mechanism
TX failed due to a collision.
Definition: radio.h:503
#define PROCESS_YIELD_UNTIL(c)
Yield the currently running process until a condition occurs.
Definition: process.h:178
#define PROCESS_BEGIN()
Define the beginning of a process.
Definition: process.h:120
The delay in usec between a call to the radio API&#39;s transmit function and the end of SFD transmission...
Definition: radio.h:341
The maximum transmission power in dBm.
Definition: radio.h:318
#define PROCESS_END()
Define the end of a process.
Definition: process.h:131
Received signal strength indicator in dBm.
Definition: radio.h:218
int frame802154_parse(uint8_t *data, int len, frame802154_t *pf)
Parses an input frame.
Definition: frame802154.c:500
int(* receiving_packet)(void)
Check if the radio driver is currently receiving a packet.
Definition: radio.h:676
The short address (16 bits) for the radio, which is used by the h/w filter.
Definition: radio.h:166
#define PROCESS_WAIT_EVENT_UNTIL(c)
Wait for an event to be posted to the process, with an extra condition.
Definition: process.h:157
radio_result_t(* set_value)(radio_param_t param, radio_value_t value)
Set a radio parameter value.
Definition: radio.h:748
int(* pending_packet)(void)
Check if a packet has been received and is available in the radio driver&#39;s buffers.
Definition: radio.h:689
void clock_delay_usec(uint16_t dt)
Delay a given number of microseconds.
Definition: clock.c:150
The structure of a Contiki-NG radio device driver.
Definition: radio.h:526
#define RTIMER_BUSYWAIT_UNTIL(cond, max_time)
Busy-wait until a condition for at most max_time.
Definition: rtimer.h:211
static void set_channel(uint8_t channel)
Set the current operating channel.
Definition: cc2538-rf.c:175
Channel used for radio communication.
Definition: radio.h:134
The value argument was incorrect.
Definition: radio.h:474
The parameter was set/read successfully.
Definition: radio.h:472
int(* channel_clear)(void)
Perform a Clear-Channel Assessment (CCA) to find out if there is a packet in the air or not...
Definition: radio.h:664
unsigned long clock_seconds(void)
Get the current value of the platform seconds.
Definition: clock.c:130
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
void leds_on(unsigned char leds)
Turn on multiple LEDs.
Definition: minileds.c:63
Radio transmission mode determines if the radio has send on CCA (RADIO_TX_MODE_SEND_ON_CCA) enabled o...
Definition: radio.h:180
#define RTIMER_SECOND
Number of rtimer ticks for 1 second.
Definition: rtimer.h:112
void leds_off(unsigned char leds)
Turn off multiple LEDs.
Definition: minileds.c:69
linkaddr_t linkaddr_node_addr
The link-layer address of the node.
Definition: linkaddr.c:48
#define RTIMER_NOW()
Get the current clock time.
Definition: rtimer.h:185
The RSSI value of the last received packet.
Definition: radio.h:226
The physical layer header (PHR) + MAC layer footer (MFR) overhead in bytes.
Definition: radio.h:330
Clear channel assessment threshold in dBm.
Definition: radio.h:205
#define CLOCK_SECOND
A second, measured in system clock time.
Definition: clock.h:82
void(* input)(void)
Callback for getting notified of incoming packet.
Definition: mac.h:72
int(* send)(const void *payload, unsigned short payload_len)
Prepare & transmit a packet.
Definition: radio.h:623
int(* transmit)(unsigned short transmit_len)
Send the packet that has previously been prepared.
Definition: radio.h:611
void process_poll(struct process *p)
Request a process to be polled.
Definition: process.c:371
int(* off)(void)
Turn the radio off.
Definition: radio.h:721
The personal area network identifier (PAN ID), which is used by the h/w frame filtering functionality...
Definition: radio.h:150
The lowest radio channel number.
Definition: radio.h:303
Radio receiver mode determines if the radio has address filter (RADIO_RX_MODE_ADDRESS_FILTER) and aut...
Definition: radio.h:173
The highest radio channel number.
Definition: radio.h:308
#define PACKETBUF_SIZE
The size of the packetbuf, in bytes.
Definition: packetbuf.h:67
The air time of one byte in usec, e.g.
Definition: radio.h:335
void clock_delay(unsigned int i)
Obsolete delay function but we implement it here since some code still uses it.
Definition: clock.c:164
int etimer_expired(struct etimer *et)
Check if an event timer has expired.
Definition: etimer.c:213
#define RADIO_RX_MODE_ADDRESS_FILTER
Enable address-based frame filtering.
Definition: radio.h:443
uint8_t frame_type
3 bit.
Definition: frame802154.h:153
#define PROCESS_YIELD()
Yield the currently running process.
Definition: process.h:164
When getting the value of this parameter, the radio driver should indicate whether the radio is on or...
Definition: radio.h:119
Parameters used by the frame802154_create() function.
Definition: frame802154.h:198
enum radio_result_e radio_result_t
Radio return values when setting or getting radio parameters.
A timer.
Definition: etimer.h:76
#define RADIO_TX_MODE_SEND_ON_CCA
Radio TX mode control / retrieval.
Definition: radio.h:466
int linkaddr_cmp(const linkaddr_t *addr1, const linkaddr_t *addr2)
Compare two link-layer addresses.
Definition: linkaddr.c:69
#define RADIO_RX_MODE_AUTOACK
Enable automatic transmission of ACK frames.
Definition: radio.h:448
int(* init)(void)
Initialise the radio hardware.
Definition: radio.h:547
The delay in usec between the end of SFD reception for an incoming frame and the radio API starting t...
Definition: radio.h:353
#define RADIO_RX_MODE_POLL_MODE
Enable/disable/get the state of radio driver poll mode operation.
Definition: radio.h:453
#define PROCESS_CONTEXT_BEGIN(p)
Switch context to another process.
Definition: process.h:426
uint8_t seq
Sequence number.
Definition: frame802154.h:205
static radio_value_t get_rssi(void)
Reads the current signal strength (RSSI)
Definition: cc2538-rf.c:233
Long (64 bits) address for the radio, which is used by the address filter.
Definition: radio.h:255
The minimum transmission power in dBm.
Definition: radio.h:313
Radio powered on and able to receive frames.
Definition: radio.h:392
int(* read)(void *buf, unsigned short buf_len)
Read a received packet into a buffer.
Definition: radio.h:647
Transmission power in dBm.
Definition: radio.h:192
Header file for the Packet buffer (packetbuf) management
Include file for the Contiki low-layer network stack (NETSTACK)
void watchdog_periodic(void)
Writes the WDT clear sequence.
Definition: watchdog.c:85
radio_result_t(* get_value)(radio_param_t param, radio_value_t *value)
Get a radio parameter value.
Definition: radio.h:733
PROCESS_THREAD(cc2538_rf_process, ev, data)
Implementation of the cc2538 RF driver process.
Definition: cc2538-rf.c:1110
Last packet timestamp, of type rtimer_clock_t.
Definition: radio.h:278
uint8_t dest_addr[8]
Destination address.
Definition: frame802154.h:202
An error occurred during transmission.
Definition: radio.h:498
void etimer_reset(struct etimer *et)
Reset an event timer with the same interval as was previously set.
Definition: etimer.c:192
radio_result_t(* set_object)(radio_param_t param, const void *src, size_t size)
Set a radio parameter object.
Definition: radio.h:779
Header file for the LED HAL.
uint8_t ack_required
1 bit.
Definition: frame802154.h:156
Radio powered off and in the lowest possible power consumption state.
Definition: radio.h:387
void etimer_set(struct etimer *et, clock_time_t interval)
Set an event timer.
Definition: etimer.c:177
void packetbuf_set_datalen(uint16_t len)
Set the length of the data in the packetbuf.
Definition: packetbuf.c:136
#define PROCESS_POLLHANDLER(handler)
Specify an action when a process is polled.
Definition: process.h:242
TX was successful and where an ACK was requested one was received.
Definition: radio.h:490
int(* on)(void)
Turn the radio on.
Definition: radio.h:703
#define RTIMER_BUSYWAIT(duration)
Busy-wait for a fixed duration.
Definition: rtimer.h:218
void process_start(struct process *p, process_data_t data)
Start a process.
Definition: process.c:99