1 /* ieee802154_cc1200.c - TI CC1200 driver */
2
3 #define DT_DRV_COMPAT ti_cc1200
4
5 /*
6 * Copyright (c) 2017 Intel Corporation.
7 *
8 * SPDX-License-Identifier: Apache-2.0
9 */
10
11 #define LOG_MODULE_NAME ieee802154_cc1200
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
19 #include <zephyr/kernel.h>
20 #include <zephyr/arch/cpu.h>
21 #include <zephyr/debug/stack.h>
22
23 #include <zephyr/device.h>
24 #include <zephyr/init.h>
25 #include <zephyr/net/net_if.h>
26 #include <zephyr/net/net_pkt.h>
27
28 #include <zephyr/sys/byteorder.h>
29 #include <string.h>
30 #include <zephyr/random/random.h>
31
32 #include <zephyr/drivers/spi.h>
33 #include <zephyr/drivers/gpio.h>
34
35 #include <zephyr/net/ieee802154_radio.h>
36
37 #include "ieee802154_cc1200.h"
38 #include "ieee802154_cc1200_rf.h"
39
40 /* ToDo: supporting 802.15.4g will require GPIO2
41 * used as CC1200_GPIO_SIG_RXFIFO_THR
42 *
43 * Note: GPIO3 is unused.
44 */
45 #define CC1200_IOCFG3 CC1200_GPIO_SIG_MARC_2PIN_STATUS_0
46 #define CC1200_IOCFG2 CC1200_GPIO_SIG_MARC_2PIN_STATUS_1
47 #define CC1200_IOCFG0 CC1200_GPIO_SIG_PKT_SYNC_RXTX
48
49 /***********************
50 * Debugging functions *
51 **********************/
cc1200_print_status(uint8_t status)52 static void cc1200_print_status(uint8_t status)
53 {
54 if (status == CC1200_STATUS_IDLE) {
55 LOG_DBG("Idling");
56 } else if (status == CC1200_STATUS_RX) {
57 LOG_DBG("Receiving");
58 } else if (status == CC1200_STATUS_TX) {
59 LOG_DBG("Transmitting");
60 } else if (status == CC1200_STATUS_FSTXON) {
61 LOG_DBG("FS TX on");
62 } else if (status == CC1200_STATUS_CALIBRATE) {
63 LOG_DBG("Calibrating");
64 } else if (status == CC1200_STATUS_SETTLING) {
65 LOG_DBG("Settling");
66 } else if (status == CC1200_STATUS_RX_FIFO_ERROR) {
67 LOG_DBG("RX FIFO error!");
68 } else if (status == CC1200_STATUS_TX_FIFO_ERROR) {
69 LOG_DBG("TX FIFO error!");
70 }
71 }
72
73 /*********************
74 * Generic functions *
75 ********************/
76
z_cc1200_access_reg(const struct device * dev,bool read,uint8_t addr,void * data,size_t length,bool extended,bool burst)77 bool z_cc1200_access_reg(const struct device *dev, bool read, uint8_t addr,
78 void *data, size_t length, bool extended, bool burst)
79 {
80 const struct cc1200_config *config = dev->config;
81 uint8_t cmd_buf[2];
82 const struct spi_buf buf[2] = {
83 {
84 .buf = cmd_buf,
85 .len = extended ? 2 : 1,
86 },
87 {
88 .buf = data,
89 .len = length,
90
91 }
92 };
93 struct spi_buf_set tx = { .buffers = buf };
94
95 cmd_buf[0] = 0U;
96
97 if (burst) {
98 cmd_buf[0] |= CC1200_ACCESS_BURST;
99 }
100
101 if (extended) {
102 cmd_buf[0] |= CC1200_REG_EXTENDED_ADDRESS;
103 cmd_buf[1] = addr;
104 } else {
105 cmd_buf[0] |= addr;
106 }
107
108 if (read) {
109 const struct spi_buf_set rx = {
110 .buffers = buf,
111 .count = 2
112 };
113
114 cmd_buf[0] |= CC1200_ACCESS_RD;
115
116 tx.count = 1;
117
118 return (spi_transceive_dt(&config->bus, &tx, &rx) == 0);
119 }
120
121 /* CC1200_ACCESS_WR is 0 so no need to play with it */
122 tx.count = data ? 2 : 1;
123
124 return (spi_write_dt(&config->bus, &tx) == 0);
125 }
126
get_mac(const struct device * dev)127 static inline uint8_t *get_mac(const struct device *dev)
128 {
129 struct cc1200_context *cc1200 = dev->data;
130
131 #if defined(CONFIG_IEEE802154_CC1200_RANDOM_MAC)
132 uint32_t *ptr = (uint32_t *)(cc1200->mac_addr + 4);
133
134 UNALIGNED_PUT(sys_rand32_get(), ptr);
135
136 cc1200->mac_addr[7] = (cc1200->mac_addr[7] & ~0x01) | 0x02;
137 #else
138 cc1200->mac_addr[4] = CONFIG_IEEE802154_CC1200_MAC4;
139 cc1200->mac_addr[5] = CONFIG_IEEE802154_CC1200_MAC5;
140 cc1200->mac_addr[6] = CONFIG_IEEE802154_CC1200_MAC6;
141 cc1200->mac_addr[7] = CONFIG_IEEE802154_CC1200_MAC7;
142 #endif
143
144 cc1200->mac_addr[0] = 0x00;
145 cc1200->mac_addr[1] = 0x12;
146 cc1200->mac_addr[2] = 0x4b;
147 cc1200->mac_addr[3] = 0x00;
148
149 return cc1200->mac_addr;
150 }
151
get_status(const struct device * dev)152 static uint8_t get_status(const struct device *dev)
153 {
154 uint8_t val;
155
156 if (z_cc1200_access_reg(dev, true, CC1200_INS_SNOP,
157 &val, 1, false, false)) {
158 /* See Section 3.1.2 */
159 return val & CC1200_STATUS_MASK;
160 }
161
162 /* We cannot get the status, so let's assume about readiness */
163 return CC1200_STATUS_CHIP_NOT_READY;
164 }
165
166 /******************
167 * GPIO functions *
168 *****************/
169
gpio0_int_handler(const struct device * port,struct gpio_callback * cb,uint32_t pins)170 static inline void gpio0_int_handler(const struct device *port,
171 struct gpio_callback *cb, uint32_t pins)
172 {
173 struct cc1200_context *cc1200 =
174 CONTAINER_OF(cb, struct cc1200_context, rx_tx_cb);
175
176 if (atomic_get(&cc1200->tx) == 1) {
177 if (atomic_get(&cc1200->tx_start) == 0) {
178 atomic_set(&cc1200->tx_start, 1);
179 } else {
180 atomic_set(&cc1200->tx, 0);
181 }
182
183 k_sem_give(&cc1200->tx_sync);
184 } else {
185 if (atomic_get(&cc1200->rx) == 1) {
186 k_sem_give(&cc1200->rx_lock);
187 atomic_set(&cc1200->rx, 0);
188 } else {
189 atomic_set(&cc1200->rx, 1);
190 }
191 }
192 }
193
enable_gpio0_interrupt(const struct device * dev,bool enable)194 static void enable_gpio0_interrupt(const struct device *dev, bool enable)
195 {
196 const struct cc1200_config *cfg = dev->config;
197 gpio_flags_t mode = enable ? GPIO_INT_EDGE_TO_ACTIVE : GPIO_INT_DISABLE;
198
199 gpio_pin_interrupt_configure_dt(&cfg->interrupt, mode);
200 }
201
setup_gpio_callback(const struct device * dev)202 static int setup_gpio_callback(const struct device *dev)
203 {
204 const struct cc1200_config *cfg = dev->config;
205 struct cc1200_context *cc1200 = dev->data;
206
207 gpio_init_callback(&cc1200->rx_tx_cb, gpio0_int_handler, BIT(cfg->interrupt.pin));
208
209 if (gpio_add_callback(cfg->interrupt.port, &cc1200->rx_tx_cb) != 0) {
210 return -EIO;
211 }
212
213 return 0;
214 }
215
216 /****************
217 * RF functions *
218 ***************/
219
get_lo_divider(const struct device * dev)220 static uint8_t get_lo_divider(const struct device *dev)
221 {
222 /* See Table 34 */
223 return FSD_BANDSELECT(read_reg_fs_cfg(dev)) << 1;
224 }
225
write_reg_freq(const struct device * dev,uint32_t freq)226 static bool write_reg_freq(const struct device *dev, uint32_t freq)
227 {
228 uint8_t freq_data[3];
229
230 freq_data[0] = (uint8_t)((freq & 0x00FF0000) >> 16);
231 freq_data[1] = (uint8_t)((freq & 0x0000FF00) >> 8);
232 freq_data[2] = (uint8_t)(freq & 0x000000FF);
233
234 return z_cc1200_access_reg(dev, false, CC1200_REG_FREQ2,
235 freq_data, 3, true, true);
236 }
237
238
239 /* See Section 9.12 - RF programming
240 *
241 * The given formula in datasheet cannot be simply applied here, where CPU
242 * limits us to unsigned integers of 32 bits. Instead, "slicing" it to
243 * parts that fits in such limit is a solution which is applied below.
244 *
245 * The original formula being (freqoff is neglected):
246 * Freq = ( RF * Lo_Div * 2^16 ) / Xtal
247 *
248 * RF and Xtal are, from here, expressed in KHz.
249 *
250 * It first calculates the targeted RF with given ChanCenterFreq0, channel
251 * spacing and the channel number.
252 *
253 * The calculation will slice the targeted RF by multiple of 10:
254 * 10^n where n is in [5, 3]. The rest, below 1000, is taken at once.
255 * Let's take the 434000 KHz RF for instance:
256 * it will be "sliced" in 3 parts: 400000, 30000, 4000.
257 * Or the 169406 KHz RF, 4 parts: 100000, 60000, 9000, 406.
258 *
259 * This permits also to play with Xtal to keep the result big enough to avoid
260 * losing precision. A factor - growing as much as Xtal decrease - is then
261 * applied to get to the proper result. Which one is rounded to the nearest
262 * integer, again to get a bit better precision.
263 *
264 * In the end, this algorithm below works for all the supported bands by CC1200.
265 * User does not need to pass anything extra besides the nominal settings: no
266 * pre-computed part or else.
267 */
rf_evaluate_freq_setting(const struct device * dev,uint32_t chan)268 static uint32_t rf_evaluate_freq_setting(const struct device *dev, uint32_t chan)
269 {
270 struct cc1200_context *ctx = dev->data;
271 uint32_t xtal = CONFIG_IEEE802154_CC1200_XOSC;
272 uint32_t mult_10 = 100000U;
273 uint32_t factor = 1U;
274 uint32_t freq = 0U;
275 uint32_t rf, lo_div;
276
277 rf = ctx->rf_settings->chan_center_freq0 +
278 ((chan * (uint32_t)ctx->rf_settings->channel_spacing) / 10U);
279 lo_div = get_lo_divider(dev);
280
281 LOG_DBG("Calculating freq for %u KHz RF (%u)", rf, lo_div);
282
283 while (rf > 0) {
284 uint32_t hz, freq_tmp, rst;
285
286 if (rf < 1000) {
287 hz = rf;
288 } else {
289 hz = rf / mult_10;
290 hz *= mult_10;
291 }
292
293 if (hz < 1000) {
294 freq_tmp = (hz * lo_div * 65536U) / xtal;
295 } else {
296 freq_tmp = ((hz * lo_div) / xtal) * 65536U;
297 }
298
299 rst = freq_tmp % factor;
300 freq_tmp /= factor;
301
302 if (factor > 1 && (rst/(factor/10U)) > 5) {
303 freq_tmp++;
304 }
305
306 freq += freq_tmp;
307
308 factor *= 10U;
309 mult_10 /= 10U;
310 xtal /= 10U;
311 rf -= hz;
312 }
313
314 LOG_DBG("FREQ is 0x%06X", freq);
315
316 return freq;
317 }
318
319 static bool
rf_install_settings(const struct device * dev,const struct cc1200_rf_registers_set * rf_settings)320 rf_install_settings(const struct device *dev,
321 const struct cc1200_rf_registers_set *rf_settings)
322 {
323 struct cc1200_context *cc1200 = dev->data;
324
325 if (!z_cc1200_access_reg(dev, false, CC1200_REG_SYNC3,
326 (void *)rf_settings->registers,
327 CC1200_RF_NON_EXT_SPACE_REGS, false, true) ||
328 !z_cc1200_access_reg(dev, false, CC1200_REG_IF_MIX_CFG,
329 (uint8_t *)rf_settings->registers
330 + CC1200_RF_NON_EXT_SPACE_REGS,
331 CC1200_RF_EXT_SPACE_REGS, true, true) ||
332 !write_reg_pkt_len(dev, 0xFF)) {
333 LOG_ERR("Could not install RF settings");
334 return false;
335 }
336
337 cc1200->rf_settings = rf_settings;
338
339 return true;
340 }
341
rf_calibrate(const struct device * dev)342 static int rf_calibrate(const struct device *dev)
343 {
344 if (!instruct_scal(dev)) {
345 LOG_ERR("Could not calibrate RF");
346 return -EIO;
347 }
348
349 k_busy_wait(USEC_PER_MSEC * 5U);
350
351 /* We need to re-enable RX as SCAL shuts off the freq synth */
352 if (!instruct_sidle(dev) ||
353 !instruct_sfrx(dev) ||
354 !instruct_srx(dev)) {
355 LOG_ERR("Could not switch to RX");
356 return -EIO;
357 }
358
359 k_busy_wait(USEC_PER_MSEC * 10U);
360
361 cc1200_print_status(get_status(dev));
362
363 return 0;
364 }
365
366 /****************
367 * TX functions *
368 ***************/
369
write_txfifo(const struct device * dev,void * data,size_t length)370 static inline bool write_txfifo(const struct device *dev,
371 void *data, size_t length)
372 {
373 return z_cc1200_access_reg(dev, false,
374 CC1200_REG_TXFIFO,
375 data, length, false, true);
376 }
377
378 /****************
379 * RX functions *
380 ***************/
381
read_rxfifo(const struct device * dev,void * data,size_t length)382 static inline bool read_rxfifo(const struct device *dev,
383 void *data, size_t length)
384 {
385 return z_cc1200_access_reg(dev, true,
386 CC1200_REG_RXFIFO,
387 data, length, false, true);
388 }
389
get_packet_length(const struct device * dev)390 static inline uint8_t get_packet_length(const struct device *dev)
391 {
392 uint8_t len;
393
394 if (z_cc1200_access_reg(dev, true, CC1200_REG_RXFIFO,
395 &len, 1, false, true)) {
396 return len;
397 }
398
399 return 0;
400 }
401
verify_rxfifo_validity(const struct device * dev,uint8_t pkt_len)402 static inline bool verify_rxfifo_validity(const struct device *dev,
403 uint8_t pkt_len)
404 {
405 /* packet should be at least 3 bytes as a ACK */
406 if (pkt_len < 3 ||
407 read_reg_num_rxbytes(dev) > (pkt_len + CC1200_FCS_LEN)) {
408 return false;
409 }
410
411 return true;
412 }
413
read_rxfifo_content(const struct device * dev,struct net_buf * buf,uint8_t len)414 static inline bool read_rxfifo_content(const struct device *dev,
415 struct net_buf *buf, uint8_t len)
416 {
417
418 if (!read_rxfifo(dev, buf->data, len) ||
419 (get_status(dev) == CC1200_STATUS_RX_FIFO_ERROR)) {
420 return false;
421 }
422
423 net_buf_add(buf, len);
424
425 return true;
426 }
427
verify_crc(const struct device * dev,struct net_pkt * pkt)428 static inline bool verify_crc(const struct device *dev, struct net_pkt *pkt)
429 {
430 uint8_t status[2];
431 int8_t rssi;
432
433 if (!read_rxfifo(dev, status, 2)) {
434 return false;
435 }
436
437 if (!(status[1] & CC1200_FCS_CRC_OK)) {
438 return false;
439 }
440
441 rssi = (int8_t) status[0];
442 net_pkt_set_ieee802154_rssi_dbm(
443 pkt, rssi == CC1200_INVALID_RSSI ? IEEE802154_MAC_RSSI_DBM_UNDEFINED : rssi);
444 net_pkt_set_ieee802154_lqi(pkt, status[1] & CC1200_FCS_LQI_MASK);
445
446 return true;
447 }
448
cc1200_rx(void * p1,void * p2,void * p3)449 static void cc1200_rx(void *p1, void *p2, void *p3)
450 {
451 ARG_UNUSED(p2);
452 ARG_UNUSED(p3);
453
454 const struct device *dev = p1;
455 struct cc1200_context *cc1200 = dev->data;
456 struct net_pkt *pkt;
457 uint8_t pkt_len;
458
459 while (1) {
460 pkt = NULL;
461
462 k_sem_take(&cc1200->rx_lock, K_FOREVER);
463
464 if (get_status(dev) == CC1200_STATUS_RX_FIFO_ERROR) {
465 LOG_ERR("Fifo error");
466 goto flush;
467 }
468
469 pkt_len = get_packet_length(dev);
470 if (!verify_rxfifo_validity(dev, pkt_len)) {
471 LOG_ERR("Invalid frame");
472 goto flush;
473 }
474
475 pkt = net_pkt_rx_alloc_with_buffer(cc1200->iface, pkt_len,
476 AF_UNSPEC, 0, K_NO_WAIT);
477 if (!pkt) {
478 LOG_ERR("No free pkt available");
479 goto flush;
480 }
481
482 if (!read_rxfifo_content(dev, pkt->buffer, pkt_len)) {
483 LOG_ERR("No content read");
484 goto flush;
485 }
486
487 if (!verify_crc(dev, pkt)) {
488 LOG_ERR("Bad packet CRC");
489 goto out;
490 }
491
492 if (ieee802154_handle_ack(cc1200->iface, pkt) == NET_OK) {
493 LOG_DBG("ACK packet handled");
494 goto out;
495 }
496
497 LOG_DBG("Caught a packet (%u)", pkt_len);
498
499 if (net_recv_data(cc1200->iface, pkt) < 0) {
500 LOG_DBG("Packet dropped by NET stack");
501 goto out;
502 }
503
504 log_stack_usage(&cc1200->rx_thread);
505 continue;
506 flush:
507 LOG_DBG("Flushing RX");
508 instruct_sidle(dev);
509 instruct_sfrx(dev);
510 instruct_srx(dev);
511 out:
512 if (pkt) {
513 net_pkt_unref(pkt);
514 }
515
516 }
517 }
518
519
520 /********************
521 * Radio device API *
522 *******************/
cc1200_get_capabilities(const struct device * dev)523 static enum ieee802154_hw_caps cc1200_get_capabilities(const struct device *dev)
524 {
525 return IEEE802154_HW_FCS;
526 }
527
cc1200_cca(const struct device * dev)528 static int cc1200_cca(const struct device *dev)
529 {
530 struct cc1200_context *cc1200 = dev->data;
531
532 if (atomic_get(&cc1200->rx) == 0) {
533 uint8_t status = read_reg_rssi0(dev);
534
535 if (!(status & CARRIER_SENSE) &&
536 (status & CARRIER_SENSE_VALID)) {
537 return 0;
538 }
539 }
540
541 LOG_WRN("Busy");
542
543 return -EBUSY;
544 }
545
cc1200_set_channel(const struct device * dev,uint16_t channel)546 static int cc1200_set_channel(const struct device *dev, uint16_t channel)
547 {
548 struct cc1200_context *cc1200 = dev->data;
549 uint32_t freq;
550
551 /* As SUN FSK provides a host of configurations with extremely different
552 * channel counts it doesn't make sense to validate (aka -EINVAL) a
553 * global upper limit on the number of supported channels on this page.
554 */
555 if (channel > IEEE802154_CC1200_CHANNEL_LIMIT) {
556 return -ENOTSUP;
557 }
558
559 /* Unlike usual 15.4 chips, cc1200 is closer to a bare metal radio modem
560 * and thus does not provide any means to select a channel directly, but
561 * requires instead that one calculates and configures the actual
562 * targeted frequency for the requested channel.
563 *
564 * See rf_evaluate_freq_setting() above.
565 */
566
567 if (atomic_get(&cc1200->rx) != 0) {
568 return -EIO;
569 }
570
571 freq = rf_evaluate_freq_setting(dev, channel);
572
573 if (!write_reg_freq(dev, freq) ||
574 rf_calibrate(dev)) {
575 LOG_ERR("Could not set channel %u", channel);
576 return -EIO;
577 }
578
579 return 0;
580 }
581
cc1200_set_txpower(const struct device * dev,int16_t dbm)582 static int cc1200_set_txpower(const struct device *dev, int16_t dbm)
583 {
584 uint8_t pa_power_ramp;
585
586 LOG_DBG("%d dbm", dbm);
587
588 /* See Section 7.1 */
589 dbm = ((dbm + 18) * 2) - 1;
590 if ((dbm <= 3) || (dbm >= 64)) {
591 LOG_ERR("Unhandled value");
592 return -EINVAL;
593 }
594
595 pa_power_ramp = read_reg_pa_cfg1(dev) & ~PA_POWER_RAMP_MASK;
596 pa_power_ramp |= ((uint8_t) dbm) & PA_POWER_RAMP_MASK;
597
598 if (!write_reg_pa_cfg1(dev, pa_power_ramp)) {
599 LOG_ERR("Could not proceed");
600 return -EIO;
601 }
602
603 return 0;
604 }
605
cc1200_tx(const struct device * dev,enum ieee802154_tx_mode mode,struct net_pkt * pkt,struct net_buf * frag)606 static int cc1200_tx(const struct device *dev,
607 enum ieee802154_tx_mode mode,
608 struct net_pkt *pkt,
609 struct net_buf *frag)
610 {
611 struct cc1200_context *cc1200 = dev->data;
612 uint8_t *frame = frag->data;
613 uint8_t len = frag->len;
614 bool status = false;
615
616 if (mode != IEEE802154_TX_MODE_DIRECT) {
617 NET_ERR("TX mode %d not supported", mode);
618 return -ENOTSUP;
619 }
620
621 LOG_DBG("%p (%u)", frag, len);
622
623 /* ToDo:
624 * Supporting 802.15.4g will require to loop in pkt's frags
625 * depending on len value, this will also take more time.
626 */
627
628 if (!instruct_sidle(dev) ||
629 !instruct_sfrx(dev) ||
630 !instruct_sftx(dev) ||
631 !instruct_sfstxon(dev)) {
632 LOG_ERR("Cannot switch to TX mode");
633 goto out;
634 }
635
636 if (!write_txfifo(dev, &len, CC1200_PHY_HDR_LEN) ||
637 !write_txfifo(dev, frame, len) ||
638 read_reg_num_txbytes(dev) != (len + CC1200_PHY_HDR_LEN)) {
639 LOG_ERR("Cannot fill-in TX fifo");
640 goto out;
641 }
642
643 atomic_set(&cc1200->tx, 1);
644 atomic_set(&cc1200->tx_start, 0);
645
646 if (!instruct_stx(dev)) {
647 LOG_ERR("Cannot start transmission");
648 goto out;
649 }
650
651 /* Wait for SYNC to be sent */
652 k_sem_take(&cc1200->tx_sync, K_MSEC(100));
653 if (atomic_get(&cc1200->tx_start) == 1) {
654 /* Now wait for the packet to be fully sent */
655 k_sem_take(&cc1200->tx_sync, K_MSEC(100));
656 }
657
658 out:
659 cc1200_print_status(get_status(dev));
660
661 if (atomic_get(&cc1200->tx) == 1 &&
662 read_reg_num_txbytes(dev) != 0) {
663 LOG_ERR("TX Failed");
664
665 atomic_set(&cc1200->tx_start, 0);
666 instruct_sftx(dev);
667 status = false;
668 } else {
669 status = true;
670 }
671
672 atomic_set(&cc1200->tx, 0);
673
674 /* Get back to RX */
675 instruct_srx(dev);
676
677 return status ? 0 : -EIO;
678 }
679
cc1200_start(const struct device * dev)680 static int cc1200_start(const struct device *dev)
681 {
682 if (!instruct_sidle(dev) ||
683 !instruct_sftx(dev) ||
684 !instruct_sfrx(dev) ||
685 rf_calibrate(dev)) {
686 LOG_ERR("Could not proceed");
687 return -EIO;
688 }
689
690 enable_gpio0_interrupt(dev, true);
691
692 cc1200_print_status(get_status(dev));
693
694 return 0;
695 }
696
cc1200_stop(const struct device * dev)697 static int cc1200_stop(const struct device *dev)
698 {
699 enable_gpio0_interrupt(dev, false);
700
701 if (!instruct_spwd(dev)) {
702 LOG_ERR("Could not proceed");
703 return -EIO;
704 }
705
706 return 0;
707 }
708
709 /* driver-allocated attribute memory - constant across all driver instances as
710 * this driver's channel range is configured via a global KConfig setting.
711 */
712 IEEE802154_DEFINE_PHY_SUPPORTED_CHANNELS(drv_attr, 0, IEEE802154_CC1200_CHANNEL_LIMIT);
713
cc1200_attr_get(const struct device * dev,enum ieee802154_attr attr,struct ieee802154_attr_value * value)714 static int cc1200_attr_get(const struct device *dev, enum ieee802154_attr attr,
715 struct ieee802154_attr_value *value)
716 {
717 ARG_UNUSED(dev);
718
719 return ieee802154_attr_get_channel_page_and_range(
720 attr, IEEE802154_ATTR_PHY_CHANNEL_PAGE_NINE_SUN_PREDEFINED,
721 &drv_attr.phy_supported_channels, value);
722 }
723
724 /******************
725 * Initialization *
726 *****************/
727
power_on_and_setup(const struct device * dev)728 static int power_on_and_setup(const struct device *dev)
729 {
730 if (!instruct_sres(dev)) {
731 LOG_ERR("Cannot reset");
732 return -EIO;
733 }
734
735 if (!rf_install_settings(dev, &cc1200_rf_settings)) {
736 return -EIO;
737 }
738
739 if (!write_reg_iocfg3(dev, CC1200_IOCFG3) ||
740 !write_reg_iocfg2(dev, CC1200_IOCFG2) ||
741 !write_reg_iocfg0(dev, CC1200_IOCFG0)) {
742 LOG_ERR("Cannot configure GPIOs");
743 return -EIO;
744 }
745
746 if (setup_gpio_callback(dev) != 0) {
747 return -EIO;
748 }
749
750 return rf_calibrate(dev);
751 }
752
cc1200_init(const struct device * dev)753 static int cc1200_init(const struct device *dev)
754 {
755 const struct cc1200_config *config = dev->config;
756 struct cc1200_context *cc1200 = dev->data;
757
758 atomic_set(&cc1200->tx, 0);
759 atomic_set(&cc1200->tx_start, 0);
760 atomic_set(&cc1200->rx, 0);
761 k_sem_init(&cc1200->rx_lock, 0, 1);
762 k_sem_init(&cc1200->tx_sync, 0, 1);
763
764 /* Configure GPIOs */
765 if (!gpio_is_ready_dt(&config->interrupt)) {
766 LOG_ERR("GPIO port %s is not ready",
767 config->interrupt.port->name);
768 return -ENODEV;
769 }
770 gpio_pin_configure_dt(&config->interrupt, GPIO_INPUT);
771
772 if (!spi_is_ready_dt(&config->bus)) {
773 LOG_ERR("SPI bus %s is not ready", config->bus.bus->name);
774 return -ENODEV;
775 }
776
777 LOG_DBG("GPIO and SPI configured");
778 if (power_on_and_setup(dev) != 0) {
779 LOG_ERR("Configuring CC1200 failed");
780 return -EIO;
781 }
782
783 k_thread_create(&cc1200->rx_thread, cc1200->rx_stack,
784 CONFIG_IEEE802154_CC1200_RX_STACK_SIZE,
785 cc1200_rx,
786 (void *)dev, NULL, NULL, K_PRIO_COOP(2), 0, K_NO_WAIT);
787 k_thread_name_set(&cc1200->rx_thread, "cc1200_rx");
788
789 LOG_INF("CC1200 initialized");
790
791 return 0;
792 }
793
cc1200_iface_init(struct net_if * iface)794 static void cc1200_iface_init(struct net_if *iface)
795 {
796 const struct device *dev = net_if_get_device(iface);
797 struct cc1200_context *cc1200 = dev->data;
798 uint8_t *mac = get_mac(dev);
799
800 LOG_DBG("");
801
802 net_if_set_link_addr(iface, mac, 8, NET_LINK_IEEE802154);
803
804 cc1200->iface = iface;
805
806 ieee802154_init(iface);
807 }
808
809 static const struct cc1200_config cc1200_config = {
810 .bus = SPI_DT_SPEC_INST_GET(0, SPI_WORD_SET(8), 0),
811 .interrupt = GPIO_DT_SPEC_INST_GET(0, int_gpios)
812 };
813
814 static struct cc1200_context cc1200_context_data;
815
816 static const struct ieee802154_radio_api cc1200_radio_api = {
817 .iface_api.init = cc1200_iface_init,
818
819 .get_capabilities = cc1200_get_capabilities,
820 .cca = cc1200_cca,
821 .set_channel = cc1200_set_channel,
822 .set_txpower = cc1200_set_txpower,
823 .tx = cc1200_tx,
824 .start = cc1200_start,
825 .stop = cc1200_stop,
826 .attr_get = cc1200_attr_get,
827 };
828
829 NET_DEVICE_DT_INST_DEFINE(0, cc1200_init, NULL, &cc1200_context_data,
830 &cc1200_config, CONFIG_IEEE802154_CC1200_INIT_PRIO,
831 &cc1200_radio_api, IEEE802154_L2,
832 NET_L2_GET_CTX_TYPE(IEEE802154_L2), 125);
833