diff --git a/boards/xtensa/esp32/ttgo_lora_esp32/configs/sx127x/defconfig b/boards/xtensa/esp32/ttgo_lora_esp32/configs/sx127x/defconfig new file mode 100644 index 0000000000..e8e8f7199d --- /dev/null +++ b/boards/xtensa/esp32/ttgo_lora_esp32/configs/sx127x/defconfig @@ -0,0 +1,63 @@ +# +# 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_ESP32_FLASH_DETECT is not set +# CONFIG_NSH_ARGCAT is not set +# CONFIG_NSH_CMDOPT_HEXDUMP is not set +# CONFIG_NSH_CMDPARMS is not set +CONFIG_ARCH="xtensa" +CONFIG_ARCH_BOARD="ttgo_lora_esp32" +CONFIG_ARCH_BOARD_COMMON=y +CONFIG_ARCH_BOARD_TTGO_LORA_ESP32=y +CONFIG_ARCH_CHIP="esp32" +CONFIG_ARCH_CHIP_ESP32=y +CONFIG_ARCH_CHIP_ESP32WROOM32=y +CONFIG_ARCH_STACKDUMP=y +CONFIG_ARCH_XTENSA=y +CONFIG_BOARD_LOOPSPERMSEC=16717 +CONFIG_BUILTIN=y +CONFIG_DRIVERS_LPWAN=y +CONFIG_DRIVERS_WIRELESS=y +CONFIG_ESP32_DEFAULT_CPU_FREQ_80=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_ESP32_XTAL_26MHz=y +CONFIG_EXAMPLES_SX127X=y +CONFIG_EXAMPLES_SX127X_RFFREQ=868000000 +CONFIG_FS_PROCFS=y +CONFIG_HAVE_CXX=y +CONFIG_HAVE_CXXINITIALIZE=y +CONFIG_IDLETHREAD_STACKSIZE=3072 +CONFIG_INIT_ENTRYPOINT="nsh_main" +CONFIG_INTELHEX_BINARY=y +CONFIG_LPWAN_SX127X=y +CONFIG_LPWAN_SX127X_FSKOOK=y +CONFIG_LPWAN_SX127X_RFFREQ_DEFAULT=868000000 +CONFIG_LPWAN_SX127X_RXSUPPORT=y +CONFIG_LPWAN_SX127X_TXSUPPORT=y +CONFIG_MM_REGIONS=3 +CONFIG_NSH_ARCHINIT=y +CONFIG_NSH_BUILTIN_APPS=y +CONFIG_NSH_FILEIOSIZE=512 +CONFIG_NSH_LINELEN=64 +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_SYSTEM_NSH=y +CONFIG_UART0_SERIAL_CONSOLE=y diff --git a/boards/xtensa/esp32/ttgo_lora_esp32/src/Make.defs b/boards/xtensa/esp32/ttgo_lora_esp32/src/Make.defs index 7c43f7fa27..d4e04843c6 100644 --- a/boards/xtensa/esp32/ttgo_lora_esp32/src/Make.defs +++ b/boards/xtensa/esp32/ttgo_lora_esp32/src/Make.defs @@ -47,6 +47,10 @@ ifeq ($(CONFIG_ARCH_BUTTONS),y) CSRCS += esp32_buttons.c endif +ifeq ($(CONFIG_LPWAN_SX127X),y) +CSRCS += esp32_sx127x.c +endif + SCRIPTOUT = $(BOARD_DIR)$(DELIM)scripts$(DELIM)esp32_out.ld .PHONY = context distclean diff --git a/boards/xtensa/esp32/ttgo_lora_esp32/src/esp32_bringup.c b/boards/xtensa/esp32/ttgo_lora_esp32/src/esp32_bringup.c index 3a72792c2e..99cbd4e6d9 100644 --- a/boards/xtensa/esp32/ttgo_lora_esp32/src/esp32_bringup.c +++ b/boards/xtensa/esp32/ttgo_lora_esp32/src/esp32_bringup.c @@ -200,6 +200,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 */ + #ifdef CONFIG_ESP32_BLE ret = esp32_ble_initialize(); if (ret) diff --git a/boards/xtensa/esp32/ttgo_lora_esp32/src/esp32_sx127x.c b/boards/xtensa/esp32/ttgo_lora_esp32/src/esp32_sx127x.c new file mode 100644 index 0000000000..ab565983f2 --- /dev/null +++ b/boards/xtensa/esp32/ttgo_lora_esp32/src/esp32_sx127x.c @@ -0,0 +1,227 @@ +/**************************************************************************** + * boards/xtensa/esp32/ttgo_lora_esp32/src/esp32_sx127x.c + * + * 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 + +#include +#include +#include +#include +#include + +#include +#include +#include +#include + +#include "esp32_gpio.h" +#include "esp32_spi.h" +#include "hardware/esp32_gpio_sigmap.h" + +#include "ttgo_lora_esp32.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 */ + + nxsig_usleep(1000); + + /* Set pin to high */ + + esp32_gpiowrite(GPIO_SX127X_RESET, 1); + + /* Wait 10 ms */ + + nxsig_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/ttgo_lora_esp32/src/ttgo_lora_esp32.h b/boards/xtensa/esp32/ttgo_lora_esp32/src/ttgo_lora_esp32.h index 1638fc1e2b..4132cbd20b 100644 --- a/boards/xtensa/esp32/ttgo_lora_esp32/src/ttgo_lora_esp32.h +++ b/boards/xtensa/esp32/ttgo_lora_esp32/src/ttgo_lora_esp32.h @@ -46,6 +46,11 @@ #define GPIO_SSD1306_RST 16 +/* SX1276 pins */ + +#define GPIO_SX127X_RESET 23 /* RESET connected to IO23 */ +#define GPIO_SX127X_DIO0 26 /* DIO0 connected to IO26 */ + /* BOOT Button */ #define BUTTON_BOOT 0 @@ -148,5 +153,24 @@ int esp32_gpio_init(void); int board_spidev_initialize(int bus); #endif +/**************************************************************************** + * 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_TTGO_LORA_ESP32_SRC_TTGO_LORA_ESP32_H */