From 1b5addd1fdf3c1ed04562f0caab3f18107d95df6 Mon Sep 17 00:00:00 2001 From: Aymeric Brochier Date: Tue, 17 May 2022 15:33:08 +0200 Subject: [PATCH] pkg/lorabasics: initial import --- pkg/Kconfig | 1 + pkg/lorabasics/Kconfig | 33 +++++ pkg/lorabasics/Makefile | 23 +++ pkg/lorabasics/Makefile.dep | 11 ++ pkg/lorabasics/Makefile.include | 11 ++ pkg/lorabasics/doc.txt | 13 ++ pkg/lorabasics/driver_sx1280_hal/Makefile | 3 + .../driver_sx1280_hal/driver_sx1280_hal.c | 136 ++++++++++++++++++ ...s-remove-const-from-radio_type-tcxo_.patch | Bin 0 -> 758 bytes .../0002-treewide-fix-include-paths.patch | Bin 0 -> 1859 bytes 10 files changed, 231 insertions(+) create mode 100644 pkg/lorabasics/Kconfig create mode 100644 pkg/lorabasics/Makefile create mode 100644 pkg/lorabasics/Makefile.dep create mode 100644 pkg/lorabasics/Makefile.include create mode 100644 pkg/lorabasics/doc.txt create mode 100644 pkg/lorabasics/driver_sx1280_hal/Makefile create mode 100644 pkg/lorabasics/driver_sx1280_hal/driver_sx1280_hal.c create mode 100644 pkg/lorabasics/patches/0001-smtc_ral-ral_defs-remove-const-from-radio_type-tcxo_.patch create mode 100644 pkg/lorabasics/patches/0002-treewide-fix-include-paths.patch 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 0000000000000000000000000000000000000000..894847738da4cd69e0b03c37309466d60069b44f GIT binary patch literal 758 zcmaKpQES^U5Xax|Q{3Yk#g434cIu3gvJv(&7<6x=7+IE56~_*eQc}8o_elv(pld2@ zp*#Kc)9HU}J8ZF>^95CG-Y9;n`GO1Da-LDjbgpGn%9dxW;6;W%h92+L4zm&|t&Sfu zN;6moOoeMJdTE>-g0M5az|UH3kE_*Qrn372--K6H#7r^AyPXZE8AH0LSW)E^2@N#3 z-#1T6`U-#ly!-X_`vbbIm$en$Ec|t=w5!kt@UucW^vkFcUOL+xL7YUbr(sHNUu z)B+?)0vCp}^2|ve@lPm(kf_K|n=T+9MZZz)6d9V{DeFzR;}f}wiB@&$6el+Tt5 zTY?$ryxmJ5yz1(cNkgw|2k-5N>?>d#CzGEC(@u`@IEB1d2fu{mqKf28CtMxx8vF8} oaO_i1g)v4;ATj)ybe4>#-ZJp->6J@Bz}aLH3`Jn+oTU`tA2?X-pa1{> literal 0 HcmV?d00001 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 0000000000000000000000000000000000000000..ffdce570ae61b1ac33cbf5889cd8f538b1acb643 GIT binary patch literal 1859 zcmbVMYirvu6#cGWahF1A>e!a$XF@3{ZOZmxrH~B@V+3206K#ndWVg+rKYo&9H!r8_ zYK#)!$I;cf=jvAG3m_p$6PNM8@%w4mXOV+(L>%tL0cUPXnd|qR&`sfMp23(e!3hE5 zLG=Q`*g>}nW&pP`&ZsD8t^(&Gi{T>Wi|XI-ky>U{Xr38HZ(D&I*J~(?n1#N;;A8 z+%6=w)vraNd0rp@!!TOICdJAj5!TT!K)}#Qm1V-Dc;d2xdyUyfa#bmFUI3lN*_5*$ zgsQHB%TnZ7ab|S2f$=$Cf16!|m`GDF&8a9Mwp;2=T8v0!jIY4=)5Ia6Wnr4cA*P^u z6nLIKdMg%E=Y>|h;SfyM>qTHx>u`tw&Q(<=+>Gu=cQ>QE>o23RHhlQeh&BRbo-~c> z=!VQXdzQ&;Z8bJ=2dt>1H{1;ZAUF}JqB|8CXJ;^ea7gd~U2U$lqD!cn9jtUEng%cI zgxrr|I&40I@*@JzL?Ws3-_Ep|`Y=;ZBf*#uNG>;{K%uRWpT&9QnHqiZY^{V@|)v=4Blp>p2vr7Qmh6P{H<)2*zZW!Oz)3H&n8gn2Wbx&vq0{eWA zv12$DWk;PgXUydxb0Rg|NziA$zsK3}^qREe0TpY0udi6s>w4JrXl}&bPTiKM>+7(d s)kBBm(_(M7|BLHphTqYaNwe2GVmF