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 * arg)449 static void cc1200_rx(void *arg)
450 {
451 const struct device *dev = arg;
452 struct cc1200_context *cc1200 = dev->data;
453 struct net_pkt *pkt;
454 uint8_t pkt_len;
455
456 while (1) {
457 pkt = NULL;
458
459 k_sem_take(&cc1200->rx_lock, K_FOREVER);
460
461 if (get_status(dev) == CC1200_STATUS_RX_FIFO_ERROR) {
462 LOG_ERR("Fifo error");
463 goto flush;
464 }
465
466 pkt_len = get_packet_length(dev);
467 if (!verify_rxfifo_validity(dev, pkt_len)) {
468 LOG_ERR("Invalid frame");
469 goto flush;
470 }
471
472 pkt = net_pkt_rx_alloc_with_buffer(cc1200->iface, pkt_len,
473 AF_UNSPEC, 0, K_NO_WAIT);
474 if (!pkt) {
475 LOG_ERR("No free pkt available");
476 goto flush;
477 }
478
479 if (!read_rxfifo_content(dev, pkt->buffer, pkt_len)) {
480 LOG_ERR("No content read");
481 goto flush;
482 }
483
484 if (!verify_crc(dev, pkt)) {
485 LOG_ERR("Bad packet CRC");
486 goto out;
487 }
488
489 if (ieee802154_handle_ack(cc1200->iface, pkt) == NET_OK) {
490 LOG_DBG("ACK packet handled");
491 goto out;
492 }
493
494 LOG_DBG("Caught a packet (%u)", pkt_len);
495
496 if (net_recv_data(cc1200->iface, pkt) < 0) {
497 LOG_DBG("Packet dropped by NET stack");
498 goto out;
499 }
500
501 log_stack_usage(&cc1200->rx_thread);
502 continue;
503 flush:
504 LOG_DBG("Flushing RX");
505 instruct_sidle(dev);
506 instruct_sfrx(dev);
507 instruct_srx(dev);
508 out:
509 if (pkt) {
510 net_pkt_unref(pkt);
511 }
512
513 }
514 }
515
516
517 /********************
518 * Radio device API *
519 *******************/
cc1200_get_capabilities(const struct device * dev)520 static enum ieee802154_hw_caps cc1200_get_capabilities(const struct device *dev)
521 {
522 return IEEE802154_HW_FCS;
523 }
524
cc1200_cca(const struct device * dev)525 static int cc1200_cca(const struct device *dev)
526 {
527 struct cc1200_context *cc1200 = dev->data;
528
529 if (atomic_get(&cc1200->rx) == 0) {
530 uint8_t status = read_reg_rssi0(dev);
531
532 if (!(status & CARRIER_SENSE) &&
533 (status & CARRIER_SENSE_VALID)) {
534 return 0;
535 }
536 }
537
538 LOG_WRN("Busy");
539
540 return -EBUSY;
541 }
542
cc1200_set_channel(const struct device * dev,uint16_t channel)543 static int cc1200_set_channel(const struct device *dev, uint16_t channel)
544 {
545 struct cc1200_context *cc1200 = dev->data;
546 uint32_t freq;
547
548 /* As SUN FSK provides a host of configurations with extremely different
549 * channel counts it doesn't make sense to validate (aka -EINVAL) a
550 * global upper limit on the number of supported channels on this page.
551 */
552 if (channel > IEEE802154_CC1200_CHANNEL_LIMIT) {
553 return -ENOTSUP;
554 }
555
556 /* Unlike usual 15.4 chips, cc1200 is closer to a bare metal radio modem
557 * and thus does not provide any means to select a channel directly, but
558 * requires instead that one calculates and configures the actual
559 * targeted frequency for the requested channel.
560 *
561 * See rf_evaluate_freq_setting() above.
562 */
563
564 if (atomic_get(&cc1200->rx) != 0) {
565 return -EIO;
566 }
567
568 freq = rf_evaluate_freq_setting(dev, channel);
569
570 if (!write_reg_freq(dev, freq) ||
571 rf_calibrate(dev)) {
572 LOG_ERR("Could not set channel %u", channel);
573 return -EIO;
574 }
575
576 return 0;
577 }
578
cc1200_set_txpower(const struct device * dev,int16_t dbm)579 static int cc1200_set_txpower(const struct device *dev, int16_t dbm)
580 {
581 uint8_t pa_power_ramp;
582
583 LOG_DBG("%d dbm", dbm);
584
585 /* See Section 7.1 */
586 dbm = ((dbm + 18) * 2) - 1;
587 if ((dbm <= 3) || (dbm >= 64)) {
588 LOG_ERR("Unhandled value");
589 return -EINVAL;
590 }
591
592 pa_power_ramp = read_reg_pa_cfg1(dev) & ~PA_POWER_RAMP_MASK;
593 pa_power_ramp |= ((uint8_t) dbm) & PA_POWER_RAMP_MASK;
594
595 if (!write_reg_pa_cfg1(dev, pa_power_ramp)) {
596 LOG_ERR("Could not proceed");
597 return -EIO;
598 }
599
600 return 0;
601 }
602
cc1200_tx(const struct device * dev,enum ieee802154_tx_mode mode,struct net_pkt * pkt,struct net_buf * frag)603 static int cc1200_tx(const struct device *dev,
604 enum ieee802154_tx_mode mode,
605 struct net_pkt *pkt,
606 struct net_buf *frag)
607 {
608 struct cc1200_context *cc1200 = dev->data;
609 uint8_t *frame = frag->data;
610 uint8_t len = frag->len;
611 bool status = false;
612
613 if (mode != IEEE802154_TX_MODE_DIRECT) {
614 NET_ERR("TX mode %d not supported", mode);
615 return -ENOTSUP;
616 }
617
618 LOG_DBG("%p (%u)", frag, len);
619
620 /* ToDo:
621 * Supporting 802.15.4g will require to loop in pkt's frags
622 * depending on len value, this will also take more time.
623 */
624
625 if (!instruct_sidle(dev) ||
626 !instruct_sfrx(dev) ||
627 !instruct_sftx(dev) ||
628 !instruct_sfstxon(dev)) {
629 LOG_ERR("Cannot switch to TX mode");
630 goto out;
631 }
632
633 if (!write_txfifo(dev, &len, CC1200_PHY_HDR_LEN) ||
634 !write_txfifo(dev, frame, len) ||
635 read_reg_num_txbytes(dev) != (len + CC1200_PHY_HDR_LEN)) {
636 LOG_ERR("Cannot fill-in TX fifo");
637 goto out;
638 }
639
640 atomic_set(&cc1200->tx, 1);
641 atomic_set(&cc1200->tx_start, 0);
642
643 if (!instruct_stx(dev)) {
644 LOG_ERR("Cannot start transmission");
645 goto out;
646 }
647
648 /* Wait for SYNC to be sent */
649 k_sem_take(&cc1200->tx_sync, K_MSEC(100));
650 if (atomic_get(&cc1200->tx_start) == 1) {
651 /* Now wait for the packet to be fully sent */
652 k_sem_take(&cc1200->tx_sync, K_MSEC(100));
653 }
654
655 out:
656 cc1200_print_status(get_status(dev));
657
658 if (atomic_get(&cc1200->tx) == 1 &&
659 read_reg_num_txbytes(dev) != 0) {
660 LOG_ERR("TX Failed");
661
662 atomic_set(&cc1200->tx_start, 0);
663 instruct_sftx(dev);
664 status = false;
665 } else {
666 status = true;
667 }
668
669 atomic_set(&cc1200->tx, 0);
670
671 /* Get back to RX */
672 instruct_srx(dev);
673
674 return status ? 0 : -EIO;
675 }
676
cc1200_start(const struct device * dev)677 static int cc1200_start(const struct device *dev)
678 {
679 if (!instruct_sidle(dev) ||
680 !instruct_sftx(dev) ||
681 !instruct_sfrx(dev) ||
682 rf_calibrate(dev)) {
683 LOG_ERR("Could not proceed");
684 return -EIO;
685 }
686
687 enable_gpio0_interrupt(dev, true);
688
689 cc1200_print_status(get_status(dev));
690
691 return 0;
692 }
693
cc1200_stop(const struct device * dev)694 static int cc1200_stop(const struct device *dev)
695 {
696 enable_gpio0_interrupt(dev, false);
697
698 if (!instruct_spwd(dev)) {
699 LOG_ERR("Could not proceed");
700 return -EIO;
701 }
702
703 return 0;
704 }
705
706 /* driver-allocated attribute memory - constant across all driver instances as
707 * this driver's channel range is configured via a global KConfig setting.
708 */
709 IEEE802154_DEFINE_PHY_SUPPORTED_CHANNELS(drv_attr, 0, IEEE802154_CC1200_CHANNEL_LIMIT);
710
cc1200_attr_get(const struct device * dev,enum ieee802154_attr attr,struct ieee802154_attr_value * value)711 static int cc1200_attr_get(const struct device *dev, enum ieee802154_attr attr,
712 struct ieee802154_attr_value *value)
713 {
714 ARG_UNUSED(dev);
715
716 return ieee802154_attr_get_channel_page_and_range(
717 attr, IEEE802154_ATTR_PHY_CHANNEL_PAGE_NINE_SUN_PREDEFINED,
718 &drv_attr.phy_supported_channels, value);
719 }
720
721 /******************
722 * Initialization *
723 *****************/
724
power_on_and_setup(const struct device * dev)725 static int power_on_and_setup(const struct device *dev)
726 {
727 if (!instruct_sres(dev)) {
728 LOG_ERR("Cannot reset");
729 return -EIO;
730 }
731
732 if (!rf_install_settings(dev, &cc1200_rf_settings)) {
733 return -EIO;
734 }
735
736 if (!write_reg_iocfg3(dev, CC1200_IOCFG3) ||
737 !write_reg_iocfg2(dev, CC1200_IOCFG2) ||
738 !write_reg_iocfg0(dev, CC1200_IOCFG0)) {
739 LOG_ERR("Cannot configure GPIOs");
740 return -EIO;
741 }
742
743 if (setup_gpio_callback(dev) != 0) {
744 return -EIO;
745 }
746
747 return rf_calibrate(dev);
748 }
749
cc1200_init(const struct device * dev)750 static int cc1200_init(const struct device *dev)
751 {
752 const struct cc1200_config *config = dev->config;
753 struct cc1200_context *cc1200 = dev->data;
754
755 atomic_set(&cc1200->tx, 0);
756 atomic_set(&cc1200->tx_start, 0);
757 atomic_set(&cc1200->rx, 0);
758 k_sem_init(&cc1200->rx_lock, 0, 1);
759 k_sem_init(&cc1200->tx_sync, 0, 1);
760
761 /* Configure GPIOs */
762 if (!gpio_is_ready_dt(&config->interrupt)) {
763 LOG_ERR("GPIO port %s is not ready",
764 config->interrupt.port->name);
765 return -ENODEV;
766 }
767 gpio_pin_configure_dt(&config->interrupt, GPIO_INPUT);
768
769 if (!spi_is_ready_dt(&config->bus)) {
770 LOG_ERR("SPI bus %s is not ready", config->bus.bus->name);
771 return -ENODEV;
772 }
773
774 LOG_DBG("GPIO and SPI configured");
775 if (power_on_and_setup(dev) != 0) {
776 LOG_ERR("Configuring CC1200 failed");
777 return -EIO;
778 }
779
780 k_thread_create(&cc1200->rx_thread, cc1200->rx_stack,
781 CONFIG_IEEE802154_CC1200_RX_STACK_SIZE,
782 (k_thread_entry_t)cc1200_rx,
783 (void *)dev, NULL, NULL, K_PRIO_COOP(2), 0, K_NO_WAIT);
784 k_thread_name_set(&cc1200->rx_thread, "cc1200_rx");
785
786 LOG_INF("CC1200 initialized");
787
788 return 0;
789 }
790
cc1200_iface_init(struct net_if * iface)791 static void cc1200_iface_init(struct net_if *iface)
792 {
793 const struct device *dev = net_if_get_device(iface);
794 struct cc1200_context *cc1200 = dev->data;
795 uint8_t *mac = get_mac(dev);
796
797 LOG_DBG("");
798
799 net_if_set_link_addr(iface, mac, 8, NET_LINK_IEEE802154);
800
801 cc1200->iface = iface;
802
803 ieee802154_init(iface);
804 }
805
806 static const struct cc1200_config cc1200_config = {
807 .bus = SPI_DT_SPEC_INST_GET(0, SPI_WORD_SET(8), 0),
808 .interrupt = GPIO_DT_SPEC_INST_GET(0, int_gpios)
809 };
810
811 static struct cc1200_context cc1200_context_data;
812
813 static struct ieee802154_radio_api cc1200_radio_api = {
814 .iface_api.init = cc1200_iface_init,
815
816 .get_capabilities = cc1200_get_capabilities,
817 .cca = cc1200_cca,
818 .set_channel = cc1200_set_channel,
819 .set_txpower = cc1200_set_txpower,
820 .tx = cc1200_tx,
821 .start = cc1200_start,
822 .stop = cc1200_stop,
823 .attr_get = cc1200_attr_get,
824 };
825
826 NET_DEVICE_DT_INST_DEFINE(0, cc1200_init, NULL, &cc1200_context_data,
827 &cc1200_config, CONFIG_IEEE802154_CC1200_INIT_PRIO,
828 &cc1200_radio_api, IEEE802154_L2,
829 NET_L2_GET_CTX_TYPE(IEEE802154_L2), 125);
830