ieee802154: cc2529: convert to _dt_spec

Convert cc2529 driver to `spi_dt_spec` and `gpio_dt_spec`. Required a
whole driver conversion from passing around the driver data struct to
passing around the driver itself.

Signed-off-by: Jordan Yates <jordan.yates@data61.csiro.au>
This commit is contained in:
Jordan Yates 2021-08-06 21:54:22 +10:00 committed by Anas Nashif
commit fab00d086e
2 changed files with 244 additions and 353 deletions

View file

@ -59,10 +59,6 @@ LOG_MODULE_REGISTER(LOG_MODULE_NAME);
#define CC2520_TX_THRESHOLD (0x7F) #define CC2520_TX_THRESHOLD (0x7F)
#define CC2520_FCS_LENGTH (2) #define CC2520_FCS_LENGTH (2)
#if DT_INST_SPI_DEV_HAS_CS_GPIOS(0)
static struct spi_cs_control cs_ctrl;
#endif
/********* /*********
* DEBUG * * DEBUG *
********/ ********/
@ -72,16 +68,16 @@ static inline void cc2520_print_gpio_config(const struct device *dev)
struct cc2520_context *cc2520 = dev->data; struct cc2520_context *cc2520 = dev->data;
LOG_DBG("GPIOCTRL0/1/2/3/4/5 = 0x%x/0x%x/0x%x/0x%x/0x%x/0x%x", LOG_DBG("GPIOCTRL0/1/2/3/4/5 = 0x%x/0x%x/0x%x/0x%x/0x%x/0x%x",
read_reg_gpioctrl0(cc2520), read_reg_gpioctrl0(cc2520),
read_reg_gpioctrl1(cc2520), read_reg_gpioctrl1(cc2520),
read_reg_gpioctrl2(cc2520), read_reg_gpioctrl2(cc2520),
read_reg_gpioctrl3(cc2520), read_reg_gpioctrl3(cc2520),
read_reg_gpioctrl4(cc2520), read_reg_gpioctrl4(cc2520),
read_reg_gpioctrl5(cc2520)); read_reg_gpioctrl5(cc2520));
LOG_DBG("GPIOPOLARITY: 0x%x", LOG_DBG("GPIOPOLARITY: 0x%x",
read_reg_gpiopolarity(cc2520)); read_reg_gpiopolarity(cc2520));
LOG_DBG("GPIOCTRL: 0x%x", LOG_DBG("GPIOCTRL: 0x%x",
read_reg_gpioctrl(cc2520)); read_reg_gpioctrl(cc2520));
} }
static inline void cc2520_print_exceptions(struct cc2520_context *cc2520) static inline void cc2520_print_exceptions(struct cc2520_context *cc2520)
@ -205,9 +201,10 @@ static inline void cc2520_print_errors(struct cc2520_context *cc2520)
********************/ ********************/
#define z_usleep(usec) k_busy_wait(usec) #define z_usleep(usec) k_busy_wait(usec)
bool z_cc2520_access(struct cc2520_context *ctx, bool read, uint8_t ins, bool z_cc2520_access(const struct device *dev, bool read, uint8_t ins,
uint16_t addr, void *data, size_t length) uint16_t addr, void *data, size_t length)
{ {
const struct cc2520_config *cfg = dev->config;
uint8_t cmd_buf[2]; uint8_t cmd_buf[2];
struct spi_buf buf[2] = { struct spi_buf buf[2] = {
{ {
@ -243,32 +240,32 @@ bool z_cc2520_access(struct cc2520_context *ctx, bool read, uint8_t ins,
tx.count = 1; tx.count = 1;
return (spi_transceive(ctx->spi, &ctx->spi_cfg, &tx, &rx) == 0); return (spi_transceive_dt(&cfg->bus, &tx, &rx) == 0);
} }
tx.count = data ? 2 : 1; tx.count = data ? 2 : 1;
return (spi_write(ctx->spi, &ctx->spi_cfg, &tx) == 0); return (spi_write_dt(&cfg->bus, &tx) == 0);
} }
static inline uint8_t cc2520_status(struct cc2520_context *ctx) static inline uint8_t cc2520_status(const struct device *dev)
{ {
uint8_t status; uint8_t status;
if (z_cc2520_access(ctx, true, CC2520_INS_SNOP, 0, &status, 1)) { if (z_cc2520_access(dev, true, CC2520_INS_SNOP, 0, &status, 1)) {
return status; return status;
} }
return 0; return 0;
} }
static bool verify_osc_stabilization(struct cc2520_context *cc2520) static bool verify_osc_stabilization(const struct device *dev)
{ {
uint8_t timeout = 100U; uint8_t timeout = 100U;
uint8_t status; uint8_t status;
do { do {
status = cc2520_status(cc2520); status = cc2520_status(dev);
z_usleep(1); z_usleep(1);
timeout--; timeout--;
} while (!(status & CC2520_STATUS_XOSC_STABLE_N_RUNNING) && timeout); } while (!(status & CC2520_STATUS_XOSC_STABLE_N_RUNNING) && timeout);
@ -304,13 +301,11 @@ static inline uint8_t *get_mac(const struct device *dev)
static int cc2520_set_pan_id(const struct device *dev, uint16_t pan_id) static int cc2520_set_pan_id(const struct device *dev, uint16_t pan_id)
{ {
struct cc2520_context *cc2520 = dev->data;
LOG_DBG("0x%x", pan_id); LOG_DBG("0x%x", pan_id);
pan_id = sys_le16_to_cpu(pan_id); pan_id = sys_le16_to_cpu(pan_id);
if (!write_mem_pan_id(cc2520, (uint8_t *) &pan_id)) { if (!write_mem_pan_id(dev, (uint8_t *) &pan_id)) {
LOG_ERR("Failed"); LOG_ERR("Failed");
return -EIO; return -EIO;
} }
@ -321,13 +316,11 @@ static int cc2520_set_pan_id(const struct device *dev, uint16_t pan_id)
static int cc2520_set_short_addr(const struct device *dev, static int cc2520_set_short_addr(const struct device *dev,
uint16_t short_addr) uint16_t short_addr)
{ {
struct cc2520_context *cc2520 = dev->data;
LOG_DBG("0x%x", short_addr); LOG_DBG("0x%x", short_addr);
short_addr = sys_le16_to_cpu(short_addr); short_addr = sys_le16_to_cpu(short_addr);
if (!write_mem_short_addr(cc2520, (uint8_t *) &short_addr)) { if (!write_mem_short_addr(dev, (uint8_t *) &short_addr)) {
LOG_ERR("Failed"); LOG_ERR("Failed");
return -EIO; return -EIO;
} }
@ -338,16 +331,14 @@ static int cc2520_set_short_addr(const struct device *dev,
static int cc2520_set_ieee_addr(const struct device *dev, static int cc2520_set_ieee_addr(const struct device *dev,
const uint8_t *ieee_addr) const uint8_t *ieee_addr)
{ {
struct cc2520_context *cc2520 = dev->data; if (!write_mem_ext_addr(dev, (void *)ieee_addr)) {
if (!write_mem_ext_addr(cc2520, (void *)ieee_addr)) {
LOG_ERR("Failed"); LOG_ERR("Failed");
return -EIO; return -EIO;
} }
LOG_DBG("IEEE address %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x", LOG_DBG("IEEE address %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x",
ieee_addr[7], ieee_addr[6], ieee_addr[5], ieee_addr[4], ieee_addr[7], ieee_addr[6], ieee_addr[5], ieee_addr[4],
ieee_addr[3], ieee_addr[2], ieee_addr[1], ieee_addr[0]); ieee_addr[3], ieee_addr[2], ieee_addr[1], ieee_addr[0]);
return 0; return 0;
} }
@ -357,48 +348,37 @@ static int cc2520_set_ieee_addr(const struct device *dev,
*****************/ *****************/
static inline void set_reset(const struct device *dev, uint32_t value) static inline void set_reset(const struct device *dev, uint32_t value)
{ {
struct cc2520_context *cc2520 = dev->data; const struct cc2520_config *cfg = dev->config;
gpio_pin_set_raw(cc2520->gpios[CC2520_GPIO_IDX_RESET].dev, gpio_pin_set_raw(cfg->reset.port, cfg->reset.pin, value);
cc2520->gpios[CC2520_GPIO_IDX_RESET].pin, value);
} }
static inline void set_vreg_en(const struct device *dev, uint32_t value) static inline void set_vreg_en(const struct device *dev, uint32_t value)
{ {
struct cc2520_context *cc2520 = dev->data; const struct cc2520_config *cfg = dev->config;
gpio_pin_set_raw(cc2520->gpios[CC2520_GPIO_IDX_VREG_EN].dev, gpio_pin_set_raw(cfg->vreg_en.port, cfg->vreg_en.pin, value);
cc2520->gpios[CC2520_GPIO_IDX_VREG_EN].pin, value);
} }
static inline uint32_t get_fifo(struct cc2520_context *cc2520) static inline uint32_t get_fifo(const struct device *dev)
{ {
uint32_t pin_value; const struct cc2520_config *cfg = dev->config;
pin_value = gpio_pin_get_raw(cc2520->gpios[CC2520_GPIO_IDX_FIFO].dev, return gpio_pin_get_raw(cfg->fifo.port, cfg->fifo.pin);
cc2520->gpios[CC2520_GPIO_IDX_FIFO].pin);
return pin_value;
} }
static inline uint32_t get_fifop(struct cc2520_context *cc2520) static inline uint32_t get_fifop(const struct device *dev)
{ {
uint32_t pin_value; const struct cc2520_config *cfg = dev->config;
pin_value = gpio_pin_get_raw(cc2520->gpios[CC2520_GPIO_IDX_FIFOP].dev, return gpio_pin_get_raw(cfg->fifop.port, cfg->fifop.pin);
cc2520->gpios[CC2520_GPIO_IDX_FIFOP].pin);
return pin_value;
} }
static inline uint32_t get_cca(struct cc2520_context *cc2520) static inline uint32_t get_cca(const struct device *dev)
{ {
uint32_t pin_value; const struct cc2520_config *cfg = dev->config;
pin_value = gpio_pin_get_raw(cc2520->gpios[CC2520_GPIO_IDX_CCA].dev, return gpio_pin_get_raw(cfg->cca.port, cfg->cca.pin);
cc2520->gpios[CC2520_GPIO_IDX_CCA].pin);
return pin_value;
} }
static inline void sfd_int_handler(const struct device *port, static inline void sfd_int_handler(const struct device *port,
@ -420,79 +400,76 @@ static inline void fifop_int_handler(const struct device *port,
CONTAINER_OF(cb, struct cc2520_context, fifop_cb); CONTAINER_OF(cb, struct cc2520_context, fifop_cb);
/* Note: Errata document - 1.2 */ /* Note: Errata document - 1.2 */
if (!get_fifop(cc2520) && !get_fifop(cc2520)) { if (!get_fifop(cc2520->dev) && !get_fifop(cc2520->dev)) {
return; return;
} }
if (!get_fifo(cc2520)) { if (!get_fifo(cc2520->dev)) {
cc2520->overflow = true; cc2520->overflow = true;
} }
k_sem_give(&cc2520->rx_lock); k_sem_give(&cc2520->rx_lock);
} }
static void enable_fifop_interrupt(struct cc2520_context *cc2520, static void enable_fifop_interrupt(const struct device *dev,
bool enable) bool enable)
{ {
gpio_pin_interrupt_configure( const struct cc2520_config *cfg = dev->config;
cc2520->gpios[CC2520_GPIO_IDX_FIFOP].dev, gpio_flags_t mode = enable ? GPIO_INT_EDGE_TO_ACTIVE : GPIO_INT_DISABLE;
cc2520->gpios[CC2520_GPIO_IDX_FIFOP].pin,
enable ? GPIO_INT_EDGE_TO_ACTIVE : GPIO_INT_DISABLE); gpio_pin_interrupt_configure_dt(&cfg->fifop, mode);
} }
static void enable_sfd_interrupt(struct cc2520_context *cc2520, static void enable_sfd_interrupt(const struct device *dev,
bool enable) bool enable)
{ {
gpio_pin_interrupt_configure( const struct cc2520_config *cfg = dev->config;
cc2520->gpios[CC2520_GPIO_IDX_SFD].dev, gpio_flags_t mode = enable ? GPIO_INT_EDGE_TO_ACTIVE : GPIO_INT_DISABLE;
cc2520->gpios[CC2520_GPIO_IDX_SFD].pin,
enable ? GPIO_INT_EDGE_TO_ACTIVE : GPIO_INT_DISABLE); gpio_pin_interrupt_configure_dt(&cfg->sfd, mode);
} }
static inline void setup_gpio_callbacks(const struct device *dev) static inline void setup_gpio_callbacks(const struct device *dev)
{ {
const struct cc2520_config *cfg = dev->config;
struct cc2520_context *cc2520 = dev->data; struct cc2520_context *cc2520 = dev->data;
gpio_init_callback(&cc2520->sfd_cb, sfd_int_handler, gpio_init_callback(&cc2520->sfd_cb, sfd_int_handler, BIT(cfg->sfd.pin));
BIT(cc2520->gpios[CC2520_GPIO_IDX_SFD].pin)); gpio_add_callback(cfg->sfd.port, &cc2520->sfd_cb);
gpio_add_callback(cc2520->gpios[CC2520_GPIO_IDX_SFD].dev,
&cc2520->sfd_cb);
gpio_init_callback(&cc2520->fifop_cb, fifop_int_handler, gpio_init_callback(&cc2520->fifop_cb, fifop_int_handler, BIT(cfg->fifop.pin));
BIT(cc2520->gpios[CC2520_GPIO_IDX_FIFOP].pin)); gpio_add_callback(cfg->fifop.port, &cc2520->fifop_cb);
gpio_add_callback(cc2520->gpios[CC2520_GPIO_IDX_FIFOP].dev,
&cc2520->fifop_cb);
} }
/**************** /****************
* TX functions * * TX functions *
***************/ ***************/
static inline bool write_txfifo_length(struct cc2520_context *ctx, uint8_t len) static inline bool write_txfifo_length(const struct device *dev, uint8_t len)
{ {
uint8_t length = len + CC2520_FCS_LENGTH; uint8_t length = len + CC2520_FCS_LENGTH;
return z_cc2520_access(ctx, false, CC2520_INS_TXBUF, 0, &length, 1); return z_cc2520_access(dev, false, CC2520_INS_TXBUF, 0, &length, 1);
} }
static inline bool write_txfifo_content(struct cc2520_context *ctx, static inline bool write_txfifo_content(const struct device *dev,
uint8_t *frame, uint8_t len) uint8_t *frame, uint8_t len)
{ {
return z_cc2520_access(ctx, false, CC2520_INS_TXBUF, 0, frame, len); return z_cc2520_access(dev, false, CC2520_INS_TXBUF, 0, frame, len);
} }
static inline bool verify_txfifo_status(struct cc2520_context *cc2520, static inline bool verify_txfifo_status(const struct device *dev,
uint8_t len) uint8_t len)
{ {
if (read_reg_txfifocnt(cc2520) < len || if (read_reg_txfifocnt(dev) < len ||
(read_reg_excflag0(cc2520) & EXCFLAG0_TX_UNDERFLOW)) { (read_reg_excflag0(dev) & EXCFLAG0_TX_UNDERFLOW)) {
return false; return false;
} }
return true; return true;
} }
static inline bool verify_tx_done(struct cc2520_context *cc2520) static inline bool verify_tx_done(const struct device *dev)
{ {
uint8_t timeout = 10U; uint8_t timeout = 10U;
uint8_t status; uint8_t status;
@ -500,7 +477,7 @@ static inline bool verify_tx_done(struct cc2520_context *cc2520)
do { do {
z_usleep(1); z_usleep(1);
timeout--; timeout--;
status = read_reg_excflag0(cc2520); status = read_reg_excflag0(dev);
} while (!(status & EXCFLAG0_TX_FRM_DONE) && timeout); } while (!(status & EXCFLAG0_TX_FRM_DONE) && timeout);
return !!(status & EXCFLAG0_TX_FRM_DONE); return !!(status & EXCFLAG0_TX_FRM_DONE);
@ -510,39 +487,39 @@ static inline bool verify_tx_done(struct cc2520_context *cc2520)
* RX functions * * RX functions *
***************/ ***************/
static inline void flush_rxfifo(struct cc2520_context *cc2520) static inline void flush_rxfifo(const struct device *dev)
{ {
/* Note: Errata document - 1.1 */ /* Note: Errata document - 1.1 */
enable_fifop_interrupt(cc2520, false); enable_fifop_interrupt(dev, false);
instruct_sflushrx(cc2520); instruct_sflushrx(dev);
instruct_sflushrx(cc2520); instruct_sflushrx(dev);
enable_fifop_interrupt(cc2520, true); enable_fifop_interrupt(dev, true);
write_reg_excflag0(cc2520, EXCFLAG0_RESET_RX_FLAGS); write_reg_excflag0(dev, EXCFLAG0_RESET_RX_FLAGS);
} }
static inline uint8_t read_rxfifo_length(struct cc2520_context *ctx) static inline uint8_t read_rxfifo_length(const struct device *dev)
{ {
uint8_t len; uint8_t len;
if (z_cc2520_access(ctx, true, CC2520_INS_RXBUF, 0, &len, 1)) { if (z_cc2520_access(dev, true, CC2520_INS_RXBUF, 0, &len, 1)) {
return len; return len;
} }
return 0; return 0;
} }
static inline bool read_rxfifo_content(struct cc2520_context *ctx, static inline bool read_rxfifo_content(const struct device *dev,
struct net_buf *buf, uint8_t len) struct net_buf *buf, uint8_t len)
{ {
if (!z_cc2520_access(ctx, true, CC2520_INS_RXBUF, 0, buf->data, len)) { if (!z_cc2520_access(dev, true, CC2520_INS_RXBUF, 0, buf->data, len)) {
return false; return false;
} }
if (read_reg_excflag0(ctx) & EXCFLAG0_RX_UNDERFLOW) { if (read_reg_excflag0(dev) & EXCFLAG0_RX_UNDERFLOW) {
LOG_ERR("RX underflow!"); LOG_ERR("RX underflow!");
return false; return false;
} }
@ -580,11 +557,11 @@ static inline void insert_radio_noise_details(struct net_pkt *pkt, uint8_t *buf)
net_pkt_set_ieee802154_lqi(pkt, lqi); net_pkt_set_ieee802154_lqi(pkt, lqi);
} }
static inline bool verify_crc(struct cc2520_context *ctx, struct net_pkt *pkt) static inline bool verify_crc(const struct device *dev, struct net_pkt *pkt)
{ {
uint8_t fcs[2]; uint8_t fcs[2];
if (!z_cc2520_access(ctx, true, CC2520_INS_RXBUF, 0, &fcs, 2)) { if (!z_cc2520_access(dev, true, CC2520_INS_RXBUF, 0, &fcs, 2)) {
return false; return false;
} }
@ -597,10 +574,10 @@ static inline bool verify_crc(struct cc2520_context *ctx, struct net_pkt *pkt)
return true; return true;
} }
static inline bool verify_rxfifo_validity(struct cc2520_context *ctx, static inline bool verify_rxfifo_validity(const struct device *dev,
uint8_t pkt_len) uint8_t pkt_len)
{ {
if (pkt_len < 2 || read_reg_rxfifocnt(ctx) != pkt_len) { if (pkt_len < 2 || read_reg_rxfifocnt(dev) != pkt_len) {
return false; return false;
} }
@ -609,7 +586,8 @@ static inline bool verify_rxfifo_validity(struct cc2520_context *ctx,
static void cc2520_rx(void *arg) static void cc2520_rx(void *arg)
{ {
struct cc2520_context *cc2520 = arg; const struct device *dev = arg;
struct cc2520_context *cc2520 = dev->data;
struct net_pkt *pkt; struct net_pkt *pkt;
uint8_t pkt_len; uint8_t pkt_len;
@ -625,8 +603,8 @@ static void cc2520_rx(void *arg)
goto flush; goto flush;
} }
pkt_len = read_rxfifo_length(cc2520) & 0x7f; pkt_len = read_rxfifo_length(dev) & 0x7f;
if (!verify_rxfifo_validity(cc2520, pkt_len)) { if (!verify_rxfifo_validity(dev, pkt_len)) {
LOG_ERR("Invalid content"); LOG_ERR("Invalid content");
goto flush; goto flush;
} }
@ -642,12 +620,12 @@ static void cc2520_rx(void *arg)
pkt_len -= 2U; pkt_len -= 2U;
} }
if (!read_rxfifo_content(cc2520, pkt->buffer, pkt_len)) { if (!read_rxfifo_content(dev, pkt->buffer, pkt_len)) {
LOG_ERR("No content read"); LOG_ERR("No content read");
goto flush; goto flush;
} }
if (!verify_crc(cc2520, pkt)) { if (!verify_crc(dev, pkt)) {
LOG_ERR("Bad packet CRC"); LOG_ERR("Bad packet CRC");
goto out; goto out;
} }
@ -669,7 +647,7 @@ static void cc2520_rx(void *arg)
flush: flush:
cc2520_print_exceptions(cc2520); cc2520_print_exceptions(cc2520);
cc2520_print_errors(cc2520); cc2520_print_errors(cc2520);
flush_rxfifo(cc2520); flush_rxfifo(dev);
out: out:
if (pkt) { if (pkt) {
net_pkt_unref(pkt); net_pkt_unref(pkt);
@ -690,9 +668,7 @@ static enum ieee802154_hw_caps cc2520_get_capabilities(const struct device *dev)
static int cc2520_cca(const struct device *dev) static int cc2520_cca(const struct device *dev)
{ {
struct cc2520_context *cc2520 = dev->data; if (!get_cca(dev)) {
if (!get_cca(cc2520)) {
LOG_WRN("Busy"); LOG_WRN("Busy");
return -EBUSY; return -EBUSY;
} }
@ -702,8 +678,6 @@ static int cc2520_cca(const struct device *dev)
static int cc2520_set_channel(const struct device *dev, uint16_t channel) static int cc2520_set_channel(const struct device *dev, uint16_t channel)
{ {
struct cc2520_context *cc2520 = dev->data;
LOG_DBG("%u", channel); LOG_DBG("%u", channel);
if (channel < 11 || channel > 26) { if (channel < 11 || channel > 26) {
@ -713,7 +687,7 @@ static int cc2520_set_channel(const struct device *dev, uint16_t channel)
/* See chapter 16 */ /* See chapter 16 */
channel = 11 + (channel - 11) * 5U; channel = 11 + (channel - 11) * 5U;
if (!write_reg_freqctrl(cc2520, FREQCTRL_FREQ(channel))) { if (!write_reg_freqctrl(dev, FREQCTRL_FREQ(channel))) {
LOG_ERR("Failed"); LOG_ERR("Failed");
return -EIO; return -EIO;
} }
@ -745,7 +719,6 @@ static int cc2520_filter(const struct device *dev,
static int cc2520_set_txpower(const struct device *dev, int16_t dbm) static int cc2520_set_txpower(const struct device *dev, int16_t dbm)
{ {
struct cc2520_context *cc2520 = dev->data;
uint8_t pwr; uint8_t pwr;
LOG_DBG("%d", dbm); LOG_DBG("%d", dbm);
@ -783,7 +756,7 @@ static int cc2520_set_txpower(const struct device *dev, int16_t dbm)
goto error; goto error;
} }
if (!write_reg_txpower(cc2520, pwr)) { if (!write_reg_txpower(dev, pwr)) {
goto error; goto error;
} }
@ -811,14 +784,14 @@ static int cc2520_tx(const struct device *dev,
LOG_DBG("%p (%u)", frag, len); LOG_DBG("%p (%u)", frag, len);
if (!write_reg_excflag0(cc2520, EXCFLAG0_RESET_TX_FLAGS) || if (!write_reg_excflag0(dev, EXCFLAG0_RESET_TX_FLAGS) ||
!write_txfifo_length(cc2520, len) || !write_txfifo_length(dev, len) ||
!write_txfifo_content(cc2520, frame, len)) { !write_txfifo_content(dev, frame, len)) {
LOG_ERR("Cannot feed in TX fifo"); LOG_ERR("Cannot feed in TX fifo");
goto error; goto error;
} }
if (!verify_txfifo_status(cc2520, len)) { if (!verify_txfifo_status(dev, len)) {
LOG_ERR("Did not write properly into TX FIFO"); LOG_ERR("Did not write properly into TX FIFO");
goto error; goto error;
} }
@ -832,7 +805,7 @@ static int cc2520_tx(const struct device *dev,
atomic_set(&cc2520->tx, 1); atomic_set(&cc2520->tx, 1);
k_sem_init(&cc2520->tx_sync, 0, K_SEM_MAX_LIMIT); k_sem_init(&cc2520->tx_sync, 0, K_SEM_MAX_LIMIT);
if (!instruct_stxoncca(cc2520)) { if (!instruct_stxoncca(dev)) {
LOG_ERR("Cannot start transmission"); LOG_ERR("Cannot start transmission");
goto error; goto error;
} }
@ -840,7 +813,7 @@ static int cc2520_tx(const struct device *dev,
k_sem_take(&cc2520->tx_sync, K_MSEC(10)); k_sem_take(&cc2520->tx_sync, K_MSEC(10));
retry--; retry--;
status = verify_tx_done(cc2520); status = verify_tx_done(dev);
} while (!status && retry); } while (!status && retry);
#ifdef CONFIG_IEEE802154_CC2520_CRYPTO #ifdef CONFIG_IEEE802154_CC2520_CRYPTO
@ -860,41 +833,37 @@ error:
cc2520_print_errors(cc2520); cc2520_print_errors(cc2520);
atomic_set(&cc2520->tx, 0); atomic_set(&cc2520->tx, 0);
instruct_sflushtx(cc2520); instruct_sflushtx(dev);
return -EIO; return -EIO;
} }
static int cc2520_start(const struct device *dev) static int cc2520_start(const struct device *dev)
{ {
struct cc2520_context *cc2520 = dev->data; if (!instruct_sxoscon(dev) ||
!instruct_srxon(dev) ||
if (!instruct_sxoscon(cc2520) || !verify_osc_stabilization(dev)) {
!instruct_srxon(cc2520) ||
!verify_osc_stabilization(cc2520)) {
LOG_ERR("Error starting CC2520"); LOG_ERR("Error starting CC2520");
return -EIO; return -EIO;
} }
flush_rxfifo(cc2520); flush_rxfifo(dev);
enable_fifop_interrupt(cc2520, true); enable_fifop_interrupt(dev, true);
enable_sfd_interrupt(cc2520, true); enable_sfd_interrupt(dev, true);
return 0; return 0;
} }
static int cc2520_stop(const struct device *dev) static int cc2520_stop(const struct device *dev)
{ {
struct cc2520_context *cc2520 = dev->data; flush_rxfifo(dev);
flush_rxfifo(cc2520); enable_fifop_interrupt(dev, false);
enable_sfd_interrupt(dev, false);
enable_fifop_interrupt(cc2520, false); if (!instruct_srfoff(dev) ||
enable_sfd_interrupt(cc2520, false); !instruct_sxoscoff(dev)) {
if (!instruct_srfoff(cc2520) ||
!instruct_sxoscoff(cc2520)) {
LOG_ERR("Error stopping CC2520"); LOG_ERR("Error stopping CC2520");
return -EIO; return -EIO;
} }
@ -907,8 +876,6 @@ static int cc2520_stop(const struct device *dev)
*****************/ *****************/
static int power_on_and_setup(const struct device *dev) static int power_on_and_setup(const struct device *dev)
{ {
struct cc2520_context *cc2520 = dev->data;
/* Switching to LPM2 mode */ /* Switching to LPM2 mode */
set_reset(dev, 0); set_reset(dev, 0);
z_usleep(150); z_usleep(150);
@ -923,22 +890,22 @@ static int power_on_and_setup(const struct device *dev)
set_reset(dev, 1); set_reset(dev, 1);
z_usleep(150); z_usleep(150);
if (!verify_osc_stabilization(cc2520)) { if (!verify_osc_stabilization(dev)) {
return -EIO; return -EIO;
} }
/* Default settings to always write (see chapter 28 part 1) */ /* Default settings to always write (see chapter 28 part 1) */
if (!write_reg_txpower(cc2520, CC2520_TXPOWER_DEFAULT) || if (!write_reg_txpower(dev, CC2520_TXPOWER_DEFAULT) ||
!write_reg_ccactrl0(cc2520, CC2520_CCACTRL0_DEFAULT) || !write_reg_ccactrl0(dev, CC2520_CCACTRL0_DEFAULT) ||
!write_reg_mdmctrl0(cc2520, CC2520_MDMCTRL0_DEFAULT) || !write_reg_mdmctrl0(dev, CC2520_MDMCTRL0_DEFAULT) ||
!write_reg_mdmctrl1(cc2520, CC2520_MDMCTRL1_DEFAULT) || !write_reg_mdmctrl1(dev, CC2520_MDMCTRL1_DEFAULT) ||
!write_reg_rxctrl(cc2520, CC2520_RXCTRL_DEFAULT) || !write_reg_rxctrl(dev, CC2520_RXCTRL_DEFAULT) ||
!write_reg_fsctrl(cc2520, CC2520_FSCTRL_DEFAULT) || !write_reg_fsctrl(dev, CC2520_FSCTRL_DEFAULT) ||
!write_reg_fscal1(cc2520, CC2520_FSCAL1_DEFAULT) || !write_reg_fscal1(dev, CC2520_FSCAL1_DEFAULT) ||
!write_reg_agcctrl1(cc2520, CC2520_AGCCTRL1_DEFAULT) || !write_reg_agcctrl1(dev, CC2520_AGCCTRL1_DEFAULT) ||
!write_reg_adctest0(cc2520, CC2520_ADCTEST0_DEFAULT) || !write_reg_adctest0(dev, CC2520_ADCTEST0_DEFAULT) ||
!write_reg_adctest1(cc2520, CC2520_ADCTEST1_DEFAULT) || !write_reg_adctest1(dev, CC2520_ADCTEST1_DEFAULT) ||
!write_reg_adctest2(cc2520, CC2520_ADCTEST2_DEFAULT)) { !write_reg_adctest2(dev, CC2520_ADCTEST2_DEFAULT)) {
return -EIO; return -EIO;
} }
@ -948,21 +915,21 @@ static int power_on_and_setup(const struct device *dev)
* FRMFILT0: Frame filtering (setting CC2520_FRAME_FILTERING) * FRMFILT0: Frame filtering (setting CC2520_FRAME_FILTERING)
* FIFOPCTRL: Set TX threshold (setting CC2520_TX_THRESHOLD) * FIFOPCTRL: Set TX threshold (setting CC2520_TX_THRESHOLD)
*/ */
if (!write_reg_extclock(cc2520, 0) || if (!write_reg_extclock(dev, 0) ||
!write_reg_frmctrl0(cc2520, CC2520_AUTOMATISM) || !write_reg_frmctrl0(dev, CC2520_AUTOMATISM) ||
!write_reg_frmctrl1(cc2520, FRMCTRL1_IGNORE_TX_UNDERF | !write_reg_frmctrl1(dev, FRMCTRL1_IGNORE_TX_UNDERF |
FRMCTRL1_SET_RXENMASK_ON_TX) || FRMCTRL1_SET_RXENMASK_ON_TX) ||
!write_reg_frmfilt0(cc2520, FRMFILT0_FRAME_FILTER_EN | !write_reg_frmfilt0(dev, FRMFILT0_FRAME_FILTER_EN |
FRMFILT0_MAX_FRAME_VERSION(3)) || FRMFILT0_MAX_FRAME_VERSION(3)) ||
!write_reg_frmfilt1(cc2520, FRMFILT1_ACCEPT_ALL) || !write_reg_frmfilt1(dev, FRMFILT1_ACCEPT_ALL) ||
!write_reg_srcmatch(cc2520, SRCMATCH_DEFAULTS) || !write_reg_srcmatch(dev, SRCMATCH_DEFAULTS) ||
!write_reg_fifopctrl(cc2520, !write_reg_fifopctrl(dev,
FIFOPCTRL_FIFOP_THR(CC2520_TX_THRESHOLD))) { FIFOPCTRL_FIFOP_THR(CC2520_TX_THRESHOLD))) {
return -EIO; return -EIO;
} }
/* Cleaning up TX fifo */ /* Cleaning up TX fifo */
instruct_sflushtx(cc2520); instruct_sflushtx(dev);
setup_gpio_callbacks(dev); setup_gpio_callbacks(dev);
@ -971,121 +938,36 @@ static int power_on_and_setup(const struct device *dev)
return 0; return 0;
} }
static struct cc2520_gpio_configuration *configure_gpios(const struct device *dev) static int configure_gpios(const struct device *dev)
{ {
struct cc2520_context *cc2520 = dev->data; const struct cc2520_config *cfg = dev->config;
const struct device *gpio;
/* VREG_EN */ if (!device_is_ready(cfg->vreg_en.port) ||
gpio = device_get_binding(DT_INST_GPIO_LABEL(0, vreg_en_gpios)); !device_is_ready(cfg->reset.port) ||
if (!gpio) { !device_is_ready(cfg->fifo.port) ||
return NULL; !device_is_ready(cfg->cca.port) ||
} !device_is_ready(cfg->sfd.port) ||
!device_is_ready(cfg->fifop.port)) {
cc2520->gpios[CC2520_GPIO_IDX_VREG_EN].pin = DT_INST_GPIO_PIN(0, vreg_en_gpios);
gpio_pin_configure(gpio, cc2520->gpios[CC2520_GPIO_IDX_VREG_EN].pin,
GPIO_OUTPUT_LOW | DT_INST_GPIO_FLAGS(0, vreg_en_gpios));
cc2520->gpios[CC2520_GPIO_IDX_VREG_EN].dev = gpio;
/* RESET */
gpio = device_get_binding(DT_INST_GPIO_LABEL(0, reset_gpios));
if (!gpio) {
return NULL;
}
cc2520->gpios[CC2520_GPIO_IDX_RESET].pin = DT_INST_GPIO_PIN(0, reset_gpios);
gpio_pin_configure(gpio, cc2520->gpios[CC2520_GPIO_IDX_RESET].pin,
GPIO_OUTPUT_LOW | DT_INST_GPIO_FLAGS(0, reset_gpios));
cc2520->gpios[CC2520_GPIO_IDX_RESET].dev = gpio;
/*FIFO */
gpio = device_get_binding(DT_INST_GPIO_LABEL(0, fifo_gpios));
if (!gpio) {
return NULL;
}
cc2520->gpios[CC2520_GPIO_IDX_FIFO].pin = DT_INST_GPIO_PIN(0, fifo_gpios);
gpio_pin_configure(gpio, cc2520->gpios[CC2520_GPIO_IDX_FIFO].pin,
GPIO_INPUT | DT_INST_GPIO_FLAGS(0, fifo_gpios));
cc2520->gpios[CC2520_GPIO_IDX_FIFO].dev = gpio;
/* CCA */
gpio = device_get_binding(DT_INST_GPIO_LABEL(0, cca_gpios));
if (!gpio) {
return NULL;
}
cc2520->gpios[CC2520_GPIO_IDX_CCA].pin = DT_INST_GPIO_PIN(0, cca_gpios);
gpio_pin_configure(gpio, cc2520->gpios[CC2520_GPIO_IDX_CCA].pin,
GPIO_INPUT | DT_INST_GPIO_FLAGS(0, cca_gpios));
cc2520->gpios[CC2520_GPIO_IDX_CCA].dev = gpio;
/* SFD */
gpio = device_get_binding(DT_INST_GPIO_LABEL(0, sfd_gpios));
if (!gpio) {
return NULL;
}
cc2520->gpios[CC2520_GPIO_IDX_SFD].pin = DT_INST_GPIO_PIN(0, sfd_gpios);
gpio_pin_configure(gpio, cc2520->gpios[CC2520_GPIO_IDX_SFD].pin,
GPIO_INPUT | DT_INST_GPIO_FLAGS(0, sfd_gpios));
cc2520->gpios[CC2520_GPIO_IDX_SFD].dev = gpio;
/* FIFOP */
gpio = device_get_binding(DT_INST_GPIO_LABEL(0, fifop_gpios));
if (!gpio) {
return NULL;
}
cc2520->gpios[CC2520_GPIO_IDX_FIFOP].pin = DT_INST_GPIO_PIN(0, fifop_gpios);
gpio_pin_configure(gpio, cc2520->gpios[CC2520_GPIO_IDX_FIFOP].pin,
GPIO_INPUT | DT_INST_GPIO_FLAGS(0, sfd_gpios));
cc2520->gpios[CC2520_GPIO_IDX_FIFOP].dev = gpio;
return cc2520->gpios;
}
static inline int configure_spi(const struct device *dev)
{
struct cc2520_context *cc2520 = dev->data;
cc2520->spi = device_get_binding(DT_INST_BUS_LABEL(0));
if (!cc2520->spi) {
LOG_ERR("Unable to get SPI device");
return -ENODEV; return -ENODEV;
} }
#if DT_INST_SPI_DEV_HAS_CS_GPIOS(0) gpio_pin_configure_dt(&cfg->vreg_en, GPIO_OUTPUT_LOW);
cs_ctrl.gpio_dev = device_get_binding( gpio_pin_configure_dt(&cfg->reset, GPIO_OUTPUT_LOW);
DT_INST_SPI_DEV_CS_GPIOS_LABEL(0)); gpio_pin_configure_dt(&cfg->fifo, GPIO_INPUT);
if (!cs_ctrl.gpio_dev) { gpio_pin_configure_dt(&cfg->cca, GPIO_INPUT);
LOG_ERR("Unable to get GPIO SPI CS device"); gpio_pin_configure_dt(&cfg->sfd, GPIO_INPUT);
return -ENODEV; gpio_pin_configure_dt(&cfg->fifop, GPIO_INPUT);
}
cs_ctrl.gpio_pin = DT_INST_SPI_DEV_CS_GPIOS_PIN(0);
cs_ctrl.gpio_dt_flags = DT_INST_SPI_DEV_CS_GPIOS_FLAGS(0);
cs_ctrl.delay = 0U;
cc2520->spi_cfg.cs = &cs_ctrl;
LOG_DBG("SPI GPIO CS configured on %s:%u",
DT_INST_SPI_DEV_CS_GPIOS_LABEL(0),
DT_INST_SPI_DEV_CS_GPIOS_PIN(0));
#endif
cc2520->spi_cfg.frequency = DT_INST_PROP(0, spi_max_frequency);
cc2520->spi_cfg.operation = SPI_WORD_SET(8);
cc2520->spi_cfg.slave = DT_INST_REG_ADDR(0);
return 0; return 0;
} }
static int cc2520_init(const struct device *dev) static int cc2520_init(const struct device *dev)
{ {
const struct cc2520_config *cfg = dev->config;
struct cc2520_context *cc2520 = dev->data; struct cc2520_context *cc2520 = dev->data;
cc2520->dev = dev;
atomic_set(&cc2520->tx, 0); atomic_set(&cc2520->tx, 0);
k_sem_init(&cc2520->rx_lock, 0, K_SEM_MAX_LIMIT); k_sem_init(&cc2520->rx_lock, 0, K_SEM_MAX_LIMIT);
@ -1093,13 +975,13 @@ static int cc2520_init(const struct device *dev)
k_sem_init(&cc2520->access_lock, 1, 1); k_sem_init(&cc2520->access_lock, 1, 1);
#endif #endif
if (!configure_gpios(dev)) { if (configure_gpios(dev) != 0) {
LOG_ERR("Configuring GPIOS failed"); LOG_ERR("Configuring GPIOS failed");
return -EIO; return -EIO;
} }
if (configure_spi(dev) != 0) { if (!spi_is_ready(&cfg->bus)) {
LOG_ERR("Configuring SPI failed"); LOG_ERR("SPI bus %s not ready", cfg->bus.bus->name);
return -EIO; return -EIO;
} }
@ -1113,7 +995,7 @@ static int cc2520_init(const struct device *dev)
k_thread_create(&cc2520->cc2520_rx_thread, cc2520->cc2520_rx_stack, k_thread_create(&cc2520->cc2520_rx_thread, cc2520->cc2520_rx_stack,
CONFIG_IEEE802154_CC2520_RX_STACK_SIZE, CONFIG_IEEE802154_CC2520_RX_STACK_SIZE,
(k_thread_entry_t)cc2520_rx, (k_thread_entry_t)cc2520_rx,
cc2520, NULL, NULL, K_PRIO_COOP(2), 0, K_NO_WAIT); (void *)dev, NULL, NULL, K_PRIO_COOP(2), 0, K_NO_WAIT);
k_thread_name_set(&cc2520->cc2520_rx_thread, "cc2520_rx"); k_thread_name_set(&cc2520->cc2520_rx_thread, "cc2520_rx");
LOG_INF("CC2520 initialized"); LOG_INF("CC2520 initialized");
@ -1134,6 +1016,16 @@ static void cc2520_iface_init(struct net_if *iface)
ieee802154_init(iface); ieee802154_init(iface);
} }
static const struct cc2520_config cc2520_config = {
.bus = SPI_DT_SPEC_INST_GET(0, SPI_WORD_SET(8), 0),
.vreg_en = GPIO_DT_SPEC_INST_GET(0, vreg_en_gpios),
.reset = GPIO_DT_SPEC_INST_GET(0, reset_gpios),
.fifo = GPIO_DT_SPEC_INST_GET(0, fifo_gpios),
.cca = GPIO_DT_SPEC_INST_GET(0, cca_gpios),
.sfd = GPIO_DT_SPEC_INST_GET(0, sfd_gpios),
.fifop = GPIO_DT_SPEC_INST_GET(0, fifop_gpios)
};
static struct cc2520_context cc2520_context_data; static struct cc2520_context cc2520_context_data;
static struct ieee802154_radio_api cc2520_radio_api = { static struct ieee802154_radio_api cc2520_radio_api = {
@ -1157,7 +1049,7 @@ DEVICE_DEFINE(cc2520, CONFIG_IEEE802154_CC2520_DRV_NAME,
#else #else
NET_DEVICE_INIT(cc2520, CONFIG_IEEE802154_CC2520_DRV_NAME, NET_DEVICE_INIT(cc2520, CONFIG_IEEE802154_CC2520_DRV_NAME,
cc2520_init, NULL, cc2520_init, NULL,
&cc2520_context_data, NULL, &cc2520_context_data, &cc2520_config,
CONFIG_IEEE802154_CC2520_INIT_PRIO, CONFIG_IEEE802154_CC2520_INIT_PRIO,
&cc2520_radio_api, IEEE802154_L2, &cc2520_radio_api, IEEE802154_L2,
NET_L2_GET_CTX_TYPE(IEEE802154_L2), 125); NET_L2_GET_CTX_TYPE(IEEE802154_L2), 125);
@ -1166,21 +1058,21 @@ NET_DEVICE_INIT(cc2520, CONFIG_IEEE802154_CC2520_DRV_NAME,
#ifdef CONFIG_IEEE802154_CC2520_CRYPTO #ifdef CONFIG_IEEE802154_CC2520_CRYPTO
static inline bool cc2520_read_ram(struct cc2520_context *ctx, uint16_t addr, static inline bool cc2520_read_ram(const struct device *dev, uint16_t addr,
uint8_t *data_buf, uint8_t len)
{
return z_cc2520_access(dev, true, CC2520_INS_MEMRD,
addr, data_buf, len);
}
static inline bool cc2520_write_ram(const struct device *dev, uint16_t addr,
uint8_t *data_buf, uint8_t len) uint8_t *data_buf, uint8_t len)
{ {
return z_cc2520_access(ctx, true, CC2520_INS_MEMRD, return z_cc2520_access(dev, false, CC2520_INS_MEMWR,
addr, data_buf, len); addr, data_buf, len);
} }
static inline bool cc2520_write_ram(struct cc2520_context *ctx, uint16_t addr, static inline bool instruct_uccm_ccm(const struct device *dev,
uint8_t *data_buf, uint8_t len)
{
return z_cc2520_access(ctx, false, CC2520_INS_MEMWR,
addr, data_buf, len);
}
static inline bool instruct_uccm_ccm(struct cc2520_context *cc2520,
bool uccm, bool uccm,
uint8_t key_addr, uint8_t key_addr,
uint8_t auth_crypt, uint8_t auth_crypt,
@ -1190,6 +1082,8 @@ static inline bool instruct_uccm_ccm(struct cc2520_context *cc2520,
uint8_t in_len, uint8_t in_len,
uint8_t m) uint8_t m)
{ {
const struct cc2520_config *cfg = dev->config;
struct cc2520_context *ctx = dev->data;
uint8_t cmd[9]; uint8_t cmd[9];
const struct spi_buf buf[1] = { const struct spi_buf buf[1] = {
{ {
@ -1220,11 +1114,11 @@ static inline bool instruct_uccm_ccm(struct cc2520_context *cc2520,
cmd[7] = (in_len & 0x7f); cmd[7] = (in_len & 0x7f);
cmd[8] = (m & 0x03); cmd[8] = (m & 0x03);
k_sem_take(&cc2520->access_lock, K_FOREVER); k_sem_take(&ctx->access_lock, K_FOREVER);
ret = spi_write(cc2520->spi, &cc2520->spi_cfg, &tx); ret = spi_write_dt(&cfg->bus, &tx);
k_sem_give(&cc2520->access_lock); k_sem_give(&ctx->access_lock);
if (ret) { if (ret) {
LOG_ERR("%sCCM Failed", uccm ? "U" : ""); LOG_ERR("%sCCM Failed", uccm ? "U" : "");
@ -1252,7 +1146,7 @@ static int insert_crypto_parameters(struct cipher_ctx *ctx,
struct cipher_aead_pkt *apkt, struct cipher_aead_pkt *apkt,
uint8_t *ccm_nonce, uint8_t *auth_crypt) uint8_t *ccm_nonce, uint8_t *auth_crypt)
{ {
struct cc2520_context *cc2520 = ctx->device->data; const struct device *cc2520 = ctx->device;
uint8_t data[128]; uint8_t data[128];
uint8_t *in_buf; uint8_t *in_buf;
uint8_t in_len; uint8_t in_len;
@ -1343,7 +1237,7 @@ static int cc2520_crypto_ccm(struct cipher_ctx *ctx,
struct cipher_aead_pkt *apkt, struct cipher_aead_pkt *apkt,
uint8_t *ccm_nonce) uint8_t *ccm_nonce)
{ {
struct cc2520_context *cc2520 = ctx->device->data; const struct device *cc2520 = ctx->device;
uint8_t auth_crypt; uint8_t auth_crypt;
int m; int m;
@ -1388,7 +1282,7 @@ static int cc2520_crypto_uccm(struct cipher_ctx *ctx,
struct cipher_aead_pkt *apkt, struct cipher_aead_pkt *apkt,
uint8_t *ccm_nonce) uint8_t *ccm_nonce)
{ {
struct cc2520_context *cc2520 = ctx->device->data; const struct device *cc2520 = ctx->device;
uint8_t auth_crypt; uint8_t auth_crypt;
int m; int m;

View file

@ -11,22 +11,21 @@
#include <linker/sections.h> #include <linker/sections.h>
#include <sys/atomic.h> #include <sys/atomic.h>
#include <drivers/gpio.h>
#include <drivers/spi.h> #include <drivers/spi.h>
enum cc2520_gpio_index { /* Compile time config structure
CC2520_GPIO_IDX_VREG_EN = 0, *******************************
CC2520_GPIO_IDX_RESET, */
CC2520_GPIO_IDX_FIFO,
CC2520_GPIO_IDX_CCA,
CC2520_GPIO_IDX_SFD,
CC2520_GPIO_IDX_FIFOP,
CC2520_GPIO_IDX_MAX, struct cc2520_config {
}; struct spi_dt_spec bus;
struct gpio_dt_spec vreg_en;
struct cc2520_gpio_configuration { struct gpio_dt_spec reset;
const struct device *dev; struct gpio_dt_spec fifo;
uint32_t pin; struct gpio_dt_spec cca;
struct gpio_dt_spec sfd;
struct gpio_dt_spec fifop;
}; };
/* Runtime context structure /* Runtime context structure
@ -35,11 +34,9 @@ struct cc2520_gpio_configuration {
struct cc2520_context { struct cc2520_context {
struct net_if *iface; struct net_if *iface;
/**************************/ /**************************/
struct cc2520_gpio_configuration gpios[CC2520_GPIO_IDX_MAX]; const struct device *dev;
struct gpio_callback sfd_cb; struct gpio_callback sfd_cb;
struct gpio_callback fifop_cb; struct gpio_callback fifop_cb;
const struct device *spi;
struct spi_config spi_cfg;
uint8_t mac_addr[8]; uint8_t mac_addr[8];
/************TX************/ /************TX************/
struct k_sem tx_sync; struct k_sem tx_sync;
@ -62,49 +59,49 @@ struct cc2520_context {
*************************** ***************************
*/ */
bool z_cc2520_access(struct cc2520_context *ctx, bool read, uint8_t ins, bool z_cc2520_access(const struct device *dev, bool read, uint8_t ins,
uint16_t addr, void *data, size_t length); uint16_t addr, void *data, size_t length);
#define DEFINE_SREG_READ(__reg_name, __reg_addr) \ #define DEFINE_SREG_READ(__reg_name, __reg_addr) \
static inline uint8_t read_reg_##__reg_name(struct cc2520_context *ctx) \ static inline uint8_t read_reg_##__reg_name(const struct device *dev) \
{ \ { \
uint8_t val; \ uint8_t val; \
\ \
if (z_cc2520_access(ctx, true, CC2520_INS_MEMRD, \ if (z_cc2520_access(dev, true, CC2520_INS_MEMRD, \
__reg_addr, &val, 1)) { \ __reg_addr, &val, 1)) { \
return val; \ return val; \
} \ } \
\ \
return 0; \ return 0; \
} }
#define DEFINE_SREG_WRITE(__reg_name, __reg_addr) \ #define DEFINE_SREG_WRITE(__reg_name, __reg_addr) \
static inline bool write_reg_##__reg_name(struct cc2520_context *ctx, \ static inline bool write_reg_##__reg_name(const struct device *dev, \
uint8_t val) \ uint8_t val) \
{ \ { \
return z_cc2520_access(ctx, false, CC2520_INS_MEMWR, \ return z_cc2520_access(dev, false, CC2520_INS_MEMWR, \
__reg_addr, &val, 1); \ __reg_addr, &val, 1); \
} }
#define DEFINE_FREG_READ(__reg_name, __reg_addr) \ #define DEFINE_FREG_READ(__reg_name, __reg_addr) \
static inline uint8_t read_reg_##__reg_name(struct cc2520_context *ctx) \ static inline uint8_t read_reg_##__reg_name(const struct device *dev) \
{ \ { \
uint8_t val; \ uint8_t val; \
\ \
if (z_cc2520_access(ctx, true, CC2520_INS_REGRD, \ if (z_cc2520_access(dev, true, CC2520_INS_REGRD, \
__reg_addr, &val, 1)) { \ __reg_addr, &val, 1)) { \
return val; \ return val; \
} \ } \
\ \
return 0; \ return 0; \
} }
#define DEFINE_FREG_WRITE(__reg_name, __reg_addr) \ #define DEFINE_FREG_WRITE(__reg_name, __reg_addr) \
static inline bool write_reg_##__reg_name(struct cc2520_context *ctx, \ static inline bool write_reg_##__reg_name(const struct device *dev, \
uint8_t val) \ uint8_t val) \
{ \ { \
return z_cc2520_access(ctx, false, CC2520_INS_REGWR, \ return z_cc2520_access(dev, false, CC2520_INS_REGWR, \
__reg_addr, &val, 1); \ __reg_addr, &val, 1); \
} }
DEFINE_FREG_READ(excflag0, CC2520_FREG_EXCFLAG0) DEFINE_FREG_READ(excflag0, CC2520_FREG_EXCFLAG0)
@ -150,12 +147,12 @@ DEFINE_SREG_WRITE(extclock, CC2520_SREG_EXTCLOCK)
************************ ************************
*/ */
#define DEFINE_MEM_WRITE(__mem_name, __addr, __sz) \ #define DEFINE_MEM_WRITE(__mem_name, __addr, __sz) \
static inline bool write_mem_##__mem_name(struct cc2520_context *ctx, \ static inline bool write_mem_##__mem_name(const struct device *dev, \
uint8_t *buf) \ uint8_t *buf) \
{ \ { \
return z_cc2520_access(ctx, false, CC2520_INS_MEMWR, \ return z_cc2520_access(dev, false, CC2520_INS_MEMWR, \
__addr, buf, __sz); \ __addr, buf, __sz); \
} }
DEFINE_MEM_WRITE(short_addr, CC2520_MEM_SHORT_ADDR, 2) DEFINE_MEM_WRITE(short_addr, CC2520_MEM_SHORT_ADDR, 2)
@ -167,30 +164,30 @@ DEFINE_MEM_WRITE(ext_addr, CC2520_MEM_EXT_ADDR, 8)
****************************** ******************************
*/ */
static inline bool cc2520_command_strobe(struct cc2520_context *ctx, static inline bool cc2520_command_strobe(const struct device *dev,
uint8_t instruction) uint8_t instruction)
{ {
return z_cc2520_access(ctx, false, instruction, 0, NULL, 0); return z_cc2520_access(dev, false, instruction, 0, NULL, 0);
} }
static inline bool cc2520_command_strobe_snop(struct cc2520_context *ctx, static inline bool cc2520_command_strobe_snop(const struct device *dev,
uint8_t instruction) uint8_t instruction)
{ {
uint8_t snop[1] = { CC2520_INS_SNOP }; uint8_t snop[1] = { CC2520_INS_SNOP };
return z_cc2520_access(ctx, false, instruction, 0, snop, 1); return z_cc2520_access(dev, false, instruction, 0, snop, 1);
} }
#define DEFINE_STROBE_INSTRUCTION(__ins_name, __ins) \ #define DEFINE_STROBE_INSTRUCTION(__ins_name, __ins) \
static inline bool instruct_##__ins_name(struct cc2520_context *ctx) \ static inline bool instruct_##__ins_name(const struct device *dev) \
{ \ { \
return cc2520_command_strobe(ctx, __ins); \ return cc2520_command_strobe(dev, __ins); \
} }
#define DEFINE_STROBE_SNOP_INSTRUCTION(__ins_name, __ins) \ #define DEFINE_STROBE_SNOP_INSTRUCTION(__ins_name, __ins) \
static inline bool instruct_##__ins_name(struct cc2520_context *ctx) \ static inline bool instruct_##__ins_name(const struct device *dev) \
{ \ { \
return cc2520_command_strobe_snop(ctx, __ins); \ return cc2520_command_strobe_snop(dev, __ins); \
} }
DEFINE_STROBE_INSTRUCTION(srxon, CC2520_INS_SRXON) DEFINE_STROBE_INSTRUCTION(srxon, CC2520_INS_SRXON)