From 492afe4d4a07dae883eabb8d347afd16f67021b8 Mon Sep 17 00:00:00 2001 From: Ivan Grokhotkov Date: Thu, 4 Nov 2021 13:01:19 +0100 Subject: [PATCH] esp_adc_cal: move esp_adc_cal_get_voltage into common source file --- components/esp_adc_cal/CMakeLists.txt | 9 +++- components/esp_adc_cal/esp_adc_cal_common.c | 38 +++++++++++++++++ components/esp_adc_cal/esp_adc_cal_esp32.c | 43 +++----------------- components/esp_adc_cal/esp_adc_cal_esp32c3.c | 41 +++---------------- components/esp_adc_cal/esp_adc_cal_esp32s2.c | 41 +++---------------- components/esp_adc_cal/esp_adc_cal_esp32s3.c | 21 ---------- components/esp_adc_cal/include/esp_adc_cal.h | 18 +++----- 7 files changed, 65 insertions(+), 146 deletions(-) create mode 100644 components/esp_adc_cal/esp_adc_cal_common.c diff --git a/components/esp_adc_cal/CMakeLists.txt b/components/esp_adc_cal/CMakeLists.txt index 5758cf6d1f..e2fbb5c456 100644 --- a/components/esp_adc_cal/CMakeLists.txt +++ b/components/esp_adc_cal/CMakeLists.txt @@ -1,7 +1,12 @@ idf_build_get_property(target IDF_TARGET) -set(srcs "esp_adc_cal_${target}.c") +set(srcs "esp_adc_cal_common.c") +set(src_target "esp_adc_cal_${target}.c") +if(EXISTS "${CMAKE_CURRENT_LIST_DIR}/${src_target}") + list(APPEND srcs ${src_target}) +endif() idf_component_register(SRCS ${srcs} INCLUDE_DIRS include - REQUIRES driver efuse) + REQUIRES driver + PRIV_REQUIRES efuse) diff --git a/components/esp_adc_cal/esp_adc_cal_common.c b/components/esp_adc_cal/esp_adc_cal_common.c new file mode 100644 index 0000000000..809c001625 --- /dev/null +++ b/components/esp_adc_cal/esp_adc_cal_common.c @@ -0,0 +1,38 @@ +/* + * SPDX-FileCopyrightText: 2020-2021 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include +#include +#include "esp_types.h" +#include "esp_err.h" +#include "esp_log.h" +#include "esp_check.h" +#include "driver/adc.h" +#include "hal/adc_types.h" +#include "esp_adc_cal.h" + +const static char *TAG = "ADC_CALI"; + +esp_err_t esp_adc_cal_get_voltage(adc_channel_t channel, + const esp_adc_cal_characteristics_t *chars, + uint32_t *voltage) +{ + // Check parameters + ESP_RETURN_ON_FALSE(chars != NULL, ESP_ERR_INVALID_ARG, TAG, "No characteristic input"); + ESP_RETURN_ON_FALSE(voltage != NULL, ESP_ERR_INVALID_ARG, TAG, "No output buffer"); + + esp_err_t ret = ESP_OK; + int adc_reading; + if (chars->adc_num == ADC_UNIT_1) { + ESP_RETURN_ON_FALSE(channel < SOC_ADC_CHANNEL_NUM(0), ESP_ERR_INVALID_ARG, TAG, "Invalid channel"); + adc_reading = adc1_get_raw(channel); + } else { + ESP_RETURN_ON_FALSE(channel < SOC_ADC_CHANNEL_NUM(1), ESP_ERR_INVALID_ARG, TAG, "Invalid channel"); + ret = adc2_get_raw(channel, chars->bit_width, &adc_reading); + } + *voltage = esp_adc_cal_raw_to_voltage((uint32_t)adc_reading, chars); + return ret; +} diff --git a/components/esp_adc_cal/esp_adc_cal_esp32.c b/components/esp_adc_cal/esp_adc_cal_esp32.c index 9c21e3ec33..187a35c3ed 100644 --- a/components/esp_adc_cal/esp_adc_cal_esp32.c +++ b/components/esp_adc_cal/esp_adc_cal_esp32.c @@ -1,16 +1,8 @@ -// Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD -// -// Licensed 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. +/* + * SPDX-FileCopyrightText: 2015-2021 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ #include #include "esp_types.h" @@ -83,8 +75,6 @@ #define LUT_HIGH_THRESH (LUT_LOW_THRESH + LUT_ADC_STEP_SIZE) #define ADC_12_BIT_RES 4096 -const static char LOG_TAG[] = "ADC_CALI"; - /* ------------------------ Characterization Constants ---------------------- */ static const uint32_t adc1_tp_atten_scale[4] = {65504, 86975, 120389, 224310}; static const uint32_t adc2_tp_atten_scale[4] = {65467, 86861, 120416, 224708}; @@ -363,26 +353,3 @@ uint32_t esp_adc_cal_raw_to_voltage(uint32_t adc_reading, const esp_adc_cal_char return calculate_voltage_linear(adc_reading, chars->coeff_a, chars->coeff_b); } } - -esp_err_t esp_adc_cal_get_voltage(adc_channel_t channel, - const esp_adc_cal_characteristics_t *chars, - uint32_t *voltage) -{ - //Check parameters - ESP_RETURN_ON_FALSE(chars != NULL, ESP_ERR_INVALID_ARG, LOG_TAG, "No characteristic input"); - ESP_RETURN_ON_FALSE(voltage != NULL, ESP_ERR_INVALID_ARG, LOG_TAG, "No output buffer"); - - esp_err_t ret = ESP_OK; - int adc_reading; - if (chars->adc_num == ADC_UNIT_1) { - //Check channel is valid on ADC1 - ESP_RETURN_ON_FALSE(channel < SOC_ADC_CHANNEL_NUM(0), ESP_ERR_INVALID_ARG, LOG_TAG, "Invalid channel"); - adc_reading = adc1_get_raw(channel); - } else { - //Check channel is valid on ADC2 - ESP_RETURN_ON_FALSE(channel < SOC_ADC_CHANNEL_NUM(1), ESP_ERR_INVALID_ARG, LOG_TAG, "Invalid channel"); - ret = adc2_get_raw(channel, chars->bit_width, &adc_reading); - } - *voltage = esp_adc_cal_raw_to_voltage((uint32_t)adc_reading, chars); - return ret; -} diff --git a/components/esp_adc_cal/esp_adc_cal_esp32c3.c b/components/esp_adc_cal/esp_adc_cal_esp32c3.c index 7799e5cb1a..fab3e78b65 100644 --- a/components/esp_adc_cal/esp_adc_cal_esp32c3.c +++ b/components/esp_adc_cal/esp_adc_cal_esp32c3.c @@ -1,16 +1,8 @@ -// Copyright 2019-2020 Espressif Systems (Shanghai) PTE LTD -// -// Licensed 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. +/* + * SPDX-FileCopyrightText: 2019-2021 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ #include #include @@ -136,26 +128,3 @@ uint32_t esp_adc_cal_raw_to_voltage(uint32_t adc_reading, const esp_adc_cal_char assert(chars != NULL); return adc_reading * chars->coeff_a / coeff_a_scaling + chars->coeff_b / coeff_b_scaling; } - -esp_err_t esp_adc_cal_get_voltage(adc_channel_t channel, - const esp_adc_cal_characteristics_t *chars, - uint32_t *voltage) -{ - // Check parameters - ESP_RETURN_ON_FALSE(chars != NULL, ESP_ERR_INVALID_ARG, LOG_TAG, "No characteristic input"); - ESP_RETURN_ON_FALSE(voltage != NULL, ESP_ERR_INVALID_ARG, LOG_TAG, "No output buffer"); - - esp_err_t ret = ESP_OK; - int adc_reading; - if (chars->adc_num == ADC_UNIT_1) { - //Check if channel is valid on ADC1 - ESP_RETURN_ON_FALSE(channel < SOC_ADC_CHANNEL_NUM(0), ESP_ERR_INVALID_ARG, LOG_TAG, "Invalid channel"); - adc_reading = adc1_get_raw(channel); - } else { - //Check if channel is valid on ADC2 - ESP_RETURN_ON_FALSE(channel < SOC_ADC_CHANNEL_NUM(1), ESP_ERR_INVALID_ARG, LOG_TAG, "Invalid channel"); - ret = adc2_get_raw(channel, chars->bit_width, &adc_reading); - } - *voltage = esp_adc_cal_raw_to_voltage((uint32_t)adc_reading, chars); - return ret; -} diff --git a/components/esp_adc_cal/esp_adc_cal_esp32s2.c b/components/esp_adc_cal/esp_adc_cal_esp32s2.c index 94b9682cdb..4de5b11eba 100644 --- a/components/esp_adc_cal/esp_adc_cal_esp32s2.c +++ b/components/esp_adc_cal/esp_adc_cal_esp32s2.c @@ -1,16 +1,8 @@ -// Copyright 2019-2020 Espressif Systems (Shanghai) PTE LTD -// -// Licensed 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. +/* + * SPDX-FileCopyrightText: 2019-2021 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ #include #include "esp_types.h" @@ -206,26 +198,3 @@ uint32_t esp_adc_cal_raw_to_voltage(uint32_t adc_reading, const esp_adc_cal_char assert(chars != NULL); return adc_reading * chars->coeff_a / coeff_a_scaling + chars->coeff_b / coeff_b_scaling; } - -esp_err_t esp_adc_cal_get_voltage(adc_channel_t channel, - const esp_adc_cal_characteristics_t *chars, - uint32_t *voltage) -{ - // Check parameters - ESP_RETURN_ON_FALSE(chars != NULL, ESP_ERR_INVALID_ARG, LOG_TAG, "No characteristic input"); - ESP_RETURN_ON_FALSE(voltage != NULL, ESP_ERR_INVALID_ARG, LOG_TAG, "No output buffer"); - - esp_err_t ret = ESP_OK; - int adc_reading; - if (chars->adc_num == ADC_UNIT_1) { - //Check if channel is valid on ADC1 - ESP_RETURN_ON_FALSE(channel < SOC_ADC_CHANNEL_NUM(0), ESP_ERR_INVALID_ARG, LOG_TAG, "Invalid channel"); - adc_reading = adc1_get_raw(channel); - } else { - //Check if channel is valid on ADC2 - ESP_RETURN_ON_FALSE(channel < SOC_ADC_CHANNEL_NUM(1), ESP_ERR_INVALID_ARG, LOG_TAG, "Invalid channel"); - ret = adc2_get_raw(channel, chars->bit_width, &adc_reading); - } - *voltage = esp_adc_cal_raw_to_voltage((uint32_t)adc_reading, chars); - return ret; -} diff --git a/components/esp_adc_cal/esp_adc_cal_esp32s3.c b/components/esp_adc_cal/esp_adc_cal_esp32s3.c index f7014fdf9f..7b9726d8bb 100644 --- a/components/esp_adc_cal/esp_adc_cal_esp32s3.c +++ b/components/esp_adc_cal/esp_adc_cal_esp32s3.c @@ -214,24 +214,3 @@ uint32_t esp_adc_cal_raw_to_voltage(uint32_t adc_reading, const esp_adc_cal_char return voltage; } - -esp_err_t esp_adc_cal_get_voltage(adc_channel_t channel, - const esp_adc_cal_characteristics_t *chars, - uint32_t *voltage) -{ - // Check parameters - ESP_RETURN_ON_FALSE(chars != NULL, ESP_ERR_INVALID_ARG, LOG_TAG, "No characteristic input"); - ESP_RETURN_ON_FALSE(voltage != NULL, ESP_ERR_INVALID_ARG, LOG_TAG, "No output buffer"); - - esp_err_t ret = ESP_OK; - int adc_reading; - if (chars->adc_num == ADC_UNIT_1) { - ESP_RETURN_ON_FALSE(channel < SOC_ADC_CHANNEL_NUM(0), ESP_ERR_INVALID_ARG, LOG_TAG, "Invalid channel"); - adc_reading = adc1_get_raw(channel); - } else { - ESP_RETURN_ON_FALSE(channel < SOC_ADC_CHANNEL_NUM(1), ESP_ERR_INVALID_ARG, LOG_TAG, "Invalid channel"); - ret = adc2_get_raw(channel, chars->bit_width, &adc_reading); - } - *voltage = esp_adc_cal_raw_to_voltage((uint32_t)adc_reading, chars); - return ret; -} diff --git a/components/esp_adc_cal/include/esp_adc_cal.h b/components/esp_adc_cal/include/esp_adc_cal.h index 171b2ed850..6be5ef2453 100644 --- a/components/esp_adc_cal/include/esp_adc_cal.h +++ b/components/esp_adc_cal/include/esp_adc_cal.h @@ -1,16 +1,8 @@ -// Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD -// -// Licensed 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. +/* + * SPDX-FileCopyrightText: 2015-2021 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ #ifndef __ESP_ADC_CAL_H__ #define __ESP_ADC_CAL_H__