1 #include <stdint.h>
2 #include <stdio.h>
3 #include <unistd.h>
4 #include <sys/ioctl.h>
5 #include <stdlib.h>
6 #include <fcntl.h>
7 #include <aos/errno.h>
8 #include <aos/kernel.h>
9 #if AOS_COMP_CLI
10 #include "aos/cli.h"
11 #endif
12 #include <vfsdev/pwm_dev.h>
13 
vfs_pwm_test(int channel,int freq,unsigned int period_s)14 int vfs_pwm_test(int channel, int freq, unsigned int period_s)
15 {
16     int ret = -1;
17     int fd = 0;
18     char name[16] = {0};
19     float duty_cycle = 0.5;
20 
21     if (period_s < 1)
22         period_s = 2;
23 
24     snprintf(name, sizeof(name), "/dev/pwm%d", channel);
25     fd = open(name, 0);
26     printf("open %s %s, fd:%d\r\n", name, fd >= 0 ? "success" : "fail", fd);
27 
28     if (fd >= 0) {
29 
30         ret = ioctl(fd, IOC_PWM_FREQ, (unsigned long)freq);
31         printf("set pwm's freq to %d %s, ret:%d\r\n", freq, ret ? "fail" : "succeed", ret);
32 
33         ret = ioctl(fd, IOC_PWM_DUTY_CYCLE, (unsigned long)&duty_cycle);
34         printf("set pwm's duty cycle to %f %s, ret:%d\r\n", duty_cycle, ret ? "fail" : "succeed", ret);
35 
36         ret = ioctl(fd, IOC_PWM_ON, (unsigned long)0);
37         printf("set pwm on %s, ret:%d\r\n", ret ? "fail" : "succeed", ret);
38 
39         aos_msleep(period_s * 1000);
40         ret = ioctl(fd, IOC_PWM_OFF, (unsigned long)0);
41         printf("set pwm off %s, ret:%d\r\n", ret ? "fail" : "succeed", ret);
42         close(fd);
43         ret = 0;
44     } else
45         ret = -EIO;
46 
47     return ret;
48 }
49 
pwm_output_test(int32_t argc,char ** argv)50 static void pwm_output_test(int32_t argc, char **argv)
51 {
52     int32_t ret;
53     int channel;
54     int freq;
55     unsigned int period_s;
56 
57     channel = argc > 1 ? atoi(argv[1]) : 0;
58     freq = argc > 2 ? atoi(argv[2]) : 100;
59     period_s = argc > 3 ? atoi(argv[3]) : 3;
60 
61     printf("pwm comp output test start!");
62 
63     ret = vfs_pwm_test(channel, freq, period_s);
64     if (!ret) {
65         printf("pwm comp output test success!");
66     } else {
67          printf("pwm comp output test failed, ret:%d\r\n", ret);
68     }
69     return;
70 }
71 
72 #if AOS_COMP_CLI
73 /* reg args: fun, cmd, description*/
74 ALIOS_CLI_CMD_REGISTER(pwm_output_test, pwm_output, pwm output operation example)
75 #endif
76