1 /*
2  * Copyright (C) 2021 Alibaba Group Holding Limited
3  */
4 
5 #include <stdint.h>
6 #include <aos/pwm.h>
7 #include <aos/kernel.h>
8 
9 #if AOS_COMP_CLI
10 #include "aos/cli.h"
11 #endif
12 
aos_pwm_cli_help(void)13 void aos_pwm_cli_help(void)
14 {
15     printf("help:\r\n");
16     printf("period[ns]\r\n");
17     printf("duty[ns]\r\n");
18     printf("duration[s]\r\n");
19     printf("polarity 0:normal 1:inverse\r\n");
20     printf("aos_pwm_example channel period duty polarity enable duration\r\n");
21 }
aos_pwm_cli_cmd(int argc,char ** argv)22 static void aos_pwm_cli_cmd(int argc, char **argv)
23 {
24     aos_status_t ret;
25     aos_pwm_ref_t ref;
26     uint32_t channel, period, duty_cycle, polarity, enabled, duration;
27     aos_pwm_attr_t attr;
28 
29     if (argc != 7) {
30         aos_pwm_cli_help();
31         return;
32     }
33     printf("aos pwm example test begin ...\r\n");
34     channel  = strtoul(argv[1], NULL, 0);
35     period = strtoul(argv[2], NULL, 0);
36     duty_cycle = strtoul(argv[3], NULL, 0);
37     polarity = strtoul(argv[4], NULL, 0);
38     enabled = strtoul(argv[5], NULL, 0);
39     duration = strtoul(argv[6], NULL, 0);
40     ret = aos_pwm_get(&ref, channel);
41     if (ret) {
42         printf("aos_pwm_get fail, ret %d\r\n", ret);
43         return;
44     }
45     attr.period = period;
46     attr.duty_cycle = duty_cycle;
47     attr.enabled = enabled;
48     attr.polarity = polarity;
49     ret = aos_pwm_set_attr(&ref, &attr);
50     if (ret) {
51         printf("aos_pwm_set_attr fail, ret %d\r\n", ret);
52         goto out;
53     }
54     ret = aos_pwm_get_attr(&ref, &attr);
55     if (ret) {
56         printf("aos_pwm_get_attr fail, ret %d\r\n", ret);
57         goto out;
58     }
59     printf("aos_pwm_attr_get:\r\n");
60     printf("period %u ns\r\n", attr.period);
61     printf("duty_cycle %u ns\r\n", attr.duty_cycle);
62     printf("polarity %s\r\n", attr.polarity ? "inverse" : "normal");
63     printf("enabled %u\r\n", attr.enabled);
64     aos_msleep(1000 * duration);
65     printf("aos_pwm_test success\r\n");
66  out:
67     aos_pwm_put(&ref);
68     return;
69 }
70 
71 #if AOS_COMP_CLI
72 /* reg args: fun, cmd, description*/
73 ALIOS_CLI_CMD_REGISTER(aos_pwm_cli_cmd, aos_pwm_example, aos pwm example)
74 #endif
75