Quellcode durchsuchen

优化互补PWM输出功能的实现逻辑,兼容原本API,增加互补PWM使能/失能命令

Trisuborn vor 4 Jahren
Ursprung
Commit
55402e19db

+ 4 - 0
bsp/stm32/libraries/HAL_Drivers/drv_pwm.c

@@ -309,8 +309,12 @@ 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:

+ 4 - 2
components/drivers/include/drivers/rt_drv_pwm.h

@@ -18,6 +18,8 @@
 #define PWM_CMD_DISABLE     (128 + 1)
 #define PWM_CMD_SET         (128 + 2)
 #define PWM_CMD_GET         (128 + 3)
+#define PWMN_CMD_ENABLE     (128 + 4)
+#define PWMN_CMD_DISABLE    (128 + 5)/
 
 struct rt_pwm_configuration
 {
@@ -46,8 +48,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_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_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);
 
 #endif /* __DRV_PWM_H_INCLUDE__ */

+ 18 - 16
components/drivers/misc/rt_drv_pwm.c

@@ -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_uint8_t complementary)
+rt_err_t rt_pwm_enable(struct rt_device_pwm *device, int channel)
 {
     rt_err_t result = RT_EOK;
     struct rt_pwm_configuration configuration = {0};
@@ -136,14 +136,14 @@ rt_err_t rt_pwm_enable(struct rt_device_pwm *device, int channel, rt_uint8_t com
         return -RT_EIO;
     }
 
-    configuration.channel = channel;
-    configuration.complementary = complementary ? (RT_TRUE) : (RT_FALSE);
+    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 */
     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_uint8_t complementary)
+rt_err_t rt_pwm_disable(struct rt_device_pwm *device, int channel)
 {
     rt_err_t result = RT_EOK;
     struct rt_pwm_configuration configuration = {0};
@@ -153,8 +153,8 @@ rt_err_t rt_pwm_disable(struct rt_device_pwm *device, int channel, rt_uint8_t co
         return -RT_EIO;
     }
 
-    configuration.channel = channel;
-    configuration.complementary = complementary ? (RT_TRUE) : (RT_FALSE);
+    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 */
     result = rt_device_control(&device->parent, PWM_CMD_DISABLE, &configuration);
 
     return result;
@@ -204,10 +204,10 @@ static int pwm_enable(int argc, char **argv)
     int result = 0;
     struct rt_device_pwm *device = RT_NULL;
 
-    if (argc != 4)
+    if (argc != 3)
     {
-        rt_kprintf("Usage: pwm_enable pwm1 1 1\n");
-        rt_kprintf("       pwm_enable <pwm_dev> <channel> <complementary> \n");
+        rt_kprintf("Usage: pwm_enable pwm1 1\n");
+        rt_kprintf("       pwm_enable <pwm_dev> <channel/-channel>\n");
         result = -RT_ERROR;
         goto _exit;
     }
@@ -219,22 +219,23 @@ static int pwm_enable(int argc, char **argv)
         goto _exit;
     }
 
-    result = rt_pwm_enable(device, atoi(argv[2]), atoi(argv[3]));
+    /* If channel is complementary(1), make the channel number to nagetive */
+    result = rt_pwm_enable(device, atoi(argv[2]));
 
 _exit:
     return result;
 }
-MSH_CMD_EXPORT(pwm_enable, pwm_enable <pwm_dev> <channel> <complementary>);
+MSH_CMD_EXPORT(pwm_enable, pwm_enable <pwm_dev> <channel/-channel>);
 
 static int pwm_disable(int argc, char **argv)
 {
     int result = 0;
     struct rt_device_pwm *device = RT_NULL;
 
-    if (argc != 4)
+    if (argc != 3)
     {
-        rt_kprintf("Usage: pwm_enable pwm1 1 1\n");
-        rt_kprintf("       pwm_disable <pwm_dev> <channel> <complementary> \n");
+        rt_kprintf("Usage: pwm_disable pwm1 1\n");
+        rt_kprintf("       pwm_disable <pwm_dev> <channel/-channel> \n");
         result = -RT_ERROR;
         goto _exit;
     }
@@ -246,12 +247,13 @@ static int pwm_disable(int argc, char **argv)
         goto _exit;
     }
 
-    result = rt_pwm_disable(device, atoi(argv[2]), atoi(argv[3]));
+    /* If channel is complementary(1), make the channel number to nagetive */
+    result = rt_pwm_disable(device, atoi(argv[2]));
 
 _exit:
     return result;
 }
-MSH_CMD_EXPORT(pwm_disable, pwm_disable <pwm_dev> <channel> <complementary>);
+MSH_CMD_EXPORT(pwm_disable, pwm_disable <pwm_dev> <channel/-channel>);
 
 static int pwm_set(int argc, char **argv)
 {