Skip to content

Commit 643b358

Browse files
fdcavalcantixiaoxiang781216
authored andcommitted
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>
1 parent 55fbf93 commit 643b358

File tree

15 files changed

+941
-94
lines changed

15 files changed

+941
-94
lines changed
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
/****************************************************************************
2+
* boards/xtensa/esp32/common/include/esp32_board_adc.h
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*
6+
* Licensed to the Apache Software Foundation (ASF) under one or more
7+
* contributor license agreements. See the NOTICE file distributed with
8+
* this work for additional information regarding copyright ownership. The
9+
* ASF licenses this file to you under the Apache License, Version 2.0 (the
10+
* "License"); you may not use this file except in compliance with the
11+
* License. You may obtain a copy of the License at
12+
*
13+
* http://www.apache.org/licenses/LICENSE-2.0
14+
*
15+
* Unless required by applicable law or agreed to in writing, software
16+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
17+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
18+
* License for the specific language governing permissions and limitations
19+
* under the License.
20+
*
21+
****************************************************************************/
22+
23+
#ifndef __BOARDS_XTENSA_ESP32_COMMON_INCLUDE_ESP32_BOARD_ADC_H
24+
#define __BOARDS_XTENSA_ESP32_COMMON_INCLUDE_ESP32_BOARD_ADC_H
25+
26+
/****************************************************************************
27+
* Included Files
28+
****************************************************************************/
29+
30+
#include <nuttx/config.h>
31+
32+
/****************************************************************************
33+
* Pre-processor Definitions
34+
****************************************************************************/
35+
36+
#ifndef __ASSEMBLY__
37+
38+
#undef EXTERN
39+
#if defined(__cplusplus)
40+
#define EXTERN extern "C"
41+
extern "C"
42+
{
43+
#else
44+
#define EXTERN extern
45+
#endif
46+
47+
/****************************************************************************
48+
* Public Function Prototypes
49+
****************************************************************************/
50+
51+
/****************************************************************************
52+
* Name: board_adc_init
53+
*
54+
* Description:
55+
* Initialize and configuree the ADC driver for the board.
56+
* It registers the ADC channels specified in the configuration and ensures
57+
* that the ADC hardware is properly set up for use.
58+
*
59+
* Input Parameters:
60+
* None.
61+
*
62+
* Returned Value:
63+
* Returns zero (OK) on successful initialization and registration of the
64+
* ADC channels; a negated errno value is returned to indicate the nature
65+
* of any failure.
66+
*
67+
****************************************************************************/
68+
69+
#ifdef CONFIG_ESPRESSIF_ADC
70+
int board_adc_init(void);
71+
#endif
72+
73+
#undef EXTERN
74+
#if defined(__cplusplus)
75+
}
76+
#endif
77+
78+
#endif /* __ASSEMBLY__ */
79+
#endif /* __BOARDS_XTENSA_ESP32_COMMON_INCLUDE_ESP32_BOARD_ADC_H */

boards/xtensa/esp32/common/src/Make.defs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ ifeq ($(CONFIG_WATCHDOG),y)
2626
CSRCS += esp32_board_wdt.c
2727
endif
2828

29+
ifeq ($(CONFIG_ESPRESSIF_ADC),y)
30+
CSRCS += esp32_board_adc.c
31+
endif
32+
2933
ifeq ($(CONFIG_ONESHOT),y)
3034
CSRCS += esp32_oneshot.c
3135
endif
Lines changed: 248 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,248 @@
1+
/****************************************************************************
2+
* boards/xtensa/esp32/common/src/esp32_board_adc.c
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*
6+
* Licensed to the Apache Software Foundation (ASF) under one or more
7+
* contributor license agreements. See the NOTICE file distributed with
8+
* this work for additional information regarding copyright ownership. The
9+
* ASF licenses this file to you under the Apache License, Version 2.0 (the
10+
* "License"); you may not use this file except in compliance with the
11+
* License. You may obtain a copy of the License at
12+
*
13+
* http://www.apache.org/licenses/LICENSE-2.0
14+
*
15+
* Unless required by applicable law or agreed to in writing, software
16+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
17+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
18+
* License for the specific language governing permissions and limitations
19+
* under the License.
20+
*
21+
****************************************************************************/
22+
23+
/****************************************************************************
24+
* Included Files
25+
****************************************************************************/
26+
27+
#include <nuttx/config.h>
28+
#include <stdio.h>
29+
#include <syslog.h>
30+
31+
#include <nuttx/analog/adc.h>
32+
33+
#include "espressif/esp_adc.h"
34+
35+
#include "esp32_board_adc.h"
36+
37+
/****************************************************************************
38+
* Pre-processor Definitions
39+
****************************************************************************/
40+
41+
/* The number of channels for each ADC */
42+
43+
#define ADC_1_MAX_CHANNELS 8
44+
#define ADC_2_MAX_CHANNELS 10
45+
46+
/****************************************************************************
47+
* Private Data
48+
****************************************************************************/
49+
50+
/* Select channels to be used for each ADC.
51+
*
52+
* GPIOs are fixed for each channel and configured in the lower-half driver.
53+
*
54+
* ADC 1
55+
* Channel: 0 1 2 3 4 5 6 7
56+
* GPIO: 36 37 38 39 32 33 34 35
57+
*
58+
* ADC 2
59+
* Channel: 0 1 2 3 4 5 6 7 8 9
60+
* GPIO: 4 0 2 15 13 12 14 27 25 26
61+
*
62+
* On the chanlist arrays below, channels are added +1. Do not change.
63+
* Important: if using more than 8 channels, edit CONFIG_ADC_FIFOSIZE.
64+
*/
65+
66+
#ifdef CONFIG_ESPRESSIF_ADC_1
67+
static const uint8_t g_chanlist_adc1[ADC_1_MAX_CHANNELS] =
68+
{
69+
#ifdef CONFIG_ESPRESSIF_ADC_1_CH0
70+
1,
71+
#endif
72+
#ifdef CONFIG_ESPRESSIF_ADC_1_CH1
73+
2,
74+
#endif
75+
#ifdef CONFIG_ESPRESSIF_ADC_1_CH2
76+
3,
77+
#endif
78+
#ifdef CONFIG_ESPRESSIF_ADC_1_CH3
79+
4,
80+
#endif
81+
#ifdef CONFIG_ESPRESSIF_ADC_1_CH4
82+
5,
83+
#endif
84+
#ifdef CONFIG_ESPRESSIF_ADC_1_CH5
85+
6,
86+
#endif
87+
#ifdef CONFIG_ESPRESSIF_ADC_1_CH6
88+
7,
89+
#endif
90+
#ifdef CONFIG_ESPRESSIF_ADC_1_CH7
91+
8,
92+
#endif
93+
};
94+
#endif
95+
96+
#ifdef CONFIG_ESPRESSIF_ADC_2
97+
static const uint8_t g_chanlist_adc2[ADC_2_MAX_CHANNELS] =
98+
{
99+
#ifdef CONFIG_ESPRESSIF_ADC_2_CH0
100+
1,
101+
#endif
102+
#ifdef CONFIG_ESPRESSIF_ADC_2_CH1
103+
2,
104+
#endif
105+
#ifdef CONFIG_ESPRESSIF_ADC_2_CH2
106+
3,
107+
#endif
108+
#ifdef CONFIG_ESPRESSIF_ADC_2_CH3
109+
4,
110+
#endif
111+
#ifdef CONFIG_ESPRESSIF_ADC_2_CH4
112+
5,
113+
#endif
114+
#ifdef CONFIG_ESPRESSIF_ADC_2_CH5
115+
6,
116+
#endif
117+
#ifdef CONFIG_ESPRESSIF_ADC_2_CH6
118+
7,
119+
#endif
120+
#ifdef CONFIG_ESPRESSIF_ADC_2_CH7
121+
8,
122+
#endif
123+
#ifdef CONFIG_ESPRESSIF_ADC_2_CH8
124+
9,
125+
#endif
126+
#ifdef CONFIG_ESPRESSIF_ADC_2_CH9
127+
10
128+
#endif
129+
};
130+
#endif
131+
132+
/****************************************************************************
133+
* Private Functions
134+
****************************************************************************/
135+
136+
/****************************************************************************
137+
* Name: board_adc_register
138+
*
139+
* Description:
140+
* This function registers the ADC driver for the specified ADC channel.
141+
* It initializes the ADC hardware, creates the device name, and registers
142+
* the ADC device with the system.
143+
*
144+
* Input Parameters:
145+
* adc_num - The ADC channel number to register.
146+
*
147+
* Returned Value:
148+
* Returns zero (OK) on successful registration; a negated errno value is
149+
* returned to indicate the nature of any failure.
150+
*
151+
****************************************************************************/
152+
153+
static int board_adc_register(int adc_num)
154+
{
155+
int ret;
156+
char devname[12];
157+
struct adc_dev_s *adcdev;
158+
159+
adcdev = kmm_malloc(sizeof(struct adc_dev_s));
160+
if (adcdev == NULL)
161+
{
162+
syslog(LOG_ERR, "ERROR: Failed to allocate adc_dev_s instance\n");
163+
return -ENOMEM;
164+
}
165+
166+
memset(adcdev, 0, sizeof(struct adc_dev_s));
167+
168+
switch (adc_num)
169+
{
170+
case 1:
171+
#ifdef CONFIG_ESPRESSIF_ADC_1
172+
adcdev = esp_adc_initialize(adc_num, g_chanlist_adc1);
173+
break;
174+
#endif
175+
case 2:
176+
#ifdef CONFIG_ESPRESSIF_ADC_2
177+
adcdev = esp_adc_initialize(adc_num, g_chanlist_adc2);
178+
break;
179+
#endif
180+
default:
181+
syslog(LOG_ERR, "ERROR: Unsupported ADC number: %d\n", adc_num);
182+
return ERROR;
183+
}
184+
185+
if (adcdev == NULL)
186+
{
187+
syslog(LOG_ERR, "ERROR: Failed to initialize ADC %d\n", adc_num);
188+
return -ENODEV;
189+
}
190+
191+
/* Register the ADC driver at "/dev/adcx" */
192+
193+
snprintf(devname, sizeof(devname), "/dev/adc%d", adc_num - 1);
194+
ret = adc_register(devname, adcdev);
195+
if (ret < 0)
196+
{
197+
kmm_free(adcdev);
198+
syslog(LOG_ERR, "ERROR: adc_register %s failed: %d\n", devname, ret);
199+
return ret;
200+
}
201+
202+
return ret;
203+
}
204+
205+
/****************************************************************************
206+
* Public Functions
207+
****************************************************************************/
208+
209+
/****************************************************************************
210+
* Name: board_adc_init
211+
*
212+
* Description:
213+
* Initialize and configuree the ADC driver for the board.
214+
* It registers the ADC channels specified in the configuration and ensures
215+
* that the ADC hardware is properly set up for use.
216+
*
217+
* Input Parameters:
218+
* None.
219+
*
220+
* Returned Value:
221+
* Returns zero (OK) on successful initialization and registration of the
222+
* ADC channels; a negated errno value is returned to indicate the nature
223+
* of any failure.
224+
*
225+
****************************************************************************/
226+
227+
int board_adc_init(void)
228+
{
229+
int ret;
230+
231+
#ifdef CONFIG_ESPRESSIF_ADC_1
232+
ret = board_adc_register(1);
233+
if (ret != OK)
234+
{
235+
return ret;
236+
}
237+
#endif
238+
239+
#ifdef CONFIG_ESPRESSIF_ADC_2
240+
ret = board_adc_register(2);
241+
if (ret != OK)
242+
{
243+
return ret;
244+
}
245+
#endif
246+
247+
return ret;
248+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#
2+
# This file is autogenerated: PLEASE DO NOT EDIT IT.
3+
#
4+
# You can use "make menuconfig" to make any modifications to the installed .config file.
5+
# You can then do "make savedefconfig" to generate a new defconfig file that includes your
6+
# modifications.
7+
#
8+
# CONFIG_ARCH_LEDS is not set
9+
# CONFIG_NSH_ARGCAT is not set
10+
# CONFIG_NSH_CMDOPT_HEXDUMP is not set
11+
CONFIG_ARCH="xtensa"
12+
CONFIG_ARCH_BOARD="esp32-devkitc"
13+
CONFIG_ARCH_BOARD_COMMON=y
14+
CONFIG_ARCH_BOARD_ESP32_DEVKITC=y
15+
CONFIG_ARCH_CHIP="esp32"
16+
CONFIG_ARCH_CHIP_ESP32=y
17+
CONFIG_ARCH_CHIP_ESP32WROVER=y
18+
CONFIG_ARCH_STACKDUMP=y
19+
CONFIG_ARCH_XTENSA=y
20+
CONFIG_BOARD_LOOPSPERMSEC=16717
21+
CONFIG_BUILTIN=y
22+
CONFIG_ESP32_UART0=y
23+
CONFIG_ESPRESSIF_ADC=y
24+
CONFIG_EXAMPLES_ADC=y
25+
CONFIG_EXAMPLES_ADC_SWTRIG=y
26+
CONFIG_FS_PROCFS=y
27+
CONFIG_HAVE_CXX=y
28+
CONFIG_HAVE_CXXINITIALIZE=y
29+
CONFIG_IDLETHREAD_STACKSIZE=3072
30+
CONFIG_INIT_ENTRYPOINT="nsh_main"
31+
CONFIG_INIT_STACKSIZE=3072
32+
CONFIG_INTELHEX_BINARY=y
33+
CONFIG_LINE_MAX=64
34+
CONFIG_MM_REGIONS=3
35+
CONFIG_NSH_ARCHINIT=y
36+
CONFIG_NSH_BUILTIN_APPS=y
37+
CONFIG_NSH_FILEIOSIZE=512
38+
CONFIG_NSH_READLINE=y
39+
CONFIG_PREALLOC_TIMERS=4
40+
CONFIG_RAM_SIZE=114688
41+
CONFIG_RAM_START=0x20000000
42+
CONFIG_RR_INTERVAL=200
43+
CONFIG_SCHED_WAITPID=y
44+
CONFIG_START_DAY=6
45+
CONFIG_START_MONTH=12
46+
CONFIG_START_YEAR=2011
47+
CONFIG_SYSLOG_BUFFER=y
48+
CONFIG_SYSTEM_NSH=y
49+
CONFIG_UART0_SERIAL_CONSOLE=y

0 commit comments

Comments
 (0)