Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
Richard 祝发兴 - -IT
rt-thread
提交
aed3cc16
R
rt-thread
项目概览
Richard 祝发兴 - -IT
/
rt-thread
与 Fork 源项目一致
Fork自
RT-Thread / rt-thread
通知
1
Star
1
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
1
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
R
rt-thread
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
1
Issue
1
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
aed3cc16
编写于
1月 19, 2021
作者:
Trisuborn
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
为stm32的PWM驱动添加对STM32H7的支持、添加互补PWM功能的支持,补全pwm_get命令
上级
6742f9a3
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
111 addition
and
25 deletion
+111
-25
bsp/stm32/libraries/HAL_Drivers/drv_pwm.c
bsp/stm32/libraries/HAL_Drivers/drv_pwm.c
+29
-9
components/drivers/include/drivers/rt_drv_pwm.h
components/drivers/include/drivers/rt_drv_pwm.h
+11
-5
components/drivers/misc/rt_drv_pwm.c
components/drivers/misc/rt_drv_pwm.c
+71
-11
未找到文件。
bsp/stm32/libraries/HAL_Drivers/drv_pwm.c
浏览文件 @
aed3cc16
...
...
@@ -9,8 +9,10 @@
*/
#include <board.h>
#ifdef RT_USING_PWM
#include "drv_config.h"
#include <drivers/rt_drv_pwm.h>
//#define DRV_DEBUG
#define LOG_TAG "drv.pwm"
...
...
@@ -167,13 +169,27 @@ static rt_err_t drv_pwm_enable(TIM_HandleTypeDef *htim, struct rt_pwm_configurat
/* Converts the channel number to the channel number of Hal library */
rt_uint32_t
channel
=
0x04
*
(
configuration
->
channel
-
1
);
if
(
!
enable
)
if
(
!
configuration
->
complementary
)
{
HAL_TIM_PWM_Stop
(
htim
,
channel
);
if
(
!
enable
)
{
HAL_TIM_PWM_Stop
(
htim
,
channel
);
}
else
{
HAL_TIM_PWM_Start
(
htim
,
channel
);
}
}
else
else
if
(
configuration
->
complementary
)
{
HAL_TIM_PWM_Start
(
htim
,
channel
);
if
(
!
enable
)
{
HAL_TIMEx_PWMN_Stop
(
htim
,
channel
);
}
else
{
HAL_TIMEx_PWMN_Start
(
htim
,
channel
);
}
}
return
RT_EOK
;
...
...
@@ -187,21 +203,23 @@ static rt_err_t drv_pwm_get(TIM_HandleTypeDef *htim, struct rt_pwm_configuration
#if defined(SOC_SERIES_STM32F2) || defined(SOC_SERIES_STM32F4) || defined(SOC_SERIES_STM32F7)
if
(
htim
->
Instance
==
TIM9
||
htim
->
Instance
==
TIM10
||
htim
->
Instance
==
TIM11
)
#elif defined(SOC_SERIES_STM32L4)
#elif defined(SOC_SERIES_STM32L4)
|| defined(SOC_SERIES_STM32H7)
if
(
htim
->
Instance
==
TIM15
||
htim
->
Instance
==
TIM16
||
htim
->
Instance
==
TIM17
)
#elif defined(SOC_SERIES_STM32MP1)
if
(
htim
->
Instance
==
TIM4
)
if
(
htim
->
Instance
==
TIM4
)
#elif defined(SOC_SERIES_STM32F1) || defined(SOC_SERIES_STM32F0) || defined(SOC_SERIES_STM32G0)
if
(
0
)
#endif
{
#if !defined(SOC_SERIES_STM32F0) && !defined(SOC_SERIES_STM32G0)
tim_clock
=
HAL_RCC_GetPCLK2Freq
()
*
2
;
#else
tim_clock
=
HAL_RCC_GetPCLK2Freq
();
#endif
}
else
{
#if defined(SOC_SERIES_STM32L4) || defined(SOC_SERIES_STM32F0) || defined(SOC_SERIES_STM32G0)
#if defined(SOC_SERIES_STM32L4) || defined(SOC_SERIES_STM32F0) || defined(SOC_SERIES_STM32G0)
|| defined(SOC_SERIES_STM32H7)
tim_clock
=
HAL_RCC_GetPCLK1Freq
();
#else
tim_clock
=
HAL_RCC_GetPCLK1Freq
()
*
2
;
...
...
@@ -234,7 +252,7 @@ static rt_err_t drv_pwm_set(TIM_HandleTypeDef *htim, struct rt_pwm_configuration
#if defined(SOC_SERIES_STM32F2) || defined(SOC_SERIES_STM32F4) || defined(SOC_SERIES_STM32F7)
if
(
htim
->
Instance
==
TIM9
||
htim
->
Instance
==
TIM10
||
htim
->
Instance
==
TIM11
)
#elif defined(SOC_SERIES_STM32L4)
#elif defined(SOC_SERIES_STM32L4)
|| defined(SOC_SERIES_STM32H7)
if
(
htim
->
Instance
==
TIM15
||
htim
->
Instance
==
TIM16
||
htim
->
Instance
==
TIM17
)
#elif defined(SOC_SERIES_STM32MP1)
if
(
htim
->
Instance
==
TIM4
)
...
...
@@ -244,11 +262,13 @@ static rt_err_t drv_pwm_set(TIM_HandleTypeDef *htim, struct rt_pwm_configuration
{
#if !defined(SOC_SERIES_STM32F0) && !defined(SOC_SERIES_STM32G0)
tim_clock
=
HAL_RCC_GetPCLK2Freq
()
*
2
;
#else
tim_clock
=
HAL_RCC_GetPCLK2Freq
();
#endif
}
else
{
#if defined(SOC_SERIES_STM32L4) || defined(SOC_SERIES_STM32F0) || defined(SOC_SERIES_STM32G0)
#if defined(SOC_SERIES_STM32L4) || defined(SOC_SERIES_STM32F0) || defined(SOC_SERIES_STM32G0)
|| defined(SOC_SERIES_STM32H7)
tim_clock
=
HAL_RCC_GetPCLK1Freq
();
#else
tim_clock
=
HAL_RCC_GetPCLK1Freq
()
*
2
;
...
...
components/drivers/include/drivers/rt_drv_pwm.h
浏览文件 @
aed3cc16
...
...
@@ -21,9 +21,15 @@
struct
rt_pwm_configuration
{
rt_uint32_t
channel
;
/* 0-n */
rt_uint32_t
period
;
/* unit:ns 1ns~4.29s:1Ghz~0.23hz */
rt_uint32_t
pulse
;
/* unit:ns (pulse<=period) */
rt_uint32_t
channel
;
/* 0-n */
rt_uint32_t
period
;
/* unit:ns 1ns~4.29s:1Ghz~0.23hz */
rt_uint32_t
pulse
;
/* unit:ns (pulse<=period) */
/*
* RT_TRUE : The channel of pwm is complememtary.
* RT_FALSE : The channel of pwm is nomal.
*/
rt_bool_t
complementary
;
};
struct
rt_device_pwm
;
...
...
@@ -40,8 +46,8 @@ struct rt_device_pwm
rt_err_t
rt_device_pwm_register
(
struct
rt_device_pwm
*
device
,
const
char
*
name
,
const
struct
rt_pwm_ops
*
ops
,
const
void
*
user_data
);
rt_err_t
rt_pwm_enable
(
struct
rt_device_pwm
*
device
,
int
channel
);
rt_err_t
rt_pwm_disable
(
struct
rt_device_pwm
*
device
,
int
channel
);
rt_err_t
rt_pwm_enable
(
struct
rt_device_pwm
*
device
,
int
channel
,
rt_uint8_t
complementary
);
rt_err_t
rt_pwm_disable
(
struct
rt_device_pwm
*
device
,
int
channel
,
rt_uint8_t
complementary
);
rt_err_t
rt_pwm_set
(
struct
rt_device_pwm
*
device
,
int
channel
,
rt_uint32_t
period
,
rt_uint32_t
pulse
);
#endif
/* __DRV_PWM_H_INCLUDE__ */
components/drivers/misc/rt_drv_pwm.c
浏览文件 @
aed3cc16
...
...
@@ -126,7 +126,7 @@ rt_err_t rt_device_pwm_register(struct rt_device_pwm *device, const char *name,
return
result
;
}
rt_err_t
rt_pwm_enable
(
struct
rt_device_pwm
*
device
,
int
channel
)
rt_err_t
rt_pwm_enable
(
struct
rt_device_pwm
*
device
,
int
channel
,
rt_uint8_t
complementary
)
{
rt_err_t
result
=
RT_EOK
;
struct
rt_pwm_configuration
configuration
=
{
0
};
...
...
@@ -137,12 +137,13 @@ rt_err_t rt_pwm_enable(struct rt_device_pwm *device, int channel)
}
configuration
.
channel
=
channel
;
configuration
.
complementary
=
complementary
?
(
RT_TRUE
)
:
(
RT_FALSE
);
result
=
rt_device_control
(
&
device
->
parent
,
PWM_CMD_ENABLE
,
&
configuration
);
return
result
;
}
rt_err_t
rt_pwm_disable
(
struct
rt_device_pwm
*
device
,
int
channel
)
rt_err_t
rt_pwm_disable
(
struct
rt_device_pwm
*
device
,
int
channel
,
rt_uint8_t
complementary
)
{
rt_err_t
result
=
RT_EOK
;
struct
rt_pwm_configuration
configuration
=
{
0
};
...
...
@@ -153,6 +154,7 @@ rt_err_t rt_pwm_disable(struct rt_device_pwm *device, int channel)
}
configuration
.
channel
=
channel
;
configuration
.
complementary
=
complementary
?
(
RT_TRUE
)
:
(
RT_FALSE
);
result
=
rt_device_control
(
&
device
->
parent
,
PWM_CMD_DISABLE
,
&
configuration
);
return
result
;
...
...
@@ -176,6 +178,20 @@ rt_err_t rt_pwm_set(struct rt_device_pwm *device, int channel, rt_uint32_t perio
return
result
;
}
rt_err_t
rt_pwm_get
(
struct
rt_device_pwm
*
device
,
struct
rt_pwm_configuration
*
cfg
)
{
rt_err_t
result
=
RT_EOK
;
if
(
!
device
)
{
return
-
RT_EIO
;
}
result
=
rt_device_control
(
&
device
->
parent
,
PWM_CMD_GET
,
cfg
);
return
result
;
}
#ifdef RT_USING_FINSH
#include <finsh.h>
...
...
@@ -188,9 +204,10 @@ static int pwm_enable(int argc, char **argv)
int
result
=
0
;
struct
rt_device_pwm
*
device
=
RT_NULL
;
if
(
argc
!=
3
)
if
(
argc
!=
4
)
{
rt_kprintf
(
"Usage: pwm_enable pwm1 1
\n
"
);
rt_kprintf
(
"Usage: pwm_enable pwm1 1 1
\n
"
);
rt_kprintf
(
" pwm_enable <pwm_dev> <channel> <complementary>
\n
"
);
result
=
-
RT_ERROR
;
goto
_exit
;
}
...
...
@@ -202,21 +219,22 @@ static int pwm_enable(int argc, char **argv)
goto
_exit
;
}
result
=
rt_pwm_enable
(
device
,
atoi
(
argv
[
2
]));
result
=
rt_pwm_enable
(
device
,
atoi
(
argv
[
2
])
,
atoi
(
argv
[
3
])
);
_exit:
return
result
;
}
MSH_CMD_EXPORT
(
pwm_enable
,
pwm_enable
pwm1
1
);
MSH_CMD_EXPORT
(
pwm_enable
,
pwm_enable
<
pwm_dev
>
<
channel
>
<
complementary
>
);
static
int
pwm_disable
(
int
argc
,
char
**
argv
)
{
int
result
=
0
;
struct
rt_device_pwm
*
device
=
RT_NULL
;
if
(
argc
!=
3
)
if
(
argc
!=
4
)
{
rt_kprintf
(
"Usage: pwm_enable pwm1 1
\n
"
);
rt_kprintf
(
"Usage: pwm_enable pwm1 1 1
\n
"
);
rt_kprintf
(
" pwm_disable <pwm_dev> <channel> <complementary>
\n
"
);
result
=
-
RT_ERROR
;
goto
_exit
;
}
...
...
@@ -228,12 +246,12 @@ static int pwm_disable(int argc, char **argv)
goto
_exit
;
}
result
=
rt_pwm_disable
(
device
,
atoi
(
argv
[
2
]));
result
=
rt_pwm_disable
(
device
,
atoi
(
argv
[
2
])
,
atoi
(
argv
[
3
])
);
_exit:
return
result
;
}
MSH_CMD_EXPORT
(
pwm_disable
,
pwm_disable
pwm1
1
);
MSH_CMD_EXPORT
(
pwm_disable
,
pwm_disable
<
pwm_dev
>
<
channel
>
<
complementary
>
);
static
int
pwm_set
(
int
argc
,
char
**
argv
)
{
...
...
@@ -243,6 +261,7 @@ static int pwm_set(int argc, char **argv)
if
(
argc
!=
5
)
{
rt_kprintf
(
"Usage: pwm_set pwm1 1 100 50
\n
"
);
rt_kprintf
(
"Usage: pwm_set <pwm_dev> <channel> <period> <pulse>
\n
"
);
result
=
-
RT_ERROR
;
goto
_exit
;
}
...
...
@@ -259,7 +278,48 @@ static int pwm_set(int argc, char **argv)
_exit:
return
result
;
}
MSH_CMD_EXPORT
(
pwm_set
,
pwm_set
pwm1
1
100
50
);
MSH_CMD_EXPORT
(
pwm_set
,
pwm_set
<
pwm_dev
>
<
channel
>
<
period
>
<
pulse
>
);
static
int
pwm_get
(
int
argc
,
char
**
argv
)
{
int
result
=
0
;
struct
rt_device_pwm
*
device
=
RT_NULL
;
struct
rt_pwm_configuration
cfg
=
{
0
};
if
(
argc
!=
3
)
{
rt_kprintf
(
"Usage: pwm_get pwm1 1
\n
"
);
rt_kprintf
(
" pwm_get <pwm_dev> <channel>
\n
"
);
result
=
-
RT_ERROR
;
goto
_exit
;
}
device
=
(
struct
rt_device_pwm
*
)
rt_device_find
(
argv
[
1
]);
if
(
!
device
)
{
result
=
-
RT_EIO
;
goto
_exit
;
}
cfg
.
channel
=
atoi
(
argv
[
2
]);
result
=
rt_pwm_get
(
device
,
&
cfg
);
if
(
result
!=
RT_EOK
)
{
rt_kprintf
(
"Get info of device: [%s] error.
\n
"
,
argv
[
1
]);
}
else
{
rt_kprintf
(
"Get info of device: [%s]:
\n
"
,
argv
[
1
]);
rt_kprintf
(
"period : %d
\n
"
,
cfg
.
period
);
rt_kprintf
(
"pulse : %d
\n
"
,
cfg
.
pulse
);
rt_kprintf
(
"Duty cycle : %d%%
\n
"
,
(
int
)(((
double
)(
cfg
.
pulse
)
/
(
cfg
.
period
))
*
100
));
}
_exit:
return
result
;
}
MSH_CMD_EXPORT
(
pwm_get
,
pwm_get
<
pwm_dev
>
<
channel
>
);
#endif
/* FINSH_USING_MSH */
#endif
/* RT_USING_FINSH */
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录