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