drv_adc.c 6.6 KB
Newer Older
W
wangyq2018 已提交
1 2 3
/*
 * Copyright (C) 2018 Shanghai Eastsoft Microelectronics Co., Ltd.
 *
mysterywolf's avatar
mysterywolf 已提交
4
 * SPDX-License-Identifier: Apache-2.0
5 6 7 8 9 10 11 12 13 14 15 16
 *
 * 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
 *
 * 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.
W
wangyq2018 已提交
17 18 19 20
 *
 * Change Logs:
 * Date           Author        Notes
 * 2019-04-03     wangyq        the first version
mysterywolf's avatar
mysterywolf 已提交
21
 * 2019-11-01     wangyq        update libraries
22
 * 2021-04-20     liuhy         the second version
W
wangyq2018 已提交
23 24 25 26 27 28
 */

#include <rthw.h>
#include <rtthread.h>
#include <rtdevice.h>
#include "board.h"
mysterywolf's avatar
mysterywolf 已提交
29
#include "drv_adc.h"
W
wangyq2018 已提交
30 31 32 33

#ifdef RT_USING_ADC

/* define adc instance */
34 35

#ifdef BSP_USING_ADC0
W
wangyq2018 已提交
36
static struct rt_adc_device _device_adc0;
37
#endif /*BSP_USING_ADC0*/
mysterywolf's avatar
mysterywolf 已提交
38

39 40 41
#ifdef BSP_USING_ADC1
static struct rt_adc_device _device_adc1;
#endif /*BSP_USING_ADC1*/
W
wangyq2018 已提交
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68

/* enable or disable adc */
static rt_err_t es32f3_adc_enabled(struct rt_adc_device *device, rt_uint32_t channel, rt_bool_t enabled)
{
    adc_handle_t *_hadc = (adc_handle_t *)device->parent.user_data;

    RT_ASSERT(device != RT_NULL);

    if (enabled)
    {
        ADC_ENABLE(_hadc); ;
    }
    else
    {
        ADC_DISABLE(_hadc);
    }

    return RT_EOK;
}

static adc_channel_t es32f3_adc_get_channel(rt_uint32_t channel)
{
    adc_channel_t es32f3_channel;
    gpio_init_t gpio_initstruct;

    /* Initialize ADC pin */
    gpio_initstruct.mode = GPIO_MODE_INPUT;
mysterywolf's avatar
mysterywolf 已提交
69 70
    gpio_initstruct.pupd = GPIO_FLOATING;
    gpio_initstruct.odos = GPIO_OPEN_DRAIN;
L
liuhy 已提交
71 72
    gpio_initstruct.podrv = GPIO_OUT_DRIVE_6;
    gpio_initstruct.nodrv = GPIO_OUT_DRIVE_6;
W
wangyq2018 已提交
73 74 75 76 77 78 79 80 81
    gpio_initstruct.flt  = GPIO_FILTER_DISABLE;
    gpio_initstruct.type = GPIO_TYPE_CMOS;
    gpio_initstruct.func = GPIO_FUNC_0;

    /* select gpio pin as adc function */
    switch (channel)
    {
    case  0:
        es32f3_channel = ADC_CHANNEL_0;
82
        ald_gpio_init(ES_GPIO_ADC_CH0_GPIO, ES_GPIO_ADC_CH0_PIN, &gpio_initstruct);
W
wangyq2018 已提交
83 84 85
        break;
    case  1:
        es32f3_channel = ADC_CHANNEL_1;
86
        ald_gpio_init(ES_GPIO_ADC_CH1_GPIO, ES_GPIO_ADC_CH1_PIN, &gpio_initstruct);
W
wangyq2018 已提交
87 88 89
        break;
    case  2:
        es32f3_channel = ADC_CHANNEL_2;
90
        ald_gpio_init(ES_GPIO_ADC_CH2_GPIO, ES_GPIO_ADC_CH2_PIN, &gpio_initstruct);
W
wangyq2018 已提交
91 92 93
        break;
    case  3:
        es32f3_channel = ADC_CHANNEL_3;
94
        ald_gpio_init(ES_GPIO_ADC_CH3_GPIO, ES_GPIO_ADC_CH3_PIN, &gpio_initstruct);
W
wangyq2018 已提交
95 96 97
        break;
    case  4:
        es32f3_channel = ADC_CHANNEL_4;
98
        ald_gpio_init(ES_GPIO_ADC_CH4_GPIO, ES_GPIO_ADC_CH4_PIN, &gpio_initstruct);
W
wangyq2018 已提交
99 100 101
        break;
    case  5:
        es32f3_channel = ADC_CHANNEL_5;
102
        ald_gpio_init(ES_GPIO_ADC_CH5_GPIO, ES_GPIO_ADC_CH5_PIN, &gpio_initstruct);
W
wangyq2018 已提交
103 104 105
        break;
    case  6:
        es32f3_channel = ADC_CHANNEL_6;
106
        ald_gpio_init(ES_GPIO_ADC_CH6_GPIO, ES_GPIO_ADC_CH6_PIN, &gpio_initstruct);
W
wangyq2018 已提交
107 108 109
        break;
    case  7:
        es32f3_channel = ADC_CHANNEL_7;
110
        ald_gpio_init(ES_GPIO_ADC_CH7_GPIO, ES_GPIO_ADC_CH7_PIN, &gpio_initstruct);
W
wangyq2018 已提交
111 112 113
        break;
    case  8:
        es32f3_channel = ADC_CHANNEL_8;
114
        ald_gpio_init(ES_GPIO_ADC_CH8_GPIO, ES_GPIO_ADC_CH8_PIN, &gpio_initstruct);
W
wangyq2018 已提交
115 116 117
        break;
    case  9:
        es32f3_channel = ADC_CHANNEL_9;
118
        ald_gpio_init(ES_GPIO_ADC_CH9_GPIO, ES_GPIO_ADC_CH9_PIN, &gpio_initstruct);
W
wangyq2018 已提交
119 120 121
        break;
    case 10:
        es32f3_channel = ADC_CHANNEL_10;
122
        ald_gpio_init(ES_GPIO_ADC_CH10_GPIO, ES_GPIO_ADC_CH10_PIN, &gpio_initstruct);
W
wangyq2018 已提交
123 124 125
        break;
    case 11:
        es32f3_channel = ADC_CHANNEL_11;
126
        ald_gpio_init(ES_GPIO_ADC_CH11_GPIO, ES_GPIO_ADC_CH11_PIN, &gpio_initstruct);
W
wangyq2018 已提交
127 128 129
        break;
    case 12:
        es32f3_channel = ADC_CHANNEL_12;
130
        ald_gpio_init(ES_GPIO_ADC_CH12_GPIO, ES_GPIO_ADC_CH12_PIN, &gpio_initstruct);
W
wangyq2018 已提交
131 132 133
        break;
    case 13:
        es32f3_channel = ADC_CHANNEL_13;
134
        ald_gpio_init(ES_GPIO_ADC_CH13_GPIO, ES_GPIO_ADC_CH13_PIN, &gpio_initstruct);
W
wangyq2018 已提交
135 136 137
        break;
    case 14:
        es32f3_channel = ADC_CHANNEL_14;
138
        ald_gpio_init(ES_GPIO_ADC_CH14_GPIO, ES_GPIO_ADC_CH14_PIN, &gpio_initstruct);
W
wangyq2018 已提交
139 140 141
        break;
    case 15:
        es32f3_channel = ADC_CHANNEL_15;
142
        ald_gpio_init(ES_GPIO_ADC_CH15_GPIO, ES_GPIO_ADC_CH15_PIN, &gpio_initstruct);
W
wangyq2018 已提交
143
        break;
mysterywolf's avatar
mysterywolf 已提交
144

W
wangyq2018 已提交
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
    default:
        break;
    }

    return es32f3_channel;
}

static rt_err_t es32f3_get_adc_value(struct rt_adc_device *device, rt_uint32_t channel, rt_uint32_t *value)
{
    adc_handle_t *_hadc = (adc_handle_t *)device->parent.user_data;
    adc_nch_conf_t nm_config;

    RT_ASSERT(device != RT_NULL);
    RT_ASSERT(value != RT_NULL);

    /* config adc channel */
W
wangyq2018 已提交
161 162
    nm_config.ch       = es32f3_adc_get_channel(channel);
    nm_config.idx          = ADC_NCH_IDX_1;
163
    nm_config.samp = ES_ADC0_NCH_SAMPLETIME;
W
wangyq2018 已提交
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
    ald_adc_normal_channel_config(_hadc, &nm_config);

    ald_adc_normal_start(_hadc);

    if (ald_adc_normal_poll_for_conversion(_hadc, 5000) == OK)
        *value = ald_adc_normal_get_value(_hadc);

    return RT_EOK;
}

static const struct rt_adc_ops es32f3_adc_ops =
{
    es32f3_adc_enabled,
    es32f3_get_adc_value,
};

int rt_hw_adc_init(void)
{
    int result = RT_EOK;
183
    adc_handle_t _h_adc;
W
wangyq2018 已提交
184 185

    /* adc function initialization */
186 187 188 189 190 191 192 193
    _h_adc.init.scan = DISABLE;
    _h_adc.init.cont = DISABLE;
    _h_adc.init.disc = ADC_ALL_DISABLE;
    _h_adc.init.disc_nr = ADC_DISC_NR_1;
    _h_adc.init.nch_nr = ADC_NCH_NR_16;
    _h_adc.init.nche_sel = ADC_NCHESEL_MODE_ALL;
    _h_adc.init.cont = DISABLE;
    _h_adc.init.n_ref = ADC_NEG_REF_VSS;
mysterywolf's avatar
mysterywolf 已提交
194
    _h_adc.init.p_ref = ADC_POS_REF_VDD;
195 196

#ifdef BSP_USING_ADC0
mysterywolf's avatar
mysterywolf 已提交
197

198
    static adc_handle_t _h_adc0;
mysterywolf's avatar
mysterywolf 已提交
199

200
    _h_adc0.init = _h_adc.init;
mysterywolf's avatar
mysterywolf 已提交
201

W
wangyq2018 已提交
202
    _h_adc0.perh = ADC0;
203 204 205
    _h_adc0.init.align = ES_ADC0_ALIGN;
    _h_adc0.init.data_bit = ES_ADC0_DATA_BIT;
    _h_adc0.init.div = ES_ADC0_CLK_DIV;
W
wangyq2018 已提交
206
    ald_adc_init(&_h_adc0);
mysterywolf's avatar
mysterywolf 已提交
207

208
    rt_hw_adc_register(&_device_adc0, ES_DEVICE_NAME_ADC0, &es32f3_adc_ops, &_h_adc0);
mysterywolf's avatar
mysterywolf 已提交
209

210
#endif /*BSP_USING_ADC0*/
mysterywolf's avatar
mysterywolf 已提交
211 212

#ifdef BSP_USING_ADC1
213 214

    static adc_handle_t _h_adc1;
mysterywolf's avatar
mysterywolf 已提交
215

216
    _h_adc1.init = _h_adc.init;
mysterywolf's avatar
mysterywolf 已提交
217

218 219 220 221 222
    _h_adc1.perh = ADC1;
    _h_adc1.init.align = ES_ADC1_ALIGN;
    _h_adc1.init.data_bit = ES_ADC1_DATA_BIT;
    _h_adc1.init.div = ES_ADC1_CLK_DIV;
    ald_adc_init(&_h_adc1);
mysterywolf's avatar
mysterywolf 已提交
223

224
    rt_hw_adc_register(&_device_adc1, ES_DEVICE_NAME_ADC1, &es32f3_adc_ops, &_h_adc1);
mysterywolf's avatar
mysterywolf 已提交
225

226
#endif /*BSP_USING_ADC1*/
mysterywolf's avatar
mysterywolf 已提交
227

W
wangyq2018 已提交
228 229 230 231 232 233

    return result;
}
INIT_BOARD_EXPORT(rt_hw_adc_init);

#endif