summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorliYangYang <[email protected]>2022-09-01 12:59:44 +0800
committerGitHub <[email protected]>2022-09-01 00:59:44 -0400
commitd25bf469fa0c38377cdd987679c9faf841dca3c8 (patch)
tree19ecc11487d7b3357301adbd2e7da1d23aa12a25
parent6ac09a6db02e9772886d5f76163f534f959934cf (diff)
[rt_drv_pwm]完善PWM框架互补输出部分代码 (#6338)
* [pwm]Improve the code * 将与bsp无关的代码移植到框架部分 * 添加注释
-rw-r--r--bsp/stm32/libraries/HAL_Drivers/drv_pwm.c6
-rw-r--r--components/drivers/include/drivers/rt_drv_pwm.h2
-rw-r--r--components/drivers/misc/rt_drv_pwm.c47
3 files changed, 43 insertions, 12 deletions
diff --git a/bsp/stm32/libraries/HAL_Drivers/drv_pwm.c b/bsp/stm32/libraries/HAL_Drivers/drv_pwm.c
index ff875e8946..b74428d816 100644
--- a/bsp/stm32/libraries/HAL_Drivers/drv_pwm.c
+++ b/bsp/stm32/libraries/HAL_Drivers/drv_pwm.c
@@ -1,5 +1,5 @@
/*
- * Copyright (c) 2006-2021, RT-Thread Development Team
+ * Copyright (c) 2006-2022, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
@@ -383,12 +383,8 @@ static rt_err_t drv_pwm_control(struct rt_device_pwm *device, int cmd, void *arg
switch (cmd)
{
- case PWMN_CMD_ENABLE:
- configuration->complementary = RT_TRUE;
case PWM_CMD_ENABLE:
return drv_pwm_enable(htim, configuration, RT_TRUE);
- case PWMN_CMD_DISABLE:
- configuration->complementary = RT_FALSE;
case PWM_CMD_DISABLE:
return drv_pwm_enable(htim, configuration, RT_FALSE);
case PWM_CMD_SET:
diff --git a/components/drivers/include/drivers/rt_drv_pwm.h b/components/drivers/include/drivers/rt_drv_pwm.h
index 597842be79..1338d43ee8 100644
--- a/components/drivers/include/drivers/rt_drv_pwm.h
+++ b/components/drivers/include/drivers/rt_drv_pwm.h
@@ -24,7 +24,7 @@
struct rt_pwm_configuration
{
- rt_uint32_t channel; /* 1-n or 0-n, which depends on specific MCU requirements */
+ rt_uint32_t channel; /* 0 ~ n or 0 ~ -n, which depends on specific MCU requirements */
rt_uint32_t period; /* unit:ns 1ns~4.29s:1Ghz~0.23hz */
rt_uint32_t pulse; /* unit:ns (pulse<=period) */
diff --git a/components/drivers/misc/rt_drv_pwm.c b/components/drivers/misc/rt_drv_pwm.c
index bb599721b0..23a3283de4 100644
--- a/components/drivers/misc/rt_drv_pwm.c
+++ b/components/drivers/misc/rt_drv_pwm.c
@@ -8,6 +8,7 @@
* 2018-05-07 aozima the first version
* 2022-05-14 Stanley Lwin add pwm function
* 2022-07-25 liYony fix complementary outputs and add usage information in finsh
+ * 2022-08-31 liYony Add complementary output section to framework for management
*/
#include <rtdevice.h>
@@ -16,10 +17,20 @@ static rt_err_t _pwm_control(rt_device_t dev, int cmd, void *args)
{
rt_err_t result = RT_EOK;
struct rt_device_pwm *pwm = (struct rt_device_pwm *)dev;
+ struct rt_pwm_configuration *configuration = (struct rt_pwm_configuration *)args;
- if (pwm->ops->control)
+ switch (cmd)
{
- result = pwm->ops->control(pwm, cmd, args);
+ case PWMN_CMD_ENABLE:
+ configuration->complementary = RT_TRUE;
+ break;
+ case PWMN_CMD_DISABLE:
+ configuration->complementary = RT_FALSE;
+ break;
+ default:
+ if(pwm->ops->control)
+ result = pwm->ops->control(pwm, cmd, args);
+ break;
}
return result;
@@ -136,8 +147,20 @@ rt_err_t rt_pwm_enable(struct rt_device_pwm *device, int channel)
return -RT_EIO;
}
- configuration.channel = (channel > 0) ? (channel) : (-channel); /* Make it is positive num forever */
- configuration.complementary = (channel > 0) ? (RT_FALSE) : (RT_TRUE); /* If nagetive, it's complementary */
+ /* Make it is positive num forever */
+ configuration.channel = (channel > 0) ? (channel) : (-channel);
+
+ /* If channel is a positive number (0 ~ n), it means using normal output pin.
+ * If channel is a negative number (0 ~ -n), it means using complementary output pin. */
+ if(channel > 0)
+ {
+ result = rt_device_control(&device->parent, PWMN_CMD_DISABLE, &configuration);
+ }
+ else
+ {
+ result = rt_device_control(&device->parent, PWMN_CMD_ENABLE, &configuration);
+ }
+
result = rt_device_control(&device->parent, PWM_CMD_ENABLE, &configuration);
return result;
@@ -153,8 +176,20 @@ rt_err_t rt_pwm_disable(struct rt_device_pwm *device, int channel)
return -RT_EIO;
}
- configuration.channel = (channel > 0) ? (channel) : (-channel); /* Make it is positive num forever */
- configuration.complementary = (channel > 0) ? (RT_FALSE) : (RT_TRUE); /* If nagetive, it's complementary */
+ /* Make it is positive num forever */
+ configuration.channel = (channel > 0) ? (channel) : (-channel);
+
+ /* If channel is a positive number (0 ~ n), it means using normal output pin.
+ * If channel is a negative number (0 ~ -n), it means using complementary output pin. */
+ if(channel > 0)
+ {
+ result = rt_device_control(&device->parent, PWMN_CMD_DISABLE, &configuration);
+ }
+ else
+ {
+ result = rt_device_control(&device->parent, PWMN_CMD_ENABLE, &configuration);
+ }
+
result = rt_device_control(&device->parent, PWM_CMD_DISABLE, &configuration);
return result;