diff --git a/drivers/at86rf2xx/at86rf2xx_internal.c b/drivers/at86rf2xx/at86rf2xx_internal.c index 118f464a24..1a958494b7 100644 --- a/drivers/at86rf2xx/at86rf2xx_internal.c +++ b/drivers/at86rf2xx/at86rf2xx_internal.c @@ -240,3 +240,22 @@ void at86rf2xx_get_random(at86rf2xx_t *dev, uint8_t *data, size_t len) at86rf2xx_enable_smart_idle(dev); } #endif + +#if !AT86RF2XX_IS_PERIPH +void at86rf2xx_spi_init(at86rf2xx_t *dev, void (*irq_handler)(void *arg)) +{ + /* initialize GPIOs */ + spi_init_cs(dev->params.spi, dev->params.cs_pin); + gpio_init(dev->params.sleep_pin, GPIO_OUT); + gpio_clear(dev->params.sleep_pin); + gpio_init(dev->params.reset_pin, GPIO_OUT); + gpio_set(dev->params.reset_pin); + gpio_init_int(dev->params.int_pin, GPIO_IN, GPIO_RISING, irq_handler, dev); + + /* Intentionally check if bus can be acquired, if assertions are on */ + if (!IS_ACTIVE(NDEBUG)) { + spi_acquire(dev->params.spi, dev->params.cs_pin, SPI_MODE_0, dev->params.spi_clk); + spi_release(dev->params.spi); + } +} +#endif diff --git a/drivers/at86rf2xx/at86rf2xx_netdev.c b/drivers/at86rf2xx/at86rf2xx_netdev.c index fa425697bc..572d1c26c5 100644 --- a/drivers/at86rf2xx/at86rf2xx_netdev.c +++ b/drivers/at86rf2xx/at86rf2xx_netdev.c @@ -62,15 +62,12 @@ const netdev_driver_t at86rf2xx_driver = { .set = _set, }; -#if AT86RF2XX_IS_PERIPH /* SOC has radio interrupts, store reference to netdev */ static netdev_t *at86rfmega_dev; -#else static void _irq_handler(void *arg) { netdev_trigger_event_isr(arg); } -#endif static int _init(netdev_t *netdev) { @@ -78,23 +75,12 @@ static int _init(netdev_t *netdev) netdev_ieee802154_t, netdev); at86rf2xx_t *dev = container_of(netdev_ieee802154, at86rf2xx_t, netdev); -#if AT86RF2XX_IS_PERIPH - at86rfmega_dev = netdev; -#else - /* initialize GPIOs */ - spi_init_cs(dev->params.spi, dev->params.cs_pin); - gpio_init(dev->params.sleep_pin, GPIO_OUT); - gpio_clear(dev->params.sleep_pin); - gpio_init(dev->params.reset_pin, GPIO_OUT); - gpio_set(dev->params.reset_pin); - gpio_init_int(dev->params.int_pin, GPIO_IN, GPIO_RISING, _irq_handler, dev); - - /* Intentionally check if bus can be acquired, if assertions are on */ - if (!IS_ACTIVE(NDEBUG)) { - spi_acquire(dev->params.spi, dev->params.cs_pin, SPI_MODE_0, dev->params.spi_clk); - spi_release(dev->params.spi); + if (AT86RF2XX_IS_PERIPH) { + at86rfmega_dev = netdev; + } + else { + at86rf2xx_spi_init(dev, _irq_handler); } -#endif /* reset hardware into a defined state */ at86rf2xx_hardware_reset(dev); @@ -161,11 +147,7 @@ static int _recv(netdev_t *netdev, void *buf, size_t len, void *info) at86rf2xx_fb_start(dev); /* get the size of the received packet */ -#if AT86RF2XX_IS_PERIPH - phr = TST_RX_LENGTH; -#else - at86rf2xx_fb_read(dev, &phr, 1); -#endif + phr = at86rf2xx_get_rx_len(dev); /* ignore MSB (refer p.80) and subtract length of FCS field */ pkt_len = (phr & 0x7f) - 2; @@ -752,12 +734,7 @@ static void _isr(netdev_t *netdev) } /* read (consume) device status */ -#if AT86RF2XX_IS_PERIPH - irq_mask = dev->irq_status; - dev->irq_status = 0; -#else - irq_mask = at86rf2xx_reg_read(dev, AT86RF2XX_REG__IRQ_STATUS); -#endif + irq_mask = at86rf2xx_get_irq_flags(dev); trac_status = at86rf2xx_reg_read(dev, AT86RF2XX_REG__TRX_STATE) & AT86RF2XX_TRX_STATE_MASK__TRAC; diff --git a/drivers/at86rf2xx/include/at86rf2xx_internal.h b/drivers/at86rf2xx/include/at86rf2xx_internal.h index 3dabdba27c..7daa3ac8e7 100644 --- a/drivers/at86rf2xx/include/at86rf2xx_internal.h +++ b/drivers/at86rf2xx/include/at86rf2xx_internal.h @@ -30,8 +30,8 @@ #if AT86RF2XX_IS_PERIPH #include -#include "at86rf2xx_registers.h" #endif +#include "at86rf2xx_registers.h" #ifdef __cplusplus extern "C" { @@ -242,6 +242,54 @@ void at86rf2xx_configure_phy(at86rf2xx_t *dev); void at86rf2xx_get_random(at86rf2xx_t *dev, uint8_t *data, size_t len); #endif +/** + * @brief Initialize AT86RF2XX SPI communication + * + * @param[in,out] dev device to initialize + * @param[in] irq_handler IRQ handler + */ +void at86rf2xx_spi_init(at86rf2xx_t *dev, void (*irq_handler)(void *arg)); + +/** + * @brief Get the PSDU length of the received frame. + * + * @param[in] dev pointer to the device descriptor + * + * @return the PSDU length + */ +static inline uint8_t at86rf2xx_get_rx_len(at86rf2xx_t *dev) +{ + (void) dev; +#if AT86RF2XX_IS_PERIPH + return TST_RX_LENGTH; +#else + uint8_t phr; + at86rf2xx_fb_read(dev, &phr, 1); + return phr; +#endif +} + +/** + * @brief Get the IRQ flags. + * + * This function clears the IRQ flags + * @param[in,out] dev pointer to the device descriptor + * + * @return IRQ flags + */ +static inline uint8_t at86rf2xx_get_irq_flags(at86rf2xx_t *dev) +{ + (void) dev; +#if AT86RF2XX_IS_PERIPH + uint8_t irq_mask = dev->irq_status; + dev->irq_status = 0; + return irq_mask; +#else + return at86rf2xx_reg_read(dev, AT86RF2XX_REG__IRQ_STATUS); +#endif + +} + #ifdef __cplusplus } #endif