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