diff --git a/boards/sensebox_samd21/Makefile.include b/boards/sensebox_samd21/Makefile.include index e9aaa4ec5a37638f8a2d3fc77340f4f0797a7715..8d1d48ee6f12f5c41451dcf55b1887bd485da3da 100644 --- a/boards/sensebox_samd21/Makefile.include +++ b/boards/sensebox_samd21/Makefile.include @@ -22,5 +22,7 @@ else include $(RIOTMAKE)/tools/bossa.inc.mk endif +CFLAGS += -DSX127X_USE_DIO_MULTI + # setup the boards dependencies include $(RIOTBOARD)/$(BOARD)/Makefile.dep diff --git a/boards/sensebox_samd21/include/board.h b/boards/sensebox_samd21/include/board.h index ee6c2f6e3f9845e69b13886a699dfb90974d2770..19089ec22a8cdfca14b7e51469444ca457925ada 100644 --- a/boards/sensebox_samd21/include/board.h +++ b/boards/sensebox_samd21/include/board.h @@ -129,18 +129,17 @@ extern "C" { #define SX127X_PARAM_RESET GPIO_UNDEF -#define SX127X_PARAM_DIOMULTI XBEE1_INT_PIN /* D24 */ +#define SX127X_PARAM_DIO0 GPIO_UNDEF -#define SX127X_PARAM_PASELECT (SX127X_PA_BOOST) +#define SX127X_PARAM_DIO1 GPIO_UNDEF + +#define SX127X_PARAM_DIO2 GPIO_UNDEF + +#define SX127X_PARAM_DIO3 GPIO_UNDEF -#define SX127X_PARAMS { .spi = SX127X_PARAM_SPI, \ - .nss_pin = SX127X_PARAM_SPI_NSS, \ - .reset_pin = SX127X_PARAM_RESET, \ - .dio0_pin = SX127X_PARAM_DIOMULTI,\ - .dio1_pin = SX127X_PARAM_DIO1, \ - .dio2_pin = SX127X_PARAM_DIO2, \ - .dio3_pin = SX127X_PARAM_DIO3, \ - .paselect = SX127X_PARAM_PASELECT } +#define SX127X_PARAM_DIO_MULTI XBEE1_INT_PIN /* D24 */ + +#define SX127X_PARAM_PASELECT (SX127X_PA_BOOST) /** @} */ /** diff --git a/drivers/include/sx127x.h b/drivers/include/sx127x.h index eb9ede59ed6dc70803d02b9e921de324a4ac9f8e..c7712be83839ace42436d5e3d03785c806c0cbcb 100644 --- a/drivers/include/sx127x.h +++ b/drivers/include/sx127x.h @@ -92,6 +92,9 @@ extern "C" { #define SX127X_IRQ_DIO3 (1<<3) /**< DIO3 IRQ */ #define SX127X_IRQ_DIO4 (1<<4) /**< DIO4 IRQ */ #define SX127X_IRQ_DIO5 (1<<5) /**< DIO5 IRQ */ +#ifdef SX127X_USE_DIO_MULTI +#define SX127X_IRQ_DIO_MULTI (1<<6) /**< DIO MULTI IRQ */ +#endif /** @} */ /** @@ -208,6 +211,9 @@ typedef struct { gpio_t dio3_pin; /**< Interrupt line DIO3 (CAD done) */ gpio_t dio4_pin; /**< Interrupt line DIO4 (not used) */ gpio_t dio5_pin; /**< Interrupt line DIO5 (not used) */ +#ifdef SX127X_USE_DIO_MULTI + gpio_t dio_multi_pin; /**< Interrupt line for multiple IRQs */ +#endif uint8_t paselect; /**< Power amplifier mode (RFO or PABOOST) */ } sx127x_params_t; @@ -246,7 +252,7 @@ void sx127x_setup(sx127x_t *dev, const sx127x_params_t *params); * * @param[in] dev The sx127x device descriptor */ -void sx127x_reset(const sx127x_t *dev); +int sx127x_reset(const sx127x_t *dev); /** * @brief Initializes the transceiver. diff --git a/drivers/sx127x/include/sx127x_params.h b/drivers/sx127x/include/sx127x_params.h index 15763b4a9e424b4589fac0bfb585f5ee7273bf7a..3e621161b4db8e79d8c693ad78618557b009143a 100644 --- a/drivers/sx127x/include/sx127x_params.h +++ b/drivers/sx127x/include/sx127x_params.h @@ -60,19 +60,35 @@ extern "C" { #define SX127X_PARAM_DIO3 GPIO_PIN(1, 4) /* D5 */ #endif +#ifndef SX127X_PARAM_DIO_MULTI +#define SX127X_PARAM_DIO_MULTI GPIO_UNDEF +#endif + #ifndef SX127X_PARAM_PASELECT #define SX127X_PARAM_PASELECT (SX127X_PA_RFO) #endif #ifndef SX127X_PARAMS -#define SX127X_PARAMS { .spi = SX127X_PARAM_SPI, \ - .nss_pin = SX127X_PARAM_SPI_NSS, \ - .reset_pin = SX127X_PARAM_RESET, \ - .dio0_pin = SX127X_PARAM_DIO0, \ - .dio1_pin = SX127X_PARAM_DIO1, \ - .dio2_pin = SX127X_PARAM_DIO2, \ - .dio3_pin = SX127X_PARAM_DIO3, \ - .paselect = SX127X_PARAM_PASELECT } +#ifdef SX127X_USE_DIO_MULTI +#define SX127X_PARAMS { .spi = SX127X_PARAM_SPI, \ + .nss_pin = SX127X_PARAM_SPI_NSS, \ + .reset_pin = SX127X_PARAM_RESET, \ + .dio0_pin = SX127X_PARAM_DIO0, \ + .dio1_pin = SX127X_PARAM_DIO1, \ + .dio2_pin = SX127X_PARAM_DIO2, \ + .dio3_pin = SX127X_PARAM_DIO3, \ + .dio_multi_pin = SX127X_PARAM_DIO_MULTI,\ + .paselect = SX127X_PARAM_PASELECT } +#else +#define SX127X_PARAMS { .spi = SX127X_PARAM_SPI, \ + .nss_pin = SX127X_PARAM_SPI_NSS, \ + .reset_pin = SX127X_PARAM_RESET, \ + .dio0_pin = SX127X_PARAM_DIO0, \ + .dio1_pin = SX127X_PARAM_DIO1, \ + .dio2_pin = SX127X_PARAM_DIO2, \ + .dio3_pin = SX127X_PARAM_DIO3, \ + .paselect = SX127X_PARAM_PASELECT } +#endif #endif /**@}*/ diff --git a/drivers/sx127x/sx127x.c b/drivers/sx127x/sx127x.c index e7347d2d0e348eab43c24ca04cf5836dc42ca872..87dfa797cde4007c230dd1d34a9708a7abde11d7 100644 --- a/drivers/sx127x/sx127x.c +++ b/drivers/sx127x/sx127x.c @@ -48,11 +48,14 @@ static void _on_tx_timeout(void *arg); static void _on_rx_timeout(void *arg); /* SX127X DIO interrupt handlers initialization */ +#ifndef SX127X_USE_DIO_MULTI static void sx127x_on_dio0_isr(void *arg); static void sx127x_on_dio1_isr(void *arg); static void sx127x_on_dio2_isr(void *arg); static void sx127x_on_dio3_isr(void *arg); - +#else +static void sx127x_on_dio_multi_isr(void *arg); +#endif void sx127x_setup(sx127x_t *dev, const sx127x_params_t *params) { @@ -61,7 +64,7 @@ void sx127x_setup(sx127x_t *dev, const sx127x_params_t *params) memcpy(&dev->params, params, sizeof(sx127x_params_t)); } -void sx127x_reset(const sx127x_t *dev) +int sx127x_reset(const sx127x_t *dev) { /* * This reset scheme complies with 7.2 chapter of the SX1272/1276 datasheet @@ -72,6 +75,13 @@ void sx127x_reset(const sx127x_t *dev) * 2. Set NReset in Hi-Z state * 3. Wait at least 5 milliseconds */ + + /* Check if the reset pin is defined */ + if (dev->params.reset_pin == GPIO_UNDEF) { + DEBUG("[sx127x] error: No reset pin defined.\n"); + return -SX127X_ERR_GPIOS; + } + gpio_init(dev->params.reset_pin, GPIO_OUT); /* Set reset pin to 0 */ @@ -85,6 +95,8 @@ void sx127x_reset(const sx127x_t *dev) /* Wait 10 ms */ xtimer_usleep(1000 * 10); + + return 0; } int sx127x_init(sx127x_t *dev) @@ -187,6 +199,7 @@ static void sx127x_on_dio_isr(sx127x_t *dev, sx127x_flags_t flag) sx127x_isr((netdev_t *)dev); } +#ifndef SX127X_USE_DIO_MULTI static void sx127x_on_dio0_isr(void *arg) { sx127x_on_dio_isr((sx127x_t*) arg, SX127X_IRQ_DIO0); @@ -206,37 +219,81 @@ static void sx127x_on_dio3_isr(void *arg) { sx127x_on_dio_isr((sx127x_t*) arg, SX127X_IRQ_DIO3); } +#else +static void sx127x_on_dio_multi_isr(void *arg) +{ + sx127x_on_dio_isr((sx127x_t*) arg, SX127X_IRQ_DIO_MULTI); +} +#endif /* Internal event handlers */ static int _init_gpios(sx127x_t *dev) { - int res = gpio_init_int(dev->params.dio0_pin, GPIO_IN, GPIO_RISING, - sx127x_on_dio0_isr, dev); - if (res < 0) { - DEBUG("[sx127x] error: failed to initialize DIO0 pin\n"); - return res; + int res; + +#ifndef SX127X_USE_DIO_MULTI + /* Check if DIO0 pin is defined */ + if (dev->params.dio0_pin != GPIO_UNDEF) { + res = gpio_init_int(dev->params.dio0_pin, GPIO_IN, GPIO_RISING, + sx127x_on_dio0_isr, dev); + if (res < 0) { + DEBUG("[sx127x] error: failed to initialize DIO0 pin\n"); + return res; + } + } + else { + DEBUG("[sx127x] error: no DIO0 pin defined\n"); + DEBUG("[sx127x] error: at least one interrupt should be defined\n"); + return SX127X_ERR_GPIOS; } - res = gpio_init_int(dev->params.dio1_pin, GPIO_IN, GPIO_RISING, - sx127x_on_dio1_isr, dev); - if (res < 0) { - DEBUG("[sx127x] error: failed to initialize DIO1 pin\n"); - return res; + /* Check if DIO1 pin is defined */ + if (dev->params.dio1_pin != GPIO_UNDEF) { + res = gpio_init_int(dev->params.dio1_pin, GPIO_IN, GPIO_RISING, + sx127x_on_dio1_isr, dev); + if (res < 0) { + DEBUG("[sx127x] error: failed to initialize DIO1 pin\n"); + return res; + } } - res = gpio_init_int(dev->params.dio2_pin, GPIO_IN, GPIO_RISING, - sx127x_on_dio2_isr, dev); - if (res < 0) { - DEBUG("[sx127x] error: failed to initialize DIO2 pin\n"); - return res; + /* check if DIO2 pin is defined */ + if (dev->params.dio2_pin != GPIO_UNDEF) { + res = gpio_init_int(dev->params.dio2_pin, GPIO_IN, GPIO_RISING, + sx127x_on_dio2_isr, dev); + if (res < 0) { + DEBUG("[sx127x] error: failed to initialize DIO2 pin\n"); + return res; + } } - res = gpio_init_int(dev->params.dio3_pin, GPIO_IN, GPIO_RISING, - sx127x_on_dio3_isr, dev); - if (res < 0) { - DEBUG("[sx127x] error: failed to initialize DIO3 pin\n"); - return res; + /* check if DIO3 pin is defined */ + if (dev->params.dio3_pin != GPIO_UNDEF) { + res = gpio_init_int(dev->params.dio3_pin, GPIO_IN, GPIO_RISING, + sx127x_on_dio3_isr, dev); + if (res < 0) { + DEBUG("[sx127x] error: failed to initialize DIO3 pin\n"); + return res; + } + } +#else + if (dev->params.dio_multi_pin != GPIO_UNDEF) { + DEBUG("[sx127x] info: Trying to initialize DIO MULTI pin\n"); + res = gpio_init_int(dev->params.dio_multi_pin, GPIO_IN, GPIO_RISING, + sx127x_on_dio_multi_isr, dev); + if (res < 0) { + DEBUG("[sx127x] error: failed to initialize DIO MULTI pin\n"); + return res; + } + + DEBUG("[sx127x] info: DIO MULTI pin initialized successfully\n"); } + else { + DEBUG("[sx127x] error: no DIO MULTI pin defined\n"); + DEBUG("[sx127x] error at least one interrupt should be defined\n"); + return SX127X_ERR_GPIOS; + } +#endif return res; } diff --git a/drivers/sx127x/sx127x_netdev.c b/drivers/sx127x/sx127x_netdev.c index 597f17b3f671606d052fee2a0d347fe7942d3319..84d22c38b4d1d4673306e659b094f491870a6bb5 100644 --- a/drivers/sx127x/sx127x_netdev.c +++ b/drivers/sx127x/sx127x_netdev.c @@ -240,6 +240,37 @@ static void _isr(netdev_t *netdev) sx127x_t *dev = (sx127x_t *) netdev; uint8_t irq = dev->irq; + +#ifdef SX127X_USE_DIO_MULTI + /* if the IRQ is from an OR'd pin check the actual IRQ on the registers */ + if (irq == SX127X_IRQ_DIO_MULTI) { + uint8_t interruptReg = sx127x_reg_read(dev, SX127X_REG_LR_IRQFLAGS); + + switch (interruptReg) { + case SX127X_RF_LORA_IRQFLAGS_TXDONE: + case SX127X_RF_LORA_IRQFLAGS_RXDONE: + irq = SX127X_IRQ_DIO0; + break; + + case SX127X_RF_LORA_IRQFLAGS_RXTIMEOUT: + irq = SX127X_IRQ_DIO1; + break; + + case SX127X_RF_LORA_IRQFLAGS_FHSSCHANGEDCHANNEL: + irq = SX127X_IRQ_DIO2; + break; + + case SX127X_RF_LORA_IRQFLAGS_CADDETECTED: + case SX127X_RF_LORA_IRQFLAGS_CADDONE: + irq = SX127X_IRQ_DIO3; + break; + + default: + break; + } + } +#endif + dev->irq = 0; switch (irq) {