diff --git a/bsp/bouffalo_lab/libraries/rt_drivers/Kconfig b/bsp/bouffalo_lab/libraries/rt_drivers/Kconfig index c224af264a..b82041bd7c 100755 --- a/bsp/bouffalo_lab/libraries/rt_drivers/Kconfig +++ b/bsp/bouffalo_lab/libraries/rt_drivers/Kconfig @@ -5,6 +5,11 @@ menu "General Drivers Configuration" select RT_USING_PIN default y + config BSP_USING_ADC + bool "Enable ADC" + select RT_USING_ADC + default n + menu "General Purpose UARTs" menuconfig BSP_USING_UART0 diff --git a/bsp/bouffalo_lab/libraries/rt_drivers/SConscript b/bsp/bouffalo_lab/libraries/rt_drivers/SConscript index 8c910363e5..458d3b87d1 100755 --- a/bsp/bouffalo_lab/libraries/rt_drivers/SConscript +++ b/bsp/bouffalo_lab/libraries/rt_drivers/SConscript @@ -12,6 +12,9 @@ if GetDepend('BSP_USING_GPIO'): if GetDepend('BSP_USING_I2C'): src += ['drv_i2c.c'] +if GetDepend('BSP_USING_ADC'): + src += ['drv_adc.c'] + if GetDepend('BSP_USING_RTC'): src += ['drv_rtc.c'] diff --git a/bsp/bouffalo_lab/libraries/rt_drivers/drv_adc.c b/bsp/bouffalo_lab/libraries/rt_drivers/drv_adc.c new file mode 100644 index 0000000000..8dc28cd8b8 --- /dev/null +++ b/bsp/bouffalo_lab/libraries/rt_drivers/drv_adc.c @@ -0,0 +1,113 @@ +/* + * Copyright (c) 2006-2023, RT-Thread Development Team + * + * SPDX-License-Identifier: Apache-2.0 + * + * Change Logs: + * Date Author Notes + * 2023-03-29 rose_man first version + */ +#include "board.h" +#include "drv_adc.h" + +#if defined BSP_USING_ADC + +#define DBG_TAG "drv.adc" +#define DBG_LVL DBG_INFO +#include + +#if defined(BSP_USING_BL70X) || defined(BSP_USING_BL60X) +#define ADC_GPIP_BASE ((uint32_t)0x40002000) +#elif defined(BSP_USING_BL61X) || defined(BSP_USING_BL808) +#define ADC_GPIP_BASE ((uint32_t)0x20002000) +#endif + +static struct bflb_adc_config_s adc_config = +{ + ADC_CLK_DIV_32, + false, + false, + false, + ADC_RESOLUTION_16B, + ADC_VREF_2P0V +}; + +struct bl_adc +{ + struct rt_adc_device bl_adc_device; + struct bflb_device_s *bflb_device; +}; + +static struct bl_adc bl_adc_obj; + +static rt_err_t bl_adc_enabled(struct rt_adc_device *device, rt_uint32_t channel, rt_bool_t enabled) +{ + struct bflb_device_s *bl_adc_handler; + RT_ASSERT(device != RT_NULL); + bl_adc_handler = device->parent.user_data; + struct bflb_adc_channel_s chan; + + chan.pos_chan = channel; + chan.neg_chan = ADC_CHANNEL_GND; + + bflb_adc_channel_config(bl_adc_handler, &chan, channel); + + bflb_adc_link_rxdma(bl_adc_handler, enabled); + + if (enabled) + { + bflb_adc_start_conversion(bl_adc_handler); + } + else + { + bflb_adc_stop_conversion(bl_adc_handler); + } + + return RT_EOK; +} + +static rt_err_t bl_adc_get_value(struct rt_adc_device *device, rt_uint32_t channel, rt_uint32_t *value) +{ + struct bflb_device_s *bl_adc_handler; + + RT_ASSERT(device != RT_NULL); + RT_ASSERT(value != RT_NULL); + + bl_adc_handler = device->parent.user_data; + + /* get ADC value */ + *value = (rt_uint32_t)bflb_adc_read_raw(bl_adc_handler); + + return RT_EOK; +} + +static const struct rt_adc_ops bl_adc_ops = +{ + .enabled = bl_adc_enabled, + .convert = bl_adc_get_value, +}; + +int rt_hw_adc_init(void) +{ + int result = RT_EOK; + /* ADC init */ + bl_adc_obj.bflb_device = bflb_device_get_by_name("adc"); + + bflb_adc_init(bl_adc_obj.bflb_device, &adc_config); + + /* register ADC device */ + if (rt_hw_adc_register(&bl_adc_obj.bl_adc_device, "adc", &bl_adc_ops, &bl_adc_obj.bflb_device) == RT_EOK) + { + LOG_D("adc init success"); + } + else + { + LOG_E("adc register failed"); + result = -RT_ERROR; + } + + return result; +} +INIT_BOARD_EXPORT(rt_hw_adc_init); + +#endif /* BSP_USING_ADC */ diff --git a/bsp/bouffalo_lab/libraries/rt_drivers/drv_adc.h b/bsp/bouffalo_lab/libraries/rt_drivers/drv_adc.h new file mode 100644 index 0000000000..c6b9812653 --- /dev/null +++ b/bsp/bouffalo_lab/libraries/rt_drivers/drv_adc.h @@ -0,0 +1,12 @@ +#ifndef __DRV_ADC_H__ +#define __DRV_ADC_H__ + +#include +#include "rtdevice.h" +#include +#include "bflb_adc.h" +#include "bflb_core.h" + +int rt_hw_adc_init(void); + +#endif /* __DRV_ADC_H__ */