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 
22 #ifndef CC2538_RF_H
23 #define CC2538_RF_H
24 
25 #include <stdbool.h>
26 
27 #include "board.h"
28 
29 #include "net/ieee802154.h"
30 #include "kernel_defines.h"
31 
32 #if IS_USED(MODULE_IEEE802154_RADIO_HAL)
33 #include "net/ieee802154/radio.h"
34 #if IS_USED(MODULE_NETDEV_IEEE802154_SUBMAC)
35 #include "net/netdev/ieee802154_submac.h"
36 #endif
37 #else
38 #include "net/netdev.h"
39 #include "net/netdev/ieee802154.h"
40 #endif
41 
42 #include "net/netopt.h"
43 
44 #ifdef __cplusplus
45 extern "C" {
46 #endif
47 
48 #define CC2538_AUTOCRC_LEN (2)
49 #define CC2538_RF_FIFO_SIZE (128)
50 #define CC2538_PACKET_LENGTH_SIZE (1)
51 
52 #define CC2538_RF_MAX_DATA_LEN (CC2538_RF_FIFO_SIZE - CC2538_PACKET_LENGTH_SIZE)
53 
54 /* TODO: Move these to sys/include/net/ieee802154.h somehow */
55 /* IEEE 802.15.4 defined constants (2.4 GHz logical channels) */
56 #define IEEE802154_MIN_FREQ (2405)
57 #define IEEE802154_MAX_FREQ (2480)
59 #define IEEE802154_CHANNEL_SPACING (5)
61 #define IEEE802154_CHAN2FREQ(chan) ( IEEE802154_MIN_FREQ + ((chan) - IEEE802154_CHANNEL_MIN) * IEEE802154_CHANNEL_SPACING )
62 #define IEEE802154_FREQ2CHAN(freq) ( IEEE802154_CHANNEL_MIN + ((freq) - IEEE802154_MIN_FREQ) / IEEE802154_CHANNEL_SPACING )
63 /* /TODO */
64 
65 #define CC2538_MIN_FREQ (2394)
66 #define CC2538_MAX_FREQ (2507)
67 
68 #define CC2538_RF_POWER_DEFAULT (CONFIG_IEEE802154_DEFAULT_TXPOWER)
69 #define CC2538_RF_CHANNEL_DEFAULT (CONFIG_IEEE802154_DEFAULT_CHANNEL)
70 
71 #define OUTPUT_POWER_MIN (-24)
72 #define OUTPUT_POWER_MAX (7)
73 #define NUM_POWER_LEVELS ( OUTPUT_POWER_MAX - OUTPUT_POWER_MIN + 1 )
74 
75 #define CC2538_CORR_VAL_MIN (50U)
76 #define CC2538_CORR_VAL_MAX (110U)
77 #define CC2538_CORR_VAL_MASK (0x7F)
78 
79 #define CC2538_CRC_BIT_MASK (0x80)
80 
81 #define CC2538_CCA_THR_MASK (0x000000FF)
83 #define CC2538_CCA_MODE_MASK (0x18)
84 #define CC2538_CCA_MODE_POS (3U)
86 #define CC2538_CSP_SKIP_INST_MASK (0x70)
87 #define CC2538_CSP_SKIP_INST_POS (4U)
89 #define CC2538_CSP_SKIP_N_MASK (0x08)
91 #define CC2538_CSP_SKIP_COND_CCA (0x00)
92 #define CC2538_CSP_SKIP_COND_CSPZ (0x06)
93 #define CC2538_CSP_SKIP_COND_RSSI (0x07)
95 #define CC2538_SFR_MTMSEL_MASK (0x7)
96 #define CC2538_SFR_MTMSEL_TIMER_P (0x2)
97 #define CC2538_MCTRL_SYNC_MASK (0x2)
98 #define CC2538_MCTRL_RUN_MASK (0x1)
100 #define CC2538_CSP_MCU_CTRL_MASK (0x1)
102 #define CC2538_CSP_INCMAXY_MAX_MASK (0x7)
105 #define CC2538_RXENABLE_RXON_MASK (0x80)
107 #define CC2538_RSSI_OFFSET (-73)
108 #define CC2538_RF_SENSITIVITY (-97)
110 #define CC2538_ACCEPT_FT_2_ACK (1 << 5)
111 #define CC2538_STATE_SFD_WAIT_RANGE_MIN (0x03U)
112 #define CC2538_STATE_SFD_WAIT_RANGE_MAX (0x06U)
113 #define CC2538_FRMCTRL1_PENDING_OR_MASK (0x04)
116 #define RFCORE_ASSERT(expr) (void)( (expr) || RFCORE_ASSERT_failure(#expr, __FUNCTION__, __LINE__) )
117 
118 #if DEVELHELP
119 #define RFCORE_WAIT_UNTIL(expr) while (!(expr)) { \
120  DEBUG("RFCORE_WAIT_UNTIL(%s) at line %u in %s()\n", #expr, __LINE__, __FUNCTION__); \
121  thread_yield(); \
122 }
123 #else
124 #define RFCORE_WAIT_UNTIL(expr) while (!(expr)) thread_yield()
125 #endif
126 
127 #define RFCORE_FLUSH_RECEIVE_FIFO() rfcore_strobe(ISFLUSHRX)
128 
129 #define ABS_DIFF(x, y) ( ((x) < (y))? ((y) - (x)) : ((x) - (y)) )
130 #define BOOLEAN(x) ( (x) != 0 )
131 #define NOT(x) ( (x) == 0 )
132 #define GET_BYTE(buffer, index) ( (unsigned char*)(buffer) )[index]
133 
134 #define BIT(n) ( 1 << (n) )
135 
136 enum {
137  FSM_STATE_IDLE = 0,
138  FSM_STATE_RX_CALIBRATION = 2,
139  FSM_STATE_TX_CALIBRATION = 32,
140 };
141 
142 /*
143  * @brief RFCORE_XREG_RFERRM bits
144  */
145 enum {
146  STROBE_ERR = BIT(6),
147  TXUNDERF = BIT(5),
148  TXOVERF = BIT(4),
149  RXUNDERF = BIT(3),
150  RXOVERF = BIT(2),
151  RXABO = BIT(1),
152  NLOCK = BIT(0),
153 };
154 
155  /*
156  * @brief RFCORE_XREG_FRMCTRL0 bits
157  */
158 enum {
159  SET_RXENMASK_ON_TX = BIT(0),
160  IGNORE_TX_UNDERF = BIT(1),
161  PENDING_OR = BIT(2),
162 };
163 
164  /*
165  * @brief RFCORE_XREG_FRMCTRL1 bits
166  */
167 enum {
168  ENERGY_SCAN = BIT(4),
169  AUTOACK = BIT(5),
170  AUTOCRC = BIT(6),
171  APPEND_DATA_MODE = BIT(7),
172 };
173 
174 /*
175  * @brief RFCORE_XREG_RFIRQM0 / RFCORE_XREG_RFIRQF0 bits
176  */
177 enum {
178  ACT_UNUSED = BIT(0),
179  SFD = BIT(1),
180  FIFOP = BIT(2),
181  SRC_MATCH_DONE = BIT(3),
182  SRC_MATCH_FOUND = BIT(4),
183  FRAME_ACCEPTED = BIT(5),
184  RXPKTDONE = BIT(6),
185  RXMASKZERO = BIT(7),
186 };
187 
188 /*
189  * @brief RFCORE_XREG_RFIRQM1 / RFCORE_XREG_RFIRQF1 bits
190  */
191 enum {
192  TXACKDONE = BIT(0),
193  TXDONE = BIT(1),
194  RF_IDLE = BIT(2),
195  CSP_MANINT = BIT(3),
196  CSP_STOP = BIT(4),
197  CSP_WAIT = BIT(5),
198 };
199 
200 /* Values for use with CCTEST_OBSSELx registers: */
201 enum {
202  rfc_obs_sig0 = 0,
203  rfc_obs_sig1 = 1,
204  rfc_obs_sig2 = 2,
205 };
206 
207 /* Values for RFCORE_XREG_RFC_OBS_CTRLx registers: */
208 enum {
211  rfc_sniff_data = 0x08,
213  rfc_sniff_clk = 0x09,
214  rssi_valid = 0x0c,
217  demod_cca = 0x0d,
220  sampled_cca = 0x0e,
223  sfd_sync = 0x0f,
227  tx_active = 0x10,
229  rx_active = 0x11,
231  ffctrl_fifo = 0x12,
233  ffctrl_fifop = 0x13,
239  packet_done = 0x14,
244  rfc_rand_q = 0x17,
246  rfc_rand_i = 0x18,
248  lock_status = 0x19,
249  pa_pd = 0x20,
250  lna_pd = 0x2a,
251  disabled = 0xff,
252 };
253 
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 
292 typedef struct {
293 #if IS_USED(MODULE_NETDEV_IEEE802154_SUBMAC)
295 #elif !IS_USED(MODULE_IEEE802154_RADIO_HAL)
297 #endif
298  uint8_t state;
299 } cc2538_rf_t;
300 
305 void cc2538_irq_handler(void);
306 
313 bool cc2538_channel_clear(void);
314 
320 void cc2538_get_addr_long(uint8_t *addr);
321 
327 void cc2538_get_addr_short(uint8_t *addr);
328 
334 unsigned int cc2538_get_chan(void);
335 
342 bool cc2538_get_monitor(void);
343 
349 uint16_t cc2538_get_pan(void);
350 
356 int cc2538_get_tx_power(void);
357 
362 void cc2538_init(void);
363 
370 bool cc2538_is_on(void);
371 
376 void cc2538_off(void);
377 
382 bool cc2538_on(void);
383 
389 void cc2538_setup(cc2538_rf_t *dev);
390 
396 void cc2538_set_addr_short(const uint8_t *addr);
397 
403 void cc2538_set_addr_long(const uint8_t *addr);
404 
410 void cc2538_set_chan(unsigned int chan);
411 
417 void cc2538_set_freq(unsigned int MHz);
418 
424 void cc2538_set_monitor(bool mode);
425 
431 void cc2538_set_pan(uint16_t pan);
432 
440 
446 void cc2538_set_tx_power(int dBm);
447 
448 #ifdef __cplusplus
449 }
450 #endif
451 
452 #endif /* CC2538_RF_H */
453 
cc2538_set_pan
void cc2538_set_pan(uint16_t pan)
Set the PAN ID of the device.
rx_active
@ rx_active
Indicates that FFCTRL is in one of the RX states.
Definition: cc2538_rf.h:229
cc2538_is_on
bool cc2538_is_on(void)
Check if device is active.
ieee802154.h
IEEE 802.15.4 header definitions.
cc2538_rf_t
Device descriptor for CC2538 transceiver.
Definition: cc2538_rf.h:292
rfc_rand_q
@ rfc_rand_q
Random data output from the Q channel of the receiver.
Definition: cc2538_rf.h:244
kernel_defines.h
Common macros and compiler attributes/pragmas configuration.
ieee802154.h
Definitions for netdev common IEEE 802.15.4 code.
netopt.h
Definition of global configuration options.
pa_pd
@ pa_pd
Power amplifier power-down signal.
Definition: cc2538_rf.h:249
rfc_rand_i
@ rfc_rand_i
Random data output from the I channel of the receiver.
Definition: cc2538_rf.h:246
rfc_sniff_data
@ rfc_sniff_data
Data from packet sniffer.
Definition: cc2538_rf.h:211
cc2538_irq_handler
void cc2538_irq_handler(void)
IRQ handler for RF events.
ffctrl_fifo
@ ffctrl_fifo
Pin is high when one or more bytes are in the RXFIFO.
Definition: cc2538_rf.h:231
disabled
@ disabled
disabled
Definition: cc2538_rf.h:251
cc2538_set_addr_long
void cc2538_set_addr_long(const uint8_t *addr)
Set the long address of the device.
lock_status
@ lock_status
1 when PLL is in lock, otherwise 0
Definition: cc2538_rf.h:248
netopt_state_t
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:805
sfd_sync
@ sfd_sync
Pin is high when a SFD has been received or transmitted.
Definition: cc2538_rf.h:223
cc2538_on
bool cc2538_on(void)
Activate the CC2538 radio device.
cc2538_set_state
void cc2538_set_state(cc2538_rf_t *dev, netopt_state_t state)
Set the state of the device.
cc2538_set_monitor
void cc2538_set_monitor(bool mode)
Enable/disable monitor (promiscuous) mode for the device.
rssi_valid
@ rssi_valid
Pin is high when the RSSI value has been updated at least once since RX was started.
Definition: cc2538_rf.h:214
netdev.h
Definitions low-level network driver interface.
tx_active
@ tx_active
Indicates that FFCTRL is in one of the TX states.
Definition: cc2538_rf.h:227
cc2538_get_addr_long
void cc2538_get_addr_long(uint8_t *addr)
Get the configured long address of the device.
cc2538_set_freq
void cc2538_set_freq(unsigned int MHz)
Set the frequency of the device.
cc2538_set_tx_power
void cc2538_set_tx_power(int dBm)
Set the transmission power for the device.
demod_cca
@ demod_cca
Clear channel assessment.
Definition: cc2538_rf.h:217
cc2538_set_chan
void cc2538_set_chan(unsigned int chan)
Set the channel number of the device.
ffctrl_fifop
@ 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:233
cc2538_setup
void cc2538_setup(cc2538_rf_t *dev)
Setup a CC2538 radio device for use with netdev.
cc2538_get_chan
unsigned int cc2538_get_chan(void)
Get the configured channel number of the device.
netdev
Structure to hold driver state.
Definition: netdev.h:302
cc2538_set_addr_short
void cc2538_set_addr_short(const uint8_t *addr)
Set the short address of the device.
cc2538_rf_t::state
uint8_t state
current state of the radio
Definition: cc2538_rf.h:298
cc2538_channel_clear
bool cc2538_channel_clear(void)
Trigger a clear channel assessment.
cc2538_get_pan
uint16_t cc2538_get_pan(void)
Get the configured PAN ID of the device.
rfc_xor_rand_i_q
@ rfc_xor_rand_i_q
XOR between I and Q random outputs.
Definition: cc2538_rf.h:242
netdev_ieee802154_t
Extended structure to hold IEEE 802.15.4 driver state.
Definition: ieee802154.h:90
packet_done
@ packet_done
A complete frame has been received.
Definition: cc2538_rf.h:239
sampled_cca
@ sampled_cca
A sampled version of the CCA bit from demodulator.
Definition: cc2538_rf.h:220
cc2538_rf_t::netdev
netdev_ieee802154_t netdev
netdev parent struct
Definition: cc2538_rf.h:296
netdev_ieee802154_submac_t
IEEE 802.15.4 SubMAC netdev descriptor.
Definition: ieee802154_submac.h:47
cc2538_get_monitor
bool cc2538_get_monitor(void)
Check if device is in monitor (promiscuous) mode.
constant_value_1
@ constant_value_1
Constant value 1.
Definition: cc2538_rf.h:210
lna_pd
@ lna_pd
LNA power-down signal.
Definition: cc2538_rf.h:250
cc2538_init
void cc2538_init(void)
Initialise the CC2538 radio hardware.
cc2538_get_addr_short
void cc2538_get_addr_short(uint8_t *addr)
Get the configured short address of the device.
cc2538_off
void cc2538_off(void)
Deactivate the CC2538 radio device.
RXPKTDONE
@ RXPKTDONE
End of frame event.
Definition: cc2538_rf.h:184
cc2538_get_tx_power
int cc2538_get_tx_power(void)
Get the configured transmission power of the device.
constant_value_0
@ constant_value_0
Constant value 0.
Definition: cc2538_rf.h:209
SFD
@ SFD
Start of frame event.
Definition: cc2538_rf.h:179
rfc_sniff_clk
@ rfc_sniff_clk
250kHz clock for packet sniffer data.
Definition: cc2538_rf.h:213