Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
天中雨水
rt-thread
提交
b2bc0dcd
R
rt-thread
项目概览
天中雨水
/
rt-thread
该项目与 Fork 源项目分叉
Fork自
RT-Thread / rt-thread
通知
2
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,发现更多精彩内容 >>
未验证
提交
b2bc0dcd
编写于
7月 04, 2022
作者:
BreederBai
提交者:
GitHub
7月 04, 2022
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
[rt_drv_pwm]增加API:增加单独设置PWM频率和脉宽的函数 (#6130)
* [rt_drv_pwm]增加API:增加单独设置PWM频率和脉宽的函数
上级
78faea4b
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
133 addition
and
0 deletion
+133
-0
bsp/stm32/libraries/HAL_Drivers/drv_pwm.c
bsp/stm32/libraries/HAL_Drivers/drv_pwm.c
+95
-0
components/drivers/include/drivers/rt_drv_pwm.h
components/drivers/include/drivers/rt_drv_pwm.h
+4
-0
components/drivers/misc/rt_drv_pwm.c
components/drivers/misc/rt_drv_pwm.c
+34
-0
未找到文件。
bsp/stm32/libraries/HAL_Drivers/drv_pwm.c
浏览文件 @
b2bc0dcd
...
...
@@ -340,6 +340,97 @@ static rt_err_t drv_pwm_set(TIM_HandleTypeDef *htim, struct rt_pwm_configuration
return
RT_EOK
;
}
static
rt_err_t
drv_pwm_set_period
(
TIM_HandleTypeDef
*
htim
,
struct
rt_pwm_configuration
*
configuration
)
{
rt_uint32_t
period
;
rt_uint64_t
tim_clock
,
psc
;
rt_uint32_t
pclk1_doubler
,
pclk2_doubler
;
pclkx_doubler_get
(
&
pclk1_doubler
,
&
pclk2_doubler
);
#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) || defined(SOC_SERIES_STM32H7)|| defined(SOC_SERIES_STM32F3)
if
(
htim
->
Instance
==
TIM15
||
htim
->
Instance
==
TIM16
||
htim
->
Instance
==
TIM17
)
#elif defined(SOC_SERIES_STM32MP1)
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
=
(
rt_uint32_t
)(
HAL_RCC_GetPCLK2Freq
()
*
pclk2_doubler
);
#endif
}
else
{
tim_clock
=
(
rt_uint32_t
)(
HAL_RCC_GetPCLK1Freq
()
*
pclk1_doubler
);
}
/* Convert nanosecond to frequency and duty cycle. 1s = 1 * 1000 * 1000 * 1000 ns */
tim_clock
/=
1000000UL
;
period
=
(
unsigned
long
long
)
configuration
->
period
*
tim_clock
/
1000ULL
;
psc
=
period
/
MAX_PERIOD
+
1
;
period
=
period
/
psc
;
__HAL_TIM_SET_PRESCALER
(
htim
,
psc
-
1
);
if
(
period
<
MIN_PERIOD
)
{
period
=
MIN_PERIOD
;
}
__HAL_TIM_SET_AUTORELOAD
(
htim
,
period
-
1
);
return
RT_EOK
;
}
static
rt_err_t
drv_pwm_set_pulse
(
TIM_HandleTypeDef
*
htim
,
struct
rt_pwm_configuration
*
configuration
)
{
rt_uint32_t
period
,
pulse
;
rt_uint64_t
tim_clock
;
rt_uint32_t
pclk1_doubler
,
pclk2_doubler
;
/* Converts the channel number to the channel number of Hal library */
rt_uint32_t
channel
=
0x04
*
(
configuration
->
channel
-
1
);
pclkx_doubler_get
(
&
pclk1_doubler
,
&
pclk2_doubler
);
#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) || defined(SOC_SERIES_STM32H7)|| defined(SOC_SERIES_STM32F3)
if
(
htim
->
Instance
==
TIM15
||
htim
->
Instance
==
TIM16
||
htim
->
Instance
==
TIM17
)
#elif defined(SOC_SERIES_STM32MP1)
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
=
(
rt_uint32_t
)(
HAL_RCC_GetPCLK2Freq
()
*
pclk2_doubler
);
#endif
}
else
{
tim_clock
=
(
rt_uint32_t
)(
HAL_RCC_GetPCLK1Freq
()
*
pclk1_doubler
);
}
/* Convert nanosecond to frequency and duty cycle. 1s = 1 * 1000 * 1000 * 1000 ns */
tim_clock
/=
1000000UL
;
period
=
(
__HAL_TIM_GET_AUTORELOAD
(
htim
)
+
1
)
*
(
htim
->
Instance
->
PSC
+
1
)
*
1000UL
/
tim_clock
;
pulse
=
(
unsigned
long
long
)
configuration
->
pulse
*
(
__HAL_TIM_GET_AUTORELOAD
(
htim
)
+
1
)
/
period
;
if
(
pulse
<
MIN_PULSE
)
{
pulse
=
MIN_PULSE
;
}
else
if
(
pulse
>
period
)
{
pulse
=
period
;
}
__HAL_TIM_SET_COMPARE
(
htim
,
channel
,
pulse
-
1
);
return
RT_EOK
;
}
static
rt_err_t
drv_pwm_control
(
struct
rt_device_pwm
*
device
,
int
cmd
,
void
*
arg
)
{
struct
rt_pwm_configuration
*
configuration
=
(
struct
rt_pwm_configuration
*
)
arg
;
...
...
@@ -357,6 +448,10 @@ static rt_err_t drv_pwm_control(struct rt_device_pwm *device, int cmd, void *arg
return
drv_pwm_enable
(
htim
,
configuration
,
RT_FALSE
);
case
PWM_CMD_SET
:
return
drv_pwm_set
(
htim
,
configuration
);
case
PWM_CMD_SET_PERIOD
:
return
drv_pwm_set_period
(
htim
,
configuration
);
case
PWM_CMD_SET_PULSE
:
return
drv_pwm_set_pulse
(
htim
,
configuration
);
case
PWM_CMD_GET
:
return
drv_pwm_get
(
htim
,
configuration
);
default:
...
...
components/drivers/include/drivers/rt_drv_pwm.h
浏览文件 @
b2bc0dcd
...
...
@@ -19,6 +19,8 @@
#define PWM_CMD_GET (RT_DEVICE_CTRL_BASE(PWM) + 3)
#define PWMN_CMD_ENABLE (RT_DEVICE_CTRL_BASE(PWM) + 4)
#define PWMN_CMD_DISABLE (RT_DEVICE_CTRL_BASE(PWM) + 5)
#define PWM_CMD_SET_PERIOD (RT_DEVICE_CTRL_BASE(PWM) + 6)
#define PWM_CMD_SET_PULSE (RT_DEVICE_CTRL_BASE(PWM) + 7)
struct
rt_pwm_configuration
{
...
...
@@ -50,5 +52,7 @@ rt_err_t rt_device_pwm_register(struct rt_device_pwm *device, const char *name,
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_set
(
struct
rt_device_pwm
*
device
,
int
channel
,
rt_uint32_t
period
,
rt_uint32_t
pulse
);
rt_err_t
rt_pwm_set_period
(
struct
rt_device_pwm
*
device
,
int
channel
,
rt_uint32_t
period
);
rt_err_t
rt_pwm_set_pulse
(
struct
rt_device_pwm
*
device
,
int
channel
,
rt_uint32_t
pulse
);
#endif
/* __DRV_PWM_H_INCLUDE__ */
components/drivers/misc/rt_drv_pwm.c
浏览文件 @
b2bc0dcd
...
...
@@ -177,6 +177,40 @@ rt_err_t rt_pwm_set(struct rt_device_pwm *device, int channel, rt_uint32_t perio
return
result
;
}
rt_err_t
rt_pwm_set_period
(
struct
rt_device_pwm
*
device
,
int
channel
,
rt_uint32_t
period
)
{
rt_err_t
result
=
RT_EOK
;
struct
rt_pwm_configuration
configuration
=
{
0
};
if
(
!
device
)
{
return
-
RT_EIO
;
}
configuration
.
channel
=
channel
;
configuration
.
period
=
period
;
result
=
rt_device_control
(
&
device
->
parent
,
PWM_CMD_SET_PERIOD
,
&
configuration
);
return
result
;
}
rt_err_t
rt_pwm_set_pulse
(
struct
rt_device_pwm
*
device
,
int
channel
,
rt_uint32_t
pulse
)
{
rt_err_t
result
=
RT_EOK
;
struct
rt_pwm_configuration
configuration
=
{
0
};
if
(
!
device
)
{
return
-
RT_EIO
;
}
configuration
.
channel
=
channel
;
configuration
.
pulse
=
pulse
;
result
=
rt_device_control
(
&
device
->
parent
,
PWM_CMD_SET_PULSE
,
&
configuration
);
return
result
;
}
rt_err_t
rt_pwm_get
(
struct
rt_device_pwm
*
device
,
struct
rt_pwm_configuration
*
cfg
)
{
rt_err_t
result
=
RT_EOK
;
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录