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 "cc1200-const.h"
36 #include "cc1200-conf.h"
37 #include "cc1200-arch.h"
38 #include "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 */
681  rx_mode_value = (RADIO_RX_MODE_AUTOACK | RADIO_RX_MODE_ADDRESS_FILTER);
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) {
1234  return RADIO_RESULT_INVALID_VALUE;
1235  }
1236 
1237  switch(param) {
1238  case RADIO_PARAM_POWER_MODE:
1239 
1240  if(rf_flags & RF_ON) {
1241  *value = (radio_value_t)RADIO_POWER_MODE_ON;
1242  } else {
1243  *value = (radio_value_t)RADIO_POWER_MODE_OFF;
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:
1253  case RADIO_PARAM_16BIT_ADDR:
1254 
1255  return RADIO_RESULT_NOT_SUPPORTED;
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 
1272  case RADIO_PARAM_CCA_THRESHOLD:
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 
1285  case RADIO_PARAM_64BIT_ADDR:
1286 
1287  return RADIO_RESULT_NOT_SUPPORTED;
1288 
1289  case RADIO_CONST_CHANNEL_MIN:
1290 
1291  *value = (radio_value_t)CC1200_RF_CFG.min_channel;
1292  return RADIO_RESULT_OK;
1293 
1294  case RADIO_CONST_CHANNEL_MAX:
1295 
1296  *value = (radio_value_t)CC1200_RF_CFG.max_channel;
1297  return RADIO_RESULT_OK;
1298 
1299  case RADIO_CONST_TXPOWER_MIN:
1300 
1301  *value = (radio_value_t)CC1200_CONST_TX_POWER_MIN;
1302  return RADIO_RESULT_OK;
1303 
1304  case RADIO_CONST_TXPOWER_MAX:
1305 
1306  *value = (radio_value_t)CC1200_RF_CFG.max_txpower;
1307  return RADIO_RESULT_OK;
1308 
1309  case RADIO_CONST_PHY_OVERHEAD:
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 
1321  case RADIO_CONST_BYTE_AIR_TIME:
1322  *value = (radio_value_t)8*1000*1000 / CC1200_RF_CFG.bitrate;
1323  return RADIO_RESULT_OK;
1324 
1325  case RADIO_CONST_DELAY_BEFORE_TX:
1326  *value = (radio_value_t)CC1200_RF_CFG.delay_before_tx;
1327  return RADIO_RESULT_OK;
1328 
1329  case RADIO_CONST_DELAY_BEFORE_RX:
1330  *value = (radio_value_t)CC1200_RF_CFG.delay_before_rx;
1331  return RADIO_RESULT_OK;
1332 
1333  case RADIO_CONST_DELAY_BEFORE_DETECT:
1334  *value = (radio_value_t)CC1200_RF_CFG.delay_before_detect;
1335  return RADIO_RESULT_OK;
1336 
1337  default:
1338 
1339  return RADIO_RESULT_NOT_SUPPORTED;
1340 
1341  }
1342 
1343 }
1344 /*---------------------------------------------------------------------------*/
1345 /* Set a radio parameter value. */
1346 static radio_result_t
1347 set_value(radio_param_t param, radio_value_t value)
1348 {
1349 
1350  switch(param) {
1351  case RADIO_PARAM_POWER_MODE:
1352 
1353  if(value == RADIO_POWER_MODE_ON) {
1354  on();
1355  return RADIO_RESULT_OK;
1356  }
1357 
1358  if(value == RADIO_POWER_MODE_OFF) {
1359  off();
1360  return RADIO_RESULT_OK;
1361  }
1362 
1363  return RADIO_RESULT_INVALID_VALUE;
1364 
1365  case RADIO_PARAM_CHANNEL:
1366 
1367  if(set_channel(value) == CHANNEL_OUT_OF_LIMITS) {
1368  return RADIO_RESULT_INVALID_VALUE;
1369  }
1370 
1371  /*
1372  * We always return OK here even if the channel update was
1373  * postponed. rf_channel is NOT updated in this case until
1374  * the channel update was performed. So reading back
1375  * the channel using get_value() might return the "old" channel
1376  * until the channel was actually changed
1377  */
1378 
1379  return RADIO_RESULT_OK;
1380 
1381  case RADIO_PARAM_PAN_ID:
1382  case RADIO_PARAM_16BIT_ADDR:
1383 
1384  return RADIO_RESULT_NOT_SUPPORTED;
1385 
1386  case RADIO_PARAM_RX_MODE:
1387 
1388  rx_mode_value = value;
1389  return RADIO_RESULT_OK;
1390 
1391  case RADIO_PARAM_TX_MODE:
1392 
1393  tx_mode_value = value;
1394  return RADIO_RESULT_OK;
1395 
1396  case RADIO_PARAM_TXPOWER:
1397 
1398  if(value > (radio_value_t)CC1200_RF_CFG.max_txpower) {
1399  value = (radio_value_t)CC1200_RF_CFG.max_txpower;
1400  }
1401 
1402  if(value < (radio_value_t)CC1200_CONST_TX_POWER_MIN) {
1403  value = (radio_value_t)CC1200_CONST_TX_POWER_MIN;
1404  }
1405 
1406  /* We update the output power as soon as we transmit the next packet */
1407  new_txpower = (int8_t)value;
1408  return RADIO_RESULT_OK;
1409 
1410  case RADIO_PARAM_CCA_THRESHOLD:
1411 
1412  if(value > (radio_value_t)CC1200_CONST_CCA_THRESHOLD_MAX) {
1413  value = (radio_value_t)CC1200_CONST_CCA_THRESHOLD_MAX;
1414  }
1415 
1416  if(value < (radio_value_t)CC1200_CONST_CCA_THRESHOLD_MIN) {
1417  value = (radio_value_t)CC1200_CONST_CCA_THRESHOLD_MIN;
1418  }
1419 
1420  /* When to update the threshold? Let's do it in channel_clear() ... */
1421  new_cca_threshold = (int8_t)value;
1422  return RADIO_RESULT_OK;
1423 
1424  case RADIO_PARAM_RSSI:
1425  case RADIO_PARAM_64BIT_ADDR:
1426 
1427  default:
1428 
1429  return RADIO_RESULT_NOT_SUPPORTED;
1430 
1431  }
1432 
1433 }
1434 /*---------------------------------------------------------------------------*/
1435 /* Get a radio parameter object. */
1436 static radio_result_t
1437 get_object(radio_param_t param, void *dest, size_t size)
1438 {
1439  if(param == RADIO_PARAM_LAST_PACKET_TIMESTAMP) {
1440  if(size != sizeof(rtimer_clock_t) || !dest) {
1441  return RADIO_RESULT_INVALID_VALUE;
1442  }
1443  *(rtimer_clock_t *)dest = sfd_timestamp;
1444  return RADIO_RESULT_OK;
1445  }
1446 
1447 #if MAC_CONF_WITH_TSCH
1448  if(param == RADIO_CONST_TSCH_TIMING) {
1449  if(size != sizeof(uint16_t *) || !dest) {
1450  return RADIO_RESULT_INVALID_VALUE;
1451  }
1452  *(const uint16_t **)dest = CC1200_RF_CFG.tsch_timing;
1453  return RADIO_RESULT_OK;
1454  }
1455 #endif /* MAC_CONF_WITH_TSCH */
1456 
1457  return RADIO_RESULT_NOT_SUPPORTED;
1458 
1459 }
1460 /*---------------------------------------------------------------------------*/
1461 /* Set a radio parameter object. */
1462 static radio_result_t
1463 set_object(radio_param_t param, const void *src, size_t size)
1464 {
1465 
1466  return RADIO_RESULT_NOT_SUPPORTED;
1467 
1468 }
1469 /*---------------------------------------------------------------------------*/
1470 /*---------------------------------------------------------------------------*/
1471 /*
1472  * CC1200 low level functions
1473  */
1474 /*---------------------------------------------------------------------------*/
1475 /*---------------------------------------------------------------------------*/
1476 
1477 /* Send a command strobe. */
1478 static uint8_t
1479 strobe(uint8_t strobe)
1480 {
1481 
1482  uint8_t ret;
1483 
1484  cc1200_arch_spi_select();
1485  ret = cc1200_arch_spi_rw_byte(strobe);
1486  cc1200_arch_spi_deselect();
1487 
1488  return ret;
1489 
1490 }
1491 /*---------------------------------------------------------------------------*/
1492 /* Reset CC1200. */
1493 static void
1494 reset(void)
1495 {
1496 
1497  cc1200_arch_spi_select();
1498  cc1200_arch_spi_rw_byte(CC1200_SRES);
1499  /*
1500  * Here we should wait for SO to go low again.
1501  * As we don't have access to this pin we just wait for 100µs.
1502  */
1503  clock_delay(100);
1504  cc1200_arch_spi_deselect();
1505 
1506 }
1507 /*---------------------------------------------------------------------------*/
1508 /* Write a single byte to the specified address. */
1509 static uint8_t
1510 single_write(uint16_t addr, uint8_t val)
1511 {
1512 
1513  uint8_t ret;
1514 
1515  cc1200_arch_spi_select();
1516  if(CC1200_IS_EXTENDED_ADDR(addr)) {
1517  cc1200_arch_spi_rw_byte(CC1200_EXTENDED_WRITE_CMD);
1518  cc1200_arch_spi_rw_byte((uint8_t)addr);
1519  } else {
1520  cc1200_arch_spi_rw_byte(addr | CC1200_WRITE_BIT);
1521  }
1522  ret = cc1200_arch_spi_rw_byte(val);
1523  cc1200_arch_spi_deselect();
1524 
1525  return ret;
1526 
1527 }
1528 /*---------------------------------------------------------------------------*/
1529 /* Read a single byte from the specified address. */
1530 static uint8_t
1531 single_read(uint16_t addr)
1532 {
1533 
1534  uint8_t ret;
1535 
1536  cc1200_arch_spi_select();
1537  if(CC1200_IS_EXTENDED_ADDR(addr)) {
1538  cc1200_arch_spi_rw_byte(CC1200_EXTENDED_READ_CMD);
1539  cc1200_arch_spi_rw_byte((uint8_t)addr);
1540  } else {
1541  cc1200_arch_spi_rw_byte(addr | CC1200_READ_BIT);
1542  }
1543  ret = cc1200_arch_spi_rw_byte(0);
1544  cc1200_arch_spi_deselect();
1545 
1546  return ret;
1547 
1548 }
1549 /*---------------------------------------------------------------------------*/
1550 /* Write a burst of bytes starting at the specified address. */
1551 static void
1552 burst_write(uint16_t addr, const uint8_t *data, uint8_t data_len)
1553 {
1554 
1555  cc1200_arch_spi_select();
1556  if(CC1200_IS_EXTENDED_ADDR(addr)) {
1557  cc1200_arch_spi_rw_byte(CC1200_EXTENDED_BURST_WRITE_CMD);
1558  cc1200_arch_spi_rw_byte((uint8_t)addr);
1559  } else {
1560  cc1200_arch_spi_rw_byte(addr | CC1200_WRITE_BIT | CC1200_BURST_BIT);
1561  }
1562  cc1200_arch_spi_rw(NULL, data, data_len);
1563  cc1200_arch_spi_deselect();
1564 
1565 }
1566 /*---------------------------------------------------------------------------*/
1567 /* Read a burst of bytes starting at the specified address. */
1568 static void
1569 burst_read(uint16_t addr, uint8_t *data, uint8_t data_len)
1570 {
1571 
1572  cc1200_arch_spi_select();
1573  if(CC1200_IS_EXTENDED_ADDR(addr)) {
1574  cc1200_arch_spi_rw_byte(CC1200_EXTENDED_BURST_READ_CMD);
1575  cc1200_arch_spi_rw_byte((uint8_t)addr);
1576  } else {
1577  cc1200_arch_spi_rw_byte(addr | CC1200_READ_BIT | CC1200_BURST_BIT);
1578  }
1579  cc1200_arch_spi_rw(data, NULL, data_len);
1580  cc1200_arch_spi_deselect();
1581 
1582 }
1583 /*---------------------------------------------------------------------------*/
1584 /* Write a list of register settings. */
1585 static void
1586 write_reg_settings(const registerSetting_t *reg_settings,
1587  uint16_t sizeof_reg_settings)
1588 {
1589 
1590  int i = sizeof_reg_settings / sizeof(registerSetting_t);
1591 
1592  if(reg_settings != NULL) {
1593  while(i--) {
1594  single_write(reg_settings->addr,
1595  reg_settings->val);
1596  reg_settings++;
1597  }
1598  }
1599 
1600 }
1601 /*---------------------------------------------------------------------------*/
1602 /* Configure the radio (write basic configuration). */
1603 static void
1604 configure(void)
1605 {
1606 
1607  uint8_t reg;
1608 #if CC1200_RF_TESTMODE
1609  uint32_t freq;
1610 #endif
1611 
1612  /*
1613  * As we only write registers which are different from the chip's reset
1614  * state, let's assure that the chip is in a clean state
1615  */
1616  reset();
1617 
1618  /* Write the configuration as exported from SmartRF Studio */
1619  write_reg_settings(CC1200_RF_CFG.register_settings,
1620  CC1200_RF_CFG.size_of_register_settings);
1621 
1622  /* Write frequency offset */
1623 #if CC1200_FREQ_OFFSET
1624  /* MSB */
1625  single_write(CC1200_FREQOFF1, (uint8_t)(CC1200_FREQ_OFFSET >> 8));
1626  /* LSB */
1627  single_write(CC1200_FREQOFF0, (uint8_t)(CC1200_FREQ_OFFSET));
1628 #endif
1629 
1630  /* RSSI offset */
1631  single_write(CC1200_AGC_GAIN_ADJUST, (int8_t)CC1200_RF_CFG.rssi_offset);
1632 
1633  /***************************************************************************
1634  * RF test modes needed during hardware development
1635  **************************************************************************/
1636 
1637 #if (CC1200_RF_TESTMODE == 1) || (CC1200_RF_TESTMODE == 2)
1638 
1639  strobe(CC1200_SFTX);
1640  single_write(CC1200_TXFIRST, 0);
1641  single_write(CC1200_TXLAST, 0xFF);
1642  update_txpower(CC1200_CONST_TX_POWER_MAX);
1643  single_write(CC1200_PKT_CFG2, 0x02);
1644  freq = calculate_freq(CC1200_DEFAULT_CHANNEL - CC1200_RF_CFG.min_channel);
1645  single_write(CC1200_FREQ0, ((uint8_t *)&freq)[0]);
1646  single_write(CC1200_FREQ1, ((uint8_t *)&freq)[1]);
1647  single_write(CC1200_FREQ2, ((uint8_t *)&freq)[2]);
1648 
1649  printf("RF: Freq0 0x%02x\n", ((uint8_t *)&freq)[0]);
1650  printf("RF: Freq1 0x%02x\n", ((uint8_t *)&freq)[1]);
1651  printf("RF: Freq2 0x%02x\n", ((uint8_t *)&freq)[2]);
1652 
1653 #if (CC1200_RF_TESTMODE == 1)
1654  single_write(CC1200_SYNC_CFG1, 0xE8);
1655  single_write(CC1200_PREAMBLE_CFG1, 0x00);
1656  single_write(CC1200_MDMCFG1, 0x46);
1657  single_write(CC1200_PKT_CFG0, 0x40);
1658  single_write(CC1200_FS_DIG1, 0x07);
1659  single_write(CC1200_FS_DIG0, 0xAA);
1660  single_write(CC1200_FS_DVC1, 0xFF);
1661  single_write(CC1200_FS_DVC0, 0x17);
1662 #endif
1663 
1664 #if (CC1200_RF_TESTMODE == 2)
1665  single_write(CC1200_SYNC_CFG1, 0xE8);
1666  single_write(CC1200_PREAMBLE_CFG1, 0x00);
1667  single_write(CC1200_MDMCFG1, 0x06);
1668  single_write(CC1200_PA_CFG1, 0x3F);
1669  single_write(CC1200_MDMCFG2, 0x03);
1670  single_write(CC1200_FS_DIG1, 0x07);
1671  single_write(CC1200_FS_DIG0, 0xAA);
1672  single_write(CC1200_FS_DVC0, 0x17);
1673  single_write(CC1200_SERIAL_STATUS, 0x08);
1674 #endif
1675 
1676  strobe(CC1200_STX);
1677 
1678  while(1) {
1679 #if (CC1200_RF_TESTMODE == 1)
1681  RTIMER_BUSYWAIT(RTIMER_SECOND / 10);
1682  leds_off(LEDS_YELLOW);
1683  leds_on(LEDS_RED);
1685  RTIMER_BUSYWAIT(RTIMER_SECOND / 10);
1686  leds_off(LEDS_RED);
1687  leds_on(LEDS_YELLOW);
1688 #else
1690  RTIMER_BUSYWAIT(RTIMER_SECOND / 10);
1691  leds_off(LEDS_GREEN);
1692  leds_on(LEDS_RED);
1694  RTIMER_BUSYWAIT(RTIMER_SECOND / 10);
1695  leds_off(LEDS_RED);
1696  leds_on(LEDS_GREEN);
1697 #endif
1698  }
1699 
1700 #elif (CC1200_RF_TESTMODE == 3)
1701 
1702  /* CS on GPIO3 */
1703  single_write(CC1200_IOCFG3, CC1200_IOCFG_CARRIER_SENSE);
1704  single_write(CC1200_IOCFG2, CC1200_IOCFG_SERIAL_CLK);
1705  single_write(CC1200_IOCFG0, CC1200_IOCFG_SERIAL_RX);
1706  update_cca_threshold(CC1200_RF_CFG.cca_threshold);
1707  freq = calculate_freq(CC1200_DEFAULT_CHANNEL - CC1200_RF_CFG.min_channel);
1708  single_write(CC1200_FREQ0, ((uint8_t *)&freq)[0]);
1709  single_write(CC1200_FREQ1, ((uint8_t *)&freq)[1]);
1710  single_write(CC1200_FREQ2, ((uint8_t *)&freq)[2]);
1711  strobe(CC1200_SRX);
1712 
1713  while(1) {
1714 
1716  RTIMER_BUSYWAIT(RTIMER_SECOND / 10);
1717  leds_off(LEDS_GREEN);
1718  leds_on(LEDS_YELLOW);
1720  RTIMER_BUSYWAIT(RTIMER_SECOND / 10);
1721  leds_off(LEDS_YELLOW);
1722  leds_on(LEDS_GREEN);
1723  clock_delay_usec(1000);
1724 
1725  /* CS on GPIO3 */
1726  if(cc1200_arch_gpio3_read_pin() == 1) {
1727  leds_on(LEDS_RED);
1728  } else {
1729  leds_off(LEDS_RED);
1730  }
1731 
1732  }
1733 
1734 #endif /* #if CC1200_RF_TESTMODE == ... */
1735 
1736  /***************************************************************************
1737  * Set the stuff we need for this driver to work. Don't touch!
1738  **************************************************************************/
1739 
1740  /* GPIOx configuration */
1741  single_write(CC1200_IOCFG3, GPIO3_IOCFG);
1742  single_write(CC1200_IOCFG2, GPIO2_IOCFG);
1743  single_write(CC1200_IOCFG0, GPIO0_IOCFG);
1744 
1745  reg = single_read(CC1200_SETTLING_CFG);
1746  /*
1747  * Turn of auto calibration. This gives us better control
1748  * over the timing (RX/TX & TX /RX turnaround!). We calibrate manually:
1749  * - Upon wake-up (on())
1750  * - Before going to TX (transmit())
1751  * - When setting an new channel (set_channel())
1752  */
1753  reg &= ~(3 << 3);
1754 #if CC1200_AUTOCAL
1755  /* We calibrate when going from idle to RX or TX */
1756  reg |= (1 << 3);
1757 #endif
1758  single_write(CC1200_SETTLING_CFG, reg);
1759 
1760  /* Configure RXOFF_MODE */
1761  reg = single_read(CC1200_RFEND_CFG1);
1762  reg &= ~(3 << 4); /* RXOFF_MODE = IDLE */
1763 #if RXOFF_MODE_RX
1764  reg |= (3 << 4); /* RXOFF_MODE = RX */
1765 #endif
1766  reg |= 0x0F; /* Disable RX timeout */
1767  single_write(CC1200_RFEND_CFG1, reg);
1768 
1769  /* Configure TXOFF_MODE */
1770  reg = single_read(CC1200_RFEND_CFG0);
1771  reg &= ~(3 << 4); /* TXOFF_MODE = IDLE */
1772 #if TXOFF_MODE_RX
1773  reg |= (3 << 4); /* TXOFF_MODE = RX */
1774 #endif
1775  single_write(CC1200_RFEND_CFG0, reg);
1776 
1777  /*
1778  * CCA Mode 0: Always give clear channel indication.
1779  * CCA is done "by hand". Keep in mind: automatic CCA would also
1780  * affect the transmission of the ACK and is not implemented yet!
1781  */
1782 #if CC1200_802154G
1783  single_write(CC1200_PKT_CFG2, (1 << 5));
1784 #else
1785  single_write(CC1200_PKT_CFG2, 0x00);
1786 #endif
1787 
1788  /* Configure appendix */
1789  reg = single_read(CC1200_PKT_CFG1);
1790 #if APPEND_STATUS
1791  reg |= (1 << 0);
1792 #else
1793  reg &= ~(1 << 0);
1794 #endif
1795  single_write(CC1200_PKT_CFG1, reg);
1796 
1797  /* Variable packet length mode */
1798  reg = single_read(CC1200_PKT_CFG0);
1799  reg &= ~(3 << 5);
1800  reg |= (1 << 5);
1801  single_write(CC1200_PKT_CFG0, reg);
1802 
1803 #ifdef FIFO_THRESHOLD
1804  /* FIFO threshold */
1805  single_write(CC1200_FIFO_CFG, FIFO_THRESHOLD);
1806 #endif
1807 
1808 }
1809 /*---------------------------------------------------------------------------*/
1810 /* Return the radio's state. */
1811 static uint8_t
1812 state(void)
1813 {
1814 
1815 #if STATE_USES_MARC_STATE
1816  return single_read(CC1200_MARCSTATE) & 0x1f;
1817 #else
1818  return strobe(CC1200_SNOP) & 0x70;
1819 #endif
1820 
1821 }
1822 /*---------------------------------------------------------------------------*/
1823 #if !CC1200_AUTOCAL
1824 /* Perform manual calibration. */
1825 static void
1826 calibrate(void)
1827 {
1828 
1829 #ifdef RF_FORCE_CALIBRATION
1830  if (!(rf_flags & RF_FORCE_CALIBRATION)
1831  && ((clock_seconds() - cal_timer) < CC1200_CAL_TIMEOUT_SECONDS)) {
1832  /* Timeout not reached, defer calibration... */
1833  return;
1834  }
1835  rf_flags &= ~RF_FORCE_CALIBRATION;
1836 #endif
1837 
1838  INFO("RF: Calibrate\n");
1839 
1840  strobe(CC1200_SCAL);
1841  RTIMER_BUSYWAIT_UNTIL_STATE(STATE_CALIBRATE, RTIMER_SECOND / 100);
1842  RTIMER_BUSYWAIT_UNTIL_STATE(STATE_IDLE, RTIMER_SECOND / 100);
1843 
1844 #if CC1200_CAL_TIMEOUT_SECONDS
1845  cal_timer = clock_seconds();
1846 #endif
1847 
1848 }
1849 #endif
1850 /*---------------------------------------------------------------------------*/
1851 /* Enter IDLE state. */
1852 static void
1853 idle(void)
1854 {
1855 
1856  uint8_t s;
1857 
1858  DISABLE_GPIO_INTERRUPTS();
1859 
1860  TX_LEDS_OFF();
1861  RX_LEDS_OFF();
1862 
1863  ENERGEST_OFF(ENERGEST_TYPE_LISTEN);
1864  ENERGEST_OFF(ENERGEST_TYPE_TRANSMIT);
1865 
1866  s = state();
1867 
1868  if(s == STATE_IDLE) {
1869  return;
1870  } else if(s == STATE_RX_FIFO_ERR) {
1871  WARNING("RF: RX FIFO error!\n");
1872  strobe(CC1200_SFRX);
1873  } else if(s == STATE_TX_FIFO_ERR) {
1874  WARNING("RF: TX FIFO error!\n");
1875  strobe(CC1200_SFTX);
1876  }
1877 
1878  strobe(CC1200_SIDLE);
1879  RTIMER_BUSYWAIT_UNTIL_STATE(STATE_IDLE, RTIMER_SECOND / 100);
1880 
1881 } /* idle(), 21.05.2015 */
1882 /*---------------------------------------------------------------------------*/
1883 /* Enter RX state. */
1884 static void
1885 idle_calibrate_rx(void)
1886 {
1887 
1888  RF_ASSERT(state() == STATE_IDLE);
1889 
1890 #if !CC1200_AUTOCAL
1891  calibrate();
1892 #endif
1893 
1894  rf_flags &= ~RF_RX_PROCESSING_PKT;
1895  strobe(CC1200_SFRX);
1896  strobe(CC1200_SRX);
1897  RTIMER_BUSYWAIT_UNTIL_STATE(STATE_RX, RTIMER_SECOND / 100);
1898 
1899  ENABLE_GPIO_INTERRUPTS();
1900 
1901  ENERGEST_ON(ENERGEST_TYPE_LISTEN);
1902 
1903 }
1904 /*---------------------------------------------------------------------------*/
1905 /* Restart RX from within RX interrupt. */
1906 static void
1907 rx_rx(void)
1908 {
1909 
1910  uint8_t s = state();
1911 
1912  if(s == STATE_IDLE) {
1913  /* Proceed to rx */
1914  } else if(s == STATE_RX_FIFO_ERR) {
1915  WARNING("RF: RX FIFO error!\n");
1916  strobe(CC1200_SFRX);
1917  } else if(s == STATE_TX_FIFO_ERR) {
1918  WARNING("RF: TX FIFO error!\n");
1919  strobe(CC1200_SFTX);
1920  } else {
1921  strobe(CC1200_SIDLE);
1922  RTIMER_BUSYWAIT_UNTIL_STATE(STATE_IDLE,
1923  RTIMER_SECOND / 100);
1924  }
1925 
1926  RX_LEDS_OFF();
1927  rf_flags &= ~RF_RX_PROCESSING_PKT;
1928 
1929  /* Clear pending GPIO interrupts */
1930  ENABLE_GPIO_INTERRUPTS();
1931 
1932  strobe(CC1200_SFRX);
1933  strobe(CC1200_SRX);
1934  RTIMER_BUSYWAIT_UNTIL_STATE(STATE_RX, RTIMER_SECOND / 100);
1935 
1936 }
1937 /*---------------------------------------------------------------------------*/
1938 /* Fill TX FIFO (if not already done), start TX and wait for TX to complete (blocking!). */
1939 static int
1940 idle_tx_rx(const uint8_t *payload, uint16_t payload_len)
1941 {
1942 #if (CC1200_MAX_PAYLOAD_LEN > (CC1200_FIFO_SIZE - PHR_LEN))
1943  uint8_t to_write;
1944  const uint8_t *p;
1945 #endif
1946 
1947  /* Prepare for RX */
1948  rf_flags &= ~RF_RX_PROCESSING_PKT;
1949  strobe(CC1200_SFRX);
1950 
1951  /* Configure GPIO0 to detect TX state */
1952  single_write(CC1200_IOCFG0, CC1200_IOCFG_MARC_2PIN_STATUS_0);
1953 
1954 #if CC1200_WITH_TX_BUF
1955  /* Prepare and write header */
1956  copy_header_to_tx_fifo(payload_len);
1957 
1958  /*
1959  * Fill FIFO with data. If SPI is slow it might make sense
1960  * to divide this process into several chunks.
1961  * The best solution would be to perform TX FIFO refill
1962  * using an interrupt, but we are blocking here (= in TX) anyway...
1963  */
1964 
1965 #if (CC1200_MAX_PAYLOAD_LEN > (CC1200_FIFO_SIZE - PHR_LEN))
1966  to_write = MIN(payload_len, (CC1200_FIFO_SIZE - PHR_LEN));
1967  burst_write(CC1200_TXFIFO, payload, to_write);
1968  bytes_left_to_write = payload_len - to_write;
1969  p = payload + to_write;
1970 #else
1971  burst_write(CC1200_TXFIFO, payload, payload_len);
1972 #endif
1973 #endif /* CC1200_WITH_TX_BUF */
1974 
1975 #if USE_SFSTXON
1976  /* Wait for synthesizer to be ready */
1977  RTIMER_BUSYWAIT_UNTIL_STATE(STATE_FSTXON, RTIMER_SECOND / 100);
1978 #endif
1979 
1980  /* Start TX */
1981  strobe(CC1200_STX);
1982 
1983  /* Wait for TX to start. */
1984  RTIMER_BUSYWAIT_UNTIL((cc1200_arch_gpio0_read_pin() == 1), RTIMER_SECOND / 100);
1985 
1986  /* Turned off at the latest in idle() */
1987  TX_LEDS_ON();
1988 
1989  /* Turned off at the latest in idle() */
1990  ENERGEST_ON(ENERGEST_TYPE_TRANSMIT);
1991 
1992  if((cc1200_arch_gpio0_read_pin() == 0) &&
1993  (single_read(CC1200_NUM_TXBYTES) != 0)) {
1994 
1995  /*
1996  * TX didn't start in time. We also check NUM_TXBYES
1997  * in case we missed the rising edge of the GPIO signal
1998  */
1999 
2000  ERROR("RF: TX doesn't start!\n");
2001 #if (CC1200_MAX_PAYLOAD_LEN > (CC1200_FIFO_SIZE - PHR_LEN))
2002  single_write(CC1200_IOCFG2, GPIO2_IOCFG);
2003 #endif
2004  idle();
2005 
2006  /* Re-configure GPIO0 */
2007  single_write(CC1200_IOCFG0, GPIO0_IOCFG);
2008 
2009  return RADIO_TX_ERR;
2010 
2011  }
2012 
2013 #if (CC1200_MAX_PAYLOAD_LEN > (CC1200_FIFO_SIZE - PHR_LEN)) && CC1200_WITH_TX_BUF
2014  if(bytes_left_to_write != 0) {
2015  rtimer_clock_t t0;
2016  uint8_t s;
2017  t0 = RTIMER_NOW();
2018  do {
2019  if((bytes_left_to_write != 0) &&
2020  (cc1200_arch_gpio2_read_pin() == 0)) {
2021  /* TX TIFO is drained below FIFO_THRESHOLD. Re-fill... */
2022  to_write = MIN(bytes_left_to_write, FIFO_THRESHOLD);
2023  burst_write(CC1200_TXFIFO, p, to_write);
2024  bytes_left_to_write -= to_write;
2025  p += to_write;
2026  t0 += CC1200_RF_CFG.tx_pkt_lifetime;
2027  }
2028  } while((cc1200_arch_gpio0_read_pin() == 1) &&
2029  RTIMER_CLOCK_LT(RTIMER_NOW(), t0 + CC1200_RF_CFG.tx_pkt_lifetime));
2030 
2031  /*
2032  * At this point we either left TX or a timeout occurred. If all went
2033  * well, we are in RX (or at least settling) now.
2034  * If we didn't manage to refill the TX FIFO, an underflow might
2035  * have occur-ed - the radio might be still in TX here!
2036  */
2037 
2038  s = state();
2039  if((s != STATE_RX) && (s != STATE_SETTLING)) {
2040 
2041  /*
2042  * Something bad happened. Wait for radio to enter a
2043  * stable state (in case of an error we are in TX here)
2044  */
2045 
2046  INFO("RF: TX failure!\n");
2047  RTIMER_BUSYWAIT_UNTIL((state() != STATE_TX), RTIMER_SECOND / 100);
2048  /* Re-configure GPIO2 */
2049  single_write(CC1200_IOCFG2, GPIO2_IOCFG);
2050  idle();
2051 
2052  /* Re-configure GPIO0 */
2053  single_write(CC1200_IOCFG0, GPIO0_IOCFG);
2054 
2055  return RADIO_TX_ERR;
2056 
2057  }
2058 
2059  } else {
2060  /* Wait for TX to complete */
2061  RTIMER_BUSYWAIT_UNTIL((cc1200_arch_gpio0_read_pin() == 0),
2062  CC1200_RF_CFG.tx_pkt_lifetime);
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 #endif
2069 
2070  if(cc1200_arch_gpio0_read_pin() == 1) {
2071  /* TX takes to long - abort */
2072  ERROR("RF: TX takes to long!\n");
2073 #if (CC1200_MAX_PAYLOAD_LEN > (CC1200_FIFO_SIZE - PHR_LEN))
2074  /* Re-configure GPIO2 */
2075  single_write(CC1200_IOCFG2, GPIO2_IOCFG);
2076 #endif
2077  idle();
2078 
2079  /* Re-configure GPIO0 */
2080  single_write(CC1200_IOCFG0, GPIO0_IOCFG);
2081 
2082  return RADIO_TX_ERR;
2083 
2084  }
2085 
2086 #if (CC1200_MAX_PAYLOAD_LEN > (CC1200_FIFO_SIZE - PHR_LEN))
2087  /* Re-configure GPIO2 */
2088  single_write(CC1200_IOCFG2, GPIO2_IOCFG);
2089 #endif
2090 
2091  /* Re-configure GPIO0 */
2092  single_write(CC1200_IOCFG0, GPIO0_IOCFG);
2093 
2094  TX_LEDS_OFF();
2095 
2096  ENERGEST_SWITCH(ENERGEST_TYPE_TRANSMIT, ENERGEST_TYPE_LISTEN);
2097 
2098  return RADIO_TX_OK;
2099 
2100 }
2101 /*---------------------------------------------------------------------------*/
2102 /* Update TX power */
2103 static void
2104 update_txpower(int8_t txpower_dbm)
2105 {
2106 
2107  uint8_t reg = single_read(CC1200_PA_CFG1);
2108 
2109  reg &= ~0x3F;
2110  /* Up to now we don't handle the special power levels PA_POWER_RAMP < 3 */
2111  reg |= ((((txpower_dbm + 18) * 2) - 1) & 0x3F);
2112  single_write(CC1200_PA_CFG1, reg);
2113 
2114  txpower = txpower_dbm;
2115 
2116 }
2117 /*---------------------------------------------------------------------------*/
2118 /* Update CCA threshold */
2119 static void
2120 update_cca_threshold(int8_t threshold_dbm)
2121 {
2122 
2123  single_write(CC1200_AGC_CS_THR, (uint8_t)threshold_dbm);
2124  cca_threshold = threshold_dbm;
2125 
2126 }
2127 /*---------------------------------------------------------------------------*/
2128 /* Calculate FREQ register from channel */
2129 static uint32_t
2130 calculate_freq(uint8_t channel)
2131 {
2132 
2133  uint32_t freq;
2134 
2135  freq = CC1200_RF_CFG.chan_center_freq0 + (channel * CC1200_RF_CFG.chan_spacing) / 1000 /* /1000 because chan_spacing is in Hz */;
2136  freq *= FREQ_MULTIPLIER;
2137  freq /= FREQ_DIVIDER;
2138 
2139  return freq;
2140 
2141 }
2142 /*---------------------------------------------------------------------------*/
2143 /* Update rf channel if possible, else postpone it (->pollhandler) */
2144 static int
2145 set_channel(uint8_t channel)
2146 {
2147 
2148  uint8_t was_off = 0;
2149  uint32_t freq;
2150 
2151  channel %= (CC1200_RF_CFG.max_channel - CC1200_RF_CFG.min_channel + 1);
2152  channel += CC1200_RF_CFG.min_channel;
2153 
2154 #if 0
2155  /*
2156  * We explicitly allow a channel update even if the channel does not change.
2157  * This feature can be used to manually force a calibration.
2158  */
2159  if(channel == rf_channel) {
2160  return rf_channel;
2161  }
2162 #endif
2163 
2164  if(channel < CC1200_RF_CFG.min_channel ||
2165  channel > CC1200_RF_CFG.max_channel) {
2166  /* Invalid channel */
2167  return CHANNEL_OUT_OF_LIMITS;
2168  }
2169 
2170  if(SPI_IS_LOCKED() || (rf_flags & RF_TX_ACTIVE) || receiving_packet()) {
2171 
2172  /* We are busy, postpone channel update */
2173 
2174  new_rf_channel = channel;
2175  rf_flags |= RF_UPDATE_CHANNEL;
2176  process_poll(&cc1200_process);
2177  INFO("RF: Channel update postponed\n");
2178 
2179  return CHANNEL_UPDATE_POSTPONED;
2180 
2181  }
2182  rf_flags &= ~RF_UPDATE_CHANNEL;
2183 
2184  INFO("RF: Channel update (%d)\n", channel);
2185 
2186  if(!(rf_flags & RF_ON)) {
2187  was_off = 1;
2188  on();
2189  }
2190 
2191  LOCK_SPI();
2192 
2193  idle();
2194 
2195  freq = calculate_freq(channel - CC1200_RF_CFG.min_channel);
2196  single_write(CC1200_FREQ0, ((uint8_t *)&freq)[0]);
2197  single_write(CC1200_FREQ1, ((uint8_t *)&freq)[1]);
2198  single_write(CC1200_FREQ2, ((uint8_t *)&freq)[2]);
2199 
2200  rf_channel = channel;
2201 
2202  /* Turn on RX again unless we turn off anyway */
2203  if(!was_off) {
2204 #ifdef RF_FORCE_CALIBRATION
2205  rf_flags |= RF_FORCE_CALIBRATION;
2206 #endif
2207  idle_calibrate_rx();
2208  }
2209 
2210  RELEASE_SPI();
2211 
2212  if(was_off) {
2213  off();
2214  }
2215 
2216  return CHANNEL_UPDATE_SUCCEEDED;
2217 
2218 }
2219 /*---------------------------------------------------------------------------*/
2220 /* Check broadcast address. */
2221 static int
2222 is_broadcast_addr(uint8_t mode, uint8_t *addr)
2223 {
2224 
2225  int i = mode == FRAME802154_SHORTADDRMODE ? 2 : 8;
2226 
2227  while(i-- > 0) {
2228  if(addr[i] != 0xff) {
2229  return 0;
2230  }
2231  }
2232 
2233  return 1;
2234 
2235 }
2236 /*---------------------------------------------------------------------------*/
2237 static int
2238 addr_check_auto_ack(uint8_t *frame, uint16_t frame_len)
2239 {
2240 
2241  frame802154_t info154;
2242 
2243  if(frame802154_parse(frame, frame_len, &info154) != 0) {
2244 
2245  /* We received a valid 802.15.4 frame */
2246 
2247  if(!(rx_mode_value & RADIO_RX_MODE_ADDRESS_FILTER) ||
2248  info154.fcf.frame_type == FRAME802154_ACKFRAME ||
2249  is_broadcast_addr(info154.fcf.dest_addr_mode,
2250  (uint8_t *)&info154.dest_addr) ||
2251  linkaddr_cmp((linkaddr_t *)&info154.dest_addr,
2252  &linkaddr_node_addr)) {
2253 
2254  /*
2255  * Address check succeeded or address filter disabled.
2256  * We send an ACK in case a corresponding data frame
2257  * is received even in promiscuous mode (if auto-ack is
2258  * enabled)!
2259  */
2260 
2261  if((rx_mode_value & RADIO_RX_MODE_AUTOACK) &&
2262  info154.fcf.frame_type == FRAME802154_DATAFRAME &&
2263  info154.fcf.ack_required != 0 &&
2264  (!(rx_mode_value & RADIO_RX_MODE_ADDRESS_FILTER) ||
2265  linkaddr_cmp((linkaddr_t *)&info154.dest_addr,
2266  &linkaddr_node_addr))) {
2267 
2268  /*
2269  * Data frame destined for us & ACK request bit set -> send ACK.
2270  * Make sure the preamble length is configured accordingly as
2271  * MAC timing parameters rely on this!
2272  */
2273 
2274  uint8_t ack[ACK_LEN] = { FRAME802154_ACKFRAME, 0, info154.seq };
2275 
2276 #if (RXOFF_MODE_RX == 1)
2277  /*
2278  * This turns off GPIOx interrupts. Make sure they are turned on
2279  * in rx_rx() later on!
2280  */
2281  idle();
2282 #endif
2283 
2284  prepare((const uint8_t *)ack, ACK_LEN);
2285  idle_tx_rx((const uint8_t *)ack, ACK_LEN);
2286 
2287  /* rx_rx() will follow */
2288 
2289  return ADDR_CHECK_OK_ACK_SEND;
2290 
2291  }
2292 
2293  return ADDR_CHECK_OK;
2294 
2295  } else {
2296 
2297  return ADDR_CHECK_FAILED;
2298 
2299  }
2300 
2301  }
2302 
2303  return INVALID_FRAME;
2304 
2305 }
2306 /*---------------------------------------------------------------------------*/
2307 /*
2308  * The CC1200 interrupt handler: called by the hardware interrupt
2309  * handler, which is defined as part of the cc1200-arch interface.
2310  */
2311 int
2312 cc1200_rx_interrupt(void)
2313 {
2314 
2315  /* The radio's state */
2316  uint8_t s;
2317  /* The number of bytes in the RX FIFO waiting for read-out */
2318  uint8_t num_rxbytes;
2319  /* The payload length read as the first byte from the RX FIFO */
2320  static uint16_t payload_len;
2321  /*
2322  * The number of bytes already read out and placed in the
2323  * intermediate buffer
2324  */
2325  static uint16_t bytes_read;
2326  /*
2327  * We use an intermediate buffer for the packet before
2328  * we pass it to the next upper layer. We also place RSSI +
2329  * LQI in this buffer
2330  */
2331  static uint8_t buf[CC1200_MAX_PAYLOAD_LEN + APPENDIX_LEN];
2332 
2333  /*
2334  * If CC1200_USE_GPIO2 is enabled, we come here either once RX FIFO
2335  * threshold is reached (GPIO2 rising edge)
2336  * or at the end of the packet (GPIO0 falling edge).
2337  */
2338 #if CC1200_USE_GPIO2
2339  int gpio2 = cc1200_arch_gpio2_read_pin();
2340  int gpio0 = cc1200_arch_gpio0_read_pin();
2341  if((rf_flags & RF_RX_ONGOING) == 0 && gpio2 > 0) {
2342  rf_flags |= RF_RX_ONGOING;
2343  sfd_timestamp = RTIMER_NOW();
2344  }
2345  if(gpio0 == 0) {
2346  rf_flags &= ~RF_RX_ONGOING;
2347  }
2348 #endif
2349 
2350  if(SPI_IS_LOCKED()) {
2351 
2352  /*
2353  * SPI is in use. Exit and make sure this
2354  * function is called from the poll handler as soon
2355  * as SPI is available again
2356  */
2357 
2358  rf_flags |= RF_POLL_RX_INTERRUPT;
2359  process_poll(&cc1200_process);
2360  return 1;
2361 
2362  }
2363  rf_flags &= ~RF_POLL_RX_INTERRUPT;
2364 
2365  LOCK_SPI();
2366 
2367  /*
2368  * If CC1200_USE_GPIO2 is enabled, we come here either once RX FIFO
2369  * threshold is reached (GPIO2 rising edge)
2370  * or at the end of the packet (GPIO0 falling edge).
2371  */
2372 
2373  /* Make sure we are in a sane state. Sane means: either RX or IDLE */
2374  s = state();
2375  if((s == STATE_RX_FIFO_ERR) || (s == STATE_TX_FIFO_ERR)) {
2376 
2377  rx_rx();
2378  RELEASE_SPI();
2379  return 0;
2380 
2381  }
2382 
2383  num_rxbytes = single_read(CC1200_NUM_RXBYTES);
2384 
2385  if(num_rxbytes == 0) {
2386 
2387  /*
2388  * This might happen from time to time because
2389  * this function is also called by the pollhandler and / or
2390  * from TWO interrupts which can occur at the same time.
2391  */
2392 
2393  INFO("RF: RX FIFO empty!\n");
2394  RELEASE_SPI();
2395  return 0;
2396 
2397  }
2398 
2399  if(!(rf_flags & RF_RX_PROCESSING_PKT)) {
2400 
2401 #if CC1200_802154G
2402  struct {
2403  uint8_t phra;
2404  uint8_t phrb;
2405  }
2406  phr;
2407 
2408  if(num_rxbytes < PHR_LEN) {
2409 
2410  WARNING("RF: PHR incomplete!\n");
2411  rx_rx();
2412  RELEASE_SPI();
2413  return 0;
2414 
2415  }
2416 
2417  burst_read(CC1200_RXFIFO,
2418  &phr,
2419  PHR_LEN);
2420  payload_len = (phr.phra & 0x07);
2421  payload_len <<= 8;
2422  payload_len += phr.phrb;
2423 
2424  if(phr.phra & (1 << 4)) {
2425  /* CRC16, payload_len += 2 */
2426  payload_len -= 2;
2427  } else {
2428  /* CRC16, payload_len += 4 */
2429  payload_len -= 4;
2430  }
2431 #else
2432  /* Read first byte in RX FIFO (payload length) */
2433  burst_read(CC1200_RXFIFO,
2434  (uint8_t *)&payload_len,
2435  PHR_LEN);
2436 #endif
2437 
2438  if(payload_len < ACK_LEN) {
2439  /* Packet to short. Discard it */
2440  WARNING("RF: Packet too short!\n");
2441  rx_rx();
2442  RELEASE_SPI();
2443  return 0;
2444  }
2445 
2446  if(payload_len > CC1200_MAX_PAYLOAD_LEN) {
2447  /* Packet to long. Discard it */
2448  WARNING("RF: Packet to long!\n");
2449  rx_rx();
2450  RELEASE_SPI();
2451  return 0;
2452  }
2453 
2454  RX_LEDS_ON();
2455  bytes_read = 0;
2456  num_rxbytes -= PHR_LEN;
2457 
2458  rf_flags |= RF_RX_PROCESSING_PKT;
2459 
2460  /* Fall through... */
2461 
2462  }
2463 
2464  if(rf_flags & RF_RX_PROCESSING_PKT) {
2465 
2466  /*
2467  * Read out remaining bytes unless FIFO is empty.
2468  * We have at least num_rxbytes in the FIFO to be read out.
2469  */
2470 
2471  if((num_rxbytes + bytes_read) > (payload_len + CC_APPENDIX_LEN)) {
2472 
2473  /*
2474  * We have a mismatch between the number of bytes in the RX FIFO
2475  * and the payload_len. This would lead to an buffer overflow,
2476  * so we catch this error here.
2477  */
2478 
2479  WARNING("RF: RX length mismatch %d %d %d!\n", num_rxbytes,
2480  bytes_read,
2481  payload_len);
2482  rx_rx();
2483  RELEASE_SPI();
2484  return 0;
2485 
2486  }
2487 
2488  burst_read(CC1200_RXFIFO,
2489  &buf[bytes_read],
2490  num_rxbytes);
2491 
2492  bytes_read += num_rxbytes;
2493  num_rxbytes = 0;
2494 
2495  if(bytes_read == (payload_len + CC_APPENDIX_LEN)) {
2496 
2497  /*
2498  * End of packet. Read appendix (if available), check CRC
2499  * and copy the data from temporary buffer to rx_pkt
2500  * RSSI offset already set using AGC_GAIN_ADJUST.GAIN_ADJUSTMENT
2501  */
2502 
2503 #if APPEND_STATUS
2504  uint8_t crc_lqi = buf[bytes_read - 1];
2505 #else
2506  int8_t rssi = single_read(CC1200_RSSI1);
2507  uint8_t crc_lqi = single_read(CC1200_LQI_VAL);
2508 #endif
2509 
2510  if(!(crc_lqi & (1 << 7))) {
2511  /* CRC error. Drop the packet */
2512  INFO("RF: CRC error!\n");
2513  } else if(rx_pkt_len != 0) {
2514  /* An old packet is pending. Drop the packet */
2515  WARNING("RF: Packet pending!\n");
2516  } else {
2517 
2518  int ret = addr_check_auto_ack(buf, bytes_read);
2519 
2520  if((ret == ADDR_CHECK_OK) ||
2521  (ret == ADDR_CHECK_OK_ACK_SEND)) {
2522 #if APPEND_STATUS
2523  /* RSSI + LQI already read out and placed into buf */
2524 #else
2525  buf[bytes_read++] = (uint8_t)rssi;
2526  buf[bytes_read++] = crc_lqi;
2527 #endif
2528  rx_pkt_len = bytes_read;
2529  memcpy((void *)rx_pkt, buf, rx_pkt_len);
2530  rx_rx();
2531  process_poll(&cc1200_process);
2532  RELEASE_SPI();
2533  return 1;
2534 
2535  } else {
2536  /* Invalid address. Drop the packet */
2537  }
2538 
2539  }
2540 
2541  /* Buffer full, address or CRC check failed */
2542  rx_rx();
2543  RELEASE_SPI();
2544  return 0;
2545 
2546  } /* if (bytes_read == payload_len) */
2547 
2548  }
2549 
2550  RELEASE_SPI();
2551  return 0;
2552 
2553 }
2554 /*---------------------------------------------------------------------------*/
radio_result_t(* get_object)(radio_param_t param, void *dest, size_t size)
Get a radio parameter object.
Definition: radio.h:290
void * packetbuf_dataptr(void)
Get a pointer to the data in the packetbuf.
Definition: packetbuf.c:143
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:252
#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
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:116
Header file for the energy estimation mechanism
#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
#define PROCESS_END()
Define the end of a process.
Definition: process.h:131
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:268
#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:283
int(* pending_packet)(void)
Check if the radio driver has just received a packet.
Definition: radio.h:271
void clock_delay_usec(uint16_t dt)
Delay a given number of microseconds.
Definition: clock.c:150
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
static void set_channel(uint8_t channel)
Set the current operating channel.
Definition: cc2538-rf.c:166
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
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
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:160
#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:258
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
#define PACKETBUF_SIZE
The size of the packetbuf, in bytes.
Definition: packetbuf.h:66
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
The radio reception mode controls address filtering and automatic transmission of acknowledgements in...
Definition: radio.h:214
uint8_t frame_type
3 bit.
Definition: frame802154.h:153
#define PROCESS_YIELD()
Yield the currently running process.
Definition: process.h:164
Parameters used by the frame802154_create() function.
Definition: frame802154.h:198
A timer.
Definition: etimer.h:75
#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
int linkaddr_cmp(const linkaddr_t *addr1, const linkaddr_t *addr2)
Compare two link-layer addresses.
Definition: linkaddr.c:69
#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:224
int(* read)(void *buf, unsigned short buf_len)
Read a received packet into a buffer.
Definition: radio.h:261
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:280
PROCESS_THREAD(cc2538_rf_process, ev, data)
Implementation of the cc2538 RF driver process.
Definition: cc2538-rf.c:1035
uint8_t dest_addr[8]
Destination address.
Definition: frame802154.h:202
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:296
Header file for the LED HAL.
uint8_t ack_required
1 bit.
Definition: frame802154.h:156
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
int(* on)(void)
Turn the radio on.
Definition: radio.h:274
#define RTIMER_BUSYWAIT(duration)
Busy-wait for a fixed duration.
Definition: rtimer.h:209
void process_start(struct process *p, process_data_t data)
Start a process.
Definition: process.c:99