mirror of
https://github.com/RT-Thread/rt-thread.git
synced 2025-01-16 00:49:31 +08:00
7761923ec5
Squashed commit of the following: commit 32dd349ccff85cdcad81021b5185f8f7347786db Author: Huaqi Fang <578567190@qq.com> Date: Thu Jun 11 16:08:08 2020 +0800 bsp/nuclei: Add more documentation about how to use RT-Thread in RV-STAR Signed-off-by: Huaqi Fang <578567190@qq.com> commit c9474c20a558e9d7934a3d72cc9bb17a98c20871 Author: Huaqi Fang <578567190@qq.com> Date: Thu Jun 11 14:06:42 2020 +0800 bsp/nuclei: remove comments in rt_hw_*_drvinit Signed-off-by: Huaqi Fang <578567190@qq.com> commit 89b5caa2c3e55dc352b13f8e33229f03351b7049 Author: Huaqi Fang <578567190@qq.com> Date: Thu Jun 11 13:52:12 2020 +0800 bsp/nuclei: Add a entry README.md for nuclei bsp Signed-off-by: Huaqi Fang <578567190@qq.com> commit ae50d6e9bdc92ca4767c5ac7d0ded7252314a42e Author: Huaqi Fang <578567190@qq.com> Date: Thu Jun 11 11:52:52 2020 +0800 bsp/nuclei: update README.md cover more pinmux section Signed-off-by: Huaqi Fang <578567190@qq.com> commit 1a42d85dfe5bc7b009f057784249d54d0c82de0f Author: Huaqi Fang <578567190@qq.com> Date: Thu Jun 11 08:52:06 2020 +0800 bsp/nuclei: clean up unused code in drv_spi.c Signed-off-by: Huaqi Fang <578567190@qq.com> commit b40edcdf59c96ef261f65a0c023a24ac78cabb39 Author: Huaqi Fang <578567190@qq.com> Date: Wed Jun 10 14:09:15 2020 +0800 bsp/nuclei: Format the code comments for hw pinmux init Signed-off-by: Huaqi Fang <578567190@qq.com> commit c8ae9fdfdb989bbff85d911fee3124abd9d145db Author: Huaqi Fang <578567190@qq.com> Date: Mon Jun 8 17:12:43 2020 +0800 bsp/nuclei: Add more drivers support for gd32vf103 * More drivers are supported for rvstar board, see README.md * If user want to use these supported drivers, menuconfig is required * User also need to setup pinmux for its coresponding driver in board/board.c Signed-off-by: Huaqi Fang <578567190@qq.com> Signed-off-by: Huaqi Fang <578567190@qq.com>
138 lines
3.5 KiB
C
138 lines
3.5 KiB
C
/*
|
|
* Copyright (c) 2006-2020, RT-Thread Development Team
|
|
*
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
*
|
|
* Change Logs:
|
|
* Date Author Notes
|
|
* 2020-06-03 hqfang the first version.
|
|
*
|
|
*/
|
|
#include "drv_adc.h"
|
|
|
|
#ifdef BSP_USING_ADC
|
|
|
|
#if !defined(BSP_USING_ADC0) && !defined(BSP_USING_ADC1)
|
|
#error "Please define at least one BSP_USING_ADCx"
|
|
/* this driver can be disabled at menuconfig -> Hardware Drivers Config -> On-chip Peripheral Drivers -> Enable ADC */
|
|
#endif
|
|
|
|
|
|
static struct gd32_adc_config adc_config[] =
|
|
{
|
|
#ifdef BSP_USING_ADC0
|
|
{
|
|
"adc0",
|
|
ADC0,
|
|
},
|
|
#endif
|
|
#ifdef BSP_USING_ADC1
|
|
{
|
|
"adc1",
|
|
ADC1,
|
|
},
|
|
#endif
|
|
};
|
|
|
|
static struct gd32_adc adc_obj[sizeof(adc_config) / sizeof(adc_config[0])] = {0};
|
|
|
|
static void gd32_adc_init(struct gd32_adc_config *config)
|
|
{
|
|
RT_ASSERT(config != RT_NULL);
|
|
adc_deinit(config->adc_periph);
|
|
|
|
ADC_CTL0(config->adc_periph) &= ~(ADC_CTL0_SYNCM);
|
|
ADC_CTL0(config->adc_periph) |= ADC_MODE_FREE;
|
|
|
|
ADC_CTL1(config->adc_periph) |= ADC_CTL1_TSVREN;
|
|
|
|
adc_resolution_config(config->adc_periph, ADC_RESOLUTION_12B);
|
|
/* ADC contineous function enable */
|
|
adc_special_function_config(config->adc_periph, ADC_SCAN_MODE, ENABLE);
|
|
/* ADC data alignment config */
|
|
adc_data_alignment_config(config->adc_periph, ADC_DATAALIGN_RIGHT);
|
|
/* ADC channel length config */
|
|
adc_channel_length_config(config->adc_periph, ADC_REGULAR_CHANNEL, 1);
|
|
adc_external_trigger_source_config(config->adc_periph, ADC_REGULAR_CHANNEL, ADC0_1_EXTTRIG_REGULAR_NONE);
|
|
|
|
/* ADC enable */
|
|
adc_external_trigger_config(config->adc_periph, ADC_REGULAR_CHANNEL, ENABLE);
|
|
adc_enable(config->adc_periph);
|
|
|
|
adc_calibration_enable(config->adc_periph);
|
|
}
|
|
|
|
static rt_err_t gd32_adc_enabled(struct rt_adc_device *device, rt_uint32_t channel, rt_bool_t enabled)
|
|
{
|
|
if (channel > ADC_CHANNEL_17)
|
|
{
|
|
return RT_EINVAL;
|
|
}
|
|
return RT_EOK;
|
|
}
|
|
|
|
static rt_err_t gd32_adc_convert(struct rt_adc_device *device, rt_uint32_t channel, rt_uint32_t *value)
|
|
{
|
|
struct gd32_adc_config *config;
|
|
RT_ASSERT(device != RT_NULL);
|
|
|
|
if (channel > ADC_CHANNEL_17)
|
|
{
|
|
return RT_EINVAL;
|
|
}
|
|
config = (struct gd32_adc_config *)(device->parent.user_data);
|
|
|
|
if (channel > ADC_CHANNEL_15)
|
|
{
|
|
adc_regular_channel_config(config->adc_periph, 0, channel, ADC_SAMPLETIME_239POINT5);
|
|
}
|
|
else
|
|
{
|
|
adc_regular_channel_config(config->adc_periph, 0, channel, ADC_SAMPLETIME_55POINT5);
|
|
}
|
|
adc_software_trigger_enable(config->adc_periph, ADC_REGULAR_CHANNEL);
|
|
|
|
while (SET != adc_flag_get(config->adc_periph, ADC_FLAG_EOC));
|
|
adc_flag_clear(config->adc_periph, ADC_FLAG_EOC);
|
|
|
|
*value = ADC_RDATA(config->adc_periph);
|
|
|
|
return RT_EOK;
|
|
}
|
|
|
|
static struct rt_adc_ops gd32_adc_ops =
|
|
{
|
|
.enabled = gd32_adc_enabled,
|
|
.convert = gd32_adc_convert,
|
|
};
|
|
|
|
int rt_hw_adc_init(void)
|
|
{
|
|
int i = 0;
|
|
int result = RT_EOK;
|
|
|
|
#if defined(BSP_USING_ADC0)
|
|
rcu_periph_clock_enable(RCU_ADC0);
|
|
#endif
|
|
#if defined(BSP_USING_ADC1)
|
|
rcu_periph_clock_enable(RCU_ADC1);
|
|
#endif
|
|
|
|
rcu_adc_clock_config(RCU_CKADC_CKAPB2_DIV8);
|
|
|
|
for (i = 0; i < sizeof(adc_obj) / sizeof(adc_obj[0]); i++)
|
|
{
|
|
adc_obj[i].config = &adc_config[i];
|
|
gd32_adc_init(&adc_config[i]);
|
|
rt_hw_adc_register(&adc_obj[i].adc_device, \
|
|
adc_obj[i].config->name, &gd32_adc_ops, adc_obj[i].config);
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
INIT_DEVICE_EXPORT(rt_hw_adc_init);
|
|
|
|
#endif /* BSP_USING_ADC */
|
|
|