-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlibpin.c
359 lines (297 loc) · 8.83 KB
/
libpin.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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
#include <assert.h>
#include <errno.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <endian.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include "libpin.h"
#include "pinutil.h"
/* swd pin definitions */
#define PIN_CLOCK 20
#define PIN_DATA 21
/* both signals are active low
* external - goes to a fet off the gpio to get a hard power-on reset
* reset - goes to the actual pin for soft reset
*/
#define PIN_RESET 16
#define PIN_EXT_POR 18
/* ez port defines */
#define PIN_EZ_CS 23
#define PIN_EZ_DO 24
#define PIN_EZ_DI 25
/* for experimenting w/ the swd data line */
#define PIN_HIZ_DATA 1
struct pinctl {
unsigned phase; /* us */
/* direction change requires turnaround time clockin. this is
* inserted on the next bus op.
*/
int last_rw_op;
int fd;
uint32_t* regs;
};
static void clock_out_bit(struct pinctl* c, int p, int val)
{
PIN_WRITE(c->regs, p, val);
PIN_WRITE(c->regs, PIN_CLOCK, 1);
usleep(c->phase);
PIN_WRITE(c->regs, PIN_CLOCK, 0);
usleep(c->phase);
}
static uint8_t clock_in_bit(struct pinctl* c, int p)
{
uint8_t rv = PIN_READ(c->regs, p);
PIN_WRITE(c->regs, PIN_CLOCK, 1);
usleep(c->phase);
PIN_WRITE(c->regs, PIN_CLOCK, 0);
usleep(c->phase);
return rv;
}
static int clock_turnaround(struct pinctl* c, int op)
{
if (c->last_rw_op == op)
return 0;
/* the pin is not supposed be driven by either host or target, but
* when we change to input state the drive stops. dropping the pin
* is only so that it shows up on the analyzer before the mode
* changes to allow for the pin not to be driven.
*/
if (c->last_rw_op)
PIN_WRITE(c->regs, PIN_DATA, PIN_HIZ_DATA <= 0 ? 0 : 1);
PIN_DIR(c->regs, PIN_DATA, 0);
for (int i = 0; i < 1; i++)
clock_in_bit(c, PIN_DATA);
if (op)
PIN_DIR(c->regs, PIN_DATA, 1);
c->last_rw_op = op;
return 1;
}
struct pinctl* pins_open(unsigned phase)
{
/* make sure the clock rate is representable w/ usleep(3) since i'm
* lazy and linux isn't quite rtos-y. also the spec says that the max
* clock phase time is 500us.
*/
if (phase < 10) {
fprintf(stderr, "%s:%d: phase:%uus not representable\n", __func__, __LINE__, phase);
return NULL;
} else if (phase > 500) {
fprintf(stderr, "%s:%d: phase:%uus out of spec (max 500us)\n", __func__, __LINE__, phase);
return NULL;
}
printf("%s:%d: clock phase:time %uus\n", __func__, __LINE__, phase);
struct pinctl* rv = malloc(sizeof(*rv));
if (!rv) goto err_exit;
rv->fd = -1;
rv->regs = MAP_FAILED;
rv->phase = phase;
rv->fd = open("/dev/gpiomem", O_RDWR);
if (rv->fd == -1) {
fprintf(stderr, "%s:%d: open():%d:%s\n", __func__, __LINE__, errno, strerror(errno));
goto err_exit;
}
rv->regs = mmap(0, REG_MAP_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, rv->fd, 0);
if (rv->regs == MAP_FAILED) {
fprintf(stderr, "%s:%d: mmap():%d:%s\n", __func__, __LINE__, errno, strerror(errno));
goto err_exit;
}
/* setup high-z configuration.
* data - up. the device is supposed to have this set?
* clock - down. this is always host controlled?
*/
PIN_CONFIG_HIZ(rv->regs, PIN_CLOCK, -1);
PIN_CONFIG_HIZ(rv->regs, PIN_DATA, PIN_HIZ_DATA);
/* default data to output */
rv->last_rw_op = 1;
PIN_DIR(rv->regs, PIN_CLOCK, 1);
PIN_DIR(rv->regs, PIN_DATA, 1);
/* default to regular operation */
PIN_WRITE(rv->regs, PIN_DATA, 1);
PIN_WRITE(rv->regs, PIN_CLOCK, 0);
/* the mchck reset pin isn't exposed (and i fucked the pad), so futz
* w/ external power which is almost the same thing. this is
* separate from the ezport (except for checking ez cs at reset
* time), so is outside of that config.
*/
PIN_CONFIG_HIZ(rv->regs, PIN_EXT_POR, 1);
PIN_DIR(rv->regs, PIN_EXT_POR, 1);
PIN_WRITE(rv->regs, PIN_EXT_POR, 0);
/* the reset pin has a chip-internal pull-up, but wwhen we wait to
* come out of reset.
*/
PIN_CONFIG_HIZ(rv->regs, PIN_RESET, 1);
PIN_DIR(rv->regs, PIN_RESET, 0);
#if PIN_EZ
PIN_CONFIG_HIZ(rv->regs, PIN_EZ_CS, 1);
PIN_CONFIG_HIZ(rv->regs, PIN_EZ_DO, 0);
PIN_CONFIG_HIZ(rv->regs, PIN_EZ_DI, 0);
PIN_DIR(rv->regs, PIN_EZ_CS, 1);
PIN_DIR(rv->regs, PIN_EZ_DO, 0);
PIN_DIR(rv->regs, PIN_EZ_DI, 1);
PIN_WRITE(rv->regs, PIN_EZ_CS, 0);
PIN_READ(rv->regs, PIN_EZ_DO);
PIN_WRITE(rv->regs, PIN_EZ_DI, 0);
ez_reset(rv);
#endif /* PIN_EZ */
#if 0
usleep(200000);
PIN_WRITE(rv->regs, PIN_EXT_POR, 1);
usleep(200000);
PIN_WRITE(rv->regs, PIN_EXT_POR, 0);
usleep(200000);
// assert(0);
#endif
return rv;
err_exit:
pins_close(rv);
return NULL;
}
void pins_close(struct pinctl* c)
{
if (!c) return;
if ((c->regs != MAP_FAILED) && (munmap(c->regs, REG_MAP_SIZE) != 0))
fprintf(stderr, "%s:%d: munmap():%d:%s\n", __func__, __LINE__, errno, strerror(errno));
if ((c->fd != -1) && (close(c->fd) != 0))
fprintf(stderr, "%s:%d: close():%d:%s\n", __func__, __LINE__, errno, strerror(errno));
free(c);
}
int pins_write(struct pinctl* c, const uint8_t* bs, int nb)
{
assert(c != NULL);
assert(c->regs != MAP_FAILED);
/* turnaround time? */
clock_turnaround(c, 1);
int i, j;
for (i = 0; i < nb / 8; i++)
for (j = 0; j < 8; j++)
clock_out_bit(c, PIN_DATA, bs[i] & (1 << j));
for (j = 0; j < (nb & 7); j++)
clock_out_bit(c, PIN_DATA, bs[i] & (1 << j));
return nb;
}
int pins_read(struct pinctl* c, uint8_t* bs, int nb)
{
assert(c != NULL);
assert(c->regs != MAP_FAILED);
/* turnaround time? */
clock_turnaround(c, 0);
int i, j;
for (i = 0; i < nb / 8; i++) {
bs[i] = 0;
for (j = 0; j < 8; j++)
bs[i] |= (clock_in_bit(c, PIN_DATA) << j);
}
bs[i] = 0;
for (j = 0; j < (nb & 7); j++)
bs[i] |= (clock_in_bit(c, PIN_DATA) << j);
return nb;
}
int ez_write(struct pinctl* c, const uint8_t* bs, int nb)
{
assert(c != NULL);
assert(c->regs != MAP_FAILED);
PIN_DIR(c->regs, PIN_EZ_DO, 0);
PIN_DIR(c->regs, PIN_EZ_DI, 1);
PIN_WRITE(c->regs, PIN_EZ_CS, 0);
usleep(c->phase);
/* to be as consistent as possible, ez port is msb-first, but are
* 'complete' octets, so that's nice.
*/
for (int i = 0; i < nb; i++)
for (int j = 7; j >= 0; j--)
clock_out_bit(c, PIN_EZ_DI, bs[i] & (1 << j));
PIN_WRITE(c->regs, PIN_EZ_CS, 1);
PIN_DIR(c->regs, PIN_EZ_DO, 0);
PIN_DIR(c->regs, PIN_EZ_DI, 0);
usleep(2 * c->phase);
return nb;
}
int ez_read(struct pinctl* c, uint8_t* bs, int nb)
{
assert(c != NULL);
assert(c->regs != MAP_FAILED);
PIN_DIR(c->regs, PIN_EZ_DO, 0);
PIN_DIR(c->regs, PIN_EZ_DI, 0);
PIN_WRITE(c->regs, PIN_EZ_CS, 0);
/* to be as consistent as possible, ez port is msb-first, but are
* 'complete' octets, so that's nice.
*/
for (int i = 0; i < nb; i++) {
uint8_t v = 0;
for (int j = 7; j >= 0; j--)
v |= clock_in_bit(c, PIN_EZ_DO) << j;
bs[i] = v;
}
PIN_WRITE(c->regs, PIN_EZ_CS, 1);
usleep(2 * c->phase);
return nb;
}
int pins_reset(struct pinctl* c, int external)
{
/* drop all the outputs so that we don't 'power' the target. */
PIN_WRITE(c->regs, PIN_CLOCK, 0);
PIN_WRITE(c->regs, PIN_DATA, 0);
if (external) {
PIN_WRITE(c->regs, PIN_EXT_POR, 1);
usleep(1000000);
PIN_WRITE(c->regs, PIN_EXT_POR, 0);
PIN_DIR(c->regs, PIN_RESET, 0);
int i, v;
for (i = 0, v = PIN_READ(c->regs, PIN_RESET); !v; i++, v = PIN_READ(c->regs, PIN_RESET))
usleep(1000);
fprintf(stderr, "%s:%d: n:%dms\n", __func__, __LINE__, i);
} else {
PIN_WRITE(c->regs, PIN_RESET, 0);
PIN_DIR(c->regs, PIN_RESET, 1);
usleep(1000);
}
/* wait for reset to release */
PIN_DIR(c->regs, PIN_RESET, 0);
int i, v;
for (i = 0, v = PIN_READ(c->regs, PIN_RESET); !v; i++, v = PIN_READ(c->regs, PIN_RESET))
usleep(1000);
fprintf(stderr, "%s:%d: reset release %dms\n", __func__, __LINE__, i);
return 0;
}
int ez_reset(struct pinctl* c)
{
#if !PIN_EZ
// assert(0);
#endif
/* drop all the outputs so that we don't 'power' the target. */
PIN_WRITE(c->regs, PIN_CLOCK, 0);
PIN_WRITE(c->regs, PIN_DATA, 0);
/* enable ezport cs so that when we toggle power it's waiting */
PIN_WRITE(c->regs, PIN_EZ_CS, 0);
usleep(10000);
/* bounce power since i busted the reset line, and there's no
* programmatic control if we're coming through here (eg, security
* is enabled).
*/
PIN_WRITE(c->regs, PIN_EXT_POR, 1);
usleep(100000);
PIN_WRITE(c->regs, PIN_EXT_POR, 0);
usleep(100000);
PIN_WRITE(c->regs, PIN_EZ_CS, 1);
usleep(10000);
/* check that the ezport stuff came back ok? */
uint8_t bs[] = { 0x05 };
ez_write(c, bs, sizeof(bs));
ez_read(c, bs, sizeof(bs));
fprintf(stderr, "%s:%d: status:%02x\n", __func__, __LINE__, bs[0]);
bs[0] = 0xb9;
ez_write(c, bs, sizeof(bs));
usleep(10000);
bs[0] = 0xb9;
ez_write(c, bs, sizeof(bs));
ez_read(c, bs, sizeof(bs));
fprintf(stderr, "%s:%d: status:%02x\n", __func__, __LINE__, bs[0]);
// assert(0);
return !!(bs[0] & (1 << 7));
}