From 0e5b4a0ff220cfb83086e28c5a4b93ca6e63bbac Mon Sep 17 00:00:00 2001 From: aozima Date: Fri, 8 Jun 2018 16:04:29 +0800 Subject: [PATCH] [DeviceDrivers] export pwm_enable/set to shell. --- components/drivers/misc/_pwm.c | 101 +++++++++++++++++++++++++-------- 1 file changed, 76 insertions(+), 25 deletions(-) diff --git a/components/drivers/misc/_pwm.c b/components/drivers/misc/_pwm.c index 82d0ca523a..fc5b9b5976 100644 --- a/components/drivers/misc/_pwm.c +++ b/components/drivers/misc/_pwm.c @@ -36,9 +36,9 @@ static rt_err_t _pwm_control(rt_device_t dev, int cmd, void *args) if (pwm->ops->control) { result = pwm->ops->control(pwm, cmd, args); - } + } - return result; + return result; } @@ -52,9 +52,9 @@ static rt_size_t _pwm_read(rt_device_t dev, rt_off_t pos, void *buffer, rt_size_ rt_err_t result = RT_EOK; struct rt_device_pwm *pwm = (struct rt_device_pwm *)dev; rt_uint32_t *pulse = (rt_uint32_t *)buffer; - struct rt_pwm_configuration configuration={0}; - - configuration.channel = pos; + struct rt_pwm_configuration configuration = {0}; + + configuration.channel = pos; if (pwm->ops->control) { @@ -80,9 +80,9 @@ static rt_size_t _pwm_write(rt_device_t dev, rt_off_t pos, const void *buffer, r rt_err_t result = RT_EOK; struct rt_device_pwm *pwm = (struct rt_device_pwm *)dev; rt_uint32_t *pulse = (rt_uint32_t *)buffer; - struct rt_pwm_configuration configuration={0}; + struct rt_pwm_configuration configuration = {0}; - configuration.channel = pos; + configuration.channel = pos; if (pwm->ops->control) { @@ -131,33 +131,84 @@ rt_err_t rt_device_pwm_register(struct rt_device_pwm *device, const char *name, rt_err_t rt_pwm_enable(int channel) { rt_err_t result = RT_EOK; - struct rt_device *device = rt_device_find("pwm"); - struct rt_pwm_configuration configuration={0}; + struct rt_device *device = rt_device_find("pwm"); + struct rt_pwm_configuration configuration = {0}; - if(!device) - { - return -RT_EIO; - } - - configuration.channel = channel; + if (!device) + { + return -RT_EIO; + } + + configuration.channel = channel; result = rt_device_control(device, PWM_CMD_ENABLE, &configuration); - return result; + return result; } rt_err_t rt_pwm_set(int channel, rt_uint32_t period, rt_uint32_t pulse) { rt_err_t result = RT_EOK; - struct rt_device *pwm = rt_device_find("pwm"); - - if(!pwm) - { - return -RT_EIO; - } + struct rt_device *device = rt_device_find("pwm"); + struct rt_pwm_configuration configuration = {0}; - return result; + if (!device) + { + return -RT_EIO; + } + + configuration.channel = channel; + configuration.period = period; + configuration.pulse = pulse; + result = rt_device_control(device, PWM_CMD_SET, &configuration); + + return result; } -#ifdef finsh -#endif /**/ +#ifdef RT_USING_FINSH +#include + +FINSH_FUNCTION_EXPORT_ALIAS(rt_pwm_enable, pwm_enable, enable pwm by channel.); +FINSH_FUNCTION_EXPORT_ALIAS(rt_pwm_set, pwm_set, set pwm.); + +#ifdef FINSH_USING_MSH +static int pwm_enable(int argc, char **argv) +{ + int result = 0; + + if (argc != 2) + { + rt_kprintf("Usage: pwm_enable 1\n"); + result = -RT_ERROR; + goto _exit; + } + + result = rt_pwm_enable(atoi(argv[1])); + +_exit: + return result; +} +MSH_CMD_EXPORT(pwm_enable, pwm_enable 1); + +static int pwm_set(int argc, char **argv) +{ + int result = 0; + + if (argc != 4) + { + rt_kprintf("Usage: pwm_set 1 100 50\n"); + result = -RT_ERROR; + goto _exit; + } + + result = rt_pwm_set(atoi(argv[1]), atoi(argv[2]), atoi(argv[3])); + +_exit: + return result; +} +MSH_CMD_EXPORT(pwm_set, pwm_set 1 100 50); + +#endif /* FINSH_USING_MSH */ + + +#endif /* RT_USING_FINSH */