forked from matrix-io/xc3sprog
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathiogpiodpi.cpp
153 lines (125 loc) · 3.41 KB
/
iogpiodpi.cpp
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
#include <iogpiodpi.h>
#include <iostream>
IOGPIODPi::IOGPIODPi(int tms, int tck, int tdi, int tdo)
: TMSPin(tms), TCKPin(tck), TDIPin(tdi), TDOPin(tdo)
{
chip = gpiod_chip_open_by_name("gpiochip0");
if (!chip) {
perror("Open chip failed");
}
TMSline = gpiod_chip_get_line(chip, TMSPin);
TCKline = gpiod_chip_get_line(chip, TCKPin);
TDIline = gpiod_chip_get_line(chip, TDIPin);
TDOline = gpiod_chip_get_line(chip, TDOPin);
if (!TMSline) {
perror("TMSline line config failed");
gpiod_chip_close(chip);
}
if (!TCKline) {
perror("TCKline line config failed");
gpiod_chip_close(chip);
}
if (!TDIline) {
perror("TDIline line config failed");
gpiod_chip_close(chip);
}
if (!TDOline) {
perror("TDOline line config failed");
gpiod_chip_close(chip);
}
int ret;
ret = gpiod_line_request_output(TMSline, "Consumer", 0);
if (ret < 0) {
perror("Request TMSline as output failed");
gpiod_line_release(TMSline);
gpiod_chip_close(chip);
}
ret = gpiod_line_request_output(TCKline, "Consumer", 0);
if (ret < 0) {
perror("Request TCKline as output failed");
gpiod_line_release(TCKline);
gpiod_chip_close(chip);
}
ret = gpiod_line_request_output(TDIline, "Consumer", 0);
if (ret < 0) {
perror("Request TDIline as output failed");
gpiod_line_release(TDIline);
gpiod_chip_close(chip);
}
ret = gpiod_line_request_input(TDOline, "Consumer");
if (ret < 0) {
perror("Request TDOline as input failed");
gpiod_line_release(TDOline);
gpiod_chip_close(chip);
}
}
IOGPIODPi::~IOGPIODPi()
{
gpiod_line_release(TMSline);
gpiod_line_release(TCKline);
gpiod_line_release(TDIline);
gpiod_line_release(TDOline);
gpiod_chip_close(chip);
}
void IOGPIODPi::txrx_block(const unsigned char *tdi, unsigned char *tdo, int length, bool last)
{
// std::cerr << "txrx_block" << std::endl;
int i=0;
int j=0;
unsigned char tdo_byte=0;
unsigned char tdi_byte;
if (tdi)
tdi_byte = tdi[j];
while(i<length-1){
tdo_byte=tdo_byte+(txrx(false, (tdi_byte&1)==1)<<(i%8));
if (tdi)
tdi_byte=tdi_byte>>1;
i++;
if((i%8)==0){ // Next byte
if(tdo)
tdo[j]=tdo_byte; // Save the TDO byte
tdo_byte=0;
j++;
if (tdi)
tdi_byte=tdi[j]; // Get the next TDI byte
}
};
tdo_byte=tdo_byte+(txrx(last, (tdi_byte&1)==1)<<(i%8));
if(tdo)
tdo[j]=tdo_byte;
gpiod_line_set_value(TCKline, 0);
return;
}
void IOGPIODPi::tx_tms(unsigned char *pat, int length, int force)
{
int i;
unsigned char tms;
for (i = 0; i < length; i++)
{
if ((i & 0x7) == 0)
tms = pat[i>>3];
tx((tms & 0x01), true);
tms = tms >> 1;
}
gpiod_line_set_value(TCKline, 0);
}
void IOGPIODPi::tx(bool tms, bool tdi)
{
gpiod_line_set_value(TCKline, 0);
if(tdi)
gpiod_line_set_value(TDIline, 1);
else
gpiod_line_set_value(TDIline, 0);
if(tms)
gpiod_line_set_value(TMSline, 1);
else
gpiod_line_set_value(TMSline, 0);
gpiod_line_set_value(TCKline, 1);
}
bool IOGPIODPi::txrx(bool tms, bool tdi)
{
// std::cerr << "txrx" << gpiod_line_get_value(TDOline) << std::endl;
tx(tms, tdi);
return gpiod_line_get_value(TDOline);
// return digitalRead(TDOPin);
}