diff --git a/boards/xtensa/esp32/common/include/esp32_board_adc.h b/boards/xtensa/esp32/common/include/esp32_board_adc.h new file mode 100644 index 00000000000..aa8df1204da --- /dev/null +++ b/boards/xtensa/esp32/common/include/esp32_board_adc.h @@ -0,0 +1,79 @@ +/**************************************************************************** + * boards/xtensa/esp32/common/include/esp32_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_XTENSA_ESP32_COMMON_INCLUDE_ESP32_BOARD_ADC_H +#define __BOARDS_XTENSA_ESP32_COMMON_INCLUDE_ESP32_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: + * Initialize and configuree the ADC driver for the board. + * It registers the ADC channels specified in the configuration and ensures + * that the ADC hardware is properly set up for use. + * + * Input Parameters: + * None. + * + * Returned Value: + * Returns zero (OK) on successful initialization and registration of the + * ADC channels; 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_XTENSA_ESP32_COMMON_INCLUDE_ESP32_BOARD_ADC_H */ diff --git a/boards/xtensa/esp32/common/src/Make.defs b/boards/xtensa/esp32/common/src/Make.defs index 98d871f1429..3d91a0e8cb7 100644 --- a/boards/xtensa/esp32/common/src/Make.defs +++ b/boards/xtensa/esp32/common/src/Make.defs @@ -26,6 +26,10 @@ ifeq ($(CONFIG_WATCHDOG),y) CSRCS += esp32_board_wdt.c endif +ifeq ($(CONFIG_ESPRESSIF_ADC),y) + CSRCS += esp32_board_adc.c +endif + ifeq ($(CONFIG_ONESHOT),y) CSRCS += esp32_oneshot.c endif diff --git a/boards/xtensa/esp32/common/src/esp32_board_adc.c b/boards/xtensa/esp32/common/src/esp32_board_adc.c new file mode 100644 index 00000000000..a570bd27ea2 --- /dev/null +++ b/boards/xtensa/esp32/common/src/esp32_board_adc.c @@ -0,0 +1,248 @@ +/**************************************************************************** + * boards/xtensa/esp32/common/src/esp32_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 "esp32_board_adc.h" + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +/* The number of channels for each ADC */ + +#define ADC_1_MAX_CHANNELS 8 +#define ADC_2_MAX_CHANNELS 10 + +/**************************************************************************** + * 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 7 + * GPIO: 36 37 38 39 32 33 34 35 + * + * ADC 2 + * Channel: 0 1 2 3 4 5 6 7 8 9 + * GPIO: 4 0 2 15 13 12 14 27 25 26 + * + * On the chanlist arrays below, channels are added +1. Do not change. + * Important: if using more than 8 channels, edit CONFIG_ADC_FIFOSIZE. + */ + +#ifdef CONFIG_ESPRESSIF_ADC_1 +static const uint8_t g_chanlist_adc1[ADC_1_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 +#ifdef CONFIG_ESPRESSIF_ADC_1_CH7 + 8, +#endif +}; +#endif + +#ifdef CONFIG_ESPRESSIF_ADC_2 +static const uint8_t g_chanlist_adc2[ADC_2_MAX_CHANNELS] = +{ +#ifdef CONFIG_ESPRESSIF_ADC_2_CH0 + 1, +#endif +#ifdef CONFIG_ESPRESSIF_ADC_2_CH1 + 2, +#endif +#ifdef CONFIG_ESPRESSIF_ADC_2_CH2 + 3, +#endif +#ifdef CONFIG_ESPRESSIF_ADC_2_CH3 + 4, +#endif +#ifdef CONFIG_ESPRESSIF_ADC_2_CH4 + 5, +#endif +#ifdef CONFIG_ESPRESSIF_ADC_2_CH5 + 6, +#endif +#ifdef CONFIG_ESPRESSIF_ADC_2_CH6 + 7, +#endif +#ifdef CONFIG_ESPRESSIF_ADC_2_CH7 + 8, +#endif +#ifdef CONFIG_ESPRESSIF_ADC_2_CH8 + 9, +#endif +#ifdef CONFIG_ESPRESSIF_ADC_2_CH9 + 10 +#endif +}; +#endif + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: board_adc_register + * + * Description: + * This function registers the ADC driver for the specified ADC channel. + * It initializes the ADC hardware, creates the device name, and registers + * the ADC device with the system. + * + * Input Parameters: + * adc_num - The ADC channel number to register. + * + * Returned Value: + * Returns zero (OK) on successful registration; a negated errno value is + * returned to indicate the nature of any failure. + * + ****************************************************************************/ + +static int board_adc_register(int adc_num) +{ + int ret; + char devname[12]; + 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)); + + switch (adc_num) + { + case 1: +#ifdef CONFIG_ESPRESSIF_ADC_1 + adcdev = esp_adc_initialize(adc_num, g_chanlist_adc1); + break; +#endif + case 2: +#ifdef CONFIG_ESPRESSIF_ADC_2 + adcdev = esp_adc_initialize(adc_num, g_chanlist_adc2); + break; +#endif + default: + syslog(LOG_ERR, "ERROR: Unsupported ADC number: %d\n", adc_num); + return ERROR; + } + + if (adcdev == NULL) + { + syslog(LOG_ERR, "ERROR: Failed to initialize ADC %d\n", adc_num); + return -ENODEV; + } + + /* Register the ADC driver at "/dev/adcx" */ + + snprintf(devname, sizeof(devname), "/dev/adc%d", adc_num - 1); + ret = adc_register(devname, adcdev); + if (ret < 0) + { + kmm_free(adcdev); + syslog(LOG_ERR, "ERROR: adc_register %s failed: %d\n", devname, ret); + return ret; + } + + return ret; +} + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: board_adc_init + * + * Description: + * Initialize and configuree the ADC driver for the board. + * It registers the ADC channels specified in the configuration and ensures + * that the ADC hardware is properly set up for use. + * + * Input Parameters: + * None. + * + * Returned Value: + * Returns zero (OK) on successful initialization and registration of the + * ADC channels; a negated errno value is returned to indicate the nature + * of any failure. + * + ****************************************************************************/ + +int board_adc_init(void) +{ + int ret; + +#ifdef CONFIG_ESPRESSIF_ADC_1 + ret = board_adc_register(1); + if (ret != OK) + { + return ret; + } +#endif + +#ifdef CONFIG_ESPRESSIF_ADC_2 + ret = board_adc_register(2); + if (ret != OK) + { + return ret; + } +#endif + + return ret; +} diff --git a/boards/xtensa/esp32/esp32-devkitc/configs/adc/defconfig b/boards/xtensa/esp32/esp32-devkitc/configs/adc/defconfig new file mode 100644 index 00000000000..6eeaec0ab3d --- /dev/null +++ b/boards/xtensa/esp32/esp32-devkitc/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_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_ESPRESSIF_ADC=y +CONFIG_EXAMPLES_ADC=y +CONFIG_EXAMPLES_ADC_SWTRIG=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_LINE_MAX=64 +CONFIG_MM_REGIONS=3 +CONFIG_NSH_ARCHINIT=y +CONFIG_NSH_BUILTIN_APPS=y +CONFIG_NSH_FILEIOSIZE=512 +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_SYSLOG_BUFFER=y +CONFIG_SYSTEM_NSH=y +CONFIG_UART0_SERIAL_CONSOLE=y diff --git a/boards/xtensa/esp32/esp32-devkitc/src/esp32_bringup.c b/boards/xtensa/esp32/esp32-devkitc/src/esp32_bringup.c index b42fdd759dc..bc6b7741644 100644 --- a/boards/xtensa/esp32/esp32-devkitc/src/esp32_bringup.c +++ b/boards/xtensa/esp32/esp32-devkitc/src/esp32_bringup.c @@ -187,6 +187,10 @@ # include "espressif/esp_espnow_pktradio.h" #endif +#ifdef CONFIG_ESPRESSIF_ADC +# include "esp32_board_adc.h" +#endif + #include "esp32-devkitc.h" /**************************************************************************** @@ -777,6 +781,14 @@ int esp32_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. diff --git a/boards/xtensa/esp32s2/common/include/esp32s2_board_adc.h b/boards/xtensa/esp32s2/common/include/esp32s2_board_adc.h new file mode 100644 index 00000000000..988b21e49e8 --- /dev/null +++ b/boards/xtensa/esp32s2/common/include/esp32s2_board_adc.h @@ -0,0 +1,79 @@ +/**************************************************************************** + * boards/xtensa/esp32s2/common/include/esp32s2_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_XTENSA_ESP32S2_COMMON_INCLUDE_ESP32S2_BOARD_ADC_H +#define __BOARDS_XTENSA_ESP32S2_COMMON_INCLUDE_ESP32S2_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: + * Initialize and configuree the ADC driver for the board. + * It registers the ADC channels specified in the configuration and ensures + * that the ADC hardware is properly set up for use. + * + * Input Parameters: + * None. + * + * Returned Value: + * Returns zero (OK) on successful initialization and registration of the + * ADC channels; 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_XTENSA_ESP32S2_COMMON_INCLUDE_ESP32S2_BOARD_ADC_H */ diff --git a/boards/xtensa/esp32s2/common/src/Make.defs b/boards/xtensa/esp32s2/common/src/Make.defs index a9811113594..ad51dfd7a27 100644 --- a/boards/xtensa/esp32s2/common/src/Make.defs +++ b/boards/xtensa/esp32s2/common/src/Make.defs @@ -22,6 +22,10 @@ ifeq ($(CONFIG_ARCH_BOARD_COMMON),y) +ifeq ($(CONFIG_ESPRESSIF_ADC),y) + CSRCS += esp32s2_board_adc.c +endif + ifeq ($(CONFIG_WATCHDOG),y) CSRCS += esp32s2_board_wdt.c endif diff --git a/boards/xtensa/esp32s2/common/src/esp32s2_board_adc.c b/boards/xtensa/esp32s2/common/src/esp32s2_board_adc.c new file mode 100644 index 00000000000..bc7255a1f9b --- /dev/null +++ b/boards/xtensa/esp32s2/common/src/esp32s2_board_adc.c @@ -0,0 +1,253 @@ +/**************************************************************************** + * boards/xtensa/esp32s2/common/src/esp32s2_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 "esp32s2_board_adc.h" + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +/* The number of channels for each ADC */ + +#define ADC_MAX_CHANNELS 10 + +/**************************************************************************** + * 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 7 8 9 + * GPIO: 1 2 3 4 5 6 7 8 9 10 + * + * ADC 2 + * Channel: 0 1 2 3 4 5 6 7 8 9 + * GPIO: 11 12 13 14 15 16 17 18 19 20 + + * On the chanlist arrays below, channels are added +1. Do not change. + * Important: if using more than 8 channels, edit CONFIG_ADC_FIFOSIZE. + */ + +#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 +#ifdef CONFIG_ESPRESSIF_ADC_1_CH7 + 8, +#endif +#ifdef CONFIG_ESPRESSIF_ADC_1_CH8 + 9, +#endif +#ifdef CONFIG_ESPRESSIF_ADC_1_CH9 + 10 +#endif +}; +#endif + +#ifdef CONFIG_ESPRESSIF_ADC_2 +static const uint8_t g_chanlist_adc2[ADC_MAX_CHANNELS] = +{ +#ifdef CONFIG_ESPRESSIF_ADC_2_CH0 + 1, +#endif +#ifdef CONFIG_ESPRESSIF_ADC_2_CH1 + 2, +#endif +#ifdef CONFIG_ESPRESSIF_ADC_2_CH2 + 3, +#endif +#ifdef CONFIG_ESPRESSIF_ADC_2_CH3 + 4, +#endif +#ifdef CONFIG_ESPRESSIF_ADC_2_CH4 + 5, +#endif +#ifdef CONFIG_ESPRESSIF_ADC_2_CH5 + 6, +#endif +#ifdef CONFIG_ESPRESSIF_ADC_2_CH6 + 7, +#endif +#ifdef CONFIG_ESPRESSIF_ADC_2_CH7 + 8, +#endif +#ifdef CONFIG_ESPRESSIF_ADC_2_CH8 + 9, +#endif +#ifdef CONFIG_ESPRESSIF_ADC_2_CH9 + 10 +#endif +}; +#endif + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: board_adc_register + * + * Description: + * This function registers the ADC driver for the specified ADC channel. + * It initializes the ADC hardware, creates the device name, and registers + * the ADC device with the system. + * + * Input Parameters: + * adc_num - The ADC channel number to register. + * + * Returned Value: + * Returns zero (OK) on successful registration; a negated errno value is + * returned to indicate the nature of any failure. + * + ****************************************************************************/ + +static int board_adc_register(int adc_num) +{ + int ret; + char devname[12]; + 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)); + + switch (adc_num) + { + case 1: +#ifdef CONFIG_ESPRESSIF_ADC_1 + adcdev = esp_adc_initialize(adc_num, g_chanlist_adc1); + break; +#endif + case 2: +#ifdef CONFIG_ESPRESSIF_ADC_2 + adcdev = esp_adc_initialize(adc_num, g_chanlist_adc2); + break; +#endif + default: + syslog(LOG_ERR, "ERROR: Unsupported ADC number: %d\n", adc_num); + return ERROR; + } + + if (adcdev == NULL) + { + syslog(LOG_ERR, "ERROR: Failed to initialize ADC %d\n", adc_num); + return -ENODEV; + } + + /* Register the ADC driver at "/dev/adcx" */ + + snprintf(devname, sizeof(devname), "/dev/adc%d", adc_num - 1); + ret = adc_register(devname, adcdev); + if (ret < 0) + { + kmm_free(adcdev); + syslog(LOG_ERR, "ERROR: adc_register %s failed: %d\n", devname, ret); + return ret; + } + + return ret; +} + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: board_adc_init + * + * Description: + * Initialize and configuree the ADC driver for the board. + * It registers the ADC channels specified in the configuration and ensures + * that the ADC hardware is properly set up for use. + * + * Input Parameters: + * None. + * + * Returned Value: + * Returns zero (OK) on successful initialization and registration of the + * ADC channels; a negated errno value is returned to indicate the nature + * of any failure. + * + ****************************************************************************/ + +int board_adc_init(void) +{ + int ret; + +#ifdef CONFIG_ESPRESSIF_ADC_1 + ret = board_adc_register(1); + if (ret != OK) + { + return ret; + } +#endif + +#ifdef CONFIG_ESPRESSIF_ADC_2 + ret = board_adc_register(2); + if (ret != OK) + { + return ret; + } +#endif + + return ret; +} diff --git a/boards/xtensa/esp32s2/esp32s2-saola-1/configs/adc/defconfig b/boards/xtensa/esp32s2/esp32s2-saola-1/configs/adc/defconfig new file mode 100644 index 00000000000..9013015ee2d --- /dev/null +++ b/boards/xtensa/esp32s2/esp32s2-saola-1/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_ARCH_LEDS 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_ESPRESSIF_ADC=y +CONFIG_EXAMPLES_ADC=y +CONFIG_EXAMPLES_ADC_SWTRIG=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_LINE_MAX=64 +CONFIG_NSH_ARCHINIT=y +CONFIG_NSH_BUILTIN_APPS=y +CONFIG_NSH_FILEIOSIZE=512 +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_SYSLOG_BUFFER=y +CONFIG_SYSTEM_NSH=y +CONFIG_UART0_SERIAL_CONSOLE=y 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 70ea3dd7d38..d1fa8eb93a3 100644 --- a/boards/xtensa/esp32s2/esp32s2-saola-1/src/esp32s2_bringup.c +++ b/boards/xtensa/esp32s2/esp32s2-saola-1/src/esp32s2_bringup.c @@ -109,6 +109,10 @@ # include "espressif/esp_nxdiag.h" #endif +#ifdef CONFIG_ESPRESSIF_ADC +# include "esp32s2_board_adc.h" +#endif + #include "esp32s2-saola-1.h" /**************************************************************************** @@ -447,6 +451,14 @@ int esp32s2_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. diff --git a/boards/xtensa/esp32s3/common/include/esp32s3_board_adc.h b/boards/xtensa/esp32s3/common/include/esp32s3_board_adc.h index 7bb2906c464..1db3f63fc7e 100644 --- a/boards/xtensa/esp32s3/common/include/esp32s3_board_adc.h +++ b/boards/xtensa/esp32s3/common/include/esp32s3_board_adc.h @@ -52,18 +52,21 @@ extern "C" * Name: board_adc_init * * Description: - * Configure the ADC driver. + * Initialize and configuree the ADC driver for the board. + * It registers the ADC channels specified in the configuration and ensures + * that the ADC hardware is properly set up for use. * * Input Parameters: * None. * * Returned Value: - * Zero (OK) is returned on success; A negated errno value is returned - * to indicate the nature of any failure. + * Returns zero (OK) on successful initialization and registration of the + * ADC channels; a negated errno value is returned to indicate the nature + * of any failure. * ****************************************************************************/ -#ifdef CONFIG_ESP32S3_ADC +#ifdef CONFIG_ESPRESSIF_ADC int board_adc_init(void); #endif diff --git a/boards/xtensa/esp32s3/common/src/Make.defs b/boards/xtensa/esp32s3/common/src/Make.defs index 81248118dc4..51864aa215a 100644 --- a/boards/xtensa/esp32s3/common/src/Make.defs +++ b/boards/xtensa/esp32s3/common/src/Make.defs @@ -66,7 +66,7 @@ ifeq ($(CONFIG_ESP32S3_OTG),y) CSRCS += esp32s3_board_usb.c endif -ifeq ($(CONFIG_ESP32S3_ADC),y) +ifeq ($(CONFIG_ESPRESSIF_ADC),y) CSRCS += esp32s3_board_adc.c endif diff --git a/boards/xtensa/esp32s3/common/src/esp32s3_board_adc.c b/boards/xtensa/esp32s3/common/src/esp32s3_board_adc.c index 3c43c4670cd..97c3b6d49c2 100644 --- a/boards/xtensa/esp32s3/common/src/esp32s3_board_adc.c +++ b/boards/xtensa/esp32s3/common/src/esp32s3_board_adc.c @@ -27,10 +27,10 @@ #include #include #include -#include -#include -#include "esp32s3_adc.h" +#include + +#include "espressif/esp_adc.h" #include "esp32s3_board_adc.h" @@ -38,6 +38,102 @@ * Pre-processor Definitions ****************************************************************************/ +/* The number of channels for each ADC */ + +#define ADC_MAX_CHANNELS 10 + +/**************************************************************************** + * 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 7 8 9 + * GPIO: 1 2 3 4 5 6 7 8 9 10 + * + * ADC 2 + * Channel: 0 1 2 3 4 5 6 7 8 9 + * GPIO: 11 12 13 14 15 16 17 18 19 20 + + * On the chanlist arrays below, channels are added +1. Do not change. + * Important: if using more than 8 channels, edit CONFIG_ADC_FIFOSIZE. + */ + +#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 +#ifdef CONFIG_ESPRESSIF_ADC_1_CH7 + 8, +#endif +#ifdef CONFIG_ESPRESSIF_ADC_1_CH8 + 9, +#endif +#ifdef CONFIG_ESPRESSIF_ADC_1_CH9 + 10 +#endif +}; +#endif + +#ifdef CONFIG_ESPRESSIF_ADC_2 +static const uint8_t g_chanlist_adc2[ADC_MAX_CHANNELS] = +{ +#ifdef CONFIG_ESPRESSIF_ADC_2_CH0 + 1, +#endif +#ifdef CONFIG_ESPRESSIF_ADC_2_CH1 + 2, +#endif +#ifdef CONFIG_ESPRESSIF_ADC_2_CH2 + 3, +#endif +#ifdef CONFIG_ESPRESSIF_ADC_2_CH3 + 4, +#endif +#ifdef CONFIG_ESPRESSIF_ADC_2_CH4 + 5, +#endif +#ifdef CONFIG_ESPRESSIF_ADC_2_CH5 + 6, +#endif +#ifdef CONFIG_ESPRESSIF_ADC_2_CH6 + 7, +#endif +#ifdef CONFIG_ESPRESSIF_ADC_2_CH7 + 8, +#endif +#ifdef CONFIG_ESPRESSIF_ADC_2_CH8 + 9, +#endif +#ifdef CONFIG_ESPRESSIF_ADC_2_CH9 + 10 +#endif +}; +#endif + /**************************************************************************** * Private Functions ****************************************************************************/ @@ -46,18 +142,20 @@ * Name: board_adc_register * * Description: - * Register the ADC driver. + * This function registers the ADC driver for the specified ADC channel. + * It initializes the ADC hardware, creates the device name, and registers + * the ADC device with the system. * * Input Parameters: - * channel - ADC channel number. + * adc_num - The ADC channel number to register. * * Returned Value: - * Zero (OK) is returned on success; A negated errno value is returned - * to indicate the nature of any failure. + * Returns zero (OK) on successful registration; a negated errno value is + * returned to indicate the nature of any failure. * ****************************************************************************/ -static int board_adc_register(int channel) +static int board_adc_register(int adc_num) { int ret; char devname[12]; @@ -66,16 +164,38 @@ static int board_adc_register(int channel) adcdev = kmm_malloc(sizeof(struct adc_dev_s)); if (adcdev == NULL) { - aerr("ERROR: Failed to allocate adc_dev_s instance\n"); + syslog(LOG_ERR, "ERROR: Failed to allocate adc_dev_s instance\n"); return -ENOMEM; } memset(adcdev, 0, sizeof(struct adc_dev_s)); - esp32s3_adc_init(channel, adcdev); - snprintf(devname, sizeof(devname), "/dev/adc%d", channel); - /* Register the ADC driver at "/dev/adcx_x" */ + switch (adc_num) + { + case 1: +#ifdef CONFIG_ESPRESSIF_ADC_1 + adcdev = esp_adc_initialize(adc_num, g_chanlist_adc1); + break; +#endif + case 2: +#ifdef CONFIG_ESPRESSIF_ADC_2 + adcdev = esp_adc_initialize(adc_num, g_chanlist_adc2); + break; +#endif + default: + syslog(LOG_ERR, "ERROR: Unsupported ADC number: %d\n", adc_num); + return ERROR; + } + if (adcdev == NULL) + { + syslog(LOG_ERR, "ERROR: Failed to initialize ADC %d\n", adc_num); + return -ENODEV; + } + + /* Register the ADC driver at "/dev/adcx" */ + + snprintf(devname, sizeof(devname), "/dev/adc%d", adc_num - 1); ret = adc_register(devname, adcdev); if (ret < 0) { @@ -95,14 +215,17 @@ static int board_adc_register(int channel) * Name: board_adc_init * * Description: - * Configure the ADC driver. + * Initialize and configuree the ADC driver for the board. + * It registers the ADC channels specified in the configuration and ensures + * that the ADC hardware is properly set up for use. * * Input Parameters: * None. * * Returned Value: - * Zero (OK) is returned on success; A negated errno value is returned - * to indicate the nature of any failure. + * Returns zero (OK) on successful initialization and registration of the + * ADC channels; a negated errno value is returned to indicate the nature + * of any failure. * ****************************************************************************/ @@ -110,80 +233,16 @@ int board_adc_init(void) { int ret; -#ifdef CONFIG_ESP32S3_ADC1_CHANNEL0 - ret = board_adc_register(ESP32S3_ADC1_CHANNEL0); +#ifdef CONFIG_ESPRESSIF_ADC_1 + ret = board_adc_register(1); if (ret != OK) { return ret; } #endif -#ifdef CONFIG_ESP32S3_ADC1_CHANNEL1 - ret = board_adc_register(ESP32S3_ADC1_CHANNEL1); - if (ret != OK) - { - return ret; - } -#endif - -#ifdef CONFIG_ESP32S3_ADC1_CHANNEL2 - ret = board_adc_register(ESP32S3_ADC1_CHANNEL2); - if (ret != OK) - { - return ret; - } -#endif - -#ifdef CONFIG_ESP32S3_ADC1_CHANNEL3 - ret = board_adc_register(ESP32S3_ADC1_CHANNEL3); - if (ret != OK) - { - return ret; - } -#endif - -#ifdef CONFIG_ESP32S3_ADC1_CHANNEL4 - ret = board_adc_register(ESP32S3_ADC1_CHANNEL4); - if (ret != OK) - { - return ret; - } -#endif - -#ifdef CONFIG_ESP32S3_ADC1_CHANNEL5 - ret = board_adc_register(ESP32S3_ADC1_CHANNEL5); - if (ret != OK) - { - return ret; - } -#endif - -#ifdef CONFIG_ESP32S3_ADC1_CHANNEL6 - ret = board_adc_register(ESP32S3_ADC1_CHANNEL6); - if (ret != OK) - { - return ret; - } -#endif - -#ifdef CONFIG_ESP32S3_ADC1_CHANNEL7 - ret = board_adc_register(ESP32S3_ADC1_CHANNEL7); - if (ret != OK) - { - return ret; - } -#endif - -#ifdef CONFIG_ESP32S3_ADC1_CHANNEL8 - ret = board_adc_register(ESP32S3_ADC1_CHANNEL8); - if (ret != OK) - { - return ret; - } -#endif - -#ifdef CONFIG_ESP32S3_ADC1_CHANNEL9 - ret = board_adc_register(ESP32S3_ADC1_CHANNEL9); +#ifdef CONFIG_ESPRESSIF_ADC_2 + ret = board_adc_register(2); if (ret != OK) { return ret; diff --git a/boards/xtensa/esp32s3/esp32s3-devkit/configs/adc/defconfig b/boards/xtensa/esp32s3/esp32s3-devkit/configs/adc/defconfig index 78f8aa0affe..b56766f56c2 100644 --- a/boards/xtensa/esp32s3/esp32s3-devkit/configs/adc/defconfig +++ b/boards/xtensa/esp32s3/esp32s3-devkit/configs/adc/defconfig @@ -22,11 +22,8 @@ CONFIG_BOARD_LOOPSPERMSEC=16717 CONFIG_BUILTIN=y CONFIG_DEBUG_FULLOPT=y CONFIG_DEBUG_SYMBOLS=y -CONFIG_ESP32S3_ADC1_CHANNEL0=y -CONFIG_ESP32S3_ADC1_CHANNEL1=y -CONFIG_ESP32S3_ADC1_CHANNEL2=y -CONFIG_ESP32S3_ADC=y CONFIG_ESP32S3_UART0=y +CONFIG_ESPRESSIF_ADC=y CONFIG_EXAMPLES_ADC=y CONFIG_EXAMPLES_ADC_SWTRIG=y CONFIG_FS_PROCFS=y diff --git a/boards/xtensa/esp32s3/esp32s3-devkit/src/esp32s3_bringup.c b/boards/xtensa/esp32s3/esp32s3-devkit/src/esp32s3_bringup.c index c8a63fe627d..917ece9fe11 100644 --- a/boards/xtensa/esp32s3/esp32s3-devkit/src/esp32s3_bringup.c +++ b/boards/xtensa/esp32s3/esp32s3-devkit/src/esp32s3_bringup.c @@ -124,7 +124,7 @@ # include "esp32s3_aes.h" #endif -#ifdef CONFIG_ESP32S3_ADC +#ifdef CONFIG_ESPRESSIF_ADC #include "esp32s3_board_adc.h" #endif @@ -522,7 +522,7 @@ int esp32s3_bringup(void) #endif #endif -#ifdef CONFIG_ESP32S3_ADC +#ifdef CONFIG_ESPRESSIF_ADC /* Configure ADC */ ret = board_adc_init();