From 5ba40d083a6547b6a751434d3947ab64c48d78be Mon Sep 17 00:00:00 2001 From: Tiago Medicci Serrano Date: Fri, 15 Dec 2023 10:12:32 -0300 Subject: [PATCH] esp32/rmt: Use the Espressif`s common RMT driver. This commit implements the common RMT driver (already available for the other Espressif's xtensa-based devices) for ESP32. This allows us to have a proper separation between the lower and upper-half drivers and use the 'ws2812' example to drive WS2812 RGB LEDs. --- arch/xtensa/src/esp32/Kconfig | 2 + arch/xtensa/src/esp32/esp32_gpio.c | 2 + arch/xtensa/src/esp32/esp32_gpio.h | 18 ++ .../esp32/common/include/esp32_board_rmt.h | 97 +++++++++++ boards/xtensa/esp32/common/src/Make.defs | 4 +- .../xtensa/esp32/common/src/esp32_board_rmt.c | 155 ++++++++++++++++++ .../esp32/esp32-devkitc/configs/rmt/defconfig | 57 +++++++ .../esp32/esp32-devkitc/src/esp32-devkitc.h | 12 +- .../esp32/esp32-devkitc/src/esp32_bringup.c | 20 ++- 9 files changed, 362 insertions(+), 5 deletions(-) create mode 100644 boards/xtensa/esp32/common/include/esp32_board_rmt.h create mode 100644 boards/xtensa/esp32/common/src/esp32_board_rmt.c create mode 100644 boards/xtensa/esp32/esp32-devkitc/configs/rmt/defconfig diff --git a/arch/xtensa/src/esp32/Kconfig b/arch/xtensa/src/esp32/Kconfig index 8e24e6f747..be4846b3c8 100644 --- a/arch/xtensa/src/esp32/Kconfig +++ b/arch/xtensa/src/esp32/Kconfig @@ -213,6 +213,8 @@ config ESP32_EXCEPTION_ENABLE_CACHE menu "ESP32 Peripheral Selection" +source "arch/xtensa/src/common/espressif/Kconfig" + config ESP32_UART bool default n diff --git a/arch/xtensa/src/esp32/esp32_gpio.c b/arch/xtensa/src/esp32/esp32_gpio.c index e6af92aba9..7f36eb7fb3 100644 --- a/arch/xtensa/src/esp32/esp32_gpio.c +++ b/arch/xtensa/src/esp32/esp32_gpio.c @@ -35,6 +35,8 @@ #include "xtensa.h" +#include "soc/soc_caps.h" + #include "hardware/esp32_iomux.h" #include "hardware/esp32_gpio.h" diff --git a/arch/xtensa/src/esp32/esp32_gpio.h b/arch/xtensa/src/esp32/esp32_gpio.h index 5d7cc8518e..a581bbac76 100644 --- a/arch/xtensa/src/esp32/esp32_gpio.h +++ b/arch/xtensa/src/esp32/esp32_gpio.h @@ -102,6 +102,24 @@ #define ONLOW_WE 0x0c #define ONHIGH_WE 0x0d +/* Check whether it is a valid GPIO number */ + +#define GPIO_IS_VALID_GPIO(gpio_num) ((gpio_num >= 0) && \ + (((1ULL << (gpio_num)) & \ + SOC_GPIO_VALID_GPIO_MASK) != 0)) + +/* Check whether it can be a valid GPIO number of output mode */ + +#define GPIO_IS_VALID_OUTPUT_GPIO(gpio_num) \ + ((gpio_num >= 0) && \ + (((1ULL << (gpio_num)) & SOC_GPIO_VALID_OUTPUT_GPIO_MASK) != 0)) + +/* Check whether it can be a valid digital I/O pad */ + +#define GPIO_IS_VALID_DIGITAL_IO_PAD(gpio_num) \ + ((gpio_num >= 0) && \ + (((1ULL << (gpio_num)) & SOC_GPIO_VALID_DIGITAL_IO_PAD_MASK) != 0)) + /**************************************************************************** * Public Types ****************************************************************************/ diff --git a/boards/xtensa/esp32/common/include/esp32_board_rmt.h b/boards/xtensa/esp32/common/include/esp32_board_rmt.h new file mode 100644 index 0000000000..afb7cdb0f9 --- /dev/null +++ b/boards/xtensa/esp32/common/include/esp32_board_rmt.h @@ -0,0 +1,97 @@ +/**************************************************************************** + * boards/xtensa/esp32/common/include/esp32_board_rmt.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_COMMON_INCLUDE_ESP32_BOARD_RMT_H +#define __BOARDS_XTENSA_ESP32_COMMON_INCLUDE_ESP32_BOARD_RMT_H + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +#ifndef __ASSEMBLY__ + +/**************************************************************************** + * Public Data + ****************************************************************************/ + +#undef EXTERN +#if defined(__cplusplus) +#define EXTERN extern "C" +extern "C" +{ +#else +#define EXTERN extern +#endif + +/**************************************************************************** + * Public Function Prototypes + ****************************************************************************/ + +#ifdef CONFIG_ESP_RMT + +/**************************************************************************** + * Name: board_rmt_rxinitialize + * + * Description: + * Initialize the RMT peripheral and register a RX device. + * + * Input Parameters: + * ch - the RMT's channel that will be used + * pin - The pin used for the RX channel + * + * Returned Value: + * Zero (OK) on success; a negated errno value on failure. + * + ****************************************************************************/ + +int board_rmt_rxinitialize(int ch, int pin); + +/**************************************************************************** + * Name: board_rmt_txinitialize + * + * Description: + * Initialize the RMT peripheral and register a TX device. + * + * Input Parameters: + * ch - the RMT's channel that will be used + * pin - The pin used for the TX channel + * + * Returned Value: + * Zero (OK) on success; a negated errno value on failure. + * + ****************************************************************************/ + +int board_rmt_txinitialize(int ch, int pin); + +#endif /* CONFIG_ESP_RMT */ + +#undef EXTERN +#if defined(__cplusplus) +} +#endif + +#endif /* __ASSEMBLY__ */ +#endif /* __BOARDS_XTENSA_ESP32_COMMON_INCLUDE_ESP32_BOARD_RMT_H */ diff --git a/boards/xtensa/esp32/common/src/Make.defs b/boards/xtensa/esp32/common/src/Make.defs index 41ff806de8..a7f05e7097 100644 --- a/boards/xtensa/esp32/common/src/Make.defs +++ b/boards/xtensa/esp32/common/src/Make.defs @@ -140,8 +140,8 @@ ifeq ($(CONFIG_RGBLED),y) CSRCS += esp32_rgbled.c endif -ifeq ($(CONFIG_ESP32_RMT),y) - CSRCS += esp32_rmt.c +ifeq ($(CONFIG_ESP_RMT),y) + CSRCS += esp32_board_rmt.c endif #ifeq ($(CONFIG_DAC),y) diff --git a/boards/xtensa/esp32/common/src/esp32_board_rmt.c b/boards/xtensa/esp32/common/src/esp32_board_rmt.c new file mode 100644 index 0000000000..cf78ae127e --- /dev/null +++ b/boards/xtensa/esp32/common/src/esp32_board_rmt.c @@ -0,0 +1,155 @@ +/**************************************************************************** + * boards/xtensa/esp32/common/src/esp32_board_rmt.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 "xtensa.h" + +#include +#include +#ifdef CONFIG_WS2812_NON_SPI_DRIVER +#include + +#include "espressif/esp_ws2812.h" +#endif + +#include "espressif/esp_rmt.h" + +#ifdef CONFIG_ESP_RMT + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +/**************************************************************************** + * Private Types + ****************************************************************************/ + +/**************************************************************************** + * Private Function Prototypes + ****************************************************************************/ + +/**************************************************************************** + * Private Data + ****************************************************************************/ + +/**************************************************************************** + * Public Data + ****************************************************************************/ + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: board_rmt_rxinitialize + * + * Description: + * Initialize the RMT peripheral and register a RX device. + * + * Input Parameters: + * ch - the RMT's channel that will be used + * pin - The pin used for the RX channel + * + * Returned Value: + * Zero (OK) on success; a negated errno value on failure. + * + ****************************************************************************/ + +int board_rmt_rxinitialize(int ch, int pin) +{ + int ret; + + struct rmt_dev_s *rmt = esp_rmt_rx_init(ch, pin); + + ret = rmtchar_register(rmt); + if (ret < 0) + { + rmterr("ERROR: rmtchar_register failed: %d\n", ret); + return ret; + } + + return ret; +} + +/**************************************************************************** + * Name: board_rmt_txinitialize + * + * Description: + * Initialize the RMT peripheral and register a TX device. + * + * Input Parameters: + * ch - the RMT's channel that will be used + * pin - The pin used for the TX channel + * + * Returned Value: + * Zero (OK) on success; a negated errno value on failure. + * + ****************************************************************************/ + +int board_rmt_txinitialize(int ch, int pin) +{ + int ret; + struct rmt_dev_s *rmt; +#ifdef CONFIG_WS2812_NON_SPI_DRIVER + struct ws2812_dev_s *led; +#endif + + rmt = esp_rmt_tx_init(ch, pin); + + if (rmt == NULL) + { + rmterr("ERROR: esp_rmt_tx_init failed\n"); + return -ENODEV; + } + + ret = rmtchar_register(rmt); + if (ret < 0) + { + rmterr("ERROR: rmtchar_register failed: %d\n", ret); + return ret; + } + +#ifdef CONFIG_WS2812_NON_SPI_DRIVER + led = esp_ws2812_setup("/dev/leds0", rmt, + CONFIG_WS2812_LED_COUNT, false); + + if (led == NULL) + { + rmterr("ERROR: esp_ws2812_setup failed\n"); + return -ENODEV; + } +#endif + + return ret; +} +#endif diff --git a/boards/xtensa/esp32/esp32-devkitc/configs/rmt/defconfig b/boards/xtensa/esp32/esp32-devkitc/configs/rmt/defconfig new file mode 100644 index 0000000000..3878b2eace --- /dev/null +++ b/boards/xtensa/esp32/esp32-devkitc/configs/rmt/defconfig @@ -0,0 +1,57 @@ +# +# 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_NSH_ARGCAT is not set +# CONFIG_NSH_CMDOPT_HEXDUMP is not set +CONFIG_ARCH="xtensa" +CONFIG_ARCH_BOARD="esp32-devkitc" +CONFIG_ARCH_BOARD_COMMON=y +CONFIG_ARCH_BOARD_ESP32_DEVKITC=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_ESP32_UART0=y +CONFIG_ESP_RMT=y +CONFIG_EXAMPLES_RMTCHAR=y +CONFIG_EXAMPLES_RMTCHAR_RX=y +CONFIG_EXAMPLES_RMTCHAR_TX=y +CONFIG_EXAMPLES_WS2812=y +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_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_RMT=y +CONFIG_RMTCHAR=y +CONFIG_RMT_DEFAULT_RX_BUFFER_SIZE=256 +CONFIG_RR_INTERVAL=200 +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 +CONFIG_WS2812=y +CONFIG_WS2812_LED_COUNT=100 +CONFIG_WS2812_NON_SPI_DRIVER=y diff --git a/boards/xtensa/esp32/esp32-devkitc/src/esp32-devkitc.h b/boards/xtensa/esp32/esp32-devkitc/src/esp32-devkitc.h index b81544a589..0e3ad3f7d7 100644 --- a/boards/xtensa/esp32/esp32-devkitc/src/esp32-devkitc.h +++ b/boards/xtensa/esp32/esp32-devkitc/src/esp32-devkitc.h @@ -68,8 +68,16 @@ /* RMT gpio */ -#define RMT_OUTPUT_PIN 4 -#define RMT_CHANNEL 0 +#define RMT_RXCHANNEL 1 +#define RMT_TXCHANNEL 0 + +#ifdef CONFIG_RMT_LOOP_TEST_MODE +# define RMT_INPUT_PIN 0 +# define RMT_OUTPUT_PIN 0 +#else +# define RMT_INPUT_PIN 2 +# define RMT_OUTPUT_PIN 4 +#endif /**************************************************************************** * Public Types diff --git a/boards/xtensa/esp32/esp32-devkitc/src/esp32_bringup.c b/boards/xtensa/esp32/esp32-devkitc/src/esp32_bringup.c index d062d49929..a4f0bb69b7 100644 --- a/boards/xtensa/esp32/esp32-devkitc/src/esp32_bringup.c +++ b/boards/xtensa/esp32/esp32-devkitc/src/esp32_bringup.c @@ -165,6 +165,10 @@ # include "esp32_board_dac.h" #endif +#ifdef CONFIG_ESP_RMT +# include "esp32_board_rmt.h" +#endif + #include "esp32-devkitc.h" /**************************************************************************** @@ -653,6 +657,20 @@ int esp32_bringup(void) } #endif +#ifdef CONFIG_ESP_RMT + ret = board_rmt_txinitialize(RMT_TXCHANNEL, RMT_OUTPUT_PIN); + if (ret < 0) + { + syslog(LOG_ERR, "ERROR: board_rmt_txinitialize() failed: %d\n", ret); + } + + ret = board_rmt_rxinitialize(RMT_RXCHANNEL, RMT_INPUT_PIN); + if (ret < 0) + { + syslog(LOG_ERR, "ERROR: board_rmt_txinitialize() failed: %d\n", ret); + } +#endif + #ifdef CONFIG_RTC_DRIVER /* Instantiate the ESP32 RTC driver */ @@ -676,7 +694,7 @@ int esp32_bringup(void) #endif #ifdef CONFIG_WS2812 -# ifndef CONFIG_WS2812_NON_SPI_DRIVER +# ifndef CONFIG_WS2812_NON_SPI_DRIVER ret = board_ws2812_initialize(0, ESP32_SPI3, CONFIG_WS2812_LED_COUNT); if (ret < 0) {