-
Notifications
You must be signed in to change notification settings - Fork 251
/
Copy pathusb.rs
703 lines (640 loc) · 24.8 KB
/
usb.rs
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
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
//! Universal Serial Bus (USB)
// See [Chapter 4 Section 1](https://datasheets.raspberrypi.org/rp2040/rp2040_datasheet.pdf) for more details
//! ## Usage
//!
//! Initialize the Usb Bus forcing the VBUS detection.
//! ```no_run
//! use rp2040_hal::{clocks::init_clocks_and_plls, pac, Sio, usb::UsbBus, watchdog::Watchdog};
//! use usb_device::class_prelude::UsbBusAllocator;
//!
//! const XOSC_CRYSTAL_FREQ: u32 = 12_000_000; // Typically found in BSP crates
//!
//! let mut pac = pac::Peripherals::take().unwrap();
//! let mut watchdog = Watchdog::new(pac.WATCHDOG);
//! let mut clocks = init_clocks_and_plls(
//! XOSC_CRYSTAL_FREQ,
//! pac.XOSC,
//! pac.CLOCKS,
//! pac.PLL_SYS,
//! pac.PLL_USB,
//! &mut pac.RESETS,
//! &mut watchdog
//! ).ok().unwrap();
//!
//! let usb_bus = UsbBusAllocator::new(UsbBus::new(
//! pac.USBCTRL_REGS,
//! pac.USBCTRL_DPRAM,
//! clocks.usb_clock,
//! true,
//! &mut pac.RESETS,
//! ));
//! // Use the usb_bus as usual.
//! ```
//!
//! See [pico_usb_serial.rs](https://github.com/rp-rs/rp-hal-boards/blob/main/boards/rp-pico/examples/pico_usb_serial.rs) for more complete examples
//!
//!
//! ## Enumeration issue with small EP0 max packet size
//!
//! During enumeration Windows hosts send a `StatusOut` after the `DataIn` packet of the first
//! `Get Descriptor` resquest even if the `DataIn` isn't completed (typically when the `max_packet_size_ep0`
//! is less than 18bytes). The next request is a `Set Address` that expect a `StatusIn`.
//!
//! The issue is that by the time the previous `DataIn` packet is acknoledged and the `StatusOut`
//! followed by `Setup` are received, the usb stack may have already prepared the next `DataIn` payload
//! in the EP0 IN mailbox resulting in the payload being transmitted to the host instead of the
//! `StatusIn` for the `Set Address` request as expected by the host.
//!
//! To avoid that issue, the EP0 In mailbox should be invalidated between the `Setup` packet and the
//! next `StatusIn` initiated by the host. The workaround implemented clears the available bit of the
//! EP0 In endpoint's buffer to stop the device from sending the data instead of the status packet.
//! This workaround has the caveat that the poll function must be called between those two which
//! are only separated by a few microseconds.
//!
//! If the required timing cannot be met, using an maximum packet size of the endpoint 0 above 18bytes
//! (e.g. `.max_packet_size_ep0(64)`) should avoid that issue.
//!
//! ## Issue on RP2040B0 and RP2040B1: USB device fails to exit RESET state on busy USB bus.
//!
//! The feature `rp2040-e5`implements the workaround described by [RP2040-E5](https://datasheets.raspberrypi.com/rp2040/rp2040-datasheet.pdf#%5B%7B%22num%22%3A630%2C%22gen%22%3A0%7D%2C%7B%22name%22%3A%22XYZ%22%7D%2C115%2C158.848%2Cnull%5D).
//!
//! The workaround requires the GPIO block to be released from its reset and has for side effect
//! that GPIO15 will be stolen for a few hundred microseconds each time a Reset is detected on the
//! USB bus.
//!
//! The pin will be temporarily put in "bus keep" mode, weakly pulling the output towards its current
//! logic level. In absence of external loads, the current logic level will be maintained.
//! A user will lose control of the pin's output and reading from it may not reflect the actual state
//! of the external pin.
//!
//! ```no_run
//! # use rp2040_hal::{clocks::init_clocks_and_plls, pac, usb::UsbBus, watchdog::Watchdog};
//! # use usb_device::class_prelude::UsbBusAllocator;
//! use rp2040_hal::{gpio::Pins, Sio};
//!
//! # const XOSC_CRYSTAL_FREQ: u32 = 12_000_000; // Typically found in BSP crates
//! #
//! # let mut pac = pac::Peripherals::take().unwrap();
//! # let mut watchdog = Watchdog::new(pac.WATCHDOG);
//! # let mut clocks = init_clocks_and_plls(
//! # XOSC_CRYSTAL_FREQ,
//! # pac.XOSC,
//! # pac.CLOCKS,
//! # pac.PLL_SYS,
//! # pac.PLL_USB,
//! # &mut pac.RESETS,
//! # &mut watchdog
//! # ).ok().unwrap();
//! #
//! // required for the errata 5's workaround to function properly.
//! let sio = Sio::new(pac.SIO);
//! let _pins = Pins::new(
//! pac.IO_BANK0,
//! pac.PADS_BANK0,
//! sio.gpio_bank0,
//! &mut pac.RESETS,
//! );
//! #
//! # let usb_bus = UsbBusAllocator::new(UsbBus::new(
//! # pac.USBCTRL_REGS,
//! # pac.USBCTRL_DPRAM,
//! # clocks.usb_clock,
//! # true,
//! # &mut pac.RESETS,
//! # ));
//! # // Use the usb_bus as usual.
//! ```
use core::cell::RefCell;
use critical_section::{self, Mutex};
use usb_device::{
bus::{PollResult, UsbBus as UsbBusTrait},
endpoint::{EndpointAddress, EndpointType},
Result as UsbResult, UsbDirection, UsbError,
};
use crate::{
clocks::UsbClock,
pac::{RESETS, USBCTRL_DPRAM, USBCTRL_REGS},
resets::SubsystemReset,
};
#[cfg(feature = "rp2040-e5")]
mod errata5;
#[allow(clippy::bool_to_int_with_if)]
fn ep_addr_to_ep_buf_ctrl_idx(ep_addr: EndpointAddress) -> usize {
ep_addr.index() * 2 + (if ep_addr.is_in() { 0 } else { 1 })
}
#[derive(Debug)]
struct Endpoint {
ep_type: EndpointType,
max_packet_size: u16,
buffer_offset: u16,
}
impl Endpoint {
unsafe fn get_buf_parts(&self) -> (*mut u8, usize) {
const DPRAM_BASE: *mut u8 = USBCTRL_DPRAM::ptr() as *mut u8;
if self.ep_type == EndpointType::Control {
(DPRAM_BASE.offset(0x100), self.max_packet_size as usize)
} else {
(
DPRAM_BASE.offset(0x180 + (self.buffer_offset * 64) as isize),
self.max_packet_size as usize,
)
}
}
fn get_buf(&self) -> &[u8] {
// SAFETY:
// offset is checked by Inner::ep_allocate.
unsafe {
let (base, len) = self.get_buf_parts();
core::slice::from_raw_parts(base as *const _, len)
}
}
fn get_buf_mut(&mut self) -> &mut [u8] {
// SAFETY:
// offset is checked by Inner::ep_allocate.
unsafe {
let (base, len) = self.get_buf_parts();
core::slice::from_raw_parts_mut(base, len)
}
}
}
struct Inner {
ctrl_reg: USBCTRL_REGS,
ctrl_dpram: USBCTRL_DPRAM,
in_endpoints: [Option<Endpoint>; 16],
out_endpoints: [Option<Endpoint>; 16],
next_offset: u16,
read_setup: bool,
#[cfg(feature = "rp2040-e5")]
errata5_state: Option<errata5::Errata5State>,
}
impl Inner {
fn new(ctrl_reg: USBCTRL_REGS, ctrl_dpram: USBCTRL_DPRAM) -> Self {
Self {
ctrl_reg,
ctrl_dpram,
in_endpoints: Default::default(),
out_endpoints: Default::default(),
next_offset: 0,
read_setup: false,
#[cfg(feature = "rp2040-e5")]
errata5_state: None,
}
}
fn ep_allocate(
&mut self,
ep_addr: Option<EndpointAddress>,
ep_dir: UsbDirection,
ep_type: EndpointType,
max_packet_size: u16,
) -> UsbResult<EndpointAddress> {
let ep_addr = ep_addr
.or_else(|| {
let eps = if ep_dir == UsbDirection::In {
self.in_endpoints.iter()
} else {
self.out_endpoints.iter()
};
// find free end point
let mut iter = eps.enumerate();
// reserve ep0 for the control endpoint
if ep_type != EndpointType::Control {
iter.next();
}
iter.find(|(_, ep)| ep.is_none())
.map(|(index, _)| EndpointAddress::from_parts(index, ep_dir))
})
.ok_or(UsbError::EndpointOverflow)?;
let is_ep0 = ep_addr.index() == 0;
let is_ctrl_ep = ep_type == EndpointType::Control;
if !(is_ep0 ^ !is_ctrl_ep) {
return Err(UsbError::Unsupported);
}
let eps = if ep_addr.is_in() {
&mut self.in_endpoints
} else {
&mut self.out_endpoints
};
let maybe_ep = eps
.get_mut(ep_addr.index())
.ok_or(UsbError::EndpointOverflow)?;
if maybe_ep.is_some() {
return Err(UsbError::InvalidEndpoint);
}
// Validate buffer size. From datasheet (4.1.2.5):
// Data Buffers are typically 64 bytes long as this is the max normal packet size for most FS packets.
// For Isochronous endpoints a maximum buffer size of 1023 bytes is supported.
// For other packet types the maximum size is 64 bytes per buffer.
if (!matches!(ep_type, EndpointType::Isochronous { .. }) && max_packet_size > 64)
|| max_packet_size > 1023
{
return Err(UsbError::Unsupported);
}
if ep_addr.index() == 0 {
*maybe_ep = Some(Endpoint {
ep_type,
max_packet_size,
buffer_offset: 0, // not used on CTRL ep
});
} else {
// size in 64bytes units.
// NOTE: the compiler is smart enough to recognize /64 as a 6bit right shift so let's
// keep the division here for the sake of clarity
let aligned_sized = (max_packet_size + 63) / 64;
if (self.next_offset + aligned_sized) > (4096 / 64) {
return Err(UsbError::EndpointMemoryOverflow);
}
let buffer_offset = self.next_offset;
self.next_offset += aligned_sized;
*maybe_ep = Some(Endpoint {
ep_type,
max_packet_size,
buffer_offset,
});
}
Ok(ep_addr)
}
fn ep_reset_all(&mut self) {
self.ctrl_reg
.sie_ctrl
.modify(|_, w| w.ep0_int_1buf().set_bit());
// expect ctrl ep to receive on DATA first
self.ctrl_dpram.ep_buffer_control[0].write(|w| w.pid_0().set_bit());
self.ctrl_dpram.ep_buffer_control[1].write(|w| w.pid_0().set_bit());
cortex_m::asm::delay(12);
self.ctrl_dpram.ep_buffer_control[1].write(|w| w.available_0().set_bit());
for (index, ep) in itertools::interleave(
self.in_endpoints.iter().skip(1), // skip control endpoint
self.out_endpoints.iter().skip(1), // skip control endpoint
)
.enumerate()
.filter_map(|(i, ep)| ep.as_ref().map(|ep| (i, ep)))
{
use crate::pac::usbctrl_dpram::ep_control::ENDPOINT_TYPE_A;
let ep_type = match ep.ep_type {
EndpointType::Bulk => ENDPOINT_TYPE_A::BULK,
EndpointType::Isochronous { .. } => ENDPOINT_TYPE_A::ISOCHRONOUS,
EndpointType::Control => ENDPOINT_TYPE_A::CONTROL,
EndpointType::Interrupt => ENDPOINT_TYPE_A::INTERRUPT,
};
// configure
// ep 0 in&out are not part of index (skipped before enumeration)
self.ctrl_dpram.ep_control[index].modify(|_, w| unsafe {
w.endpoint_type().variant(ep_type);
w.interrupt_per_buff().set_bit();
w.enable().set_bit();
w.buffer_address().bits(0x180 + (ep.buffer_offset << 6))
});
// reset OUT ep and prepare IN ep to accept data
let buf_control = &self.ctrl_dpram.ep_buffer_control[index + 2];
if (index & 1) == 0 {
// first write occur on DATA0 so prepare the pid bit to be flipped
buf_control.write(|w| w.pid_0().set_bit());
} else {
buf_control.write(|w| unsafe {
w.pid_0().clear_bit();
w.length_0().bits(ep.max_packet_size)
});
cortex_m::asm::delay(12);
buf_control.modify(|_, w| w.available_0().set_bit());
}
}
}
fn ep_write(&mut self, ep_addr: EndpointAddress, buf: &[u8]) -> UsbResult<usize> {
let index = ep_addr.index();
let ep = self
.in_endpoints
.get_mut(index)
.and_then(Option::as_mut)
.ok_or(UsbError::InvalidEndpoint)?;
let buf_control = &self.ctrl_dpram.ep_buffer_control[index * 2];
if buf_control.read().available_0().bit_is_set() {
return Err(UsbError::WouldBlock);
}
let ep_buf = ep.get_buf_mut();
if ep_buf.len() < buf.len() {
return Err(UsbError::BufferOverflow);
}
ep_buf[..buf.len()].copy_from_slice(buf);
buf_control.modify(|r, w| unsafe {
w.length_0().bits(buf.len() as u16);
w.full_0().set_bit();
w.pid_0().bit(!r.pid_0().bit())
});
cortex_m::asm::delay(12);
buf_control.modify(|_, w| w.available_0().set_bit());
Ok(buf.len())
}
fn ep_read(&mut self, ep_addr: EndpointAddress, buf: &mut [u8]) -> UsbResult<usize> {
let index = ep_addr.index();
let ep = self
.out_endpoints
.get_mut(index)
.and_then(Option::as_mut)
.ok_or(UsbError::InvalidEndpoint)?;
let buf_control = &self.ctrl_dpram.ep_buffer_control[index * 2 + 1];
let buf_control_val = buf_control.read();
let process_setup = index == 0 && self.read_setup;
if process_setup {
// assume we want to read the setup request
//
// the OUT packet will be either data or a status zlp
let len = 8;
let ep_buf =
unsafe { core::slice::from_raw_parts(USBCTRL_DPRAM::ptr() as *const u8, len) };
if len > buf.len() {
return Err(UsbError::BufferOverflow);
}
buf[..len].copy_from_slice(&ep_buf[..len]);
// Next packet will be on DATA1 so clear pid_0 so it gets flipped by next buf config
self.ctrl_dpram.ep_buffer_control[0].modify(|_, w| w.pid_0().clear_bit());
// clear setup request flag
self.ctrl_reg
.sie_status
.write(|w| w.setup_rec().clear_bit_by_one());
// clear any out standing out flag e.g. in case a zlp got discarded
self.ctrl_reg.buff_status.write(|w| unsafe { w.bits(2) });
let is_in_request = (buf[0] & 0x80) == 0x80;
let data_length = u16::from(buf[6]) | (u16::from(buf[7]) << 8);
let expect_data_or_zlp = is_in_request || data_length != 0;
buf_control.modify(|_, w| unsafe {
w.length_0().bits(ep.max_packet_size);
w.full_0().clear_bit();
w.pid_0().set_bit()
});
// enable if and only if a dataphase is expected.
cortex_m::asm::delay(12);
buf_control.modify(|_, w| w.available_0().bit(expect_data_or_zlp));
self.read_setup = false;
Ok(len)
} else {
if buf_control_val.full_0().bit_is_clear() {
return Err(UsbError::WouldBlock);
}
let len = buf_control_val.length_0().bits().into();
if len > buf.len() {
return Err(UsbError::BufferOverflow);
}
buf[..len].copy_from_slice(&ep.get_buf()[..len]);
// Clear OUT flag once it is read.
self.ctrl_reg
.buff_status
.write(|w| unsafe { w.bits(1 << (index * 2 + 1)) });
buf_control.modify(|r, w| unsafe {
w.length_0().bits(ep.max_packet_size);
w.full_0().clear_bit();
w.pid_0().bit(!r.pid_0().bit())
});
if index != 0 || len == ep.max_packet_size.into() {
// only mark as available on the control endpoint if and only if the packet was
// max_packet_size
cortex_m::asm::delay(12);
buf_control.modify(|_, w| w.available_0().set_bit());
}
Ok(len)
}
}
}
/// Usb bus
pub struct UsbBus {
inner: Mutex<RefCell<Inner>>,
}
impl UsbBus {
/// Create new usb bus struct and bring up usb as device.
pub fn new(
ctrl_reg: USBCTRL_REGS,
ctrl_dpram: USBCTRL_DPRAM,
_pll: UsbClock,
force_vbus_detect_bit: bool,
resets: &mut RESETS,
) -> Self {
#[cfg(feature = "rp2040-e5")]
errata5::Errata5State::check_bank0_reset();
ctrl_reg.reset_bring_down(resets);
ctrl_reg.reset_bring_up(resets);
unsafe {
let raw_ctrl_reg =
core::slice::from_raw_parts_mut(USBCTRL_REGS::ptr() as *mut u32, 1 + 0x98 / 4);
raw_ctrl_reg.fill(0);
let raw_ctrl_pdram =
core::slice::from_raw_parts_mut(USBCTRL_DPRAM::ptr() as *mut u32, 1 + 0xfc / 4);
raw_ctrl_pdram.fill(0);
}
ctrl_reg.usb_muxing.modify(|_, w| {
w.to_phy().set_bit();
w.softcon().set_bit()
});
if force_vbus_detect_bit {
ctrl_reg.usb_pwr.modify(|_, w| {
w.vbus_detect().set_bit();
w.vbus_detect_override_en().set_bit()
});
}
ctrl_reg.main_ctrl.modify(|_, w| {
w.sim_timing().clear_bit();
w.host_ndevice().clear_bit();
w.controller_en().set_bit()
});
Self {
inner: Mutex::new(RefCell::new(Inner::new(ctrl_reg, ctrl_dpram))),
}
}
/// Generates a resume request on the bus.
pub fn remote_wakeup(&self) {
critical_section::with(|cs| {
let inner = self.inner.borrow(cs).borrow_mut();
inner.ctrl_reg.sie_ctrl.modify(|_, w| w.resume().set_bit());
});
}
}
impl UsbBusTrait for UsbBus {
fn alloc_ep(
&mut self,
ep_dir: UsbDirection,
ep_addr: Option<EndpointAddress>,
ep_type: EndpointType,
max_packet_size: u16,
_interval: u8,
) -> UsbResult<EndpointAddress> {
critical_section::with(|cs| {
let mut inner = self.inner.borrow(cs).borrow_mut();
inner.ep_allocate(ep_addr, ep_dir, ep_type, max_packet_size)
})
}
fn enable(&mut self) {
critical_section::with(|cs| {
let inner = self.inner.borrow(cs).borrow_mut();
// at this stage ep's are expected to be in their reset state
// TODO: is it worth having a debug_assert for that here?
// Enable interrupt generation when a buffer is done, when the bus is reset,
// and when a setup packet is received
// this should be sufficient for device mode, will need more for host.
inner.ctrl_reg.inte.modify(|_, w| {
w.buff_status()
.set_bit()
.bus_reset()
.set_bit()
.dev_resume_from_host()
.set_bit()
.dev_suspend()
.set_bit()
.setup_req()
.set_bit()
});
// enable pull up to let the host know we exist.
inner
.ctrl_reg
.sie_ctrl
.modify(|_, w| w.pullup_en().set_bit());
})
}
fn reset(&self) {
critical_section::with(|cs| {
let mut inner = self.inner.borrow(cs).borrow_mut();
// clear reset flag
inner
.ctrl_reg
.sie_status
.write(|w| w.bus_reset().clear_bit_by_one());
inner
.ctrl_reg
.buff_status
.write(|w| unsafe { w.bits(0xFFFF_FFFF) });
// reset all endpoints
inner.ep_reset_all();
// Reset address register
inner.ctrl_reg.addr_endp.reset();
// TODO: RP2040-E5: work around implementation
// TODO: reset all endpoints & buffer statuses
})
}
fn set_device_address(&self, addr: u8) {
critical_section::with(|cs| {
let inner = self.inner.borrow(cs).borrow_mut();
inner
.ctrl_reg
.addr_endp
.modify(|_, w| unsafe { w.address().bits(addr & 0x7F) });
// reset ep0
inner.ctrl_dpram.ep_buffer_control[0].modify(|_, w| w.pid_0().set_bit());
inner.ctrl_dpram.ep_buffer_control[1].modify(|_, w| w.pid_0().set_bit());
})
}
fn write(&self, ep_addr: EndpointAddress, buf: &[u8]) -> UsbResult<usize> {
critical_section::with(|cs| {
let mut inner = self.inner.borrow(cs).borrow_mut();
inner.ep_write(ep_addr, buf)
})
}
fn read(&self, ep_addr: EndpointAddress, buf: &mut [u8]) -> UsbResult<usize> {
critical_section::with(|cs| {
let mut inner = self.inner.borrow(cs).borrow_mut();
inner.ep_read(ep_addr, buf)
})
}
fn set_stalled(&self, ep_addr: EndpointAddress, stalled: bool) {
critical_section::with(|cs| {
let inner = self.inner.borrow(cs).borrow_mut();
if ep_addr.index() == 0 {
inner.ctrl_reg.ep_stall_arm.modify(|_, w| {
if ep_addr.is_in() {
w.ep0_in().bit(stalled)
} else {
w.ep0_out().bit(stalled)
}
});
}
let index = ep_addr_to_ep_buf_ctrl_idx(ep_addr);
inner.ctrl_dpram.ep_buffer_control[index].modify(|_, w| w.stall().bit(stalled));
})
}
fn is_stalled(&self, ep_addr: EndpointAddress) -> bool {
critical_section::with(|cs| {
let inner = self.inner.borrow(cs).borrow_mut();
let index = ep_addr_to_ep_buf_ctrl_idx(ep_addr);
inner.ctrl_dpram.ep_buffer_control[index]
.read()
.stall()
.bit_is_set()
})
}
fn suspend(&self) {}
fn resume(&self) {}
fn poll(&self) -> PollResult {
critical_section::with(|cs| {
let mut inner = self.inner.borrow(cs).borrow_mut();
#[cfg(feature = "rp2040-e5")]
if let Some(state) = inner.errata5_state.take() {
unsafe {
inner.errata5_state = state.update();
}
return if inner.errata5_state.is_some() {
PollResult::None
} else {
PollResult::Reset
};
}
// check for bus reset and/or suspended states.
let ints = inner.ctrl_reg.ints.read();
let mut buff_status = inner.ctrl_reg.buff_status.read().bits();
if ints.bus_reset().bit_is_set() {
#[cfg(feature = "rp2040-e5")]
if inner.ctrl_reg.sie_status.read().connected().bit_is_clear() {
inner.errata5_state = Some(errata5::Errata5State::start());
return PollResult::None;
} else {
return PollResult::Reset;
}
#[cfg(not(feature = "rp2040-e5"))]
return PollResult::Reset;
} else if buff_status == 0 && ints.setup_req().bit_is_clear() {
if ints.dev_suspend().bit_is_set() {
inner
.ctrl_reg
.sie_status
.write(|w| w.suspended().clear_bit_by_one());
return PollResult::Suspend;
} else if ints.dev_resume_from_host().bit_is_set() {
inner
.ctrl_reg
.sie_status
.write(|w| w.resume().clear_bit_by_one());
return PollResult::Resume;
}
return PollResult::None;
}
let (mut ep_out, mut ep_in_complete, mut ep_setup): (u16, u16, u16) = (0, 0, 0);
// IN Complete shall only be reported once.
inner
.ctrl_reg
.buff_status
.write(|w| unsafe { w.bits(0x5555_5555) });
for i in 0..32u32 {
if buff_status == 0 {
break;
} else if (buff_status & 1) == 1 {
let is_in = (i & 1) == 0;
let ep_idx = i / 2;
if is_in {
ep_in_complete |= 1 << ep_idx;
} else {
ep_out |= 1 << ep_idx;
}
}
buff_status >>= 1;
}
// check for setup request
if ints.setup_req().bit_is_set() {
// Small max_packet_size_ep0 Work-Around
inner.ctrl_dpram.ep_buffer_control[0].modify(|_, w| w.available_0().clear_bit());
ep_setup |= 1;
inner.read_setup = true;
}
PollResult::Data {
ep_out,
ep_in_complete,
ep_setup,
}
})
}
const QUIRK_SET_ADDRESS_BEFORE_STATUS: bool = false;
}