Skip to content

Commit

Permalink
Add free function
Browse files Browse the repository at this point in the history
  • Loading branch information
jnthbdn committed Dec 11, 2023
1 parent 9372dce commit 39d3c8d
Showing 1 changed file with 16 additions and 3 deletions.
19 changes: 16 additions & 3 deletions rp2040-hal/src/usb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,18 +172,20 @@ struct Inner {
out_endpoints: [Option<Endpoint>; 16],
next_offset: u16,
read_setup: bool,
pll: UsbClock,
#[cfg(feature = "rp2040-e5")]
errata5_state: Option<errata5::Errata5State>,
}
impl Inner {
fn new(ctrl_reg: USBCTRL_REGS, ctrl_dpram: USBCTRL_DPRAM) -> Self {
fn new(ctrl_reg: USBCTRL_REGS, ctrl_dpram: USBCTRL_DPRAM, pll: UsbClock) -> Self {
Self {
ctrl_reg,
ctrl_dpram,
in_endpoints: Default::default(),
out_endpoints: Default::default(),
next_offset: 0,
read_setup: false,
pll,
#[cfg(feature = "rp2040-e5")]
errata5_state: None,
}
Expand Down Expand Up @@ -438,7 +440,7 @@ impl UsbBus {
pub fn new(
ctrl_reg: USBCTRL_REGS,
ctrl_dpram: USBCTRL_DPRAM,
_pll: UsbClock,
pll: UsbClock,
force_vbus_detect_bit: bool,
resets: &mut RESETS,
) -> Self {
Expand Down Expand Up @@ -476,7 +478,7 @@ impl UsbBus {
});

Self {
inner: Mutex::new(RefCell::new(Inner::new(ctrl_reg, ctrl_dpram))),
inner: Mutex::new(RefCell::new(Inner::new(ctrl_reg, ctrl_dpram, pll))),
}
}

Expand All @@ -487,6 +489,17 @@ impl UsbBus {
inner.ctrl_reg.sie_ctrl.modify(|_, w| w.resume().set_bit());
});
}

/// Stop and free the Usb resources
pub fn free(self, resets: &mut RESETS) -> (USBCTRL_REGS, USBCTRL_DPRAM, UsbClock) {
critical_section::with(|_cs| {
let inner = self.inner.into_inner().into_inner();

inner.ctrl_reg.reset_bring_down(resets);

(inner.ctrl_reg, inner.ctrl_dpram, inner.pll)
})
}
}

impl UsbBusTrait for UsbBus {
Expand Down

0 comments on commit 39d3c8d

Please sign in to comment.