1 /*
2 * Copyright (c) 2006-2023, RT-Thread Development Team
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 *
6 * Change Logs:
7 * Date Author Notes
8 * 2023-11-23 Chushicheng first version
9 */
10
11 #include <rtthread.h>
12 #include <rtdevice.h>
13 #include "drv_adc.h"
14 #include "esp_adc/adc_oneshot.h"
15 #include "esp_adc/adc_cali.h"
16 #include "esp_adc/adc_cali_scheme.h"
17 #include "hal/adc_types.h"
18
19 #ifdef BSP_USING_ADC
20 #define DBG_TAG "drv.adc"
21 #define DBG_LVL DBG_INFO
22 #include <rtdbg.h>
23
24 #define EXAMPLE_READ_LEN 256
25 #define GET_UNIT(x) ((x>>3) & 0x1)
26
27 /* esp i2c dirver class */
28 struct esp_adc
29 {
30 struct rt_adc_ops ops;
31 struct rt_adc_device adc_device;
32 rt_uint8_t adc_id;
33 adc_oneshot_unit_handle_t handle;
34 adc_cali_handle_t cali_handle;
35 rt_uint8_t do_calibration1;
36 };
37
38 static struct esp_adc_config adc_config[] =
39 {
40 #ifdef BSP_USING_ADC1
41 {
42 .adc_id = ADC_UNIT_1,
43 .device_name = "adc1",
44 },
45 #endif
46 };
47
48 static struct esp_adc esp_adc_obj[sizeof(adc_config) / sizeof(adc_config[0])];
49
_adc_enabled(struct rt_adc_device * device,rt_int8_t channel,rt_bool_t enabled)50 static rt_err_t _adc_enabled(struct rt_adc_device *device, rt_int8_t channel, rt_bool_t enabled)
51 {
52 struct esp_adc *_adc = rt_container_of(device, struct esp_adc, adc_device);
53
54 if(enabled)
55 {
56 //-------------ADC Init---------------//
57 adc_oneshot_unit_init_cfg_t init_config = {
58 .unit_id = _adc->adc_id,
59 };
60 ESP_ERROR_CHECK(adc_oneshot_new_unit(&init_config, &_adc->handle));
61 //-------------ADC Config---------------//
62 adc_oneshot_chan_cfg_t config = {
63 .bitwidth = ADC_BITWIDTH_DEFAULT,
64 .atten = ADC_ATTEN_DB_11,
65 };
66 ESP_ERROR_CHECK(adc_oneshot_config_channel(_adc->handle, channel, &config));
67
68 //-------------ADC Calibration Init---------------//
69 adc_cali_curve_fitting_config_t cali_config = {
70 .unit_id = _adc->adc_id,
71 .atten = ADC_ATTEN_DB_11,
72 .bitwidth = ADC_BITWIDTH_DEFAULT,
73 };
74 if (adc_cali_create_scheme_curve_fitting(&cali_config, &_adc->cali_handle) == ESP_OK)
75 {
76 _adc->do_calibration1 = 1;
77 }
78 else
79 {
80 _adc->do_calibration1 = 0;
81 }
82 // _adc->do_calibration1 = example_adc_calibration_init(_adc->adc_id, ADC_ATTEN_DB_11, &_adc->cali_handle);
83 }
84 else
85 {
86 ESP_ERROR_CHECK(adc_oneshot_del_unit(_adc->handle));
87 ESP_ERROR_CHECK(adc_cali_delete_scheme_curve_fitting(_adc->cali_handle));
88 }
89
90 return RT_EOK;
91 }
92
_adc_get_value(struct rt_adc_device * device,rt_int8_t channel,rt_uint32_t * value)93 static rt_err_t _adc_get_value(struct rt_adc_device *device, rt_int8_t channel, rt_uint32_t *value)
94 {
95 RT_ASSERT(device != RT_NULL);
96 RT_ASSERT(value != RT_NULL);
97
98 rt_uint32_t adc_raw = 0;
99 struct esp_adc *_adc = rt_container_of(device, struct esp_adc, adc_device);
100 ESP_ERROR_CHECK(adc_oneshot_read(_adc->handle, channel, &adc_raw));
101
102 if (_adc->do_calibration1)
103 {
104 ESP_ERROR_CHECK(adc_cali_raw_to_voltage(_adc->cali_handle, adc_raw, value));
105 }
106 else
107 {
108 *value = adc_raw;
109 }
110
111 return RT_EOK;
112 }
113
114 static const struct rt_adc_ops esp_adc_ops =
115 {
116 .enabled = _adc_enabled,
117 .convert = _adc_get_value,
118 .get_resolution = RT_NULL,
119 .get_vref = RT_NULL,
120 };
121
rt_hw_adc_init(void)122 int rt_hw_adc_init(void)
123 {
124 int result = RT_EOK;
125
126 for (rt_size_t i = 0; i < sizeof(esp_adc_obj) / sizeof(struct esp_adc); i++)
127 {
128 esp_adc_obj[i].adc_id = adc_config[i].adc_id;
129 /* register ADC device */
130 if (rt_hw_adc_register(&esp_adc_obj[i].adc_device, adc_config[i].device_name, &esp_adc_ops, &adc_config[i]) == RT_EOK)
131 {
132 LOG_D("%s init success", adc_config[i].device_name);
133 }
134 else
135 {
136 LOG_E("%s register failed", adc_config[i].device_name);
137 result = -RT_ERROR;
138 }
139 }
140
141 return result;
142 }
143 INIT_BOARD_EXPORT(rt_hw_adc_init);
144
145 #endif /* BSP_USING_ADC */
146