-
Notifications
You must be signed in to change notification settings - Fork 0
/
ftdi_pwr.c
254 lines (214 loc) · 5.46 KB
/
ftdi_pwr.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
/*
* ftdi_pwr - Manage power and reset on ATX motherboard
*
* Copyright (C) 2012 Jonas Jonsson <jonas@websystem.se>
*
* This program 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 3 of the License, or
* (at your option) any later version.
*
* This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include <libgen.h> /* for basename() */
#include <string.h>
#include <ftdi.h>
#define USB_VENDOR_ID 0x0403
#define USB_PRODUCT_ID 0x6001
#define USB_PRODUCT "EVAL232 Board USB <-> Serial"
/* CBUSx for RESET and POWER */
#define RESET_PIN 2
#define POWER_PIN 3
enum command {
CHECK,
RESET,
POWER,
LONGPOWER
};
void print_cbus(const struct ftdi_eeprom *eeprom)
{
char *cbus_mux[] = {"TXDEN", "PWREN", "RXLED", "TXLED", "TX+RXLED",
"SLEEP","CLK48","CLK24","CLK12","CLK6",
"IOMODE","BB_WR","BB_RD"};
for (int i = 0; i < 5; i++) {
printf("CBUS%d: %2d(%s)\n", i, eeprom->cbus_function[i],
cbus_mux[eeprom->cbus_function[i]]);
}
}
int check_eeprom(struct ftdi_context *ftdi)
{
char buf[FTDI_DEFAULT_EEPROM_SIZE];
int ret;
struct ftdi_eeprom eeprom;
ret = ftdi_read_eeprom(ftdi, buf);
if (ret < 0) {
fprintf(stderr, "Unable to read eeprom: %d (%s)\n", ret,
ftdi_get_error_string(ftdi));
goto err_out;
}
ret = ftdi_eeprom_decode(&eeprom, buf, FTDI_DEFAULT_EEPROM_SIZE);
if (ret < 0) {
fprintf(stderr, "Unable to decode eeprom: %d (%s)\n", ret,
ftdi_get_error_string(ftdi));
goto err_out;
}
print_cbus(&eeprom);
if (eeprom.cbus_function[2] != CBUS_IOMODE ||
eeprom.cbus_function[3] != CBUS_IOMODE) {
printf("CBUS2 or CBUS3 is wrong, fixing...\n");
eeprom.cbus_function[2] = CBUS_IOMODE;
eeprom.cbus_function[3] = CBUS_IOMODE;
/* ftdi_eeprom_decode() doesn't set size correct */
if (eeprom.size == 0)
eeprom.size = FTDI_DEFAULT_EEPROM_SIZE;
ret = ftdi_eeprom_build(&eeprom, buf);
if (ret < 0) {
fprintf(stderr, "Unable to build eeprom: %d (%s)\n",
ret, ftdi_get_error_string(ftdi));
goto err_out;
}
ret = ftdi_write_eeprom(ftdi, buf);
if (ret < 0) {
fprintf(stderr, "Unable to write eeprom: %d (%s)\n",
ret, ftdi_get_error_string(ftdi));
goto err_out;
}
print_cbus(&eeprom);
}
ftdi_eeprom_free(&eeprom);
return 0;
err_out:
ftdi_eeprom_free(&eeprom);
return -1;
}
/* Toggle CBUS pin high for time seconds */
int toggle_cbus(struct ftdi_context *ftdi, int pin, int time)
{
int ret;
unsigned char bitmask;
/*
* BITMASK
* CBUS Bits
* 3210 3210
* xxxx xxxx
* | |------ Output Control 0->LO, 1->HI
* |----------- Input/Output 0->Input, 1->Output
*/
bitmask = 0x11 << pin;
ret = ftdi_set_bitmode(ftdi, bitmask, BITMODE_CBUS);
if (ret < 0) {
fprintf(stderr, "Unable to set bitmode %#.2x: %d (%s)\n", ret,
ftdi_get_error_string(ftdi));
ftdi_free(ftdi);
return -1;
}
usleep(time * 1000000);
bitmask = 0x10 << pin;
ret = ftdi_set_bitmode(ftdi, bitmask, BITMODE_CBUS);
if (ret < 0) {
fprintf(stderr, "Unable to set bitmode %#.2x: %d (%s)\n", ret,
ftdi_get_error_string(ftdi));
ftdi_free(ftdi);
return -1;
}
return 0;
}
int reset(struct ftdi_context *ftdi)
{
printf("Toggle reset for 1 second\n");
return toggle_cbus(ftdi, RESET_PIN, 1);
}
int power(struct ftdi_context *ftdi)
{
printf("Toggle power for 1 second\n");
return toggle_cbus(ftdi, POWER_PIN, 1);
}
int longpower(struct ftdi_context *ftdi)
{
printf("Toggle power for 5 seconds\n");
return toggle_cbus(ftdi, POWER_PIN, 5);
}
int doit(enum command cmd)
{
struct ftdi_context *ftdi;
int ret;
ftdi = ftdi_new();
if (ftdi == NULL) {
fprintf(stderr, "Failed to allocate ftdi structure\n");
return -1;
}
printf("Searching for device 0x%.4x 0x%.4x, %s\n", USB_VENDOR_ID, USB_PRODUCT_ID, USB_PRODUCT);
ret = ftdi_usb_open_desc(ftdi, USB_VENDOR_ID, USB_PRODUCT_ID, USB_PRODUCT, NULL);
if (ret < 0) {
fprintf(stderr, "Unable to find ftdi device: %d (%s)\n", ret,
ftdi_get_error_string(ftdi));
goto err_out;
}
printf("Checking eeprom\n");
if (check_eeprom(ftdi))
goto err_out;
switch (cmd) {
case CHECK:
break;
case RESET:
if (reset(ftdi))
goto err_out;
break;
case POWER:
if (power(ftdi))
goto err_out;
break;
case LONGPOWER:
if (longpower(ftdi))
goto err_out;
break;
default:
fprintf(stderr, "Wrong cmd in doit()\n");
goto err_out;
break;
}
ret = ftdi_usb_close(ftdi);
if (ret < 0) {
fprintf(stderr, "Unable to close ftdi device: %d (%s)\n", ret,
ftdi_get_error_string(ftdi));
goto err_out;
}
out:
ftdi_free(ftdi);
return 0;
err_out:
ftdi_free(ftdi);
return -1;
}
void usage(char *argv0)
{
printf("Usage: %s check|reset|power|longpower\n", basename(argv0));
}
int main(int argc, char *argv[])
{
enum command cmd;
if (argc != 2) {
usage(argv[0]);
return -1;
}
if (strcmp(argv[1], "check") == 0) {
cmd = CHECK;
} else if (strcmp(argv[1], "reset") == 0) {
cmd = RESET;
} else if (strcmp(argv[1], "power") == 0) {
cmd = POWER;
} else if (strcmp(argv[1], "longpower") == 0) {
cmd = LONGPOWER;
} else {
usage(argv[0]);
return -1;
}
return doit(cmd);
}