Skip to content

Commit 238a525

Browse files
fdcavalcantixiaoxiang781216
authored andcommitted
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 <[email protected]>
1 parent 643b358 commit 238a525

File tree

15 files changed

+848
-0
lines changed

15 files changed

+848
-0
lines changed
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
/****************************************************************************
2+
* boards/risc-v/esp32c3/common/include/esp_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_RISCV_ESP32C3_COMMON_INCLUDE_ESP32C3_BOARD_ADC_H
24+
#define __BOARDS_RISCV_ESP32C3_COMMON_INCLUDE_ESP32C3_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+
* This function configures and initializes the ADC driver for the board.
56+
* It allocates memory for the ADC device structure, sets up the ADC
57+
* hardware, and registers the ADC device with the system.
58+
*
59+
* Input Parameters:
60+
* None.
61+
*
62+
* Returned Value:
63+
* Returns zero (OK) on successful initialization and registration of the
64+
* ADC device; 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_RISCV_ESP32C3_COMMON_INCLUDE_ESP32C3_BOARD_ADC_H */

boards/risc-v/esp32c3/common/src/Make.defs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@
2222

2323
ifeq ($(CONFIG_ARCH_BOARD_COMMON),y)
2424

25+
ifeq ($(CONFIG_ESPRESSIF_ADC),y)
26+
CSRCS += esp_board_adc.c
27+
endif
28+
2529
ifeq ($(CONFIG_ESPRESSIF_LEDC),y)
2630
CSRCS += esp_board_ledc.c
2731
endif
Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
/****************************************************************************
2+
* boards/risc-v/esp32c3/common/src/esp_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 "esp_board_adc.h"
36+
37+
/****************************************************************************
38+
* Pre-processor Definitions
39+
****************************************************************************/
40+
41+
/* The number of channels for each ADC */
42+
43+
#define ADC_MAX_CHANNELS 5
44+
45+
/****************************************************************************
46+
* Private Data
47+
****************************************************************************/
48+
49+
/* Select channels to be used for each ADC.
50+
*
51+
* GPIOs are fixed for each channel and configured in the lower-half driver.
52+
*
53+
* ADC 1
54+
* Channel: 0 1 2 3 4 5
55+
* GPIO: 0 1 2 3 4 5
56+
*
57+
* On the chanlist arrays below, channels are added +1. Do not change.
58+
*/
59+
60+
#ifdef CONFIG_ESPRESSIF_ADC_1
61+
static const uint8_t g_chanlist_adc1[ADC_MAX_CHANNELS] =
62+
{
63+
#ifdef CONFIG_ESPRESSIF_ADC_1_CH0
64+
1,
65+
#endif
66+
#ifdef CONFIG_ESPRESSIF_ADC_1_CH1
67+
2,
68+
#endif
69+
#ifdef CONFIG_ESPRESSIF_ADC_1_CH2
70+
3,
71+
#endif
72+
#ifdef CONFIG_ESPRESSIF_ADC_1_CH3
73+
4,
74+
#endif
75+
#ifdef CONFIG_ESPRESSIF_ADC_1_CH4
76+
5,
77+
#endif
78+
#ifdef CONFIG_ESPRESSIF_ADC_1_CH5
79+
6,
80+
#endif
81+
};
82+
#endif
83+
84+
/****************************************************************************
85+
* Public Functions
86+
****************************************************************************/
87+
88+
/****************************************************************************
89+
* Name: board_adc_init
90+
*
91+
* Description:
92+
* This function configures and initializes the ADC driver for the board.
93+
* It allocates memory for the ADC device structure, sets up the ADC
94+
* hardware, and registers the ADC device with the system.
95+
*
96+
* Input Parameters:
97+
* None.
98+
*
99+
* Returned Value:
100+
* Returns zero (OK) on successful initialization and registration of the
101+
* ADC device; a negated errno value is returned to indicate the nature
102+
* of any failure.
103+
*
104+
****************************************************************************/
105+
106+
int board_adc_init(void)
107+
{
108+
int ret;
109+
struct adc_dev_s *adcdev;
110+
111+
adcdev = kmm_malloc(sizeof(struct adc_dev_s));
112+
if (adcdev == NULL)
113+
{
114+
syslog(LOG_ERR, "ERROR: Failed to allocate adc_dev_s instance\n");
115+
return -ENOMEM;
116+
}
117+
118+
memset(adcdev, 0, sizeof(struct adc_dev_s));
119+
120+
adcdev = esp_adc_initialize(1, g_chanlist_adc1);
121+
122+
if (adcdev == NULL)
123+
{
124+
syslog(LOG_ERR, "ERROR: Failed to initialize ADC1\n");
125+
return -ENODEV;
126+
}
127+
128+
/* Register the ADC driver at "/dev/adcx" */
129+
130+
ret = adc_register("/dev/adc0", adcdev);
131+
if (ret < 0)
132+
{
133+
kmm_free(adcdev);
134+
syslog(LOG_ERR, "ERROR: adc_register /dev/adc0 failed: %d\n", ret);
135+
}
136+
137+
return ret;
138+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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_NSH_ARGCAT is not set
9+
# CONFIG_NSH_CMDOPT_HEXDUMP is not set
10+
CONFIG_ARCH="risc-v"
11+
CONFIG_ARCH_BOARD="esp32c3-generic"
12+
CONFIG_ARCH_BOARD_COMMON=y
13+
CONFIG_ARCH_BOARD_ESP32C3_GENERIC=y
14+
CONFIG_ARCH_CHIP="esp32c3"
15+
CONFIG_ARCH_CHIP_ESP32C3_GENERIC=y
16+
CONFIG_ARCH_INTERRUPTSTACK=1536
17+
CONFIG_ARCH_RISCV=y
18+
CONFIG_ARCH_STACKDUMP=y
19+
CONFIG_BOARDCTL_RESET=y
20+
CONFIG_BOARD_LOOPSPERMSEC=15000
21+
CONFIG_BUILTIN=y
22+
CONFIG_ESPRESSIF_ADC=y
23+
CONFIG_EXAMPLES_ADC=y
24+
CONFIG_EXAMPLES_ADC_SWTRIG=y
25+
CONFIG_FS_PROCFS=y
26+
CONFIG_IDLETHREAD_STACKSIZE=2048
27+
CONFIG_INIT_ENTRYPOINT="nsh_main"
28+
CONFIG_INTELHEX_BINARY=y
29+
CONFIG_LIBC_PERROR_STDOUT=y
30+
CONFIG_LIBC_STRERROR=y
31+
CONFIG_NFILE_DESCRIPTORS_PER_BLOCK=6
32+
CONFIG_NSH_ARCHINIT=y
33+
CONFIG_NSH_BUILTIN_APPS=y
34+
CONFIG_NSH_FILEIOSIZE=512
35+
CONFIG_NSH_READLINE=y
36+
CONFIG_NSH_STRERROR=y
37+
CONFIG_PREALLOC_TIMERS=0
38+
CONFIG_RR_INTERVAL=200
39+
CONFIG_SCHED_BACKTRACE=y
40+
CONFIG_SCHED_WAITPID=y
41+
CONFIG_START_DAY=29
42+
CONFIG_START_MONTH=11
43+
CONFIG_START_YEAR=2019
44+
CONFIG_SYSTEM_DUMPSTACK=y
45+
CONFIG_SYSTEM_NSH=y
46+
CONFIG_TESTING_GETPRIME=y
47+
CONFIG_TESTING_OSTEST=y
48+
CONFIG_UART0_SERIAL_CONSOLE=y

boards/risc-v/esp32c3/esp32c3-generic/src/esp32c3_bringup.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@
4141
#include "esp_board_i2c.h"
4242
#include "esp_board_bmp180.h"
4343

44+
#ifdef CONFIG_ESPRESSIF_ADC
45+
# include "esp_board_adc.h"
46+
#endif
47+
4448
#ifdef CONFIG_WATCHDOG
4549
# include "espressif/esp_wdt.h"
4650
#endif
@@ -399,6 +403,14 @@ int esp_bringup(void)
399403
}
400404
#endif
401405

406+
#ifdef CONFIG_ESPRESSIF_ADC
407+
ret = board_adc_init();
408+
if (ret < 0)
409+
{
410+
syslog(LOG_ERR, "Failed to initialize ADC driver: %d\n", ret);
411+
}
412+
#endif
413+
402414
/* If we got here then perhaps not all initialization was successful, but
403415
* at least enough succeeded to bring-up NSH with perhaps reduced
404416
* capabilities.
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
/****************************************************************************
2+
* boards/risc-v/esp32c6/common/include/esp_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_RISCV_ESP32C6_COMMON_INCLUDE_ESP32C6_BOARD_ADC_H
24+
#define __BOARDS_RISCV_ESP32C6_COMMON_INCLUDE_ESP32C6_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+
* This function configures and initializes the ADC driver for the board.
56+
* It allocates memory for the ADC device structure, sets up the ADC
57+
* hardware, and registers the ADC device with the system.
58+
*
59+
* Input Parameters:
60+
* None.
61+
*
62+
* Returned Value:
63+
* Returns zero (OK) on successful initialization and registration of the
64+
* ADC device; 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_RISCV_ESP32C6_COMMON_INCLUDE_ESP32C6_BOARD_ADC_H */

boards/risc-v/esp32c6/common/src/Make.defs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@
2222

2323
ifeq ($(CONFIG_ARCH_BOARD_COMMON),y)
2424

25+
ifeq ($(CONFIG_ESPRESSIF_ADC),y)
26+
CSRCS += esp_board_adc.c
27+
endif
28+
2529
ifeq ($(CONFIG_ESPRESSIF_LEDC),y)
2630
CSRCS += esp_board_ledc.c
2731
endif

0 commit comments

Comments
 (0)