boards/xtensa: ADC support on Espressif devices

Adds adc defconfig and board support for: esp32-devkitc, esp32s2-saola-1 and esp32s3-devkit.

Signed-off-by: Filipe Cavalcanti <filipe.cavalcanti@espressif.com>
This commit is contained in:
Filipe Cavalcanti
2025-03-26 08:13:01 -03:00
committed by Xiang Xiao
parent 55fbf93c9c
commit 643b3586c3
15 changed files with 941 additions and 94 deletions
@@ -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 <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_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 */
+4
View File
@@ -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
@@ -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 <nuttx/config.h>
#include <stdio.h>
#include <syslog.h>
#include <nuttx/analog/adc.h>
#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;
}
@@ -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
@@ -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.
@@ -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 <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_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 */
@@ -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
@@ -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 <nuttx/config.h>
#include <stdio.h>
#include <syslog.h>
#include <nuttx/analog/adc.h>
#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;
}
@@ -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
@@ -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.
@@ -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
+1 -1
View File
@@ -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
@@ -27,10 +27,10 @@
#include <nuttx/config.h>
#include <stdio.h>
#include <syslog.h>
#include <debug.h>
#include <sys/types.h>
#include "esp32s3_adc.h"
#include <nuttx/analog/adc.h>
#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;
@@ -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
@@ -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();