mirror of
				https://github.com/espressif/esp-idf.git
				synced 2025-10-31 13:09:38 +00:00 
			
		
		
		
	esp_adc_cal: move esp_adc_cal_get_voltage into common source file
This commit is contained in:
		| @@ -1,7 +1,12 @@ | |||||||
| idf_build_get_property(target IDF_TARGET) | 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} | idf_component_register(SRCS ${srcs} | ||||||
|                        INCLUDE_DIRS include |                        INCLUDE_DIRS include | ||||||
|                        REQUIRES driver efuse) |                        REQUIRES driver | ||||||
|  |                        PRIV_REQUIRES efuse) | ||||||
|   | |||||||
							
								
								
									
										46
									
								
								components/esp_adc_cal/esp_adc_cal_common.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										46
									
								
								components/esp_adc_cal/esp_adc_cal_common.c
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,46 @@ | |||||||
|  | /* | ||||||
|  |  * SPDX-FileCopyrightText: 2020-2021 Espressif Systems (Shanghai) CO LTD | ||||||
|  |  * | ||||||
|  |  * SPDX-License-Identifier: Apache-2.0 | ||||||
|  |  */ | ||||||
|  |  | ||||||
|  | #include <stdint.h> | ||||||
|  | #include <string.h> | ||||||
|  | #include "esp_types.h" | ||||||
|  | #include "esp_err.h" | ||||||
|  | #include "esp_log.h" | ||||||
|  | #include "driver/adc.h" | ||||||
|  | #include "hal/adc_types.h" | ||||||
|  | #include "esp_adc_cal.h" | ||||||
|  |  | ||||||
|  | #define ADC_CAL_CHECK(cond, ret) ({                                         \ | ||||||
|  |             if(!(cond)){                                                    \ | ||||||
|  |                 return ret;                                                 \ | ||||||
|  |             }                                                               \ | ||||||
|  | }) | ||||||
|  |  | ||||||
|  | const __attribute__((unused)) 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 | ||||||
|  |     ADC_CAL_CHECK(chars != NULL, ESP_ERR_INVALID_ARG); | ||||||
|  |     ADC_CAL_CHECK(voltage != NULL, ESP_ERR_INVALID_ARG); | ||||||
|  |  | ||||||
|  |     esp_err_t ret = ESP_OK; | ||||||
|  |     int adc_reading; | ||||||
|  |     if (chars->adc_num == ADC_UNIT_1) { | ||||||
|  |         ADC_CAL_CHECK(channel < SOC_ADC_CHANNEL_NUM(0), ESP_ERR_INVALID_ARG); | ||||||
|  |         adc_reading = adc1_get_raw(channel); | ||||||
|  |     } else { | ||||||
|  |         ADC_CAL_CHECK(channel < SOC_ADC_CHANNEL_NUM(1), ESP_ERR_INVALID_ARG); | ||||||
|  |         ret = adc2_get_raw(channel, chars->bit_width, &adc_reading); | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     if (ret == ESP_OK) { | ||||||
|  |         *voltage = esp_adc_cal_raw_to_voltage((uint32_t)adc_reading, chars); | ||||||
|  |     } | ||||||
|  |     return ret; | ||||||
|  | } | ||||||
| @@ -1,16 +1,8 @@ | |||||||
| // Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD | /* | ||||||
| // |  * SPDX-FileCopyrightText: 2015-2021 Espressif Systems (Shanghai) CO LTD | ||||||
| // Licensed under the Apache License, Version 2.0 (the "License"); |  * | ||||||
| // you may not use this file except in compliance with the License. |  * SPDX-License-Identifier: Apache-2.0 | ||||||
| // 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. |  | ||||||
|  |  | ||||||
| #include <stdint.h> | #include <stdint.h> | ||||||
| #include "esp_types.h" | #include "esp_types.h" | ||||||
| @@ -82,12 +74,6 @@ | |||||||
| #define LUT_HIGH_THRESH                 (LUT_LOW_THRESH + LUT_ADC_STEP_SIZE) | #define LUT_HIGH_THRESH                 (LUT_LOW_THRESH + LUT_ADC_STEP_SIZE) | ||||||
| #define ADC_12_BIT_RES                  4096 | #define ADC_12_BIT_RES                  4096 | ||||||
|  |  | ||||||
| #define ADC_CAL_CHECK(cond, ret) ({                                         \ |  | ||||||
|             if(!(cond)){                                                    \ |  | ||||||
|                 return ret;                                                 \ |  | ||||||
|             }                                                               \ |  | ||||||
| }) |  | ||||||
|  |  | ||||||
| /* ------------------------ Characterization Constants ---------------------- */ | /* ------------------------ Characterization Constants ---------------------- */ | ||||||
| static const uint32_t adc1_tp_atten_scale[4] = {65504, 86975, 120389, 224310}; | 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}; | static const uint32_t adc2_tp_atten_scale[4] = {65467, 86861, 120416, 224708}; | ||||||
| @@ -366,27 +352,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); |         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 |  | ||||||
|     ADC_CAL_CHECK(chars != NULL, ESP_ERR_INVALID_ARG); |  | ||||||
|     ADC_CAL_CHECK(voltage != NULL, ESP_ERR_INVALID_ARG); |  | ||||||
|  |  | ||||||
|     int adc_reading; |  | ||||||
|     if (chars->adc_num == ADC_UNIT_1) { |  | ||||||
|         //Check channel is valid on ADC1 |  | ||||||
|         ADC_CAL_CHECK((adc1_channel_t)channel < ADC1_CHANNEL_MAX, ESP_ERR_INVALID_ARG); |  | ||||||
|         adc_reading = adc1_get_raw(channel); |  | ||||||
|     } else { |  | ||||||
|         //Check channel is valid on ADC2 |  | ||||||
|         ADC_CAL_CHECK((adc2_channel_t)channel < ADC2_CHANNEL_MAX, ESP_ERR_INVALID_ARG); |  | ||||||
|         if (adc2_get_raw(channel, chars->bit_width, &adc_reading) != ESP_OK) { |  | ||||||
|             return ESP_ERR_TIMEOUT;     //Timed out waiting for ADC2 |  | ||||||
|         } |  | ||||||
|     } |  | ||||||
|     *voltage = esp_adc_cal_raw_to_voltage((uint32_t)adc_reading, chars); |  | ||||||
|     return ESP_OK; |  | ||||||
| } |  | ||||||
|   | |||||||
| @@ -1,16 +1,8 @@ | |||||||
| // Copyright 2019-2020 Espressif Systems (Shanghai) PTE LTD | /* | ||||||
| // |  * SPDX-FileCopyrightText: 2019-2021 Espressif Systems (Shanghai) CO LTD | ||||||
| // Licensed under the Apache License, Version 2.0 (the "License"); |  * | ||||||
| // you may not use this file except in compliance with the License. |  * SPDX-License-Identifier: Apache-2.0 | ||||||
| // 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. |  | ||||||
|  |  | ||||||
| #include <stdint.h> | #include <stdint.h> | ||||||
| #include <stdbool.h> | #include <stdbool.h> | ||||||
| @@ -144,27 +136,3 @@ uint32_t esp_adc_cal_raw_to_voltage(uint32_t adc_reading, const esp_adc_cal_char | |||||||
|  |  | ||||||
|     return adc_reading * chars->coeff_a / coeff_a_scaling + chars->coeff_b / coeff_b_scaling; |     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 |  | ||||||
|     ADC_CALIB_CHECK(chars != NULL, "No characteristic input.", ESP_ERR_INVALID_ARG); |  | ||||||
|     ADC_CALIB_CHECK(voltage != NULL, "No output buffer.", ESP_ERR_INVALID_ARG); |  | ||||||
|  |  | ||||||
|     int adc_reading; |  | ||||||
|     if (chars->adc_num == ADC_UNIT_1) { |  | ||||||
|         //Check if channel is valid on ADC1 |  | ||||||
|         ADC_CALIB_CHECK((adc1_channel_t)channel < ADC1_CHANNEL_MAX, "Invalid channel", ESP_ERR_INVALID_ARG); |  | ||||||
|         adc_reading = adc1_get_raw(channel); |  | ||||||
|     } else { |  | ||||||
|         //Check if channel is valid on ADC2 |  | ||||||
|         ADC_CALIB_CHECK((adc2_channel_t)channel < ADC2_CHANNEL_MAX, "Invalid channel", ESP_ERR_INVALID_ARG); |  | ||||||
|         if (adc2_get_raw(channel, chars->bit_width, &adc_reading) != ESP_OK) { |  | ||||||
|             return ESP_ERR_TIMEOUT;     //Timed out waiting for ADC2 |  | ||||||
|         } |  | ||||||
|     } |  | ||||||
|     *voltage = esp_adc_cal_raw_to_voltage((uint32_t)adc_reading, chars); |  | ||||||
|     return ESP_OK; |  | ||||||
| } |  | ||||||
|   | |||||||
| @@ -1,16 +1,8 @@ | |||||||
| // Copyright 2019-2020 Espressif Systems (Shanghai) PTE LTD | /* | ||||||
| // |  * SPDX-FileCopyrightText: 2019-2021 Espressif Systems (Shanghai) CO LTD | ||||||
| // Licensed under the Apache License, Version 2.0 (the "License"); |  * | ||||||
| // you may not use this file except in compliance with the License. |  * SPDX-License-Identifier: Apache-2.0 | ||||||
| // 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. |  | ||||||
|  |  | ||||||
| #include <stdint.h> | #include <stdint.h> | ||||||
| #include "esp_types.h" | #include "esp_types.h" | ||||||
| @@ -178,6 +170,7 @@ esp_adc_cal_value_t esp_adc_cal_characterize(adc_unit_t adc_num, | |||||||
|         esp_adc_cal_characteristics_t *chars) |         esp_adc_cal_characteristics_t *chars) | ||||||
| { | { | ||||||
|     bool res; |     bool res; | ||||||
|  |     bool res __attribute__((unused)); | ||||||
|     adc_calib_parsed_info efuse_parsed_data = {0}; |     adc_calib_parsed_info efuse_parsed_data = {0}; | ||||||
|     // Check parameters |     // Check parameters | ||||||
|     assert((adc_num == ADC_UNIT_1) || (adc_num == ADC_UNIT_2)); |     assert((adc_num == ADC_UNIT_1) || (adc_num == ADC_UNIT_2)); | ||||||
| @@ -211,27 +204,3 @@ uint32_t esp_adc_cal_raw_to_voltage(uint32_t adc_reading, const esp_adc_cal_char | |||||||
|  |  | ||||||
|     return adc_reading * chars->coeff_a / coeff_a_scaling + chars->coeff_b / coeff_b_scaling; |     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 |  | ||||||
|     ADC_CAL_CHECK(chars != NULL, ESP_ERR_INVALID_ARG); |  | ||||||
|     ADC_CAL_CHECK(voltage != NULL, ESP_ERR_INVALID_ARG); |  | ||||||
|  |  | ||||||
|     int adc_reading; |  | ||||||
|     if (chars->adc_num == ADC_UNIT_1) { |  | ||||||
|         //Check if channel is valid on ADC1 |  | ||||||
|         ADC_CAL_CHECK((adc1_channel_t)channel < ADC1_CHANNEL_MAX, ESP_ERR_INVALID_ARG); |  | ||||||
|         adc_reading = adc1_get_raw(channel); |  | ||||||
|     } else { |  | ||||||
|         //Check if channel is valid on ADC2 |  | ||||||
|         ADC_CAL_CHECK((adc2_channel_t)channel < ADC2_CHANNEL_MAX, ESP_ERR_INVALID_ARG); |  | ||||||
|         if (adc2_get_raw(channel, chars->bit_width, &adc_reading) != ESP_OK) { |  | ||||||
|             return ESP_ERR_TIMEOUT;     //Timed out waiting for ADC2 |  | ||||||
|         } |  | ||||||
|     } |  | ||||||
|     *voltage = esp_adc_cal_raw_to_voltage((uint32_t)adc_reading, chars); |  | ||||||
|     return ESP_OK; |  | ||||||
| } |  | ||||||
|   | |||||||
| @@ -1,16 +1,8 @@ | |||||||
| // Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD | /* | ||||||
| // |  * SPDX-FileCopyrightText: 2015-2021 Espressif Systems (Shanghai) CO LTD | ||||||
| // Licensed under the Apache License, Version 2.0 (the "License"); |  * | ||||||
| // you may not use this file except in compliance with the License. |  * SPDX-License-Identifier: Apache-2.0 | ||||||
| // 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 __ESP_ADC_CAL_H__ | #ifndef __ESP_ADC_CAL_H__ | ||||||
| #define __ESP_ADC_CAL_H__ | #define __ESP_ADC_CAL_H__ | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user
	 Ivan Grokhotkov
					Ivan Grokhotkov