/
usb.cpp
259 lines (250 loc) · 6.67 KB
/
usb.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
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
#include "usb.h"
#include "usb.pio.h"
#include <stdio.h>
#include <string.h>
#include "pico/stdlib.h"
#include <deque>
#include <set>
#define INTERVAL 10 // interrupt transfer period [mS]
#define GPIO_USB 26 // GPIO26 for D-, GPIO27 for D+
#define KEYREP1 30
#define KEYREP2 5
static PIO pio;
static uint sm;
static u8 usbbuf[8];
struct Key {
void update(u32 _usage, u8 _mod) {
usages = _usage;
mod = _mod;
std::set<u8> curset;
for (int i = 0; i < 32; i += 8) {
u8 d = usages >> i;
if (d) {
curset.insert(d);
if (!lastset.count(d)) {
q.push_back({ lastid = d, lastmod = mod });
timer = KEYREP1;
}
}
}
lastset = curset;
}
void repeat() {
if (timer && !--timer && mod == lastmod) {
int i;
for (i = 0; i < 32 && lastid != usages >> i & 0xff; i += 8)
;
if (i < 32) {
q.push_back({ lastid, lastmod });
timer = KEYREP2;
}
}
}
u32 usages;
u8 mod, lastmod, lastid, timer;
std::set<u8> lastset;
std::deque<std::pair<u8, u8>> q;
} key;
bool keyget(u8 *usageP, u8 *modP) {
if (key.q.empty()) return false;
auto [ usage, mod ] = key.q.front();
key.q.pop_front();
if (usageP) *usageP = usage;
if (modP) *modP = mod;
return true;
}
struct NRZI {
static constexpr int N = 8;
constexpr NRZI() : b(), d(), ofs(0), len(0), count(0), cur(1) {
for (u8 i = 0; i < 2 * N; i++) b[i] = 0;
for (u8 i = 0; i < N; i++) d[i] = 0;
}
constexpr void append(u16 c, u8 n) {
for (u8 i = 0; i < n; i++) {
if (c >> i & 1) b[ofs >> 3] |= 1 << (ofs & 7);
ofs++;
}
}
constexpr void sync() { append(0x80, 8); }
constexpr void ack() { append(0xd2, 8); }
constexpr void setup() { append(0x2d, 8); }
constexpr void data0() { append(0xc3, 8); }
constexpr void in() { append(0x69, 8); }
constexpr void address(u8 v) { append(v, 7); }
constexpr void endpoint(u8 v) { append(v, 4); }
constexpr void bmRequestType(u8 v) { append(v, 8); }
constexpr void bRequest(u8 v) { append(v, 8); }
constexpr void wValue(u16 v) { append(v, 16); }
constexpr void wIndex(u16 v) { append(v, 16); }
constexpr void wLength(u16 v) { append(v, 16); }
constexpr u16 calc_crc(u16 poly, u16 mask) {
u16 data = 0, crc = mask;
for (u8 i = 16; i < ofs; i++) {
if (!(i & 7)) data = b[i >> 3];
bool f = (data ^ crc) & 1;
data >>= 1;
crc >>= 1;
if (f) crc ^= poly;
}
return ~crc & mask;
}
constexpr void crc5() { append(calc_crc(0x14, 0x1f), 5); }
constexpr void crc16() { append(calc_crc(0xa001, 0xffff), 16); }
constexpr void end() {
auto f = [&](u8 v) {
len >> 4 < N || printf(""); // compile should fail if overflow
d[len >> 4] |= v << ((len & 15) << 1);
len++;
};
for (u8 i = 0; i < ofs; i++) {
if (b[i >> 3] >> (i & 7) & 1) {
f(cur);
if (++count < 6) continue;
}
f(cur = ~cur & 3);
count = 0;
}
len += 2;
f(1); f(1);
}
u8 ofs, len, count, cur;
u8 b[2 * N];
u32 d[N];
};
static constexpr struct EOP : NRZI {
constexpr EOP() : NRZI() {
end();
}
} eop;
static constexpr struct Ack : NRZI {
constexpr Ack() : NRZI() {
sync(); ack(); end();
}
} ack;
static constexpr struct Setup : NRZI {
constexpr Setup(int a) : NRZI() {
sync(); setup(); address(a); endpoint(0); crc5(); end();
}
} setup0(0), setup1(1);
static constexpr struct SetAddress1 : NRZI {
constexpr SetAddress1() : NRZI() {
sync(); data0(); bmRequestType(0); bRequest(5); wValue(1); wIndex(0); wLength(0); crc16(); end();
}
} setaddress1;
static constexpr struct SetConfiguration1 : NRZI {
constexpr SetConfiguration1() : NRZI() {
sync(); data0(); bmRequestType(0); bRequest(9); wValue(1); wIndex(0); wLength(0); crc16(); end();
}
} setconfiguration1;
static constexpr struct DataIn : NRZI {
constexpr DataIn(int a, int e) : NRZI() {
sync(); in(); address(a); endpoint(e); crc5(); end();
}
} datain00(0, 0), datain10(1, 0), datain11(1, 1);
static void __no_inline_not_in_flash_func(usb_trans)(const NRZI &nrzi, bool recv) {
if (recv) memset(usbbuf, 0, sizeof(usbbuf));
pio_sm_put_blocking(pio, sm, (recv ? 0x20000 : 0x10000) | nrzi.len - 1);
for (u8 i = 0; i < nrzi.len + 15 >> 4; i++)
pio_sm_put_blocking(pio, sm, nrzi.d[i]);
if (!recv) return;
u8 d, last = 0, index = 0, count = 0, mask = 1;
do {
d = pio_sm_get_blocking(pio, sm);
if (d == last) {
if (index < sizeof(usbbuf)) usbbuf[index] |= mask;
if (!(mask <<= 1)) {
mask = 1;
index++;
}
count++;
}
else {
last = d;
if (count < 6 && !(mask <<= 1)) {
mask = 1;
index++;
}
count = 0;
}
} while (d);
}
static bool __no_inline_not_in_flash_func(usb_task)(repeating_timer_t *timer) {
enum State {
STATE_INIT, STATE_WAIT, STATE_RECHECK, STATE_RECHECK_DISCON,
STATE_RESET, STATE_EOP40, STATE_CONFIG, STATE_CONNECTED
};
static enum State state, nextstate;
static u16 count;
auto wait = [&](State s, int c) { nextstate = s; count = c; state = STATE_WAIT; };
auto DATA0orDATA1 = [] { return usbbuf[1] == 0xc3 || usbbuf[1] == 0x4b; };
auto datain = [&](const NRZI &pkt) {
u8 i = 100;
do {
if (!--i) return true;
usb_trans(pkt, true);
} while (!DATA0orDATA1());
usb_trans(ack, false);
return false;
};
switch (state) {
case STATE_INIT:
if (gpio_get(GPIO_USB)) wait(STATE_RECHECK, 500);
break;
case STATE_RECHECK:
if (gpio_get(GPIO_USB)) wait(STATE_RESET, 200);
else state = STATE_INIT;
break;
case STATE_RESET:
printf("USB: bus reset\n");
pio_sm_put_blocking(pio, sm, 15000); // 10mS
wait(STATE_EOP40, 10);
break;
case STATE_EOP40:
usb_trans(eop, false);
if (++count >= 40) state = STATE_CONFIG;
break;
case STATE_CONFIG:
usb_trans(setup0, false);
usb_trans(setaddress1, true);
if (datain(datain00)) break;
usb_trans(setup1, false);
usb_trans(setconfiguration1, true);
if (datain(datain10)) break;
printf("USB: connected.\n");
state = STATE_CONNECTED;
count = 0;
break;
case STATE_CONNECTED:
if (gpio_get(GPIO_USB)) {
usb_trans(eop, false);
if (++count < INTERVAL) break;
count = 0;
key.repeat();
usb_trans(datain11, true);
if (!DATA0orDATA1()) break;
usb_trans(ack, false);
#if 0
printf("PID:%02X MOD:%02X KEY1:%02X KEY2:%02X KEY3:%02X KEY4:%02X\n",
usbbuf[1], usbbuf[2], usbbuf[4], usbbuf[5], usbbuf[6], usbbuf[7]);
#endif
key.update((u32 &)usbbuf[4], usbbuf[2]);
}
else wait(STATE_RECHECK_DISCON, 500);
break;
case STATE_RECHECK_DISCON:
state = gpio_get(GPIO_USB) ? STATE_CONNECTED : STATE_INIT;
break;
case STATE_WAIT:
if (!--count) state = nextstate;
break;
}
return true;
}
void usb_init(PIO _pio) {
pio = _pio;
uint offset = pio_add_program(pio, &usb_program);
sm = pio_claim_unused_sm(pio, true);
usb_program_init(pio, sm, offset);
static repeating_timer_t timer;
add_repeating_timer_ms(-1, &usb_task, nullptr, &timer);
}