summaryrefslogtreecommitdiff
path: root/components/drivers
diff options
context:
space:
mode:
author1ridic <[email protected]>2023-12-23 14:47:25 +0800
committerMeco Man <[email protected]>2023-12-25 12:07:49 +0800
commit3da0b76add0894e0edee9d8f658326a2090bed80 (patch)
treec6e2947960a26c840a8f192c7108de61cf7830b9 /components/drivers
parent00bb3c62246656587e0518c43bc3bf2a3b2b19f5 (diff)
[pwm] Add second-level command completion
Diffstat (limited to 'components/drivers')
-rw-r--r--components/drivers/misc/rt_drv_pwm.c220
1 files changed, 118 insertions, 102 deletions
diff --git a/components/drivers/misc/rt_drv_pwm.c b/components/drivers/misc/rt_drv_pwm.c
index af87a5b60e..68f0cab18e 100644
--- a/components/drivers/misc/rt_drv_pwm.c
+++ b/components/drivers/misc/rt_drv_pwm.c
@@ -10,6 +10,7 @@
* 2022-07-25 liYony fix complementary outputs and add usage information in finsh
* 2022-08-31 liYony Add complementary output section to framework for management
* 2022-09-24 qiyu Add dead-time and phase configuration
+ * 2023-12-23 1ridic Add second-level command completion
*/
#include <rtdevice.h>
@@ -22,22 +23,21 @@ static rt_err_t _pwm_control(rt_device_t dev, int cmd, void *args)
switch (cmd)
{
- case PWMN_CMD_ENABLE:
- configuration->complementary = RT_TRUE;
- break;
- case PWMN_CMD_DISABLE:
- configuration->complementary = RT_FALSE;
- break;
- default:
- if(pwm->ops->control)
- result = pwm->ops->control(pwm, cmd, args);
- break;
+ case PWMN_CMD_ENABLE:
+ configuration->complementary = RT_TRUE;
+ break;
+ case PWMN_CMD_DISABLE:
+ configuration->complementary = RT_FALSE;
+ break;
+ default:
+ if (pwm->ops->control)
+ result = pwm->ops->control(pwm, cmd, args);
+ break;
}
return result;
}
-
/*
pos: channel
void *buffer: rt_uint32_t pulse[size]
@@ -153,7 +153,7 @@ rt_err_t rt_pwm_enable(struct rt_device_pwm *device, int channel)
/* If channel is a positive number (0 ~ n), it means using normal output pin.
* If channel is a negative number (0 ~ -n), it means using complementary output pin. */
- if(channel > 0)
+ if (channel > 0)
{
result = rt_device_control(&device->parent, PWMN_CMD_DISABLE, &configuration);
}
@@ -182,7 +182,7 @@ rt_err_t rt_pwm_disable(struct rt_device_pwm *device, int channel)
/* If channel is a positive number (0 ~ n), it means using normal output pin.
* If channel is a negative number (0 ~ -n), it means using complementary output pin. */
- if(channel > 0)
+ if (channel > 0)
{
result = rt_device_control(&device->parent, PWMN_CMD_DISABLE, &configuration);
}
@@ -301,18 +301,30 @@ rt_err_t rt_pwm_get(struct rt_device_pwm *device, struct rt_pwm_configuration *c
#include <string.h>
#include <finsh.h>
-static int pwm(int argc, char **argv)
+static enum pwm_list_parameters
+{
+ PWM_LIST_PROBE = 1,
+ PWM_LIST_ENABLE,
+ PWM_LIST_DISABLE,
+ PWM_LIST_GET,
+ PWM_LIST_SET,
+ PWM_LIST_PHASE,
+ PWM_LIST_DEAD_TIME,
+} pwm_list_parameters;
+
+CMD_OPTIONS_STATEMENT(pwm_list)
+int pwm_list(int argc, char **argv)
{
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 > 1)
+ if (argc > 1)
{
- if(!strcmp(argv[1], "probe"))
+ if (MSH_OPT_ID_GET(pwm_list) == PWM_LIST_PROBE)
{
- if(argc == 3)
+ if (argc == 3)
{
pwm_device = (struct rt_device_pwm *)rt_device_find(argv[2]);
result_str = (pwm_device == RT_NULL) ? "failure" : "success";
@@ -323,115 +335,119 @@ static int pwm(int argc, char **argv)
rt_kprintf("pwm probe <device name> - probe pwm by name\n");
}
}
- else
+ else if (pwm_device == RT_NULL)
{
- if(pwm_device == RT_NULL)
+ rt_kprintf("Please using 'pwm probe <device name>' first.\n");
+ return -RT_ERROR;
+ }
+
+ switch (MSH_OPT_ID_GET(pwm_list))
+ {
+ case PWM_LIST_ENABLE:
+ if (argc == 3)
{
- rt_kprintf("Please using 'pwm probe <device name>' first.\n");
- return -RT_ERROR;
+ 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);
}
- if(!strcmp(argv[1], "enable"))
+ else
{
- 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");
- rt_kprintf(" e.g. MSH >pwm enable 1 - PWM_CH1 nomal\n");
- rt_kprintf(" e.g. MSH >pwm enable -1 - PWM_CH1N complememtary\n");
- }
+ rt_kprintf("pwm enable <channel> - enable pwm channel\n");
+ rt_kprintf(" e.g. MSH >pwm enable 1 - PWM_CH1 nomal\n");
+ rt_kprintf(" e.g. MSH >pwm enable -1 - PWM_CH1N complememtary\n");
}
- else if(!strcmp(argv[1], "disable"))
+ break;
+
+ case PWM_LIST_DISABLE:
+ if (argc == 3)
{
- if(argc == 3)
- {
- result = rt_pwm_disable(pwm_device, atoi(argv[2]));
- }
- else
- {
- rt_kprintf("pwm disable <channel> - disable pwm channel\n");
- }
+ result = rt_pwm_disable(pwm_device, atoi(argv[2]));
}
- else if(!strcmp(argv[1], "get"))
+ else
{
- 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);
- }
+ rt_kprintf("pwm disable <channel> - disable pwm channel\n");
}
- else if (!strcmp(argv[1], "set"))
+ break;
+
+ case PWM_LIST_GET:
+ cfg.channel = atoi(argv[2]);
+ result = rt_pwm_get(pwm_device, &cfg);
+ if (result == RT_EOK)
{
- 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,(rt_base_t)atoi(argv[2]));
- }
- else
- {
- rt_kprintf("Set info of device: [%s] error\n", pwm_device);
- rt_kprintf("Usage: pwm set <channel> <period> <pulse>\n");
- }
+ 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 if(!strcmp(argv[1], "phase"))
+ else
{
- if(argc == 4)
- {
- result = rt_pwm_set_phase(pwm_device, atoi(argv[2]),atoi(argv[3]));
- result_str = (result == RT_EOK) ? "success" : "failure";
- rt_kprintf("%s phase is set %d \n", pwm_device->parent.parent.name, (rt_base_t)atoi(argv[3]));
- }
+ rt_kprintf("Get info of device: [%s] error.\n", pwm_device);
}
- else if(!strcmp(argv[1], "dead_time"))
+ break;
+
+ case PWM_LIST_SET:
+ if (argc == 5)
{
- if(argc == 4)
- {
- result = rt_pwm_set_dead_time(pwm_device, atoi(argv[2]),atoi(argv[3]));
- result_str = (result == RT_EOK) ? "success" : "failure";
- rt_kprintf("%s dead_time is set %d \n", pwm_device->parent.parent.name, (rt_base_t)atoi(argv[3]));
- }
+ 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, (rt_base_t)atoi(argv[2]));
}
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");
- rt_kprintf("pwm phase <channel> <phase> - set pwm phase\n");
- rt_kprintf("pwm dead_time <channel> <dead_time> - set pwm dead time\n");
- result = -RT_ERROR;
+ rt_kprintf("Set info of device: [%s] error\n", pwm_device);
+ rt_kprintf("Usage: pwm set <channel> <period> <pulse>\n");
+ }
+ break;
+
+ case PWM_LIST_PHASE:
+ if (argc == 4)
+ {
+ result = rt_pwm_set_phase(pwm_device, atoi(argv[2]), atoi(argv[3]));
+ result_str = (result == RT_EOK) ? "success" : "failure";
+ rt_kprintf("%s phase is set %d \n", pwm_device->parent.parent.name, (rt_base_t)atoi(argv[3]));
+ }
+ break;
+
+ case PWM_LIST_DEAD_TIME:
+ if (argc == 4)
+ {
+ result = rt_pwm_set_dead_time(pwm_device, atoi(argv[2]), atoi(argv[3]));
+ result_str = (result == RT_EOK) ? "success" : "failure";
+ rt_kprintf("%s dead_time is set %d \n", pwm_device->parent.parent.name, (rt_base_t)atoi(argv[3]));
}
+ break;
+
+ default:
+ goto _usage;
+ break;
}
}
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");
- rt_kprintf("pwm phase <channel> <phase> - set pwm phase\n");
- rt_kprintf("pwm dead_time <channel> <dead_time> - set pwm dead time\n");
- result = -RT_ERROR;
+ goto _usage;
}
+ return result;
- return RT_EOK;
+_usage:
+ 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");
+ rt_kprintf("pwm phase <channel> <phase> - set pwm phase\n");
+ rt_kprintf("pwm dead_time <channel> <dead_time> - set pwm dead time\n");
+ result = -RT_ERROR;
+ return result;
}
-MSH_CMD_EXPORT(pwm, pwm [option]);
+CMD_OPTIONS_NODE_START(pwm_list)
+CMD_OPTIONS_NODE(PWM_LIST_PROBE, probe, probe pwm by name)
+CMD_OPTIONS_NODE(PWM_LIST_ENABLE, enable, enable pwm channel)
+CMD_OPTIONS_NODE(PWM_LIST_DISABLE, disable, disable pwm channel)
+CMD_OPTIONS_NODE(PWM_LIST_GET, get, get pwm channel info)
+CMD_OPTIONS_NODE(PWM_LIST_SET, set, set pwm channel info)
+CMD_OPTIONS_NODE(PWM_LIST_PHASE, phase, set pwm phase)
+CMD_OPTIONS_NODE(PWM_LIST_DEAD_TIME, dead_time, set pwm dead time)
+CMD_OPTIONS_NODE_END
+MSH_CMD_EXPORT_ALIAS(pwm_list, pwm, control pwm device, optenable);
#endif /* RT_USING_FINSH */