Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
2020fengziyang
rt-thread
提交
04a17d46
R
rt-thread
项目概览
2020fengziyang
/
rt-thread
与 Fork 源项目一致
Fork自
RT-Thread / rt-thread
通知
1
Star
1
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
R
rt-thread
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
未验证
提交
04a17d46
编写于
5月 28, 2022
作者:
S
Stanley Lwin
提交者:
GitHub
5月 29, 2022
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
[device][adc] implement adc_get_vref (#5988)
* add adc_get_vref add stm32_adc_get_vref
上级
ccfd2c3b
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
48 addition
and
18 deletion
+48
-18
bsp/stm32/libraries/HAL_Drivers/drv_adc.c
bsp/stm32/libraries/HAL_Drivers/drv_adc.c
+9
-1
components/drivers/include/drivers/adc.h
components/drivers/include/drivers/adc.h
+6
-4
components/drivers/misc/adc.c
components/drivers/misc/adc.c
+33
-13
未找到文件。
bsp/stm32/libraries/HAL_Drivers/drv_adc.c
浏览文件 @
04a17d46
/*
* Copyright (c) 2006-202
1
, RT-Thread Development Team
* Copyright (c) 2006-202
2
, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
...
...
@@ -10,6 +10,7 @@
* 2019-02-01 yuneizhilin fix the stm32_adc_init function initialization issue
* 2020-06-17 thread-liu Porting for stm32mp1xx
* 2020-10-14 Dozingfiretruck Porting for stm32wbxx
* 2022-05-22 Stanley Lwin Add stm32_adc_get_vref
*/
#include <board.h>
...
...
@@ -170,6 +171,12 @@ static rt_uint32_t stm32_adc_get_channel(rt_uint32_t channel)
return
stm32_channel
;
}
static
rt_int16_t
stm32_adc_get_vref
(
struct
rt_adc_device
*
device
)
{
RT_ASSERT
(
device
);
return
3300
;
}
static
rt_err_t
stm32_adc_get_value
(
struct
rt_adc_device
*
device
,
rt_uint32_t
channel
,
rt_uint32_t
*
value
)
{
ADC_ChannelConfTypeDef
ADC_ChanConf
;
...
...
@@ -285,6 +292,7 @@ static const struct rt_adc_ops stm_adc_ops =
.
enabled
=
stm32_adc_enabled
,
.
convert
=
stm32_adc_get_value
,
.
get_resolution
=
stm32_adc_get_resolution
,
.
get_vref
=
stm32_adc_get_vref
,
};
static
int
stm32_adc_init
(
void
)
...
...
components/drivers/include/drivers/adc.h
浏览文件 @
04a17d46
/*
* Copyright (c) 2006-202
1
, RT-Thread Development Team
* Copyright (c) 2006-202
2
, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
...
...
@@ -7,7 +7,7 @@
* Date Author Notes
* 2018-05-07 aozima the first version
* 2018-11-16 Ernest Chen add finsh command and update adc function
* 2022-05-11
Stanley Lwin add finsh voltage conversion command
* 2022-05-11 Stanley Lwin add finsh voltage conversion command
*/
#ifndef __ADC_H__
...
...
@@ -20,6 +20,7 @@ struct rt_adc_ops
rt_err_t
(
*
enabled
)(
struct
rt_adc_device
*
device
,
rt_uint32_t
channel
,
rt_bool_t
enabled
);
rt_err_t
(
*
convert
)(
struct
rt_adc_device
*
device
,
rt_uint32_t
channel
,
rt_uint32_t
*
value
);
rt_uint8_t
(
*
get_resolution
)(
struct
rt_adc_device
*
device
);
rt_int16_t
(
*
get_vref
)
(
const
struct
rt_adc_device
*
device
);
};
struct
rt_adc_device
...
...
@@ -33,7 +34,8 @@ typedef enum
{
RT_ADC_CMD_ENABLE
=
RT_DEVICE_CTRL_BASE
(
ADC
)
+
1
,
RT_ADC_CMD_DISABLE
=
RT_DEVICE_CTRL_BASE
(
ADC
)
+
2
,
RT_ADC_CMD_GET_RESOLUTION
=
RT_DEVICE_CTRL_BASE
(
ADC
)
+
3
,
RT_ADC_CMD_GET_RESOLUTION
=
RT_DEVICE_CTRL_BASE
(
ADC
)
+
3
,
/* get the resolution in bits */
RT_ADC_CMD_GET_VREF
=
RT_DEVICE_CTRL_BASE
(
ADC
)
+
4
,
/* get reference voltage */
}
rt_adc_cmd_t
;
rt_err_t
rt_hw_adc_register
(
rt_adc_device_t
adc
,
const
char
*
name
,
const
struct
rt_adc_ops
*
ops
,
const
void
*
user_data
);
...
...
@@ -41,6 +43,6 @@ rt_err_t rt_hw_adc_register(rt_adc_device_t adc,const char *name, const struct r
rt_uint32_t
rt_adc_read
(
rt_adc_device_t
dev
,
rt_uint32_t
channel
);
rt_err_t
rt_adc_enable
(
rt_adc_device_t
dev
,
rt_uint32_t
channel
);
rt_err_t
rt_adc_disable
(
rt_adc_device_t
dev
,
rt_uint32_t
channel
);
rt_
uint32
_t
rt_adc_voltage
(
rt_adc_device_t
dev
,
rt_uint32_t
channel
);
rt_
int16
_t
rt_adc_voltage
(
rt_adc_device_t
dev
,
rt_uint32_t
channel
);
#endif
/* __ADC_H__ */
components/drivers/misc/adc.c
浏览文件 @
04a17d46
/*
* Copyright (c) 2006-202
1
, RT-Thread Development Team
* Copyright (c) 2006-202
2
, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
...
...
@@ -17,7 +17,6 @@
#include <stdlib.h>
#define DBG_TAG "adc"
#define REFER_VOLTAGE 330
/*reference voltage, multiplied by 100 and reserve 2 decimal places for data accuracy*/
#define DBG_LVL DBG_INFO
#include <rtdbg.h>
...
...
@@ -54,7 +53,7 @@ static rt_err_t _adc_control(rt_device_t dev, int cmd, void *args)
{
result
=
adc
->
ops
->
enabled
(
adc
,
(
rt_uint32_t
)
args
,
RT_FALSE
);
}
else
if
(
cmd
==
RT_ADC_CMD_GET_RESOLUTION
&&
adc
->
ops
->
get_resolution
)
else
if
(
cmd
==
RT_ADC_CMD_GET_RESOLUTION
&&
adc
->
ops
->
get_resolution
&&
args
)
{
rt_uint8_t
resolution
=
adc
->
ops
->
get_resolution
(
adc
);
if
(
resolution
!=
0
)
...
...
@@ -64,6 +63,15 @@ static rt_err_t _adc_control(rt_device_t dev, int cmd, void *args)
result
=
RT_EOK
;
}
}
else
if
(
cmd
==
RT_ADC_CMD_GET_VREF
&&
adc
->
ops
->
get_vref
&&
args
)
{
rt_int16_t
value
=
adc
->
ops
->
get_vref
(
adc
);
if
(
value
!=
0
)
{
*
((
rt_int16_t
*
)
args
)
=
value
;
result
=
RT_EOK
;
}
}
return
result
;
}
...
...
@@ -154,28 +162,40 @@ rt_err_t rt_adc_disable(rt_adc_device_t dev, rt_uint32_t channel)
return
result
;
}
rt_
uint32
_t
rt_adc_voltage
(
rt_adc_device_t
dev
,
rt_uint32_t
channel
)
rt_
int16
_t
rt_adc_voltage
(
rt_adc_device_t
dev
,
rt_uint32_t
channel
)
{
rt_uint32_t
value
=
0
,
voltage
=
0
;
rt_uint32_t
value
=
0
;
rt_int16_t
vref
=
0
,
voltage
=
0
;
rt_uint8_t
resolution
=
0
;
RT_ASSERT
(
dev
);
/*
read the value and convert to voltage
*/
if
(
dev
->
ops
->
get_resolution
!=
RT_NULL
&&
dev
->
ops
->
convert
!=
RT_NULL
)
/*
get the resolution in bits
*/
if
(
_adc_control
((
rt_device_t
)
dev
,
RT_ADC_CMD_GET_RESOLUTION
,
&
resolution
)
!=
RT_EOK
)
{
/*get the convert bits*/
rt_uint8_t
resolution
=
dev
->
ops
->
get_resolution
(
dev
);
dev
->
ops
->
convert
(
dev
,
channel
,
&
value
);
voltage
=
value
*
REFER_VOLTAGE
/
(
1
<<
resolution
);
goto
_voltage_exit
;
}
/*get the reference voltage*/
if
(
_adc_control
((
rt_device_t
)
dev
,
RT_ADC_CMD_GET_VREF
,
&
vref
)
!=
RT_EOK
)
{
goto
_voltage_exit
;
}
/*read the value and convert to voltage*/
dev
->
ops
->
convert
(
dev
,
channel
,
&
value
);
voltage
=
value
*
vref
/
(
1
<<
resolution
);
_voltage_exit:
return
voltage
;
}
#ifdef RT_USING_FINSH
static
int
adc
(
int
argc
,
char
**
argv
)
{
int
value
=
0
,
voltage
=
0
;
int
value
=
0
;
rt_int16_t
voltage
=
0
;
rt_err_t
result
=
-
RT_ERROR
;
static
rt_adc_device_t
adc_device
=
RT_NULL
;
char
*
result_str
;
...
...
@@ -246,7 +266,7 @@ static int adc(int argc, char **argv)
{
voltage
=
rt_adc_voltage
(
adc_device
,
atoi
(
argv
[
2
]));
result_str
=
(
result
==
RT_EOK
)
?
"success"
:
"failure"
;
rt_kprintf
(
"%s channel %d voltage is %d.%0
2d
\n
"
,
adc_device
->
parent
.
parent
.
name
,
atoi
(
argv
[
2
]),
voltage
/
100
,
voltage
%
1
00
);
rt_kprintf
(
"%s channel %d voltage is %d.%0
3dV
\n
"
,
adc_device
->
parent
.
parent
.
name
,
atoi
(
argv
[
2
]),
voltage
/
1000
,
voltage
%
10
00
);
}
else
{
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录