cc2538_rf.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2016 MUTEX NZ Ltd.
3  * Copyright (C) 2015 Loci Controls Inc.
4  *
5  * This file is subject to the terms and conditions of the GNU Lesser
6  * General Public License v2.1. See the file LICENSE in the top level
7  * directory for more details.
8  *
9  */
10 
11 #pragma once
12 
24 #include <stdbool.h>
25 
26 #include "board.h"
27 #include "cc2538_rfcore.h"
28 
29 #include "net/ieee802154.h"
30 #include "kernel_defines.h"
31 
32 #include "net/ieee802154/radio.h"
33 
34 #include "net/netopt.h"
35 
36 #ifdef __cplusplus
37 extern "C" {
38 #endif
39 
40 #define CC2538_AUTOCRC_LEN (2)
41 #define CC2538_RF_FIFO_SIZE (128)
42 #define CC2538_PACKET_LENGTH_SIZE (1)
43 #define CC2538_LENGTH_BYTE_MASK (0x7F)
45 #define CC2538_RF_MAX_DATA_LEN (CC2538_RF_FIFO_SIZE - CC2538_PACKET_LENGTH_SIZE)
46 
47 /* TODO: Move these to sys/include/net/ieee802154.h somehow */
48 /* IEEE 802.15.4 defined constants (2.4 GHz logical channels) */
49 #define IEEE802154_MIN_FREQ (2405)
50 #define IEEE802154_MAX_FREQ (2480)
52 #define IEEE802154_CHANNEL_SPACING (5)
54 #define IEEE802154_CHAN2FREQ(chan) ( IEEE802154_MIN_FREQ + ((chan) - IEEE802154_CHANNEL_MIN) * IEEE802154_CHANNEL_SPACING )
55 #define IEEE802154_FREQ2CHAN(freq) ( IEEE802154_CHANNEL_MIN + ((freq) - IEEE802154_MIN_FREQ) / IEEE802154_CHANNEL_SPACING )
56 /* /TODO */
57 
58 #define CC2538_MIN_FREQ (2394)
59 #define CC2538_MAX_FREQ (2507)
60 
61 #define CC2538_RF_POWER_DEFAULT (CONFIG_IEEE802154_DEFAULT_TXPOWER)
62 #define CC2538_RF_CHANNEL_DEFAULT (CONFIG_IEEE802154_DEFAULT_CHANNEL)
63 
64 #define OUTPUT_POWER_MIN (-24)
65 #define OUTPUT_POWER_MAX (7)
66 #define NUM_POWER_LEVELS ( OUTPUT_POWER_MAX - OUTPUT_POWER_MIN + 1 )
67 
68 #define CC2538_CORR_VAL_MIN (50U)
69 #define CC2538_CORR_VAL_MAX (110U)
70 #define CC2538_CORR_VAL_MASK (0x7F)
71 
72 #define CC2538_CRC_BIT_MASK (0x80)
73 
74 #define CC2538_CCA_THR_MASK (0x000000FF)
76 #define CC2538_CCA_MODE_MASK (0x18)
77 #define CC2538_CCA_MODE_POS (3U)
79 #define CC2538_CSP_SKIP_INST_MASK (0x70)
80 #define CC2538_CSP_SKIP_INST_POS (4U)
82 #define CC2538_CSP_SKIP_N_MASK (0x08)
84 #define CC2538_CSP_SKIP_COND_CCA (0x00)
85 #define CC2538_CSP_SKIP_COND_CSPZ (0x06)
86 #define CC2538_CSP_SKIP_COND_RSSI (0x07)
88 #define CC2538_SFR_MTMSEL_MASK (0x7)
89 #define CC2538_SFR_MTMSEL_TIMER_P (0x2)
90 #define CC2538_MCTRL_SYNC_MASK (0x2)
91 #define CC2538_MCTRL_RUN_MASK (0x1)
93 #define CC2538_CSP_MCU_CTRL_MASK (0x1)
95 #define CC2538_CSP_INCMAXY_MAX_MASK (0x7)
98 #define CC2538_RXENABLE_RXON_MASK (0x80)
100 #define CC2538_RSSI_OFFSET (-73)
101 #define CC2538_RF_SENSITIVITY (-97)
103 #define CC2538_ACCEPT_FT_0_BEACON (1 << 3)
104 #define CC2538_ACCEPT_FT_1_DATA (1 << 4)
105 #define CC2538_ACCEPT_FT_2_ACK (1 << 5)
106 #define CC2538_ACCEPT_FT_3_CMD (1 << 6)
107 #define CC2538_STATE_SFD_WAIT_RANGE_MIN (0x03U)
108 #define CC2538_STATE_SFD_WAIT_RANGE_MAX (0x06U)
109 #define CC2538_FRMCTRL1_PENDING_OR_MASK (0x04)
111 #define CC2538_FRMCTRL0_RX_MODE_DIS (0xC)
114 #define RFCORE_ASSERT(expr) (void)( (expr) || RFCORE_ASSERT_failure(#expr, __FUNCTION__, __LINE__) )
115 
116 #if DEVELHELP
117 #define RFCORE_WAIT_UNTIL(expr) while (!(expr)) { \
118  DEBUG("RFCORE_WAIT_UNTIL(%s) at line %u in %s()\n", #expr, __LINE__, __FUNCTION__); \
119  thread_yield(); \
120 }
121 #else
122 #define RFCORE_WAIT_UNTIL(expr) while (!(expr)) thread_yield()
123 #endif
124 
125 #define RFCORE_FLUSH_RECEIVE_FIFO() rfcore_strobe(ISFLUSHRX)
126 
127 #define ABS_DIFF(x, y) ( ((x) < (y))? ((y) - (x)) : ((x) - (y)) )
128 #define BOOLEAN(x) ( (x) != 0 )
129 #define NOT(x) ( (x) == 0 )
130 #define GET_BYTE(buffer, index) ( (unsigned char*)(buffer) )[index]
131 
132 #define BIT(n) ( 1 << (n) )
133 
134 enum {
135  FSM_STATE_IDLE = 0,
136  FSM_STATE_RX_CALIBRATION = 2,
137  FSM_STATE_TX_CALIBRATION = 32,
138 };
139 
143 enum {
144  STROBE_ERR = BIT(6),
145  TXUNDERF = BIT(5),
146  TXOVERF = BIT(4),
147  RXUNDERF = BIT(3),
148  RXOVERF = BIT(2),
149  RXABO = BIT(1),
150  NLOCK = BIT(0),
151 };
152 
156 enum {
157  SET_RXENMASK_ON_TX = BIT(0),
158  IGNORE_TX_UNDERF = BIT(1),
159  PENDING_OR = BIT(2),
160 };
161 
165 enum {
166  ENERGY_SCAN = BIT(4),
167  AUTOACK = BIT(5),
168  AUTOCRC = BIT(6),
169  APPEND_DATA_MODE = BIT(7),
170 };
171 
175 enum {
176  ACT_UNUSED = BIT(0),
177  SFD = BIT(1),
178  FIFOP = BIT(2),
179  SRC_MATCH_DONE = BIT(3),
180  SRC_MATCH_FOUND = BIT(4),
181  FRAME_ACCEPTED = BIT(5),
182  RXPKTDONE = BIT(6),
183  RXMASKZERO = BIT(7),
184 };
185 
189 enum {
190  TXACKDONE = BIT(0),
191  TXDONE = BIT(1),
192  RF_IDLE = BIT(2),
193  CSP_MANINT = BIT(3),
194  CSP_STOP = BIT(4),
195  CSP_WAIT = BIT(5),
196 };
197 
201 enum {
202  rfc_obs_sig0 = 0,
203  rfc_obs_sig1 = 1,
204  rfc_obs_sig2 = 2,
205 };
206 
210 enum {
213  rfc_sniff_data = 0x08,
215  rfc_sniff_clk = 0x09,
216  rssi_valid = 0x0c,
219  demod_cca = 0x0d,
222  sampled_cca = 0x0e,
225  sfd_sync = 0x0f,
229  tx_active = 0x10,
231  rx_active = 0x11,
233  ffctrl_fifo = 0x12,
235  ffctrl_fifop = 0x13,
241  packet_done = 0x14,
244  rfc_xor_rand_i_q = 0x16,
246  rfc_rand_q = 0x17,
248  rfc_rand_i = 0x18,
250  lock_status = 0x19,
251  pa_pd = 0x20,
252  lna_pd = 0x2a,
253  disabled = 0xff,
254 };
255 
259 #ifndef CONFIG_CC2538_RF_OBS_0
260 #define CONFIG_CC2538_RF_OBS_0 tx_active
261 #endif
262 #ifndef CONFIG_CC2538_RF_OBS_1
263 #define CONFIG_CC2538_RF_OBS_1 rx_active
264 #endif
265 #ifndef CONFIG_CC2538_RF_OBS_2
266 #define CONFIG_CC2538_RF_OBS_2 rssi_valid
267 #endif
268 
269 /* Default configuration for cc2538dk or similar */
270 #ifndef CONFIG_CC2538_RF_OBS_SIG_0_PCX
271 #define CONFIG_CC2538_RF_OBS_SIG_0_PCX 0 /* PC0 = LED_1 (red) */
272 #endif
273 #ifndef CONFIG_CC2538_RF_OBS_SIG_1_PCX
274 #define CONFIG_CC2538_RF_OBS_SIG_1_PCX 1 /* PC0 = LED_2 (red) */
275 #endif
276 #ifndef CONFIG_CC2538_RF_OBS_SIG_2_PCX
277 #define CONFIG_CC2538_RF_OBS_SIG_2_PCX 2 /* PC0 = LED_3 (red) */
278 #endif
279 #if ((CONFIG_CC2538_RF_OBS_SIG_2_PCX > 7) || \
280  (CONFIG_CC2538_RF_OBS_SIG_1_PCX > 7) || \
281  (CONFIG_CC2538_RF_OBS_SIG_0_PCX > 7))
282 #error "CONFIG_CC2538_RF_OBS_SIG_X_PCX must be between 0-7 (PC0-PC7)"
283 #endif
284 
288 typedef struct {
289  uint8_t state;
290 } cc2538_rf_t;
291 
300 
304 static inline void cc2538_rf_enable_irq(void)
305 {
306  RFCORE_XREG_RFIRQM1 = TXDONE | CSP_STOP | TXACKDONE;
308 }
309 
313 static inline void cc2538_rf_disable_irq(void)
314 {
317 }
318 
324 
332 
338 void cc2538_get_addr_long(uint8_t *addr);
339 
345 void cc2538_get_addr_short(uint8_t *addr);
346 
352 unsigned int cc2538_get_chan(void);
353 
361 
367 uint16_t cc2538_get_pan(void);
368 
375 
380 void cc2538_init(void);
381 
388 bool cc2538_is_on(void);
389 
394 void cc2538_off(void);
395 
400 bool cc2538_on(void);
401 
408 
414 void cc2538_set_addr_short(const uint8_t *addr);
415 
421 void cc2538_set_addr_long(const uint8_t *addr);
422 
428 void cc2538_set_chan(unsigned int chan);
429 
435 void cc2538_set_freq(unsigned int MHz);
436 
442 void cc2538_set_monitor(bool mode);
443 
449 void cc2538_set_pan(uint16_t pan);
450 
458 
464 void cc2538_set_tx_power(int dBm);
465 
466 #ifdef __cplusplus
467 }
468 #endif
469 
#define RFCORE_XREG_RFIRQM0
RF interrupt masks.
Definition: cc2538.h:383
#define RFCORE_XREG_RFIRQM1
RF interrupt masks.
Definition: cc2538.h:384
void cc2538_set_pan(uint16_t pan)
Set the PAN ID of the device.
void cc2538_set_addr_long(const uint8_t *addr)
Set the long address of the device.
unsigned int cc2538_get_chan(void)
Get the configured channel number of the device.
void cc2538_off(void)
Deactivate the CC2538 radio device.
bool cc2538_on(void)
Activate the CC2538 radio device.
void cc2538_rf_hal_setup(ieee802154_dev_t *hal)
Setup CC2538 in order to be used with the IEEE 802.15.4 Radio HAL.
void cc2538_irq_handler(void)
IRQ handler for RF events.
bool cc2538_channel_clear(void)
Trigger a clear channel assessment.
void cc2538_set_addr_short(const uint8_t *addr)
Set the short address of the device.
void cc2538_set_chan(unsigned int chan)
Set the channel number of the device.
static void cc2538_rf_enable_irq(void)
Enable CC2538 RF IRQs.
Definition: cc2538_rf.h:304
void cc2538_init(void)
Initialise the CC2538 radio hardware.
uint16_t cc2538_get_pan(void)
Get the configured PAN ID of the device.
void cc2538_set_tx_power(int dBm)
Set the transmission power for the device.
void cc2538_set_monitor(bool mode)
Enable/disable monitor (promiscuous) mode for the device.
@ RXPKTDONE
End of frame event.
Definition: cc2538_rf.h:182
@ SFD
Start of frame event.
Definition: cc2538_rf.h:177
void cc2538_get_addr_short(uint8_t *addr)
Get the configured short address of the device.
static void cc2538_rf_disable_irq(void)
Disable CC2538 RF IRQs.
Definition: cc2538_rf.h:313
bool cc2538_is_on(void)
Check if device is active.
bool cc2538_get_monitor(void)
Check if device is in monitor (promiscuous) mode.
void cc2538_set_state(cc2538_rf_t *dev, netopt_state_t state)
Set the state of the device.
@ rfc_rand_i
Random data output from the I channel of the receiver.
Definition: cc2538_rf.h:248
@ lna_pd
LNA power-down signal.
Definition: cc2538_rf.h:252
@ rssi_valid
Pin is high when the RSSI value has been updated at least once since RX was started.
Definition: cc2538_rf.h:216
@ packet_done
A complete frame has been received.
Definition: cc2538_rf.h:241
@ rx_active
Indicates that FFCTRL is in one of the RX states.
Definition: cc2538_rf.h:231
@ disabled
disabled
Definition: cc2538_rf.h:253
@ constant_value_0
Constant value 0.
Definition: cc2538_rf.h:211
@ sampled_cca
A sampled version of the CCA bit from demodulator.
Definition: cc2538_rf.h:222
@ rfc_xor_rand_i_q
XOR between I and Q random outputs.
Definition: cc2538_rf.h:244
@ rfc_sniff_data
Data from packet sniffer.
Definition: cc2538_rf.h:213
@ ffctrl_fifop
Pin is high when the number of bytes in the RXFIFO exceeds the programmable threshold or at least one...
Definition: cc2538_rf.h:235
@ pa_pd
Power amplifier power-down signal.
Definition: cc2538_rf.h:251
@ sfd_sync
Pin is high when a SFD has been received or transmitted.
Definition: cc2538_rf.h:225
@ demod_cca
Clear channel assessment.
Definition: cc2538_rf.h:219
@ constant_value_1
Constant value 1.
Definition: cc2538_rf.h:212
@ tx_active
Indicates that FFCTRL is in one of the TX states.
Definition: cc2538_rf.h:229
@ ffctrl_fifo
Pin is high when one or more bytes are in the RXFIFO.
Definition: cc2538_rf.h:233
@ rfc_rand_q
Random data output from the Q channel of the receiver.
Definition: cc2538_rf.h:246
@ rfc_sniff_clk
250kHz clock for packet sniffer data.
Definition: cc2538_rf.h:215
@ lock_status
1 when PLL is in lock, otherwise 0
Definition: cc2538_rf.h:250
void cc2538_set_freq(unsigned int MHz)
Set the frequency of the device.
void cc2538_get_addr_long(uint8_t *addr)
Get the configured long address of the device.
int cc2538_get_tx_power(void)
Get the configured transmission power of the device.
void cc2538_setup(cc2538_rf_t *dev)
Setup a CC2538 radio device.
CC2538 RF core interface.
netopt_state_t
Option parameter to be used with NETOPT_STATE to set or get the state of a network device or protocol...
Definition: netopt.h:893
Common macros and compiler attributes/pragmas configuration.
Definition of global configuration options.
Device descriptor for CC2538 transceiver.
Definition: cc2538_rf.h:288
uint8_t state
current state of the radio
Definition: cc2538_rf.h:289
the IEEE802.15.4 device descriptor
Definition: radio.h:414
IEEE 802.15.4 header definitions.