Skip to main content

esp_hal/usb/otg/
embassy_usb_host.rs

1//! USB OTG host driver for embassy-usb-host.
2
3use embassy_usb_driver::host::{DeviceEvent, UsbHostController};
4use embassy_usb_synopsys_otg::{
5    host::{OtgHost as OtgHostDriver, OtgHostInstance},
6    otg_v1::Otg,
7};
8
9use crate::usb::otg::Usb;
10
11/// Asynchronous USB host controller.
12pub struct Driver<'d> {
13    inner: OtgHostDriver<'d>,
14    _usb: Usb<'d>,
15}
16
17impl<'d> Driver<'d> {
18    /// Creates a new host controller driver for embassy-usb-host.
19    pub fn new(peri: Usb<'d>) -> Self {
20        let i = peri.info();
21        let instance = OtgHostInstance {
22            regs: unsafe { Otg::from_ptr(i.register_ptr.cast_mut()) },
23            state: peri.embassy_host_state(),
24            fifo_depth_words: i.fifo_depth_words as u16,
25            phy_type: i.phy_type,
26        };
27
28        (i.enable_host_mode)();
29        peri.bind_host_interrupt();
30
31        Self {
32            inner: OtgHostDriver::new(instance),
33            _usb: peri,
34        }
35    }
36}
37
38impl<'d> UsbHostController<'d> for Driver<'d> {
39    type Allocator = <OtgHostDriver<'d> as UsbHostController<'d>>::Allocator;
40
41    fn allocator(&self) -> Self::Allocator {
42        self.inner.allocator()
43    }
44
45    async fn wait_for_device_event(&mut self) -> DeviceEvent {
46        self.inner.wait_for_device_event().await
47    }
48
49    async fn bus_reset(&mut self) {
50        self.inner.bus_reset().await
51    }
52}
53
54impl<'d> Drop for Driver<'d> {
55    fn drop(&mut self) {
56        self._usb.disable_host_interrupt();
57        (self._usb.info().platform_bus_disable_on_drop)();
58    }
59}