diff --git a/components/drivers/misc/rt_drv_pwm.c b/components/drivers/misc/rt_drv_pwm.c index 20bac08b82..2571eeb202 100644 --- a/components/drivers/misc/rt_drv_pwm.c +++ b/components/drivers/misc/rt_drv_pwm.c @@ -1,11 +1,12 @@ /* - * Copyright (c) 2006-2021, RT-Thread Development Team + * Copyright (c) 2006-2022, RT-Thread Development Team * * SPDX-License-Identifier: Apache-2.0 * * Change Logs: * Date Author Notes * 2018-05-07 aozima the first version + * 2022-05-14 Stanley Lwin add pwm function */ #include @@ -192,130 +193,112 @@ rt_err_t rt_pwm_get(struct rt_device_pwm *device, struct rt_pwm_configuration *c #ifdef RT_USING_FINSH #include +#include #include -static int pwm_enable(int argc, char **argv) +static int pwm(int argc, char **argv) { - int result = 0; - struct rt_device_pwm *device = RT_NULL; - - if (argc != 3) - { - rt_kprintf("Usage: pwm_enable pwm1 1\n"); - rt_kprintf(" pwm_enable \n"); - result = -RT_ERROR; - goto _exit; - } - - device = (struct rt_device_pwm *)rt_device_find(argv[1]); - if (!device) - { - result = -RT_EIO; - goto _exit; - } - - /* 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 ); - -static int pwm_disable(int argc, char **argv) -{ - int result = 0; - struct rt_device_pwm *device = RT_NULL; - - if (argc != 3) - { - rt_kprintf("Usage: pwm_disable pwm1 1\n"); - rt_kprintf(" pwm_disable \n"); - result = -RT_ERROR; - goto _exit; - } - - device = (struct rt_device_pwm *)rt_device_find(argv[1]); - if (!device) - { - result = -RT_EIO; - goto _exit; - } - - /* 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 ); - -static int pwm_set(int argc, char **argv) -{ - int result = 0; - struct rt_device_pwm *device = RT_NULL; - - if (argc != 5) - { - rt_kprintf("Usage: pwm_set pwm1 1 100 50\n"); - rt_kprintf("Usage: pwm_set \n"); - result = -RT_ERROR; - goto _exit; - } - - device = (struct rt_device_pwm *)rt_device_find(argv[1]); - if (!device) - { - result = -RT_EIO; - goto _exit; - } - - result = rt_pwm_set(device, atoi(argv[2]), atoi(argv[3]), atoi(argv[4])); - -_exit: - return result; -} -MSH_CMD_EXPORT(pwm_set, pwm_set ); - - -static int pwm_get(int argc, char **argv) -{ - int result = 0; - struct rt_device_pwm *device = RT_NULL; + rt_err_t result = -RT_ERROR; + char *result_str; + static struct rt_device_pwm *pwm_device = RT_NULL; struct rt_pwm_configuration cfg = {0}; - if (argc != 3) + if(argc > 1) { - rt_kprintf("Usage: pwm_get pwm1 1\n"); - rt_kprintf(" pwm_get \n"); - result = -RT_ERROR; - goto _exit; - } + if(!strcmp(argv[1], "probe")) + { + if(argc == 3) + { + pwm_device = (struct rt_device_pwm *)rt_device_find(argv[2]); + result_str = (pwm_device == RT_NULL) ? "failure" : "success"; + rt_kprintf("probe %s %s\n", argv[2], result_str); + } + else + { + rt_kprintf("pwm probe - probe pwm by name\n"); + } + } + else + { + if(pwm_device == RT_NULL) + { + rt_kprintf("Please using 'pwm probe ' first.\n"); + return -RT_ERROR; + } + if(!strcmp(argv[1], "enable")) + { + if(argc == 3) + { + result = rt_pwm_enable(pwm_device, atoi(argv[2])); + result_str = (result == RT_EOK) ? "success" : "failure"; + rt_kprintf("%s channel %d is enabled %s \n", pwm_device->parent.parent.name, atoi(argv[2]), result_str); + } + else + { + rt_kprintf("pwm enable - enable pwm channel\n"); + } + } + else if(!strcmp(argv[1], "disable")) + { + if(argc == 3) + { + result = rt_pwm_disable(pwm_device, atoi(argv[2])); + } + else + { + rt_kprintf("pwm disable - disable pwm channel\n"); + } + } + else if(!strcmp(argv[1], "get")) + { + cfg.channel = atoi(argv[2]); + result = rt_pwm_get(pwm_device, &cfg); + if(result == RT_EOK) + { + rt_kprintf("Info of device [%s] channel [%d]:\n",pwm_device, atoi(argv[2])); + 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)); + } + else + { + rt_kprintf("Get info of device: [%s] error.\n", pwm_device); + } + } + else if (!strcmp(argv[1], "set")) + { + if(argc == 5) + { + result = rt_pwm_set(pwm_device, atoi(argv[2]), atoi(argv[3]), atoi(argv[4])); + rt_kprintf("pwm info set on %s at channel %d\n",pwm_device,atoi(argv[2])); + } + else + { + rt_kprintf("Set info of device: [%s] error\n", pwm_device); + rt_kprintf("Usage: pwm set \n"); + } + } - 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("pwm get - get pwm channel info\n"); + } + } } 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)); + rt_kprintf("Usage: \n"); + rt_kprintf("pwm probe - probe pwm by name\n"); + rt_kprintf("pwm enable - enable pwm channel\n"); + rt_kprintf("pwm disable - disable pwm channel\n"); + rt_kprintf("pwm get - get pwm channel info\n"); + rt_kprintf("pwm set - set pwm channel info\n"); + + result = - RT_ERROR; } -_exit: - return result; + return RT_EOK; } -MSH_CMD_EXPORT(pwm_get, pwm_get ); +MSH_CMD_EXPORT(pwm, pwm