Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
2301_77017786
rt-thread
提交
c8956357
R
rt-thread
项目概览
2301_77017786
/
rt-thread
与 Fork 源项目一致
Fork自
RT-Thread / rt-thread
通知
1
Star
0
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,发现更多精彩内容 >>
提交
c8956357
编写于
4月 08, 2023
作者:
mysterywolf
提交者:
mysterywolf
4月 08, 2023
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
[stm32][adc] 优化adc通道获取流程
上级
505023b6
变更
1
显示空白变更内容
内联
并排
Showing
1 changed file
with
100 addition
and
163 deletion
+100
-163
bsp/stm32/libraries/HAL_Drivers/drv_adc.c
bsp/stm32/libraries/HAL_Drivers/drv_adc.c
+100
-163
未找到文件。
bsp/stm32/libraries/HAL_Drivers/drv_adc.c
浏览文件 @
c8956357
...
@@ -45,9 +45,100 @@ struct stm32_adc
...
@@ -45,9 +45,100 @@ struct stm32_adc
};
};
static
struct
stm32_adc
stm32_adc_obj
[
sizeof
(
adc_config
)
/
sizeof
(
adc_config
[
0
])];
static
struct
stm32_adc
stm32_adc_obj
[
sizeof
(
adc_config
)
/
sizeof
(
adc_config
[
0
])];
static
rt_uint32_t
stm32_adc_get_channel
(
rt_uint32_t
channel
);
static
rt_err_t
stm32_adc_enabled
(
struct
rt_adc_device
*
device
,
rt_uint32_t
channel
,
rt_bool_t
enabled
)
static
rt_err_t
stm32_adc_get_channel
(
rt_int8_t
rt_channel
,
uint32_t
*
stm32_channel
)
{
switch
(
rt_channel
)
{
case
0
:
*
stm32_channel
=
ADC_CHANNEL_0
;
break
;
case
1
:
*
stm32_channel
=
ADC_CHANNEL_1
;
break
;
case
2
:
*
stm32_channel
=
ADC_CHANNEL_2
;
break
;
case
3
:
*
stm32_channel
=
ADC_CHANNEL_3
;
break
;
case
4
:
*
stm32_channel
=
ADC_CHANNEL_4
;
break
;
case
5
:
*
stm32_channel
=
ADC_CHANNEL_5
;
break
;
case
6
:
*
stm32_channel
=
ADC_CHANNEL_6
;
break
;
case
7
:
*
stm32_channel
=
ADC_CHANNEL_7
;
break
;
case
8
:
*
stm32_channel
=
ADC_CHANNEL_8
;
break
;
case
9
:
*
stm32_channel
=
ADC_CHANNEL_9
;
break
;
case
10
:
*
stm32_channel
=
ADC_CHANNEL_10
;
break
;
case
11
:
*
stm32_channel
=
ADC_CHANNEL_11
;
break
;
case
12
:
*
stm32_channel
=
ADC_CHANNEL_12
;
break
;
case
13
:
*
stm32_channel
=
ADC_CHANNEL_13
;
break
;
case
14
:
*
stm32_channel
=
ADC_CHANNEL_14
;
break
;
case
15
:
*
stm32_channel
=
ADC_CHANNEL_15
;
break
;
#ifdef ADC_CHANNEL_16
case
16
:
*
stm32_channel
=
ADC_CHANNEL_16
;
break
;
#endif
/* ADC_CHANNEL_16 */
case
17
:
*
stm32_channel
=
ADC_CHANNEL_17
;
break
;
#ifdef ADC_CHANNEL_18
case
18
:
*
stm32_channel
=
ADC_CHANNEL_18
;
break
;
#endif
/* ADC_CHANNEL_18 */
#ifdef ADC_CHANNEL_19
case
19
:
*
stm32_channel
=
ADC_CHANNEL_19
;
break
;
#endif
/* ADC_CHANNEL_19 */
#ifdef ADC_CHANNEL_VREFINT
case
RT_ADC_INTERN_CH_VREF
:
*
stm32_channel
=
ADC_CHANNEL_VREFINT
;
break
;
#endif
/* ADC_CHANNEL_VREFINT */
#ifdef ADC_CHANNEL_VBAT
case
RT_ADC_INTERN_CH_VBAT
:
*
stm32_channel
=
ADC_CHANNEL_VBAT
;
break
;
#endif
/* ADC_CHANNEL_VBAT */
#ifdef ADC_CHANNEL_TEMPSENSOR
case
RT_ADC_INTERN_CH_TEMPER
:
*
stm32_channel
=
ADC_CHANNEL_TEMPSENSOR
;
break
;
#endif
/* ADC_CHANNEL_TEMPSENSOR */
default:
return
-
RT_EINVAL
;
}
return
RT_EOK
;
}
static
rt_err_t
stm32_adc_enabled
(
struct
rt_adc_device
*
device
,
rt_int8_t
channel
,
rt_bool_t
enabled
)
{
{
ADC_HandleTypeDef
*
stm32_adc_handler
;
ADC_HandleTypeDef
*
stm32_adc_handler
;
RT_ASSERT
(
device
!=
RT_NULL
);
RT_ASSERT
(
device
!=
RT_NULL
);
...
@@ -57,68 +148,11 @@ static rt_err_t stm32_adc_enabled(struct rt_adc_device *device, rt_uint32_t chan
...
@@ -57,68 +148,11 @@ static rt_err_t stm32_adc_enabled(struct rt_adc_device *device, rt_uint32_t chan
{
{
ADC_ChannelConfTypeDef
ADC_ChanConf
;
ADC_ChannelConfTypeDef
ADC_ChanConf
;
rt_memset
(
&
ADC_ChanConf
,
0
,
sizeof
(
ADC_ChanConf
));
rt_memset
(
&
ADC_ChanConf
,
0
,
sizeof
(
ADC_ChanConf
));
#ifndef ADC_CHANNEL_16
if
(
channel
==
16
)
{
LOG_E
(
"ADC channel must not be 16."
);
return
-
RT_ERROR
;
}
#endif
/* ADC channel number is up to 17 */
if
(
stm32_adc_get_channel
(
channel
,
&
ADC_ChanConf
.
Channel
)
!=
RT_EOK
)
#if !defined(ADC_CHANNEL_18)
if
(
channel
<=
17
||
(
#ifdef ADC_CHANNEL_VREFINT
channel
!=
(
ADC_CHANNEL_VREFINT
-
ADC_CHANNEL_0
)
#endif
/* ADC_CHANNEL_VREFINT */
#ifdef ADC_CHANNEL_TEMPSENSOR
||
channel
!=
(
ADC_CHANNEL_TEMPSENSOR
-
ADC_CHANNEL_0
)
#endif
/* ADC_CHANNEL_TEMPSENSOR */
#ifdef ADC_CHANNEL_VBAT
||
channel
!=
(
ADC_CHANNEL_VBAT
-
ADC_CHANNEL_0
)
#endif
/* ADC_CHANNEL_VBAT */
))
/* ADC channel number is up to 19 */
#elif defined(ADC_CHANNEL_19)
if
(
channel
<=
19
||
(
#ifdef ADC_CHANNEL_VREFINT
channel
!=
(
ADC_CHANNEL_VREFINT
-
ADC_CHANNEL_0
)
#endif
/* ADC_CHANNEL_VREFINT */
#ifndef ADC_CHANNEL_TEMPSENSOR
||
channel
!=
(
ADC_CHANNEL_TEMPSENSOR
-
ADC_CHANNEL_0
)
#endif
/* ADC_CHANNEL_TEMPSENSOR */
#ifdef ADC_CHANNEL_VBAT
||
channel
!=
(
=
ADC_CHANNEL_VBAT
-
ADC_CHANNEL_0
)
#endif
/* ADC_CHANNEL_VBAT */
))
/* ADC channel number is up to 18 */
#else
if
(
channel
<=
18
||
(
#ifdef ADC_CHANNEL_VREFINT
channel
!=
(
ADC_CHANNEL_VREFINT
-
ADC_CHANNEL_0
)
#endif
/* ADC_CHANNEL_VREFINT */
#ifdef ADC_CHANNEL_TEMPSENSOR
||
channel
!=
(
ADC_CHANNEL_TEMPSENSOR
-
ADC_CHANNEL_0
)
#endif
/* ADC_CHANNEL_TEMPSENSOR */
#ifdef ADC_CHANNEL_VBAT
||
channel
!=
(
ADC_CHANNEL_VBAT
-
ADC_CHANNEL_0
)
#endif
/* ADC_CHANNEL_VBAT */
))
#endif
/* !defined(ADC_CHANNEL_18) */
{
{
/* set stm32 ADC channel */
LOG_E
(
"ADC channel illegal: %d"
,
channel
);
ADC_ChanConf
.
Channel
=
stm32_adc_get_channel
(
channel
);
return
-
RT_EINVAL
;
}
else
{
#if !defined(ADC_CHANNEL_18)
LOG_E
(
"ADC channel must be between 0 and 17."
);
#elif defined(ADC_CHANNEL_19)
LOG_E
(
"ADC channel must be between 0 and 19."
);
#else
LOG_E
(
"ADC channel must be between 0 and 18."
);
#endif
/* !defined(ADC_CHANNEL_18) */
return
-
RT_ERROR
;
}
}
#if defined(SOC_SERIES_STM32MP1) || defined (SOC_SERIES_STM32H7) || defined (SOC_SERIES_STM32WB)
#if defined(SOC_SERIES_STM32MP1) || defined (SOC_SERIES_STM32H7) || defined (SOC_SERIES_STM32WB)
...
@@ -219,103 +253,6 @@ static rt_uint8_t stm32_adc_get_resolution(struct rt_adc_device *device)
...
@@ -219,103 +253,6 @@ static rt_uint8_t stm32_adc_get_resolution(struct rt_adc_device *device)
#endif
/* defined(SOC_SERIES_STM32F1) || defined(SOC_SERIES_STM32F3) */
#endif
/* defined(SOC_SERIES_STM32F1) || defined(SOC_SERIES_STM32F3) */
}
}
static
rt_uint32_t
stm32_adc_get_channel
(
rt_uint32_t
channel
)
{
rt_uint32_t
stm32_channel
=
0
;
switch
(
channel
)
{
case
0
:
stm32_channel
=
ADC_CHANNEL_0
;
break
;
case
1
:
stm32_channel
=
ADC_CHANNEL_1
;
break
;
case
2
:
stm32_channel
=
ADC_CHANNEL_2
;
break
;
case
3
:
stm32_channel
=
ADC_CHANNEL_3
;
break
;
case
4
:
stm32_channel
=
ADC_CHANNEL_4
;
break
;
case
5
:
stm32_channel
=
ADC_CHANNEL_5
;
break
;
case
6
:
stm32_channel
=
ADC_CHANNEL_6
;
break
;
case
7
:
stm32_channel
=
ADC_CHANNEL_7
;
break
;
case
8
:
stm32_channel
=
ADC_CHANNEL_8
;
break
;
case
9
:
stm32_channel
=
ADC_CHANNEL_9
;
break
;
case
10
:
stm32_channel
=
ADC_CHANNEL_10
;
break
;
case
11
:
stm32_channel
=
ADC_CHANNEL_11
;
break
;
case
12
:
stm32_channel
=
ADC_CHANNEL_12
;
break
;
case
13
:
stm32_channel
=
ADC_CHANNEL_13
;
break
;
case
14
:
stm32_channel
=
ADC_CHANNEL_14
;
break
;
case
15
:
stm32_channel
=
ADC_CHANNEL_15
;
break
;
#ifdef ADC_CHANNEL_16
case
16
:
stm32_channel
=
ADC_CHANNEL_16
;
break
;
#endif
case
17
:
stm32_channel
=
ADC_CHANNEL_17
;
break
;
#ifdef ADC_CHANNEL_18
case
18
:
stm32_channel
=
ADC_CHANNEL_18
;
break
;
#endif
#ifdef ADC_CHANNEL_19
case
19
:
stm32_channel
=
ADC_CHANNEL_19
;
break
;
#endif
default:
switch
(
channel
)
{
#ifdef ADC_CHANNEL_VREFINT
case
ADC_CHANNEL_VREFINT
-
ADC_CHANNEL_0
:
stm32_channel
=
ADC_CHANNEL_VREFINT
;
break
;
#endif
/* ADC_CHANNEL_VREFINT */
#ifdef ADC_CHANNEL_VBAT
case
ADC_CHANNEL_VBAT
-
ADC_CHANNEL_0
:
stm32_channel
=
ADC_CHANNEL_VBAT
;
break
;
#endif
/* ADC_CHANNEL_VBAT */
#ifdef ADC_CHANNEL_TEMPSENSOR
case
ADC_CHANNEL_TEMPSENSOR
-
ADC_CHANNEL_0
:
stm32_channel
=
ADC_CHANNEL_TEMPSENSOR
;
break
;
#endif
/* ADC_CHANNEL_TEMPSENSOR */
}
break
;
}
return
stm32_channel
;
}
static
rt_int16_t
stm32_adc_get_vref
(
struct
rt_adc_device
*
device
)
static
rt_int16_t
stm32_adc_get_vref
(
struct
rt_adc_device
*
device
)
{
{
if
(
device
==
RT_NULL
)
if
(
device
==
RT_NULL
)
...
@@ -328,10 +265,10 @@ static rt_int16_t stm32_adc_get_vref (struct rt_adc_device *device)
...
@@ -328,10 +265,10 @@ static rt_int16_t stm32_adc_get_vref (struct rt_adc_device *device)
ADC_HandleTypeDef
*
stm32_adc_handler
=
device
->
parent
.
user_data
;
ADC_HandleTypeDef
*
stm32_adc_handler
=
device
->
parent
.
user_data
;
ret
=
rt_adc_enable
(
device
,
ADC_CHANNEL_VREFINT
-
ADC_CHANNEL_0
);
ret
=
rt_adc_enable
(
device
,
RT_ADC_INTERN_CH_VREF
);
if
(
ret
!=
RT_EOK
)
return
(
rt_int16_t
)
ret
;
if
(
ret
!=
RT_EOK
)
return
(
rt_int16_t
)
ret
;
vref_value
=
rt_adc_read
(
device
,
ADC_CHANNEL_VREFINT
-
ADC_CHANNEL_0
);
vref_value
=
rt_adc_read
(
device
,
RT_ADC_INTERN_CH_VREF
);
ret
=
rt_adc_disable
(
device
,
ADC_CHANNEL_VREFINT
-
ADC_CHANNEL_0
);
ret
=
rt_adc_disable
(
device
,
RT_ADC_INTERN_CH_VREF
);
if
(
ret
!=
RT_EOK
)
return
(
rt_int16_t
)
ret
;
if
(
ret
!=
RT_EOK
)
return
(
rt_int16_t
)
ret
;
vref_mv
=
__LL_ADC_CALC_VREFANALOG_VOLTAGE
(
vref_value
,
stm32_adc_handler
->
Init
.
Resolution
);
vref_mv
=
__LL_ADC_CALC_VREFANALOG_VOLTAGE
(
vref_value
,
stm32_adc_handler
->
Init
.
Resolution
);
...
@@ -341,7 +278,7 @@ static rt_int16_t stm32_adc_get_vref (struct rt_adc_device *device)
...
@@ -341,7 +278,7 @@ static rt_int16_t stm32_adc_get_vref (struct rt_adc_device *device)
return
vref_mv
;
return
vref_mv
;
}
}
static
rt_err_t
stm32_adc_get_value
(
struct
rt_adc_device
*
device
,
rt_
uint32
_t
channel
,
rt_uint32_t
*
value
)
static
rt_err_t
stm32_adc_get_value
(
struct
rt_adc_device
*
device
,
rt_
int8
_t
channel
,
rt_uint32_t
*
value
)
{
{
ADC_HandleTypeDef
*
stm32_adc_handler
;
ADC_HandleTypeDef
*
stm32_adc_handler
;
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录