-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpwm.c
More file actions
102 lines (80 loc) · 1.88 KB
/
Copy pathpwm.c
File metadata and controls
102 lines (80 loc) · 1.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#include "pwm.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <fcntl.h>
#include <poll.h>
/****************************************************************
* pwm_set_duty
* \input duty period in ns
****************************************************************/
int pwm_set_duty(unsigned int duty)
{
int fd, len;
char buf[MAX_BUF];
fd = open(SYSFS_PWM_DIR "/duty", O_WRONLY);
if (fd < 0) {
perror("/duty");
return fd;
}
len = snprintf(buf, sizeof(buf), "%d", duty);
write(fd, buf, len);
close(fd);
return 0;
}
/****************************************************************
* pwm_set_period
* \input period in ns
****************************************************************/
int pwm_set_period(unsigned int period)
{
int fd, len;
char buf[MAX_BUF];
fd = open(SYSFS_PWM_DIR "/period", O_WRONLY);
if (fd < 0) {
perror("/period");
return fd;
}
len = snprintf(buf, sizeof(buf), "%d", period);
write(fd, buf, len);
close(fd);
return 0;
}
/****************************************************************
* pwm_set_polarity
* \input 1/0
****************************************************************/
int pwm_set_polarity(unsigned int polarity)
{
int fd, len;
char buf[MAX_BUF];
fd = open(SYSFS_PWM_DIR "/duty", O_WRONLY);
if (fd < 0) {
perror("/duty");
return fd;
}
len = snprintf(buf, sizeof(buf), "%d", polarity);
write(fd, buf, len);
close(fd);
return 0;
}
/****************************************************************
* pwm_set_enable
* \input 1/0
****************************************************************/
int pwm_set_enable(unsigned int enable)
{
int fd, len;
char buf[MAX_BUF];
fd = open(SYSFS_PWM_DIR "/run", O_WRONLY);
if (fd < 0) {
perror("/run");
return fd;
}
len = snprintf(buf, sizeof(buf), "%d", enable);
write(fd, buf, len);
close(fd);
return 0;
}