From 238a5250f63f0e9308dd8bfb4e6c2ecb4e344a31 Mon Sep 17 00:00:00 2001 From: Filipe Cavalcanti Date: Wed, 26 Mar 2025 08:14:32 -0300 Subject: [PATCH] boards/risc-v: ADC support on Espressif devices Adds adc defconfig and board support for: esp32c3-generic, esp32c6-devkitc and esp32h2-devkit. Signed-off-by: Filipe Cavalcanti --- .../esp32c3/common/include/esp_board_adc.h | 79 ++++++++++ boards/risc-v/esp32c3/common/src/Make.defs | 4 + .../risc-v/esp32c3/common/src/esp_board_adc.c | 138 +++++++++++++++++ .../esp32c3-generic/configs/adc/defconfig | 48 ++++++ .../esp32c3-generic/src/esp32c3_bringup.c | 12 ++ .../esp32c6/common/include/esp_board_adc.h | 79 ++++++++++ boards/risc-v/esp32c6/common/src/Make.defs | 4 + .../risc-v/esp32c6/common/src/esp_board_adc.c | 141 ++++++++++++++++++ .../esp32c6-devkitc/configs/adc/defconfig | 50 +++++++ .../esp32c6-devkitc/src/esp32c6_bringup.c | 14 ++ .../esp32h2/common/include/esp_board_adc.h | 79 ++++++++++ boards/risc-v/esp32h2/common/src/Make.defs | 4 + .../risc-v/esp32h2/common/src/esp_board_adc.c | 135 +++++++++++++++++ .../esp32h2-devkit/configs/adc/defconfig | 49 ++++++ .../esp32h2-devkit/src/esp32h2_bringup.c | 12 ++ 15 files changed, 848 insertions(+) create mode 100644 boards/risc-v/esp32c3/common/include/esp_board_adc.h create mode 100644 boards/risc-v/esp32c3/common/src/esp_board_adc.c create mode 100644 boards/risc-v/esp32c3/esp32c3-generic/configs/adc/defconfig create mode 100644 boards/risc-v/esp32c6/common/include/esp_board_adc.h create mode 100644 boards/risc-v/esp32c6/common/src/esp_board_adc.c create mode 100644 boards/risc-v/esp32c6/esp32c6-devkitc/configs/adc/defconfig create mode 100644 boards/risc-v/esp32h2/common/include/esp_board_adc.h create mode 100644 boards/risc-v/esp32h2/common/src/esp_board_adc.c create mode 100644 boards/risc-v/esp32h2/esp32h2-devkit/configs/adc/defconfig diff --git a/boards/risc-v/esp32c3/common/include/esp_board_adc.h b/boards/risc-v/esp32c3/common/include/esp_board_adc.h new file mode 100644 index 00000000000..03cd60662e8 --- /dev/null +++ b/boards/risc-v/esp32c3/common/include/esp_board_adc.h @@ -0,0 +1,79 @@ +/**************************************************************************** + * boards/risc-v/esp32c3/common/include/esp_board_adc.h + * + * SPDX-License-Identifier: Apache-2.0 + * + * 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_ESP32C3_COMMON_INCLUDE_ESP32C3_BOARD_ADC_H +#define __BOARDS_RISCV_ESP32C3_COMMON_INCLUDE_ESP32C3_BOARD_ADC_H + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include + +/**************************************************************************** + * 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_adc_init + * + * Description: + * This function configures and initializes the ADC driver for the board. + * It allocates memory for the ADC device structure, sets up the ADC + * hardware, and registers the ADC device with the system. + * + * Input Parameters: + * None. + * + * Returned Value: + * Returns zero (OK) on successful initialization and registration of the + * ADC device; a negated errno value is returned to indicate the nature + * of any failure. + * + ****************************************************************************/ + +#ifdef CONFIG_ESPRESSIF_ADC +int board_adc_init(void); +#endif + +#undef EXTERN +#if defined(__cplusplus) +} +#endif + +#endif /* __ASSEMBLY__ */ +#endif /* __BOARDS_RISCV_ESP32C3_COMMON_INCLUDE_ESP32C3_BOARD_ADC_H */ diff --git a/boards/risc-v/esp32c3/common/src/Make.defs b/boards/risc-v/esp32c3/common/src/Make.defs index a6805d033c5..96f03779782 100644 --- a/boards/risc-v/esp32c3/common/src/Make.defs +++ b/boards/risc-v/esp32c3/common/src/Make.defs @@ -22,6 +22,10 @@ ifeq ($(CONFIG_ARCH_BOARD_COMMON),y) +ifeq ($(CONFIG_ESPRESSIF_ADC),y) + CSRCS += esp_board_adc.c +endif + ifeq ($(CONFIG_ESPRESSIF_LEDC),y) CSRCS += esp_board_ledc.c endif diff --git a/boards/risc-v/esp32c3/common/src/esp_board_adc.c b/boards/risc-v/esp32c3/common/src/esp_board_adc.c new file mode 100644 index 00000000000..e16c5d291f2 --- /dev/null +++ b/boards/risc-v/esp32c3/common/src/esp_board_adc.c @@ -0,0 +1,138 @@ +/**************************************************************************** + * boards/risc-v/esp32c3/common/src/esp_board_adc.c + * + * SPDX-License-Identifier: Apache-2.0 + * + * 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 "espressif/esp_adc.h" + +#include "esp_board_adc.h" + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +/* The number of channels for each ADC */ + +#define ADC_MAX_CHANNELS 5 + +/**************************************************************************** + * Private Data + ****************************************************************************/ + +/* Select channels to be used for each ADC. + * + * GPIOs are fixed for each channel and configured in the lower-half driver. + * + * ADC 1 + * Channel: 0 1 2 3 4 5 + * GPIO: 0 1 2 3 4 5 + * + * On the chanlist arrays below, channels are added +1. Do not change. + */ + +#ifdef CONFIG_ESPRESSIF_ADC_1 +static const uint8_t g_chanlist_adc1[ADC_MAX_CHANNELS] = +{ +#ifdef CONFIG_ESPRESSIF_ADC_1_CH0 + 1, +#endif +#ifdef CONFIG_ESPRESSIF_ADC_1_CH1 + 2, +#endif +#ifdef CONFIG_ESPRESSIF_ADC_1_CH2 + 3, +#endif +#ifdef CONFIG_ESPRESSIF_ADC_1_CH3 + 4, +#endif +#ifdef CONFIG_ESPRESSIF_ADC_1_CH4 + 5, +#endif +#ifdef CONFIG_ESPRESSIF_ADC_1_CH5 + 6, +#endif +}; +#endif + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: board_adc_init + * + * Description: + * This function configures and initializes the ADC driver for the board. + * It allocates memory for the ADC device structure, sets up the ADC + * hardware, and registers the ADC device with the system. + * + * Input Parameters: + * None. + * + * Returned Value: + * Returns zero (OK) on successful initialization and registration of the + * ADC device; a negated errno value is returned to indicate the nature + * of any failure. + * + ****************************************************************************/ + +int board_adc_init(void) +{ + int ret; + struct adc_dev_s *adcdev; + + adcdev = kmm_malloc(sizeof(struct adc_dev_s)); + if (adcdev == NULL) + { + syslog(LOG_ERR, "ERROR: Failed to allocate adc_dev_s instance\n"); + return -ENOMEM; + } + + memset(adcdev, 0, sizeof(struct adc_dev_s)); + + adcdev = esp_adc_initialize(1, g_chanlist_adc1); + + if (adcdev == NULL) + { + syslog(LOG_ERR, "ERROR: Failed to initialize ADC1\n"); + return -ENODEV; + } + + /* Register the ADC driver at "/dev/adcx" */ + + ret = adc_register("/dev/adc0", adcdev); + if (ret < 0) + { + kmm_free(adcdev); + syslog(LOG_ERR, "ERROR: adc_register /dev/adc0 failed: %d\n", ret); + } + + return ret; +} diff --git a/boards/risc-v/esp32c3/esp32c3-generic/configs/adc/defconfig b/boards/risc-v/esp32c3/esp32c3-generic/configs/adc/defconfig new file mode 100644 index 00000000000..e05a815391c --- /dev/null +++ b/boards/risc-v/esp32c3/esp32c3-generic/configs/adc/defconfig @@ -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="esp32c3" +CONFIG_ARCH_CHIP_ESP32C3_GENERIC=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_ESPRESSIF_ADC=y +CONFIG_EXAMPLES_ADC=y +CONFIG_EXAMPLES_ADC_SWTRIG=y +CONFIG_FS_PROCFS=y +CONFIG_IDLETHREAD_STACKSIZE=2048 +CONFIG_INIT_ENTRYPOINT="nsh_main" +CONFIG_INTELHEX_BINARY=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_TESTING_GETPRIME=y +CONFIG_TESTING_OSTEST=y +CONFIG_UART0_SERIAL_CONSOLE=y diff --git a/boards/risc-v/esp32c3/esp32c3-generic/src/esp32c3_bringup.c b/boards/risc-v/esp32c3/esp32c3-generic/src/esp32c3_bringup.c index b25a9cff6d8..5c3bcc974ca 100644 --- a/boards/risc-v/esp32c3/esp32c3-generic/src/esp32c3_bringup.c +++ b/boards/risc-v/esp32c3/esp32c3-generic/src/esp32c3_bringup.c @@ -41,6 +41,10 @@ #include "esp_board_i2c.h" #include "esp_board_bmp180.h" +#ifdef CONFIG_ESPRESSIF_ADC +# include "esp_board_adc.h" +#endif + #ifdef CONFIG_WATCHDOG # include "espressif/esp_wdt.h" #endif @@ -399,6 +403,14 @@ int esp_bringup(void) } #endif +#ifdef CONFIG_ESPRESSIF_ADC + ret = board_adc_init(); + if (ret < 0) + { + syslog(LOG_ERR, "Failed to initialize ADC driver: %d\n", ret); + } +#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. diff --git a/boards/risc-v/esp32c6/common/include/esp_board_adc.h b/boards/risc-v/esp32c6/common/include/esp_board_adc.h new file mode 100644 index 00000000000..d208643a501 --- /dev/null +++ b/boards/risc-v/esp32c6/common/include/esp_board_adc.h @@ -0,0 +1,79 @@ +/**************************************************************************** + * boards/risc-v/esp32c6/common/include/esp_board_adc.h + * + * SPDX-License-Identifier: Apache-2.0 + * + * 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_ESP32C6_COMMON_INCLUDE_ESP32C6_BOARD_ADC_H +#define __BOARDS_RISCV_ESP32C6_COMMON_INCLUDE_ESP32C6_BOARD_ADC_H + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include + +/**************************************************************************** + * 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_adc_init + * + * Description: + * This function configures and initializes the ADC driver for the board. + * It allocates memory for the ADC device structure, sets up the ADC + * hardware, and registers the ADC device with the system. + * + * Input Parameters: + * None. + * + * Returned Value: + * Returns zero (OK) on successful initialization and registration of the + * ADC device; a negated errno value is returned to indicate the nature + * of any failure. + * + ****************************************************************************/ + +#ifdef CONFIG_ESPRESSIF_ADC +int board_adc_init(void); +#endif + +#undef EXTERN +#if defined(__cplusplus) +} +#endif + +#endif /* __ASSEMBLY__ */ +#endif /* __BOARDS_RISCV_ESP32C6_COMMON_INCLUDE_ESP32C6_BOARD_ADC_H */ diff --git a/boards/risc-v/esp32c6/common/src/Make.defs b/boards/risc-v/esp32c6/common/src/Make.defs index 18efe25006f..d3b3f4c4833 100644 --- a/boards/risc-v/esp32c6/common/src/Make.defs +++ b/boards/risc-v/esp32c6/common/src/Make.defs @@ -22,6 +22,10 @@ ifeq ($(CONFIG_ARCH_BOARD_COMMON),y) +ifeq ($(CONFIG_ESPRESSIF_ADC),y) + CSRCS += esp_board_adc.c +endif + ifeq ($(CONFIG_ESPRESSIF_LEDC),y) CSRCS += esp_board_ledc.c endif diff --git a/boards/risc-v/esp32c6/common/src/esp_board_adc.c b/boards/risc-v/esp32c6/common/src/esp_board_adc.c new file mode 100644 index 00000000000..726c2bf94b0 --- /dev/null +++ b/boards/risc-v/esp32c6/common/src/esp_board_adc.c @@ -0,0 +1,141 @@ +/**************************************************************************** + * boards/risc-v/esp32c6/common/src/esp_board_adc.c + * + * SPDX-License-Identifier: Apache-2.0 + * + * 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 "espressif/esp_adc.h" + +#include "esp_board_adc.h" + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +/* The number of channels for each ADC */ + +#define ADC_MAX_CHANNELS 7 + +/**************************************************************************** + * Private Data + ****************************************************************************/ + +/* Select channels to be used for each ADC. + * + * GPIOs are fixed for each channel and configured in the lower-half driver. + * + * ADC 1 + * Channel: 0 1 2 3 4 5 6 + * GPIO: 0 1 2 3 4 5 6 + * + * On the chanlist arrays below, channels are added +1. Do not change. + */ + +#ifdef CONFIG_ESPRESSIF_ADC_1 +static const uint8_t g_chanlist_adc1[ADC_MAX_CHANNELS] = +{ +#ifdef CONFIG_ESPRESSIF_ADC_1_CH0 + 1, +#endif +#ifdef CONFIG_ESPRESSIF_ADC_1_CH1 + 2, +#endif +#ifdef CONFIG_ESPRESSIF_ADC_1_CH2 + 3, +#endif +#ifdef CONFIG_ESPRESSIF_ADC_1_CH3 + 4, +#endif +#ifdef CONFIG_ESPRESSIF_ADC_1_CH4 + 5, +#endif +#ifdef CONFIG_ESPRESSIF_ADC_1_CH5 + 6, +#endif +#ifdef CONFIG_ESPRESSIF_ADC_1_CH6 + 7, +#endif +}; +#endif + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: board_adc_init + * + * Description: + * This function configures and initializes the ADC driver for the board. + * It allocates memory for the ADC device structure, sets up the ADC + * hardware, and registers the ADC device with the system. + * + * Input Parameters: + * None. + * + * Returned Value: + * Returns zero (OK) on successful initialization and registration of the + * ADC device; a negated errno value is returned to indicate the nature + * of any failure. + * + ****************************************************************************/ + +int board_adc_init(void) +{ + int ret; + struct adc_dev_s *adcdev; + + adcdev = kmm_malloc(sizeof(struct adc_dev_s)); + if (adcdev == NULL) + { + syslog(LOG_ERR, "ERROR: Failed to allocate adc_dev_s instance\n"); + return -ENOMEM; + } + + memset(adcdev, 0, sizeof(struct adc_dev_s)); + + adcdev = esp_adc_initialize(1, g_chanlist_adc1); + + if (adcdev == NULL) + { + syslog(LOG_ERR, "ERROR: Failed to initialize ADC1\n"); + return -ENODEV; + } + + /* Register the ADC driver at "/dev/adcx" */ + + ret = adc_register("/dev/adc0", adcdev); + if (ret < 0) + { + kmm_free(adcdev); + syslog(LOG_ERR, "ERROR: adc_register /dev/adc0 failed: %d\n", ret); + } + + return ret; +} diff --git a/boards/risc-v/esp32c6/esp32c6-devkitc/configs/adc/defconfig b/boards/risc-v/esp32c6/esp32c6-devkitc/configs/adc/defconfig new file mode 100644 index 00000000000..c769ec2d9d7 --- /dev/null +++ b/boards/risc-v/esp32c6/esp32c6-devkitc/configs/adc/defconfig @@ -0,0 +1,50 @@ +# +# 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-devkitc" +CONFIG_ARCH_BOARD_COMMON=y +CONFIG_ARCH_BOARD_ESP32C6_DEVKITC=y +CONFIG_ARCH_CHIP="esp32c6" +CONFIG_ARCH_CHIP_ESP32C6=y +CONFIG_ARCH_CHIP_ESP32C6WROOM1=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_ESPRESSIF_ADC=y +CONFIG_ESPRESSIF_ESP32C6=y +CONFIG_EXAMPLES_ADC=y +CONFIG_EXAMPLES_ADC_SWTRIG=y +CONFIG_FS_PROCFS=y +CONFIG_IDLETHREAD_STACKSIZE=2048 +CONFIG_INIT_ENTRYPOINT="nsh_main" +CONFIG_INTELHEX_BINARY=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_TESTING_GETPRIME=y +CONFIG_TESTING_OSTEST=y +CONFIG_UART0_SERIAL_CONSOLE=y diff --git a/boards/risc-v/esp32c6/esp32c6-devkitc/src/esp32c6_bringup.c b/boards/risc-v/esp32c6/esp32c6-devkitc/src/esp32c6_bringup.c index 9b1c43eb984..51bd4161b27 100644 --- a/boards/risc-v/esp32c6/esp32c6-devkitc/src/esp32c6_bringup.c +++ b/boards/risc-v/esp32c6/esp32c6-devkitc/src/esp32c6_bringup.c @@ -41,6 +41,10 @@ #include "esp_board_i2c.h" #include "esp_board_bmp180.h" +#ifdef CONFIG_ESPRESSIF_ADC +# include "esp_board_adc.h" +#endif + #ifdef CONFIG_WATCHDOG # include "espressif/esp_wdt.h" #endif @@ -422,6 +426,16 @@ int esp_bringup(void) } #endif +#ifdef CONFIG_ESPRESSIF_ADC + /* Initialize and register the pcnt/qencoder driver */ + + ret = board_adc_init(); + if (ret < 0) + { + syslog(LOG_ERR, "ERROR: board_adc_init failed: %d\n", ret); + } +#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. diff --git a/boards/risc-v/esp32h2/common/include/esp_board_adc.h b/boards/risc-v/esp32h2/common/include/esp_board_adc.h new file mode 100644 index 00000000000..868ff417eb7 --- /dev/null +++ b/boards/risc-v/esp32h2/common/include/esp_board_adc.h @@ -0,0 +1,79 @@ +/**************************************************************************** + * boards/risc-v/esp32h2/common/include/esp_board_adc.h + * + * SPDX-License-Identifier: Apache-2.0 + * + * 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_ESP32C6_COMMON_INCLUDE_ESP32C6_BOARD_ADC_H +#define __BOARDS_RISCV_ESP32C6_COMMON_INCLUDE_ESP32C6_BOARD_ADC_H + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include + +/**************************************************************************** + * 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_adc_init + * + * Description: + * This function configures and initializes the ADC driver for the board. + * It allocates memory for the ADC device structure, sets up the ADC + * hardware, and registers the ADC device with the system. + * + * Input Parameters: + * None. + * + * Returned Value: + * Returns zero (OK) on successful initialization and registration of the + * ADC device; a negated errno value is returned to indicate the nature + * of any failure. + * + ****************************************************************************/ + +#ifdef CONFIG_ESPRESSIF_ADC +int board_adc_init(void); +#endif + +#undef EXTERN +#if defined(__cplusplus) +} +#endif + +#endif /* __ASSEMBLY__ */ +#endif /* __BOARDS_RISCV_ESP32C6_COMMON_INCLUDE_ESP32C6_BOARD_ADC_H */ diff --git a/boards/risc-v/esp32h2/common/src/Make.defs b/boards/risc-v/esp32h2/common/src/Make.defs index be47fb1424f..c36fa1a91ed 100644 --- a/boards/risc-v/esp32h2/common/src/Make.defs +++ b/boards/risc-v/esp32h2/common/src/Make.defs @@ -22,6 +22,10 @@ ifeq ($(CONFIG_ARCH_BOARD_COMMON),y) +ifeq ($(CONFIG_ESPRESSIF_ADC),y) + CSRCS += esp_board_adc.c +endif + ifeq ($(CONFIG_ESPRESSIF_LEDC),y) CSRCS += esp_board_ledc.c endif diff --git a/boards/risc-v/esp32h2/common/src/esp_board_adc.c b/boards/risc-v/esp32h2/common/src/esp_board_adc.c new file mode 100644 index 00000000000..5401507d08d --- /dev/null +++ b/boards/risc-v/esp32h2/common/src/esp_board_adc.c @@ -0,0 +1,135 @@ +/**************************************************************************** + * boards/risc-v/esp32h2/common/src/esp_board_adc.c + * + * SPDX-License-Identifier: Apache-2.0 + * + * 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 "espressif/esp_adc.h" + +#include "esp_board_adc.h" + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +/* The number of channels for each ADC */ + +#define ADC_MAX_CHANNELS 7 + +/**************************************************************************** + * Private Data + ****************************************************************************/ + +/* Select channels to be used for each ADC. + * + * GPIOs are fixed for each channel and configured in the lower-half driver. + * + * ADC 1 + * Channel: 0 1 2 3 4 + * GPIO: 1 2 3 4 5 + * + * On the chanlist arrays below, channels are added +1. Do not change. + */ + +#ifdef CONFIG_ESPRESSIF_ADC_1 +static const uint8_t g_chanlist_adc1[ADC_MAX_CHANNELS] = +{ +#ifdef CONFIG_ESPRESSIF_ADC_1_CH0 + 1, +#endif +#ifdef CONFIG_ESPRESSIF_ADC_1_CH1 + 2, +#endif +#ifdef CONFIG_ESPRESSIF_ADC_1_CH2 + 3, +#endif +#ifdef CONFIG_ESPRESSIF_ADC_1_CH3 + 4, +#endif +#ifdef CONFIG_ESPRESSIF_ADC_1_CH4 + 5, +#endif +}; +#endif + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: board_adc_init + * + * Description: + * This function configures and initializes the ADC driver for the board. + * It allocates memory for the ADC device structure, sets up the ADC + * hardware, and registers the ADC device with the system. + * + * Input Parameters: + * None. + * + * Returned Value: + * Returns zero (OK) on successful initialization and registration of the + * ADC device; a negated errno value is returned to indicate the nature + * of any failure. + * + ****************************************************************************/ + +int board_adc_init(void) +{ + int ret; + struct adc_dev_s *adcdev; + + adcdev = kmm_malloc(sizeof(struct adc_dev_s)); + if (adcdev == NULL) + { + syslog(LOG_ERR, "ERROR: Failed to allocate adc_dev_s instance\n"); + return -ENOMEM; + } + + memset(adcdev, 0, sizeof(struct adc_dev_s)); + + adcdev = esp_adc_initialize(1, g_chanlist_adc1); + + if (adcdev == NULL) + { + syslog(LOG_ERR, "ERROR: Failed to initialize ADC1\n"); + return -ENODEV; + } + + /* Register the ADC driver at "/dev/adcx" */ + + ret = adc_register("/dev/adc0", adcdev); + if (ret < 0) + { + kmm_free(adcdev); + syslog(LOG_ERR, "ERROR: adc_register /dev/adc0 failed: %d\n", ret); + } + + return ret; +} diff --git a/boards/risc-v/esp32h2/esp32h2-devkit/configs/adc/defconfig b/boards/risc-v/esp32h2/esp32h2-devkit/configs/adc/defconfig new file mode 100644 index 00000000000..aa8fa6aeb2b --- /dev/null +++ b/boards/risc-v/esp32h2/esp32h2-devkit/configs/adc/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_NSH_ARGCAT is not set +# CONFIG_NSH_CMDOPT_HEXDUMP is not set +CONFIG_ARCH="risc-v" +CONFIG_ARCH_BOARD="esp32h2-devkit" +CONFIG_ARCH_BOARD_COMMON=y +CONFIG_ARCH_BOARD_ESP32H2_DEVKIT=y +CONFIG_ARCH_CHIP="esp32h2" +CONFIG_ARCH_CHIP_ESP32H2=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_ESPRESSIF_ADC=y +CONFIG_ESPRESSIF_ESP32H2=y +CONFIG_EXAMPLES_ADC=y +CONFIG_EXAMPLES_ADC_SWTRIG=y +CONFIG_FS_PROCFS=y +CONFIG_IDLETHREAD_STACKSIZE=2048 +CONFIG_INIT_ENTRYPOINT="nsh_main" +CONFIG_INTELHEX_BINARY=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_TESTING_GETPRIME=y +CONFIG_TESTING_OSTEST=y +CONFIG_UART0_SERIAL_CONSOLE=y diff --git a/boards/risc-v/esp32h2/esp32h2-devkit/src/esp32h2_bringup.c b/boards/risc-v/esp32h2/esp32h2-devkit/src/esp32h2_bringup.c index 6c3fb943c05..cf839fb8445 100644 --- a/boards/risc-v/esp32h2/esp32h2-devkit/src/esp32h2_bringup.c +++ b/boards/risc-v/esp32h2/esp32h2-devkit/src/esp32h2_bringup.c @@ -103,6 +103,10 @@ # include "esp_board_pcnt.h" #endif +#ifdef CONFIG_ESPRESSIF_ADC +# include "esp_board_adc.h" +#endif + #ifdef CONFIG_SYSTEM_NXDIAG_ESPRESSIF_CHIP_WO_TOOL # include "espressif/esp_nxdiag.h" #endif @@ -389,6 +393,14 @@ int esp_bringup(void) } #endif +#ifdef CONFIG_ESPRESSIF_ADC + ret = board_adc_init(); + if (ret < 0) + { + syslog(LOG_ERR, "ERROR: board_adc_init failed: %d\n", ret); + } +#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.