-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathgptp_sync_pout.c
298 lines (271 loc) · 6.97 KB
/
gptp_sync_pout.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
/*
* Excelfore gptp - Implementation of gPTP(IEEE 802.1AS)
* Copyright (C) 2019 Excelfore Corporation (https://excelfore.com)
*
* This file is part of Excelfore-gptp.
*
* Excelfore-gptp is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* Excelfore-gptp is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Excelfore-gptp. If not, see
* <https://www.gnu.org/licenses/old-licenses/gpl-2.0.html>.
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/io.h>
#include <sys/types.h>
#include <fcntl.h>
#include <signal.h>
#include <string.h>
#include <errno.h>
#include <xl4unibase/unibase_binding.h>
#include "gptpmasterclock.h"
#define DEFAULT_SETUPTIME 1000 /* 1uSec */
#define DEFAULT_WAKEUPTIME 200000 /* 200uSec */
#define DEFAULT_INTERVAL 100000000 /* 100msec */
#define GOOD_TIMING_RANGE 100 /* nsec */
#define BAD_TIMING_THRESH 10000 /* 10 usec */
static int pout_prepare(int portn)
{
int fd;
char *dev="/sys/class/gpio/export";
char gpstr[16];
sprintf(gpstr, "%d", portn);
if((fd=open(dev, O_WRONLY))<0){
UB_LOG(UBL_ERROR, "%s:can't open %s:%s\n", __func__, dev, strerror(errno));
return -1;
}
if(write(fd, gpstr, strlen(gpstr))<0){
UB_LOG(UBL_ERROR, "%s:can't write:%s\n", __func__, strerror(errno));
}
close(fd);
return 0;
}
static int pout_set_direction(int out, int portn)
{
int fd=0;
char gpstr[64];
int res;
sprintf(gpstr, "/sys/class/gpio/gpio%d/direction", portn);
if((fd=open(gpstr, O_RDWR))<0){
UB_LOG(UBL_ERROR, "%s:can't open %s:%s\n", __func__, gpstr, strerror(errno));
return -1;
}
if(out)
res=write(fd,"out",3);
else
res=write(fd,"in",2);
if(res<0){
UB_LOG(UBL_ERROR, "%s:can't write:%s\n", __func__, strerror(errno));
return -1;
}
close(fd);
return 0;
}
static int gpiofd;
static int sysfs_pout_init(int portn)
{
char gpstr[64];
if(pout_prepare(portn)) return -1;
pout_set_direction(1, portn);
sprintf(gpstr, "/sys/class/gpio/gpio%d/value", portn);
if((gpiofd=open(gpstr, O_RDWR))<0){
UB_LOG(UBL_ERROR, "%s:can't open %s:%s\n", __func__, gpstr, strerror(errno));
return -1;
}
return 0;
}
static int sysfs_pout_write(int d)
{
if(d)
return write(gpiofd,"1",1);
else
return write(gpiofd,"0",1);
}
static int sysfs_pout_close(int portn)
{
if(gpiofd>0) close(gpiofd);
gpiofd=0;
pout_set_direction(0, portn);
return 0;
}
#define BASEPORT 0x378 /* lp1, 0x3bc, 0x278 */
static int pcprt_pout_init()
{
//set permissions to access port
if (ioperm(BASEPORT, 3, 1)){
UB_LOG(UBL_ERROR, "%s:ioperm:%s\n", __func__, strerror(errno));
return -1;
}
return 0;
}
static int pcprt_pout_close()
{
if (ioperm(BASEPORT, 3, 0)) {
UB_LOG(UBL_ERROR, "%s:ioperm:%s\n", __func__, strerror(errno));
return -1;
}
return 0;
}
static void pcprt_pout_write(int d)
{
outb(d, BASEPORT);
}
static int stoplpout=0;
static void getout(int sig)
{
UB_LOG(UBL_INFO, "going to terminate the process\n");
stoplpout=1;
}
static int lpout(int setup, int wakeup, int interval, int portn)
{
struct timespec ts1, ts2;
int64_t cts64, ts64; // current time
int64_t tgts64; // target time
int64_t tsiv64=interval;
int64_t last_tgts64=0;
bool over_run=false;
if(portn>=0){
if(sysfs_pout_init(portn)) return -1;
}else{
if(pcprt_pout_init()) return -1;
}
while (stoplpout==0) {
cts64=gptpmasterclock_getts64();
tgts64=cts64+tsiv64;
tgts64=(tgts64/interval)*interval; // align to the interval time
ts64=tgts64-cts64;
while(ts64>wakeup){
ts64-=wakeup;
memset(&ts2,0,sizeof(ts2));
UB_LOG(UBL_DEBUGV, "sleep for %"PRIi64"nsec\n",ts64);
UB_NSEC2TS(ts64, ts1);
if(clock_nanosleep(CLOCK_REALTIME,0,&ts1,&ts2)){
UB_LOG(UBL_INFO, "sleep failed %s\n", strerror(errno));
cts64=gptpmasterclock_getts64();
ts64=tgts64-cts64;
if(ts64<=0) break;
}else{
break;
}
}
cts64=gptpmasterclock_getts64();
ts64=tgts64-cts64;
if(ts64<0){
over_run=true;
UB_LOG(UBL_DEBUGV, "over run by sleep, %"PRIi64" nsec\n", ts64);
}else{
UB_LOG(UBL_DEBUGV, "spin for %"PRIi64"nsec\n",ts64);
while(ts64>setup){
cts64=gptpmasterclock_getts64();
ts64=tgts64-cts64;
}
}
cts64=gptpmasterclock_getts64();
// rising edge
if(portn>=0){
sysfs_pout_write(1);
}else{
pcprt_pout_write(0xff);
}
ts64=gptpmasterclock_getts64();
ts64=ts64-cts64;
ts64 /= 2;
ts64 += cts64; // ts64 = the center btw ts64 and cts
ts64 -= tgts64; // ts64 = diff to target time
if(over_run){
UB_LOG(UBL_INFO, "sleep over run happened, %s %"PRIi64"nsec away\n",
ts64<0?"-":"+", ts64);
goto fedge;
}else if(ts64 > BAD_TIMING_THRESH){
UB_LOG(UBL_INFO, "the result time was too bad, %s %"PRIi64"nsec away\n",
ts64<0?"-":"+", ts64);
goto fedge;
}
if(ts64 < GOOD_TIMING_RANGE){
UB_LOG(UBL_DEBUG, "good result time, %s %"PRIi64"nsec away\n",
ts64<0?"-":"+", ts64);
goto fedge;
}
UB_LOG(UBL_DEBUG, "the result time, %s %"PRIi64"nsec away\n",
ts64<0?"-":"+", ts64);
if(ts64<0){
// move to later timing, shorten 'setup'
setup -= ts64/2;
if(setup<0) setup=0;
}else{
// move to earlier timing, lengthen 'setup'
setup += ts64/2;
}
if(tgts64-last_tgts64>UB_SEC_NS){
UB_LOG(UBL_INFO, "new setup = %d\n",setup);
}
last_tgts64=tgts64;
fedge:
usleep(tsiv64/10000); // duty 10% pulse
// falling edge
if(portn>=0){
sysfs_pout_write(0);
}else{
pcprt_pout_write(0);
}
}
if(portn>=0){
sysfs_pout_write(0);
sysfs_pout_close(portn);
}else{
pcprt_pout_write(0);
pcprt_pout_close();
}
return 0;
}
static int print_usage(const char *argv0)
{
UB_LOG(UBL_INFO, "%s [-s setup_time][-w wakeup_time][-i interval_time(in msec)]\n", argv0);
UB_LOG(UBL_INFO, " [-n sysfs gpio port number, if not defined use Parallel port]\n");
UB_LOG(UBL_INFO, "BBB: -n 60 -w 600000\n");
return -1;
}
int main(int argc, char* argv[])
{
int setup=DEFAULT_SETUPTIME; /* hit the port in advance of this time */
int wakeup=DEFAULT_WAKEUPTIME; /* wake up the process this time before the target time */
int interval=DEFAULT_INTERVAL; /* pulse interval time in nsec */
int opt;
int portn=-1;
unibase_init_para_t init_para;
ubb_default_initpara(&init_para);
unibase_init(&init_para);
gptpmasterclock_init(NULL);
while ((opt = getopt(argc, (char **)argv, "hs:w:i:n:")) != -1){
switch (opt){
case 's':
setup=atoi(optarg);
break;
case 'n':
portn=atoi(optarg);
break;
case 'w':
wakeup=atoi(optarg);
break;
case 'i':
interval=atoi(optarg)*1000000;
break;
case 'h':
return print_usage(argv[0]);
}
}
signal(SIGINT, getout);
lpout(setup, wakeup, interval, portn);
gptpmasterclock_close();
return 0;
}