diff --git a/boards/Kconfig b/boards/Kconfig index 9b01091bef1..7ffd3b21584 100644 --- a/boards/Kconfig +++ b/boards/Kconfig @@ -249,6 +249,18 @@ config ARCH_BOARD_ESP32_WROVERKIT LCD screen MicroSD card slot +config ARCH_BOARD_TTGO_LORA_ESP32 + bool "TTGO LoRa SX1276 ESP32" + depends on ARCH_CHIP_ESP32WROOM32 + select ARCH_HAVE_LEDS + select ARCH_HAVE_BUTTONS + select ARCH_HAVE_IRQBUTTONS if ESP32_GPIO_IRQ + ---help--- + TTGO-LoRa-SX1276-ESP32 is an ESP32 board with LoRa. Usually it + comes with an OLED display, but there are options without + display. This port is for board version 1.0, more info: + https://github.com/Xinyuan-LilyGO/TTGO-LoRa-Series + config ARCH_BOARD_ESP32C3_DEVKIT bool "Espressif ESP32-C3 DevKit" depends on ARCH_CHIP_ESP32C3MINI1 || ARCH_CHIP_ESP32C3WROOM02 @@ -2528,6 +2540,7 @@ config ARCH_BOARD default "tm4c123g-launchpad" if ARCH_BOARD_TM4C123G_LAUNCHPAD default "tm4c1294-launchpad" if ARCH_BOARD_TM4C1294_LAUNCHPAD default "tms570ls31x-usb-kit" if ARCH_BOARD_TMS570LS31X_USB_KIT + default "ttgo_lora_esp32" if ARCH_BOARD_TTGO_LORA_ESP32 default "twr-k60n512" if ARCH_BOARD_TWR_K60N512 default "twr-k64f120m" if ARCH_BOARD_TWR_K64F120M default "u-blox-c027" if ARCH_BOARD_U_BLOX_C027 @@ -3242,6 +3255,9 @@ endif if ARCH_BOARD_ESP32_WROVERKIT source "boards/xtensa/esp32/esp32-wrover-kit/Kconfig" endif +if ARCH_BOARD_TTGO_LORA_ESP32 +source "boards/xtensa/esp32/ttgo_lora_esp32/Kconfig" +endif if ARCH_BOARD_Z16F2800100ZCOG source "boards/z16/z16f/z16f2800100zcog/Kconfig" endif diff --git a/boards/xtensa/esp32/ttgo_lora_esp32/Kconfig b/boards/xtensa/esp32/ttgo_lora_esp32/Kconfig new file mode 100644 index 00000000000..09beaacf8c4 --- /dev/null +++ b/boards/xtensa/esp32/ttgo_lora_esp32/Kconfig @@ -0,0 +1,8 @@ +# +# For a description of the syntax of this configuration file, +# see the file kconfig-language.txt in the NuttX tools repository. +# + +if ARCH_BOARD_TTGO_LORA_ESP32 + +endif # ARCH_BOARD_TTGO_LORA_ESP32 diff --git a/boards/xtensa/esp32/ttgo_lora_esp32/configs/nsh/defconfig b/boards/xtensa/esp32/ttgo_lora_esp32/configs/nsh/defconfig new file mode 100644 index 00000000000..d3117fa04b2 --- /dev/null +++ b/boards/xtensa/esp32/ttgo_lora_esp32/configs/nsh/defconfig @@ -0,0 +1,49 @@ +# +# 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_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_ESP32_DEFAULT_CPU_FREQ_80=y +CONFIG_ESP32_UART0=y +CONFIG_ESP32_XTAL_26MHz=y +CONFIG_FS_PROCFS=y +CONFIG_HAVE_CXX=y +CONFIG_HAVE_CXXINITIALIZE=y +CONFIG_IDLETHREAD_STACKSIZE=3072 +CONFIG_INTELHEX_BINARY=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_RAW_BINARY=y +CONFIG_RR_INTERVAL=200 +CONFIG_SCHED_WAITPID=y +CONFIG_SDCLONE_DISABLE=y +CONFIG_START_DAY=6 +CONFIG_START_MONTH=12 +CONFIG_START_YEAR=2011 +CONFIG_SYSTEM_NSH=y +CONFIG_UART0_SERIAL_CONSOLE=y +CONFIG_USER_ENTRYPOINT="nsh_main" diff --git a/boards/xtensa/esp32/ttgo_lora_esp32/include/board.h b/boards/xtensa/esp32/ttgo_lora_esp32/include/board.h new file mode 100644 index 00000000000..8aff8d80006 --- /dev/null +++ b/boards/xtensa/esp32/ttgo_lora_esp32/include/board.h @@ -0,0 +1,85 @@ +/**************************************************************************** + * boards/xtensa/esp32/ttgo_lora_esp32/include/board.h + * + * 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. + * + ****************************************************************************/ + +#ifndef __BOARDS_XTENSA_ESP32_TTGO_LORA_ESP32_INCLUDE_BOARD_H +#define __BOARDS_XTENSA_ESP32_TTGO_LORA_ESP32_INCLUDE_BOARD_H + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +/* Clocking *****************************************************************/ + +/* The TTGO-LoRa-SX1276-ESP32 is fitted with a 26MHz crystal */ + +#ifdef CONFIG_ESP32_XTAL_26MHz +# define BOARD_XTAL_FREQUENCY 26000000 +#else +# define BOARD_XTAL_FREQUENCY 40000000 +#endif + +/* Clock reconfiguration is currently disabled, so the CPU will be running + * at the XTAL frequency or at two times the XTAL frequency, depending upon + * how we load the code: + * + * - If we load the code into FLASH at address 0x1000 where it is started by + * the second level bootloader, then the frequency is the crystal + * frequency. + * - If we load the code into IRAM after the second level bootloader has run + * this frequency will be twice the crystal frequency. + * + * Don't ask me for an explanation. + */ + +/* Note: The bootloader (esp-idf bootloader.bin) configures: + * + * - CPU frequency to 80MHz + * - The XTAL frequency according to the SDK config CONFIG_ESP32_XTAL_FREQ, + * which is 40MHz by default. + * + * Reference: + * https://github.com/espressif/esp-idf/blob + * /6fd855ab8d00d23bad4660216bc2122c2285d5be/components + * /bootloader_support/src/bootloader_clock.c#L38-L62 + */ + +#ifdef CONFIG_ESP32_RUN_IRAM +# define BOARD_CLOCK_FREQUENCY (2 * BOARD_XTAL_FREQUENCY) +#else +#ifdef CONFIG_ESP32_DEFAULT_CPU_FREQ_MHZ +# define BOARD_CLOCK_FREQUENCY (CONFIG_ESP32_DEFAULT_CPU_FREQ_MHZ * 1000000) +#else +# define BOARD_CLOCK_FREQUENCY 80000000 +#endif +#endif + +/* LED definitions **********************************************************/ + +/* Define how many LEDs this board has (needed by userleds) */ + +#define BOARD_NLEDS 1 + +/* GPIO pins used by the GPIO Subsystem */ + +#define BOARD_NGPIOOUT 1 /* Amount of GPIO Output pins */ +#define BOARD_NGPIOIN 1 /* Amount of GPIO Input without Interruption */ +#define BOARD_NGPIOINT 1 /* Amount of GPIO Input w/ Interruption pins */ + +#endif /* __BOARDS_XTENSA_ESP32_TTGO_LORA_ESP32_INCLUDE_BOARD_H */ diff --git a/boards/xtensa/esp32/ttgo_lora_esp32/scripts/.gitignore b/boards/xtensa/esp32/ttgo_lora_esp32/scripts/.gitignore new file mode 100644 index 00000000000..461270524bd --- /dev/null +++ b/boards/xtensa/esp32/ttgo_lora_esp32/scripts/.gitignore @@ -0,0 +1 @@ +/esp32_out.ld diff --git a/boards/xtensa/esp32/ttgo_lora_esp32/scripts/Make.defs b/boards/xtensa/esp32/ttgo_lora_esp32/scripts/Make.defs new file mode 100644 index 00000000000..5d3c431d910 --- /dev/null +++ b/boards/xtensa/esp32/ttgo_lora_esp32/scripts/Make.defs @@ -0,0 +1,128 @@ +############################################################################ +# boards/xtensa/esp32/ttgo_lora_esp32/scripts/Make.defs +# +# 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. +# +############################################################################ + +include $(TOPDIR)/.config +include $(TOPDIR)/tools/Config.mk +include $(TOPDIR)/tools/esp32/Config.mk +include $(TOPDIR)/arch/xtensa/src/lx6/Toolchain.defs + +# Pick the linker scripts from the board level if they exist, if not +# pick the common linker scripts. + +ifneq ($(wildcard $(BOARD_DIR)$(DELIM)scripts$(DELIM)esp32_rom.ld),) + LDSCRIPT_ROM = $(BOARD_DIR)$(DELIM)scripts$(DELIM)esp32_rom.ld +else + LDSCRIPT_ROM = $(BOARD_COMMON_DIR)$(DELIM)scripts$(DELIM)esp32_rom.ld +endif + +ifneq ($(wildcard $(BOARD_DIR)$(DELIM)scripts$(DELIM)esp32.ld),) + LDSCRIPT_SECT = $(BOARD_DIR)$(DELIM)scripts$(DELIM)esp32.ld +else + ifeq ($(CONFIG_ESP32_APP_FORMAT_MCUBOOT),y) + LDSCRIPT_SECT = $(BOARD_COMMON_DIR)$(DELIM)scripts$(DELIM)esp32_mcuboot.ld + else + LDSCRIPT_SECT = $(BOARD_COMMON_DIR)$(DELIM)scripts$(DELIM)esp32.ld + endif +endif + +ifneq ($(wildcard $(BOARD_DIR)$(DELIM)scripts$(DELIM)esp32.template.ld),) + LDSCRIPT_TEMPLATE = $(BOARD_DIR)$(DELIM)scripts$(DELIM)esp32.template.ld +else + LDSCRIPT_TEMPLATE = $(BOARD_COMMON_DIR)$(DELIM)scripts$(DELIM)esp32.template.ld +endif + +# This is the generated memory layout linker script. It will always be +# generated at the board level. + +LDSCRIPT_MEMORY = $(BOARD_DIR)$(DELIM)scripts$(DELIM)esp32_out.ld + +ifeq ($(CONFIG_CYGWIN_WINTOOL),y) + ARCHSCRIPT = -T "${shell cygpath -w $(LDSCRIPT_MEMORY)}" + ARCHSCRIPT += -T "${shell cygpath -w $(LDSCRIPT_SECT)}" + ARCHSCRIPT += -T "${shell cygpath -w $(LDSCRIPT_ROM)}" +else + ARCHSCRIPT = -T$(LDSCRIPT_MEMORY) -T$(LDSCRIPT_SECT) -T$(LDSCRIPT_ROM) +endif + +ifeq ($(CONFIG_DEBUG_SYMBOLS),y) + ARCHOPTIMIZATION = -g +endif + +ifneq ($(CONFIG_DEBUG_NOOPT),y) + ARCHOPTIMIZATION += $(MAXOPTIMIZATION) -fno-strict-aliasing -fno-strength-reduce -fomit-frame-pointer +endif + +ARCHCFLAGS = -fno-builtin -ffunction-sections -fdata-sections -fstrict-volatile-bitfields -mlongcalls +ARCHCXXFLAGS = $(ARCHCFLAGS) -fno-exceptions -fcheck-new -fno-rtti +ARCHWARNINGS = -Wall -Wstrict-prototypes -Wshadow -Wundef +ARCHWARNINGSXX = -Wall -Wshadow -Wundef +ARCHPICFLAGS = -fpic + +# if SPIRAM/PSRAM is used then we need to include a workaround + +ifeq ($(CONFIG_ESP32_SPIRAM),y) + ARCHCFLAGS += -mfix-esp32-psram-cache-issue +endif + +CFLAGS := $(ARCHCFLAGS) $(ARCHWARNINGS) $(ARCHOPTIMIZATION) $(ARCHCPUFLAGS) $(ARCHINCLUDES) $(ARCHDEFINES) $(EXTRAFLAGS) -pipe +CPICFLAGS = $(ARCHPICFLAGS) $(CFLAGS) +CXXFLAGS := $(ARCHCXXFLAGS) $(ARCHWARNINGSXX) $(ARCHOPTIMIZATION) $(ARCHCPUFLAGS) $(ARCHXXINCLUDES) $(ARCHDEFINES) $(EXTRAFLAGS) -pipe +CXXPICFLAGS = $(ARCHPICFLAGS) $(CXXFLAGS) +CPPFLAGS := $(ARCHINCLUDES) $(ARCHDEFINES) $(EXTRAFLAGS) +AFLAGS := $(CFLAGS) -D__ASSEMBLY__ + +NXFLATLDFLAGS1 = -r -d -warn-common +NXFLATLDFLAGS2 = $(NXFLATLDFLAGS1) -T$(TOPDIR)/binfmt/libnxflat/gnu-nxflat-pcrel.ld -no-check-sections +LDNXFLATFLAGS = -e main -s 2048 + +# Loadable module definitions + +CMODULEFLAGS = $(CFLAGS) -mtext-section-literals + +LDMODULEFLAGS = -r -e module_initialize +ifeq ($(CONFIG_CYGWIN_WINTOOL),y) + LDMODULEFLAGS += -T "${shell cygpath -w $(TOPDIR)/libs/libc/modlib/gnu-elf.ld}" +else + LDMODULEFLAGS += -T $(TOPDIR)/libs/libc/modlib/gnu-elf.ld +endif + +# ELF module definitions + +CELFFLAGS = $(CFLAGS) -mtext-section-literals +CXXELFFLAGS = $(CXXFLAGS) -mtext-section-literals + +LDELFFLAGS = -r -e main +ifeq ($(CONFIG_CYGWIN_WINTOOL),y) + LDELFFLAGS += -T "${shell cygpath -w $(TOPDIR)$(DELIM)binfmt$(DELIM)libelf$(DELIM)gnu-elf.ld}" +else + LDELFFLAGS += -T $(TOPDIR)$(DELIM)binfmt$(DELIM)libelf$(DELIM)gnu-elf.ld +endif + +LDFLAGS += -nostartfiles -nodefaultlibs + +ifeq ($(CONFIG_CYGWIN_WINTOOL),y) + LDFLAGS += -Map="${shell cygpath -w $(TOPDIR)/nuttx.map}" --cref +else + LDFLAGS += -Map=$(TOPDIR)/nuttx.map --cref +endif + +ifeq ($(CONFIG_DEBUG_SYMBOLS),y) + LDFLAGS += -g +endif diff --git a/boards/xtensa/esp32/ttgo_lora_esp32/src/Make.defs b/boards/xtensa/esp32/ttgo_lora_esp32/src/Make.defs new file mode 100644 index 00000000000..ee2742db50d --- /dev/null +++ b/boards/xtensa/esp32/ttgo_lora_esp32/src/Make.defs @@ -0,0 +1,70 @@ +############################################################################ +# boards/xtensa/esp32/ttgo_lora_esp32/src/Make.defs +# +# 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. +# +############################################################################ + +include $(TOPDIR)/Make.defs + +CONFIGFILE = $(TOPDIR)$(DELIM)include$(DELIM)nuttx$(DELIM)config.h + +CSRCS = esp32_boot.c esp32_bringup.c + +ifeq ($(CONFIG_BOARDCTL),y) +CSRCS += esp32_appinit.c +ifeq ($(CONFIG_BOARDCTL_RESET),y) +CSRCS += esp32_reset.c +endif +endif + +ifeq ($(CONFIG_MMCSD),y) +CSRCS += esp32_mmcsd.c +endif + +ifeq ($(CONFIG_ESP32_SPIFLASH),y) +CSRCS += esp32_spiflash.c +endif + +ifeq ($(CONFIG_DEV_GPIO),y) +CSRCS += esp32_gpio.c +endif + +ifeq ($(CONFIG_USERLED),y) +CSRCS += esp32_userleds.c +endif + +ifeq ($(CONFIG_ARCH_BUTTONS),y) +CSRCS += esp32_buttons.c +endif + +SCRIPTOUT = $(BOARD_DIR)$(DELIM)scripts$(DELIM)esp32_out.ld + +.PHONY = context distclean + +$(SCRIPTOUT): $(LDSCRIPT_TEMPLATE) $(CONFIGFILE) + $(Q) $(CC) -isystem $(TOPDIR)/include -C -P -x c -E $(LDSCRIPT_TEMPLATE) -o $@ + +context:: $(SCRIPTOUT) + +distclean:: + $(call DELFILE, $(SCRIPTOUT)) + $(call DELFILE, $(TOPDIR)/esp32_qemu_img.bin) + +DEPPATH += --dep-path board +VPATH += :board +CFLAGS += $(shell $(INCDIR) "$(CC)" $(TOPDIR)$(DELIM)arch$(DELIM)$(CONFIG_ARCH)$(DELIM)src$(DELIM)board$(DELIM)board) + diff --git a/boards/xtensa/esp32/ttgo_lora_esp32/src/esp32_appinit.c b/boards/xtensa/esp32/ttgo_lora_esp32/src/esp32_appinit.c new file mode 100644 index 00000000000..840720d03bd --- /dev/null +++ b/boards/xtensa/esp32/ttgo_lora_esp32/src/esp32_appinit.c @@ -0,0 +1,80 @@ +/**************************************************************************** + * boards/xtensa/esp32/ttgo_lora_esp32/src/esp32_appinit.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 "ttgo_lora_esp32.h" + +#ifdef CONFIG_BOARDCTL + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: board_app_initialize + * + * Description: + * Perform application specific initialization. This function is never + * called directly from application code, but only indirectly via the + * (non-standard) boardctl() interface using the command BOARDIOC_INIT. + * + * Input Parameters: + * arg - The boardctl() argument is passed to the board_app_initialize() + * implementation without modification. The argument has no + * meaning to NuttX; the meaning of the argument is a contract + * between the board-specific initialization logic and the + * matching application logic. The value could be such things as a + * mode enumeration value, a set of DIP switch settings, a + * pointer to configuration data read from a file or serial FLASH, + * or whatever you would like to do with it. Every implementation + * should accept zero/NULL as a default configuration. + * + * Returned Value: + * Zero (OK) is returned on success; a negated errno value is returned on + * any failure to indicate the nature of the failure. + * + ****************************************************************************/ + +int board_app_initialize(uintptr_t arg) +{ +#ifdef CONFIG_BOARD_LATE_INITIALIZE + /* Board initialization already performed by board_late_initialize() */ + + return OK; +#else + /* Perform board-specific initialization */ + + return esp32_bringup(); +#endif +} + +#endif /* CONFIG_BOARDCTL */ diff --git a/boards/xtensa/esp32/ttgo_lora_esp32/src/esp32_boot.c b/boards/xtensa/esp32/ttgo_lora_esp32/src/esp32_boot.c new file mode 100644 index 00000000000..982411a9252 --- /dev/null +++ b/boards/xtensa/esp32/ttgo_lora_esp32/src/esp32_boot.c @@ -0,0 +1,96 @@ +/**************************************************************************** + * boards/xtensa/esp32/ttgo_lora_esp32/src/esp32_boot.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 "ttgo_lora_esp32.h" + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: esp32_board_initialize + * + * Description: + * All ESP32 architectures must provide the following entry point. + * This entry point is called early in the initialization -- after all + * memory has been configured and mapped but before any devices have been + * initialized. + * + ****************************************************************************/ + +void esp32_board_initialize(void) +{ +} + +/**************************************************************************** + * Name: board_late_initialize + * + * Description: + * If CONFIG_BOARD_LATE_INITIALIZE is selected, then an additional + * initialization call will be performed in the boot-up sequence to a + * function called board_late_initialize(). board_late_initialize() will + * be called immediately after up_initialize() is called and just before + * the initial application is started. This additional initialization + * phase may be used, for example, to initialize board-specific device + * drivers. + * + ****************************************************************************/ + +#ifdef CONFIG_BOARD_LATE_INITIALIZE +void board_late_initialize(void) +{ + /* Perform board-specific initialization */ + + esp32_bringup(); + +#ifdef CONFIG_SMP + /* To avoid corrupting the heap, this region of memory (~3KB) is not + * included until the APP CPU has started. + * So we can't add it with the rest of the regions at xtensa_add_region(), + * that function is called early from up_initialize(). We wait until the + * SMP bringup is complete. + */ + + umm_addregion((FAR void *)HEAP_REGION_ROMAPP_START, + (size_t)(HEAP_REGION_ROMAPP_END - HEAP_REGION_ROMAPP_START)); +#endif +} +#endif diff --git a/boards/xtensa/esp32/ttgo_lora_esp32/src/esp32_bringup.c b/boards/xtensa/esp32/ttgo_lora_esp32/src/esp32_bringup.c new file mode 100644 index 00000000000..3d307cfb2d8 --- /dev/null +++ b/boards/xtensa/esp32/ttgo_lora_esp32/src/esp32_bringup.c @@ -0,0 +1,397 @@ +/**************************************************************************** + * boards/xtensa/esp32/ttgo_lora_esp32/src/esp32_bringup.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 + +#include +#include +#include + +#include "esp32_spiflash.h" +#include "esp32_partition.h" + +#ifdef CONFIG_USERLED +# include +#endif + +#ifdef CONFIG_CAN_MCP2515 +# include "esp32_mcp2515.h" +#endif + +#ifdef CONFIG_TIMER +#include +#endif + +#ifdef CONFIG_ONESHOT +# include "esp32_board_oneshot.h" +#endif + +#ifdef CONFIG_WATCHDOG +# include "esp32_board_wdt.h" +#endif + +#ifdef CONFIG_ESP32_WIRELESS +# include "esp32_board_wlan.h" +#endif + +#ifdef CONFIG_ESP32_I2C +# include "esp32_board_i2c.h" +#endif + +#ifdef CONFIG_SENSORS_BMP180 +# include "esp32_bmp180.h" +#endif + +#ifdef CONFIG_LCD_HT16K33 +# include "esp32_ht16k33.h" +#endif + +#ifdef CONFIG_ESP32_AES_ACCELERATOR +# include "esp32_aes.h" +#endif + +#ifdef CONFIG_ESP32_RT_TIMER +# include "esp32_rt_timer.h" +#endif + +#ifdef CONFIG_INPUT_BUTTONS +# include +#endif + +#ifdef CONFIG_RTC_DRIVER +# include "esp32_rtc_lowerhalf.h" +#endif + +#ifdef CONFIG_SPI_DRIVER +# include "esp32_spi.h" +#endif + +#include "ttgo_lora_esp32.h" + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: esp32_bringup + * + * Description: + * Perform architecture-specific initialization + * + * CONFIG_BOARD_LATE_INITIALIZE=y : + * Called from board_late_initialize(). + * + * CONFIG_BOARD_LATE_INITIALIZE=n && CONFIG_BOARDCTL=y : + * Called from the NSH library + * + ****************************************************************************/ + +int esp32_bringup(void) +{ + int ret; + +#ifdef CONFIG_ESP32_AES_ACCELERATOR + ret = esp32_aes_init(); + if (ret < 0) + { + syslog(LOG_ERR, + "ERROR: Failed to initialize AES: %d\n", ret); + } +#endif + +#if defined(CONFIG_ESP32_EFUSE) + ret = esp32_efuse_initialize("/dev/efuse"); + if (ret < 0) + { + syslog(LOG_ERR, "ERROR: Failed to init EFUSE: %d\n", ret); + } +#endif + +#ifdef CONFIG_FS_PROCFS + /* Mount the procfs file system */ + + ret = nx_mount(NULL, "/proc", "procfs", 0, NULL); + if (ret < 0) + { + syslog(LOG_ERR, "ERROR: Failed to mount procfs at /proc: %d\n", ret); + } +#endif + +#ifdef CONFIG_FS_TMPFS + /* Mount the tmpfs file system */ + + ret = nx_mount(NULL, CONFIG_LIBC_TMPDIR, "tmpfs", 0, NULL); + if (ret < 0) + { + syslog(LOG_ERR, "ERROR: Failed to mount tmpfs at %s: %d\n", + CONFIG_LIBC_TMPDIR, ret); + } +#endif + +#ifdef CONFIG_ESP32_SPIFLASH + +#ifdef CONFIG_ESP32_SPIFLASH_ENCRYPTION_TEST + esp32_spiflash_encrypt_test(); +#endif + + ret = esp32_spiflash_init(); + if (ret) + { + syslog(LOG_ERR, "ERROR: Failed to initialize SPI Flash\n"); + return ret; + } +#endif + +#ifdef CONFIG_ESP32_PARTITION + ret = esp32_partition_init(); + if (ret < 0) + { + syslog(LOG_ERR, "ERROR: Failed to initialize partition error=%d\n", + ret); + return ret; + } +#endif + +#ifdef CONFIG_ESP32_RT_TIMER + ret = esp32_rt_timer_init(); + if (ret < 0) + { + syslog(LOG_ERR, "Failed to initialize RT timer: %d\n", ret); + } +#endif + +#ifdef CONFIG_ESP32_WIRELESS + ret = board_wlan_init(); + if (ret < 0) + { + syslog(LOG_ERR, "ERROR: Failed to initialize wireless subsystem=%d\n", + ret); + return ret; + } +#endif + +/* First, register the timer drivers and let timer 1 for oneshot + * if it is enabled. + */ + +#ifdef CONFIG_TIMER + +#if defined(CONFIG_ESP32_TIMER0) && !defined(CONFIG_ESP32_RT_TIMER) + ret = esp32_timer_initialize("/dev/timer0", TIMER0); + if (ret < 0) + { + syslog(LOG_ERR, + "ERROR: Failed to initialize timer driver: %d\n", + ret); + return ret; + } +#endif + +#if defined(CONFIG_ESP32_TIMER1) && !defined(CONFIG_ONESHOT) + ret = esp32_timer_initialize("/dev/timer1", TIMER1); + if (ret < 0) + { + syslog(LOG_ERR, + "ERROR: Failed to initialize timer driver: %d\n", + ret); + return ret; + } +#endif + +#ifdef CONFIG_ESP32_TIMER2 + ret = esp32_timer_initialize("/dev/timer2", TIMER2); + if (ret < 0) + { + syslog(LOG_ERR, + "ERROR: Failed to initialize timer driver: %d\n", + ret); + return ret; + } +#endif + +#ifdef CONFIG_ESP32_TIMER3 + ret = esp32_timer_initialize("/dev/timer3", TIMER3); + if (ret < 0) + { + syslog(LOG_ERR, + "ERROR: Failed to initialize timer driver: %d\n", + ret); + return ret; + } +#endif + +#endif /* CONFIG_TIMER */ + + /* Now register one oneshot driver */ + +#if defined(CONFIG_ONESHOT) && defined(CONFIG_ESP32_TIMER1) + + ret = esp32_oneshot_init(ONESHOT_TIMER, ONESHOT_RESOLUTION_US); + if (ret < 0) + { + syslog(LOG_ERR, "ERROR: esp32_oneshot_init() failed: %d\n", ret); + } + +#endif /* CONFIG_ONESHOT */ + +#ifdef CONFIG_USERLED + /* Register the LED driver */ + + ret = userled_lower_initialize("/dev/userleds"); + if (ret < 0) + { + syslog(LOG_ERR, "ERROR: userled_lower_initialize() failed: %d\n", ret); + } +#endif + +#ifdef CONFIG_CAN_MCP2515 + /* Configure and initialize the MCP2515 CAN device */ + + ret = board_mcp2515_initialize(0); + if (ret < 0) + { + syslog(LOG_ERR, "ERROR: board_mcp2515_initialize() failed: %d\n", ret); + } +#endif + +#ifdef CONFIG_WATCHDOG + /* Configure watchdog timer */ + + ret = board_wdt_init(); + if (ret < 0) + { + syslog(LOG_ERR, + "ERROR: Failed to initialize watchdog drivers: %d\n", + ret); + } +#endif + +#ifdef CONFIG_DEV_GPIO + ret = esp32_gpio_init(); + if (ret < 0) + { + syslog(LOG_ERR, "Failed to initialize GPIO Driver: %d\n", ret); + return ret; + } +#endif + +#ifdef CONFIG_I2C_DRIVER + +#ifdef CONFIG_ESP32_I2C0 + ret = esp32_i2c_register(0); + + if (ret < 0) + { + syslog(LOG_ERR, "Failed to initialize I2C Driver for I2C0: %d\n", ret); + return ret; + } +#endif + +#ifdef CONFIG_ESP32_I2C1 + ret = esp32_i2c_register(1); + + if (ret < 0) + { + syslog(LOG_ERR, "Failed to initialize I2C Driver for I2C1: %d\n", ret); + return ret; + } +#endif + +#endif + +#ifdef CONFIG_SENSORS_BMP180 + /* Try to register BMP180 device in I2C0 */ + + ret = board_bmp180_initialize(0, 0); + + if (ret < 0) + { + syslog(LOG_ERR, "Failed to initialize BMP180 driver: %d\n", ret); + return ret; + } +#endif + +#ifdef CONFIG_LCD_HT16K33 + /* Try to register HT16K33 in the I2C0 */ + + ret = board_ht16k33_initialize(0, 0); + + if (ret < 0) + { + syslog(LOG_ERR, "Failed to initialize HT16K33 driver: %d\n", ret); + return ret; + } +#endif + +#ifdef CONFIG_INPUT_BUTTONS + /* Register the BUTTON driver */ + + ret = btn_lower_initialize("/dev/buttons"); + if (ret < 0) + { + syslog(LOG_ERR, "ERROR: btn_lower_initialize() failed: %d\n", ret); + } +#endif + +#ifdef CONFIG_RTC_DRIVER + /* Instantiate the ESP32 RTC driver */ + + ret = esp32_rtc_driverinit(); + if (ret < 0) + { + syslog(LOG_ERR, + "ERROR: Failed to Instantiate the RTC driver: %d\n", ret); + } +#endif + +#ifdef CONFIG_SPI_DRIVER +# ifdef CONFIG_ESP32_SPI2 + ret = board_spidev_initialize(ESP32_SPI2); + if (ret < 0) + { + syslog(LOG_ERR, "Failed to initialize SPI%d driver: %d\n", + ESP32_SPI2, ret); + } +# endif +#endif + + /* If we got here then perhaps not all initialization was successful, but + * at least enough succeeded to bring-up NSH with perhaps reduced + * capabilities. + */ + + UNUSED(ret); + return OK; +} diff --git a/boards/xtensa/esp32/ttgo_lora_esp32/src/esp32_buttons.c b/boards/xtensa/esp32/ttgo_lora_esp32/src/esp32_buttons.c new file mode 100644 index 00000000000..4ae808d3112 --- /dev/null +++ b/boards/xtensa/esp32/ttgo_lora_esp32/src/esp32_buttons.c @@ -0,0 +1,166 @@ +/**************************************************************************** + * boards/xtensa/esp32/ttgo_lora_esp32/src/esp32_buttons.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 "ttgo_lora_esp32.h" + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: board_button_initialize + * + * Description: + * board_button_initialize() must be called to initialize button resources. + * After that, board_buttons() may be called to collect the current state + * of all buttons or board_button_irq() may be called to register button + * interrupt handlers. + * + ****************************************************************************/ + +uint32_t board_button_initialize(void) +{ + esp32_configgpio(BUTTON_BOOT, INPUT_FUNCTION_3 | PULLUP); + return 1; +} + +/**************************************************************************** + * Name: board_buttons + * + * Description: + * After board_button_initialize() has been called, board_buttons() may be + * called to collect the state of all buttons. board_buttons() returns an + * 8-bit bit set with each bit associated with a button. See the + * BUTTON_*_BIT definitions in board.h for the meaning of each bit. + * + ****************************************************************************/ + +uint32_t board_buttons(void) +{ + uint8_t ret = 0; + int i = 0; + int n = 0; + + bool b0 = esp32_gpioread(BUTTON_BOOT); + + for (i = 0; i < 10; i++) + { + up_mdelay(1); /* TODO */ + + bool b1 = esp32_gpioread(BUTTON_BOOT); + + if (b0 == b1) + { + n++; + } + else + { + n = 0; + } + + if (3 == n) + { + break; + } + + b0 = b1; + } + + iinfo("b=%d n=%d \n", b0, n); + + /* Low value means that the button is pressed */ + + if (!b0) + { + ret = 0x1; + } + + return ret; +} + +/**************************************************************************** + * Name: board_button_irq + * + * Description: + * board_button_irq() may be called to register an interrupt handler that + * will be called when a button is depressed or released. The ID value is + * a button enumeration value that uniquely identifies a button resource. + * See the BUTTON_* definitions in board.h for the meaning of enumeration + * value. + * + ****************************************************************************/ + +#ifdef CONFIG_ARCH_IRQBUTTONS +int board_button_irq(int id, xcpt_t irqhandler, FAR void *arg) +{ + int ret; + DEBUGASSERT(id == BUTTON_BOOT); + + int irq = ESP32_PIN2IRQ(BUTTON_BOOT); + + if (NULL != irqhandler) + { + /* Make sure the interrupt is disabled */ + + esp32_gpioirqdisable(irq); + + ret = irq_attach(irq, irqhandler, arg); + if (ret < 0) + { + syslog(LOG_ERR, "ERROR: irq_attach() failed: %d\n", ret); + return ret; + } + + gpioinfo("Attach %p\n", irqhandler); + + gpioinfo("Enabling the interrupt\n"); + + /* Configure the interrupt for rising and falling edges */ + + esp32_gpioirqenable(irq, CHANGE); + } + else + { + gpioinfo("Disable the interrupt\n"); + esp32_gpioirqdisable(irq); + } + + return OK; +} +#endif diff --git a/boards/xtensa/esp32/ttgo_lora_esp32/src/esp32_gpio.c b/boards/xtensa/esp32/ttgo_lora_esp32/src/esp32_gpio.c new file mode 100644 index 00000000000..a1d3f03046d --- /dev/null +++ b/boards/xtensa/esp32/ttgo_lora_esp32/src/esp32_gpio.c @@ -0,0 +1,384 @@ +/**************************************************************************** + * boards/xtensa/esp32/ttgo_lora_esp32/src/esp32_gpio.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 "ttgo_lora_esp32.h" +#include "esp32_gpio.h" +#include "hardware/esp32_gpio_sigmap.h" + +#if defined(CONFIG_DEV_GPIO) && !defined(CONFIG_GPIO_LOWER_HALF) + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +#if !defined(CONFIG_ESP32_GPIO_IRQ) && BOARD_NGPIOINT > 0 +# error "NGPIOINT is > 0 and GPIO interrupts aren't enabled" +#endif + +/* Output pins. GPIO15 is used as an example, any other outputs could be + * used. + */ + +#define GPIO_OUT1 15 + +/* Input pins. GPIO18 is used as an example, any other inputs could be + * used. + */ + +#define GPIO_IN1 18 + +/* Interrupt pins. GPIO22 is used as an example, any other inputs could be + * used. + */ + +#define GPIO_IRQPIN1 22 + +/**************************************************************************** + * Private Types + ****************************************************************************/ + +struct esp32gpio_dev_s +{ + struct gpio_dev_s gpio; + uint8_t id; +}; + +struct esp32gpint_dev_s +{ + struct esp32gpio_dev_s esp32gpio; + pin_interrupt_t callback; +}; + +/**************************************************************************** + * Private Function Prototypes + ****************************************************************************/ + +#if BOARD_NGPIOOUT > 0 +static int gpout_read(FAR struct gpio_dev_s *dev, FAR bool *value); +static int gpout_write(FAR struct gpio_dev_s *dev, bool value); +#endif + +#if BOARD_NGPIOIN > 0 +static int gpin_read(FAR struct gpio_dev_s *dev, FAR bool *value); +#endif + +#if BOARD_NGPIOINT > 0 +static int gpint_read(FAR struct gpio_dev_s *dev, FAR bool *value); +static int gpint_attach(FAR struct gpio_dev_s *dev, + pin_interrupt_t callback); +static int gpint_enable(FAR struct gpio_dev_s *dev, bool enable); +#endif + +/**************************************************************************** + * Private Data + ****************************************************************************/ + +#if BOARD_NGPIOOUT > 0 +static const struct gpio_operations_s gpout_ops = +{ + .go_read = gpout_read, + .go_write = gpout_write, + .go_attach = NULL, + .go_enable = NULL, +}; + +/* This array maps the GPIO pins used as OUTPUT */ + +static const uint32_t g_gpiooutputs[BOARD_NGPIOOUT] = +{ + GPIO_OUT1 +}; + +static struct esp32gpio_dev_s g_gpout[BOARD_NGPIOOUT]; +#endif + +#if BOARD_NGPIOIN > 0 +static const struct gpio_operations_s gpin_ops = +{ + .go_read = gpin_read, + .go_write = NULL, + .go_attach = NULL, + .go_enable = NULL, +}; + +/* This array maps the GPIO pins used as INTERRUPT INPUTS */ + +static const uint32_t g_gpioinputs[BOARD_NGPIOIN] = +{ + GPIO_IN1 +}; + +static struct esp32gpio_dev_s g_gpin[BOARD_NGPIOIN]; +#endif + +#if BOARD_NGPIOINT > 0 +static const struct gpio_operations_s gpint_ops = +{ + .go_read = gpint_read, + .go_write = NULL, + .go_attach = gpint_attach, + .go_enable = gpint_enable, +}; + +/* This array maps the GPIO pins used as INTERRUPT INPUTS */ + +static const uint32_t g_gpiointinputs[BOARD_NGPIOINT] = +{ + GPIO_IRQPIN1, +}; + +static struct esp32gpint_dev_s g_gpint[BOARD_NGPIOINT]; +#endif + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: gpout_read + ****************************************************************************/ + +#if BOARD_NGPIOOUT > 0 +static int gpout_read(FAR struct gpio_dev_s *dev, FAR bool *value) +{ + FAR struct esp32gpio_dev_s *esp32gpio = (FAR struct esp32gpio_dev_s *)dev; + + DEBUGASSERT(esp32gpio != NULL && value != NULL); + DEBUGASSERT(esp32gpio->id < BOARD_NGPIOOUT); + gpioinfo("Reading...\n"); + + *value = esp32_gpioread(g_gpiooutputs[esp32gpio->id]); + return OK; +} + +/**************************************************************************** + * Name: gpout_write + ****************************************************************************/ + +static int gpout_write(FAR struct gpio_dev_s *dev, bool value) +{ + FAR struct esp32gpio_dev_s *esp32gpio = (FAR struct esp32gpio_dev_s *)dev; + + DEBUGASSERT(esp32gpio != NULL); + DEBUGASSERT(esp32gpio->id < BOARD_NGPIOOUT); + gpioinfo("Writing %d\n", (int)value); + + esp32_gpiowrite(g_gpiooutputs[esp32gpio->id], value); + return OK; +} +#endif + +/**************************************************************************** + * Name: gpin_read + ****************************************************************************/ + +#if BOARD_NGPIOIN > 0 +static int gpin_read(FAR struct gpio_dev_s *dev, FAR bool *value) +{ + FAR struct esp32gpio_dev_s *esp32gpio = (FAR struct esp32gpio_dev_s *)dev; + + DEBUGASSERT(esp32gpio != NULL && value != NULL); + DEBUGASSERT(esp32gpio->id < BOARD_NGPIOIN); + gpioinfo("Reading... pin %d\n", g_gpioinputs[esp32gpio->id]); + + *value = esp32_gpioread(g_gpioinputs[esp32gpio->id]); + return OK; +} +#endif + +/**************************************************************************** + * Name: esp32gpio_interrupt + ****************************************************************************/ + +#if BOARD_NGPIOINT > 0 +static int esp32gpio_interrupt(int irq, void *context, void *arg) +{ + FAR struct esp32gpint_dev_s *esp32gpint = + (FAR struct esp32gpint_dev_s *)arg; + + DEBUGASSERT(esp32gpint != NULL && esp32gpint->callback != NULL); + gpioinfo("Interrupt! callback=%p\n", esp32gpint->callback); + + esp32gpint->callback(&esp32gpint->esp32gpio.gpio, + esp32gpint->esp32gpio.id); + return OK; +} + +/**************************************************************************** + * Name: gpint_read + ****************************************************************************/ + +static int gpint_read(FAR struct gpio_dev_s *dev, FAR bool *value) +{ + FAR struct esp32gpint_dev_s *esp32gpint = + (FAR struct esp32gpint_dev_s *)dev; + + DEBUGASSERT(esp32gpint != NULL && value != NULL); + DEBUGASSERT(esp32gpint->esp32gpio.id < BOARD_NGPIOINT); + gpioinfo("Reading int pin...\n"); + + *value = esp32_gpioread(g_gpiointinputs[esp32gpint->esp32gpio.id]); + return OK; +} + +/**************************************************************************** + * Name: gpint_attach + ****************************************************************************/ + +static int gpint_attach(FAR struct gpio_dev_s *dev, + pin_interrupt_t callback) +{ + FAR struct esp32gpint_dev_s *esp32gpint = + (FAR struct esp32gpint_dev_s *)dev; + int irq = ESP32_PIN2IRQ(g_gpiointinputs[esp32gpint->esp32gpio.id]); + int ret; + + gpioinfo("Attaching the callback\n"); + + /* Make sure the interrupt is disabled */ + + esp32_gpioirqdisable(irq); + ret = irq_attach(irq, + esp32gpio_interrupt, + &g_gpint[esp32gpint->esp32gpio.id]); + if (ret < 0) + { + syslog(LOG_ERR, "ERROR: gpint_attach() failed: %d\n", ret); + return ret; + } + + gpioinfo("Attach %p\n", callback); + esp32gpint->callback = callback; + return OK; +} + +/**************************************************************************** + * Name: gpint_enable + ****************************************************************************/ + +static int gpint_enable(FAR struct gpio_dev_s *dev, bool enable) +{ + FAR struct esp32gpint_dev_s *esp32gpint = + (FAR struct esp32gpint_dev_s *)dev; + int irq = ESP32_PIN2IRQ(g_gpiointinputs[esp32gpint->esp32gpio.id]); + + if (enable) + { + if (esp32gpint->callback != NULL) + { + gpioinfo("Enabling the interrupt\n"); + + /* Configure the interrupt for rising edge */ + + esp32_gpioirqenable(irq, RISING); + } + } + else + { + gpioinfo("Disable the interrupt\n"); + esp32_gpioirqdisable(irq); + } + + return OK; +} +#endif + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: esp32_gpio_init + ****************************************************************************/ + +int esp32_gpio_init(void) +{ + int i; + +#if BOARD_NGPIOOUT > 0 + for (i = 0; i < BOARD_NGPIOOUT; i++) + { + /* Setup and register the GPIO pin */ + + g_gpout[i].gpio.gp_pintype = GPIO_OUTPUT_PIN; + g_gpout[i].gpio.gp_ops = &gpout_ops; + g_gpout[i].id = i; + gpio_pin_register(&g_gpout[i].gpio, i); + + /* Configure the pins that will be used as output */ + + esp32_gpio_matrix_out(g_gpiooutputs[i], SIG_GPIO_OUT_IDX, 0, 0); + esp32_configgpio(g_gpiooutputs[i], OUTPUT_FUNCTION_3 | + INPUT_FUNCTION_3); + esp32_gpiowrite(g_gpiooutputs[i], 0); + } +#endif + +#if BOARD_NGPIOIN > 0 + for (i = 0; i < BOARD_NGPIOIN; i++) + { + /* Setup and register the GPIO pin */ + + g_gpin[i].gpio.gp_pintype = GPIO_INPUT_PIN; + g_gpin[i].gpio.gp_ops = &gpin_ops; + g_gpin[i].id = i; + gpio_pin_register(&g_gpin[i].gpio, i); + + /* Configure the pins that will be used as INPUT */ + + esp32_configgpio(g_gpioinputs[i], INPUT_FUNCTION_3); + } +#endif + +#if BOARD_NGPIOINT > 0 + for (i = 0; i < BOARD_NGPIOINT; i++) + { + /* Setup and register the GPIO pin */ + + g_gpint[i].esp32gpio.gpio.gp_pintype = GPIO_INTERRUPT_PIN; + g_gpint[i].esp32gpio.gpio.gp_ops = &gpint_ops; + g_gpint[i].esp32gpio.id = i; + gpio_pin_register(&g_gpint[i].esp32gpio.gpio, i); + + /* Configure the pins that will be used as interrupt input */ + + esp32_configgpio(g_gpiointinputs[i], INPUT_FUNCTION_3 | PULLDOWN); + } +#endif + + return OK; +} +#endif /* CONFIG_DEV_GPIO && !CONFIG_GPIO_LOWER_HALF */ diff --git a/boards/xtensa/esp32/ttgo_lora_esp32/src/esp32_reset.c b/boards/xtensa/esp32/ttgo_lora_esp32/src/esp32_reset.c new file mode 100644 index 00000000000..25a93347fed --- /dev/null +++ b/boards/xtensa/esp32/ttgo_lora_esp32/src/esp32_reset.c @@ -0,0 +1,63 @@ +/**************************************************************************** + * boards/xtensa/esp32/ttgo_lora_esp32/src/esp32_reset.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 + +#ifdef CONFIG_BOARDCTL_RESET + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: board_reset + * + * Description: + * Reset board. Support for this function is required by board-level + * logic if CONFIG_BOARDCTL_RESET is selected. + * + * Input Parameters: + * status - Status information provided with the reset event. This + * meaning of this status information is board-specific. If not + * used by a board, the value zero may be provided in calls to + * board_reset(). + * + * Returned Value: + * If this function returns, then it was not possible to power-off the + * board due to some constraints. The return value in this case is a + * board-specific reason for the failure to shutdown. + * + ****************************************************************************/ + +int board_reset(int status) +{ + up_systemreset(); + + return 0; +} + +#endif /* CONFIG_BOARDCTL_RESET */ diff --git a/boards/xtensa/esp32/ttgo_lora_esp32/src/esp32_spiflash.c b/boards/xtensa/esp32/ttgo_lora_esp32/src/esp32_spiflash.c new file mode 100644 index 00000000000..f4a623af314 --- /dev/null +++ b/boards/xtensa/esp32/ttgo_lora_esp32/src/esp32_spiflash.c @@ -0,0 +1,370 @@ +/**************************************************************************** + * boards/xtensa/esp32/ttgo_lora_esp32/src/esp32_spiflash.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 +#ifdef CONFIG_BCH +#include +#endif + +#include "esp32_spiflash.h" +#include "ttgo_lora_esp32.h" + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +#define ARRAYSIZE(x) (sizeof((x)) / sizeof((x)[0])) + +#define ESP32_MTD_OFFSET CONFIG_ESP32_MTD_OFFSET +#define ESP32_MTD_SIZE CONFIG_ESP32_MTD_SIZE + +/**************************************************************************** + * Private Types + ****************************************************************************/ + +#ifdef CONFIG_ESP32_HAVE_OTA_PARTITION + +struct ota_partition_s +{ + uint32_t offset; /* Partition offset from the beginning of MTD */ + uint32_t size; /* Partition size in bytes */ + const char *devpath; /* Partition device path */ +}; + +#endif + +/**************************************************************************** + * Private Function Prototypes + ****************************************************************************/ + +#ifdef CONFIG_ESP32_HAVE_OTA_PARTITION +static int init_ota_partitions(void); +#endif + +/**************************************************************************** + * Private Data + ****************************************************************************/ + +#ifdef CONFIG_ESP32_HAVE_OTA_PARTITION +static const struct ota_partition_s g_ota_partition_table[] = +{ + { + .offset = CONFIG_ESP32_OTA_PRIMARY_SLOT_OFFSET, + .size = CONFIG_ESP32_OTA_SLOT_SIZE, + .devpath = CONFIG_ESP32_OTA_PRIMARY_SLOT_DEVPATH + }, + { + .offset = CONFIG_ESP32_OTA_SECONDARY_SLOT_OFFSET, + .size = CONFIG_ESP32_OTA_SLOT_SIZE, + .devpath = CONFIG_ESP32_OTA_SECONDARY_SLOT_DEVPATH + }, + { + .offset = CONFIG_ESP32_OTA_SCRATCH_OFFSET, + .size = CONFIG_ESP32_OTA_SCRATCH_SIZE, + .devpath = CONFIG_ESP32_OTA_SCRATCH_DEVPATH + } +}; +#endif + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +#ifdef CONFIG_ESP32_HAVE_OTA_PARTITION +static int init_ota_partitions(void) +{ + FAR struct mtd_dev_s *mtd; +#ifdef CONFIG_BCH + char blockdev[18]; +#endif + int ret = OK; + + for (int i = 0; i < ARRAYSIZE(g_ota_partition_table); ++i) + { + const struct ota_partition_s *part = &g_ota_partition_table[i]; + mtd = esp32_spiflash_alloc_mtdpart(part->offset, part->size); + + ret = ftl_initialize(i, mtd); + if (ret < 0) + { + ferr("ERROR: Failed to initialize the FTL layer: %d\n", ret); + return ret; + } + +#ifdef CONFIG_BCH + snprintf(blockdev, 18, "/dev/mtdblock%d", i); + + ret = bchdev_register(blockdev, part->devpath, false); + if (ret < 0) + { + ferr("ERROR: bchdev_register %s failed: %d\n", part->devpath, ret); + return ret; + } +#endif + } + + return ret; +} +#endif + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: esp32_spiflash_init + * + * Description: + * Initialize the SPIFLASH and register the MTD device. + ****************************************************************************/ + +int esp32_spiflash_init(void) +{ + FAR struct mtd_dev_s *mtd; + int ret = ERROR; + +#ifdef CONFIG_ESP32_HAVE_OTA_PARTITION + ret = init_ota_partitions(); + if (ret < 0) + { + return ret; + } +#endif + + mtd = esp32_spiflash_alloc_mtdpart(ESP32_MTD_OFFSET, ESP32_MTD_SIZE); + +#if defined (CONFIG_ESP32_SPIFLASH_SMARTFS) + ret = smart_initialize(0, mtd, NULL); + if (ret < 0) + { + finfo("smart_initialize failed, Trying to erase first...\n"); + ret = mtd->ioctl(mtd, MTDIOC_BULKERASE, 0); + if (ret < 0) + { + ferr("ERROR: ioctl(BULKERASE) failed: %d\n", ret); + return ret; + } + + finfo("Erase successful, initializing again\n"); + ret = smart_initialize(0, mtd, NULL); + if (ret < 0) + { + ferr("ERROR: smart_initialize failed: %d\n", ret); + return ret; + } + } + +#elif defined (CONFIG_ESP32_SPIFLASH_NXFFS) + ret = nxffs_initialize(mtd); + if (ret < 0) + { + ferr("ERROR: NXFFS init failed: %d\n", ret); + return ret; + } + +#else + ret = register_mtddriver("/dev/esp32flash", mtd, 0755, NULL); + if (ret < 0) + { + ferr("ERROR: Register mtd failed: %d\n", ret); + return ret; + } +#endif + + return ret; +} + +/**************************************************************************** + * Name: esp32_spiflash_encrypt_test + * + * Description: + * Test ESP32 SPI Flash driver read/write with encryption. + * + * Input Parameters: + * None + * + * Returned Value: + * None. + * + ****************************************************************************/ + +#ifdef CONFIG_ESP32_SPIFLASH_ENCRYPTION_TEST + +void esp32_spiflash_encrypt_test(void) +{ + int i; + int ret; + uint8_t *wbuf; + uint8_t *rbuf; + struct mtd_geometry_s geo; + uint32_t erase_block; + uint32_t erase_nblocks; + uint32_t rw_block; + uint32_t rw_nblocks; + struct mtd_dev_s *mtd = esp32_spiflash_get_mtd(); + struct mtd_dev_s *enc_mtd = esp32_spiflash_encrypt_get_mtd(); + const uint32_t address = CONFIG_ESP32_SPIFLASH_TEST_ADDRESS; + const uint32_t size = 4096; + + ret = MTD_IOCTL(enc_mtd, MTDIOC_GEOMETRY, + (unsigned long)(uintptr_t)&geo); + if (ret < 0) + { + ferr("ERROR: Failed to get GEO ret = %d\n", ret); + DEBUGASSERT(0); + } + + wbuf = kmm_malloc(size); + if (!wbuf) + { + ferr("ERROR: Failed to alloc %d heap\n", size); + DEBUGASSERT(0); + } + + rbuf = kmm_malloc(size); + if (!rbuf) + { + ferr("ERROR: Failed to alloc %d heap\n", size); + DEBUGASSERT(0); + } + + for (i = 0; i < size; i++) + { + wbuf[i] = (uint8_t)random(); + } + + erase_block = address / geo.erasesize; + erase_nblocks = size / geo.erasesize; + + rw_block = address / geo.blocksize; + rw_nblocks = size / geo.blocksize; + + ret = MTD_ERASE(enc_mtd, erase_block, erase_nblocks); + if (ret != erase_nblocks) + { + ferr("ERROR: Failed to erase block ret=%d\n", ret); + DEBUGASSERT(0); + } + + ret = MTD_BWRITE(enc_mtd, rw_block, rw_nblocks, wbuf); + if (ret != rw_nblocks) + { + ferr("ERROR: Failed to encrypt write ret=%d\n", ret); + DEBUGASSERT(0); + } + + memset(rbuf, 0, size); + ret = MTD_BREAD(enc_mtd, rw_block, rw_nblocks, rbuf); + if (ret != rw_nblocks) + { + ferr("ERROR: Failed to decrypt read ret=%d\n", ret); + DEBUGASSERT(0); + } + + if (memcmp(wbuf, rbuf, size)) + { + ferr("ASSERT: Encrypted and decrypted data is not same\n"); + DEBUGASSERT(0); + } + + memset(rbuf, 0, size); + ret = MTD_BREAD(mtd, rw_block, rw_nblocks, rbuf); + if (ret != rw_nblocks) + { + ferr("ERROR: Failed to read ret=%d\n", ret); + DEBUGASSERT(0); + } + + if (!memcmp(wbuf, rbuf, size)) + { + ferr("ASSERT: Encrypted and normal data is same\n"); + DEBUGASSERT(0); + } + + for (i = 0; i < size; i++) + { + wbuf[i] = (uint8_t)random(); + } + + ret = MTD_ERASE(enc_mtd, erase_block, erase_nblocks); + if (ret != erase_nblocks) + { + ferr("ERROR: Failed to erase ret=%d\n", ret); + DEBUGASSERT(0); + } + + ret = MTD_BWRITE(mtd, rw_block, rw_nblocks, wbuf); + if (ret != rw_nblocks) + { + ferr("ERROR: Failed to write ret=%d\n", ret); + DEBUGASSERT(0); + } + + memset(rbuf, 0, size); + ret = MTD_BREAD(enc_mtd, rw_block, rw_nblocks, rbuf); + if (ret != rw_nblocks) + { + ferr("ERROR: Failed to decrypt read ret=%d\n", ret); + DEBUGASSERT(0); + } + + if (!memcmp(wbuf, rbuf, size)) + { + ferr("ASSERT: Normal and decrypted data is same\n"); + DEBUGASSERT(0); + } + + memset(rbuf, 0, size); + ret = MTD_BREAD(mtd, rw_block, rw_nblocks, rbuf); + if (ret != rw_nblocks) + { + ferr("ERROR: Failed to read ret=%d\n", ret); + DEBUGASSERT(0); + } + + if (memcmp(wbuf, rbuf, size)) + { + ferr("ASSERT: Normal and normal data is not same\n"); + DEBUGASSERT(0); + } + + kmm_free(wbuf); + kmm_free(rbuf); + + finfo("INFO: ESP32 SPI Flash encryption test successfully\n"); +} + +#endif /* CONFIG_ESP32_SPIFLASH_ENCRYPTION_TEST */ diff --git a/boards/xtensa/esp32/ttgo_lora_esp32/src/esp32_userleds.c b/boards/xtensa/esp32/ttgo_lora_esp32/src/esp32_userleds.c new file mode 100644 index 00000000000..3e89b7a6389 --- /dev/null +++ b/boards/xtensa/esp32/ttgo_lora_esp32/src/esp32_userleds.c @@ -0,0 +1,95 @@ +/**************************************************************************** + * boards/xtensa/esp32/ttgo_lora_esp32/src/esp32_userleds.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 "esp32_gpio.h" +#include "ttgo_lora_esp32.h" + +/**************************************************************************** + * Private Data + ****************************************************************************/ + +/* This array maps an LED number to GPIO pin configuration */ + +static const uint32_t g_ledcfg[BOARD_NLEDS] = +{ + GPIO_LED1, +}; + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: board_userled_initialize + ****************************************************************************/ + +uint32_t board_userled_initialize(void) +{ + uint8_t i; + + for (i = 0; i < BOARD_NLEDS; i++) + { + esp32_configgpio(g_ledcfg[i], OUTPUT); + } + + return BOARD_NLEDS; +} + +/**************************************************************************** + * Name: board_userled + ****************************************************************************/ + +void board_userled(int led, bool ledon) +{ + if ((unsigned)led < BOARD_NLEDS) + { + esp32_gpiowrite(g_ledcfg[led], ledon); + } +} + +/**************************************************************************** + * Name: board_userled_all + ****************************************************************************/ + +void board_userled_all(uint32_t ledset) +{ + uint8_t i; + + /* Configure LED1-8 GPIOs for output */ + + for (i = 0; i < BOARD_NLEDS; i++) + { + esp32_gpiowrite(g_ledcfg[i], (ledset & (1 << i)) != 0); + } +} + 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 new file mode 100644 index 00000000000..cd57b39509a --- /dev/null +++ b/boards/xtensa/esp32/ttgo_lora_esp32/src/ttgo_lora_esp32.h @@ -0,0 +1,159 @@ +/**************************************************************************** + * boards/xtensa/esp32/ttgo_lora_esp32/src/ttgo_lora_esp32.h + * + * 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. + * + ****************************************************************************/ + +#ifndef __BOARDS_XTENSA_ESP32_TTGO_LORA_ESP32_SRC_TTGO_LORA_ESP32_H +#define __BOARDS_XTENSA_ESP32_TTGO_LORA_ESP32_SRC_TTGO_LORA_ESP32_H + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include +#include +#include + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +/* TTGO-LoRa-SX1276-ESP32 GPIOs *********************************************/ + +/* BOOT Button */ + +#define BUTTON_BOOT 0 + +/* LED + * + * This is an externally connected LED used for testing. + */ + +#define GPIO_LED1 2 + +/* MCP2515 Interrupt pin */ + +#define GPIO_MCP2515_IRQ 22 + +/* TIMERS */ + +#define TIMER0 0 +#define TIMER1 1 +#define TIMER2 2 +#define TIMER3 3 + +/* ONESHOT */ + +#define ONESHOT_TIMER 1 +#define ONESHOT_RESOLUTION_US 1 + +/**************************************************************************** + * Public Types + ****************************************************************************/ + +/**************************************************************************** + * Public Data + ****************************************************************************/ + +#ifndef __ASSEMBLY__ + +/**************************************************************************** + * Public Function Prototypes + ****************************************************************************/ + +/**************************************************************************** + * Name: esp32_bringup + * + * Description: + * Perform architecture-specific initialization + * + * CONFIG_BOARD_LATE_INITIALIZE=y : + * Called from board_late_initialize(). + * + * CONFIG_BOARD_LATE_INITIALIZE=y && CONFIG_BOARDCTL=y : + * Called from the NSH library via board_app_initialize() + * + ****************************************************************************/ + +int esp32_bringup(void); + +/**************************************************************************** + * Name: esp32_mmcsd_initialize + * + * Description: + * Initialize SPI-based SD card and card detect thread. + ****************************************************************************/ + +int esp32_mmcsd_initialize(int minor); + +/**************************************************************************** + * Name: esp32_spiflash_init + * + * Description: + * Initialize the SPIFLASH and register the MTD device. + ****************************************************************************/ + +int esp32_spiflash_init(void); + +/**************************************************************************** + * Name: esp32_spiflash_encrypt_test + * + * Description: + * Test ESP32 SPI Flash driver read/write with encryption. + * + * Input Parameters: + * None + * + * Returned Value: + * None. + * + ****************************************************************************/ + +#ifdef CONFIG_ESP32_SPIFLASH_ENCRYPTION_TEST +void esp32_spiflash_encrypt_test(void); +#endif + +/**************************************************************************** + * Name: esp32_gpio_init + ****************************************************************************/ + +#ifdef CONFIG_DEV_GPIO +int esp32_gpio_init(void); +#endif + +/**************************************************************************** + * Name: board_spidev_initialize + * + * Description: + * Initialize SPI driver and register the /dev/spi device. + * + * Input Parameters: + * bus - The SPI bus number, used to build the device path as /dev/spiN + * + * Returned Value: + * Zero (OK) is returned on success; A negated errno value is returned + * to indicate the nature of any failure. + * + ****************************************************************************/ + +#ifdef CONFIG_SPI_DRIVER +int board_spidev_initialize(int bus); +#endif + +#endif /* __ASSEMBLY__ */ +#endif /* __BOARDS_XTENSA_ESP32_TTGO_LORA_ESP32_SRC_TTGO_LORA_ESP32_H */