Skip to content

Commit 3c2169e

Browse files
[bsp][bouffalo_lab]add drv_adc (#7125)
* [bsp][bouffalo_lab]add drv_adc * Update bsp/bouffalo_lab/libraries/rt_drivers/Kconfig Co-authored-by: Man, Jianting (Meco) <920369182@qq.com> --------- Co-authored-by: Man, Jianting (Meco) <920369182@qq.com>
1 parent fb09316 commit 3c2169e

4 files changed

Lines changed: 133 additions & 0 deletions

File tree

bsp/bouffalo_lab/libraries/rt_drivers/Kconfig

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@ menu "General Drivers Configuration"
55
select RT_USING_PIN
66
default y
77

8+
config BSP_USING_ADC
9+
bool "Enable ADC"
10+
select RT_USING_ADC
11+
default n
12+
813
menu "General Purpose UARTs"
914

1015
menuconfig BSP_USING_UART0

bsp/bouffalo_lab/libraries/rt_drivers/SConscript

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ if GetDepend('BSP_USING_GPIO'):
1212
if GetDepend('BSP_USING_I2C'):
1313
src += ['drv_i2c.c']
1414

15+
if GetDepend('BSP_USING_ADC'):
16+
src += ['drv_adc.c']
17+
1518
if GetDepend('BSP_USING_RTC'):
1619
src += ['drv_rtc.c']
1720

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
/*
2+
* Copyright (c) 2006-2023, RT-Thread Development Team
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*
6+
* Change Logs:
7+
* Date Author Notes
8+
* 2023-03-29 rose_man first version
9+
*/
10+
#include "board.h"
11+
#include "drv_adc.h"
12+
13+
#if defined BSP_USING_ADC
14+
15+
#define DBG_TAG "drv.adc"
16+
#define DBG_LVL DBG_INFO
17+
#include <rtdbg.h>
18+
19+
#if defined(BSP_USING_BL70X) || defined(BSP_USING_BL60X)
20+
#define ADC_GPIP_BASE ((uint32_t)0x40002000)
21+
#elif defined(BSP_USING_BL61X) || defined(BSP_USING_BL808)
22+
#define ADC_GPIP_BASE ((uint32_t)0x20002000)
23+
#endif
24+
25+
static struct bflb_adc_config_s adc_config =
26+
{
27+
ADC_CLK_DIV_32,
28+
false,
29+
false,
30+
false,
31+
ADC_RESOLUTION_16B,
32+
ADC_VREF_2P0V
33+
};
34+
35+
struct bl_adc
36+
{
37+
struct rt_adc_device bl_adc_device;
38+
struct bflb_device_s *bflb_device;
39+
};
40+
41+
static struct bl_adc bl_adc_obj;
42+
43+
static rt_err_t bl_adc_enabled(struct rt_adc_device *device, rt_uint32_t channel, rt_bool_t enabled)
44+
{
45+
struct bflb_device_s *bl_adc_handler;
46+
RT_ASSERT(device != RT_NULL);
47+
bl_adc_handler = device->parent.user_data;
48+
struct bflb_adc_channel_s chan;
49+
50+
chan.pos_chan = channel;
51+
chan.neg_chan = ADC_CHANNEL_GND;
52+
53+
bflb_adc_channel_config(bl_adc_handler, &chan, channel);
54+
55+
bflb_adc_link_rxdma(bl_adc_handler, enabled);
56+
57+
if (enabled)
58+
{
59+
bflb_adc_start_conversion(bl_adc_handler);
60+
}
61+
else
62+
{
63+
bflb_adc_stop_conversion(bl_adc_handler);
64+
}
65+
66+
return RT_EOK;
67+
}
68+
69+
static rt_err_t bl_adc_get_value(struct rt_adc_device *device, rt_uint32_t channel, rt_uint32_t *value)
70+
{
71+
struct bflb_device_s *bl_adc_handler;
72+
73+
RT_ASSERT(device != RT_NULL);
74+
RT_ASSERT(value != RT_NULL);
75+
76+
bl_adc_handler = device->parent.user_data;
77+
78+
/* get ADC value */
79+
*value = (rt_uint32_t)bflb_adc_read_raw(bl_adc_handler);
80+
81+
return RT_EOK;
82+
}
83+
84+
static const struct rt_adc_ops bl_adc_ops =
85+
{
86+
.enabled = bl_adc_enabled,
87+
.convert = bl_adc_get_value,
88+
};
89+
90+
int rt_hw_adc_init(void)
91+
{
92+
int result = RT_EOK;
93+
/* ADC init */
94+
bl_adc_obj.bflb_device = bflb_device_get_by_name("adc");
95+
96+
bflb_adc_init(bl_adc_obj.bflb_device, &adc_config);
97+
98+
/* register ADC device */
99+
if (rt_hw_adc_register(&bl_adc_obj.bl_adc_device, "adc", &bl_adc_ops, &bl_adc_obj.bflb_device) == RT_EOK)
100+
{
101+
LOG_D("adc init success");
102+
}
103+
else
104+
{
105+
LOG_E("adc register failed");
106+
result = -RT_ERROR;
107+
}
108+
109+
return result;
110+
}
111+
INIT_BOARD_EXPORT(rt_hw_adc_init);
112+
113+
#endif /* BSP_USING_ADC */
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#ifndef __DRV_ADC_H__
2+
#define __DRV_ADC_H__
3+
4+
#include <rtthread.h>
5+
#include "rtdevice.h"
6+
#include <rthw.h>
7+
#include "bflb_adc.h"
8+
#include "bflb_core.h"
9+
10+
int rt_hw_adc_init(void);
11+
12+
#endif /* __DRV_ADC_H__ */

0 commit comments

Comments
 (0)