-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpipe_packet.c
413 lines (361 loc) · 15 KB
/
pipe_packet.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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
/* Talk to the USB link, and interface to the packet descriptor queues in
* shared memory space.
*
* 27 Sep 2021
*
* MIT License
*
* Copyright (c) 2021 Matt Evans
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include "pico/stdlib.h"
#include "hardware/gpio.h"
#include "tusb.h"
#include "hw.h"
#include "version.h"
#include "podule_interface.h"
#include "podule_regs.h"
#define DEBUG 2
typedef struct {
bool tx_ongoing;
unsigned int tx_total;
unsigned int tx_pos;
uint8_t tx_buf[512 + 3];
unsigned int rx_total;
unsigned int rx_pos;
unsigned int rx_last_descr;
bool rx_packet_pending;
uint8_t rx_buf[512 + 3];
} pp_state_t;
static pp_state_t state;
#define PKT_HDR_SIZE 3
// Called at init, but can also be requested by loader on soft reset:
void pipe_init(void)
{
volatile uint8_t *r = podule_if_get_regs();
// Reset packet registers
memset((void *)&r[PR_TX0_0], 0, PR_NUM_DESCRS*4);
memset((void *)&r[PR_RX0_0], 0, PR_NUM_DESCRS*4);
r[PR_TX_TAIL] = 0;
r[PR_RX_HEAD] = 0;
// Reset state
state.tx_ongoing = false;
state.rx_pos = 0;
state.tx_pos = 0;
state.rx_last_descr = 0;
state.rx_packet_pending = false;
}
static void pipe_tx_done(void)
{
volatile uint8_t *r = podule_if_get_regs();
unsigned int tail = r[PR_TX_TAIL];
// Descriptor clean/non-ready:
PR_TX_DESCR(r, tail) = PR_TX_DESCR(r, tail) & ~(PR_DESCR_READY);
// Move on tail pointer:
tail = (tail + 1) & PR_DESCRS_MASK;
r[PR_TX_TAIL] = tail;
}
static void pipe_tx_start(uint32_t descr)
{
volatile uint8_t *r = podule_if_get_regs();
unsigned int len = PR_DESCR_SIZE(descr);
unsigned int cid = PR_DESCR_CID(descr);
unsigned int addr = PR_DESCR_ADDR(descr);
uint8_t *tx_data = (uint8_t *)((uintptr_t)&r[PR_TX_BUFFERS] +
(uintptr_t)addr);
if (addr + len >= PR_RX_TX_BUFSZ) {
printf("[pipe TX ERROR: TX off end of buffer! "
"%08x, CID%d, addr %d, len %d - dropping packet]\n",
descr, cid, addr, len);
pipe_tx_done();
return;
}
#if DEBUG > 0
printf("[pipe TX start: %08x: CID%d, addr %d/%p, len %d]\n",
descr, cid, addr, tx_data, len);
#endif
/* One wart is that we send a little header before the data.
*
* An approach to this (given that the tud_cdc_n_write() below
* might just accept 1 byte right now) is to have a FSM
* tracking how much of the header's been sent, moving onto
* the data eventually.
*
* It's much easier, though, to just assemble the outgoing
* data into a buffer (memcpy the payload) and track progress
* through that.
*/
state.tx_buf[0] = cid;
state.tx_buf[1] = len & 0xff;
state.tx_buf[2] = (len >> 8) & 0xff;
memcpy(&state.tx_buf[PKT_HDR_SIZE], tx_data, len);
state.tx_total = len + 3;
/* Try to queue as much as possible in the USB TX FIFOs: */
unsigned int tx_written = tud_cdc_n_write(0, state.tx_buf, state.tx_total);
tud_cdc_n_write_flush(0);
#if DEBUG > 2
for (int i = 0; i < tx_written; i++) {
printf("%02x ", state.tx_buf[i]);
}
printf("\n");
#endif
if (tx_written == state.tx_total) {
#if DEBUG > 1
printf("[pipe TX done: submitted %d in one go]\n", tx_written);
#endif
/* Submitted entire packet, now it's SEP. */
state.tx_ongoing = false;
pipe_tx_done();
} else {
#if DEBUG > 1
printf("[pipe TX ongoing: submitted %d, %d total]\n",
tx_written, state.tx_total);
#endif
/* We're not done with the packet, there's more work
* to do later on.
*
* The descriptor is still marked ready in the queue,
* but this state change inhibits the register i/f
* continually starting a new tx, and the rest of the
* work occurs via pipe_tx_continue().
*/
state.tx_ongoing = true;
state.tx_pos = tx_written;
/* The packet descriptor remains ready/unconsumed;
* it's consumed in pipe_tx_done(). We could consume
* it here just as easily.
*
* Note this would be a different semantic of queue
* view, where consuming a packet doesn't mean it's
* hit USB yet.
*/
}
}
static void pipe_tx_continue(void)
{
if (!state.tx_ongoing) {
printf("[pipe TX ERROR: continue, but no work to do!\n");
return;
}
/* Send more data: */
unsigned int tx_written = tud_cdc_n_write(0,
&state.tx_buf[state.tx_pos],
state.tx_total - state.tx_pos);
tud_cdc_n_write_flush(0);
state.tx_pos += tx_written;
if (state.tx_pos >= state.tx_total) {
#if DEBUG > 1
printf("[pipe TX complete: submitted %d of %d total]\n",
tx_written, state.tx_total);
#endif
state.tx_ongoing = false;
pipe_tx_done();
} else {
if (tx_written != 0) {
// If it's really busy, and repeatedly writing 0, be quiet.
#if DEBUG > 1
printf("[pipe TX ongoing2: submitted %d, now %d of %d total]\n",
tx_written, state.tx_pos, state.tx_total);
#endif
}
}
}
/* Add packet to RX buffer. Returns true if accepted/there is space,
* allowing rudimentary flow-control/backpressure.
*/
static bool pipe_rx_packet(uint8_t cid, uint16_t len, uint8_t *data)
{
volatile uint8_t *r = podule_if_get_regs();
// FIXME: Multiple RX buffers!
unsigned int last = state.rx_last_descr;
unsigned int head = r[PR_RX_HEAD];
/* Before then, this is a hack. It makes sure there are no
* unconsumed RX descriptors (checking that the last created
* has been consumed), because an unconsumed descriptor means
* an unconsumed RX buffer (and there's only one of those).
*/
if (PR_DESCR_IS_READY(PR_RX_DESCR(r, last))) {
static int last_last = -1;
if (last_last != last) {
// Dumb rate-limiting
printf("[pipe RX: No room for packet]\n");
last_last = last;
}
return false;
}
if (len > PR_RX_TX_BUFSZ) {
printf("[pipe RX ERROR: Packet size %d is too large! Dropping.]\n",
len);
return true;
}
/* Create a descriptor at PR_RX_HEAD: */
unsigned int addr = 0; // Right at the start of the RX buffer
memcpy((void *)&r[PR_RX_BUFFERS + addr], data, len);
// Once data is in place, construct the descriptor -- making it ready:
PR_RX_DESCR(r, head) = ((uint32_t)cid << PR_DESCR_CID_SHIFT) |
((uint32_t)(len-1) << PR_DESCR_SIZE_SHIFT) |
(addr << PR_DESCR_ADDR_SHIFT) |
PR_DESCR_READY;
state.rx_last_descr = head;
// Move on head pointer:
head = (head + 1) & PR_DESCRS_MASK;
r[PR_RX_HEAD] = head;
return true;
}
/* Assembles a single packet from possibly multi-chunk multi-receives,
* staging the data into the state.rx_buf buffer until it's complete, then
* copying that into the RX buffer area.
*
* This could be made more complex and zero-copy by receiving just the header
* (to work out size) then allocating a correctly-sized block in the RX area,
* but "meh" and we're not close to needing to scrimp on memory either.
*/
static void pipe_rx(void)
{
unsigned int len = 0;
static unsigned int pkt_counter = 0;
/* If a packet's pending, try to deliver it without receiving more USB
* data.
*/
if (!state.rx_packet_pending) {
/* If we're in the middle of a packet, state.rx_pos is
* non-zero so that this receive places data at the end of
* previous data: */
len = tud_cdc_n_read(0, &state.rx_buf[state.rx_pos],
sizeof(state.rx_buf) - state.rx_pos);
#if DEBUG > 0
printf("[pipe RX %d bytes]\n", len);
#endif
state.rx_pos += len;
}
// Check for packet partial/complete:
if (state.rx_pos >= PKT_HDR_SIZE) {
// Decode size
uint16_t data_len = state.rx_buf[1] | ((uint32_t)state.rx_buf[2] << 8);
state.rx_total = PKT_HDR_SIZE + data_len;
#if DEBUG > 2
printf("[pipe RX packet header: CID%d, size %d (data size %d)]\n",
state.rx_buf[0], state.rx_total, data_len);
#endif
// Did we get all of it?
if (state.rx_pos >= state.rx_total) {
// Pop it into the RX buffer/descriptor for Arc to see:
bool accepted = pipe_rx_packet(state.rx_buf[0],
data_len,
&state.rx_buf[PKT_HDR_SIZE]);
#if DEBUG > 1
if (accepted) {
printf("[pipe RX packet complete: CID%d, size %d"
" (data size %d)]\n",
state.rx_buf[0], state.rx_total,
data_len);
} else {
// Rate-limit this!
static int last_count = ~0;
if (pkt_counter != last_count) {
printf("[pipe RX packet stalled: "
"CID%d, size %d (data size %d)]\n",
state.rx_buf[0], state.rx_total,
data_len);
last_count = pkt_counter;
}
}
#endif
/* If it wasn't accepted (no room in RX queue,
* Arc isn't listening) then just quit now.
* It's safe, because next loop we'll try this
* all over again. In the meantime, USB reads
* aren't performed so the backpressure
* propagates to the host.
*/
if (!accepted) {
state.rx_packet_pending = true;
return;
} else {
pkt_counter++;
state.rx_packet_pending = false;
}
// Did we read too much/some of the next packet?
if (state.rx_pos > state.rx_total) {
/* Move the excess data read to the
* bottom of the buffer, so that the
* next read just tacks data onto the
* end:
*/
unsigned int excess = state.rx_total -
state.rx_pos;
memmove(&state.rx_buf[0],
&state.rx_buf[state.rx_total],
excess);
state.rx_pos = excess;
#if DEBUG > 1
printf("[pipe RX packet excess %d]\n",
excess);
#endif
} else {
/* We read an exact amount, Complete now,
* and next RX occurs at the start, afresh.
*/
state.rx_pos = 0;
}
}
}
// Else do nothing, next reception we'll check again.
}
// Check whether the packet descriptors have some work for us (or an ongoing transfer)
void pipe_poll(void)
{
volatile uint8_t *r = podule_if_get_regs();
/* Check USB */
bool cdc_connected = tud_cdc_n_connected(0);
static bool last_connected = false;
if (!cdc_connected && last_connected) {
// Disconnect occurred!
printf("[pipe disconnected]\n");
}
last_connected = cdc_connected;
//////////////////////////////////////////////////////////////////////
// Receive
if ((cdc_connected && tud_cdc_n_available(0)) || state.rx_packet_pending) {
pipe_rx();
}
//////////////////////////////////////////////////////////////////////
// Transmit
if (state.tx_ongoing) {
if (cdc_connected) {
pipe_tx_continue();
} else {
state.tx_ongoing = false;
}
} else {
/* Check registers: */
unsigned int tail = r[PR_TX_TAIL];
uint32_t descr = PR_TX_DESCR(r, tail);
if (PR_DESCR_IS_READY(descr)) {
if (cdc_connected)
pipe_tx_start(descr);
else
pipe_tx_done(); // Consume immediately
}
}
}