1 /*
2  * Copyright (C) 2020-2023 Alibaba Group Holding Limited
3  */
4 #include <stdio.h>
5 #include <stdint.h>
6 #include <stdbool.h>
7 #include <ulog/ulog.h>
8 #include <vfsdev/pwm_dev.h>
9 
beeper_start(uint16_t port,uint16_t frequency,uint16_t duration)10 void beeper_start(uint16_t port, uint16_t frequency, uint16_t duration)
11 {
12     int ret = -1;
13     int fd = 0;
14     char name[16] = {0};
15     float duty_cycle = 0.8;
16 
17     snprintf(name, sizeof(name), "/dev/pwm%d", port);
18     fd = open(name, 0);
19     if (fd >= 0) {
20         if (frequency > 0) {
21             ret = ioctl(fd, IOC_PWM_FREQ, (unsigned long)frequency);
22             ret = ioctl(fd, IOC_PWM_DUTY_CYCLE, (unsigned long)&duty_cycle);
23             ret = ioctl(fd, IOC_PWM_ON, (unsigned long)0);
24         }
25         if (duration != 0) {
26             aos_msleep(duration);
27         }
28         if (frequency > 0 && duration > 0) {
29             ret = ioctl(fd, IOC_PWM_OFF, (unsigned long)0);
30             close(fd);
31         }
32     }
33 }
34 
beeper_stop(uint16_t port)35 void beeper_stop(uint16_t port)
36 {
37     int ret = -1;
38     int fd = 0;
39     char name[16] = {0};
40     float duty_cycle = 0.8;
41     int freq = 1;
42     unsigned int period_s;
43 
44     snprintf(name, sizeof(name), "/dev/pwm%d", port);
45     fd = open(name, 0);
46     if (fd >= 0) {
47         ret = ioctl(fd, IOC_PWM_FREQ, (unsigned long)freq);
48         ret = ioctl(fd, IOC_PWM_DUTY_CYCLE, (unsigned long)&duty_cycle);
49         ret = ioctl(fd, IOC_PWM_ON, (unsigned long)0);
50         ret = ioctl(fd, IOC_PWM_OFF, (unsigned long)0);
51         close(fd);
52     }
53 }
54