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

* add finsh pwm function
This commit is contained in:
Stanley Lwin 2022-05-18 19:18:55 -07:00 committed by GitHub
parent 08737c2a26
commit 3ecfe9c61e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 96 additions and 113 deletions

View File

@ -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 * 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
#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;
if (argc != 3)
{
rt_kprintf("Usage: pwm_enable pwm1 1\n");
rt_kprintf(" pwm_enable <pwm_dev> <channel/-channel>\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 <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");
rt_kprintf(" pwm_disable <pwm_dev> <channel/-channel> \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 <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("Usage: pwm_set <pwm_dev> <channel> <period> <pulse>\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 <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}; struct rt_pwm_configuration cfg = {0};
if (argc != 3) if(argc > 1)
{ {
rt_kprintf("Usage: pwm_get pwm1 1\n"); if(!strcmp(argv[1], "probe"))
rt_kprintf(" pwm_get <pwm_dev> <channel>\n");
result = -RT_ERROR;
goto _exit;
}
device = (struct rt_device_pwm *)rt_device_find(argv[1]);
if (!device)
{ {
result = -RT_EIO; if(argc == 3)
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]); 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 else
{ {
rt_kprintf("Get info of device: [%s]:\n", argv[1]); rt_kprintf("pwm probe <device name> - probe pwm by name\n");
}
}
else
{
if(pwm_device == RT_NULL)
{
rt_kprintf("Please using 'pwm probe <device name>' 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 <channel> - 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 <channel> - 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("period : %d\n", cfg.period);
rt_kprintf("pulse : %d\n", cfg.pulse); rt_kprintf("pulse : %d\n", cfg.pulse);
rt_kprintf("Duty cycle : %d%%\n",(int)(((double)(cfg.pulse)/(cfg.period)) * 100)); rt_kprintf("Duty cycle : %d%%\n",(int)(((double)(cfg.pulse)/(cfg.period)) * 100));
} }
else
_exit: {
return result; rt_kprintf("Get info of device: [%s] error.\n", pwm_device);
} }
MSH_CMD_EXPORT(pwm_get, pwm_get <pwm_dev> <channel>); }
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");
}
}
else
{
rt_kprintf("pwm get <channel> - get pwm channel info\n");
}
}
}
else
{
rt_kprintf("Usage: \n");
rt_kprintf("pwm probe <device name> - probe pwm by name\n");
rt_kprintf("pwm enable <channel> - enable pwm channel\n");
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;
}
return RT_EOK;
}
MSH_CMD_EXPORT(pwm, pwm <device name> <option> <channel>);
#endif /* RT_USING_FINSH */ #endif /* RT_USING_FINSH */