-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.c
95 lines (70 loc) · 2.46 KB
/
main.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
#include <string.h>
#include <ctype.h>
#include "xec.c"
#define debug(__format, ...) { printf("___ %s : %d : %s ___ "__format" \n", \
__FILE__, __LINE__, __func__, __VA_ARGS__); }
/// XEC params
bool _xec_monitor = 0;
bool _xec_read = 0;
bool _xec_write = 0;
int arg_to_int(char *arg) {
int c = (int) atol(arg);
c = (c > (int) 0 ? c : (int) strtoul(arg, NULL, 16));
return c;
}
int main(int argc, char *argv[])
{
int err_code = 0;
xec_init();
for ( size_t i = 0; i < argc; i++ )
{
debug("arg %d == %s", i, argv[i]);
if (0 == strcmp(argv[i], "read"))
{
/* read */
if (argv[i+1] == NULL) { debug("%s ", "read address malfunction"); err_code = -1; goto exit; }
uint8_t a = arg_to_int(argv[i+1]);
debug("read from %02x", a);
xec_wait_write();
xec_port_write(command_port, xec_read);
xec_wait_write();
xec_port_write(data_port, a);
xec_wait_read();
uint8_t data = xec_port_read(data_port);
debug("%02x a == %02x", a, data);
return 0;
}
else if (0 == strcmp(argv[i], "write"))
{
/* write */
if (argv[i+1] == NULL) { debug("%s ", "read address malfunction"); err_code = -1; goto exit; }
if (argv[i+2] == NULL) { debug("%s ", "write address malfunction"); err_code = -1; goto exit; }
uint8_t a = arg_to_int(argv[i+1]);
uint8_t b = arg_to_int(argv[i+2]);
debug("write %02x to %02x", b, a);
xec_wait_write();
xec_port_write(command_port, xec_write);
xec_wait_write();
xec_port_write(data_port, a);
xec_wait_write();
xec_port_write(data_port, b);
xec_wait_read();
return 0;
}
else if (0 == strcmp(argv[i], "monitor"))
{
/* monitor */
xec_monitor();
return 0;
}
}
exit:
xec_close();
debug("err code %d", err_code);
exit(err_code);
}
/*
xec_init();
xec_clear();
xec_monitor();
*/