From 98b8e3ae9c7c299862988494f13807eb3421f949 Mon Sep 17 00:00:00 2001 From: 9names <60134748+9names@users.noreply.github.com> Date: Fri, 17 Sep 2021 09:40:10 +1000 Subject: [PATCH] Handle usb interrupt flags in driver (#106) Set interrupt enable flags in USB driver. The flags were already being cleared in the USB driver. Update USB examples to remove interrupt flag set/check as it is no longer necessary. --- .../examples/pico_usb_serial_interrupt.rs | 31 ------------------- .../pico/examples/pico_usb_twitchy_mouse.rs | 29 ----------------- rp2040-hal/src/usb.rs | 12 +++++++ 3 files changed, 12 insertions(+), 60 deletions(-) diff --git a/boards/pico/examples/pico_usb_serial_interrupt.rs b/boards/pico/examples/pico_usb_serial_interrupt.rs index e43d708..8ad12a0 100644 --- a/boards/pico/examples/pico_usb_serial_interrupt.rs +++ b/boards/pico/examples/pico_usb_serial_interrupt.rs @@ -84,20 +84,6 @@ fn main() -> ! { USB_DEVICE = Some(usb_dev); } - // The USB driver doesn't enable key interrupts yet, so manually do that here - unsafe { - let p = pac::Peripherals::steal(); - // Enable interrupts for when a buffer is done, when the bus is reset, - // and when a setup packet is received - p.USBCTRL_REGS.inte.modify(|_, w| { - w.buff_status() - .set_bit() - .bus_reset() - .set_bit() - .setup_req() - .set_bit() - }); - } // Enable the USB interrupt unsafe { pac::NVIC::unmask(hal::pac::Interrupt::USBCTRL_IRQ); @@ -158,21 +144,4 @@ unsafe fn USBCTRL_IRQ() { } }); } - - // Clear pending interrupt flags here. - // We could also move some of our code into these states to handle events - let p = pac::Peripherals::steal(); - let status = &p.USBCTRL_REGS.sie_status; - if status.read().ack_rec().bit_is_set() { - status.modify(|_r, w| w.ack_rec().set_bit()); - } - if status.read().setup_rec().bit_is_set() { - status.modify(|_r, w| w.setup_rec().set_bit()); - } - if status.read().trans_complete().bit_is_set() { - status.modify(|_r, w| w.trans_complete().set_bit()); - } - if status.read().bus_reset().bit_is_set() { - status.modify(|_r, w| w.bus_reset().set_bit()); - } } diff --git a/boards/pico/examples/pico_usb_twitchy_mouse.rs b/boards/pico/examples/pico_usb_twitchy_mouse.rs index 2ddd025..25a0782 100644 --- a/boards/pico/examples/pico_usb_twitchy_mouse.rs +++ b/boards/pico/examples/pico_usb_twitchy_mouse.rs @@ -77,19 +77,6 @@ fn main() -> ! { USB_DEVICE = Some(usb_dev); } - unsafe { - let p = pac::Peripherals::steal(); - // Enable interrupts for when a buffer is done, when the bus is reset, - // and when a setup packet is received - p.USBCTRL_REGS.inte.modify(|_, w| { - w.buff_status() - .set_bit() - .bus_reset() - .set_bit() - .setup_req() - .set_bit() - }); - } unsafe { pac::NVIC::unmask(hal::pac::Interrupt::USBCTRL_IRQ); }; @@ -133,20 +120,4 @@ unsafe fn USBCTRL_IRQ() { let usb_dev = USB_DEVICE.as_mut().unwrap(); let usb_hid = USB_HID.as_mut().unwrap(); usb_dev.poll(&mut [usb_hid]); - - // Clear pending interrupt flags - let p = pac::Peripherals::steal(); - let status = &p.USBCTRL_REGS.sie_status; - if status.read().ack_rec().bit_is_set() { - status.modify(|_r, w| w.ack_rec().set_bit()); - } - if status.read().setup_rec().bit_is_set() { - status.modify(|_r, w| w.setup_rec().set_bit()); - } - if status.read().trans_complete().bit_is_set() { - status.modify(|_r, w| w.trans_complete().set_bit()); - } - if status.read().bus_reset().bit_is_set() { - status.modify(|_r, w| w.bus_reset().set_bit()); - } } diff --git a/rp2040-hal/src/usb.rs b/rp2040-hal/src/usb.rs index 05be781..947a77e 100644 --- a/rp2040-hal/src/usb.rs +++ b/rp2040-hal/src/usb.rs @@ -407,6 +407,18 @@ impl UsbBusTrait for UsbBus { // 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() + .setup_req() + .set_bit() + }); + // enable pull up to let the host know we exist. inner .ctrl_reg