/* * Copyright (C) 2018 Shanghai Eastsoft Microelectronics Co., Ltd. * * SPDX-License-Identifier: Apache-2.0 * * Change Logs: * Date Author Notes * 2019-10-23 yuzrain the first version */ #include #include #include #include "board.h" #include "drv_adc.h" #include "md_gpio.h" #include "md_adc.h" #include "md_rcu.h" #include "md_syscfg.h" #ifdef RT_USING_ADC #define BSP_ADC_CHANNEL_NUM 8 /* define adc instance */ static struct rt_adc_device _device_adc0; /* enable or disable adc */ static rt_err_t es32f0_adc_enabled(struct rt_adc_device *device, rt_uint32_t channel, rt_bool_t enabled) { RT_ASSERT(device != RT_NULL); if (enabled) { md_adc_enable_ssen_ss0en(ADC); } else { md_adc_disable_ssen_ss0en(ADC); } return RT_EOK; } static void _adc_channel_config(rt_uint32_t channel) { /* select gpio pin as adc function */ switch (channel) { case 0: md_gpio_set_mode(GPIOA, MD_GPIO_PIN_0, MD_GPIO_MODE_ANALOG); break; case 1: md_gpio_set_mode(GPIOA, MD_GPIO_PIN_1, MD_GPIO_MODE_ANALOG); break; case 2: md_gpio_set_mode(GPIOA, MD_GPIO_PIN_2, MD_GPIO_MODE_ANALOG); break; case 3: md_gpio_set_mode(GPIOA, MD_GPIO_PIN_3, MD_GPIO_MODE_ANALOG); break; case 4: md_gpio_set_mode(GPIOA, MD_GPIO_PIN_4, MD_GPIO_MODE_ANALOG); break; case 5: md_gpio_set_mode(GPIOA, MD_GPIO_PIN_5, MD_GPIO_MODE_ANALOG); break; case 6: md_gpio_set_mode(GPIOA, MD_GPIO_PIN_6, MD_GPIO_MODE_ANALOG); break; case 7: md_gpio_set_mode(GPIOA, MD_GPIO_PIN_7, MD_GPIO_MODE_ANALOG); break; default: break; } } static rt_err_t es32f0_get_adc_value(struct rt_adc_device *device, rt_uint32_t channel, rt_uint32_t *value) { rt_uint32_t chn_data[BSP_ADC_CHANNEL_NUM]; rt_uint32_t i; RT_ASSERT(device != RT_NULL); RT_ASSERT(value != RT_NULL); /* config adc channel */ _adc_channel_config(channel); md_adc_set_swtri_ss0(ADC); while ((ADC->RIF & ADC_RIF_SS0RIF_MSK) == 0); for (i=0; i