-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpwm_sw-nanosleep.c
133 lines (108 loc) · 3.15 KB
/
pwm_sw-nanosleep.c
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
// Ron Ocampo
// Dinggao Pan
// 11/10/2017
// HW2 SW PWM w/o ptp
#include <time.h>
#include <stdio.h>
#include <signal.h>
#include <stdlib.h>
#include <stdint.h>
#include <unistd.h>
#define PULSELEN 1000000
#define TONANO 1000000000
struct timespec starttime, testStart, testEnd;
FILE *infileptr;
FILE *exportfileptr;
FILE *gpiodirectionptr;
FILE *gpioedgeptr;
FILE *outputValue;
void pulse();
void waiting(uint64_t interval);
double TimeSpecToNano(struct timespec* ts);
//handle cntrl+c
void sigintHandler(int sig_num)
{
printf("\nTerminating... \n");
exit(0);
}
int main(int argc, char *argv[])
{
//check that path is given
if (argc!=2)
{
printf("Please enter the path of input file\n");
exit (0);
}
//init handler
signal(SIGINT, sigintHandler);
//open file for reading initializing the line variable
infileptr = fopen(argv[1], "r");
//making sure the input file exists
if (infileptr == NULL)
{
fprintf(stderr, "File '%s' does not exist\n", argv[1]);
exit(EXIT_FAILURE);
}
//open pin
exportfileptr = fopen("/sys/class/gpio/export", "w");
fprintf(exportfileptr, "39"); //pin4 on header P8 gpio1[7]
fclose(exportfileptr);
//configure pin as output
gpiodirectionptr = fopen("/sys/class/gpio/gpio39/direction", "w");
fprintf(gpiodirectionptr, "out");
fclose(gpiodirectionptr);
printf("entering while loop\n");
//get start time
printf("try to get times\n");
long double waitTime, timestamp, tsPrev, tsNano, startNano;
fscanf(infileptr, "%Lf", ×tamp);
while(timestamp != tsPrev)
{
if (timestamp == 0)
{
clock_gettime(CLOCK_MONOTONIC, &starttime);
startNano = TimeSpecToNano(&starttime);
} else {
clock_gettime(CLOCK_MONOTONIC, &testStart);
tsNano = TimeSpecToNano(&testStart);
waitTime = timestamp * TONANO + startNano - tsNano;
printf("timestamp in Nano: %Lf, starttime in Nano: %Lf, now: %Lf\n",
timestamp * TONANO, startNano, tsNano);
printf("wait time is : %Lf\n", waitTime);
waiting((uint64_t)waitTime / 1000);
clock_gettime(CLOCK_MONOTONIC, &testEnd);
printf("Current timestamp = %Lf \n", timestamp);
printf("Real Timestamp = %Lf, \n", (TimeSpecToNano(&testEnd) - startNano)/TONANO);
}
pulse();
tsPrev = timestamp;
fscanf(infileptr, "%Lf", ×tamp);
}
fclose(infileptr);
exportfileptr = fopen("/sys/class/gpio/unexport", "w");
fprintf(exportfileptr, "39"); //pin4 on header P8 gpio1[7]
fclose(exportfileptr);
fprintf(stdout, "Done\n");
return 0;
}
void pulse()
{
outputValue = fopen("/sys/class/gpio/gpio39/value", "w");
fprintf(outputValue, "1");
waiting((uint64_t)1000);
fprintf(outputValue, "0");
fclose(outputValue);
}
void waiting(uint64_t interval)
{
struct timespec deadline;
clock_gettime(CLOCK_MONOTONIC, &deadline);
// set the deadline according to the time you want to sleep
deadline.tv_nsec += (interval % TONANO);
deadline.tv_sec += (interval / TONANO);
printf("result: %d\n", !clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &deadline, NULL));
}
double TimeSpecToNano(struct timespec* ts)
{
return (long double)ts->tv_sec * 1000000000.0 + (long double)ts->tv_nsec;
}