boards/risc-v/espressif: Add PWM support for ESP32-[C3/C6/H2]

Add support for the PWM peripheral to ESP32-C3/C6/H2 by using the Espressif HAL
This commit is contained in:
Lucas Saavedra Vaz 2023-07-21 10:05:57 -03:00 committed by Xiang Xiao
parent c31681df54
commit 8dea103ecb
9 changed files with 401 additions and 0 deletions

View file

@ -0,0 +1,74 @@
/****************************************************************************
* boards/risc-v/espressif/common/include/esp_board_ledc.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_RISCV_ESPRESSIF_COMMON_INCLUDE_ESP_BOARD_LEDC_H
#define __BOARDS_RISCV_ESPRESSIF_COMMON_INCLUDE_ESP_BOARD_LEDC_H
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
#ifndef __ASSEMBLY__
#undef EXTERN
#if defined(__cplusplus)
#define EXTERN extern "C"
extern "C"
{
#else
#define EXTERN extern
#endif
/****************************************************************************
* Public Function Prototypes
****************************************************************************/
/****************************************************************************
* Name: board_ledc_setup
*
* Description:
* Initialize LEDC PWM and register the PWM device.
*
* Input Parameters:
* None.
*
* Returned Value:
* Zero (OK) is returned on success; A negated errno value is returned
* to indicate the nature of any failure.
*
****************************************************************************/
#ifdef CONFIG_ESPRESSIF_LEDC
int board_ledc_setup(void);
#endif
#undef EXTERN
#if defined(__cplusplus)
}
#endif
#endif /* __ASSEMBLY__ */
#endif /* __BOARDS_RISCV_ESPRESSIF_COMMON_INCLUDE_ESP_BOARD_LEDC_H */

View file

@ -20,6 +20,10 @@
ifeq ($(CONFIG_ARCH_BOARD_COMMON),y)
ifeq ($(CONFIG_ESPRESSIF_LEDC),y)
CSRCS += esp_board_ledc.c
endif
DEPPATH += --dep-path src
VPATH += :src
CFLAGS += ${INCDIR_PREFIX}$(TOPDIR)$(DELIM)arch$(DELIM)$(CONFIG_ARCH)$(DELIM)src$(DELIM)board$(DELIM)src

View file

@ -0,0 +1,147 @@
/****************************************************************************
* boards/risc-v/espressif/common/src/esp_board_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 <nuttx/config.h>
#include <sys/types.h>
#include <errno.h>
#include <debug.h>
#include <nuttx/board.h>
#include <nuttx/timers/pwm.h>
#include <arch/board/board.h>
#include "esp_ledc.h"
#include "esp_board_ledc.h"
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
#define LEDC_TIMER0 0
#define LEDC_TIMER1 1
#define LEDC_TIMER2 2
#define LEDC_TIMER3 3
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: board_ledc_setup
*
* Description:
* Initialize LEDC PWM and register the PWM device.
*
* Input Parameters:
* None.
*
* Returned Value:
* Zero (OK) is returned on success; A negated errno value is returned
* to indicate the nature of any failure.
*
****************************************************************************/
int board_ledc_setup(void)
{
int ret = OK;
struct pwm_lowerhalf_s *pwm;
#ifdef CONFIG_ESPRESSIF_LEDC_TIMER0
pwm = esp_ledc_init(LEDC_TIMER0);
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_ESPRESSIF_LEDC_TIMER1
pwm = esp_ledc_init(LEDC_TIMER1);
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_ESPRESSIF_LEDC_TIMER2
pwm = esp_ledc_init(LEDC_TIMER2);
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_ESPRESSIF_LEDC_TIMER3
pwm = esp_ledc_init(LEDC_TIMER3);
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
return ret;
}

View file

@ -0,0 +1,48 @@
#
# 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_NSH_ARGCAT is not set
# CONFIG_NSH_CMDOPT_HEXDUMP is not set
CONFIG_ARCH="risc-v"
CONFIG_ARCH_BOARD="esp32c3-generic"
CONFIG_ARCH_BOARD_COMMON=y
CONFIG_ARCH_BOARD_ESP32C3_GENERIC=y
CONFIG_ARCH_CHIP="espressif"
CONFIG_ARCH_CHIP_ESPRESSIF=y
CONFIG_ARCH_INTERRUPTSTACK=1536
CONFIG_ARCH_RISCV=y
CONFIG_ARCH_STACKDUMP=y
CONFIG_BOARDCTL_RESET=y
CONFIG_BOARD_LOOPSPERMSEC=15000
CONFIG_BUILTIN=y
CONFIG_DEV_ZERO=y
CONFIG_ESPRESSIF_LEDC=y
CONFIG_ESPRESSIF_LEDC_TIMER0=y
CONFIG_EXAMPLES_PWM=y
CONFIG_FS_PROCFS=y
CONFIG_IDLETHREAD_STACKSIZE=2048
CONFIG_INIT_ENTRYPOINT="nsh_main"
CONFIG_INTELHEX_BINARY=y
CONFIG_LIBC_FLOATINGPOINT=y
CONFIG_LIBC_PERROR_STDOUT=y
CONFIG_LIBC_STRERROR=y
CONFIG_NFILE_DESCRIPTORS_PER_BLOCK=6
CONFIG_NSH_ARCHINIT=y
CONFIG_NSH_BUILTIN_APPS=y
CONFIG_NSH_FILEIOSIZE=512
CONFIG_NSH_READLINE=y
CONFIG_NSH_STRERROR=y
CONFIG_PREALLOC_TIMERS=0
CONFIG_RR_INTERVAL=200
CONFIG_SCHED_BACKTRACE=y
CONFIG_SCHED_WAITPID=y
CONFIG_START_DAY=29
CONFIG_START_MONTH=11
CONFIG_START_YEAR=2019
CONFIG_SYSTEM_DUMPSTACK=y
CONFIG_SYSTEM_NSH=y
CONFIG_UART0_SERIAL_CONSOLE=y

View file

@ -34,6 +34,8 @@
#include <nuttx/fs/fs.h>
#include "esp_board_ledc.h"
#ifdef CONFIG_WATCHDOG
# include "esp_wdt.h"
#endif
@ -173,6 +175,14 @@ int esp_bringup(void)
}
#endif
#ifdef CONFIG_ESPRESSIF_LEDC
ret = board_ledc_setup();
if (ret < 0)
{
syslog(LOG_ERR, "ERROR: board_ledc_setup() failed: %d\n", ret);
}
#endif /* CONFIG_ESPRESSIF_LEDC */
/* If we got here then perhaps not all initialization was successful, but
* at least enough succeeded to bring-up NSH with perhaps reduced
* capabilities.

View file

@ -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_NSH_ARGCAT is not set
# CONFIG_NSH_CMDOPT_HEXDUMP is not set
CONFIG_ARCH="risc-v"
CONFIG_ARCH_BOARD="esp32c6-generic"
CONFIG_ARCH_BOARD_COMMON=y
CONFIG_ARCH_BOARD_ESP32C6_GENERIC=y
CONFIG_ARCH_CHIP="espressif"
CONFIG_ARCH_CHIP_ESPRESSIF=y
CONFIG_ARCH_INTERRUPTSTACK=2048
CONFIG_ARCH_RISCV=y
CONFIG_ARCH_STACKDUMP=y
CONFIG_BOARDCTL_RESET=y
CONFIG_BOARD_LOOPSPERMSEC=15000
CONFIG_BUILTIN=y
CONFIG_DEV_ZERO=y
CONFIG_ESPRESSIF_ESP32C6=y
CONFIG_ESPRESSIF_LEDC=y
CONFIG_ESPRESSIF_LEDC_TIMER0=y
CONFIG_EXAMPLES_PWM=y
CONFIG_FS_PROCFS=y
CONFIG_IDLETHREAD_STACKSIZE=2048
CONFIG_INIT_ENTRYPOINT="nsh_main"
CONFIG_INTELHEX_BINARY=y
CONFIG_LIBC_FLOATINGPOINT=y
CONFIG_LIBC_PERROR_STDOUT=y
CONFIG_LIBC_STRERROR=y
CONFIG_NFILE_DESCRIPTORS_PER_BLOCK=6
CONFIG_NSH_ARCHINIT=y
CONFIG_NSH_BUILTIN_APPS=y
CONFIG_NSH_FILEIOSIZE=512
CONFIG_NSH_READLINE=y
CONFIG_NSH_STRERROR=y
CONFIG_PREALLOC_TIMERS=0
CONFIG_RR_INTERVAL=200
CONFIG_SCHED_BACKTRACE=y
CONFIG_SCHED_WAITPID=y
CONFIG_START_DAY=29
CONFIG_START_MONTH=11
CONFIG_START_YEAR=2019
CONFIG_SYSTEM_DUMPSTACK=y
CONFIG_SYSTEM_NSH=y
CONFIG_UART0_SERIAL_CONSOLE=y

View file

@ -34,6 +34,8 @@
#include <nuttx/fs/fs.h>
#include "esp_board_ledc.h"
#ifdef CONFIG_WATCHDOG
# include "esp_wdt.h"
#endif
@ -173,6 +175,14 @@ int esp_bringup(void)
}
#endif
#ifdef CONFIG_ESPRESSIF_LEDC
ret = board_ledc_setup();
if (ret < 0)
{
syslog(LOG_ERR, "ERROR: board_ledc_setup() failed: %d\n", ret);
}
#endif /* CONFIG_ESPRESSIF_LEDC */
/* If we got here then perhaps not all initialization was successful, but
* at least enough succeeded to bring-up NSH with perhaps reduced
* capabilities.

View file

@ -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_NSH_ARGCAT is not set
# CONFIG_NSH_CMDOPT_HEXDUMP is not set
CONFIG_ARCH="risc-v"
CONFIG_ARCH_BOARD="esp32h2-generic"
CONFIG_ARCH_BOARD_COMMON=y
CONFIG_ARCH_BOARD_ESP32H2_GENERIC=y
CONFIG_ARCH_CHIP="espressif"
CONFIG_ARCH_CHIP_ESPRESSIF=y
CONFIG_ARCH_INTERRUPTSTACK=2048
CONFIG_ARCH_RISCV=y
CONFIG_ARCH_STACKDUMP=y
CONFIG_BOARDCTL_RESET=y
CONFIG_BOARD_LOOPSPERMSEC=15000
CONFIG_BUILTIN=y
CONFIG_DEV_ZERO=y
CONFIG_ESPRESSIF_ESP32H2=y
CONFIG_ESPRESSIF_LEDC=y
CONFIG_ESPRESSIF_LEDC_TIMER0=y
CONFIG_EXAMPLES_PWM=y
CONFIG_FS_PROCFS=y
CONFIG_IDLETHREAD_STACKSIZE=2048
CONFIG_INIT_ENTRYPOINT="nsh_main"
CONFIG_INTELHEX_BINARY=y
CONFIG_LIBC_FLOATINGPOINT=y
CONFIG_LIBC_PERROR_STDOUT=y
CONFIG_LIBC_STRERROR=y
CONFIG_NFILE_DESCRIPTORS_PER_BLOCK=6
CONFIG_NSH_ARCHINIT=y
CONFIG_NSH_BUILTIN_APPS=y
CONFIG_NSH_FILEIOSIZE=512
CONFIG_NSH_READLINE=y
CONFIG_NSH_STRERROR=y
CONFIG_PREALLOC_TIMERS=0
CONFIG_RR_INTERVAL=200
CONFIG_SCHED_BACKTRACE=y
CONFIG_SCHED_WAITPID=y
CONFIG_START_DAY=29
CONFIG_START_MONTH=11
CONFIG_START_YEAR=2019
CONFIG_SYSTEM_DUMPSTACK=y
CONFIG_SYSTEM_NSH=y
CONFIG_UART0_SERIAL_CONSOLE=y

View file

@ -34,6 +34,8 @@
#include <nuttx/fs/fs.h>
#include "esp_board_ledc.h"
#ifdef CONFIG_WATCHDOG
# include "esp_wdt.h"
#endif
@ -173,6 +175,14 @@ int esp_bringup(void)
}
#endif
#ifdef CONFIG_ESPRESSIF_LEDC
ret = board_ledc_setup();
if (ret < 0)
{
syslog(LOG_ERR, "ERROR: board_ledc_setup() failed: %d\n", ret);
}
#endif /* CONFIG_ESPRESSIF_LEDC */
/* If we got here then perhaps not all initialization was successful, but
* at least enough succeeded to bring-up NSH with perhaps reduced
* capabilities.