diff --git a/pkg/Kconfig b/pkg/Kconfig index 2de9bb54d2..8bc7e16e6f 100644 --- a/pkg/Kconfig +++ b/pkg/Kconfig @@ -38,6 +38,7 @@ rsource "libcose/Kconfig" rsource "libfixmath/Kconfig" rsource "libhydrogen/Kconfig" rsource "littlefs2/Kconfig" +rsource "lorabasics/Kconfig" rsource "lora-serialization/Kconfig" rsource "lua/Kconfig" rsource "lv_drivers/Kconfig" diff --git a/pkg/lorabasics/Kconfig b/pkg/lorabasics/Kconfig new file mode 100644 index 0000000000..aeeb28828d --- /dev/null +++ b/pkg/lorabasics/Kconfig @@ -0,0 +1,33 @@ +# Copyright (c) 2022 Inria +# +# This file is subject to the terms and conditions of the GNU Lesser +# General Public License v2.1. See the file LICENSE in the top level +# directory for more details. + +config PACKAGE_LORABASICS + bool "LoRa Basics" + depends on TEST_KCONFIG + # depends on HAS_ARCH_32_BIT + select MODULE_LORABASICS_SMTC_RAL + +if PACKAGE_LORABASICS + +config MODULE_LORABASICS_SX1280_DRIVER + bool "LoRaBasics SX1280 driver code" + depends on HAS_PERIPH_SPI + depends on HAS_PERIPH_GPIO + depends on HAS_PERIPH_GPIO_IRQ + select MODULE_PERIPH_SPI + select MODULE_PERIPH_GPIO + select MODULE_PERIPH_GPIO_IRQ + select MODULE_ZTIMER + select MODULE_ZTIMER_MSEC + select MODULE_LORABASICS_DRIVER_SX1280_HAL + +config MODULE_LORABASICS_DRIVER_SX1280_HAL + bool "LoRaBasicsModem SX1280 driver hal" + +config MODULE_LORABASICS_SMTC_RAL + bool "LoRaBasicsModem Radio Abstraction Layer (RAL)" + +endif # PACKAGE_LORABASICS diff --git a/pkg/lorabasics/Makefile b/pkg/lorabasics/Makefile new file mode 100644 index 0000000000..282eeaa8b2 --- /dev/null +++ b/pkg/lorabasics/Makefile @@ -0,0 +1,23 @@ +PKG_NAME=lorabasicsmodem +PKG_URL=https://github.com/lorabasics/lorabasicsmodem.git +PKG_VERSION=04e415bcfbdc7f1f4bff918a4867fab53bc8bb8a +PKG_LICENSE=BSD + +include $(RIOTBASE)/pkg/pkg.mk + +CFLAGS += -Wno-error=unused-parameter +CFLAGS += -Wno-error=unused-function + +IGNORE_MODULES := lorabasics_driver_sx1280_hal + # + +LORABASICS_MODULES := $(filter-out $(IGNORE_MODULES),$(filter lorabasics%,$(USEMODULE))) + +all: $(LORABASICS_MODULES) + @true + +lorabasics_smtc_ral: + $(QQ)"$(MAKE)" -C $(PKG_SOURCE_DIR)/smtc_ral/src -f $(RIOTBASE)/Makefile.base MODULE=$@ SRC="ral.c ral_sx1280.c" + +lorabasics_sx1280_driver: + $(QQ)"$(MAKE)" -C $(PKG_SOURCE_DIR)/sx1280_driver/src -f $(RIOTBASE)/Makefile.base MODULE=$@ diff --git a/pkg/lorabasics/Makefile.dep b/pkg/lorabasics/Makefile.dep new file mode 100644 index 0000000000..08222d5ccd --- /dev/null +++ b/pkg/lorabasics/Makefile.dep @@ -0,0 +1,11 @@ +ifneq (,$(filter lorabasics_sx1280_driver,$(USEMODULE))) + FEATURES_REQUIRED += periph_gpio_irq + FEATURES_REQUIRED += periph_spi + + USEMODULE += ztimer_msec + + USEMODULE += lorabasics_driver_sx1280_hal +endif +# This package has assumptions that only work for 32-bit architectures +FEATURES_REQUIRED += arch_32bit +USEMODULE += lorabasics_smtc_ral diff --git a/pkg/lorabasics/Makefile.include b/pkg/lorabasics/Makefile.include new file mode 100644 index 0000000000..ff41b54cc7 --- /dev/null +++ b/pkg/lorabasics/Makefile.include @@ -0,0 +1,11 @@ +INCLUDES += -I$(PKGDIRBASE)/lorabasicsmodem \ + # + +ifneq (,$(filter lorabasics_driver_sx1280_hal,$(USEMODULE))) + DIRS += $(RIOTBASE)/pkg/lorabasics/driver_sx1280_hal + + # In the lorabasics package setting this define includes RAL code for the sx1280 radio + CFLAGS += -DSX1280 +endif + +PSEUDOMODULES += lorabasics diff --git a/pkg/lorabasics/doc.txt b/pkg/lorabasics/doc.txt new file mode 100644 index 0000000000..d9a8fe1d3a --- /dev/null +++ b/pkg/lorabasics/doc.txt @@ -0,0 +1,13 @@ +/** + * @defgroup pkg_lorabasicsmodem LoRa Basics + * @ingroup pkg + * @brief LoRa Basics Modem + * + * # HAL implementation for the SX1280 LoRa radio driver and vendor code for SX1280 + * + * # License + * + * Licensed under BSD. + * + * @see git@github.com:lorabasics/lorabasicsmodem.git + */ diff --git a/pkg/lorabasics/driver_sx1280_hal/Makefile b/pkg/lorabasics/driver_sx1280_hal/Makefile new file mode 100644 index 0000000000..e93476024b --- /dev/null +++ b/pkg/lorabasics/driver_sx1280_hal/Makefile @@ -0,0 +1,3 @@ +MODULE = lorabasics_driver_sx1280_hal + +include $(RIOTBASE)/Makefile.base diff --git a/pkg/lorabasics/driver_sx1280_hal/driver_sx1280_hal.c b/pkg/lorabasics/driver_sx1280_hal/driver_sx1280_hal.c new file mode 100644 index 0000000000..ecf0059495 --- /dev/null +++ b/pkg/lorabasics/driver_sx1280_hal/driver_sx1280_hal.c @@ -0,0 +1,136 @@ +/* + * Copyright (C) 2022 Inria + * + * This file is subject to the terms and conditions of the GNU Lesser + * General Public License v2.1. See the file LICENSE in the top level + * directory for more details. + */ + +/** + * @ingroup pkg_lorabasicsmodem + * @{ + * + * @file + * @brief HAL implementation for the SX1280 LoRa radio driver + * + * @author Francisco Molina + * @author Nicolas Albarel + * @author Didier Donsez + * @author Olivier Alphand + * @author Aymeric Brochier + * + * @} + */ + +#include +#include +#include "irq.h" +#include "periph/spi.h" +#include "ztimer.h" + +#include "sx1280.h" +#include "sx1280_constants.h" +#include "sx1280_driver/src/sx1280_hal.h" + +#define ENABLE_DEBUG 0 +#include "debug.h" + +/** + * @brief Wait until radio busy pin is reset to 0 + */ +static void sx1280_hal_wait_on_busy(const void *context) +{ + sx1280_t *dev = (sx1280_t *)context; + + while (gpio_read(dev->params->dio0_pin)) {} +} + +sx1280_hal_status_t sx1280_hal_write(const void *context, const uint8_t *command, + const uint16_t command_length, + const uint8_t *data, const uint16_t data_length) +{ + sx1280_t *dev = (sx1280_t *)context; + + sx1280_hal_wakeup(context); + + DEBUG("[sx1280_hal_write]: command_length=%d data_length=%d\n", command_length, data_length); + + spi_acquire(dev->params->spi, SPI_CS_UNDEF, dev->params->spi_mode, dev->params->spi_clk); + spi_transfer_bytes(dev->params->spi, dev->params->nss_pin, data_length != 0, + command, NULL, command_length); + if (data_length) { + spi_transfer_bytes(dev->params->spi, dev->params->nss_pin, false, data, + NULL, data_length); + } + spi_release(dev->params->spi); + + /* 0x84 - SX1280_SET_SLEEP opcode. In sleep mode the radio dio is stuck + to 1 => do not test it */ + if (command[0] != 0x84) { + sx1280_hal_wait_on_busy(context); + } + return SX1280_HAL_STATUS_OK; +} + +sx1280_hal_status_t sx1280_hal_read(const void *context, const uint8_t *command, + const uint16_t command_length, + uint8_t *data, const uint16_t data_length) +{ + sx1280_t *dev = (sx1280_t *)context; + + sx1280_hal_wakeup(context); + + spi_acquire(dev->params->spi, SPI_CS_UNDEF, dev->params->spi_mode, dev->params->spi_clk); + spi_transfer_bytes(dev->params->spi, dev->params->nss_pin, true, \ + command, NULL, command_length); + spi_transfer_bytes(dev->params->spi, dev->params->nss_pin, false, \ + NULL, data, data_length); + spi_release(dev->params->spi); + + return SX1280_HAL_STATUS_OK; +} + +void sx1280_hal_reset( const void *context ) +{ + sx1280_t *dev = (sx1280_t *)context; + + gpio_clear(dev->params->reset_pin); + ztimer_sleep(ZTIMER_MSEC, SX1280_RESET_MS); + gpio_set(dev->params->reset_pin); + ztimer_sleep(ZTIMER_MSEC, SX1280_WAKEUP_TIME_MS); +} + +sx1280_hal_status_t sx1280_hal_wakeup(const void *context) +{ + sx1280_t *dev = (sx1280_t *)context; + + if (dev->mode == SX1280_HAL_OP_MODE_SLEEP) { + /* Busy is HIGH in sleep mode, wake-up the device */ + gpio_clear(dev->params->nss_pin); + sx1280_hal_wait_on_busy(context); + gpio_set(dev->params->nss_pin); + + /* Radio is awake in STDBY_RC mode */ + dev->mode = SX1280_HAL_OP_MODE_STDBY_RC; + } + else { + /* if the radio is awake, just wait until busy pin get low */ + sx1280_hal_wait_on_busy(context); + } + + return SX1280_HAL_STATUS_OK; +} + +sx1280_hal_operating_mode_t sx1280_hal_get_operating_mode(const void *context) +{ + sx1280_t *dev = (sx1280_t *)context; + + return dev->mode; +} + +void sx1280_hal_set_operating_mode(const void *context, const sx1280_hal_operating_mode_t op_mode) +{ + sx1280_t *dev = (sx1280_t *)context; + + dev->mode = op_mode; +} diff --git a/pkg/lorabasics/patches/0001-smtc_ral-ral_defs-remove-const-from-radio_type-tcxo_.patch b/pkg/lorabasics/patches/0001-smtc_ral-ral_defs-remove-const-from-radio_type-tcxo_.patch new file mode 100644 index 0000000000..894847738d Binary files /dev/null and b/pkg/lorabasics/patches/0001-smtc_ral-ral_defs-remove-const-from-radio_type-tcxo_.patch differ diff --git a/pkg/lorabasics/patches/0002-treewide-fix-include-paths.patch b/pkg/lorabasics/patches/0002-treewide-fix-include-paths.patch new file mode 100644 index 0000000000..ffdce570ae Binary files /dev/null and b/pkg/lorabasics/patches/0002-treewide-fix-include-paths.patch differ