forked from oxavelar/HighLatencyGPIO
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cc
More file actions
executable file
·143 lines (111 loc) · 3.33 KB
/
main.cc
File metadata and controls
executable file
·143 lines (111 loc) · 3.33 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
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
#include "GPIO.hh"
// STL
#include <iostream>
#include <chrono>
#include <unistd.h> // usleep()
using namespace std::chrono;
using namespace std;
class Handler
{
public:
Handler(int id):_g(id, GPIO::Type::BOTH, std::bind(&Handler::handle, this, std::placeholders::_1, std::placeholders::_2)){
_accum = std::chrono::duration<double, std::micro>(0.0);
_value = _g.getValue();
}
void handle(unsigned short id, GPIO::Value val)
{
_value = val;
const high_resolution_clock::time_point end = high_resolution_clock::now();
const auto time_span = duration_cast<microseconds>(end - _beg);
_accum += time_span;
std::cout << "id: " << id << "; Val: " << val << "; Latency: " << time_span.count() << " microseconds" << std::endl;
}
void startTimeout(){
_beg = high_resolution_clock::now();
}
void waitValueISR(GPIO::Value v){
auto timeout_us = high_resolution_clock::now() + std::chrono::microseconds(1000000);
while(_value != v && timeout_us > high_resolution_clock::now()){
//cout << "_v: " << h._value << "; v: " << v << endl;
usleep(10000);
}
if(_value != v){
cout << "Timeout waiting " << v << " on " << _g.id() << endl;
throw std::runtime_error("Timeout waiting " + std::to_string(v));
}else
cout << "WaitValueISR " << _g.id() << " Got: " << v << endl;
}
void showValue(GPIO &g1){
//return ;
//auto v1 = GPIO::Value::LOW;
auto v1 = g1.getValue();
auto v2 = _g.getValue();
//if (v1 != v2)
cout << "Set " << v1 << "; Read " << v2 << endl;
}
protected:
GPIO _g;
high_resolution_clock::time_point _beg;
duration<double, std::micro> _accum;
volatile int _value = -1;
};
int main()
{
{
// Short GPIO 15 (input) to GPIO 27 (output) for the following latency test
GPIO gpio1(395);
GPIO gpio2(401);
Handler h1(384);
Handler h2(385);
Handler h3(386);
Handler h4(387);
Handler h5(388);
Handler h6(389);
Handler h7(390);
Handler h8(391);
Handler h9(392);
Handler h10(393);
Handler h11(394);
Handler h12(396);
Handler h13(397);
usleep(125000);
gpio1.setValue(GPIO::LOW);
usleep(125000);
gpio1.setValue(GPIO::HIGH);
usleep(125000);
gpio1.setValue(GPIO::LOW);
usleep(125000);
cout << "Main Cycle" << endl;
/*try {
throw std::runtime_error("hello there!");
}catch(...){
cout << "Handled1 " << endl;
}*/
const unsigned int nIterations = 50000;
//for(unsigned int i=0;i<nIterations;++i)
while(1)
{
cout << "High" << endl;
gpio1.setValue(GPIO::HIGH);
gpio2.setValue(GPIO::HIGH);
h12.startTimeout();
h8.startTimeout();
h12.waitValueISR(GPIO::LOW);
h8.waitValueISR(GPIO::HIGH);
h12.showValue(gpio1);
h8.showValue(gpio1);
//usleep(15000);
cout << "Low" << endl;
gpio1.setValue(GPIO::LOW);
gpio2.setValue(GPIO::LOW);
h8.startTimeout();
h12.startTimeout();
h8.waitValueISR(GPIO::LOW);
h12.waitValueISR(GPIO::HIGH);
h12.showValue(gpio1);
h8.showValue(gpio1);
//usleep(40000);
}
//std::cout << "Average: " << _accum.count()/nIterations << " microseconds " << std::endl;
}
}