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 * p1,void * p2,void * p3)294 static void rf2xx_thread_main(void *p1, void *p2, void *p3)
295 {
296 ARG_UNUSED(p2);
297 ARG_UNUSED(p3);
298
299 struct rf2xx_context *ctx = p1;
300 uint8_t isr_status;
301
302 while (true) {
303 k_sem_take(&ctx->trx_isr_lock, K_FOREVER);
304
305 isr_status = rf2xx_iface_reg_read(ctx->dev,
306 RF2XX_IRQ_STATUS_REG);
307
308 /*
309 * IRQ_7 (BAT_LOW) Indicates a supply voltage below the
310 * programmed threshold. 9.5.4
311 * IRQ_6 (TRX_UR) Indicates a Frame Buffer access
312 * violation. 9.3.3
313 * IRQ_5 (AMI) Indicates address matching. 8.2
314 * IRQ_4 (CCA_ED_DONE) Multi-functional interrupt:
315 * 1. AWAKE_END: 7.1.2.5
316 * • Indicates finished transition to TRX_OFF state
317 * from P_ON, SLEEP, DEEP_SLEEP, or RESET state.
318 * 2. CCA_ED_DONE: 8.5.4
319 * • Indicates the end of a CCA or ED
320 * measurement. 8.6.4
321 * IRQ_3 (TRX_END)
322 * RX: Indicates the completion of a frame
323 * reception. 7.1.3
324 * TX: Indicates the completion of a frame
325 * transmission. 7.1.3
326 * IRQ_2 (RX_START) Indicates the start of a PSDU
327 * reception; the AT86RF233 state changed to BUSY_RX;
328 * the PHR can be read from Frame Buffer. 7.1.3
329 * IRQ_1 (PLL_UNLOCK) Indicates PLL unlock. If the radio
330 * transceiver is in BUSY_TX / BUSY_TX_ARET state, the
331 * PA is turned off immediately. 9.7.5
332 * IRQ_0 (PLL_LOCK) Indicates PLL lock.
333 */
334 if (isr_status & (1 << RF2XX_RX_START)) {
335 if (ctx->trx_model != RF2XX_TRX_MODEL_231) {
336 rf2xx_iface_sram_read(ctx->dev, 0,
337 &ctx->rx_phr, 1);
338 }
339 }
340 if (isr_status & (1 << RF2XX_TRX_END)) {
341 rf2xx_process_trx_end(ctx->dev);
342 }
343 }
344 }
345
get_mac(const struct device * dev)346 static inline uint8_t *get_mac(const struct device *dev)
347 {
348 const struct rf2xx_config *conf = dev->config;
349 struct rf2xx_context *ctx = dev->data;
350 uint32_t *ptr = (uint32_t *)(ctx->mac_addr);
351
352 if (!conf->has_mac) {
353 UNALIGNED_PUT(sys_rand32_get(), ptr);
354 ptr = (uint32_t *)(ctx->mac_addr + 4);
355 UNALIGNED_PUT(sys_rand32_get(), ptr);
356 }
357
358 /*
359 * Clear bit 0 to ensure it isn't a multicast address and set
360 * bit 1 to indicate address is locally administered and may
361 * not be globally unique.
362 */
363 ctx->mac_addr[0] = (ctx->mac_addr[0] & ~0x01) | 0x02;
364
365 return ctx->mac_addr;
366 }
367
rf2xx_get_capabilities(const struct device * dev)368 static enum ieee802154_hw_caps rf2xx_get_capabilities(const struct device *dev)
369 {
370 LOG_DBG("HW Caps");
371
372 return IEEE802154_HW_FCS |
373 IEEE802154_HW_PROMISC |
374 IEEE802154_HW_FILTER |
375 IEEE802154_HW_CSMA |
376 IEEE802154_HW_RETRANSMISSION |
377 IEEE802154_HW_TX_RX_ACK |
378 IEEE802154_HW_RX_TX_ACK;
379 }
380
rf2xx_configure_sub_channel(const struct device * dev,uint16_t channel)381 static int rf2xx_configure_sub_channel(const struct device *dev, uint16_t channel)
382 {
383 struct rf2xx_context *ctx = dev->data;
384 uint8_t reg;
385 uint8_t cc_mask;
386
387 if (ctx->cc_page == IEEE802154_ATTR_PHY_CHANNEL_PAGE_ZERO_OQPSK_2450_BPSK_868_915) {
388 cc_mask = channel == 0
389 ? RF2XX_CC_BPSK_20
390 : RF2XX_CC_BPSK_40;
391 } else if (ctx->cc_page == IEEE802154_ATTR_PHY_CHANNEL_PAGE_TWO_OQPSK_868_915) {
392 cc_mask = channel == 0
393 ? RF2XX_CC_OQPSK_SIN_RC_100
394 : RF2XX_CC_OQPSK_SIN_250;
395 } else {
396 cc_mask = RF2XX_CC_OQPSK_RC_250;
397 }
398
399 reg = rf2xx_iface_reg_read(dev, RF2XX_TRX_CTRL_2_REG)
400 & ~RF2XX_SUB_CHANNEL_MASK;
401 rf2xx_iface_reg_write(dev, RF2XX_TRX_CTRL_2_REG, reg | cc_mask);
402
403 return 0;
404 }
405
rf2xx_configure_trx_path(const struct device * dev)406 static int rf2xx_configure_trx_path(const struct device *dev)
407 {
408 struct rf2xx_context *ctx = dev->data;
409 uint8_t reg;
410 uint8_t gc_tx_offset;
411
412 if (ctx->cc_page == IEEE802154_ATTR_PHY_CHANNEL_PAGE_ZERO_OQPSK_2450_BPSK_868_915) {
413 gc_tx_offset = 0x03;
414 } else {
415 gc_tx_offset = 0x02;
416 }
417
418 reg = rf2xx_iface_reg_read(dev, RF2XX_RF_CTRL_0_REG)
419 & ~RF2XX_GC_TX_OFFS_MASK;
420 rf2xx_iface_reg_write(dev, RF2XX_RF_CTRL_0_REG, reg | gc_tx_offset);
421
422 return 0;
423 }
424
rf2xx_cca(const struct device * dev)425 static int rf2xx_cca(const struct device *dev)
426 {
427 ARG_UNUSED(dev);
428
429 LOG_DBG("CCA");
430
431 return 0;
432 }
433
rf2xx_set_channel(const struct device * dev,uint16_t channel)434 static int rf2xx_set_channel(const struct device *dev, uint16_t channel)
435 {
436 struct rf2xx_context *ctx = dev->data;
437 uint8_t reg;
438
439 LOG_DBG("Set Channel %d", channel);
440
441 if (ctx->trx_model == RF2XX_TRX_MODEL_212) {
442 if ((ctx->cc_page == IEEE802154_ATTR_PHY_CHANNEL_PAGE_ZERO_OQPSK_2450_BPSK_868_915
443 || ctx->cc_page == IEEE802154_ATTR_PHY_CHANNEL_PAGE_TWO_OQPSK_868_915)
444 && channel > 10) {
445 LOG_ERR("Unsupported channel %u", channel);
446 return channel > 26 ? -EINVAL : -ENOTSUP;
447 }
448 if (ctx->cc_page == IEEE802154_ATTR_PHY_CHANNEL_PAGE_FIVE_OQPSK_780 &&
449 channel > 3) {
450 LOG_ERR("Unsupported channel %u", channel);
451 return channel > 7 ? -EINVAL : -ENOTSUP;
452 }
453
454 rf2xx_configure_sub_channel(dev, channel);
455 rf2xx_configure_trx_path(dev);
456 rf2xx_set_rssi_base(dev, channel);
457 } else {
458 /* 2.4G O-QPSK, channel page zero */
459 if (channel < 11 || channel > 26) {
460 LOG_ERR("Unsupported channel %u", channel);
461 return channel < 11 ? -ENOTSUP : -EINVAL;
462 }
463 }
464
465 reg = rf2xx_iface_reg_read(dev, RF2XX_PHY_CC_CCA_REG) & ~0x1f;
466 rf2xx_iface_reg_write(dev, RF2XX_PHY_CC_CCA_REG, reg | channel);
467
468 return 0;
469 }
470
rf2xx_set_txpower(const struct device * dev,int16_t dbm)471 static int rf2xx_set_txpower(const struct device *dev, int16_t dbm)
472 {
473 const struct rf2xx_config *conf = dev->config;
474 struct rf2xx_context *ctx = dev->data;
475 float min, max, step;
476 uint8_t reg;
477 uint8_t idx;
478 uint8_t val;
479
480 LOG_DBG("Try set Power to %d", dbm);
481
482 /**
483 * if table size is equal 1 the code assumes a table was not defined. In
484 * this case the transceiver PHY_TX_PWR register will be set with value
485 * zero. This is a safe value for all variants and represents an output
486 * power above 0 dBm.
487 *
488 * Note: This is a special case too which avoid division by zero when
489 * computing the step variable.
490 */
491 if (conf->tx_pwr_table_size == 1) {
492 rf2xx_iface_reg_write(dev, RF2XX_PHY_TX_PWR_REG, 0);
493
494 return 0;
495 }
496
497 min = conf->tx_pwr_min[1];
498 if (conf->tx_pwr_min[0] == 0x01) {
499 min *= -1.0f;
500 }
501
502 max = conf->tx_pwr_max[1];
503 if (conf->tx_pwr_max[0] == 0x01) {
504 min *= -1.0f;
505 }
506
507 step = (max - min) / ((float)conf->tx_pwr_table_size - 1.0f);
508
509 if (step == 0.0f) {
510 step = 1.0f;
511 }
512
513 LOG_DBG("Tx-power values: min %f, max %f, step %f, entries %d",
514 (double)min, (double)max, (double)step, conf->tx_pwr_table_size);
515
516 if (dbm < min) {
517 LOG_INF("TX-power %d dBm below min of %f dBm, using %f dBm",
518 dbm, (double)min, (double)max);
519 dbm = min;
520 } else if (dbm > max) {
521 LOG_INF("TX-power %d dBm above max of %f dBm, using %f dBm",
522 dbm, (double)min, (double)max);
523 dbm = max;
524 }
525
526 idx = abs((int) (((float)(dbm - max) / step)));
527 LOG_DBG("Tx-power idx: %d", idx);
528
529 if (idx >= conf->tx_pwr_table_size) {
530 idx = conf->tx_pwr_table_size - 1;
531 }
532
533 val = conf->tx_pwr_table[idx];
534
535 if (ctx->trx_model != RF2XX_TRX_MODEL_212) {
536 reg = rf2xx_iface_reg_read(dev, RF2XX_PHY_TX_PWR_REG) & 0xf0;
537 val = reg + (val & 0x0f);
538 }
539
540 LOG_DBG("Tx-power normalized: %d dBm, PHY_TX_PWR 0x%02x, idx %d",
541 dbm, val, idx);
542
543 rf2xx_iface_reg_write(dev, RF2XX_PHY_TX_PWR_REG, val);
544
545 return 0;
546 }
547
rf2xx_set_ieee_addr(const struct device * dev,bool set,const uint8_t * ieee_addr)548 static int rf2xx_set_ieee_addr(const struct device *dev, bool set,
549 const uint8_t *ieee_addr)
550 {
551 const uint8_t *ptr_to_reg = ieee_addr;
552
553 LOG_DBG("IEEE address %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x",
554 ieee_addr[7], ieee_addr[6], ieee_addr[5], ieee_addr[4],
555 ieee_addr[3], ieee_addr[2], ieee_addr[1], ieee_addr[0]);
556
557 if (set) {
558 for (uint8_t i = 0; i < 8; i++, ptr_to_reg++) {
559 rf2xx_iface_reg_write(dev, (RF2XX_IEEE_ADDR_0_REG + i),
560 *ptr_to_reg);
561 }
562 } else {
563 for (uint8_t i = 0; i < 8; i++) {
564 rf2xx_iface_reg_write(dev, (RF2XX_IEEE_ADDR_0_REG + i),
565 0);
566 }
567 }
568
569 return 0;
570 }
571
rf2xx_set_short_addr(const struct device * dev,bool set,uint16_t short_addr)572 static int rf2xx_set_short_addr(const struct device *dev, bool set,
573 uint16_t short_addr)
574 {
575 uint8_t short_addr_le[2] = { 0xFF, 0xFF };
576
577 if (set) {
578 sys_put_le16(short_addr, short_addr_le);
579 }
580
581 rf2xx_iface_reg_write(dev, RF2XX_SHORT_ADDR_0_REG, short_addr_le[0]);
582 rf2xx_iface_reg_write(dev, RF2XX_SHORT_ADDR_1_REG, short_addr_le[1]);
583 rf2xx_iface_reg_write(dev, RF2XX_CSMA_SEED_0_REG,
584 short_addr_le[0] + short_addr_le[1]);
585
586 LOG_DBG("Short Address: 0x%02X%02X", short_addr_le[1],
587 short_addr_le[0]);
588
589 return 0;
590 }
591
rf2xx_set_pan_id(const struct device * dev,bool set,uint16_t pan_id)592 static int rf2xx_set_pan_id(const struct device *dev, bool set,
593 uint16_t pan_id)
594 {
595 uint8_t pan_id_le[2] = { 0xFF, 0xFF };
596
597 if (set) {
598 sys_put_le16(pan_id, pan_id_le);
599 }
600
601 rf2xx_iface_reg_write(dev, RF2XX_PAN_ID_0_REG, pan_id_le[0]);
602 rf2xx_iface_reg_write(dev, RF2XX_PAN_ID_1_REG, pan_id_le[1]);
603
604 LOG_DBG("Pan Id: 0x%02X%02X", pan_id_le[1], pan_id_le[0]);
605
606 return 0;
607 }
608
rf2xx_filter(const struct device * dev,bool set,enum ieee802154_filter_type type,const struct ieee802154_filter * filter)609 static int rf2xx_filter(const struct device *dev,
610 bool set, enum ieee802154_filter_type type,
611 const struct ieee802154_filter *filter)
612 {
613 LOG_DBG("Applying filter %u", type);
614
615 if (type == IEEE802154_FILTER_TYPE_IEEE_ADDR) {
616 return rf2xx_set_ieee_addr(dev, set, filter->ieee_addr);
617 } else if (type == IEEE802154_FILTER_TYPE_SHORT_ADDR) {
618 return rf2xx_set_short_addr(dev, set, filter->short_addr);
619 } else if (type == IEEE802154_FILTER_TYPE_PAN_ID) {
620 return rf2xx_set_pan_id(dev, set, filter->pan_id);
621 }
622
623 return -ENOTSUP;
624 }
625
626 #if defined(CONFIG_NET_L2_OPENTHREAD)
rf2xx_handle_ack(struct rf2xx_context * ctx,struct net_buf * frag)627 static void rf2xx_handle_ack(struct rf2xx_context *ctx, struct net_buf *frag)
628 {
629 if ((frag->data[0] & RF2XX_FRAME_CTRL_ACK_REQUEST_BIT) == 0) {
630 return;
631 }
632
633 rf2xx_ack_psdu[0] = RF2XX_ACK_FRAME_TYPE;
634 rf2xx_ack_psdu[2] = frag->data[2];
635
636 if (ctx->trx_trac == RF2XX_TRX_PHY_STATE_TRAC_SUCCESS_DATA_PENDING) {
637 rf2xx_ack_psdu[0] |= RF2XX_ACK_FRAME_PENDING_BIT;
638 }
639
640 net_pkt_cursor_init(&rf2xx_ack_pkt);
641
642 if (ieee802154_handle_ack(ctx->iface, &rf2xx_ack_pkt) != NET_OK) {
643 LOG_INF("ACK packet not handled.");
644 }
645 }
646 #else
647 #define rf2xx_handle_ack(...)
648 #endif
649
rf2xx_tx(const struct device * dev,enum ieee802154_tx_mode mode,struct net_pkt * pkt,struct net_buf * frag)650 static int rf2xx_tx(const struct device *dev,
651 enum ieee802154_tx_mode mode,
652 struct net_pkt *pkt,
653 struct net_buf *frag)
654 {
655 ARG_UNUSED(pkt);
656
657 struct rf2xx_context *ctx = dev->data;
658 int response = 0;
659
660 LOG_DBG("TX");
661
662 if (ctx->tx_mode != mode) {
663 switch (mode) {
664 case IEEE802154_TX_MODE_DIRECT:
665 /* skip retries & csma/ca algorithm */
666 rf2xx_iface_reg_write(dev, RF2XX_XAH_CTRL_0_REG, 0x0E);
667 break;
668 case IEEE802154_TX_MODE_CSMA_CA:
669 /* backoff maxBE = 5, minBE = 3 */
670 rf2xx_iface_reg_write(dev, RF2XX_CSMA_BE_REG, 0x53);
671 /* max frame retries = 3, csma/ca retries = 4 */
672 rf2xx_iface_reg_write(dev, RF2XX_XAH_CTRL_0_REG, 0x38);
673 break;
674 case IEEE802154_TX_MODE_CCA:
675 /* backoff period = 0 */
676 rf2xx_iface_reg_write(dev, RF2XX_CSMA_BE_REG, 0x00);
677 /* no frame retries & no csma/ca retries */
678 rf2xx_iface_reg_write(dev, RF2XX_XAH_CTRL_0_REG, 0x00);
679 break;
680 case IEEE802154_TX_MODE_TXTIME:
681 case IEEE802154_TX_MODE_TXTIME_CCA:
682 default:
683 NET_ERR("TX mode %d not supported", mode);
684 return -ENOTSUP;
685 }
686
687 ctx->tx_mode = mode;
688 }
689
690 rf2xx_trx_set_tx_state(dev);
691 rf2xx_iface_reg_read(dev, RF2XX_IRQ_STATUS_REG);
692
693 k_sem_reset(&ctx->trx_tx_sync);
694 rf2xx_iface_frame_write(dev, frag->data, frag->len);
695 rf2xx_iface_phy_tx_start(dev);
696 k_sem_take(&ctx->trx_tx_sync, K_FOREVER);
697
698 switch (ctx->trx_trac) {
699 /* Channel is still busy after attempting MAX_CSMA_RETRIES of
700 * CSMA-CA
701 */
702 case RF2XX_TRX_PHY_STATE_TRAC_CHANNEL_ACCESS_FAILED:
703 response = -EBUSY;
704 break;
705 /* No acknowledgment frames were received during all retry
706 * attempts
707 */
708 case RF2XX_TRX_PHY_STATE_TRAC_NO_ACK:
709 response = -EAGAIN;
710 break;
711 /* Transaction not yet finished */
712 case RF2XX_TRX_PHY_STATE_TRAC_INVALID:
713 response = -EINTR;
714 break;
715 /* RF2XX_TRX_PHY_STATE_TRAC_SUCCESS:
716 * The transaction was responded to by a valid ACK, or, if no
717 * ACK is requested, after a successful frame transmission.
718 *
719 * RF2XX_TRX_PHY_STATE_TRAC_SUCCESS_DATA_PENDING:
720 * Equivalent to SUCCESS and indicating that the “Frame
721 * Pending” bit (see Section 8.1.2.2) of the received
722 * acknowledgment frame was set.
723 */
724 default:
725 rf2xx_handle_ack(ctx, frag);
726 break;
727 }
728
729 return response;
730 }
731
rf2xx_start(const struct device * dev)732 static int rf2xx_start(const struct device *dev)
733 {
734 const struct rf2xx_config *conf = dev->config;
735
736 LOG_DBG("Start");
737
738 rf2xx_trx_set_state(dev, RF2XX_TRX_PHY_STATE_CMD_TRX_OFF);
739 rf2xx_iface_reg_read(dev, RF2XX_IRQ_STATUS_REG);
740 gpio_pin_interrupt_configure_dt(&conf->irq_gpio,
741 GPIO_INT_EDGE_TO_ACTIVE);
742 rf2xx_trx_set_rx_state(dev);
743
744 return 0;
745 }
746
rf2xx_stop(const struct device * dev)747 static int rf2xx_stop(const struct device *dev)
748 {
749 const struct rf2xx_config *conf = dev->config;
750
751 LOG_DBG("Stop");
752
753 gpio_pin_interrupt_configure_dt(&conf->irq_gpio, GPIO_INT_DISABLE);
754 rf2xx_trx_set_state(dev, RF2XX_TRX_PHY_STATE_CMD_TRX_OFF);
755 rf2xx_iface_reg_read(dev, RF2XX_IRQ_STATUS_REG);
756
757 return 0;
758 }
759
rf2xx_pan_coord_set(const struct device * dev,bool pan_coordinator)760 static int rf2xx_pan_coord_set(const struct device *dev, bool pan_coordinator)
761 {
762 uint8_t reg;
763
764 if (pan_coordinator) {
765 reg = rf2xx_iface_reg_read(dev, RF2XX_CSMA_SEED_1_REG);
766 reg |= (1 << RF2XX_AACK_I_AM_COORD);
767 rf2xx_iface_reg_write(dev, RF2XX_CSMA_SEED_1_REG, reg);
768 } else {
769 reg = rf2xx_iface_reg_read(dev, RF2XX_CSMA_SEED_1_REG);
770 reg &= ~(1 << RF2XX_AACK_I_AM_COORD);
771 rf2xx_iface_reg_write(dev, RF2XX_CSMA_SEED_1_REG, reg);
772 }
773
774 return 0;
775 }
776
rf2xx_promiscuous_set(const struct device * dev,bool promiscuous)777 static int rf2xx_promiscuous_set(const struct device *dev, bool promiscuous)
778 {
779 struct rf2xx_context *ctx = dev->data;
780 uint8_t reg;
781
782 ctx->promiscuous = promiscuous;
783
784 if (promiscuous) {
785 reg = rf2xx_iface_reg_read(dev, RF2XX_XAH_CTRL_1_REG);
786 reg |= (1 << RF2XX_AACK_PROM_MODE);
787 rf2xx_iface_reg_write(dev, RF2XX_XAH_CTRL_1_REG, reg);
788
789 reg = rf2xx_iface_reg_read(dev, RF2XX_CSMA_SEED_1_REG);
790 reg |= (1 << RF2XX_AACK_DIS_ACK);
791 rf2xx_iface_reg_write(dev, RF2XX_CSMA_SEED_1_REG, reg);
792 } else {
793 reg = rf2xx_iface_reg_read(dev, RF2XX_XAH_CTRL_1_REG);
794 reg &= ~(1 << RF2XX_AACK_PROM_MODE);
795 rf2xx_iface_reg_write(dev, RF2XX_XAH_CTRL_1_REG, reg);
796
797 reg = rf2xx_iface_reg_read(dev, RF2XX_CSMA_SEED_1_REG);
798 reg &= ~(1 << RF2XX_AACK_DIS_ACK);
799 rf2xx_iface_reg_write(dev, RF2XX_CSMA_SEED_1_REG, reg);
800 }
801
802 return 0;
803 }
804
rf2xx_configure(const struct device * dev,enum ieee802154_config_type type,const struct ieee802154_config * config)805 int rf2xx_configure(const struct device *dev,
806 enum ieee802154_config_type type,
807 const struct ieee802154_config *config)
808 {
809 int ret = -EINVAL;
810
811 LOG_DBG("Configure %d", type);
812
813 switch (type) {
814 case IEEE802154_CONFIG_AUTO_ACK_FPB:
815 case IEEE802154_CONFIG_ACK_FPB:
816 break;
817
818 case IEEE802154_CONFIG_PAN_COORDINATOR:
819 ret = rf2xx_pan_coord_set(dev, config->pan_coordinator);
820 break;
821
822 case IEEE802154_CONFIG_PROMISCUOUS:
823 ret = rf2xx_promiscuous_set(dev, config->promiscuous);
824 break;
825
826 case IEEE802154_CONFIG_EVENT_HANDLER:
827 default:
828 break;
829 }
830
831 return ret;
832 }
833
rf2xx_attr_get(const struct device * dev,enum ieee802154_attr attr,struct ieee802154_attr_value * value)834 static int rf2xx_attr_get(const struct device *dev, enum ieee802154_attr attr,
835 struct ieee802154_attr_value *value)
836 {
837 struct rf2xx_context *ctx = dev->data;
838
839 switch (attr) {
840 case IEEE802154_ATTR_PHY_SUPPORTED_CHANNEL_PAGES:
841 value->phy_supported_channel_pages = ctx->cc_page;
842 return 0;
843
844 case IEEE802154_ATTR_PHY_SUPPORTED_CHANNEL_RANGES:
845 value->phy_supported_channels = &ctx->cc_channels;
846 return 0;
847
848 default:
849 return -ENOENT;
850 }
851 }
852
power_on_and_setup(const struct device * dev)853 static int power_on_and_setup(const struct device *dev)
854 {
855 const struct rf2xx_config *conf = dev->config;
856 struct rf2xx_context *ctx = dev->data;
857 uint8_t config;
858
859 rf2xx_iface_phy_rst(dev);
860
861 /* Sync transceiver state */
862 do {
863 rf2xx_iface_reg_write(dev, RF2XX_TRX_STATE_REG,
864 RF2XX_TRX_PHY_STATE_CMD_TRX_OFF);
865 } while (RF2XX_TRX_PHY_STATUS_TRX_OFF !=
866 (rf2xx_iface_reg_read(dev, RF2XX_TRX_STATUS_REG) &
867 RF2XX_TRX_PHY_STATUS_MASK));
868
869 /* get device identification */
870 ctx->trx_model = rf2xx_iface_reg_read(dev, RF2XX_PART_NUM_REG);
871 ctx->trx_version = rf2xx_iface_reg_read(dev, RF2XX_VERSION_NUM_REG);
872
873 /**
874 * Valid transceiver are:
875 * 231-Rev-A (Version 0x02)
876 * 232-Rev-A (Version 0x02)
877 * 233-Rev-A (Version 0x01) (Warning)
878 * 233-Rev-B (Version 0x02)
879 */
880 if (ctx->trx_model <= RF2XX_TRX_MODEL_230) {
881 LOG_DBG("Invalid or not supported transceiver");
882 return -ENODEV;
883 }
884
885 if (ctx->trx_model == RF2XX_TRX_MODEL_233 && ctx->trx_version == 0x01) {
886 LOG_DBG("Transceiver is old and unstable release");
887 }
888
889 /* Set RSSI base */
890 if (ctx->trx_model == RF2XX_TRX_MODEL_212) {
891 ctx->trx_rssi_base = -100;
892 } else if (ctx->trx_model == RF2XX_TRX_MODEL_233) {
893 ctx->trx_rssi_base = -94;
894 } else if (ctx->trx_model == RF2XX_TRX_MODEL_231) {
895 ctx->trx_rssi_base = -91;
896 } else {
897 ctx->trx_rssi_base = -90;
898 }
899
900 /* Disable All Features of TRX_CTRL_0 */
901 config = 0;
902 rf2xx_iface_reg_write(dev, RF2XX_TRX_CTRL_0_REG, config);
903
904 /* Configure PHY behaviour */
905 config = (1 << RF2XX_TX_AUTO_CRC_ON) |
906 (3 << RF2XX_SPI_CMD_MODE) |
907 (1 << RF2XX_IRQ_MASK_MODE);
908 rf2xx_iface_reg_write(dev, RF2XX_TRX_CTRL_1_REG, config);
909
910 config = (1 << RF2XX_RX_SAFE_MODE);
911 if (ctx->trx_model != RF2XX_TRX_MODEL_232) {
912 config |= (1 << RF2XX_OQPSK_SCRAM_EN);
913 }
914 rf2xx_iface_reg_write(dev, RF2XX_TRX_CTRL_2_REG, config);
915
916 if (ctx->trx_model == RF2XX_TRX_MODEL_212) {
917 rf2xx_configure_trx_path(dev);
918 rf2xx_iface_reg_write(dev, RF2XX_CC_CTRL_1_REG, 0);
919 }
920
921 ctx->tx_mode = IEEE802154_TX_MODE_CSMA_CA;
922 ctx->promiscuous = false;
923
924 /* Configure INT behaviour */
925 config = (1 << RF2XX_RX_START) |
926 (1 << RF2XX_TRX_END);
927 rf2xx_iface_reg_write(dev, RF2XX_IRQ_MASK_REG, config);
928
929 gpio_init_callback(&ctx->irq_cb, trx_isr_handler,
930 BIT(conf->irq_gpio.pin));
931
932 if (gpio_add_callback(conf->irq_gpio.port, &ctx->irq_cb) < 0) {
933 LOG_ERR("Could not set IRQ callback.");
934 return -ENXIO;
935 }
936
937 return 0;
938 }
939
configure_gpios(const struct device * dev)940 static inline int configure_gpios(const struct device *dev)
941 {
942 const struct rf2xx_config *conf = dev->config;
943
944 /* Chip IRQ line */
945 if (!gpio_is_ready_dt(&conf->irq_gpio)) {
946 LOG_ERR("IRQ GPIO device not ready");
947 return -ENODEV;
948 }
949 gpio_pin_configure_dt(&conf->irq_gpio, GPIO_INPUT);
950 gpio_pin_interrupt_configure_dt(&conf->irq_gpio,
951 GPIO_INT_EDGE_TO_ACTIVE);
952
953 /* Chip RESET line */
954 if (!gpio_is_ready_dt(&conf->reset_gpio)) {
955 LOG_ERR("RESET GPIO device not ready");
956 return -ENODEV;
957 }
958 gpio_pin_configure_dt(&conf->reset_gpio, GPIO_OUTPUT_INACTIVE);
959
960 /* Chip SLPTR line */
961 if (!gpio_is_ready_dt(&conf->slptr_gpio)) {
962 LOG_ERR("SLPTR GPIO device not ready");
963 return -ENODEV;
964 }
965 gpio_pin_configure_dt(&conf->slptr_gpio, GPIO_OUTPUT_INACTIVE);
966
967 /* Chip DIG2 line (Optional feature) */
968 if (conf->dig2_gpio.port != NULL) {
969 if (!gpio_is_ready_dt(&conf->dig2_gpio)) {
970 LOG_ERR("DIG2 GPIO device not ready");
971 return -ENODEV;
972 }
973 LOG_INF("Optional instance of %s device activated",
974 conf->dig2_gpio.port->name);
975 gpio_pin_configure_dt(&conf->dig2_gpio, GPIO_INPUT);
976 gpio_pin_interrupt_configure_dt(&conf->dig2_gpio,
977 GPIO_INT_EDGE_TO_ACTIVE);
978 }
979
980 /* Chip CLKM line (Optional feature) */
981 if (conf->clkm_gpio.port != NULL) {
982 if (!gpio_is_ready_dt(&conf->clkm_gpio)) {
983 LOG_ERR("CLKM GPIO device not ready");
984 return -ENODEV;
985 }
986 LOG_INF("Optional instance of %s device activated",
987 conf->clkm_gpio.port->name);
988 gpio_pin_configure_dt(&conf->clkm_gpio, GPIO_INPUT);
989 }
990
991 return 0;
992 }
993
configure_spi(const struct device * dev)994 static inline int configure_spi(const struct device *dev)
995 {
996 const struct rf2xx_config *conf = dev->config;
997
998 if (!spi_is_ready_dt(&conf->spi)) {
999 LOG_ERR("SPI bus %s is not ready",
1000 conf->spi.bus->name);
1001 return -ENODEV;
1002 }
1003
1004 return 0;
1005 }
1006
rf2xx_init(const struct device * dev)1007 static int rf2xx_init(const struct device *dev)
1008 {
1009 struct rf2xx_context *ctx = dev->data;
1010 const struct rf2xx_config *conf = dev->config;
1011 char thread_name[20];
1012
1013 LOG_DBG("\nInitialize RF2XX Transceiver\n");
1014
1015 ctx->dev = dev;
1016
1017 k_sem_init(&ctx->trx_tx_sync, 0, 1);
1018 k_sem_init(&ctx->trx_isr_lock, 0, 1);
1019
1020 if (configure_gpios(dev) != 0) {
1021 LOG_ERR("Configuring GPIOS failed");
1022 return -EIO;
1023 }
1024
1025 if (configure_spi(dev) != 0) {
1026 LOG_ERR("Configuring SPI failed");
1027 return -EIO;
1028 }
1029
1030 LOG_DBG("GPIO and SPI configured");
1031
1032 if (power_on_and_setup(dev) != 0) {
1033 LOG_ERR("Configuring RF2XX failed");
1034 return -EIO;
1035 }
1036
1037 LOG_DBG("RADIO configured");
1038
1039 k_thread_create(&ctx->trx_thread,
1040 ctx->trx_stack,
1041 CONFIG_IEEE802154_RF2XX_RX_STACK_SIZE,
1042 rf2xx_thread_main,
1043 ctx, NULL, NULL,
1044 K_PRIO_COOP(2), 0, K_NO_WAIT);
1045
1046 snprintk(thread_name, sizeof(thread_name),
1047 "rf2xx_trx [%d]", conf->inst);
1048 k_thread_name_set(&ctx->trx_thread, thread_name);
1049
1050 LOG_DBG("Thread OK");
1051
1052 return 0;
1053 }
1054
rf2xx_iface_init(struct net_if * iface)1055 static void rf2xx_iface_init(struct net_if *iface)
1056 {
1057 const struct device *dev = net_if_get_device(iface);
1058 struct rf2xx_context *ctx = dev->data;
1059 uint8_t *mac = get_mac(dev);
1060
1061 net_if_set_link_addr(iface, mac, 8, NET_LINK_IEEE802154);
1062
1063 ctx->iface = iface;
1064
1065 if (ctx->trx_model == RF2XX_TRX_MODEL_212) {
1066 if (ctx->cc_page == IEEE802154_ATTR_PHY_CHANNEL_PAGE_ZERO_OQPSK_2450_BPSK_868_915 ||
1067 ctx->cc_page == IEEE802154_ATTR_PHY_CHANNEL_PAGE_TWO_OQPSK_868_915) {
1068 ctx->cc_range.from_channel = 0U;
1069 ctx->cc_range.to_channel = 10U;
1070 } else if (ctx->cc_page == IEEE802154_ATTR_PHY_CHANNEL_PAGE_FIVE_OQPSK_780) {
1071 ctx->cc_range.from_channel = 0U;
1072 ctx->cc_range.to_channel = 3U;
1073 } else {
1074 __ASSERT(false, "Unsupported channel page %u.", ctx->cc_page);
1075 }
1076 } else {
1077 __ASSERT(ctx->cc_page ==
1078 IEEE802154_ATTR_PHY_CHANNEL_PAGE_ZERO_OQPSK_2450_BPSK_868_915,
1079 "Unsupported channel page %u.", ctx->cc_page);
1080 ctx->cc_range.from_channel = 11U;
1081 ctx->cc_range.to_channel = 26U;
1082 }
1083
1084 ieee802154_init(iface);
1085 }
1086
1087 static const struct ieee802154_radio_api rf2xx_radio_api = {
1088 .iface_api.init = rf2xx_iface_init,
1089
1090 .get_capabilities = rf2xx_get_capabilities,
1091 .cca = rf2xx_cca,
1092 .set_channel = rf2xx_set_channel,
1093 .filter = rf2xx_filter,
1094 .set_txpower = rf2xx_set_txpower,
1095 .tx = rf2xx_tx,
1096 .start = rf2xx_start,
1097 .stop = rf2xx_stop,
1098 .configure = rf2xx_configure,
1099 .attr_get = rf2xx_attr_get,
1100 };
1101
1102 #if !defined(CONFIG_IEEE802154_RAW_MODE)
1103 #if defined(CONFIG_NET_L2_IEEE802154)
1104 #define L2 IEEE802154_L2
1105 #define L2_CTX_TYPE NET_L2_GET_CTX_TYPE(IEEE802154_L2)
1106 #define MTU RF2XX_MAX_PSDU_LENGTH
1107 #elif defined(CONFIG_NET_L2_OPENTHREAD)
1108 #define L2 OPENTHREAD_L2
1109 #define L2_CTX_TYPE NET_L2_GET_CTX_TYPE(OPENTHREAD_L2)
1110 #define MTU RF2XX_OT_PSDU_LENGTH
1111 #endif
1112 #endif /* CONFIG_IEEE802154_RAW_MODE */
1113
1114 #define DRV_INST_LOCAL_MAC_ADDRESS(n) \
1115 UTIL_AND(DT_INST_NODE_HAS_PROP(n, local_mac_address), \
1116 UTIL_AND(DT_INST_PROP_LEN(n, local_mac_address) == 8, \
1117 DT_INST_PROP(n, local_mac_address)))
1118
1119 #define IEEE802154_RF2XX_DEVICE_CONFIG(n) \
1120 BUILD_ASSERT(DT_INST_PROP_LEN(n, tx_pwr_min) == 2, \
1121 "rf2xx: Error TX-PWR-MIN len is different of two"); \
1122 BUILD_ASSERT(DT_INST_PROP_LEN(n, tx_pwr_max) == 2, \
1123 "rf2xx: Error TX-PWR-MAX len is different of two"); \
1124 BUILD_ASSERT(DT_INST_PROP_LEN(n, tx_pwr_table) != 0, \
1125 "rf2xx: Error TX-PWR-TABLE len must be greater than zero"); \
1126 static const uint8_t rf2xx_pwr_table_##n[] = \
1127 DT_INST_PROP_OR(n, tx_pwr_table, 0); \
1128 static const struct rf2xx_config rf2xx_ctx_config_##n = { \
1129 .inst = n, \
1130 .has_mac = DT_INST_NODE_HAS_PROP(n, local_mac_address), \
1131 .irq_gpio = GPIO_DT_SPEC_INST_GET(n, irq_gpios), \
1132 .reset_gpio = GPIO_DT_SPEC_INST_GET(n, reset_gpios), \
1133 .slptr_gpio = GPIO_DT_SPEC_INST_GET(n, slptr_gpios), \
1134 .dig2_gpio = GPIO_DT_SPEC_INST_GET_OR(n, dig2_gpios, {}), \
1135 .clkm_gpio = GPIO_DT_SPEC_INST_GET_OR(n, clkm_gpios, {}), \
1136 .spi = SPI_DT_SPEC_INST_GET(n, SPI_WORD_SET(8) | \
1137 SPI_TRANSFER_MSB, 0), \
1138 \
1139 .tx_pwr_min = DT_INST_PROP_OR(n, tx_pwr_min, 0), \
1140 .tx_pwr_max = DT_INST_PROP_OR(n, tx_pwr_max, 0), \
1141 .tx_pwr_table = rf2xx_pwr_table_##n, \
1142 .tx_pwr_table_size = DT_INST_PROP_LEN(n, tx_pwr_table), \
1143 }
1144
1145 #define IEEE802154_RF2XX_DEVICE_DATA(n) \
1146 static struct rf2xx_context rf2xx_ctx_data_##n = { \
1147 .mac_addr = { DRV_INST_LOCAL_MAC_ADDRESS(n) }, \
1148 .cc_page = BIT(DT_INST_ENUM_IDX_OR(n, channel_page, 0)),\
1149 .cc_channels = { \
1150 .ranges = &rf2xx_ctx_data_##n.cc_range, \
1151 .num_ranges = 1U, \
1152 } \
1153 }
1154
1155 #define IEEE802154_RF2XX_RAW_DEVICE_INIT(n) \
1156 DEVICE_DT_INST_DEFINE( \
1157 n, \
1158 &rf2xx_init, \
1159 NULL, \
1160 &rf2xx_ctx_data_##n, \
1161 &rf2xx_ctx_config_##n, \
1162 POST_KERNEL, \
1163 CONFIG_IEEE802154_RF2XX_INIT_PRIO, \
1164 &rf2xx_radio_api)
1165
1166 #define IEEE802154_RF2XX_NET_DEVICE_INIT(n) \
1167 NET_DEVICE_DT_INST_DEFINE( \
1168 n, \
1169 &rf2xx_init, \
1170 NULL, \
1171 &rf2xx_ctx_data_##n, \
1172 &rf2xx_ctx_config_##n, \
1173 CONFIG_IEEE802154_RF2XX_INIT_PRIO, \
1174 &rf2xx_radio_api, \
1175 L2, \
1176 L2_CTX_TYPE, \
1177 MTU)
1178
1179 #define IEEE802154_RF2XX_INIT(inst) \
1180 IEEE802154_RF2XX_DEVICE_CONFIG(inst); \
1181 IEEE802154_RF2XX_DEVICE_DATA(inst); \
1182 \
1183 COND_CODE_1(CONFIG_IEEE802154_RAW_MODE, \
1184 (IEEE802154_RF2XX_RAW_DEVICE_INIT(inst);), \
1185 (IEEE802154_RF2XX_NET_DEVICE_INIT(inst);))
1186
1187 DT_INST_FOREACH_STATUS_OKAY(IEEE802154_RF2XX_INIT)
1188