riscv/esp32c3: Add ESP32-C3 ADC driver

This commit is contained in:
Dong Heng
2021-05-18 09:20:46 -03:00
committed by Alan Carvalho de Assis
parent 26a5cb2094
commit f12de4f7d9
11 changed files with 2119 additions and 0 deletions
+57
View File
@@ -158,6 +158,12 @@ config ESP32C3_DISABLE_STDC_ATOMIC
menu "ESP32-C3 Peripheral Support"
config ESP32C3_ADC
bool
default n
select ANALOG
select ADC
config ESP32C3_UART
bool
default n
@@ -274,6 +280,11 @@ config ESP32C3_RWDT
to have the RTC module reset, please, use the Timers' Module WDTs.
They will only reset Main System.
config ESP32C3_ADC1
bool "ADC1"
default n
select ESP32C3_ADC
config ESP32C3_WIRELESS
bool "Wireless"
default n
@@ -511,6 +522,52 @@ config ESP32C3_LEDC_CHANNEL5_PIN
endmenu # LEDC configuration
menu "ADC Configuration"
depends on ESP32C3_ADC
if ESP32C3_ADC1
choice ESP32C3_ADC_VOL_RANGES
prompt "ADC1 voltage ranges"
default ESP32C3_ADC_VOL_750
config ESP32C3_ADC_VOL_750
bool "0~750mV"
config ESP32C3_ADC_VOL_1050
bool "0~1050mV"
config ESP32C3_ADC_VOL_1300
bool "0~1300mV"
config ESP32C3_ADC_VOL_2500
bool "0~2500mV"
endchoice
config ESP32C3_ADC1_CHANNEL0
bool "ADC1 channel 0"
default n
config ESP32C3_ADC1_CHANNEL1
bool "ADC1 channel 1"
default n
config ESP32C3_ADC1_CHANNEL2
bool "ADC1 channel 2"
default n
config ESP32C3_ADC1_CHANNEL3
bool "ADC1 channel 3"
default n
config ESP32C3_ADC1_CHANNEL4
bool "ADC1 channel 4"
default n
endif # ESP32C3_ADC1
endmenu # ADC Configuration
menu "Wi-Fi configuration"
depends on ESP32C3_WIRELESS
+4
View File
@@ -125,6 +125,10 @@ ifeq ($(CONFIG_ESP32C3_LEDC),y)
CHIP_CSRCS += esp32c3_ledc.c
endif
ifeq ($(CONFIG_ESP32C3_ADC),y)
CHIP_CSRCS += esp32c3_adc.c
endif
ifeq ($(CONFIG_ESP32C3_WIRELESS),y)
WIRELESS_DRV_UNPACK = esp-wireless-drivers-3rdparty
WIRELESS_DRV_ID = 2b53111
File diff suppressed because it is too large Load Diff
+79
View File
@@ -0,0 +1,79 @@
/****************************************************************************
* arch/risc-v/src/esp32c3/esp32c3_adc.h
*
* 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 __ARCH_RISCV_SRC_ESP32C3_ESP32C3_ADC_H
#define __ARCH_RISCV_SRC_ESP32C3_ESP32C3_ADC_H
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <nuttx/arch.h>
#include <nuttx/analog/adc.h>
#include <nuttx/analog/ioctl.h>
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
/****************************************************************************
* Public Types
****************************************************************************/
#ifndef __ASSEMBLY__
/****************************************************************************
* Public Data
****************************************************************************/
#undef EXTERN
#if defined(__cplusplus)
#define EXTERN extern "C"
extern "C"
{
#else
#define EXTERN extern
#endif
/****************************************************************************
* Name: esp32c3_adc_init
*
* Description:
* Initialize the ADC.
*
* Input Parameters:
* channel - ADC channel number
*
* Returned Value:
* ADC device structure reference on success; a NULL on failure
*
****************************************************************************/
struct adc_dev_s *esp32c3_adc_init(int channel);
#ifdef __cplusplus
}
#endif
#undef EXTERN
#endif /* __ASSEMBLY__ */
#endif /* __ARCH_RISCV_SRC_ESP32C3_ESP32C3_ADC_H */
File diff suppressed because it is too large Load Diff
@@ -0,0 +1,52 @@
/****************************************************************************
* arch/risc-v/src/esp32c3/hardware/regi2c_saradc.h
*
* 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 __ARCH_RISCV_SRC_ESP32C3_HARDWARE_REGI2C_SARADC_H_
#define __ARCH_RISCV_SRC_ESP32C3_HARDWARE_REGI2C_SARADC_H_
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
/**
* Register definitions for analog to calibrate initial code for getting a
* more precise voltage of SAR ADC.
*/
#define I2C_ADC (0x69)
#define I2C_ADC_HOSTID (0)
#define I2C_ADC1_ENCAL_GND (0x7)
#define I2C_ADC1_ENCAL_GND_MSB (0x5)
#define I2C_ADC1_ENCAL_GND_LSB (0x5)
#define I2C_ADC1_INITVAL_L (0x0)
#define I2C_ADC1_INITVAL_L_MSB (0x7)
#define I2C_ADC1_INITVAL_L_LSB (0x0)
#define I2C_ADC1_INITVAL_H (0x1)
#define I2C_ADC1_INITVAL_H_MSB (0x3)
#define I2C_ADC1_INITVAL_H_LSB (0x0)
#define I2C_ADC1_DEF (0x2)
#define I2C_ADC1_DEF_MSB (0x6)
#define I2C_ADC1_DEF_LSB (0x4)
#endif /* __ARCH_RISCV_SRC_ESP32C3_HARDWARE_REGI2C_SARADC_H_ */
@@ -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_NSH_CMDPARMS is not set
CONFIG_ARCH="risc-v"
CONFIG_ARCH_BOARD="esp32c3-devkit"
CONFIG_ARCH_BOARD_ESP32C3_DEVKIT=y
CONFIG_ARCH_CHIP="esp32c3"
CONFIG_ARCH_CHIP_ESP32C3=y
CONFIG_ARCH_CHIP_ESP32C3WROOM02=y
CONFIG_ARCH_INTERRUPTSTACK=1536
CONFIG_ARCH_RISCV=y
CONFIG_ARCH_STACKDUMP=y
CONFIG_BOARD_LOOPSPERMSEC=15000
CONFIG_BUILTIN=y
CONFIG_DEV_ZERO=y
CONFIG_ESP32C3_ADC1=y
CONFIG_ESP32C3_ADC1_CHANNEL0=y
CONFIG_EXAMPLES_ADC=y
CONFIG_EXAMPLES_ADC_GROUPSIZE=1
CONFIG_EXAMPLES_ADC_SWTRIG=y
CONFIG_FS_PROCFS=y
CONFIG_IDLETHREAD_STACKSIZE=2048
CONFIG_INTELHEX_BINARY=y
CONFIG_LIBC_PERROR_STDOUT=y
CONFIG_LIBC_STRERROR=y
CONFIG_MAX_TASKS=16
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_RAW_BINARY=y
CONFIG_RR_INTERVAL=200
CONFIG_SCHED_WAITPID=y
CONFIG_START_DAY=29
CONFIG_START_MONTH=11
CONFIG_START_YEAR=2019
CONFIG_SYSTEM_NSH=y
CONFIG_UART0_SERIAL_CONSOLE=y
CONFIG_USER_ENTRYPOINT="nsh_main"
@@ -70,6 +70,10 @@ ifeq ($(CONFIG_PWM),y)
CSRCS += esp32c3_ledc.c
endif
ifeq ($(CONFIG_ADC),y)
CSRCS += esp32c3_adc.c
endif
SCRIPTIN = $(SCRIPTDIR)$(DELIM)esp32c3.template.ld
SCRIPTOUT = $(SCRIPTDIR)$(DELIM)esp32c3_out.ld
@@ -193,5 +193,21 @@ int esp32c3_spiflash_init(void);
int esp32c3_pwm_setup(void);
#endif
/****************************************************************************
* Name: board_adc_init
*
* Description:
* Configure the ADC driver.
*
* Returned Value:
* Zero (OK) is returned on success; A negated errno value is returned
* to indicate the nature of any failure.
*
****************************************************************************/
#ifdef CONFIG_ADC
int board_adc_init(void);
#endif
#endif /* __ASSEMBLY__ */
#endif /* __BOARDS_RISCV_ESP32C3_ESP32C3_DEVKIT_SRC_ESP32C3_DEVKIT_H */
@@ -0,0 +1,150 @@
/****************************************************************************
* boards/risc-v/esp32c3/esp32c3-devkit/src/esp32c3_adc.c
*
* 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 <debug.h>
#include <sys/types.h>
#include "esp32c3_adc.h"
#include "esp32c3-devkit.h"
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: board_adc_init
*
* Description:
* Configure the ADC driver.
*
* Returned Value:
* Zero (OK) is returned on success; A negated errno value is returned
* to indicate the nature of any failure.
*
****************************************************************************/
int board_adc_init(void)
{
int ret;
struct adc_dev_s *adc;
#ifdef CONFIG_ESP32C3_ADC1_CHANNEL0
adc = esp32c3_adc_init(0);
if (!adc)
{
syslog(LOG_ERR, "ERROR: Failed to get ADC channel 0 dev\n");
return -ENODEV;
}
/* Register the ADC driver at "/dev/adc0" */
ret = adc_register("/dev/adc0", adc);
if (ret < 0)
{
syslog(LOG_ERR, "ERROR: adc_register failed: %d\n", ret);
return ret;
}
#endif
#ifdef CONFIG_ESP32C3_ADC1_CHANNEL1
adc = esp32c3_adc_init(1);
if (!adc)
{
syslog(LOG_ERR, "ERROR: Failed to get ADC channel 1 dev\n");
return -ENODEV;
}
/* Register the ADC driver at "/dev/adc1" */
ret = adc_register("/dev/adc1", adc);
if (ret < 0)
{
syslog(LOG_ERR, "ERROR: adc_register failed: %d\n", ret);
return ret;
}
#endif
#ifdef CONFIG_ESP32C3_ADC1_CHANNEL2
adc = esp32c3_adc_init(2);
if (!adc)
{
syslog(LOG_ERR, "ERROR: Failed to get ADC channel 2 dev\n");
return -ENODEV;
}
/* Register the ADC driver at "/dev/adc2" */
ret = adc_register("/dev/adc2", adc);
if (ret < 0)
{
syslog(LOG_ERR, "ERROR: adc_register failed: %d\n", ret);
return ret;
}
#endif
#ifdef CONFIG_ESP32C3_ADC1_CHANNEL3
adc = esp32c3_adc_init(3);
if (!adc)
{
syslog(LOG_ERR, "ERROR: Failed to get ADC channel 3 dev\n");
return -ENODEV;
}
/* Register the ADC driver at "/dev/adc3" */
ret = adc_register("/dev/adc3", adc);
if (ret < 0)
{
syslog(LOG_ERR, "ERROR: adc_register failed: %d\n", ret);
return ret;
}
#endif
#ifdef CONFIG_ESP32C3_ADC1_CHANNEL4
adc = esp32c3_adc_init(4);
if (!adc)
{
syslog(LOG_ERR, "ERROR: Failed to get ADC channel 4 dev\n");
return -ENODEV;
}
/* Register the ADC driver at "/dev/adc4" */
ret = adc_register("/dev/adc4", adc);
if (ret < 0)
{
syslog(LOG_ERR, "ERROR: adc_register failed: %d\n", ret);
return ret;
}
#endif
return ret;
}
@@ -291,6 +291,15 @@ int esp32c3_bringup(void)
}
#endif /* CONFIG_ESP32C3_LEDC */
#ifdef CONFIG_ESP32C3_ADC
ret = board_adc_init();
if (ret)
{
syslog(LOG_ERR, "ERROR: board_adc_init() failed: %d\n", ret);
return ret;
}
#endif /* CONFIG_ESP32C3_ADC */
/* If we got here then perhaps not all initialization was successful, but
* at least enough succeeded to bring-up NSH with perhaps reduced
* capabilities.