From 47e71fc4495bae891d707ba874c33a0365b1f45b Mon Sep 17 00:00:00 2001 From: Tiago Medicci Serrano Date: Thu, 7 Dec 2023 17:49:06 -0300 Subject: [PATCH] esp32s2/rmt: Use the Espressif's common RMT driver. This commit use the new common RMT driver for all Espressif's xtensa-based chips on ESP32-S2. --- .../common/include/esp32s2_board_rmt.h | 97 +++++++++++ boards/xtensa/esp32s2/common/src/Make.defs | 4 + .../esp32s2/common/src/esp32s2_board_rmt.c | 152 ++++++++++++++++++ .../esp32s2-saola-1/configs/rmt/defconfig | 58 +++++++ .../esp32s2-saola-1/src/esp32s2-saola-1.h | 13 ++ .../esp32s2-saola-1/src/esp32s2_bringup.c | 18 +++ 6 files changed, 342 insertions(+) create mode 100644 boards/xtensa/esp32s2/common/include/esp32s2_board_rmt.h create mode 100644 boards/xtensa/esp32s2/common/src/esp32s2_board_rmt.c create mode 100644 boards/xtensa/esp32s2/esp32s2-saola-1/configs/rmt/defconfig diff --git a/boards/xtensa/esp32s2/common/include/esp32s2_board_rmt.h b/boards/xtensa/esp32s2/common/include/esp32s2_board_rmt.h new file mode 100644 index 0000000000..1b4026f648 --- /dev/null +++ b/boards/xtensa/esp32s2/common/include/esp32s2_board_rmt.h @@ -0,0 +1,97 @@ +/**************************************************************************** + * boards/xtensa/esp32s2/common/include/esp32s2_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_ESP32S2_COMMON_INCLUDE_ESP32S2_BOARD_RMT_H +#define __BOARDS_XTENSA_ESP32S2_COMMON_INCLUDE_ESP32S2_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_ESP32S2_COMMON_INCLUDE_ESP32S2_BOARD_RMT_H */ diff --git a/boards/xtensa/esp32s2/common/src/Make.defs b/boards/xtensa/esp32s2/common/src/Make.defs index e4c1769b08..2fde45ed4c 100644 --- a/boards/xtensa/esp32s2/common/src/Make.defs +++ b/boards/xtensa/esp32s2/common/src/Make.defs @@ -68,6 +68,10 @@ ifeq ($(CONFIG_AUDIO_ES8311),y) CSRCS += esp32s2_es8311.c endif +ifeq ($(CONFIG_ESP_RMT),y) + CSRCS += esp32s2_board_rmt.c +endif + DEPPATH += --dep-path src VPATH += :src CFLAGS += ${INCDIR_PREFIX}$(TOPDIR)$(DELIM)arch$(DELIM)$(CONFIG_ARCH)$(DELIM)src$(DELIM)board$(DELIM)src diff --git a/boards/xtensa/esp32s2/common/src/esp32s2_board_rmt.c b/boards/xtensa/esp32s2/common/src/esp32s2_board_rmt.c new file mode 100644 index 0000000000..2cdd10164b --- /dev/null +++ b/boards/xtensa/esp32s2/common/src/esp32s2_board_rmt.c @@ -0,0 +1,152 @@ +/**************************************************************************** + * boards/xtensa/esp32s2/common/src/esp32s2_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/esp32s2/esp32s2-saola-1/configs/rmt/defconfig b/boards/xtensa/esp32s2/esp32s2-saola-1/configs/rmt/defconfig new file mode 100644 index 0000000000..1cdf6a2723 --- /dev/null +++ b/boards/xtensa/esp32s2/esp32s2-saola-1/configs/rmt/defconfig @@ -0,0 +1,58 @@ +# +# 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_ASSERTIONS_FILENAME is not set +# CONFIG_NDEBUG is not set +# CONFIG_NSH_ARGCAT is not set +# CONFIG_NSH_CMDOPT_HEXDUMP is not set +CONFIG_ARCH="xtensa" +CONFIG_ARCH_BOARD="esp32s2-saola-1" +CONFIG_ARCH_BOARD_COMMON=y +CONFIG_ARCH_BOARD_ESP32S2_SAOLA_1=y +CONFIG_ARCH_CHIP="esp32s2" +CONFIG_ARCH_CHIP_ESP32S2=y +CONFIG_ARCH_CHIP_ESP32S2WROVER=y +CONFIG_ARCH_STACKDUMP=y +CONFIG_ARCH_XTENSA=y +CONFIG_BOARD_LOOPSPERMSEC=16717 +CONFIG_BUILTIN=y +CONFIG_ESP32S2_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_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/esp32s2/esp32s2-saola-1/src/esp32s2-saola-1.h b/boards/xtensa/esp32s2/esp32s2-saola-1/src/esp32s2-saola-1.h index afbdf5b74d..768efbad6e 100644 --- a/boards/xtensa/esp32s2/esp32s2-saola-1/src/esp32s2-saola-1.h +++ b/boards/xtensa/esp32s2/esp32s2-saola-1/src/esp32s2-saola-1.h @@ -51,6 +51,19 @@ #define ONESHOT_TIMER TIMER0 #define ONESHOT_RESOLUTION_US 1 +/* RMT gpio */ + +#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/esp32s2/esp32s2-saola-1/src/esp32s2_bringup.c b/boards/xtensa/esp32s2/esp32s2-saola-1/src/esp32s2_bringup.c index 9c5bf5ce0c..97dbc62249 100644 --- a/boards/xtensa/esp32s2/esp32s2-saola-1/src/esp32s2_bringup.c +++ b/boards/xtensa/esp32s2/esp32s2-saola-1/src/esp32s2_bringup.c @@ -88,6 +88,10 @@ # include "esp32s2_rtc_lowerhalf.h" #endif +#ifdef CONFIG_ESP_RMT +# include "esp32s2_board_rmt.h" +#endif + #include "esp32s2-saola-1.h" /**************************************************************************** @@ -362,6 +366,20 @@ int esp32s2_bringup(void) #endif /* CONFIG_ESP32S2_I2S */ +#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 */