1 /*
2 * Copyright (C) 2018 Shanghai Eastsoft Microelectronics Co., Ltd.
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 *
6 * Licensed under the Apache License, Version 2.0 (the License); you may
7 * not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an AS IS BASIS, WITHOUT
14 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 *
18 * Change Logs:
19 * Date Author Notes
20 * 2019-04-03 wangyq the first version
21 * 2019-11-01 wangyq update libraries
22 * 2021-04-20 liuhy the second version
23 */
24
25 #include <rthw.h>
26 #include <rtthread.h>
27 #include <rtdevice.h>
28 #include "board.h"
29 #include "drv_adc.h"
30 #include <ald_gpio.h>
31 #include <ald_adc.h>
32
33 #ifdef RT_USING_ADC
34
35 /* define adc instance */
36 static struct rt_adc_device _device_adc0;
37
38 /* enable or disable adc */
es32f0_adc_enabled(struct rt_adc_device * device,rt_uint32_t channel,rt_bool_t enabled)39 static rt_err_t es32f0_adc_enabled(struct rt_adc_device *device, rt_uint32_t channel, rt_bool_t enabled)
40 {
41 adc_handle_t *_hadc = (adc_handle_t *)device->parent.user_data;
42
43 RT_ASSERT(device != RT_NULL);
44
45 if (enabled)
46 {
47 ADC_ENABLE(_hadc); ;
48 }
49 else
50 {
51 ADC_DISABLE(_hadc);
52 }
53
54 return RT_EOK;
55 }
56
es32f0_adc_get_channel(rt_uint32_t channel)57 static adc_channel_t es32f0_adc_get_channel(rt_uint32_t channel)
58 {
59 adc_channel_t es32f0_channel;
60 gpio_init_t gpio_initstruct;
61
62 /* Initialize ADC pin */
63 gpio_initstruct.mode = GPIO_MODE_INPUT;
64 gpio_initstruct.pupd = GPIO_FLOATING;
65 gpio_initstruct.odrv = GPIO_OUT_DRIVE_NORMAL;
66 gpio_initstruct.flt = GPIO_FILTER_DISABLE;
67 gpio_initstruct.type = GPIO_TYPE_CMOS;
68 gpio_initstruct.func = GPIO_FUNC_0;
69
70 /* select gpio pin as adc function */
71 switch (channel)
72 {
73 case 0:
74 es32f0_channel = ADC_CHANNEL_0;
75 ald_gpio_init(ES_GPIO_ADC_CH0_GPIO, ES_GPIO_ADC_CH0_PIN, &gpio_initstruct);
76 break;
77 case 1:
78 es32f0_channel = ADC_CHANNEL_1;
79 ald_gpio_init(ES_GPIO_ADC_CH1_GPIO, ES_GPIO_ADC_CH1_PIN, &gpio_initstruct);
80 break;
81 case 2:
82 es32f0_channel = ADC_CHANNEL_2;
83 ald_gpio_init(ES_GPIO_ADC_CH2_GPIO, ES_GPIO_ADC_CH2_PIN, &gpio_initstruct);
84 break;
85 case 3:
86 es32f0_channel = ADC_CHANNEL_3;
87 ald_gpio_init(ES_GPIO_ADC_CH3_GPIO, ES_GPIO_ADC_CH3_PIN, &gpio_initstruct);
88 break;
89 case 4:
90 es32f0_channel = ADC_CHANNEL_4;
91 ald_gpio_init(ES_GPIO_ADC_CH4_GPIO, ES_GPIO_ADC_CH4_PIN, &gpio_initstruct);
92 break;
93 case 5:
94 es32f0_channel = ADC_CHANNEL_5;
95 ald_gpio_init(ES_GPIO_ADC_CH5_GPIO, ES_GPIO_ADC_CH5_PIN, &gpio_initstruct);
96 break;
97 case 6:
98 es32f0_channel = ADC_CHANNEL_6;
99 ald_gpio_init(ES_GPIO_ADC_CH6_GPIO, ES_GPIO_ADC_CH6_PIN, &gpio_initstruct);
100 break;
101 case 7:
102 es32f0_channel = ADC_CHANNEL_7;
103 ald_gpio_init(ES_GPIO_ADC_CH7_GPIO, ES_GPIO_ADC_CH7_PIN, &gpio_initstruct);
104 break;
105 case 8:
106 es32f0_channel = ADC_CHANNEL_8;
107 ald_gpio_init(ES_GPIO_ADC_CH8_GPIO, ES_GPIO_ADC_CH8_PIN, &gpio_initstruct);
108 break;
109 case 9:
110 es32f0_channel = ADC_CHANNEL_9;
111 ald_gpio_init(ES_GPIO_ADC_CH9_GPIO, ES_GPIO_ADC_CH9_PIN, &gpio_initstruct);
112 break;
113 case 10:
114 es32f0_channel = ADC_CHANNEL_10;
115 ald_gpio_init(ES_GPIO_ADC_CH10_GPIO, ES_GPIO_ADC_CH10_PIN, &gpio_initstruct);
116 break;
117 case 11:
118 es32f0_channel = ADC_CHANNEL_11;
119 ald_gpio_init(ES_GPIO_ADC_CH11_GPIO, ES_GPIO_ADC_CH11_PIN, &gpio_initstruct);
120 break;
121 case 12:
122 es32f0_channel = ADC_CHANNEL_12;
123 ald_gpio_init(ES_GPIO_ADC_CH12_GPIO, ES_GPIO_ADC_CH12_PIN, &gpio_initstruct);
124 break;
125 case 13:
126 es32f0_channel = ADC_CHANNEL_13;
127 ald_gpio_init(ES_GPIO_ADC_CH13_GPIO, ES_GPIO_ADC_CH13_PIN, &gpio_initstruct);
128 break;
129 case 14:
130 es32f0_channel = ADC_CHANNEL_14;
131 ald_gpio_init(ES_GPIO_ADC_CH14_GPIO, ES_GPIO_ADC_CH14_PIN, &gpio_initstruct);
132 break;
133 case 15:
134 es32f0_channel = ADC_CHANNEL_15;
135 ald_gpio_init(ES_GPIO_ADC_CH15_GPIO, ES_GPIO_ADC_CH15_PIN, &gpio_initstruct);
136 break;
137
138 default:
139 break;
140 }
141
142 return es32f0_channel;
143 }
144
es32f0_get_adc_value(struct rt_adc_device * device,rt_uint32_t channel,rt_uint32_t * value)145 static rt_err_t es32f0_get_adc_value(struct rt_adc_device *device, rt_uint32_t channel, rt_uint32_t *value)
146 {
147 adc_handle_t *_hadc = (adc_handle_t *)device->parent.user_data;
148 adc_nch_conf_t nm_config;
149
150 RT_ASSERT(device != RT_NULL);
151 RT_ASSERT(value != RT_NULL);
152
153 /* config adc channel */
154 nm_config.ch = es32f0_adc_get_channel(channel);
155 nm_config.idx = ADC_NCH_IDX_1;
156
157 /*aaabbbccc*/
158 nm_config.samp = ES_ADC0_NCH_SAMPLETIME;
159 nm_config.samp = ADC_SAMPLETIME_4;
160 ald_adc_normal_channel_config(_hadc, &nm_config);
161
162 ald_adc_normal_start(_hadc);
163
164 if (ald_adc_normal_poll_for_conversion(_hadc, 5000) == OK)
165 *value = ald_adc_normal_get_value(_hadc);
166
167 return RT_EOK;
168 }
169
170 static const struct rt_adc_ops es32f0_adc_ops =
171 {
172 es32f0_adc_enabled,
173 es32f0_get_adc_value,
174 };
175
rt_hw_adc_init(void)176 int rt_hw_adc_init(void)
177 {
178 int result = RT_EOK;
179
180 adc_handle_t _h_adc;
181
182 _h_adc.init.scan = DISABLE;
183 _h_adc.init.cont = DISABLE;
184 _h_adc.init.disc = ADC_ALL_DISABLE;
185 _h_adc.init.disc_nr = ADC_DISC_NR_1;
186 _h_adc.init.nche_sel = ADC_NCHESEL_MODE_ALL;
187 _h_adc.init.n_ref = ADC_NEG_REF_VSS;
188 _h_adc.init.p_ref = ADC_POS_REF_VDD;
189 _h_adc.init.nch_nr = ADC_NCH_NR_16;
190
191 #ifdef BSP_USING_ADC0
192
193 static adc_handle_t _h_adc0;
194
195 _h_adc0.init = _h_adc.init;
196
197 _h_adc0.perh = ADC0;
198 _h_adc0.init.align = ES_ADC0_ALIGN;
199 _h_adc0.init.data_bit = ES_ADC0_DATA_BIT;
200 _h_adc0.init.div = ES_ADC0_CLK_DIV;
201 ald_adc_init(&_h_adc0);
202
203 result = rt_hw_adc_register(&_device_adc0, ES_DEVICE_NAME_ADC0, &es32f0_adc_ops, &_h_adc0);
204
205 #endif /*BSP_USING_ADC0*/
206
207 return result;
208 }
209 INIT_BOARD_EXPORT(rt_hw_adc_init);
210
211 #endif
212