1
+ // #ifndef _COLORS_
2
+ // #define _COLORS_
3
+ //
4
+ // /* FOREGROUND */
5
+ // #define _reset "\x1B[0m"
6
+ // #define KRED "\x1B[31m"
7
+ // #define KGRN "\x1B[32m"
8
+ // #define KYEL "\x1B[33m"
9
+ // #define KBLU "\x1B[34m"
10
+ // #define KMAG "\x1B[35m"
11
+ // #define KCYN "\x1B[36m"
12
+ // #define KWHT "\x1B[37m"
13
+ // #define KLRD "\033[1;31m"
14
+ // #define KLGN "\033[1;32m"
15
+ // #define _inverse "\033[1;7m"
16
+ //
17
+ // #define FRED(x) KRED x _reset
18
+ // #define FGRN(x) KGRN x _reset
19
+ // #define FYEL(x) KYEL x _reset
20
+ // #define FBLU(x) KBLU x _reset
21
+ // #define FMAG(x) KMAG x _reset
22
+ // #define FCYN(x) KCYN x _reset
23
+ // #define FWHT(x) KWHT x _reset
24
+ //
25
+ // #define BOLD(x) "\x1B[1m" x _reset
26
+ // #define UNDL(x) "\x1B[4m" x _reset
27
+ //
28
+ // #endif /* _COLORS_ */
29
+
1
30
#include < iostream>
2
31
#include < stdio.h>
3
32
#include < string.h>
6
35
#include < cctype>
7
36
#include < sstream>
8
37
#include < string>
9
-
10
38
using namespace std ;
11
39
12
40
int ec_fd;
13
41
42
+ int max_waits = 20 , wait_faults;
43
+
44
+ // (void)&ttyf;
45
+ // int ttyf = isatty(1) ? TTY : ORDINARY;
46
+
47
+ string _reset, _bright, _underline, _blink, _inverse, _black, _red, _green,
48
+ _yellow, _blue, _magenta, _cyan, _white, _black_bg, _red_bg, _green_bg,
49
+ _yellow_bg, _blue_bg, _magenta_bg, _cyan_bg, _white_bg;
50
+
51
+ extern char **environ;
52
+
53
+ int run (int argc, char **argv);
54
+
55
+ int main (int argc, char **argv, char **envp) {
56
+
57
+ bool isColored = 0 ;
58
+ for (size_t i = 1 ; i < argc; ++i) {
59
+ if (argv[i] == std::string (" -c" ) ) {
60
+ isColored = 1 ;
61
+ }
62
+ }
63
+
64
+ // for (char **env = envp; *env != 0; env++)
65
+ // {
66
+ // char *thisEnv = *env;
67
+ // printf("%s\n", thisEnv);
68
+ // }
69
+
70
+ if (isatty (STDOUT_FILENO) ^ isColored) {
71
+ _reset = " \x1b [0m" ;
72
+ _bright = " \x1b [1m" ;
73
+ _underline = " \x1b [4m" ;
74
+ _blink = " \x1b [5m" ;
75
+ _inverse = " \x1b [7m" ;
76
+
77
+ _black = " \x1b [30m" ;
78
+ _red = " \x1b [31m" ;
79
+ _green = " \x1b [32m" ;
80
+ _yellow = " \x1b [33m" ;
81
+ _blue = " \x1b [34m" ;
82
+ _magenta = " \x1b [35m" ;
83
+ _cyan = " \x1b [36m" ;
84
+ _white = " \x1b [37m" ;
85
+
86
+ _black_bg = " \x1b [40m" ;
87
+ _red_bg = " \x1b [41m" ;
88
+ _green_bg = " \x1b [42m" ;
89
+ _yellow_bg = " \x1b [43m" ;
90
+ _blue_bg = " \x1b [44m" ;
91
+ _magenta_bg = " \x1b [45m" ;
92
+ _cyan_bg = " \x1b [46m" ;
93
+ _white_bg = " \x1b [47m" ;
94
+ }
95
+
96
+ run (argc, argv);
97
+ }
98
+
14
99
int err (char *txt) {
15
100
throw std::invalid_argument (txt);
16
101
}
@@ -32,7 +117,7 @@ enum Status : int {
32
117
output_buffer_full = 0x01 ,
33
118
input_buffer_full = 0x02 ,
34
119
command = 0x08 ,
35
- burst_mode = 0x10 ,
120
+ bu_reset_mode = 0x10 ,
36
121
sci_event_pending = 0x20 ,
37
122
smi_event_pending = 0x40
38
123
};
@@ -44,8 +129,8 @@ enum Ports : int {
44
129
enum Command : int {
45
130
_read = 0x80 ,
46
131
_write = 0x81 ,
47
- burst_enable = 0x82 ,
48
- burst_disable = 0x83 ,
132
+ bu_reset_enable = 0x82 ,
133
+ bu_reset_disable = 0x83 ,
49
134
query = 0x84
50
135
};
51
136
@@ -75,81 +160,80 @@ byte ec_write(uint8_t port, byte data) {
75
160
return data;
76
161
}
77
162
78
- bool ec_wait_status (int port, Status status) {
79
- int timeline = 200 ;
80
- while (timeline > 0 ) {
81
- timeline--;
82
- byte f = (byte) ec_read (0x66 );
83
- if (((int ) status && (int ) f) == 0 )
84
- return int (1000 - timeline);
163
+ // bool ec_wait_status(int port, Status status) {
164
+ // int timeline = 200;
165
+ // while (timeline > 0) {
166
+ // timeline--;
167
+ // byte f = (byte) ec_read(0x66);
168
+ // if (((int) status && (int) f) == 0)
169
+ // return int(1000 - timeline);
170
+ // }
171
+ // return false;
172
+ // }
173
+
174
+ bool ec_wait_status (int status, bool set) {
175
+ int timeout = 500 ;
176
+ while (timeout > 0 ) {
177
+ timeout--;
178
+ byte val = ec_read (Ports::command_port);
179
+ val = set ? (byte) ~(int ) val : val;
180
+ if (((int ) status & (int ) val) == 0 ) {
181
+ return true ;
182
+ }
85
183
}
86
184
return false ;
87
185
}
88
186
89
- #ifndef _COLORS_
90
- #define _COLORS_
91
-
92
- /* FOREGROUND */
93
- #define RST " \x1B [0m"
94
- #define KRED " \x1B [31m"
95
- #define KGRN " \x1B [32m"
96
- #define KYEL " \x1B [33m"
97
- #define KBLU " \x1B [34m"
98
- #define KMAG " \x1B [35m"
99
- #define KCYN " \x1B [36m"
100
- #define KWHT " \x1B [37m"
101
- #define KGRY " \033 [1;30m"
102
- #define KLRD " \033 [1;31m"
103
- #define KLGN " \033 [1;32m"
104
- #define KRVR " \033 [1;7m"
105
-
106
- #define FRED (x ) KRED x RST
107
- #define FGRN (x ) KGRN x RST
108
- #define FYEL (x ) KYEL x RST
109
- #define FBLU (x ) KBLU x RST
110
- #define FMAG (x ) KMAG x RST
111
- #define FCYN (x ) KCYN x RST
112
- #define FWHT (x ) KWHT x RST
113
-
114
- #define BOLD (x ) " \x1B [1m" x RST
115
- #define UNDL (x ) " \x1B [4m" x RST
116
-
117
- #endif /* _COLORS_ */
187
+ bool ec_wait_read () {
188
+ if (wait_faults > max_waits) {
189
+ return true ;
190
+ } else if (ec_wait_status (Status::output_buffer_full, true )) {
191
+ wait_faults = 0 ;
192
+ return true ;
193
+ } else {
194
+ wait_faults++;
195
+ return false ;
196
+ }
197
+ }
198
+
199
+ bool ec_wait_write () {
200
+ return ec_wait_status (Status::input_buffer_full, false );
201
+ }
118
202
119
203
int monitor () {
120
204
121
205
cout << " │ " ;
122
206
for (uint16_t x = 0 ; x < 16 ; x++) {
123
207
string append = ((int ) x < 16 ? " 0" : " " );
124
- cout << KRVR << append << uppercase << std::hex << x << " " ;
208
+ cout << _inverse << append << uppercase << std::hex << x << " " ;
125
209
}
126
- cout << RST ;
210
+ cout << _reset ;
127
211
cout << endl << " ──┼" ;
128
212
for (uint16_t x = 0 ; x < 16 * 3 ; x++) {
129
213
cout << " ─" ;
130
214
}
131
215
cout << endl;
132
216
133
217
for (int i = 0 ; i < 256 ; i++) {
134
- ec_wait_status (Ports::command_port, Status::input_buffer_full );
218
+ ec_wait_write ( );
135
219
ec_write (Ports::command_port, (byte) Command::_read);
136
- ec_wait_status (Ports::command_port, Status::input_buffer_full );
220
+ ec_wait_write ( );
137
221
ec_write (Ports::data_port, (byte) (i));
138
- ec_wait_status (Ports::command_port, Status::output_buffer_full );
222
+ ec_wait_read ( );
139
223
byte data = ec_read (Ports::data_port);
140
224
141
225
if (i == 0 )
142
- cout << KRVR << " 00" << RST << " │ " ;
226
+ cout << _inverse << " 00" << _reset << " │ " ;
143
227
144
228
if ((i % 16 ) == 0 && i != 0 ) {
145
229
cout << endl;
146
- cout << RST << KRVR << uppercase << i << RST << " │ " ;
230
+ cout << _reset << _inverse << uppercase << i << _reset << " │ " ;
147
231
}
148
232
149
-
150
233
string append = ((int ) data < 16 ? " 0" : " " );
151
234
152
- cout << uppercase << ((int ) data == 0xFF ? KLGN : (int ) data == 0x00 ? KGRY : KLRD);
235
+ cout << uppercase
236
+ << ((int ) data == 0xFF ? (_green + _bright) : (int ) data == 0x00 ? (_black + _bright) : (_red + _bright));
153
237
cout << append << uppercase << std::hex << (int ) data << " " ;
154
238
// cout << std::hex << append << (int) data << " ";
155
239
}
@@ -165,10 +249,27 @@ int help() {
165
249
cout << endl;
166
250
167
251
cout << " <example>" << endl;
168
- cout << " watch -n 1 -c -d sudo xec monitor : print EC table with changes highlight every second" << endl;
252
+ cout
253
+ << " watch -n 1 -c -d sudo xec monitor : print EC table with changes highlight every second"
254
+ << endl;
255
+ cout << endl;
256
+
257
+ cout << " <arguments>" << endl;
258
+ cout
259
+ << " -c : enable colors"
260
+ << endl;
261
+ }
262
+
263
+ #include < cstdlib>
264
+ #include < iostream>
265
+
266
+ int arg_to_int (char *arg) {
267
+ int c = (int ) atol (arg);
268
+ c = (c > (int ) 0 ? c : (int ) std::stoul (arg, nullptr , 16 ));
269
+ return c;
169
270
}
170
271
171
- int main (int argc, char **argv) {
272
+ int run (int argc, char **argv) {
172
273
173
274
ec_init ();
174
275
@@ -178,16 +279,13 @@ int main(int argc, char **argv) {
178
279
if (argv[2 ] == NULL )
179
280
err (" no address" );
180
281
181
- unsigned int hex = stoul (argv[2 ], nullptr , 16 );
182
-
183
- ec_wait_status (Ports::command_port, Status::input_buffer_full);
282
+ ec_wait_write ();
184
283
ec_write (Ports::command_port, (byte) Command::_read);
185
- ec_wait_status (Ports::command_port, Status::input_buffer_full);
186
- ec_write (Ports::data_port, (byte) hex);
187
- // ec_wait_status(Ports::command_port, Status::input_buffer_full);
188
- ec_wait_status (Ports::command_port, Status::output_buffer_full);
284
+ ec_wait_write ();
285
+ ec_write (Ports::data_port, (byte) arg_to_int (argv[2 ]));
286
+ ec_wait_read ();
189
287
cout << argv[2 ] << " : " << uppercase << std::hex << " 0x"
190
- << (int )ec_read (Ports::data_port) << endl;
288
+ << (int ) ec_read (Ports::data_port) << endl;
191
289
192
290
} else if (strcmp (argv[1 ], " write" ) == 0 ) {
193
291
@@ -196,23 +294,25 @@ int main(int argc, char **argv) {
196
294
if (argv[3 ] == NULL )
197
295
err (" no 2 address" );
198
296
199
-
200
- unsigned int hex0 = stoul (argv[2 ], nullptr , 16 );
201
- unsigned int hex1 = stoul (argv[3 ], nullptr , 16 );
202
-
203
- ec_wait_status (Ports::command_port, Status::input_buffer_full);
297
+ ec_wait_write ();
204
298
ec_write (Ports::command_port, (byte) Command::_write);
205
- ec_wait_status (Ports::command_port, Status::input_buffer_full);
206
- ec_write (Ports::data_port, (byte) hex0);
207
- ec_wait_status (Ports::command_port, Status::input_buffer_full);
208
- ec_write (Ports::data_port, (byte) hex1);
299
+ ec_wait_write ();
300
+ ec_write (Ports::data_port, (byte) arg_to_int (argv[2 ]));
301
+ ec_wait_write ();
302
+ ec_write (Ports::data_port, (byte) arg_to_int (argv[3 ]));
303
+ ec_wait_read ();
209
304
ec_read (Ports::data_port);
210
305
211
306
cout << uppercase << std::hex << argv[2 ] << " : " << argv[3 ] << endl;
212
307
} else if (strcmp (argv[1 ], " monitor" ) == 0 ) {
213
308
monitor ();
214
309
} else if (strcmp (argv[1 ], " help" ) == 0 || strcmp (argv[1 ], " " ) == 0 ) {
215
310
help ();
311
+ } else if (strcmp (argv[1 ], " cti" ) == 0 || strcmp (argv[1 ], " " ) == 0 ) {
312
+
313
+ cout << arg_to_int (argv[2 ]) << endl;
314
+ cout << argv[2 ] << endl;
315
+
216
316
} else {
217
317
cout << " unknown command" << endl;
218
318
return 0 ;
0 commit comments