Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
2345VOR
rt-thread
提交
435d2b24
R
rt-thread
项目概览
2345VOR
/
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,发现更多精彩内容 >>
提交
435d2b24
编写于
4月 13, 2022
作者:
R
rtthread-bot
浏览文件
操作
浏览文件
下载
差异文件
Merge remote-tracking branch 'rtt_gitee/gitee_master'
上级
efdd28bd
9551fdc1
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
57 addition
and
55 deletion
+57
-55
bsp/gd32/libraries/gd32_drivers/drv_adc.c
bsp/gd32/libraries/gd32_drivers/drv_adc.c
+51
-51
bsp/gd32/libraries/gd32_drivers/drv_gpio.h
bsp/gd32/libraries/gd32_drivers/drv_gpio.h
+6
-4
未找到文件。
bsp/gd32/libraries/gd32_drivers/drv_adc.c
浏览文件 @
435d2b24
...
...
@@ -6,91 +6,86 @@
* Change Logs:
* Date Author Notes
* 2021-02-25 iysheng first version
* 2022-04-13 wangh adc init and convert bug fix
*/
#include <board.h>
#include <drivers/adc.h>
#define DBG_TAG "drv.adc"
#define DBG_LVL DBG_INFO
#ifdef RT_USING_ADC
#define DBG_TAG "drv.adc"
#define DBG_LVL DBG_INFO
#include <rtdbg.h>
#ifdef RT_USING_ADC
#define MAX_EXTERN_ADC_CHANNEL 16
#define MAX_EXTERN_ADC_CHANNEL 16
typedef
struct
{
typedef
struct
{
struct
rt_adc_device
adc_dev
;
char
name
[
8
];
rt_base_t
adc_pins
[
16
];
void
*
private_data
;
}
gd32_adc_device
;
static
gd32_adc_device
g_gd32
_devs
[]
=
{
static
gd32_adc_device
adc
_devs
[]
=
{
#ifdef BSP_USING_ADC0
{
{},
"adc0"
,
{
.
name
=
"adc0"
,
.
adc_pins
=
{
GET_PIN
(
A
,
0
),
GET_PIN
(
A
,
1
),
GET_PIN
(
A
,
2
),
GET_PIN
(
A
,
3
),
GET_PIN
(
A
,
4
),
GET_PIN
(
A
,
5
),
GET_PIN
(
A
,
6
),
GET_PIN
(
A
,
7
),
GET_PIN
(
B
,
0
),
GET_PIN
(
B
,
1
),
GET_PIN
(
C
,
0
),
GET_PIN
(
C
,
1
),
GET_PIN
(
C
,
2
),
GET_PIN
(
C
,
3
),
GET_PIN
(
C
,
4
),
GET_PIN
(
C
,
5
),
},
(
void
*
)
ADC0
,
.
private_data
=
(
void
*
)
ADC0
,
},
#endif
#ifdef BSP_USING_ADC1
{
{},
"adc1"
,
{
.
name
=
"adc1"
,
.
adc_pins
=
{
GET_PIN
(
A
,
0
),
GET_PIN
(
A
,
1
),
GET_PIN
(
A
,
2
),
GET_PIN
(
A
,
3
),
GET_PIN
(
A
,
4
),
GET_PIN
(
A
,
5
),
GET_PIN
(
A
,
6
),
GET_PIN
(
A
,
7
),
GET_PIN
(
B
,
0
),
GET_PIN
(
B
,
1
),
GET_PIN
(
C
,
0
),
GET_PIN
(
C
,
1
),
GET_PIN
(
C
,
2
),
GET_PIN
(
C
,
3
),
GET_PIN
(
C
,
4
),
GET_PIN
(
C
,
5
),
},
(
void
*
)
ADC1
,
.
private_data
=
(
void
*
)
ADC1
,
},
#endif
};
/*
* static void init_pin4adc
*
* @ rt_uint32_t pin: pin information
* return: N/A
*/
static
void
init_pin4adc
(
rt_base_t
pin
)
static
void
gd32_adc_gpio_init
(
rt_base_t
pin
)
{
rcu_periph_clock_enable
((
rcu_periph_enum
)
PIN_GDRCU
(
pin
));
gpio_init
(
PIN_GDPORT
(
pin
),
GPIO_MODE_AIN
,
GPIO_OSPEED_50MHZ
,
PIN_GDPIN
(
pin
));
}
static
rt_err_t
gd32
_adc_enabled
(
struct
rt_adc_device
*
device
,
rt_uint32_t
channel
,
rt_bool_t
enabled
)
static
rt_err_t
drv
_adc_enabled
(
struct
rt_adc_device
*
device
,
rt_uint32_t
channel
,
rt_bool_t
enabled
)
{
uint32_t
adc_periph
;
gd32_adc_device
*
gd32_adc
=
(
gd32_adc_device
*
)
device
;
if
(
channel
>=
MAX_EXTERN_ADC_CHANNEL
)
if
((
device
==
NULL
)
||
(
channel
>=
MAX_EXTERN_ADC_CHANNEL
))
{
LOG_E
(
"invalid channel"
);
LOG_E
(
"invalid channel
\r\n
"
);
return
-
RT_EINVAL
;
}
adc_periph
=
(
uint32_t
)(
device
->
parent
.
user_data
);
gd32_adc_device
*
gd32_adc
=
(
gd32_adc_device
*
)
device
;
uint32_t
adc_periph
=
(
uint32_t
)(
device
->
parent
.
user_data
);
if
(
enabled
==
ENABLE
)
{
init_pin4adc
(
gd32_adc
->
adc_pins
[
channel
]);
gd32_adc_gpio_init
(
gd32_adc
->
adc_pins
[
channel
]);
adc_deinit
(
adc_periph
);
adc_channel_length_config
(
adc_periph
,
ADC_REGULAR_CHANNEL
,
1
);
adc_data_alignment_config
(
adc_periph
,
ADC_DATAALIGN_RIGHT
);
adc_external_trigger_source_config
(
adc_periph
,
ADC_REGULAR_CHANNEL
,
ADC0_1_2_EXTTRIG_INSERTED_NONE
);
adc_external_trigger_config
(
adc_periph
,
ADC_REGULAR_CHANNEL
,
ENABLE
);
adc_regular_channel_config
(
adc_periph
,
0
,
channel
,
ADC_SAMPLETIME_13POINT5
);
adc_data_alignment_config
(
adc_periph
,
ADC_DATAALIGN_RIGHT
);
/* 数据右对齐 */
adc_external_trigger_config
(
adc_periph
,
ADC_REGULAR_CHANNEL
,
ENABLE
);
/* 规则组外部触发使能 */
adc_external_trigger_source_config
(
adc_periph
,
ADC_REGULAR_CHANNEL
,
ADC0_1_2_EXTTRIG_REGULAR_NONE
);
adc_special_function_config
(
adc_periph
,
ADC_SCAN_MODE
,
DISABLE
);
/* 扫描模式禁止,单通道模式 */
adc_special_function_config
(
adc_periph
,
ADC_CONTINUOUS_MODE
,
DISABLE
);
/* 连续转换禁止,单次转换模式 */
adc_discontinuous_mode_config
(
adc_periph
,
ADC_REGULAR_CHANNEL
,
1
);
/* 规则组间断使能及转换通道数目 */
adc_enable
(
adc_periph
);
rt_hw_us_delay
(
500
);
adc_calibration_enable
(
adc_periph
);
/* ADC自校准 */
}
else
{
...
...
@@ -100,30 +95,32 @@ static rt_err_t gd32_adc_enabled(struct rt_adc_device *device, rt_uint32_t chann
return
0
;
}
static
rt_err_t
gd32
_adc_convert
(
struct
rt_adc_device
*
device
,
rt_uint32_t
channel
,
rt_uint32_t
*
value
)
static
rt_err_t
drv
_adc_convert
(
struct
rt_adc_device
*
device
,
rt_uint32_t
channel
,
rt_uint32_t
*
value
)
{
uint32_t
adc_periph
;
if
(
!
value
)
if
((
device
==
NULL
)
||
(
channel
>=
MAX_EXTERN_ADC_CHANNEL
)
||
(
value
==
NULL
))
{
LOG_E
(
"invalid param"
);
LOG_E
(
"invalid param
\r\n
"
);
return
-
RT_EINVAL
;
}
adc_periph
=
(
uint32_t
)(
device
->
parent
.
user_data
);
uint32_t
adc_periph
=
(
uint32_t
)(
device
->
parent
.
user_data
);
adc_regular_channel_config
(
adc_periph
,
0
,
channel
,
ADC_SAMPLETIME_239POINT5
);
adc_software_trigger_enable
(
adc_periph
,
ADC_REGULAR_CHANNEL
);
while
(
!
adc_flag_get
(
adc_periph
,
ADC_FLAG_EOC
));
/* 等待ADC转换完成 */
adc_flag_clear
(
adc_periph
,
ADC_FLAG_EOC
);
/* 清除转换完成标志位 */
adc_flag_clear
(
adc_periph
,
ADC_FLAG_STRC
);
*
value
=
adc_regular_data_read
(
adc_periph
);
return
0
;
}
static
struct
rt_adc_ops
g_gd32
_adc_ops
=
{
gd32
_adc_enabled
,
gd32
_adc_convert
,
static
struct
rt_adc_ops
drv
_adc_ops
=
{
.
enabled
=
drv
_adc_enabled
,
.
convert
=
drv
_adc_convert
,
};
static
int
rt_hw_adc_init
(
void
)
{
int
ret
,
i
=
0
;
...
...
@@ -136,15 +133,18 @@ static int rt_hw_adc_init(void)
rcu_periph_clock_enable
(
RCU_ADC1
);
#endif
for
(;
i
<
sizeof
(
g_gd32_devs
)
/
sizeof
(
g_gd32_devs
[
0
]);
i
++
)
rcu_adc_clock_config
(
RCU_CKADC_CKAPB2_DIV6
);
/* 72/6 = 12MHz need < 14MHZ */
adc_mode_config
(
ADC_MODE_FREE
);
for
(;
i
<
sizeof
(
adc_devs
)
/
sizeof
(
gd32_adc_device
);
i
++
)
{
ret
=
rt_hw_adc_register
(
&
g_gd32_devs
[
i
].
adc_dev
,
\
(
const
char
*
)
g_gd32_devs
[
i
].
name
,
\
&
g_gd32_adc_ops
,
(
void
*
)
g_gd32
_devs
[
i
].
private_data
);
ret
=
rt_hw_adc_register
(
&
adc_devs
[
i
].
adc_dev
,
(
const
char
*
)
adc_devs
[
i
].
name
,
&
drv_adc_ops
,
(
void
*
)
adc
_devs
[
i
].
private_data
);
if
(
ret
!=
RT_EOK
)
{
/* TODO err handler */
LOG_E
(
"failed register %s, err=%d
"
,
g_gd32
_devs
[
i
].
name
,
ret
);
LOG_E
(
"failed register %s, err=%d
\r\n
"
,
adc
_devs
[
i
].
name
,
ret
);
}
}
...
...
bsp/gd32/libraries/gd32_drivers/drv_gpio.h
浏览文件 @
435d2b24
...
...
@@ -48,11 +48,13 @@ extern "C" {
#define GET_PIN(PORTx,PIN) (rt_base_t)((16 * ( ((rt_base_t)__GD32_PORT(PORTx) - (rt_base_t)GPIO_BASE)/(0x0400UL) )) + PIN)
#define PIN_PORT(pin) ((uint8_t)(((pin) >> 4) & 0xFu))
#define PIN_NO(pin) ((uint8_t)((pin) & 0xFu))
#define PIN_NUM(port, no) (((((port)&0xFu) << 4) | ((no)&0xFu)))
#define PIN_PORT(pin) ((uint8_t)(((pin) >> 4) & 0xFu))
#define PIN_NO(pin) ((uint8_t)((pin)&0xFu))
#define PIN_GDPORT(pin) (GPIO_BASE + (0x400u * PIN_PORT(pin)))
#define PIN_GDPIN(pin) ((uint16_t)(1u << PIN_NO(pin)))
#define PIN_GDPORT(pin) (GPIO_BASE + (0x400u * PIN_PORT(pin)))
/* gpio_periph GPIOA~GPIOG */
#define PIN_GDPIN(pin) ((uint16_t)(1u << PIN_NO(pin)))
/* GPIO_PIN_0~GPIO_PIN_15 */
#define PIN_GDRCU(pin) RCU_REGIDX_BIT(APB2EN_REG_OFFSET, PIN_PORT(pin) + 2)
/* pin gpio外设时钟 */
struct
pin_index
{
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录