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