This is an automated email from the ASF dual-hosted git repository. xiaoxiang pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/nuttx.git
commit e20d4912ff9a226c4fe18910aeb9e01235b1d448 Author: Alan Carvalho de Assis <[email protected]> AuthorDate: Sun Dec 21 16:30:18 2025 -0300 boards/esp32/heltec_wifi_lora32: Add support to SX1276 This commit adds support to SX1276 transceiver on Heltec WiFi LoRa32 board. Signed-off-by: Alan C. Assis <[email protected]> --- .../heltec_wifi_lora32/configs/sx1276/defconfig | 68 ++++++ .../xtensa/esp32/heltec_wifi_lora32/src/Make.defs | 4 + .../esp32/heltec_wifi_lora32/src/esp32_bringup.c | 10 + .../esp32/heltec_wifi_lora32/src/esp32_sx127x.c | 229 +++++++++++++++++++++ .../heltec_wifi_lora32/src/heltec_wifi_lora32.h | 24 +++ 5 files changed, 335 insertions(+) diff --git a/boards/xtensa/esp32/heltec_wifi_lora32/configs/sx1276/defconfig b/boards/xtensa/esp32/heltec_wifi_lora32/configs/sx1276/defconfig new file mode 100644 index 00000000000..160a52951af --- /dev/null +++ b/boards/xtensa/esp32/heltec_wifi_lora32/configs/sx1276/defconfig @@ -0,0 +1,68 @@ +# +# This file is autogenerated: PLEASE DO NOT EDIT IT. +# +# You can use "make menuconfig" to make any modifications to the installed .config file. +# You can then do "make savedefconfig" to generate a new defconfig file that includes your +# modifications. +# +# CONFIG_ARCH_LEDS is not set +# CONFIG_LPWAN_SX127X_LORA is not set +# CONFIG_NSH_ARGCAT is not set +# CONFIG_NSH_CMDOPT_HEXDUMP is not set +CONFIG_ARCH="xtensa" +CONFIG_ARCH_BOARD="heltec_wifi_lora32" +CONFIG_ARCH_BOARD_COMMON=y +CONFIG_ARCH_BOARD_HELTEC_WIFI_LORA32=y +CONFIG_ARCH_CHIP="esp32" +CONFIG_ARCH_CHIP_ESP32=y +CONFIG_ARCH_CHIP_ESP32WROVER=y +CONFIG_ARCH_STACKDUMP=y +CONFIG_ARCH_XTENSA=y +CONFIG_BOARD_LOOPSPERMSEC=16717 +CONFIG_BUILTIN=y +CONFIG_DEBUG_FEATURES=y +CONFIG_DEBUG_LIB=y +CONFIG_DRIVERS_LPWAN=y +CONFIG_DRIVERS_WIRELESS=y +CONFIG_ESP32_IGNORE_CHIP_REVISION_CHECK=y +CONFIG_ESP32_SPI2=y +CONFIG_ESP32_SPI2_CLKPIN=5 +CONFIG_ESP32_SPI2_CSPIN=18 +CONFIG_ESP32_SPI2_MISOPIN=19 +CONFIG_ESP32_SPI2_MOSIPIN=27 +CONFIG_ESP32_UART0=y +CONFIG_EXAMPLES_SX127X=y +CONFIG_EXAMPLES_SX127X_RFFREQ=915000000 +CONFIG_EXAMPLES_SX127X_TXDATA=63 +CONFIG_EXAMPLES_SX127X_TXPOWER=20 +CONFIG_FS_PROCFS=y +CONFIG_HAVE_CXX=y +CONFIG_HAVE_CXXINITIALIZE=y +CONFIG_IDLETHREAD_STACKSIZE=3072 +CONFIG_INIT_ENTRYPOINT="nsh_main" +CONFIG_INIT_STACKSIZE=3072 +CONFIG_INTELHEX_BINARY=y +CONFIG_LINE_MAX=64 +CONFIG_LPWAN_SX127X=y +CONFIG_LPWAN_SX127X_FSKOOK=y +CONFIG_LPWAN_SX127X_RFFREQ_DEFAULT=915000000 +CONFIG_LPWAN_SX127X_RXSUPPORT=y +CONFIG_LPWAN_SX127X_TXPOWER_DEFAULT=20 +CONFIG_LPWAN_SX127X_TXSUPPORT=y +CONFIG_MM_REGIONS=3 +CONFIG_NSH_ARCHINIT=y +CONFIG_NSH_BUILTIN_APPS=y +CONFIG_NSH_FILEIOSIZE=512 +CONFIG_NSH_READLINE=y +CONFIG_PREALLOC_TIMERS=4 +CONFIG_RAM_SIZE=114688 +CONFIG_RAM_START=0x20000000 +CONFIG_RR_INTERVAL=200 +CONFIG_SCHED_HPWORK=y +CONFIG_SCHED_WAITPID=y +CONFIG_START_DAY=6 +CONFIG_START_MONTH=12 +CONFIG_START_YEAR=2011 +CONFIG_SYSLOG_BUFFER=y +CONFIG_SYSTEM_NSH=y +CONFIG_UART0_SERIAL_CONSOLE=y diff --git a/boards/xtensa/esp32/heltec_wifi_lora32/src/Make.defs b/boards/xtensa/esp32/heltec_wifi_lora32/src/Make.defs index 414b3abd72c..84953ece1ac 100644 --- a/boards/xtensa/esp32/heltec_wifi_lora32/src/Make.defs +++ b/boards/xtensa/esp32/heltec_wifi_lora32/src/Make.defs @@ -26,6 +26,10 @@ ifeq ($(CONFIG_BOARDCTL),y) CSRCS += esp32_appinit.c endif +ifeq ($(CONFIG_LPWAN_SX127X),y) +CSRCS += esp32_sx127x.c +endif + DEPPATH += --dep-path board VPATH += :board CFLAGS += ${INCDIR_PREFIX}$(TOPDIR)$(DELIM)arch$(DELIM)$(CONFIG_ARCH)$(DELIM)src$(DELIM)board$(DELIM)board diff --git a/boards/xtensa/esp32/heltec_wifi_lora32/src/esp32_bringup.c b/boards/xtensa/esp32/heltec_wifi_lora32/src/esp32_bringup.c index a8d4210ab04..b4008c08d04 100644 --- a/boards/xtensa/esp32/heltec_wifi_lora32/src/esp32_bringup.c +++ b/boards/xtensa/esp32/heltec_wifi_lora32/src/esp32_bringup.c @@ -81,6 +81,16 @@ int esp32_bringup(void) } #endif +#ifdef CONFIG_LPWAN_SX127X + ret = esp32_lpwaninitialize(); + if (ret < 0) + { + syslog(LOG_ERR, + "ERROR: Failed to initialize wireless driver: %d\n", + ret); + } +#endif /* CONFIG_LPWAN_SX127X */ + /* If we got here then perhaps not all initialization was successful, but * at least enough succeeded to bring-up NSH with perhaps reduced * capabilities. diff --git a/boards/xtensa/esp32/heltec_wifi_lora32/src/esp32_sx127x.c b/boards/xtensa/esp32/heltec_wifi_lora32/src/esp32_sx127x.c new file mode 100644 index 00000000000..b481c739634 --- /dev/null +++ b/boards/xtensa/esp32/heltec_wifi_lora32/src/esp32_sx127x.c @@ -0,0 +1,229 @@ +/**************************************************************************** + * boards/xtensa/esp32/heltec_wifi_lora32/src/esp32_sx127x.c + * + * SPDX-License-Identifier: Apache-2.0 + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. The + * ASF licenses this file to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the + * License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include <nuttx/config.h> + +#include <stdio.h> +#include <stdint.h> +#include <errno.h> +#include <assert.h> +#include <debug.h> + +#include <nuttx/board.h> +#include <nuttx/signal.h> +#include <nuttx/wireless/lpwan/sx127x.h> +#include <arch/board/board.h> + +#include "esp32_gpio.h" +#include "esp32_spi.h" +#include "hardware/esp32_gpio_sigmap.h" + +#include "heltec_wifi_lora32.h" + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +/* SX127X on SPI2 bus */ + +#define SX127X_SPI 2 + +/**************************************************************************** + * Private Function Prototypes + ****************************************************************************/ + +static void sx127x_chip_reset(void); +static int sx127x_opmode_change(int opmode); +static int sx127x_freq_select(uint32_t freq); +static int sx127x_pa_select(bool enable); +static int sx127x_irq0_attach(xcpt_t isr, void *arg); + +/**************************************************************************** + * Private Data + ****************************************************************************/ + +struct sx127x_lower_s lower = +{ + .irq0attach = sx127x_irq0_attach, + .reset = sx127x_chip_reset, + .opmode_change = sx127x_opmode_change, + .freq_select = sx127x_freq_select, + .pa_select = sx127x_pa_select, + .pa_force = true +}; + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: sx127x_irq0_attach + ****************************************************************************/ + +static int sx127x_irq0_attach(xcpt_t isr, void *arg) +{ + int irq = ESP32_PIN2IRQ(GPIO_SX127X_DIO0); + int ret; + + /* Make sure the interrupt is disabled */ + + esp32_gpioirqdisable(irq); + + wlinfo("Attach DIO0 IRQ\n"); + + /* Attach to IRQ on pin connected to DIO0 */ + + ret = irq_attach(irq, isr, arg); + if (ret < 0) + { + syslog(LOG_ERR, "ERROR: gpint_attach() failed: %d\n", ret); + return ret; + } + + /* IRQ on rising edge */ + + esp32_gpioirqenable(irq, RISING); + + return OK; +} + +/**************************************************************************** + * Name: sx127x_chip_reset + ****************************************************************************/ + +static void sx127x_chip_reset(void) +{ + wlinfo("SX127X RESET\n"); + + /* Configure reset as output */ + + esp32_gpio_matrix_out(GPIO_SX127X_RESET, SIG_GPIO_OUT_IDX, 0, 0); + esp32_configgpio(GPIO_SX127X_RESET, OUTPUT_FUNCTION_3 | INPUT_FUNCTION_3); + + /* Set pin to zero */ + + esp32_gpiowrite(GPIO_SX127X_RESET, 0); + + /* Wait 1 ms */ + + nxsched_usleep(1000); + + /* Set pin to high */ + + esp32_gpiowrite(GPIO_SX127X_RESET, 1); + + /* Wait 10 ms */ + + nxsched_usleep(10000); +} + +/**************************************************************************** + * Name: sx127x_opmode_change + ****************************************************************************/ + +static int sx127x_opmode_change(int opmode) +{ + /* Do nothing */ + + return OK; +} + +/**************************************************************************** + * Name: sx127x_freq_select + ****************************************************************************/ + +static int sx127x_freq_select(uint32_t freq) +{ + int ret = OK; + + /* NOTE: this depends on your module version */ + + if (freq > SX127X_HFBAND_THR) + { + ret = -EINVAL; + wlerr("HF band not supported\n"); + } + + return ret; +} + +/**************************************************************************** + * Name: sx127x_pa_select + ****************************************************************************/ + +static int sx127x_pa_select(bool enable) +{ + int ret = OK; + + /* Only PA_BOOST output connected to antenna */ + + if (enable == false) + { + ret = -EINVAL; + wlerr("Module supports only PA_BOOST pin," + " so PA_SELECT must be enabled!\n"); + } + + return ret; +} + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +int esp32_lpwaninitialize(void) +{ + struct spi_dev_s *spidev; + int ret = OK; + + wlinfo("Register the sx127x module\n"); + + /* Setup DIO0 */ + + esp32_configgpio(GPIO_SX127X_DIO0, INPUT_FUNCTION_3 | PULLDOWN); + + /* Init SPI bus */ + + spidev = esp32_spibus_initialize(SX127X_SPI); + if (!spidev) + { + wlerr("ERROR: Failed to initialize SPI %d bus\n", SX127X_SPI); + ret = -ENODEV; + goto errout; + } + + /* Initialize SX127X */ + + ret = sx127x_register(spidev, &lower); + if (ret < 0) + { + wlerr("ERROR: Failed to register sx127x\n"); + goto errout; + } + +errout: + return ret; +} diff --git a/boards/xtensa/esp32/heltec_wifi_lora32/src/heltec_wifi_lora32.h b/boards/xtensa/esp32/heltec_wifi_lora32/src/heltec_wifi_lora32.h index 471c809d81a..4fcec679609 100644 --- a/boards/xtensa/esp32/heltec_wifi_lora32/src/heltec_wifi_lora32.h +++ b/boards/xtensa/esp32/heltec_wifi_lora32/src/heltec_wifi_lora32.h @@ -37,6 +37,11 @@ /* Heltec WiFi LoRa32 GPIOs *************************************************/ +/* SX1276 pins */ + +#define GPIO_SX127X_RESET 14 /* RESET connected to IO14 */ +#define GPIO_SX127X_DIO0 26 /* DIO0 connected to IO26 */ + /* BOOT Button */ #define BUTTON_BOOT 0 @@ -111,5 +116,24 @@ int esp32_bringup(void); +/**************************************************************************** + * Name: esp32_lpwaninitialize + * + * Description: + * Initialize the SX127x driver + * + * Input Parameters: + * None + * + * Returned Value: + * Zero (OK) is returned on success; A negated errno value is returned + * to indicate the nature of any failure. + * + ****************************************************************************/ + +#ifdef CONFIG_LPWAN_SX127X +int esp32_lpwaninitialize(void); +#endif + #endif /* __ASSEMBLY__ */ #endif /* __BOARDS_XTENSA_ESP32_HELTEC_WIFI_LORA32_SRC_HELTEC_WIFI_LORA32_H */
