|
1 | 1 | #include "firmware.h"
|
2 |
| - |
3 |
| -#define FLASH_BUFFER_SIZE 64 |
| 2 | +#define OUT_RING_SIZE 10 |
| 3 | +#define PACKET_SIZE 64 |
| 4 | +u8 out_ring_buf[OUT_RING_SIZE][PACKET_SIZE]; |
| 5 | +volatile u8 out_ring_count = 0; // Number of packets in the ring buffer |
| 6 | +volatile u8 out_ring_write_pos = 0; // Packet index in which we're currently receiving a packet, or will once it's free |
| 7 | +volatile u8 out_ring_read_pos = 0; // Packet index from which we're currently sending a packet, or will once it's filled. |
| 8 | +volatile u8 out_ring_short_packet = 0; // If nonzero, the ring ends with a short packet of this size |
| 9 | +volatile bool out_usb_pending = false; |
| 10 | +volatile bool out_bridge_pending = false; |
4 | 11 |
|
5 | 12 | typedef enum {
|
6 | 13 | PIPE_DISABLE,
|
7 | 14 | PIPE_WAIT_FOR_USB,
|
8 | 15 | PIPE_WAIT_FOR_BRIDGE,
|
9 | 16 | } PipeState;
|
10 | 17 |
|
11 |
| -PipeState pipe_state_pc_to_soc; |
12 | 18 | PipeState pipe_state_soc_to_pc;
|
13 |
| -USB_ALIGN u8 pipe_buffer_pc_to_soc[BRIDGE_BUF_SIZE]; |
14 | 19 | USB_ALIGN u8 pipe_buffer_soc_to_pc[BRIDGE_BUF_SIZE];
|
15 | 20 |
|
16 | 21 | void usbpipe_init() {
|
17 | 22 | usb_enable_ep(USB_EP_PIPE_OUT, USB_EP_TYPE_BULK, 64);
|
18 | 23 | usb_enable_ep(USB_EP_PIPE_IN, USB_EP_TYPE_BULK, 64);
|
19 | 24 |
|
20 |
| - usb_ep_start_out(USB_EP_PIPE_OUT, pipe_buffer_pc_to_soc, FLASH_BUFFER_SIZE); |
21 |
| - pipe_state_pc_to_soc = PIPE_WAIT_FOR_USB; |
| 25 | + usb_ep_start_out(USB_EP_PIPE_OUT, out_ring_buf[out_ring_write_pos], PACKET_SIZE); |
| 26 | + out_usb_pending = true; |
| 27 | + out_bridge_pending = false; |
22 | 28 |
|
23 | 29 | bridge_start_out(BRIDGE_USB, pipe_buffer_soc_to_pc);
|
24 |
| - pipe_state_soc_to_pc = PIPE_WAIT_FOR_BRIDGE; |
| 30 | + pipe_state_soc_to_pc = PIPE_WAIT_FOR_BRIDGE; |
| 31 | + |
| 32 | + bridge_enable_chan(BRIDGE_USB); // Tells SPI Daemon to start connection to USB Daemon |
25 | 33 | }
|
26 | 34 |
|
27 | 35 | void usbpipe_disable() {
|
28 | 36 | usb_disable_ep(USB_EP_PIPE_IN);
|
29 | 37 | usb_disable_ep(USB_EP_PIPE_OUT);
|
30 |
| - pipe_state_pc_to_soc = PIPE_DISABLE; |
| 38 | + out_ring_count = 0; |
| 39 | + out_ring_write_pos = 0; |
| 40 | + out_ring_read_pos = 0; |
| 41 | + out_ring_short_packet = 0; |
31 | 42 | pipe_state_soc_to_pc = PIPE_DISABLE;
|
| 43 | + bridge_disable_chan(BRIDGE_USB); // Tells SPI Daemon to close connection to USB Daemon |
32 | 44 | }
|
33 | 45 |
|
34 |
| -// Received from USB, send to bridge |
35 |
| -void pipe_usb_out_completion() { |
36 |
| - if (pipe_state_pc_to_soc == PIPE_WAIT_FOR_USB) { |
37 |
| - u32 len = usb_ep_out_length(USB_EP_PIPE_OUT); |
38 |
| - bridge_start_in(BRIDGE_USB, pipe_buffer_pc_to_soc, len); |
39 |
| - pipe_state_pc_to_soc = PIPE_WAIT_FOR_BRIDGE; |
40 |
| - } else { |
41 |
| - invalid(); |
| 46 | +void out_ring_step() { |
| 47 | + // If we are not currently receiving |
| 48 | + // And there is an empty buffer to receive data into |
| 49 | + // And there isn't an unprocessed short packet |
| 50 | + if (!out_usb_pending && out_ring_count < OUT_RING_SIZE && out_ring_short_packet == 0) { |
| 51 | + // Start reading data in over USB to the buffer at the correct position (up to 64 bytes) |
| 52 | + usb_ep_start_out(USB_EP_PIPE_OUT, out_ring_buf[out_ring_write_pos], PACKET_SIZE); |
| 53 | + // We are waiting for the transfer to complete |
| 54 | + out_usb_pending = true; |
| 55 | + } |
| 56 | + |
| 57 | + // If we are not waiting on a bridge transaction to complete and we have packets to send |
| 58 | + if (!out_bridge_pending && out_ring_count > 0) { |
| 59 | + // The size of the packet is 64 bytes (unless the below case is true) |
| 60 | + u8 len = PACKET_SIZE; |
| 61 | + // If we only have one outgoing packet and it is a short packet |
| 62 | + if (out_ring_count == 1 && out_ring_short_packet != 0) { |
| 63 | + // The length is actually a subset of a full packet |
| 64 | + len = out_ring_short_packet; |
| 65 | + // Reset the short packet var |
| 66 | + out_ring_short_packet = 0; |
| 67 | + } |
| 68 | + // Start sending data to the spi daemon |
| 69 | + bridge_start_in(BRIDGE_USB, out_ring_buf[out_ring_read_pos], len); |
| 70 | + // We are currently waiting on the SPI |
| 71 | + out_bridge_pending = true; |
42 | 72 | }
|
| 73 | +} |
43 | 74 |
|
| 75 | +// Received from USB, send to bridge |
| 76 | +void pipe_usb_out_completion() { |
| 77 | + // Get the length of the packet from USB |
| 78 | + u32 len = usb_ep_out_length(USB_EP_PIPE_OUT); |
| 79 | + // If it is less than one full packet, mark the short packet var with the length |
| 80 | + if (len < PACKET_SIZE) out_ring_short_packet = len; |
| 81 | + // Increase the next writable buffer by 1 (but loop to the beginning if necessary) |
| 82 | + out_ring_write_pos = (out_ring_write_pos + 1) % OUT_RING_SIZE; |
| 83 | + // Mark that we have one packet that needs attention |
| 84 | + out_ring_count += 1; |
| 85 | + // We are no longer operating over USB |
| 86 | + out_usb_pending = false; |
| 87 | + // Push the data to the correct place |
| 88 | + out_ring_step(); |
44 | 89 | }
|
45 | 90 |
|
46 | 91 | // Finished sending on bridge, start receive from USB
|
47 | 92 | void pipe_bridge_in_completion() {
|
48 |
| - if (pipe_state_pc_to_soc == PIPE_WAIT_FOR_BRIDGE) { |
49 |
| - usb_ep_start_out(USB_EP_PIPE_OUT, pipe_buffer_pc_to_soc, FLASH_BUFFER_SIZE); |
50 |
| - pipe_state_pc_to_soc = PIPE_WAIT_FOR_USB; |
51 |
| - } else { |
52 |
| - invalid(); |
53 |
| - } |
| 93 | + // Increment the location of where we will read from next (to send over USB) |
| 94 | + out_ring_read_pos = (out_ring_read_pos + 1) % OUT_RING_SIZE; |
| 95 | + // Decrement the number of packets that need reading |
| 96 | + out_ring_count -= 1; |
| 97 | + // Mark the bridge transfer as complete |
| 98 | + out_bridge_pending = false; |
| 99 | + // Move data along |
| 100 | + out_ring_step(); |
54 | 101 | }
|
55 | 102 |
|
56 | 103 | // Received from bridge, send to USB
|
|
0 commit comments