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