From 63a847ebca30619f1346109cab3939f45e1a5b1e Mon Sep 17 00:00:00 2001 From: Alan Carvalho de Assis Date: Tue, 31 Jan 2023 17:36:18 -0300 Subject: [PATCH] esp32s3-devkit: Add support LED PWM initialization --- arch/xtensa/src/esp32s3/esp32s3_ledc.c | 2 +- .../esp32s3-devkit/configs/ledpwm/defconfig | 51 +++++++ .../esp32s3/esp32s3-devkit/src/Make.defs | 4 + .../esp32s3-devkit/src/esp32s3-devkit.h | 12 ++ .../esp32s3-devkit/src/esp32s3_bringup.c | 12 ++ .../esp32s3/esp32s3-devkit/src/esp32s3_ledc.c | 139 ++++++++++++++++++ 6 files changed, 219 insertions(+), 1 deletion(-) create mode 100644 boards/xtensa/esp32s3/esp32s3-devkit/configs/ledpwm/defconfig create mode 100644 boards/xtensa/esp32s3/esp32s3-devkit/src/esp32s3_ledc.c diff --git a/arch/xtensa/src/esp32s3/esp32s3_ledc.c b/arch/xtensa/src/esp32s3/esp32s3_ledc.c index 6175d6c052..a44fd7feed 100644 --- a/arch/xtensa/src/esp32s3/esp32s3_ledc.c +++ b/arch/xtensa/src/esp32s3/esp32s3_ledc.c @@ -158,7 +158,7 @@ struct esp32s3_ledc_s const uint8_t num; /* Timer ID */ const uint8_t channels; /* Timer channels number */ - struct esp32s3_ledc_chan_s *chans; /* Timer channels pointer */ + struct esp32s3_ledc_chan_s *chans; /* Timer channels pointer */ uint32_t frequency; /* Timer current frequency */ uint32_t reload; /* Timer current reload */ diff --git a/boards/xtensa/esp32s3/esp32s3-devkit/configs/ledpwm/defconfig b/boards/xtensa/esp32s3/esp32s3-devkit/configs/ledpwm/defconfig new file mode 100644 index 0000000000..3899fbe062 --- /dev/null +++ b/boards/xtensa/esp32s3/esp32s3-devkit/configs/ledpwm/defconfig @@ -0,0 +1,51 @@ +# +# 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="esp32s3-devkit" +CONFIG_ARCH_BOARD_COMMON=y +CONFIG_ARCH_BOARD_ESP32S3_DEVKIT=y +CONFIG_ARCH_CHIP="esp32s3" +CONFIG_ARCH_CHIP_ESP32S3=y +CONFIG_ARCH_CHIP_ESP32S3WROOM1=y +CONFIG_ARCH_STACKDUMP=y +CONFIG_ARCH_XTENSA=y +CONFIG_BOARD_LOOPSPERMSEC=16717 +CONFIG_BUILTIN=y +CONFIG_DEBUG_FULLOPT=y +CONFIG_DEBUG_SYMBOLS=y +CONFIG_ESP32S3_LEDC=y +CONFIG_ESP32S3_LEDC_TIM0=y +CONFIG_ESP32S3_LEDC_TIM1=y +CONFIG_ESP32S3_LEDC_TIM2=y +CONFIG_ESP32S3_LEDC_TIM3=y +CONFIG_ESP32S3_UART0=y +CONFIG_EXAMPLES_PWM=y +CONFIG_FS_PROCFS=y +CONFIG_HAVE_CXX=y +CONFIG_HAVE_CXXINITIALIZE=y +CONFIG_IDLETHREAD_STACKSIZE=3072 +CONFIG_INIT_ENTRYPOINT="nsh_main" +CONFIG_INTELHEX_BINARY=y +CONFIG_NSH_ARCHINIT=y +CONFIG_NSH_BUILTIN_APPS=y +CONFIG_NSH_FILEIOSIZE=512 +CONFIG_NSH_LINELEN=64 +CONFIG_NSH_READLINE=y +CONFIG_PREALLOC_TIMERS=4 +CONFIG_RAM_SIZE=114688 +CONFIG_RAM_START=0x20000000 +CONFIG_RR_INTERVAL=200 +CONFIG_SCHED_WAITPID=y +CONFIG_START_DAY=6 +CONFIG_START_MONTH=12 +CONFIG_START_YEAR=2011 +CONFIG_SYSTEM_NSH=y +CONFIG_UART0_SERIAL_CONSOLE=y diff --git a/boards/xtensa/esp32s3/esp32s3-devkit/src/Make.defs b/boards/xtensa/esp32s3/esp32s3-devkit/src/Make.defs index 875b096532..d16ef2ba90 100644 --- a/boards/xtensa/esp32s3/esp32s3-devkit/src/Make.defs +++ b/boards/xtensa/esp32s3/esp32s3-devkit/src/Make.defs @@ -41,6 +41,10 @@ ifeq ($(CONFIG_ESP32S3_SPI),y) CSRCS += esp32s3_board_spi.c endif +ifeq ($(CONFIG_PWM),y) +CSRCS += esp32s3_ledc.c +endif + ifeq ($(CONFIG_LCD_ST7735),y) CSRCS += esp32s3_st7735.c endif diff --git a/boards/xtensa/esp32s3/esp32s3-devkit/src/esp32s3-devkit.h b/boards/xtensa/esp32s3/esp32s3-devkit/src/esp32s3-devkit.h index aedb1a82e5..4c722b6e07 100644 --- a/boards/xtensa/esp32s3/esp32s3-devkit/src/esp32s3-devkit.h +++ b/boards/xtensa/esp32s3/esp32s3-devkit/src/esp32s3-devkit.h @@ -133,5 +133,17 @@ int board_bmp180_initialize(int devno, int busno); int esp32s3_djoy_initialize(void); #endif +/**************************************************************************** + * Name: esp32s3_ledc_setup + * + * Description: + * Initialize LEDC PWM and register the PWM device. + * + ****************************************************************************/ + +#ifdef CONFIG_ESP32S3_LEDC +int esp32s3_pwm_setup(void); +#endif + #endif /* __ASSEMBLY__ */ #endif /* __BOARDS_XTENSA_ESP32S3_ESP32S3_DEVKIT_SRC_ESP32S3_DEVKIT_H */ diff --git a/boards/xtensa/esp32s3/esp32s3-devkit/src/esp32s3_bringup.c b/boards/xtensa/esp32s3/esp32s3-devkit/src/esp32s3_bringup.c index c301e3dde4..7d93dd2070 100644 --- a/boards/xtensa/esp32s3/esp32s3-devkit/src/esp32s3_bringup.c +++ b/boards/xtensa/esp32s3/esp32s3-devkit/src/esp32s3_bringup.c @@ -66,6 +66,10 @@ # include "esp32s3_efuse.h" #endif +#ifdef CONFIG_ESP32S3_LEDC +# include "esp32s3_ledc.h" +#endif + #include "esp32s3-devkit.h" /**************************************************************************** @@ -119,6 +123,14 @@ int esp32s3_bringup(void) } #endif +#ifdef CONFIG_ESP32S3_LEDC + ret = esp32s3_pwm_setup(); + if (ret < 0) + { + syslog(LOG_ERR, "ERROR: esp32s3_pwm_setup() failed: %d\n", ret); + } +#endif /* CONFIG_ESP32S3_LEDC */ + #ifdef CONFIG_ESP32S3_TIMER /* Configure general purpose timers */ diff --git a/boards/xtensa/esp32s3/esp32s3-devkit/src/esp32s3_ledc.c b/boards/xtensa/esp32s3/esp32s3-devkit/src/esp32s3_ledc.c new file mode 100644 index 0000000000..04ab043912 --- /dev/null +++ b/boards/xtensa/esp32s3/esp32s3-devkit/src/esp32s3_ledc.c @@ -0,0 +1,139 @@ +/**************************************************************************** + * boards/xtensa/esp32s3/esp32s3-devkit/src/esp32s3_ledc.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 "chip.h" +#include "esp32s3_ledc.h" + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: esp32s3_pwm_setup + * + * Description: + * Initialize LEDC PWM and register the PWM device. + * + ****************************************************************************/ + +int esp32s3_pwm_setup(void) +{ + int ret; + struct pwm_lowerhalf_s *pwm; + +#ifdef CONFIG_ESP32S3_LEDC_TIM0 + pwm = esp32s3_ledc_init(0); + if (!pwm) + { + syslog(LOG_ERR, "ERROR: Failed to get the LEDC PWM 0 lower half\n"); + return -ENODEV; + } + + /* Register the PWM driver at "/dev/pwm0" */ + + ret = pwm_register("/dev/pwm0", pwm); + if (ret < 0) + { + syslog(LOG_ERR, "ERROR: pwm_register failed: %d\n", ret); + return ret; + } +#endif + +#ifdef CONFIG_ESP32S3_LEDC_TIM1 + pwm = esp32s3_ledc_init(1); + if (!pwm) + { + syslog(LOG_ERR, "ERROR: Failed to get the LEDC PWM 1 lower half\n"); + return -ENODEV; + } + + /* Register the PWM driver at "/dev/pwm1" */ + + ret = pwm_register("/dev/pwm1", pwm); + if (ret < 0) + { + syslog(LOG_ERR, "ERROR: pwm_register failed: %d\n", ret); + return ret; + } +#endif + +#ifdef CONFIG_ESP32S3_LEDC_TIM2 + pwm = esp32s3_ledc_init(2); + if (!pwm) + { + syslog(LOG_ERR, "ERROR: Failed to get the LEDC PWM 2 lower half\n"); + return -ENODEV; + } + + /* Register the PWM driver at "/dev/pwm2" */ + + ret = pwm_register("/dev/pwm2", pwm); + if (ret < 0) + { + syslog(LOG_ERR, "ERROR: pwm_register failed: %d\n", ret); + return ret; + } +#endif + +#ifdef CONFIG_ESP32S3_LEDC_TIM3 + pwm = esp32s3_ledc_init(3); + if (!pwm) + { + syslog(LOG_ERR, "ERROR: Failed to get the LEDC PWM 3 lower half\n"); + return -ENODEV; + } + + /* Register the PWM driver at "/dev/pwm3" */ + + ret = pwm_register("/dev/pwm3", pwm); + if (ret < 0) + { + syslog(LOG_ERR, "ERROR: pwm_register failed: %d\n", ret); + return ret; + } +#endif + + /* May not be used if none LEDC TIMx is enabled */ + + UNUSED(ret); + UNUSED(pwm); + + return OK; +} +