未验证 提交 3ecfe9c6 编写于 作者: S Stanley Lwin 提交者: GitHub

[rt_drv_pwm.c] add finsh pwm function (#5944)

* add finsh pwm function
上级 08737c2a
/* /*
* Copyright (c) 2006-2021, RT-Thread Development Team * Copyright (c) 2006-2022, RT-Thread Development Team
* *
* SPDX-License-Identifier: Apache-2.0 * SPDX-License-Identifier: Apache-2.0
* *
* Change Logs: * Change Logs:
* Date Author Notes * Date Author Notes
* 2018-05-07 aozima the first version * 2018-05-07 aozima the first version
* 2022-05-14 Stanley Lwin add pwm function
*/ */
#include <drivers/rt_drv_pwm.h> #include <drivers/rt_drv_pwm.h>
...@@ -192,130 +193,112 @@ rt_err_t rt_pwm_get(struct rt_device_pwm *device, struct rt_pwm_configuration *c ...@@ -192,130 +193,112 @@ rt_err_t rt_pwm_get(struct rt_device_pwm *device, struct rt_pwm_configuration *c
#ifdef RT_USING_FINSH #ifdef RT_USING_FINSH
#include <stdlib.h> #include <stdlib.h>
#include <string.h>
#include <finsh.h> #include <finsh.h>
static int pwm_enable(int argc, char **argv) static int pwm(int argc, char **argv)
{ {
int result = 0; rt_err_t result = -RT_ERROR;
struct rt_device_pwm *device = RT_NULL; char *result_str;
static struct rt_device_pwm *pwm_device = RT_NULL;
struct rt_pwm_configuration cfg = {0};
if (argc != 3) if(argc > 1)
{
if(!strcmp(argv[1], "probe"))
{
if(argc == 3)
{ {
rt_kprintf("Usage: pwm_enable pwm1 1\n"); pwm_device = (struct rt_device_pwm *)rt_device_find(argv[2]);
rt_kprintf(" pwm_enable <pwm_dev> <channel/-channel>\n"); result_str = (pwm_device == RT_NULL) ? "failure" : "success";
result = -RT_ERROR; rt_kprintf("probe %s %s\n", argv[2], result_str);
goto _exit;
} }
else
device = (struct rt_device_pwm *)rt_device_find(argv[1]);
if (!device)
{ {
result = -RT_EIO; rt_kprintf("pwm probe <device name> - probe pwm by name\n");
goto _exit;
} }
}
/* If channel is complementary(1), make the channel number to nagetive */ else
result = rt_pwm_enable(device, atoi(argv[2]));
_exit:
return result;
}
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 != 3)
{ {
rt_kprintf("Usage: pwm_disable pwm1 1\n"); if(pwm_device == RT_NULL)
rt_kprintf(" pwm_disable <pwm_dev> <channel/-channel> \n"); {
result = -RT_ERROR; rt_kprintf("Please using 'pwm probe <device name>' first.\n");
goto _exit; return -RT_ERROR;
} }
if(!strcmp(argv[1], "enable"))
device = (struct rt_device_pwm *)rt_device_find(argv[1]); {
if (!device) if(argc == 3)
{ {
result = -RT_EIO; result = rt_pwm_enable(pwm_device, atoi(argv[2]));
goto _exit; 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
/* 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/-channel>);
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("pwm enable <channel> - enable pwm channel\n");
rt_kprintf("Usage: pwm_set <pwm_dev> <channel> <period> <pulse>\n");
result = -RT_ERROR;
goto _exit;
} }
}
device = (struct rt_device_pwm *)rt_device_find(argv[1]); else if(!strcmp(argv[1], "disable"))
if (!device)
{ {
result = -RT_EIO; if(argc == 3)
goto _exit; {
result = rt_pwm_disable(pwm_device, atoi(argv[2]));
} }
else
result = rt_pwm_set(device, atoi(argv[2]), atoi(argv[3]), atoi(argv[4]));
_exit:
return result;
}
MSH_CMD_EXPORT(pwm_set, pwm_set <pwm_dev> <channel> <period> <pulse>);
static int pwm_get(int argc, char **argv)
{
int result = 0;
struct rt_device_pwm *device = RT_NULL;
struct rt_pwm_configuration cfg = {0};
if (argc != 3)
{ {
rt_kprintf("Usage: pwm_get pwm1 1\n"); rt_kprintf("pwm disable <channel> - disable pwm channel\n");
rt_kprintf(" pwm_get <pwm_dev> <channel>\n");
result = -RT_ERROR;
goto _exit;
} }
}
device = (struct rt_device_pwm *)rt_device_find(argv[1]); else if(!strcmp(argv[1], "get"))
if (!device) {
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
{ {
result = -RT_EIO; rt_kprintf("Get info of device: [%s] error.\n", pwm_device);
goto _exit; }
}
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 <channel> <period> <pulse>\n");
}
} }
cfg.channel = atoi(argv[2]); else
result = rt_pwm_get(device, &cfg);
if (result != RT_EOK)
{ {
rt_kprintf("Get info of device: [%s] error.\n", argv[1]); rt_kprintf("pwm get <channel> - get pwm channel info\n");
}
}
} }
else else
{ {
rt_kprintf("Get info of device: [%s]:\n", argv[1]); rt_kprintf("Usage: \n");
rt_kprintf("period : %d\n", cfg.period); rt_kprintf("pwm probe <device name> - probe pwm by name\n");
rt_kprintf("pulse : %d\n", cfg.pulse); rt_kprintf("pwm enable <channel> - enable pwm channel\n");
rt_kprintf("Duty cycle : %d%%\n", (int)(((double)(cfg.pulse)/(cfg.period)) * 100)); rt_kprintf("pwm disable <channel>- disable pwm channel\n");
rt_kprintf("pwm get <channel> - get pwm channel info\n");
rt_kprintf("pwm set <channel> <period> <pulse> - set pwm channel info\n");
result = - RT_ERROR;
} }
_exit: return RT_EOK;
return result;
} }
MSH_CMD_EXPORT(pwm_get, pwm_get <pwm_dev> <channel>); MSH_CMD_EXPORT(pwm, pwm <device name> <option> <channel>);
#endif /* RT_USING_FINSH */ #endif /* RT_USING_FINSH */
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册