From 2edf153cf89bb1597a148880148c78309caa4847 Mon Sep 17 00:00:00 2001 From: fabian18 Date: Mon, 2 Sep 2019 08:49:27 +0200 Subject: [PATCH] drivers/at86rf2xx: do not hang on no dev --- drivers/at86rf2xx/at86rf2xx_netdev.c | 17 +++++++++++++---- tests/driver_at86rf2xx/main.c | 11 ++++++++++- 2 files changed, 23 insertions(+), 5 deletions(-) diff --git a/drivers/at86rf2xx/at86rf2xx_netdev.c b/drivers/at86rf2xx/at86rf2xx_netdev.c index c988ac72ba..01a4053e40 100644 --- a/drivers/at86rf2xx/at86rf2xx_netdev.c +++ b/drivers/at86rf2xx/at86rf2xx_netdev.c @@ -79,15 +79,24 @@ 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); - /* reset device to default values and put it into RX state */ - at86rf2xx_reset(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; + } + spi_release(dev->params.spi); - /* test if the SPI is set up correctly and the device is responding */ + /* test if the device is responding */ if (at86rf2xx_reg_read(dev, AT86RF2XX_REG__PART_NUM) != AT86RF2XX_PARTNUM) { DEBUG("[at86rf2xx] error: unable to read correct part number\n"); - return -1; + return -ENOTSUP; } + /* reset device to default values and put it into RX state */ + at86rf2xx_reset(dev); + return 0; } diff --git a/tests/driver_at86rf2xx/main.c b/tests/driver_at86rf2xx/main.c index a6033208a1..7f1dace64d 100644 --- a/tests/driver_at86rf2xx/main.c +++ b/tests/driver_at86rf2xx/main.c @@ -90,6 +90,7 @@ int main(void) puts("AT86RF2xx device driver test"); xtimer_init(); + unsigned dev_success = 0; for (unsigned i = 0; i < AT86RF2XX_NUM; i++) { netopt_enable_t en = NETOPT_ENABLE; const at86rf2xx_params_t *p = &at86rf2xx_params[i]; @@ -98,8 +99,16 @@ int main(void) printf("Initializing AT86RF2xx radio at SPI_%d\n", p->spi); at86rf2xx_setup(&devs[i], p); dev->event_callback = _event_cb; - dev->driver->init(dev); + if (dev->driver->init(dev) < 0) { + continue; + } dev->driver->set(dev, NETOPT_RX_END_IRQ, &en, sizeof(en)); + dev_success++; + } + + if (!dev_success) { + puts("No device could be initialized"); + return 1; } _recv_pid = thread_create(stack, sizeof(stack), THREAD_PRIORITY_MAIN - 1,