1 /* ieee802154_rf2xx.c - ATMEL RF2XX IEEE 802.15.4 Driver */
2
3 #define DT_DRV_COMPAT atmel_rf2xx
4
5 /*
6 * Copyright (c) 2019-2020 Gerson Fernando Budke
7 *
8 * SPDX-License-Identifier: Apache-2.0
9 */
10
11 #define LOG_MODULE_NAME ieee802154_rf2xx
12 #define LOG_LEVEL CONFIG_IEEE802154_DRIVER_LOG_LEVEL
13
14 #include <zephyr/logging/log.h>
15 LOG_MODULE_REGISTER(LOG_MODULE_NAME);
16
17 #include <errno.h>
18 #include <stdio.h>
19 #include <stdlib.h>
20
21 #include <zephyr/kernel.h>
22 #include <zephyr/arch/cpu.h>
23 #include <zephyr/debug/stack.h>
24
25 #include <zephyr/device.h>
26 #include <zephyr/init.h>
27 #include <zephyr/net/net_if.h>
28 #include <zephyr/net/net_pkt.h>
29
30 #include <zephyr/sys/byteorder.h>
31 #include <string.h>
32 #include <zephyr/random/random.h>
33 #include <zephyr/linker/sections.h>
34 #include <zephyr/sys/atomic.h>
35
36 #include <zephyr/drivers/spi.h>
37 #include <zephyr/drivers/gpio.h>
38
39 #include <zephyr/net/ieee802154_radio.h>
40
41 #include "ieee802154_rf2xx.h"
42 #include "ieee802154_rf2xx_regs.h"
43 #include "ieee802154_rf2xx_iface.h"
44
45 #if defined(CONFIG_NET_L2_OPENTHREAD)
46 #include <zephyr/net/openthread.h>
47
48 #define RF2XX_OT_PSDU_LENGTH 1280
49
50 #define RF2XX_ACK_FRAME_LEN 3
51 #define RF2XX_ACK_FRAME_TYPE (2 << 0)
52 #define RF2XX_ACK_FRAME_PENDING_BIT (1 << 4)
53 #define RF2XX_FRAME_CTRL_ACK_REQUEST_BIT (1 << 5)
54
55 static uint8_t rf2xx_ack_psdu[RF2XX_ACK_FRAME_LEN] = { 0 };
56 static struct net_buf rf2xx_ack_frame = {
57 .data = rf2xx_ack_psdu,
58 .size = RF2XX_ACK_FRAME_LEN,
59 .len = RF2XX_ACK_FRAME_LEN,
60 .__buf = rf2xx_ack_psdu,
61 .frags = NULL,
62 };
63 static struct net_pkt rf2xx_ack_pkt = {
64 .buffer = &rf2xx_ack_frame,
65 .cb = {
66 .lqi = 80,
67 .rssi = -40,
68 }
69 };
70 #endif /* CONFIG_NET_L2_OPENTHREAD */
71
72 /* Radio Transceiver ISR */
trx_isr_handler(const struct device * port,struct gpio_callback * cb,uint32_t pins)73 static inline void trx_isr_handler(const struct device *port,
74 struct gpio_callback *cb,
75 uint32_t pins)
76 {
77 struct rf2xx_context *ctx = CONTAINER_OF(cb,
78 struct rf2xx_context,
79 irq_cb);
80
81 ARG_UNUSED(port);
82 ARG_UNUSED(pins);
83
84 k_sem_give(&ctx->trx_isr_lock);
85 }
86
rf2xx_trx_set_state(const struct device * dev,enum rf2xx_trx_state_cmd_t state)87 static void rf2xx_trx_set_state(const struct device *dev,
88 enum rf2xx_trx_state_cmd_t state)
89 {
90 do {
91 rf2xx_iface_reg_write(dev, RF2XX_TRX_STATE_REG,
92 RF2XX_TRX_PHY_STATE_CMD_FORCE_TRX_OFF);
93 } while (RF2XX_TRX_PHY_STATUS_TRX_OFF !=
94 (rf2xx_iface_reg_read(dev, RF2XX_TRX_STATUS_REG) &
95 RF2XX_TRX_PHY_STATUS_MASK));
96
97 do {
98 rf2xx_iface_reg_write(dev, RF2XX_TRX_STATE_REG, state);
99 } while (state !=
100 (rf2xx_iface_reg_read(dev, RF2XX_TRX_STATUS_REG) &
101 RF2XX_TRX_PHY_STATUS_MASK));
102 }
103
rf2xx_trx_set_tx_state(const struct device * dev)104 static void rf2xx_trx_set_tx_state(const struct device *dev)
105 {
106 uint8_t status;
107
108 /**
109 * Ensures that RX automatically ACK will be sent when requested.
110 * Datasheet: Chapter 7.2.3 RX_AACK_ON – Receive with Automatic ACK
111 * Datasheet: Figure 7-13. Timing Example of an RX_AACK Transaction
112 * for Slotted Operation.
113 *
114 * This will create a spin lock that wait transceiver be free from
115 * current receive frame process
116 */
117 do {
118 status = (rf2xx_iface_reg_read(dev, RF2XX_TRX_STATUS_REG) &
119 RF2XX_TRX_PHY_STATUS_MASK);
120 } while (status == RF2XX_TRX_PHY_STATUS_BUSY_RX_AACK ||
121 status == RF2XX_TRX_PHY_STATUS_STATE_TRANSITION);
122
123 rf2xx_trx_set_state(dev, RF2XX_TRX_PHY_STATE_CMD_TRX_OFF);
124 rf2xx_iface_reg_read(dev, RF2XX_IRQ_STATUS_REG);
125 rf2xx_trx_set_state(dev, RF2XX_TRX_PHY_STATE_CMD_TX_ARET_ON);
126 }
127
rf2xx_trx_set_rx_state(const struct device * dev)128 static void rf2xx_trx_set_rx_state(const struct device *dev)
129 {
130 rf2xx_trx_set_state(dev, RF2XX_TRX_PHY_STATE_CMD_TRX_OFF);
131 rf2xx_iface_reg_read(dev, RF2XX_IRQ_STATUS_REG);
132 /**
133 * Set extended RX mode
134 * Datasheet: chapter 7.2 Extended Operating Mode
135 */
136 rf2xx_trx_set_state(dev, RF2XX_TRX_PHY_STATE_CMD_RX_AACK_ON);
137 }
138
rf2xx_set_rssi_base(const struct device * dev,uint16_t channel)139 static void rf2xx_set_rssi_base(const struct device *dev, uint16_t channel)
140 {
141 struct rf2xx_context *ctx = dev->data;
142 int8_t base;
143
144 if (ctx->cc_page == IEEE802154_ATTR_PHY_CHANNEL_PAGE_ZERO_OQPSK_2450_BPSK_868_915) {
145 base = channel == 0
146 ? RF2XX_RSSI_BPSK_20
147 : RF2XX_RSSI_BPSK_40;
148 } else if (ctx->cc_page == IEEE802154_ATTR_PHY_CHANNEL_PAGE_TWO_OQPSK_868_915) {
149 base = channel == 0
150 ? RF2XX_RSSI_OQPSK_SIN_RC_100
151 : RF2XX_RSSI_OQPSK_SIN_250;
152 } else {
153 base = RF2XX_RSSI_OQPSK_RC_250;
154 }
155
156 ctx->trx_rssi_base = base;
157 }
158
rf2xx_trx_rx(const struct device * dev)159 static void rf2xx_trx_rx(const struct device *dev)
160 {
161 struct rf2xx_context *ctx = dev->data;
162 struct net_pkt *pkt = NULL;
163 uint8_t rx_buf[RX2XX_MAX_FRAME_SIZE];
164 uint8_t pkt_len;
165 uint8_t frame_len;
166 uint8_t trac;
167
168 /*
169 * The rf2xx frame buffer can have length > 128 bytes. The
170 * net_pkt_rx_alloc_with_buffer allocates max value of 128 bytes.
171 *
172 * This obligate the driver to have rx_buf statically allocated with
173 * RX2XX_MAX_FRAME_SIZE.
174 */
175 if (ctx->trx_model != RF2XX_TRX_MODEL_231) {
176 pkt_len = ctx->rx_phr;
177 } else {
178 rf2xx_iface_frame_read(dev, rx_buf, RX2XX_FRAME_HEADER_SIZE);
179 pkt_len = rx_buf[RX2XX_FRAME_PHR_INDEX];
180 }
181
182 if (!ctx->promiscuous && pkt_len < RX2XX_FRAME_MIN_PHR_SIZE) {
183 LOG_ERR("Invalid RX frame length");
184 return;
185 }
186
187 frame_len = RX2XX_FRAME_HEADER_SIZE + pkt_len +
188 RX2XX_FRAME_FOOTER_SIZE;
189
190 rf2xx_iface_frame_read(dev, rx_buf, frame_len);
191
192 if (ctx->trx_model != RF2XX_TRX_MODEL_231) {
193 trac = rx_buf[pkt_len + RX2XX_FRAME_TRAC_INDEX];
194 trac = (trac >> RF2XX_RX_TRAC_STATUS) & RF2XX_RX_TRAC_BIT_MASK;
195
196 ctx->pkt_ed = rx_buf[pkt_len + RX2XX_FRAME_ED_INDEX];
197 } else {
198 trac = (rf2xx_iface_reg_read(dev, RF2XX_TRX_STATE_REG)
199 >> RF2XX_TRAC_STATUS) & RF2XX_TRAC_BIT_MASK;
200
201 ctx->pkt_ed = (rf2xx_iface_reg_read(dev, RF2XX_PHY_RSSI_REG)
202 >> RF2XX_RSSI) & RF2XX_RSSI_MASK;
203 }
204 ctx->pkt_lqi = rx_buf[pkt_len + RX2XX_FRAME_LQI_INDEX];
205
206 if (!ctx->promiscuous && trac == RF2XX_TRX_PHY_STATE_TRAC_INVALID) {
207 LOG_ERR("Invalid RX frame");
208 return;
209 }
210
211 if (!IS_ENABLED(CONFIG_IEEE802154_RAW_MODE) &&
212 !IS_ENABLED(CONFIG_NET_L2_OPENTHREAD) &&
213 pkt_len >= RX2XX_FRAME_FCS_LENGTH) {
214 pkt_len -= RX2XX_FRAME_FCS_LENGTH;
215 }
216
217 pkt = net_pkt_rx_alloc_with_buffer(ctx->iface, pkt_len,
218 AF_UNSPEC, 0, K_NO_WAIT);
219 if (!pkt) {
220 LOG_ERR("No RX buffer available");
221 return;
222 }
223
224 memcpy(pkt->buffer->data, rx_buf + RX2XX_FRAME_HEADER_SIZE, pkt_len);
225 net_buf_add(pkt->buffer, pkt_len);
226 net_pkt_set_ieee802154_lqi(pkt, ctx->pkt_lqi);
227 net_pkt_set_ieee802154_rssi_dbm(pkt, ctx->pkt_ed + ctx->trx_rssi_base);
228
229 LOG_DBG("Caught a packet (%02X) (LQI: %02X, RSSI: %d, ED: %02X)",
230 pkt_len, ctx->pkt_lqi, ctx->trx_rssi_base + ctx->pkt_ed,
231 ctx->pkt_ed);
232
233 if (net_recv_data(ctx->iface, pkt) < 0) {
234 LOG_DBG("RX Packet dropped by NET stack");
235 net_pkt_unref(pkt);
236 return;
237 }
238
239 if (LOG_LEVEL >= LOG_LEVEL_DBG) {
240 log_stack_usage(&ctx->trx_thread);
241 }
242 }
243
rf2xx_process_rx_frame(const struct device * dev)244 static void rf2xx_process_rx_frame(const struct device *dev)
245 {
246 struct rf2xx_context *ctx = dev->data;
247
248 /*
249 * NOTE: In promiscuous mode invalid frames will be processed.
250 */
251
252 if (ctx->trx_model != RF2XX_TRX_MODEL_231) {
253 rf2xx_trx_rx(dev);
254 } else {
255 /* Ensures that automatically ACK will be sent
256 * when requested
257 */
258 while (rf2xx_iface_reg_read(dev, RF2XX_TRX_STATUS_REG) ==
259 RF2XX_TRX_PHY_STATUS_BUSY_RX_AACK) {
260 ;
261 }
262
263 /* Set PLL_ON to avoid transceiver receive
264 * new data until finish reading process
265 */
266 rf2xx_trx_set_state(dev, RF2XX_TRX_PHY_STATE_CMD_PLL_ON);
267 rf2xx_trx_rx(dev);
268 rf2xx_trx_set_state(dev, RF2XX_TRX_PHY_STATE_CMD_RX_AACK_ON);
269 }
270 }
271
rf2xx_process_tx_frame(const struct device * dev)272 static void rf2xx_process_tx_frame(const struct device *dev)
273 {
274 struct rf2xx_context *ctx = dev->data;
275
276 ctx->trx_trac = (rf2xx_iface_reg_read(dev, RF2XX_TRX_STATE_REG) >>
277 RF2XX_TRAC_STATUS) & RF2XX_TRAC_BIT_MASK;
278 k_sem_give(&ctx->trx_tx_sync);
279 rf2xx_trx_set_rx_state(dev);
280 }
281
rf2xx_process_trx_end(const struct device * dev)282 static void rf2xx_process_trx_end(const struct device *dev)
283 {
284 uint8_t trx_status = (rf2xx_iface_reg_read(dev, RF2XX_TRX_STATUS_REG) &
285 RF2XX_TRX_PHY_STATUS_MASK);
286
287 if (trx_status == RF2XX_TRX_PHY_STATUS_TX_ARET_ON) {
288 rf2xx_process_tx_frame(dev);
289 } else {
290 rf2xx_process_rx_frame(dev);
291 }
292 }
293
rf2xx_thread_main(void * arg)294 static void rf2xx_thread_main(void *arg)
295 {
296 struct rf2xx_context *ctx = arg;
297 uint8_t isr_status;
298
299 while (true) {
300 k_sem_take(&ctx->trx_isr_lock, K_FOREVER);
301
302 isr_status = rf2xx_iface_reg_read(ctx->dev,
303 RF2XX_IRQ_STATUS_REG);
304
305 /*
306 * IRQ_7 (BAT_LOW) Indicates a supply voltage below the
307 * programmed threshold. 9.5.4
308 * IRQ_6 (TRX_UR) Indicates a Frame Buffer access
309 * violation. 9.3.3
310 * IRQ_5 (AMI) Indicates address matching. 8.2
311 * IRQ_4 (CCA_ED_DONE) Multi-functional interrupt:
312 * 1. AWAKE_END: 7.1.2.5
313 * • Indicates finished transition to TRX_OFF state
314 * from P_ON, SLEEP, DEEP_SLEEP, or RESET state.
315 * 2. CCA_ED_DONE: 8.5.4
316 * • Indicates the end of a CCA or ED
317 * measurement. 8.6.4
318 * IRQ_3 (TRX_END)
319 * RX: Indicates the completion of a frame
320 * reception. 7.1.3
321 * TX: Indicates the completion of a frame
322 * transmission. 7.1.3
323 * IRQ_2 (RX_START) Indicates the start of a PSDU
324 * reception; the AT86RF233 state changed to BUSY_RX;
325 * the PHR can be read from Frame Buffer. 7.1.3
326 * IRQ_1 (PLL_UNLOCK) Indicates PLL unlock. If the radio
327 * transceiver is in BUSY_TX / BUSY_TX_ARET state, the
328 * PA is turned off immediately. 9.7.5
329 * IRQ_0 (PLL_LOCK) Indicates PLL lock.
330 */
331 if (isr_status & (1 << RF2XX_RX_START)) {
332 if (ctx->trx_model != RF2XX_TRX_MODEL_231) {
333 rf2xx_iface_sram_read(ctx->dev, 0,
334 &ctx->rx_phr, 1);
335 }
336 }
337 if (isr_status & (1 << RF2XX_TRX_END)) {
338 rf2xx_process_trx_end(ctx->dev);
339 }
340 }
341 }
342
get_mac(const struct device * dev)343 static inline uint8_t *get_mac(const struct device *dev)
344 {
345 const struct rf2xx_config *conf = dev->config;
346 struct rf2xx_context *ctx = dev->data;
347 uint32_t *ptr = (uint32_t *)(ctx->mac_addr);
348
349 if (!conf->has_mac) {
350 UNALIGNED_PUT(sys_rand32_get(), ptr);
351 ptr = (uint32_t *)(ctx->mac_addr + 4);
352 UNALIGNED_PUT(sys_rand32_get(), ptr);
353 }
354
355 /*
356 * Clear bit 0 to ensure it isn't a multicast address and set
357 * bit 1 to indicate address is locally administered and may
358 * not be globally unique.
359 */
360 ctx->mac_addr[0] = (ctx->mac_addr[0] & ~0x01) | 0x02;
361
362 return ctx->mac_addr;
363 }
364
rf2xx_get_capabilities(const struct device * dev)365 static enum ieee802154_hw_caps rf2xx_get_capabilities(const struct device *dev)
366 {
367 LOG_DBG("HW Caps");
368
369 return IEEE802154_HW_FCS |
370 IEEE802154_HW_PROMISC |
371 IEEE802154_HW_FILTER |
372 IEEE802154_HW_CSMA |
373 IEEE802154_HW_RETRANSMISSION |
374 IEEE802154_HW_TX_RX_ACK |
375 IEEE802154_HW_RX_TX_ACK;
376 }
377
rf2xx_configure_sub_channel(const struct device * dev,uint16_t channel)378 static int rf2xx_configure_sub_channel(const struct device *dev, uint16_t channel)
379 {
380 struct rf2xx_context *ctx = dev->data;
381 uint8_t reg;
382 uint8_t cc_mask;
383
384 if (ctx->cc_page == IEEE802154_ATTR_PHY_CHANNEL_PAGE_ZERO_OQPSK_2450_BPSK_868_915) {
385 cc_mask = channel == 0
386 ? RF2XX_CC_BPSK_20
387 : RF2XX_CC_BPSK_40;
388 } else if (ctx->cc_page == IEEE802154_ATTR_PHY_CHANNEL_PAGE_TWO_OQPSK_868_915) {
389 cc_mask = channel == 0
390 ? RF2XX_CC_OQPSK_SIN_RC_100
391 : RF2XX_CC_OQPSK_SIN_250;
392 } else {
393 cc_mask = RF2XX_CC_OQPSK_RC_250;
394 }
395
396 reg = rf2xx_iface_reg_read(dev, RF2XX_TRX_CTRL_2_REG)
397 & ~RF2XX_SUB_CHANNEL_MASK;
398 rf2xx_iface_reg_write(dev, RF2XX_TRX_CTRL_2_REG, reg | cc_mask);
399
400 return 0;
401 }
402
rf2xx_configure_trx_path(const struct device * dev)403 static int rf2xx_configure_trx_path(const struct device *dev)
404 {
405 struct rf2xx_context *ctx = dev->data;
406 uint8_t reg;
407 uint8_t gc_tx_offset;
408
409 if (ctx->cc_page == IEEE802154_ATTR_PHY_CHANNEL_PAGE_ZERO_OQPSK_2450_BPSK_868_915) {
410 gc_tx_offset = 0x03;
411 } else {
412 gc_tx_offset = 0x02;
413 }
414
415 reg = rf2xx_iface_reg_read(dev, RF2XX_RF_CTRL_0_REG)
416 & ~RF2XX_GC_TX_OFFS_MASK;
417 rf2xx_iface_reg_write(dev, RF2XX_RF_CTRL_0_REG, reg | gc_tx_offset);
418
419 return 0;
420 }
421
rf2xx_cca(const struct device * dev)422 static int rf2xx_cca(const struct device *dev)
423 {
424 ARG_UNUSED(dev);
425
426 LOG_DBG("CCA");
427
428 return 0;
429 }
430
rf2xx_set_channel(const struct device * dev,uint16_t channel)431 static int rf2xx_set_channel(const struct device *dev, uint16_t channel)
432 {
433 struct rf2xx_context *ctx = dev->data;
434 uint8_t reg;
435
436 LOG_DBG("Set Channel %d", channel);
437
438 if (ctx->trx_model == RF2XX_TRX_MODEL_212) {
439 if ((ctx->cc_page == IEEE802154_ATTR_PHY_CHANNEL_PAGE_ZERO_OQPSK_2450_BPSK_868_915
440 || ctx->cc_page == IEEE802154_ATTR_PHY_CHANNEL_PAGE_TWO_OQPSK_868_915)
441 && channel > 10) {
442 LOG_ERR("Unsupported channel %u", channel);
443 return channel > 26 ? -EINVAL : -ENOTSUP;
444 }
445 if (ctx->cc_page == IEEE802154_ATTR_PHY_CHANNEL_PAGE_FIVE_OQPSK_780 &&
446 channel > 3) {
447 LOG_ERR("Unsupported channel %u", channel);
448 return channel > 7 ? -EINVAL : -ENOTSUP;
449 }
450
451 rf2xx_configure_sub_channel(dev, channel);
452 rf2xx_configure_trx_path(dev);
453 rf2xx_set_rssi_base(dev, channel);
454 } else {
455 /* 2.4G O-QPSK, channel page zero */
456 if (channel < 11 || channel > 26) {
457 LOG_ERR("Unsupported channel %u", channel);
458 return channel < 11 ? -ENOTSUP : -EINVAL;
459 }
460 }
461
462 reg = rf2xx_iface_reg_read(dev, RF2XX_PHY_CC_CCA_REG) & ~0x1f;
463 rf2xx_iface_reg_write(dev, RF2XX_PHY_CC_CCA_REG, reg | channel);
464
465 return 0;
466 }
467
rf2xx_set_txpower(const struct device * dev,int16_t dbm)468 static int rf2xx_set_txpower(const struct device *dev, int16_t dbm)
469 {
470 const struct rf2xx_config *conf = dev->config;
471 struct rf2xx_context *ctx = dev->data;
472 float min, max, step;
473 uint8_t reg;
474 uint8_t idx;
475 uint8_t val;
476
477 LOG_DBG("Try set Power to %d", dbm);
478
479 /**
480 * if table size is equal 1 the code assumes a table was not defined. In
481 * this case the transceiver PHY_TX_PWR register will be set with value
482 * zero. This is a safe value for all variants and represents an output
483 * power above 0 dBm.
484 *
485 * Note: This is a special case too which avoid division by zero when
486 * computing the step variable.
487 */
488 if (conf->tx_pwr_table_size == 1) {
489 rf2xx_iface_reg_write(dev, RF2XX_PHY_TX_PWR_REG, 0);
490
491 return 0;
492 }
493
494 min = conf->tx_pwr_min[1];
495 if (conf->tx_pwr_min[0] == 0x01) {
496 min *= -1.0f;
497 }
498
499 max = conf->tx_pwr_max[1];
500 if (conf->tx_pwr_max[0] == 0x01) {
501 min *= -1.0f;
502 }
503
504 step = (max - min) / ((float)conf->tx_pwr_table_size - 1.0f);
505
506 if (step == 0.0f) {
507 step = 1.0f;
508 }
509
510 LOG_DBG("Tx-power values: min %f, max %f, step %f, entries %d",
511 (double)min, (double)max, (double)step, conf->tx_pwr_table_size);
512
513 if (dbm < min) {
514 LOG_INF("TX-power %d dBm below min of %f dBm, using %f dBm",
515 dbm, (double)min, (double)max);
516 dbm = min;
517 } else if (dbm > max) {
518 LOG_INF("TX-power %d dBm above max of %f dBm, using %f dBm",
519 dbm, (double)min, (double)max);
520 dbm = max;
521 }
522
523 idx = abs((int) (((float)(dbm - max) / step)));
524 LOG_DBG("Tx-power idx: %d", idx);
525
526 if (idx >= conf->tx_pwr_table_size) {
527 idx = conf->tx_pwr_table_size - 1;
528 }
529
530 val = conf->tx_pwr_table[idx];
531
532 if (ctx->trx_model != RF2XX_TRX_MODEL_212) {
533 reg = rf2xx_iface_reg_read(dev, RF2XX_PHY_TX_PWR_REG) & 0xf0;
534 val = reg + (val & 0x0f);
535 }
536
537 LOG_DBG("Tx-power normalized: %d dBm, PHY_TX_PWR 0x%02x, idx %d",
538 dbm, val, idx);
539
540 rf2xx_iface_reg_write(dev, RF2XX_PHY_TX_PWR_REG, val);
541
542 return 0;
543 }
544
rf2xx_set_ieee_addr(const struct device * dev,bool set,const uint8_t * ieee_addr)545 static int rf2xx_set_ieee_addr(const struct device *dev, bool set,
546 const uint8_t *ieee_addr)
547 {
548 const uint8_t *ptr_to_reg = ieee_addr;
549
550 LOG_DBG("IEEE address %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x",
551 ieee_addr[7], ieee_addr[6], ieee_addr[5], ieee_addr[4],
552 ieee_addr[3], ieee_addr[2], ieee_addr[1], ieee_addr[0]);
553
554 if (set) {
555 for (uint8_t i = 0; i < 8; i++, ptr_to_reg++) {
556 rf2xx_iface_reg_write(dev, (RF2XX_IEEE_ADDR_0_REG + i),
557 *ptr_to_reg);
558 }
559 } else {
560 for (uint8_t i = 0; i < 8; i++) {
561 rf2xx_iface_reg_write(dev, (RF2XX_IEEE_ADDR_0_REG + i),
562 0);
563 }
564 }
565
566 return 0;
567 }
568
rf2xx_set_short_addr(const struct device * dev,bool set,uint16_t short_addr)569 static int rf2xx_set_short_addr(const struct device *dev, bool set,
570 uint16_t short_addr)
571 {
572 uint8_t short_addr_le[2] = { 0xFF, 0xFF };
573
574 if (set) {
575 sys_put_le16(short_addr, short_addr_le);
576 }
577
578 rf2xx_iface_reg_write(dev, RF2XX_SHORT_ADDR_0_REG, short_addr_le[0]);
579 rf2xx_iface_reg_write(dev, RF2XX_SHORT_ADDR_1_REG, short_addr_le[1]);
580 rf2xx_iface_reg_write(dev, RF2XX_CSMA_SEED_0_REG,
581 short_addr_le[0] + short_addr_le[1]);
582
583 LOG_DBG("Short Address: 0x%02X%02X", short_addr_le[1],
584 short_addr_le[0]);
585
586 return 0;
587 }
588
rf2xx_set_pan_id(const struct device * dev,bool set,uint16_t pan_id)589 static int rf2xx_set_pan_id(const struct device *dev, bool set,
590 uint16_t pan_id)
591 {
592 uint8_t pan_id_le[2] = { 0xFF, 0xFF };
593
594 if (set) {
595 sys_put_le16(pan_id, pan_id_le);
596 }
597
598 rf2xx_iface_reg_write(dev, RF2XX_PAN_ID_0_REG, pan_id_le[0]);
599 rf2xx_iface_reg_write(dev, RF2XX_PAN_ID_1_REG, pan_id_le[1]);
600
601 LOG_DBG("Pan Id: 0x%02X%02X", pan_id_le[1], pan_id_le[0]);
602
603 return 0;
604 }
605
rf2xx_filter(const struct device * dev,bool set,enum ieee802154_filter_type type,const struct ieee802154_filter * filter)606 static int rf2xx_filter(const struct device *dev,
607 bool set, enum ieee802154_filter_type type,
608 const struct ieee802154_filter *filter)
609 {
610 LOG_DBG("Applying filter %u", type);
611
612 if (type == IEEE802154_FILTER_TYPE_IEEE_ADDR) {
613 return rf2xx_set_ieee_addr(dev, set, filter->ieee_addr);
614 } else if (type == IEEE802154_FILTER_TYPE_SHORT_ADDR) {
615 return rf2xx_set_short_addr(dev, set, filter->short_addr);
616 } else if (type == IEEE802154_FILTER_TYPE_PAN_ID) {
617 return rf2xx_set_pan_id(dev, set, filter->pan_id);
618 }
619
620 return -ENOTSUP;
621 }
622
623 #if defined(CONFIG_NET_L2_OPENTHREAD)
rf2xx_handle_ack(struct rf2xx_context * ctx,struct net_buf * frag)624 static void rf2xx_handle_ack(struct rf2xx_context *ctx, struct net_buf *frag)
625 {
626 if ((frag->data[0] & RF2XX_FRAME_CTRL_ACK_REQUEST_BIT) == 0) {
627 return;
628 }
629
630 rf2xx_ack_psdu[0] = RF2XX_ACK_FRAME_TYPE;
631 rf2xx_ack_psdu[2] = frag->data[2];
632
633 if (ctx->trx_trac == RF2XX_TRX_PHY_STATE_TRAC_SUCCESS_DATA_PENDING) {
634 rf2xx_ack_psdu[0] |= RF2XX_ACK_FRAME_PENDING_BIT;
635 }
636
637 net_pkt_cursor_init(&rf2xx_ack_pkt);
638
639 if (ieee802154_handle_ack(ctx->iface, &rf2xx_ack_pkt) != NET_OK) {
640 LOG_INF("ACK packet not handled.");
641 }
642 }
643 #else
644 #define rf2xx_handle_ack(...)
645 #endif
646
rf2xx_tx(const struct device * dev,enum ieee802154_tx_mode mode,struct net_pkt * pkt,struct net_buf * frag)647 static int rf2xx_tx(const struct device *dev,
648 enum ieee802154_tx_mode mode,
649 struct net_pkt *pkt,
650 struct net_buf *frag)
651 {
652 ARG_UNUSED(pkt);
653
654 struct rf2xx_context *ctx = dev->data;
655 int response = 0;
656
657 LOG_DBG("TX");
658
659 if (ctx->tx_mode != mode) {
660 switch (mode) {
661 case IEEE802154_TX_MODE_DIRECT:
662 /* skip retries & csma/ca algorithm */
663 rf2xx_iface_reg_write(dev, RF2XX_XAH_CTRL_0_REG, 0x0E);
664 break;
665 case IEEE802154_TX_MODE_CSMA_CA:
666 /* backoff maxBE = 5, minBE = 3 */
667 rf2xx_iface_reg_write(dev, RF2XX_CSMA_BE_REG, 0x53);
668 /* max frame retries = 3, csma/ca retries = 4 */
669 rf2xx_iface_reg_write(dev, RF2XX_XAH_CTRL_0_REG, 0x38);
670 break;
671 case IEEE802154_TX_MODE_CCA:
672 /* backoff period = 0 */
673 rf2xx_iface_reg_write(dev, RF2XX_CSMA_BE_REG, 0x00);
674 /* no frame retries & no csma/ca retries */
675 rf2xx_iface_reg_write(dev, RF2XX_XAH_CTRL_0_REG, 0x00);
676 break;
677 case IEEE802154_TX_MODE_TXTIME:
678 case IEEE802154_TX_MODE_TXTIME_CCA:
679 default:
680 NET_ERR("TX mode %d not supported", mode);
681 return -ENOTSUP;
682 }
683
684 ctx->tx_mode = mode;
685 }
686
687 rf2xx_trx_set_tx_state(dev);
688 rf2xx_iface_reg_read(dev, RF2XX_IRQ_STATUS_REG);
689
690 k_sem_reset(&ctx->trx_tx_sync);
691 rf2xx_iface_frame_write(dev, frag->data, frag->len);
692 rf2xx_iface_phy_tx_start(dev);
693 k_sem_take(&ctx->trx_tx_sync, K_FOREVER);
694
695 switch (ctx->trx_trac) {
696 /* Channel is still busy after attempting MAX_CSMA_RETRIES of
697 * CSMA-CA
698 */
699 case RF2XX_TRX_PHY_STATE_TRAC_CHANNEL_ACCESS_FAILED:
700 response = -EBUSY;
701 break;
702 /* No acknowledgment frames were received during all retry
703 * attempts
704 */
705 case RF2XX_TRX_PHY_STATE_TRAC_NO_ACK:
706 response = -EAGAIN;
707 break;
708 /* Transaction not yet finished */
709 case RF2XX_TRX_PHY_STATE_TRAC_INVALID:
710 response = -EINTR;
711 break;
712 /* RF2XX_TRX_PHY_STATE_TRAC_SUCCESS:
713 * The transaction was responded to by a valid ACK, or, if no
714 * ACK is requested, after a successful frame transmission.
715 *
716 * RF2XX_TRX_PHY_STATE_TRAC_SUCCESS_DATA_PENDING:
717 * Equivalent to SUCCESS and indicating that the “Frame
718 * Pending” bit (see Section 8.1.2.2) of the received
719 * acknowledgment frame was set.
720 */
721 default:
722 rf2xx_handle_ack(ctx, frag);
723 break;
724 }
725
726 return response;
727 }
728
rf2xx_start(const struct device * dev)729 static int rf2xx_start(const struct device *dev)
730 {
731 const struct rf2xx_config *conf = dev->config;
732
733 LOG_DBG("Start");
734
735 rf2xx_trx_set_state(dev, RF2XX_TRX_PHY_STATE_CMD_TRX_OFF);
736 rf2xx_iface_reg_read(dev, RF2XX_IRQ_STATUS_REG);
737 gpio_pin_interrupt_configure_dt(&conf->irq_gpio,
738 GPIO_INT_EDGE_TO_ACTIVE);
739 rf2xx_trx_set_rx_state(dev);
740
741 return 0;
742 }
743
rf2xx_stop(const struct device * dev)744 static int rf2xx_stop(const struct device *dev)
745 {
746 const struct rf2xx_config *conf = dev->config;
747
748 LOG_DBG("Stop");
749
750 gpio_pin_interrupt_configure_dt(&conf->irq_gpio, GPIO_INT_DISABLE);
751 rf2xx_trx_set_state(dev, RF2XX_TRX_PHY_STATE_CMD_TRX_OFF);
752 rf2xx_iface_reg_read(dev, RF2XX_IRQ_STATUS_REG);
753
754 return 0;
755 }
756
rf2xx_pan_coord_set(const struct device * dev,bool pan_coordinator)757 static int rf2xx_pan_coord_set(const struct device *dev, bool pan_coordinator)
758 {
759 uint8_t reg;
760
761 if (pan_coordinator) {
762 reg = rf2xx_iface_reg_read(dev, RF2XX_CSMA_SEED_1_REG);
763 reg |= (1 << RF2XX_AACK_I_AM_COORD);
764 rf2xx_iface_reg_write(dev, RF2XX_CSMA_SEED_1_REG, reg);
765 } else {
766 reg = rf2xx_iface_reg_read(dev, RF2XX_CSMA_SEED_1_REG);
767 reg &= ~(1 << RF2XX_AACK_I_AM_COORD);
768 rf2xx_iface_reg_write(dev, RF2XX_CSMA_SEED_1_REG, reg);
769 }
770
771 return 0;
772 }
773
rf2xx_promiscuous_set(const struct device * dev,bool promiscuous)774 static int rf2xx_promiscuous_set(const struct device *dev, bool promiscuous)
775 {
776 struct rf2xx_context *ctx = dev->data;
777 uint8_t reg;
778
779 ctx->promiscuous = promiscuous;
780
781 if (promiscuous) {
782 reg = rf2xx_iface_reg_read(dev, RF2XX_XAH_CTRL_1_REG);
783 reg |= (1 << RF2XX_AACK_PROM_MODE);
784 rf2xx_iface_reg_write(dev, RF2XX_XAH_CTRL_1_REG, reg);
785
786 reg = rf2xx_iface_reg_read(dev, RF2XX_CSMA_SEED_1_REG);
787 reg |= (1 << RF2XX_AACK_DIS_ACK);
788 rf2xx_iface_reg_write(dev, RF2XX_CSMA_SEED_1_REG, reg);
789 } else {
790 reg = rf2xx_iface_reg_read(dev, RF2XX_XAH_CTRL_1_REG);
791 reg &= ~(1 << RF2XX_AACK_PROM_MODE);
792 rf2xx_iface_reg_write(dev, RF2XX_XAH_CTRL_1_REG, reg);
793
794 reg = rf2xx_iface_reg_read(dev, RF2XX_CSMA_SEED_1_REG);
795 reg &= ~(1 << RF2XX_AACK_DIS_ACK);
796 rf2xx_iface_reg_write(dev, RF2XX_CSMA_SEED_1_REG, reg);
797 }
798
799 return 0;
800 }
801
rf2xx_configure(const struct device * dev,enum ieee802154_config_type type,const struct ieee802154_config * config)802 int rf2xx_configure(const struct device *dev,
803 enum ieee802154_config_type type,
804 const struct ieee802154_config *config)
805 {
806 int ret = -EINVAL;
807
808 LOG_DBG("Configure %d", type);
809
810 switch (type) {
811 case IEEE802154_CONFIG_AUTO_ACK_FPB:
812 case IEEE802154_CONFIG_ACK_FPB:
813 break;
814
815 case IEEE802154_CONFIG_PAN_COORDINATOR:
816 ret = rf2xx_pan_coord_set(dev, config->pan_coordinator);
817 break;
818
819 case IEEE802154_CONFIG_PROMISCUOUS:
820 ret = rf2xx_promiscuous_set(dev, config->promiscuous);
821 break;
822
823 case IEEE802154_CONFIG_EVENT_HANDLER:
824 default:
825 break;
826 }
827
828 return ret;
829 }
830
rf2xx_attr_get(const struct device * dev,enum ieee802154_attr attr,struct ieee802154_attr_value * value)831 static int rf2xx_attr_get(const struct device *dev, enum ieee802154_attr attr,
832 struct ieee802154_attr_value *value)
833 {
834 struct rf2xx_context *ctx = dev->data;
835
836 switch (attr) {
837 case IEEE802154_ATTR_PHY_SUPPORTED_CHANNEL_PAGES:
838 value->phy_supported_channel_pages = ctx->cc_page;
839 return 0;
840
841 case IEEE802154_ATTR_PHY_SUPPORTED_CHANNEL_RANGES:
842 value->phy_supported_channels = &ctx->cc_channels;
843 return 0;
844
845 default:
846 return -ENOENT;
847 }
848 }
849
power_on_and_setup(const struct device * dev)850 static int power_on_and_setup(const struct device *dev)
851 {
852 const struct rf2xx_config *conf = dev->config;
853 struct rf2xx_context *ctx = dev->data;
854 uint8_t config;
855
856 rf2xx_iface_phy_rst(dev);
857
858 /* Sync transceiver state */
859 do {
860 rf2xx_iface_reg_write(dev, RF2XX_TRX_STATE_REG,
861 RF2XX_TRX_PHY_STATE_CMD_TRX_OFF);
862 } while (RF2XX_TRX_PHY_STATUS_TRX_OFF !=
863 (rf2xx_iface_reg_read(dev, RF2XX_TRX_STATUS_REG) &
864 RF2XX_TRX_PHY_STATUS_MASK));
865
866 /* get device identification */
867 ctx->trx_model = rf2xx_iface_reg_read(dev, RF2XX_PART_NUM_REG);
868 ctx->trx_version = rf2xx_iface_reg_read(dev, RF2XX_VERSION_NUM_REG);
869
870 /**
871 * Valid transceiver are:
872 * 231-Rev-A (Version 0x02)
873 * 232-Rev-A (Version 0x02)
874 * 233-Rev-A (Version 0x01) (Warning)
875 * 233-Rev-B (Version 0x02)
876 */
877 if (ctx->trx_model <= RF2XX_TRX_MODEL_230) {
878 LOG_DBG("Invalid or not supported transceiver");
879 return -ENODEV;
880 }
881
882 if (ctx->trx_model == RF2XX_TRX_MODEL_233 && ctx->trx_version == 0x01) {
883 LOG_DBG("Transceiver is old and unstable release");
884 }
885
886 /* Set RSSI base */
887 if (ctx->trx_model == RF2XX_TRX_MODEL_212) {
888 ctx->trx_rssi_base = -100;
889 } else if (ctx->trx_model == RF2XX_TRX_MODEL_233) {
890 ctx->trx_rssi_base = -94;
891 } else if (ctx->trx_model == RF2XX_TRX_MODEL_231) {
892 ctx->trx_rssi_base = -91;
893 } else {
894 ctx->trx_rssi_base = -90;
895 }
896
897 /* Disable All Features of TRX_CTRL_0 */
898 config = 0;
899 rf2xx_iface_reg_write(dev, RF2XX_TRX_CTRL_0_REG, config);
900
901 /* Configure PHY behaviour */
902 config = (1 << RF2XX_TX_AUTO_CRC_ON) |
903 (3 << RF2XX_SPI_CMD_MODE) |
904 (1 << RF2XX_IRQ_MASK_MODE);
905 rf2xx_iface_reg_write(dev, RF2XX_TRX_CTRL_1_REG, config);
906
907 config = (1 << RF2XX_RX_SAFE_MODE);
908 if (ctx->trx_model != RF2XX_TRX_MODEL_232) {
909 config |= (1 << RF2XX_OQPSK_SCRAM_EN);
910 }
911 rf2xx_iface_reg_write(dev, RF2XX_TRX_CTRL_2_REG, config);
912
913 if (ctx->trx_model == RF2XX_TRX_MODEL_212) {
914 rf2xx_configure_trx_path(dev);
915 rf2xx_iface_reg_write(dev, RF2XX_CC_CTRL_1_REG, 0);
916 }
917
918 ctx->tx_mode = IEEE802154_TX_MODE_CSMA_CA;
919 ctx->promiscuous = false;
920
921 /* Configure INT behaviour */
922 config = (1 << RF2XX_RX_START) |
923 (1 << RF2XX_TRX_END);
924 rf2xx_iface_reg_write(dev, RF2XX_IRQ_MASK_REG, config);
925
926 gpio_init_callback(&ctx->irq_cb, trx_isr_handler,
927 BIT(conf->irq_gpio.pin));
928
929 if (gpio_add_callback(conf->irq_gpio.port, &ctx->irq_cb) < 0) {
930 LOG_ERR("Could not set IRQ callback.");
931 return -ENXIO;
932 }
933
934 return 0;
935 }
936
configure_gpios(const struct device * dev)937 static inline int configure_gpios(const struct device *dev)
938 {
939 const struct rf2xx_config *conf = dev->config;
940
941 /* Chip IRQ line */
942 if (!gpio_is_ready_dt(&conf->irq_gpio)) {
943 LOG_ERR("IRQ GPIO device not ready");
944 return -ENODEV;
945 }
946 gpio_pin_configure_dt(&conf->irq_gpio, GPIO_INPUT);
947 gpio_pin_interrupt_configure_dt(&conf->irq_gpio,
948 GPIO_INT_EDGE_TO_ACTIVE);
949
950 /* Chip RESET line */
951 if (!gpio_is_ready_dt(&conf->reset_gpio)) {
952 LOG_ERR("RESET GPIO device not ready");
953 return -ENODEV;
954 }
955 gpio_pin_configure_dt(&conf->reset_gpio, GPIO_OUTPUT_INACTIVE);
956
957 /* Chip SLPTR line */
958 if (!gpio_is_ready_dt(&conf->slptr_gpio)) {
959 LOG_ERR("SLPTR GPIO device not ready");
960 return -ENODEV;
961 }
962 gpio_pin_configure_dt(&conf->slptr_gpio, GPIO_OUTPUT_INACTIVE);
963
964 /* Chip DIG2 line (Optional feature) */
965 if (conf->dig2_gpio.port != NULL) {
966 if (!gpio_is_ready_dt(&conf->dig2_gpio)) {
967 LOG_ERR("DIG2 GPIO device not ready");
968 return -ENODEV;
969 }
970 LOG_INF("Optional instance of %s device activated",
971 conf->dig2_gpio.port->name);
972 gpio_pin_configure_dt(&conf->dig2_gpio, GPIO_INPUT);
973 gpio_pin_interrupt_configure_dt(&conf->dig2_gpio,
974 GPIO_INT_EDGE_TO_ACTIVE);
975 }
976
977 /* Chip CLKM line (Optional feature) */
978 if (conf->clkm_gpio.port != NULL) {
979 if (!gpio_is_ready_dt(&conf->clkm_gpio)) {
980 LOG_ERR("CLKM GPIO device not ready");
981 return -ENODEV;
982 }
983 LOG_INF("Optional instance of %s device activated",
984 conf->clkm_gpio.port->name);
985 gpio_pin_configure_dt(&conf->clkm_gpio, GPIO_INPUT);
986 }
987
988 return 0;
989 }
990
configure_spi(const struct device * dev)991 static inline int configure_spi(const struct device *dev)
992 {
993 const struct rf2xx_config *conf = dev->config;
994
995 if (!spi_is_ready_dt(&conf->spi)) {
996 LOG_ERR("SPI bus %s is not ready",
997 conf->spi.bus->name);
998 return -ENODEV;
999 }
1000
1001 return 0;
1002 }
1003
rf2xx_init(const struct device * dev)1004 static int rf2xx_init(const struct device *dev)
1005 {
1006 struct rf2xx_context *ctx = dev->data;
1007 const struct rf2xx_config *conf = dev->config;
1008 char thread_name[20];
1009
1010 LOG_DBG("\nInitialize RF2XX Transceiver\n");
1011
1012 ctx->dev = dev;
1013
1014 k_sem_init(&ctx->trx_tx_sync, 0, 1);
1015 k_sem_init(&ctx->trx_isr_lock, 0, 1);
1016
1017 if (configure_gpios(dev) != 0) {
1018 LOG_ERR("Configuring GPIOS failed");
1019 return -EIO;
1020 }
1021
1022 if (configure_spi(dev) != 0) {
1023 LOG_ERR("Configuring SPI failed");
1024 return -EIO;
1025 }
1026
1027 LOG_DBG("GPIO and SPI configured");
1028
1029 if (power_on_and_setup(dev) != 0) {
1030 LOG_ERR("Configuring RF2XX failed");
1031 return -EIO;
1032 }
1033
1034 LOG_DBG("RADIO configured");
1035
1036 k_thread_create(&ctx->trx_thread,
1037 ctx->trx_stack,
1038 CONFIG_IEEE802154_RF2XX_RX_STACK_SIZE,
1039 (k_thread_entry_t) rf2xx_thread_main,
1040 ctx, NULL, NULL,
1041 K_PRIO_COOP(2), 0, K_NO_WAIT);
1042
1043 snprintk(thread_name, sizeof(thread_name),
1044 "rf2xx_trx [%d]", conf->inst);
1045 k_thread_name_set(&ctx->trx_thread, thread_name);
1046
1047 LOG_DBG("Thread OK");
1048
1049 return 0;
1050 }
1051
rf2xx_iface_init(struct net_if * iface)1052 static void rf2xx_iface_init(struct net_if *iface)
1053 {
1054 const struct device *dev = net_if_get_device(iface);
1055 struct rf2xx_context *ctx = dev->data;
1056 uint8_t *mac = get_mac(dev);
1057
1058 net_if_set_link_addr(iface, mac, 8, NET_LINK_IEEE802154);
1059
1060 ctx->iface = iface;
1061
1062 if (ctx->trx_model == RF2XX_TRX_MODEL_212) {
1063 if (ctx->cc_page == IEEE802154_ATTR_PHY_CHANNEL_PAGE_ZERO_OQPSK_2450_BPSK_868_915 ||
1064 ctx->cc_page == IEEE802154_ATTR_PHY_CHANNEL_PAGE_TWO_OQPSK_868_915) {
1065 ctx->cc_range.from_channel = 0U;
1066 ctx->cc_range.to_channel = 10U;
1067 } else if (ctx->cc_page == IEEE802154_ATTR_PHY_CHANNEL_PAGE_FIVE_OQPSK_780) {
1068 ctx->cc_range.from_channel = 0U;
1069 ctx->cc_range.to_channel = 3U;
1070 } else {
1071 __ASSERT(false, "Unsupported channel page %u.", ctx->cc_page);
1072 }
1073 } else {
1074 __ASSERT(ctx->cc_page ==
1075 IEEE802154_ATTR_PHY_CHANNEL_PAGE_ZERO_OQPSK_2450_BPSK_868_915,
1076 "Unsupported channel page %u.", ctx->cc_page);
1077 ctx->cc_range.from_channel = 11U;
1078 ctx->cc_range.to_channel = 26U;
1079 }
1080
1081 ieee802154_init(iface);
1082 }
1083
1084 static struct ieee802154_radio_api rf2xx_radio_api = {
1085 .iface_api.init = rf2xx_iface_init,
1086
1087 .get_capabilities = rf2xx_get_capabilities,
1088 .cca = rf2xx_cca,
1089 .set_channel = rf2xx_set_channel,
1090 .filter = rf2xx_filter,
1091 .set_txpower = rf2xx_set_txpower,
1092 .tx = rf2xx_tx,
1093 .start = rf2xx_start,
1094 .stop = rf2xx_stop,
1095 .configure = rf2xx_configure,
1096 .attr_get = rf2xx_attr_get,
1097 };
1098
1099 #if !defined(CONFIG_IEEE802154_RAW_MODE)
1100 #if defined(CONFIG_NET_L2_IEEE802154)
1101 #define L2 IEEE802154_L2
1102 #define L2_CTX_TYPE NET_L2_GET_CTX_TYPE(IEEE802154_L2)
1103 #define MTU RF2XX_MAX_PSDU_LENGTH
1104 #elif defined(CONFIG_NET_L2_OPENTHREAD)
1105 #define L2 OPENTHREAD_L2
1106 #define L2_CTX_TYPE NET_L2_GET_CTX_TYPE(OPENTHREAD_L2)
1107 #define MTU RF2XX_OT_PSDU_LENGTH
1108 #endif
1109 #endif /* CONFIG_IEEE802154_RAW_MODE */
1110
1111 #define DRV_INST_LOCAL_MAC_ADDRESS(n) \
1112 UTIL_AND(DT_INST_NODE_HAS_PROP(n, local_mac_address), \
1113 UTIL_AND(DT_INST_PROP_LEN(n, local_mac_address) == 8, \
1114 DT_INST_PROP(n, local_mac_address)))
1115
1116 #define IEEE802154_RF2XX_DEVICE_CONFIG(n) \
1117 BUILD_ASSERT(DT_INST_PROP_LEN(n, tx_pwr_min) == 2, \
1118 "rf2xx: Error TX-PWR-MIN len is different of two"); \
1119 BUILD_ASSERT(DT_INST_PROP_LEN(n, tx_pwr_max) == 2, \
1120 "rf2xx: Error TX-PWR-MAX len is different of two"); \
1121 BUILD_ASSERT(DT_INST_PROP_LEN(n, tx_pwr_table) != 0, \
1122 "rf2xx: Error TX-PWR-TABLE len must be greater than zero"); \
1123 static const uint8_t rf2xx_pwr_table_##n[] = \
1124 DT_INST_PROP_OR(n, tx_pwr_table, 0); \
1125 static const struct rf2xx_config rf2xx_ctx_config_##n = { \
1126 .inst = n, \
1127 .has_mac = DT_INST_NODE_HAS_PROP(n, local_mac_address), \
1128 .irq_gpio = GPIO_DT_SPEC_INST_GET(n, irq_gpios), \
1129 .reset_gpio = GPIO_DT_SPEC_INST_GET(n, reset_gpios), \
1130 .slptr_gpio = GPIO_DT_SPEC_INST_GET(n, slptr_gpios), \
1131 .dig2_gpio = GPIO_DT_SPEC_INST_GET_OR(n, dig2_gpios, {}), \
1132 .clkm_gpio = GPIO_DT_SPEC_INST_GET_OR(n, clkm_gpios, {}), \
1133 .spi = SPI_DT_SPEC_INST_GET(n, SPI_WORD_SET(8) | \
1134 SPI_TRANSFER_MSB, 0), \
1135 \
1136 .tx_pwr_min = DT_INST_PROP_OR(n, tx_pwr_min, 0), \
1137 .tx_pwr_max = DT_INST_PROP_OR(n, tx_pwr_max, 0), \
1138 .tx_pwr_table = rf2xx_pwr_table_##n, \
1139 .tx_pwr_table_size = DT_INST_PROP_LEN(n, tx_pwr_table), \
1140 }
1141
1142 #define IEEE802154_RF2XX_DEVICE_DATA(n) \
1143 static struct rf2xx_context rf2xx_ctx_data_##n = { \
1144 .mac_addr = { DRV_INST_LOCAL_MAC_ADDRESS(n) }, \
1145 .cc_page = BIT(DT_INST_ENUM_IDX_OR(n, channel_page, 0)),\
1146 .cc_channels = { \
1147 .ranges = &rf2xx_ctx_data_##n.cc_range, \
1148 .num_ranges = 1U, \
1149 } \
1150 }
1151
1152 #define IEEE802154_RF2XX_RAW_DEVICE_INIT(n) \
1153 DEVICE_DT_INST_DEFINE( \
1154 n, \
1155 &rf2xx_init, \
1156 NULL, \
1157 &rf2xx_ctx_data_##n, \
1158 &rf2xx_ctx_config_##n, \
1159 POST_KERNEL, \
1160 CONFIG_IEEE802154_RF2XX_INIT_PRIO, \
1161 &rf2xx_radio_api)
1162
1163 #define IEEE802154_RF2XX_NET_DEVICE_INIT(n) \
1164 NET_DEVICE_DT_INST_DEFINE( \
1165 n, \
1166 &rf2xx_init, \
1167 NULL, \
1168 &rf2xx_ctx_data_##n, \
1169 &rf2xx_ctx_config_##n, \
1170 CONFIG_IEEE802154_RF2XX_INIT_PRIO, \
1171 &rf2xx_radio_api, \
1172 L2, \
1173 L2_CTX_TYPE, \
1174 MTU)
1175
1176 #define IEEE802154_RF2XX_INIT(inst) \
1177 IEEE802154_RF2XX_DEVICE_CONFIG(inst); \
1178 IEEE802154_RF2XX_DEVICE_DATA(inst); \
1179 \
1180 COND_CODE_1(CONFIG_IEEE802154_RAW_MODE, \
1181 (IEEE802154_RF2XX_RAW_DEVICE_INIT(inst);), \
1182 (IEEE802154_RF2XX_NET_DEVICE_INIT(inst);))
1183
1184 DT_INST_FOREACH_STATUS_OKAY(IEEE802154_RF2XX_INIT)
1185