Open FFBoard
Open source force feedback firmware
rp2040_usb.c
Go to the documentation of this file.
1/*
2 * The MIT License (MIT)
3 *
4 * Copyright (c) 2020 Raspberry Pi (Trading) Ltd.
5 * Copyright (c) 2021 Ha Thach (tinyusb.org) for Double Buffered
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal
9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23 * THE SOFTWARE.
24 *
25 * This file is part of the TinyUSB stack.
26 */
27
28#include "tusb_option.h"
29
30#if CFG_TUSB_MCU == OPT_MCU_RP2040
31
32#include <stdlib.h>
33#include "rp2040_usb.h"
34
35//--------------------------------------------------------------------+
36// MACRO CONSTANT TYPEDEF PROTOTYPE
37//--------------------------------------------------------------------+
38static void _hw_endpoint_xfer_sync(struct hw_endpoint* ep);
39
40#if TUD_OPT_RP2040_USB_DEVICE_UFRAME_FIX
41 static bool e15_is_bulkin_ep(struct hw_endpoint* ep);
42 static bool e15_is_critical_frame_period(struct hw_endpoint* ep);
43#else
44 #define e15_is_bulkin_ep(x) (false)
45 #define e15_is_critical_frame_period(x) (false)
46#endif
47
48// if usb hardware is in host mode
49TU_ATTR_ALWAYS_INLINE static inline bool is_host_mode(void) {
50 return (usb_hw->main_ctrl & USB_MAIN_CTRL_HOST_NDEVICE_BITS) ? true : false;
51}
52
53//--------------------------------------------------------------------+
54// Implementation
55//--------------------------------------------------------------------+
56// Provide own byte by byte memcpy as not all copies are aligned
57static void unaligned_memcpy(void *dst, const void *src, size_t n) {
58 uint8_t *dst_byte = (uint8_t*)dst;
59 const uint8_t *src_byte = (const uint8_t*)src;
60 while (n--) {
61 *dst_byte++ = *src_byte++;
62 }
63}
64
65void rp2040_usb_init(void) {
66 // Reset usb controller
67 reset_block(RESETS_RESET_USBCTRL_BITS);
68 unreset_block_wait(RESETS_RESET_USBCTRL_BITS);
69
70#ifdef __GNUC__
71 // Clear any previous state just in case
72#pragma GCC diagnostic push
73#pragma GCC diagnostic ignored "-Warray-bounds"
74#if __GNUC__ > 6
75#pragma GCC diagnostic ignored "-Wstringop-overflow"
76#endif
77#endif
78 memset(usb_dpram, 0, sizeof(*usb_dpram));
79#ifdef __GNUC__
80#pragma GCC diagnostic pop
81#endif
82
83 // Mux the controller to the onboard usb phy
84 usb_hw->muxing = USB_USB_MUXING_TO_PHY_BITS | USB_USB_MUXING_SOFTCON_BITS;
85
86 TU_LOG2_INT(sizeof(hw_endpoint_t));
87}
88
89void __tusb_irq_path_func(hw_endpoint_reset_transfer)(struct hw_endpoint* ep) {
90 ep->active = false;
91 ep->remaining_len = 0;
92 ep->xferred_len = 0;
93 ep->user_buf = 0;
94}
95
96void __tusb_irq_path_func(_hw_endpoint_buffer_control_update32)(struct hw_endpoint* ep, uint32_t and_mask,
97 uint32_t or_mask) {
98 uint32_t value = 0;
99
100 if (and_mask) {
101 value = *ep->buffer_control & and_mask;
102 }
103
104 if (or_mask) {
105 value |= or_mask;
106 if (or_mask & USB_BUF_CTRL_AVAIL) {
107 if (*ep->buffer_control & USB_BUF_CTRL_AVAIL) {
108 panic("ep %02X was already available", ep->ep_addr);
109 }
110 *ep->buffer_control = value & ~USB_BUF_CTRL_AVAIL;
111 // 4.1.2.5.1 Con-current access: 12 cycles (should be good for 48*12Mhz = 576Mhz) after write to buffer control
112 // Don't need delay in host mode as host is in charge
113 if ( !is_host_mode()) {
114 busy_wait_at_least_cycles(12);
115 }
116 }
117 }
118
119 *ep->buffer_control = value;
120}
121
122// prepare buffer, return buffer control
123static uint32_t __tusb_irq_path_func(prepare_ep_buffer)(struct hw_endpoint* ep, uint8_t buf_id) {
124 uint16_t const buflen = tu_min16(ep->remaining_len, ep->wMaxPacketSize);
125 ep->remaining_len = (uint16_t) (ep->remaining_len - buflen);
126
127 uint32_t buf_ctrl = buflen | USB_BUF_CTRL_AVAIL;
128
129 // PID
130 buf_ctrl |= ep->next_pid ? USB_BUF_CTRL_DATA1_PID : USB_BUF_CTRL_DATA0_PID;
131 ep->next_pid ^= 1u;
132
133 if (!ep->rx) {
134 // Copy data from user buffer to hw buffer
135 unaligned_memcpy(ep->hw_data_buf + buf_id * 64, ep->user_buf, buflen);
136 ep->user_buf += buflen;
137
138 // Mark as full
139 buf_ctrl |= USB_BUF_CTRL_FULL;
140 }
141
142 // Is this the last buffer? Only really matters for host mode. Will trigger
143 // the trans complete irq but also stop it polling. We only really care about
144 // trans complete for setup packets being sent
145 if (ep->remaining_len == 0) {
146 buf_ctrl |= USB_BUF_CTRL_LAST;
147 }
148
149 if (buf_id) buf_ctrl = buf_ctrl << 16;
150
151 return buf_ctrl;
152}
153
154// Prepare buffer control register value
155void __tusb_irq_path_func(hw_endpoint_start_next_buffer)(struct hw_endpoint* ep) {
156 uint32_t ep_ctrl = *ep->endpoint_control;
157
158 // always compute and start with buffer 0
159 uint32_t buf_ctrl = prepare_ep_buffer(ep, 0) | USB_BUF_CTRL_SEL;
160
161 // For now: skip double buffered for OUT endpoint in Device mode, since
162 // host could send < 64 bytes and cause short packet on buffer0
163 // NOTE: this could happen to Host mode IN endpoint
164 // Also, Host mode "interrupt" endpoint hardware is only single buffered,
165 // NOTE2: Currently Host bulk is implemented using "interrupt" endpoint
166 bool const is_host = is_host_mode();
167 bool const force_single = (!is_host && !tu_edpt_dir(ep->ep_addr)) ||
168 (is_host && tu_edpt_number(ep->ep_addr) != 0);
169
170 if (ep->remaining_len && !force_single) {
171 // Use buffer 1 (double buffered) if there is still data
172 // TODO: Isochronous for buffer1 bit-field is different than CBI (control bulk, interrupt)
173
174 buf_ctrl |= prepare_ep_buffer(ep, 1);
175
176 // Set endpoint control double buffered bit if needed
177 ep_ctrl &= ~EP_CTRL_INTERRUPT_PER_BUFFER;
178 ep_ctrl |= EP_CTRL_DOUBLE_BUFFERED_BITS | EP_CTRL_INTERRUPT_PER_DOUBLE_BUFFER;
179 } else {
180 // Single buffered since 1 is enough
181 ep_ctrl &= ~(EP_CTRL_DOUBLE_BUFFERED_BITS | EP_CTRL_INTERRUPT_PER_DOUBLE_BUFFER);
182 ep_ctrl |= EP_CTRL_INTERRUPT_PER_BUFFER;
183 }
184
185 *ep->endpoint_control = ep_ctrl;
186
187 TU_LOG(3, " Prepare BufCtrl: [0] = 0x%04x [1] = 0x%04x\r\n", tu_u32_low16(buf_ctrl), tu_u32_high16(buf_ctrl));
188
189 // Finally, write to buffer_control which will trigger the transfer
190 // the next time the controller polls this dpram address
192}
193
194void hw_endpoint_xfer_start(struct hw_endpoint* ep, uint8_t* buffer, uint16_t total_len) {
196
197 if (ep->active) {
198 // TODO: Is this acceptable for interrupt packets?
199 TU_LOG(1, "WARN: starting new transfer on already active ep %02X\r\n", ep->ep_addr);
201 }
202
203 // Fill in info now that we're kicking off the hw
204 ep->remaining_len = total_len;
205 ep->xferred_len = 0;
206 ep->active = true;
207 ep->user_buf = buffer;
208
209 if (e15_is_bulkin_ep(ep)) {
210 usb_hw_set->inte = USB_INTS_DEV_SOF_BITS;
211 }
212
214 ep->pending = 1;
215 } else {
217 }
218
220}
221
222// sync endpoint buffer and return transferred bytes
223static uint16_t __tusb_irq_path_func(sync_ep_buffer)(struct hw_endpoint* ep, uint8_t buf_id) {
224 uint32_t buf_ctrl = _hw_endpoint_buffer_control_get_value32(ep);
225 if (buf_id) buf_ctrl = buf_ctrl >> 16;
226
227 uint16_t xferred_bytes = buf_ctrl & USB_BUF_CTRL_LEN_MASK;
228
229 if (!ep->rx) {
230 // We are continuing a transfer here. If we are TX, we have successfully
231 // sent some data can increase the length we have sent
232 assert(!(buf_ctrl & USB_BUF_CTRL_FULL));
233
234 ep->xferred_len = (uint16_t) (ep->xferred_len + xferred_bytes);
235 } else {
236 // If we have received some data, so can increase the length
237 // we have received AFTER we have copied it to the user buffer at the appropriate offset
238 assert(buf_ctrl & USB_BUF_CTRL_FULL);
239
240 unaligned_memcpy(ep->user_buf, ep->hw_data_buf + buf_id * 64, xferred_bytes);
241 ep->xferred_len = (uint16_t) (ep->xferred_len + xferred_bytes);
242 ep->user_buf += xferred_bytes;
243 }
244
245 // Short packet
246 if (xferred_bytes < ep->wMaxPacketSize) {
247 pico_trace(" Short packet on buffer %d with %u bytes\r\n", buf_id, xferred_bytes);
248 // Reduce total length as this is last packet
249 ep->remaining_len = 0;
250 }
251
252 return xferred_bytes;
253}
254
255static void __tusb_irq_path_func(_hw_endpoint_xfer_sync)(struct hw_endpoint* ep) {
256 // Update hw endpoint struct with info from hardware
257 // after a buff status interrupt
258
259 uint32_t __unused buf_ctrl = _hw_endpoint_buffer_control_get_value32(ep);
260 TU_LOG(3, " Sync BufCtrl: [0] = 0x%04x [1] = 0x%04x\r\n", tu_u32_low16(buf_ctrl), tu_u32_high16(buf_ctrl));
261
262 // always sync buffer 0
263 uint16_t buf0_bytes = sync_ep_buffer(ep, 0);
264
265 // sync buffer 1 if double buffered
266 if ((*ep->endpoint_control) & EP_CTRL_DOUBLE_BUFFERED_BITS) {
267 if (buf0_bytes == ep->wMaxPacketSize) {
268 // sync buffer 1 if not short packet
269 sync_ep_buffer(ep, 1);
270 } else {
271 // short packet on buffer 0
272 // TODO couldn't figure out how to handle this case which happen with net_lwip_webserver example
273 // At this time (currently trigger per 2 buffer), the buffer1 is probably filled with data from
274 // the next transfer (not current one). For now we disable double buffered for device OUT
275 // NOTE this could happen to Host IN
276#if 0
277 uint8_t const ep_num = tu_edpt_number(ep->ep_addr);
278 uint8_t const dir = (uint8_t) tu_edpt_dir(ep->ep_addr);
279 uint8_t const ep_id = 2*ep_num + (dir ? 0 : 1);
280
281 // abort queued transfer on buffer 1
282 usb_hw->abort |= TU_BIT(ep_id);
283
284 while ( !(usb_hw->abort_done & TU_BIT(ep_id)) ) {}
285
286 uint32_t ep_ctrl = *ep->endpoint_control;
287 ep_ctrl &= ~(EP_CTRL_DOUBLE_BUFFERED_BITS | EP_CTRL_INTERRUPT_PER_DOUBLE_BUFFER);
288 ep_ctrl |= EP_CTRL_INTERRUPT_PER_BUFFER;
289
291
292 usb_hw->abort &= ~TU_BIT(ep_id);
293
294 TU_LOG(3, "----SHORT PACKET buffer0 on EP %02X:\r\n", ep->ep_addr);
295 TU_LOG(3, " BufCtrl: [0] = 0x%04x [1] = 0x%04x\r\n", tu_u32_low16(buf_ctrl), tu_u32_high16(buf_ctrl));
296#endif
297 }
298 }
299}
300
301// Returns true if transfer is complete
302bool __tusb_irq_path_func(hw_endpoint_xfer_continue)(struct hw_endpoint* ep) {
304
305 // Part way through a transfer
306 if (!ep->active) {
307 panic("Can't continue xfer on inactive ep %02X", ep->ep_addr);
308 }
309
310 // Update EP struct from hardware state
312
313 // Now we have synced our state with the hardware. Is there more data to transfer?
314 // If we are done then notify tinyusb
315 if (ep->remaining_len == 0) {
316 pico_trace("Completed transfer of %d bytes on ep %02X\r\n", ep->xferred_len, ep->ep_addr);
317 // Notify caller we are done so it can notify the tinyusb stack
319 return true;
320 } else {
322 ep->pending = 1;
323 } else {
325 }
326 }
327
329 // More work to do
330 return false;
331}
332
333//--------------------------------------------------------------------+
334// Errata 15
335//--------------------------------------------------------------------+
336
337#if TUD_OPT_RP2040_USB_DEVICE_UFRAME_FIX
338
339/* Don't mark IN buffers as available during the last 200us of a full-speed
340 frame. This avoids a situation seen with the USB2.0 hub on a Raspberry
341 Pi 4 where a late IN token before the next full-speed SOF can cause port
342 babble and a corrupt ACK packet. The nature of the data corruption has a
343 chance to cause device lockup.
344
345 Use the next SOF to mark delayed buffers as available. This reduces
346 available Bulk IN bandwidth by approximately 20%, and requires that the
347 SOF interrupt is enabled while these transfers are ongoing.
348
349 Inherit the top-level enable from the corresponding Pico-SDK flag.
350 Applications that will not use the device in a situation where it could
351 be plugged into a Pi 4 or Pi 400 (for example, when directly connected
352 to a commodity hub or other host) can turn off the flag in the SDK.
353*/
354
355volatile uint32_t e15_last_sof = 0;
356
357// check if Errata 15 is needed for this endpoint i.e device bulk-in
358static bool __tusb_irq_path_func(e15_is_bulkin_ep)(struct hw_endpoint* ep) {
359 return (!is_host_mode() && tu_edpt_dir(ep->ep_addr) == TUSB_DIR_IN &&
361}
362
363// check if we need to apply Errata 15 workaround : i.e
364// Endpoint is BULK IN and is currently in critical frame period i.e 20% of last usb frame
365static bool __tusb_irq_path_func(e15_is_critical_frame_period)(struct hw_endpoint* ep) {
366 TU_VERIFY(e15_is_bulkin_ep(ep));
367
368 /* Avoid the last 200us (uframe 6.5-7) of a frame, up to the EOF2 point.
369 * The device state machine cannot recover from receiving an incorrect PID
370 * when it is expecting an ACK.
371 */
372 uint32_t delta = time_us_32() - e15_last_sof;
373 if (delta < 800 || delta > 998) {
374 return false;
375 }
376 TU_LOG(3, "Avoiding sof %lu now %lu last %lu\r\n", (usb_hw->sof_rd + 1) & USB_SOF_RD_BITS, time_us_32(),
378 return true;
379}
380
381#endif // TUD_OPT_RP2040_USB_DEVICE_UFRAME_FIX
382#endif
uint8_t const * buffer
Definition: midi_device.h:100
static uint32_t __tusb_irq_path_func() prepare_ep_buffer(struct hw_endpoint *ep, uint8_t buf_id)
Definition: rp2040_usb.c:123
static TU_ATTR_ALWAYS_INLINE bool is_host_mode(void)
Definition: rp2040_usb.c:49
static bool e15_is_bulkin_ep(struct hw_endpoint *ep)
Definition: rp2040_usb.c:358
void __tusb_irq_path_func() hw_endpoint_reset_transfer(struct hw_endpoint *ep)
Definition: rp2040_usb.c:89
void __tusb_irq_path_func() _hw_endpoint_buffer_control_update32(struct hw_endpoint *ep, uint32_t and_mask, uint32_t or_mask)
Definition: rp2040_usb.c:96
static uint16_t __tusb_irq_path_func() sync_ep_buffer(struct hw_endpoint *ep, uint8_t buf_id)
Definition: rp2040_usb.c:223
static void _hw_endpoint_xfer_sync(struct hw_endpoint *ep)
Definition: rp2040_usb.c:255
void __tusb_irq_path_func() hw_endpoint_start_next_buffer(struct hw_endpoint *ep)
Definition: rp2040_usb.c:155
static void unaligned_memcpy(void *dst, const void *src, size_t n)
Definition: rp2040_usb.c:57
volatile uint32_t e15_last_sof
Definition: rp2040_usb.c:355
void rp2040_usb_init(void)
Definition: rp2040_usb.c:65
bool __tusb_irq_path_func() hw_endpoint_xfer_continue(struct hw_endpoint *ep)
Definition: rp2040_usb.c:302
void hw_endpoint_xfer_start(struct hw_endpoint *ep, uint8_t *buffer, uint16_t total_len)
Definition: rp2040_usb.c:194
static bool e15_is_critical_frame_period(struct hw_endpoint *ep)
Definition: rp2040_usb.c:365
static TU_ATTR_ALWAYS_INLINE uint32_t _hw_endpoint_buffer_control_get_value32(struct hw_endpoint *ep)
Definition: rp2040_usb.h:115
static TU_ATTR_ALWAYS_INLINE void _hw_endpoint_buffer_control_set_value32(struct hw_endpoint *ep, uint32_t value)
Definition: rp2040_usb.h:120
static TU_ATTR_ALWAYS_INLINE void hw_endpoint_lock_update(__unused struct hw_endpoint *ep, __unused int delta)
Definition: rp2040_usb.h:107
AUDIO Channel Cluster Descriptor (4.1)
Definition: audio.h:647
uint16_t remaining_len
Definition: rp2040_usb.h:71
uint8_t pending
Definition: rp2040_usb.h:84
io_rw_32 * endpoint_control
Definition: rp2040_usb.h:59
uint8_t transfer_type
Definition: rp2040_usb.h:81
uint8_t * hw_data_buf
Definition: rp2040_usb.h:65
uint8_t next_pid
Definition: rp2040_usb.h:56
uint16_t xferred_len
Definition: rp2040_usb.h:72
uint16_t wMaxPacketSize
Definition: rp2040_usb.h:75
uint8_t ep_addr
Definition: rp2040_usb.h:55
io_rw_32 * buffer_control
Definition: rp2040_usb.h:62
uint8_t * user_buf
Definition: rp2040_usb.h:68
bool active
Definition: rp2040_usb.h:78
static TU_ATTR_ALWAYS_INLINE uint16_t tu_min16(uint16_t x, uint16_t y)
Definition: tusb_common.h:155
static TU_ATTR_ALWAYS_INLINE uint16_t tu_u32_low16(uint32_t ui32)
Definition: tusb_common.h:143
static TU_ATTR_ALWAYS_INLINE uint16_t tu_u32_high16(uint32_t ui32)
Definition: tusb_common.h:142
@ TUSB_DIR_IN
Definition: tusb_types.h:67
static TU_ATTR_ALWAYS_INLINE uint8_t tu_edpt_number(uint8_t addr)
Definition: tusb_types.h:507
@ TUSB_XFER_BULK
Definition: tusb_types.h:61
TU_ATTR_PACKED_END TU_ATTR_BIT_FIELD_ORDER_END static TU_ATTR_ALWAYS_INLINE tusb_dir_t tu_edpt_dir(uint8_t addr)
Definition: tusb_types.h:502