From 732cbd969c540e134c3e458adb76c34aeb3d1f21 Mon Sep 17 00:00:00 2001 From: Marian Buschsieweke Date: Mon, 1 Feb 2021 14:25:22 +0100 Subject: [PATCH] drivers: update to new periph_spi API --- drivers/at25xxx/at25xxx.c | 13 ++++++----- drivers/at86rf2xx/at86rf2xx_netdev.c | 11 ++++----- drivers/ata8520e/ata8520e.c | 4 +--- drivers/bmx280/bmx280.c | 4 +--- drivers/cc110x/cc110x.c | 12 +++------- drivers/cc110x/cc110x_calibration.c | 8 ++----- drivers/cc110x/cc110x_communication.c | 4 +--- drivers/cc110x/cc110x_netdev.c | 23 ++++--------------- drivers/cc110x/cc110x_rx_tx.c | 5 +--- drivers/cc110x/include/cc110x_communication.h | 11 +++------ drivers/kw2xrf/kw2xrf_spi.c | 18 ++++----------- drivers/lis2dh12/lis2dh12.c | 3 ++- drivers/nrf24l01p/nrf24l01p.c | 9 ++++---- .../include/nrf24l01p_ng_communication.h | 6 +---- .../nrf24l01p_ng/nrf24l01p_ng_communication.c | 5 ++-- drivers/nrf24l01p_ng/nrf24l01p_ng_netdev.c | 5 +--- tests/driver_cc110x/sc_cc110x.c | 5 +--- 17 files changed, 43 insertions(+), 103 deletions(-) diff --git a/drivers/at25xxx/at25xxx.c b/drivers/at25xxx/at25xxx.c index 486cead8c3..6327d06899 100644 --- a/drivers/at25xxx/at25xxx.c +++ b/drivers/at25xxx/at25xxx.c @@ -48,9 +48,9 @@ #define AT225XXXX_SET_BUF_SIZE (64) #endif -static inline int getbus(const at25xxx_t *dev) +static inline void getbus(const at25xxx_t *dev) { - return spi_acquire(dev->params.spi, dev->params.cs_pin, SPI_MODE_0, dev->params.spi_clk); + spi_acquire(dev->params.spi, dev->params.cs_pin, SPI_MODE_0, dev->params.spi_clk); } static inline uint32_t _pos(uint8_t cmd, uint32_t pos) @@ -254,10 +254,11 @@ int at25xxx_init(at25xxx_t *dev, const at25xxx_params_t *params) gpio_set(dev->params.hold_pin); } - /* check if the SPI configuration is valid */ - if (getbus(dev) != SPI_OK) { - return -1; + if (!IS_ACTIVE(NDEBUG)) { + /* if assertions are on, trigger an assert on incorrect SPI settings + * right on initialization to ease debugging */ + getbus(dev); + spi_release(dev->params.spi); } - spi_release(dev->params.spi); return 0; } diff --git a/drivers/at86rf2xx/at86rf2xx_netdev.c b/drivers/at86rf2xx/at86rf2xx_netdev.c index fc472e805b..78db1b26dc 100644 --- a/drivers/at86rf2xx/at86rf2xx_netdev.c +++ b/drivers/at86rf2xx/at86rf2xx_netdev.c @@ -89,14 +89,11 @@ static int _init(netdev_t *netdev) 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, - since getbus() drops the return value */ - if (spi_acquire(dev->params.spi, dev->params.cs_pin, SPI_MODE_0, - dev->params.spi_clk) < 0) { - DEBUG("[at86rf2xx] error: unable to acquire SPI bus\n"); - return -EIO; + /* 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); } - spi_release(dev->params.spi); #endif /* reset hardware into a defined state */ diff --git a/drivers/ata8520e/ata8520e.c b/drivers/ata8520e/ata8520e.c index a7e1d9a337..b47302dba2 100644 --- a/drivers/ata8520e/ata8520e.c +++ b/drivers/ata8520e/ata8520e.c @@ -156,9 +156,7 @@ static void _irq_handler(void *arg) static void _getbus(const ata8520e_t *dev) { - if (spi_acquire(SPIDEV, CSPIN, SPI_MODE_0, dev->params.spi_clk) < 0) { - DEBUG("[ata8520e] ERROR: Cannot acquire SPI bus!\n"); - } + spi_acquire(SPIDEV, CSPIN, SPI_MODE_0, dev->params.spi_clk); } static void _spi_transfer_byte(const ata8520e_t *dev, bool cont, uint8_t out) diff --git a/drivers/bmx280/bmx280.c b/drivers/bmx280/bmx280.c index 389e7b5169..5d17313015 100644 --- a/drivers/bmx280/bmx280.c +++ b/drivers/bmx280/bmx280.c @@ -50,9 +50,7 @@ #ifdef BMX280_USE_SPI /* using SPI mode */ static inline int _acquire(const bmx280_t *dev) { - if (spi_acquire(BUS, CS, MODE, CLK) != SPI_OK) { - return BMX280_ERR_BUS; - } + spi_acquire(BUS, CS, MODE, CLK); return BMX280_OK; } diff --git a/drivers/cc110x/cc110x.c b/drivers/cc110x/cc110x.c index a175845831..2697b92ecb 100644 --- a/drivers/cc110x/cc110x.c +++ b/drivers/cc110x/cc110x.c @@ -57,9 +57,7 @@ int cc110x_apply_config(cc110x_t *dev, const cc110x_config_t *conf, return -ERANGE; } - if (cc110x_acquire(dev) != SPI_OK) { - return -EIO; - } + cc110x_acquire(dev); gpio_irq_disable(dev->params.gdo0); gpio_irq_disable(dev->params.gdo2); @@ -107,9 +105,7 @@ int cc110x_set_tx_power(cc110x_t *dev, cc110x_tx_power_t power) return -ERANGE; } - if (cc110x_acquire(dev) != SPI_OK) { - return -EIO; - } + cc110x_acquire(dev); switch (dev->state) { case CC110X_STATE_IDLE: @@ -134,9 +130,7 @@ int cc110x_set_channel(cc110x_t *dev, uint8_t channel) return -EINVAL; } - if (cc110x_acquire(dev) != SPI_OK) { - return -EIO; - } + cc110x_acquire(dev); if ((channel >= CC110X_MAX_CHANNELS) || (dev->channels->map[channel] == 0xff)) { /* Channel out of range or not supported in current channel map */ diff --git a/drivers/cc110x/cc110x_calibration.c b/drivers/cc110x/cc110x_calibration.c index f111736751..89f6913bad 100644 --- a/drivers/cc110x/cc110x_calibration.c +++ b/drivers/cc110x/cc110x_calibration.c @@ -67,9 +67,7 @@ int cc110x_recalibrate(cc110x_t *dev) /* Re-acquire SPI interface in order to check if calibration * succeeded */ - if (cc110x_acquire(dev) != SPI_OK) { - return -EIO; - } + cc110x_acquire(dev); } while (cc110x_state_from_status(cc110x_status(dev)) != CC110X_STATE_IDLE); get_calibration_data(dev); @@ -84,9 +82,7 @@ int cc110x_full_calibration(cc110x_t *dev) return -EINVAL; } - if (cc110x_acquire(dev) != SPI_OK) { - return -EIO; - } + cc110x_acquire(dev); switch (dev->state) { case CC110X_STATE_IDLE: diff --git a/drivers/cc110x/cc110x_communication.c b/drivers/cc110x/cc110x_communication.c index 86ae38d1d6..f84c3eb137 100644 --- a/drivers/cc110x/cc110x_communication.c +++ b/drivers/cc110x/cc110x_communication.c @@ -39,9 +39,7 @@ int cc110x_power_on_and_acquire(cc110x_t *dev) gpio_set(cs); spi_init_cs(dev->params.spi, dev->params.cs); - if (cc110x_acquire(dev) != SPI_OK) { - return -EIO; - } + cc110x_acquire(dev); while (cc110x_state_from_status(cc110x_status(dev)) != CC110X_STATE_IDLE) { cc110x_cmd(dev, CC110X_STROBE_IDLE); diff --git a/drivers/cc110x/cc110x_netdev.c b/drivers/cc110x/cc110x_netdev.c index ae9949516e..aaebfac6fa 100644 --- a/drivers/cc110x/cc110x_netdev.c +++ b/drivers/cc110x/cc110x_netdev.c @@ -354,11 +354,7 @@ static int cc110x_recv(netdev_t *netdev, void *buf, size_t len, void *info) /* Call to cc110x_enter_rx_mode() will clear dev->buf.len, so back up it first */ int size = dev->buf.len; - if (cc110x_acquire(dev) != SPI_OK) { - DEBUG("[cc110x] netdev_driver_t::recv(): cc110x_acquire() " - "failed\n"); - return -EIO; - } + cc110x_acquire(dev); /* Copy RX info on last frame (if requested) */ if (info != NULL) { @@ -396,10 +392,7 @@ static int cc110x_send(netdev_t *netdev, const iolist_t *iolist) /* assert that cc110x_send was called with valid parameters */ assert(netdev && iolist && (iolist->iol_len == sizeof(cc1xxx_l2hdr_t))); - if (cc110x_acquire(dev) != SPI_OK) { - DEBUG("[cc110x] netdev_driver_t::send(): cc110x_acquire() failed\n"); - return -1; - } + cc110x_acquire(dev); switch (dev->state) { case CC110X_STATE_FSTXON: @@ -503,9 +496,7 @@ static int cc110x_send(netdev_t *netdev, const iolist_t *iolist) */ static int cc110x_get_promiscuous_mode(cc110x_t *dev, netopt_enable_t *dest) { - if (cc110x_acquire(dev) != SPI_OK) { - return -EIO; - } + cc110x_acquire(dev); uint8_t pktctrl1; cc110x_read(dev, CC110X_REG_PKTCTRL1, &pktctrl1); @@ -598,9 +589,7 @@ static int cc110x_get(netdev_t *netdev, netopt_t opt, */ static int cc110x_set_addr(cc110x_t *dev, uint8_t addr) { - if (cc110x_acquire(dev) != SPI_OK) { - return -EIO; - } + cc110x_acquire(dev); dev->addr = addr; cc110x_write(dev, CC110X_REG_ADDR, addr); @@ -618,9 +607,7 @@ static int cc110x_set_addr(cc110x_t *dev, uint8_t addr) */ static int cc110x_set_promiscuous_mode(cc110x_t *dev, netopt_enable_t enable) { - if (cc110x_acquire(dev) != SPI_OK) { - return -EIO; - } + cc110x_acquire(dev); uint8_t pktctrl1 = CC110X_PKTCTRL1_VALUE; if (enable == NETOPT_ENABLE) { diff --git a/drivers/cc110x/cc110x_rx_tx.c b/drivers/cc110x/cc110x_rx_tx.c index ab2d15554d..f19f396adf 100644 --- a/drivers/cc110x/cc110x_rx_tx.c +++ b/drivers/cc110x/cc110x_rx_tx.c @@ -280,10 +280,7 @@ void cc110x_isr(netdev_t *netdev) */ netdev_event_t post_isr_event = NETDEV_NO_EVENT; - if (cc110x_acquire(dev) != SPI_OK) { - DEBUG("[cc110x] ISR: CRITICAL ERROR: Couldn't acquire device\n"); - return; - } + cc110x_acquire(dev); /* Disable IRQs in a coarse manner, instead of doing so any time the * IOCFGx configuration registers are changed. (This should be less diff --git a/drivers/cc110x/include/cc110x_communication.h b/drivers/cc110x/include/cc110x_communication.h index 2f26636318..6e158ec41a 100644 --- a/drivers/cc110x/include/cc110x_communication.h +++ b/drivers/cc110x/include/cc110x_communication.h @@ -29,20 +29,15 @@ extern "C" { #endif /** - * @brief Acquire the SPI interface of the transceiver and configure it - * - * @retval SPI_OK Success - * @retval SPI_NOMODE SPI mode 0 not supported by MCU - * @retval SPI_NOCLK SPI clock given in @ref cc110x_params_t is not supported + * @brief Acquire the SPI interface of the transceiver * * @pre When first acquiring the device either after boot or after having put * the device to sleep mode, use @ref cc110x_power_on_and_acquire * instead. Subsequently, this function should be used (it is faster). */ -static inline int cc110x_acquire(cc110x_t *dev) +static inline void cc110x_acquire(cc110x_t *dev) { - return spi_acquire(dev->params.spi, dev->params.cs, SPI_MODE_0, - dev->params.spi_clk); + spi_acquire(dev->params.spi, dev->params.cs, SPI_MODE_0, dev->params.spi_clk); } /** diff --git a/drivers/kw2xrf/kw2xrf_spi.c b/drivers/kw2xrf/kw2xrf_spi.c index 223adef247..446051e751 100644 --- a/drivers/kw2xrf/kw2xrf_spi.c +++ b/drivers/kw2xrf/kw2xrf_spi.c @@ -74,21 +74,11 @@ int kw2xrf_spi_init(kw2xrf_t *dev) (unsigned)SPIDEV, res); return 1; } - /* verify SPI params */ - res = spi_acquire(SPIDEV, CSPIN, SPIMODE, SPICLK); - if (res == SPI_NOMODE) { - LOG_ERROR("[kw2xrf_spi] given SPI mode is not supported"); - return 1; + /* verify SPI params, if assertions are on */ + if (!IS_ACTIVE(NDEBUG)) { + spi_acquire(SPIDEV, CSPIN, SPIMODE, SPICLK); + spi_release(SPIDEV); } - else if (res == SPI_NOCLK) { - LOG_ERROR("[kw2xrf_spi] targeted clock speed is not supported"); - return 1; - } - else if (res != SPI_OK) { - LOG_ERROR("[kw2xrf_spi] unable to acquire bus with given parameters"); - return 1; - } - spi_release(SPIDEV); DEBUG("[kw2xrf_spi] SPI_DEV(%u) initialized: mode: %u, clk: %u, cs_pin: %u\n", (unsigned)SPIDEV, (unsigned)SPIMODE, (unsigned)SPICLK, (unsigned)CSPIN); diff --git a/drivers/lis2dh12/lis2dh12.c b/drivers/lis2dh12/lis2dh12.c index 963eb9be4c..c3c43ba9e7 100644 --- a/drivers/lis2dh12/lis2dh12.c +++ b/drivers/lis2dh12/lis2dh12.c @@ -58,7 +58,8 @@ static int _init_bus(const lis2dh12_t *dev) static int _acquire(const lis2dh12_t *dev) { - return spi_acquire(BUS, BUS_CS, BUS_MODE, BUS_CLK); + spi_acquire(BUS, BUS_CS, BUS_MODE, BUS_CLK); + return BUS_OK; } static void _release(const lis2dh12_t *dev) diff --git a/drivers/nrf24l01p/nrf24l01p.c b/drivers/nrf24l01p/nrf24l01p.c index dc72fde786..b8829871bc 100644 --- a/drivers/nrf24l01p/nrf24l01p.c +++ b/drivers/nrf24l01p/nrf24l01p.c @@ -83,12 +83,11 @@ int nrf24l01p_init(nrf24l01p_t *dev, spi_t spi, gpio_t ce, gpio_t cs, gpio_t irq /* Init IRQ pin */ gpio_init_int(dev->irq, GPIO_IN_PU, GPIO_FALLING, nrf24l01p_rx_cb, dev); - /* Test the SPI connection */ - if (spi_acquire(dev->spi, dev->cs, SPI_MODE, SPI_CLK) != SPI_OK) { - DEBUG("error: unable to acquire SPI bus with given params\n"); - return -1; + /* Test the SPI connection, if assertions are on */ + if (!IS_ACTIVE(NDEBUG)) { + spi_acquire(dev->spi, dev->cs, SPI_MODE, SPI_CLK); + spi_release(dev->spi); } - spi_release(dev->spi); xtimer_spin(DELAY_AFTER_FUNC_TICKS); diff --git a/drivers/nrf24l01p_ng/include/nrf24l01p_ng_communication.h b/drivers/nrf24l01p_ng/include/nrf24l01p_ng_communication.h index 94984b666c..ade08c49f3 100644 --- a/drivers/nrf24l01p_ng/include/nrf24l01p_ng_communication.h +++ b/drivers/nrf24l01p_ng/include/nrf24l01p_ng_communication.h @@ -105,17 +105,13 @@ extern "C" { * @brief Acquire the SPI bus of the transceiver * * @param[in] dev NRF24L01+ device handle - * - * @return @see spi_acquire */ -int nrf24l01p_ng_acquire(nrf24l01p_ng_t *dev); +void nrf24l01p_ng_acquire(nrf24l01p_ng_t *dev); /** * @brief Release the SPI bus of the transceiver * * @param[in] dev NRF24L01+ device handle - * - * @return @see spi_release */ void nrf24l01p_ng_release(nrf24l01p_ng_t *dev); diff --git a/drivers/nrf24l01p_ng/nrf24l01p_ng_communication.c b/drivers/nrf24l01p_ng/nrf24l01p_ng_communication.c index daefb8bd4d..c8662ef467 100644 --- a/drivers/nrf24l01p_ng/nrf24l01p_ng_communication.c +++ b/drivers/nrf24l01p_ng/nrf24l01p_ng_communication.c @@ -74,10 +74,9 @@ static void _nrf24l01p_ng_copy_and_swap_bytes(uint8_t* dst, const uint8_t* src, } } -int nrf24l01p_ng_acquire(nrf24l01p_ng_t *dev) +void nrf24l01p_ng_acquire(nrf24l01p_ng_t *dev) { - return spi_acquire(dev->params.spi, dev->params.pin_cs, SPI_MODE_0, - dev->params.spi_clk); + spi_acquire(dev->params.spi, dev->params.pin_cs, SPI_MODE_0, dev->params.spi_clk); } void nrf24l01p_ng_release(nrf24l01p_ng_t *dev) diff --git a/drivers/nrf24l01p_ng/nrf24l01p_ng_netdev.c b/drivers/nrf24l01p_ng/nrf24l01p_ng_netdev.c index c559b49946..9f4b5ac201 100644 --- a/drivers/nrf24l01p_ng/nrf24l01p_ng_netdev.c +++ b/drivers/nrf24l01p_ng/nrf24l01p_ng_netdev.c @@ -177,10 +177,7 @@ static int _init(netdev_t *netdev) return -EIO; } gpio_clear(dev->params.pin_ce); - if (nrf24l01p_ng_acquire(dev) < 0) { - DEBUG_PUTS("[nrf24l01p_ng] _init(): nrf24l01p_ng_acquire() failed"); - return -EIO; - } + nrf24l01p_ng_acquire(dev); if (dev->state != NRF24L01P_NG_STATE_POWER_DOWN) { nrf24l01p_ng_transition_to_power_down(dev); } diff --git a/tests/driver_cc110x/sc_cc110x.c b/tests/driver_cc110x/sc_cc110x.c index c42a14a7ea..d2c5090b85 100644 --- a/tests/driver_cc110x/sc_cc110x.c +++ b/tests/driver_cc110x/sc_cc110x.c @@ -104,10 +104,7 @@ static void print_state(cc110x_t *dev) uint8_t virtual_channel; /* Get all required data and release device */ - if (cc110x_acquire(dev) != SPI_OK) { - puts("Failed to acquire CC1100/CC1101 transceiver"); - return; - } + cc110x_acquire(dev); if (dev->state == CC110X_STATE_OFF) { cc110x_release(dev);