Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[bsp][bouffalo_lab]add drv_adc #7125

Merged
merged 3 commits into from
Mar 31, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions bsp/bouffalo_lab/libraries/rt_drivers/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ menu "General Drivers Configuration"
select RT_USING_PIN
default y

menuconfig BSP_USING_ADC
Z8MAN8 marked this conversation as resolved.
Show resolved Hide resolved
bool "Enable ADC"
select RT_USING_ADC
default n

menu "General Purpose UARTs"

menuconfig BSP_USING_UART0
Expand Down
3 changes: 3 additions & 0 deletions bsp/bouffalo_lab/libraries/rt_drivers/SConscript
Original file line number Diff line number Diff line change
Expand Up @@ -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']

group = DefineGroup('rt_drivers', src, depend = [''], CPPPATH = CPPPATH)

objs = [group]
Expand Down
113 changes: 113 additions & 0 deletions bsp/bouffalo_lab/libraries/rt_drivers/drv_adc.c
Original file line number Diff line number Diff line change
@@ -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 <rtdbg.h>

#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,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

请看一下rt_adc_ops 结构体,以及stm32驱动的实现,还有几个方法没有实现,建议实现一下

};

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 */
12 changes: 12 additions & 0 deletions bsp/bouffalo_lab/libraries/rt_drivers/drv_adc.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#ifndef __DRV_ADC_H__
#define __DRV_ADC_H__

#include <rtthread.h>
#include "rtdevice.h"
#include <rthw.h>
#include "bflb_adc.h"
#include "bflb_core.h"

int rt_hw_adc_init(void);

#endif /* __DRV_ADC_H__ */