Merge #1145
1145: STM32 USB OTG #2 r=Dirbaio a=chemicstry This is a continuation of #799 The usb serial example is already working! TODO: - [x] Add critical sections to registers shared with IRQ - [x] Fix `disable()` - [x] ~Implement cable disconnect detection~ - postponed - [x] Fix `endpoint_set_enabled()` - [x] ~Endpoint `wait_enabled`~ USB OTG does not have enable delay (?) - [x] HS internal and HS ULPI PHY - untested Co-authored-by: chemicstry <chemicstry@gmail.com> Co-authored-by: Dario Nieuwenhuis <dirbaio@dirbaio.net>
This commit is contained in:
		@@ -391,11 +391,6 @@ impl<'d, T: Instance, P: UsbSupply> driver::Bus for Bus<'d, T, P> {
 | 
			
		||||
        .await
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    #[inline]
 | 
			
		||||
    fn set_address(&mut self, _addr: u8) {
 | 
			
		||||
        // Nothing to do, the peripheral handles this.
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    fn endpoint_set_stalled(&mut self, ep_addr: EndpointAddress, stalled: bool) {
 | 
			
		||||
        let regs = T::regs();
 | 
			
		||||
        unsafe {
 | 
			
		||||
@@ -841,6 +836,11 @@ impl<'d, T: Instance> driver::ControlPipe for ControlPipe<'d, T> {
 | 
			
		||||
        let regs = T::regs();
 | 
			
		||||
        regs.tasks_ep0stall.write(|w| w.tasks_ep0stall().bit(true));
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    async fn accept_set_address(&mut self, _addr: u8) {
 | 
			
		||||
        self.accept().await;
 | 
			
		||||
        // Nothing to do, the peripheral handles this.
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
fn dma_start() {
 | 
			
		||||
 
 | 
			
		||||
@@ -406,13 +406,6 @@ impl<'d, T: Instance> driver::Bus for Bus<'d, T> {
 | 
			
		||||
        .await
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    #[inline]
 | 
			
		||||
    fn set_address(&mut self, addr: u8) {
 | 
			
		||||
        let regs = T::regs();
 | 
			
		||||
        trace!("setting addr: {}", addr);
 | 
			
		||||
        unsafe { regs.addr_endp().write(|w| w.set_address(addr)) }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    fn endpoint_set_stalled(&mut self, _ep_addr: EndpointAddress, _stalled: bool) {
 | 
			
		||||
        todo!();
 | 
			
		||||
    }
 | 
			
		||||
@@ -812,4 +805,12 @@ impl<'d, T: Instance> driver::ControlPipe for ControlPipe<'d, T> {
 | 
			
		||||
            T::dpram().ep_in_buffer_control(0).write(|w| w.set_stall(true));
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    async fn accept_set_address(&mut self, addr: u8) {
 | 
			
		||||
        self.accept().await;
 | 
			
		||||
 | 
			
		||||
        let regs = T::regs();
 | 
			
		||||
        trace!("setting addr: {}", addr);
 | 
			
		||||
        unsafe { regs.addr_endp().write(|w| w.set_address(addr)) }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -277,22 +277,20 @@ fn main() {
 | 
			
		||||
        (("dcmi", "PIXCLK"), quote!(crate::dcmi::PixClkPin)),
 | 
			
		||||
        (("usb", "DP"), quote!(crate::usb::DpPin)),
 | 
			
		||||
        (("usb", "DM"), quote!(crate::usb::DmPin)),
 | 
			
		||||
        (("otgfs", "DP"), quote!(crate::usb_otg::DpPin)),
 | 
			
		||||
        (("otgfs", "DM"), quote!(crate::usb_otg::DmPin)),
 | 
			
		||||
        (("otghs", "DP"), quote!(crate::usb_otg::DpPin)),
 | 
			
		||||
        (("otghs", "DM"), quote!(crate::usb_otg::DmPin)),
 | 
			
		||||
        (("otghs", "ULPI_CK"), quote!(crate::usb_otg::UlpiClkPin)),
 | 
			
		||||
        (("otghs", "ULPI_DIR"), quote!(crate::usb_otg::UlpiDirPin)),
 | 
			
		||||
        (("otghs", "ULPI_NXT"), quote!(crate::usb_otg::UlpiNxtPin)),
 | 
			
		||||
        (("otghs", "ULPI_STP"), quote!(crate::usb_otg::UlpiStpPin)),
 | 
			
		||||
        (("otghs", "ULPI_D0"), quote!(crate::usb_otg::UlpiD0Pin)),
 | 
			
		||||
        (("otghs", "ULPI_D1"), quote!(crate::usb_otg::UlpiD1Pin)),
 | 
			
		||||
        (("otghs", "ULPI_D2"), quote!(crate::usb_otg::UlpiD2Pin)),
 | 
			
		||||
        (("otghs", "ULPI_D3"), quote!(crate::usb_otg::UlpiD3Pin)),
 | 
			
		||||
        (("otghs", "ULPI_D4"), quote!(crate::usb_otg::UlpiD4Pin)),
 | 
			
		||||
        (("otghs", "ULPI_D5"), quote!(crate::usb_otg::UlpiD5Pin)),
 | 
			
		||||
        (("otghs", "ULPI_D6"), quote!(crate::usb_otg::UlpiD6Pin)),
 | 
			
		||||
        (("otghs", "ULPI_D7"), quote!(crate::usb_otg::UlpiD7Pin)),
 | 
			
		||||
        (("otg", "DP"), quote!(crate::usb_otg::DpPin)),
 | 
			
		||||
        (("otg", "DM"), quote!(crate::usb_otg::DmPin)),
 | 
			
		||||
        (("otg", "ULPI_CK"), quote!(crate::usb_otg::UlpiClkPin)),
 | 
			
		||||
        (("otg", "ULPI_DIR"), quote!(crate::usb_otg::UlpiDirPin)),
 | 
			
		||||
        (("otg", "ULPI_NXT"), quote!(crate::usb_otg::UlpiNxtPin)),
 | 
			
		||||
        (("otg", "ULPI_STP"), quote!(crate::usb_otg::UlpiStpPin)),
 | 
			
		||||
        (("otg", "ULPI_D0"), quote!(crate::usb_otg::UlpiD0Pin)),
 | 
			
		||||
        (("otg", "ULPI_D1"), quote!(crate::usb_otg::UlpiD1Pin)),
 | 
			
		||||
        (("otg", "ULPI_D2"), quote!(crate::usb_otg::UlpiD2Pin)),
 | 
			
		||||
        (("otg", "ULPI_D3"), quote!(crate::usb_otg::UlpiD3Pin)),
 | 
			
		||||
        (("otg", "ULPI_D4"), quote!(crate::usb_otg::UlpiD4Pin)),
 | 
			
		||||
        (("otg", "ULPI_D5"), quote!(crate::usb_otg::UlpiD5Pin)),
 | 
			
		||||
        (("otg", "ULPI_D6"), quote!(crate::usb_otg::UlpiD6Pin)),
 | 
			
		||||
        (("otg", "ULPI_D7"), quote!(crate::usb_otg::UlpiD7Pin)),
 | 
			
		||||
        (("can", "TX"), quote!(crate::can::TxPin)),
 | 
			
		||||
        (("can", "RX"), quote!(crate::can::RxPin)),
 | 
			
		||||
        (("eth", "REF_CLK"), quote!(crate::eth::RefClkPin)),
 | 
			
		||||
 
 | 
			
		||||
@@ -58,7 +58,7 @@ pub mod spi;
 | 
			
		||||
pub mod usart;
 | 
			
		||||
#[cfg(all(usb, feature = "time"))]
 | 
			
		||||
pub mod usb;
 | 
			
		||||
#[cfg(any(otgfs, otghs))]
 | 
			
		||||
#[cfg(otg)]
 | 
			
		||||
pub mod usb_otg;
 | 
			
		||||
 | 
			
		||||
#[cfg(iwdg)]
 | 
			
		||||
 
 | 
			
		||||
@@ -295,6 +295,7 @@ pub struct Config {
 | 
			
		||||
    pub apb1_pre: APBPrescaler,
 | 
			
		||||
    pub apb2_pre: APBPrescaler,
 | 
			
		||||
    pub apb3_pre: APBPrescaler,
 | 
			
		||||
    pub hsi48: bool,
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
impl Default for Config {
 | 
			
		||||
@@ -305,6 +306,7 @@ impl Default for Config {
 | 
			
		||||
            apb1_pre: Default::default(),
 | 
			
		||||
            apb2_pre: Default::default(),
 | 
			
		||||
            apb3_pre: Default::default(),
 | 
			
		||||
            hsi48: false,
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@@ -320,7 +322,6 @@ pub(crate) unsafe fn init(config: Config) {
 | 
			
		||||
            RCC.cr().write(|w| {
 | 
			
		||||
                w.set_msipllen(false);
 | 
			
		||||
                w.set_msison(true);
 | 
			
		||||
                w.set_msison(true);
 | 
			
		||||
            });
 | 
			
		||||
            while !RCC.cr().read().msisrdy() {}
 | 
			
		||||
 | 
			
		||||
@@ -340,9 +341,20 @@ pub(crate) unsafe fn init(config: Config) {
 | 
			
		||||
        }
 | 
			
		||||
        ClockSrc::PLL1R(src, m, n, div) => {
 | 
			
		||||
            let freq = match src {
 | 
			
		||||
                PllSrc::MSI(_) => MSIRange::default().into(),
 | 
			
		||||
                PllSrc::HSE(hertz) => hertz.0,
 | 
			
		||||
                PllSrc::HSI16 => HSI_FREQ.0,
 | 
			
		||||
                PllSrc::MSI(_) => {
 | 
			
		||||
                    // TODO: enable MSI
 | 
			
		||||
                    MSIRange::default().into()
 | 
			
		||||
                }
 | 
			
		||||
                PllSrc::HSE(hertz) => {
 | 
			
		||||
                    // TODO: enable HSE
 | 
			
		||||
                    hertz.0
 | 
			
		||||
                }
 | 
			
		||||
                PllSrc::HSI16 => {
 | 
			
		||||
                    RCC.cr().write(|w| w.set_hsion(true));
 | 
			
		||||
                    while !RCC.cr().read().hsirdy() {}
 | 
			
		||||
 | 
			
		||||
                    HSI_FREQ.0
 | 
			
		||||
                }
 | 
			
		||||
            };
 | 
			
		||||
 | 
			
		||||
            // disable
 | 
			
		||||
@@ -355,6 +367,7 @@ pub(crate) unsafe fn init(config: Config) {
 | 
			
		||||
            RCC.pll1cfgr().write(|w| {
 | 
			
		||||
                w.set_pllm(m.into());
 | 
			
		||||
                w.set_pllsrc(src.into());
 | 
			
		||||
                w.set_pllren(true);
 | 
			
		||||
            });
 | 
			
		||||
 | 
			
		||||
            RCC.pll1divr().modify(|w| {
 | 
			
		||||
@@ -365,15 +378,16 @@ pub(crate) unsafe fn init(config: Config) {
 | 
			
		||||
            // Enable PLL
 | 
			
		||||
            RCC.cr().modify(|w| w.set_pllon(0, true));
 | 
			
		||||
            while !RCC.cr().read().pllrdy(0) {}
 | 
			
		||||
            RCC.pll1cfgr().modify(|w| w.set_pllren(true));
 | 
			
		||||
 | 
			
		||||
            RCC.cr().write(|w| w.set_pllon(0, true));
 | 
			
		||||
            while !RCC.cr().read().pllrdy(0) {}
 | 
			
		||||
 | 
			
		||||
            pll_ck
 | 
			
		||||
        }
 | 
			
		||||
    };
 | 
			
		||||
 | 
			
		||||
    if config.hsi48 {
 | 
			
		||||
        RCC.cr().modify(|w| w.set_hsi48on(true));
 | 
			
		||||
        while !RCC.cr().read().hsi48rdy() {}
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    // TODO make configurable
 | 
			
		||||
    let power_vos = VoltageScale::Range4;
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -154,6 +154,7 @@ impl<'d, T: Instance> Driver<'d, T> {
 | 
			
		||||
 | 
			
		||||
            block_for(Duration::from_millis(100));
 | 
			
		||||
 | 
			
		||||
            #[cfg(not(usb_v4))]
 | 
			
		||||
            regs.btable().write(|w| w.set_btable(0));
 | 
			
		||||
 | 
			
		||||
            dp.set_as_af(dp.af_num(), AFType::OutputPushPull);
 | 
			
		||||
@@ -489,18 +490,6 @@ impl<'d, T: Instance> driver::Bus for Bus<'d, T> {
 | 
			
		||||
        .await
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    #[inline]
 | 
			
		||||
    fn set_address(&mut self, addr: u8) {
 | 
			
		||||
        let regs = T::regs();
 | 
			
		||||
        trace!("setting addr: {}", addr);
 | 
			
		||||
        unsafe {
 | 
			
		||||
            regs.daddr().write(|w| {
 | 
			
		||||
                w.set_ef(true);
 | 
			
		||||
                w.set_add(addr);
 | 
			
		||||
            })
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    fn endpoint_set_stalled(&mut self, ep_addr: EndpointAddress, stalled: bool) {
 | 
			
		||||
        // This can race, so do a retry loop.
 | 
			
		||||
        let reg = T::regs().epr(ep_addr.index() as _);
 | 
			
		||||
@@ -1017,4 +1006,17 @@ impl<'d, T: Instance> driver::ControlPipe for ControlPipe<'d, T> {
 | 
			
		||||
            });
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    async fn accept_set_address(&mut self, addr: u8) {
 | 
			
		||||
        self.accept().await;
 | 
			
		||||
 | 
			
		||||
        let regs = T::regs();
 | 
			
		||||
        trace!("setting addr: {}", addr);
 | 
			
		||||
        unsafe {
 | 
			
		||||
            regs.daddr().write(|w| {
 | 
			
		||||
                w.set_ef(true);
 | 
			
		||||
                w.set_add(addr);
 | 
			
		||||
            })
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -1,213 +0,0 @@
 | 
			
		||||
use core::marker::PhantomData;
 | 
			
		||||
 | 
			
		||||
use embassy_hal_common::into_ref;
 | 
			
		||||
 | 
			
		||||
use crate::gpio::sealed::AFType;
 | 
			
		||||
use crate::rcc::RccPeripheral;
 | 
			
		||||
use crate::{peripherals, Peripheral};
 | 
			
		||||
 | 
			
		||||
macro_rules! config_ulpi_pins {
 | 
			
		||||
    ($($pin:ident),*) => {
 | 
			
		||||
        into_ref!($($pin),*);
 | 
			
		||||
        // NOTE(unsafe) Exclusive access to the registers
 | 
			
		||||
        critical_section::with(|_| unsafe {
 | 
			
		||||
            $(
 | 
			
		||||
                $pin.set_as_af($pin.af_num(), AFType::OutputPushPull);
 | 
			
		||||
                #[cfg(gpio_v2)]
 | 
			
		||||
                $pin.set_speed(crate::gpio::Speed::VeryHigh);
 | 
			
		||||
            )*
 | 
			
		||||
        })
 | 
			
		||||
    };
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/// USB PHY type
 | 
			
		||||
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
 | 
			
		||||
pub enum PhyType {
 | 
			
		||||
    /// Internal Full-Speed PHY
 | 
			
		||||
    ///
 | 
			
		||||
    /// Available on most High-Speed peripherals.
 | 
			
		||||
    InternalFullSpeed,
 | 
			
		||||
    /// Internal High-Speed PHY
 | 
			
		||||
    ///
 | 
			
		||||
    /// Available on a few STM32 chips.
 | 
			
		||||
    InternalHighSpeed,
 | 
			
		||||
    /// External ULPI High-Speed PHY
 | 
			
		||||
    ExternalHighSpeed,
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
pub struct UsbOtg<'d, T: Instance> {
 | 
			
		||||
    phantom: PhantomData<&'d mut T>,
 | 
			
		||||
    _phy_type: PhyType,
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
impl<'d, T: Instance> UsbOtg<'d, T> {
 | 
			
		||||
    /// Initializes USB OTG peripheral with internal Full-Speed PHY
 | 
			
		||||
    pub fn new_fs(
 | 
			
		||||
        _peri: impl Peripheral<P = T> + 'd,
 | 
			
		||||
        dp: impl Peripheral<P = impl DpPin<T>> + 'd,
 | 
			
		||||
        dm: impl Peripheral<P = impl DmPin<T>> + 'd,
 | 
			
		||||
    ) -> Self {
 | 
			
		||||
        into_ref!(dp, dm);
 | 
			
		||||
 | 
			
		||||
        unsafe {
 | 
			
		||||
            dp.set_as_af(dp.af_num(), AFType::OutputPushPull);
 | 
			
		||||
            dm.set_as_af(dm.af_num(), AFType::OutputPushPull);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        Self {
 | 
			
		||||
            phantom: PhantomData,
 | 
			
		||||
            _phy_type: PhyType::InternalFullSpeed,
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /// Initializes USB OTG peripheral with external High-Speed PHY
 | 
			
		||||
    pub fn new_hs_ulpi(
 | 
			
		||||
        _peri: impl Peripheral<P = T> + 'd,
 | 
			
		||||
        ulpi_clk: impl Peripheral<P = impl UlpiClkPin<T>> + 'd,
 | 
			
		||||
        ulpi_dir: impl Peripheral<P = impl UlpiDirPin<T>> + 'd,
 | 
			
		||||
        ulpi_nxt: impl Peripheral<P = impl UlpiNxtPin<T>> + 'd,
 | 
			
		||||
        ulpi_stp: impl Peripheral<P = impl UlpiStpPin<T>> + 'd,
 | 
			
		||||
        ulpi_d0: impl Peripheral<P = impl UlpiD0Pin<T>> + 'd,
 | 
			
		||||
        ulpi_d1: impl Peripheral<P = impl UlpiD1Pin<T>> + 'd,
 | 
			
		||||
        ulpi_d2: impl Peripheral<P = impl UlpiD2Pin<T>> + 'd,
 | 
			
		||||
        ulpi_d3: impl Peripheral<P = impl UlpiD3Pin<T>> + 'd,
 | 
			
		||||
        ulpi_d4: impl Peripheral<P = impl UlpiD4Pin<T>> + 'd,
 | 
			
		||||
        ulpi_d5: impl Peripheral<P = impl UlpiD5Pin<T>> + 'd,
 | 
			
		||||
        ulpi_d6: impl Peripheral<P = impl UlpiD6Pin<T>> + 'd,
 | 
			
		||||
        ulpi_d7: impl Peripheral<P = impl UlpiD7Pin<T>> + 'd,
 | 
			
		||||
    ) -> Self {
 | 
			
		||||
        config_ulpi_pins!(
 | 
			
		||||
            ulpi_clk, ulpi_dir, ulpi_nxt, ulpi_stp, ulpi_d0, ulpi_d1, ulpi_d2, ulpi_d3, ulpi_d4, ulpi_d5, ulpi_d6,
 | 
			
		||||
            ulpi_d7
 | 
			
		||||
        );
 | 
			
		||||
 | 
			
		||||
        Self {
 | 
			
		||||
            phantom: PhantomData,
 | 
			
		||||
            _phy_type: PhyType::ExternalHighSpeed,
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
impl<'d, T: Instance> Drop for UsbOtg<'d, T> {
 | 
			
		||||
    fn drop(&mut self) {
 | 
			
		||||
        T::reset();
 | 
			
		||||
        T::disable();
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
pub(crate) mod sealed {
 | 
			
		||||
    pub trait Instance {
 | 
			
		||||
        const REGISTERS: *const ();
 | 
			
		||||
        const HIGH_SPEED: bool;
 | 
			
		||||
        const FIFO_DEPTH_WORDS: usize;
 | 
			
		||||
        const ENDPOINT_COUNT: usize;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
pub trait Instance: sealed::Instance + RccPeripheral {}
 | 
			
		||||
 | 
			
		||||
// Internal PHY pins
 | 
			
		||||
pin_trait!(DpPin, Instance);
 | 
			
		||||
pin_trait!(DmPin, Instance);
 | 
			
		||||
 | 
			
		||||
// External PHY pins
 | 
			
		||||
pin_trait!(UlpiClkPin, Instance);
 | 
			
		||||
pin_trait!(UlpiDirPin, Instance);
 | 
			
		||||
pin_trait!(UlpiNxtPin, Instance);
 | 
			
		||||
pin_trait!(UlpiStpPin, Instance);
 | 
			
		||||
pin_trait!(UlpiD0Pin, Instance);
 | 
			
		||||
pin_trait!(UlpiD1Pin, Instance);
 | 
			
		||||
pin_trait!(UlpiD2Pin, Instance);
 | 
			
		||||
pin_trait!(UlpiD3Pin, Instance);
 | 
			
		||||
pin_trait!(UlpiD4Pin, Instance);
 | 
			
		||||
pin_trait!(UlpiD5Pin, Instance);
 | 
			
		||||
pin_trait!(UlpiD6Pin, Instance);
 | 
			
		||||
pin_trait!(UlpiD7Pin, Instance);
 | 
			
		||||
 | 
			
		||||
foreach_peripheral!(
 | 
			
		||||
    (otgfs, $inst:ident) => {
 | 
			
		||||
        impl sealed::Instance for peripherals::$inst {
 | 
			
		||||
            const REGISTERS: *const () = crate::pac::$inst.0 as *const ();
 | 
			
		||||
            const HIGH_SPEED: bool = false;
 | 
			
		||||
 | 
			
		||||
            cfg_if::cfg_if! {
 | 
			
		||||
                if #[cfg(stm32f1)] {
 | 
			
		||||
                    const FIFO_DEPTH_WORDS: usize = 128;
 | 
			
		||||
                    const ENDPOINT_COUNT: usize = 8;
 | 
			
		||||
                } else if #[cfg(any(
 | 
			
		||||
                    stm32f2,
 | 
			
		||||
                    stm32f401,
 | 
			
		||||
                    stm32f405,
 | 
			
		||||
                    stm32f407,
 | 
			
		||||
                    stm32f411,
 | 
			
		||||
                    stm32f415,
 | 
			
		||||
                    stm32f417,
 | 
			
		||||
                    stm32f427,
 | 
			
		||||
                    stm32f429,
 | 
			
		||||
                    stm32f437,
 | 
			
		||||
                    stm32f439,
 | 
			
		||||
                ))] {
 | 
			
		||||
                    const FIFO_DEPTH_WORDS: usize = 320;
 | 
			
		||||
                    const ENDPOINT_COUNT: usize = 4;
 | 
			
		||||
                } else if #[cfg(any(
 | 
			
		||||
                    stm32f412,
 | 
			
		||||
                    stm32f413,
 | 
			
		||||
                    stm32f423,
 | 
			
		||||
                    stm32f446,
 | 
			
		||||
                    stm32f469,
 | 
			
		||||
                    stm32f479,
 | 
			
		||||
                    stm32f7,
 | 
			
		||||
                    stm32l4,
 | 
			
		||||
                    stm32u5,
 | 
			
		||||
                ))] {
 | 
			
		||||
                    const FIFO_DEPTH_WORDS: usize = 320;
 | 
			
		||||
                    const ENDPOINT_COUNT: usize = 6;
 | 
			
		||||
                } else if #[cfg(stm32g0x1)] {
 | 
			
		||||
                    const FIFO_DEPTH_WORDS: usize = 512;
 | 
			
		||||
                    const ENDPOINT_COUNT: usize = 8;
 | 
			
		||||
                } else {
 | 
			
		||||
                    compile_error!("USB_OTG_FS peripheral is not supported by this chip.");
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        impl Instance for peripherals::$inst {}
 | 
			
		||||
    };
 | 
			
		||||
 | 
			
		||||
    (otghs, $inst:ident) => {
 | 
			
		||||
        impl sealed::Instance for peripherals::$inst {
 | 
			
		||||
            const REGISTERS: *const () = crate::pac::$inst.0 as *const ();
 | 
			
		||||
            const HIGH_SPEED: bool = true;
 | 
			
		||||
 | 
			
		||||
            cfg_if::cfg_if! {
 | 
			
		||||
                if #[cfg(any(
 | 
			
		||||
                    stm32f2,
 | 
			
		||||
                    stm32f405,
 | 
			
		||||
                    stm32f407,
 | 
			
		||||
                    stm32f415,
 | 
			
		||||
                    stm32f417,
 | 
			
		||||
                    stm32f427,
 | 
			
		||||
                    stm32f429,
 | 
			
		||||
                    stm32f437,
 | 
			
		||||
                    stm32f439,
 | 
			
		||||
                ))] {
 | 
			
		||||
                    const FIFO_DEPTH_WORDS: usize = 1024;
 | 
			
		||||
                    const ENDPOINT_COUNT: usize = 6;
 | 
			
		||||
                } else if #[cfg(any(
 | 
			
		||||
                    stm32f446,
 | 
			
		||||
                    stm32f469,
 | 
			
		||||
                    stm32f479,
 | 
			
		||||
                    stm32f7,
 | 
			
		||||
                    stm32h7,
 | 
			
		||||
                ))] {
 | 
			
		||||
                    const FIFO_DEPTH_WORDS: usize = 1024;
 | 
			
		||||
                    const ENDPOINT_COUNT: usize = 9;
 | 
			
		||||
                } else {
 | 
			
		||||
                    compile_error!("USB_OTG_HS peripheral is not supported by this chip.");
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        impl Instance for peripherals::$inst {}
 | 
			
		||||
    };
 | 
			
		||||
);
 | 
			
		||||
							
								
								
									
										161
									
								
								embassy-stm32/src/usb_otg/mod.rs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										161
									
								
								embassy-stm32/src/usb_otg/mod.rs
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,161 @@
 | 
			
		||||
use embassy_cortex_m::interrupt::Interrupt;
 | 
			
		||||
 | 
			
		||||
use crate::peripherals;
 | 
			
		||||
use crate::rcc::RccPeripheral;
 | 
			
		||||
 | 
			
		||||
#[cfg(feature = "nightly")]
 | 
			
		||||
mod usb;
 | 
			
		||||
#[cfg(feature = "nightly")]
 | 
			
		||||
pub use usb::*;
 | 
			
		||||
 | 
			
		||||
// Using Instance::ENDPOINT_COUNT requires feature(const_generic_expr) so just define maximum eps
 | 
			
		||||
#[cfg(feature = "nightly")]
 | 
			
		||||
const MAX_EP_COUNT: usize = 9;
 | 
			
		||||
 | 
			
		||||
pub(crate) mod sealed {
 | 
			
		||||
    pub trait Instance {
 | 
			
		||||
        const HIGH_SPEED: bool;
 | 
			
		||||
        const FIFO_DEPTH_WORDS: u16;
 | 
			
		||||
        const ENDPOINT_COUNT: usize;
 | 
			
		||||
 | 
			
		||||
        fn regs() -> crate::pac::otg::Otg;
 | 
			
		||||
        #[cfg(feature = "nightly")]
 | 
			
		||||
        fn state() -> &'static super::State<{ super::MAX_EP_COUNT }>;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
pub trait Instance: sealed::Instance + RccPeripheral {
 | 
			
		||||
    type Interrupt: Interrupt;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// Internal PHY pins
 | 
			
		||||
pin_trait!(DpPin, Instance);
 | 
			
		||||
pin_trait!(DmPin, Instance);
 | 
			
		||||
 | 
			
		||||
// External PHY pins
 | 
			
		||||
pin_trait!(UlpiClkPin, Instance);
 | 
			
		||||
pin_trait!(UlpiDirPin, Instance);
 | 
			
		||||
pin_trait!(UlpiNxtPin, Instance);
 | 
			
		||||
pin_trait!(UlpiStpPin, Instance);
 | 
			
		||||
pin_trait!(UlpiD0Pin, Instance);
 | 
			
		||||
pin_trait!(UlpiD1Pin, Instance);
 | 
			
		||||
pin_trait!(UlpiD2Pin, Instance);
 | 
			
		||||
pin_trait!(UlpiD3Pin, Instance);
 | 
			
		||||
pin_trait!(UlpiD4Pin, Instance);
 | 
			
		||||
pin_trait!(UlpiD5Pin, Instance);
 | 
			
		||||
pin_trait!(UlpiD6Pin, Instance);
 | 
			
		||||
pin_trait!(UlpiD7Pin, Instance);
 | 
			
		||||
 | 
			
		||||
foreach_interrupt!(
 | 
			
		||||
    (USB_OTG_FS, otg, $block:ident, GLOBAL, $irq:ident) => {
 | 
			
		||||
        impl sealed::Instance for peripherals::USB_OTG_FS {
 | 
			
		||||
            const HIGH_SPEED: bool = false;
 | 
			
		||||
 | 
			
		||||
            cfg_if::cfg_if! {
 | 
			
		||||
                if #[cfg(stm32f1)] {
 | 
			
		||||
                    const FIFO_DEPTH_WORDS: u16 = 128;
 | 
			
		||||
                    const ENDPOINT_COUNT: usize = 8;
 | 
			
		||||
                } else if #[cfg(any(
 | 
			
		||||
                    stm32f2,
 | 
			
		||||
                    stm32f401,
 | 
			
		||||
                    stm32f405,
 | 
			
		||||
                    stm32f407,
 | 
			
		||||
                    stm32f411,
 | 
			
		||||
                    stm32f415,
 | 
			
		||||
                    stm32f417,
 | 
			
		||||
                    stm32f427,
 | 
			
		||||
                    stm32f429,
 | 
			
		||||
                    stm32f437,
 | 
			
		||||
                    stm32f439,
 | 
			
		||||
                ))] {
 | 
			
		||||
                    const FIFO_DEPTH_WORDS: u16 = 320;
 | 
			
		||||
                    const ENDPOINT_COUNT: usize = 4;
 | 
			
		||||
                } else if #[cfg(any(
 | 
			
		||||
                    stm32f412,
 | 
			
		||||
                    stm32f413,
 | 
			
		||||
                    stm32f423,
 | 
			
		||||
                    stm32f446,
 | 
			
		||||
                    stm32f469,
 | 
			
		||||
                    stm32f479,
 | 
			
		||||
                    stm32f7,
 | 
			
		||||
                    stm32l4,
 | 
			
		||||
                    stm32u5,
 | 
			
		||||
                ))] {
 | 
			
		||||
                    const FIFO_DEPTH_WORDS: u16 = 320;
 | 
			
		||||
                    const ENDPOINT_COUNT: usize = 6;
 | 
			
		||||
                } else if #[cfg(stm32g0x1)] {
 | 
			
		||||
                    const FIFO_DEPTH_WORDS: u16 = 512;
 | 
			
		||||
                    const ENDPOINT_COUNT: usize = 8;
 | 
			
		||||
                } else if #[cfg(stm32h7)] {
 | 
			
		||||
                    const FIFO_DEPTH_WORDS: u16 = 1024;
 | 
			
		||||
                    const ENDPOINT_COUNT: usize = 9;
 | 
			
		||||
                } else {
 | 
			
		||||
                    compile_error!("USB_OTG_FS peripheral is not supported by this chip.");
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            fn regs() -> crate::pac::otg::Otg {
 | 
			
		||||
                crate::pac::USB_OTG_FS
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            #[cfg(feature = "nightly")]
 | 
			
		||||
            fn state() -> &'static State<MAX_EP_COUNT> {
 | 
			
		||||
                static STATE: State<MAX_EP_COUNT> = State::new();
 | 
			
		||||
                &STATE
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        impl Instance for peripherals::USB_OTG_FS {
 | 
			
		||||
            type Interrupt = crate::interrupt::$irq;
 | 
			
		||||
        }
 | 
			
		||||
    };
 | 
			
		||||
 | 
			
		||||
    (USB_OTG_HS, otg, $block:ident, GLOBAL, $irq:ident) => {
 | 
			
		||||
        impl sealed::Instance for peripherals::USB_OTG_HS {
 | 
			
		||||
            const HIGH_SPEED: bool = true;
 | 
			
		||||
 | 
			
		||||
            cfg_if::cfg_if! {
 | 
			
		||||
                if #[cfg(any(
 | 
			
		||||
                    stm32f2,
 | 
			
		||||
                    stm32f405,
 | 
			
		||||
                    stm32f407,
 | 
			
		||||
                    stm32f415,
 | 
			
		||||
                    stm32f417,
 | 
			
		||||
                    stm32f427,
 | 
			
		||||
                    stm32f429,
 | 
			
		||||
                    stm32f437,
 | 
			
		||||
                    stm32f439,
 | 
			
		||||
                ))] {
 | 
			
		||||
                    const FIFO_DEPTH_WORDS: u16 = 1024;
 | 
			
		||||
                    const ENDPOINT_COUNT: usize = 6;
 | 
			
		||||
                } else if #[cfg(any(
 | 
			
		||||
                    stm32f446,
 | 
			
		||||
                    stm32f469,
 | 
			
		||||
                    stm32f479,
 | 
			
		||||
                    stm32f7,
 | 
			
		||||
                    stm32h7,
 | 
			
		||||
                ))] {
 | 
			
		||||
                    const FIFO_DEPTH_WORDS: u16 = 1024;
 | 
			
		||||
                    const ENDPOINT_COUNT: usize = 9;
 | 
			
		||||
                } else {
 | 
			
		||||
                    compile_error!("USB_OTG_HS peripheral is not supported by this chip.");
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            fn regs() -> crate::pac::otg::Otg {
 | 
			
		||||
                // OTG HS registers are a superset of FS registers
 | 
			
		||||
                crate::pac::otg::Otg(crate::pac::USB_OTG_HS.0)
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            #[cfg(feature = "nightly")]
 | 
			
		||||
            fn state() -> &'static State<MAX_EP_COUNT> {
 | 
			
		||||
                static STATE: State<MAX_EP_COUNT> = State::new();
 | 
			
		||||
                &STATE
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        impl Instance for peripherals::USB_OTG_HS {
 | 
			
		||||
            type Interrupt = crate::interrupt::$irq;
 | 
			
		||||
        }
 | 
			
		||||
    };
 | 
			
		||||
);
 | 
			
		||||
							
								
								
									
										1346
									
								
								embassy-stm32/src/usb_otg/usb.rs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										1346
									
								
								embassy-stm32/src/usb_otg/usb.rs
									
									
									
									
									
										Normal file
									
								
							
										
											
												File diff suppressed because it is too large
												Load Diff
											
										
									
								
							@@ -164,9 +164,6 @@ pub trait Bus {
 | 
			
		||||
 | 
			
		||||
    async fn poll(&mut self) -> Event;
 | 
			
		||||
 | 
			
		||||
    /// Sets the device USB address to `addr`.
 | 
			
		||||
    fn set_address(&mut self, addr: u8);
 | 
			
		||||
 | 
			
		||||
    /// Enables or disables an endpoint.
 | 
			
		||||
    fn endpoint_set_enabled(&mut self, ep_addr: EndpointAddress, enabled: bool);
 | 
			
		||||
 | 
			
		||||
@@ -306,6 +303,12 @@ pub trait ControlPipe {
 | 
			
		||||
    ///
 | 
			
		||||
    /// Sets a STALL condition on the pipe to indicate an error.
 | 
			
		||||
    async fn reject(&mut self);
 | 
			
		||||
 | 
			
		||||
    /// Accept SET_ADDRESS control and change bus address.
 | 
			
		||||
    ///
 | 
			
		||||
    /// For most drivers this function should firstly call `accept()` and then change the bus address.
 | 
			
		||||
    /// However, there are peripherals (Synopsys USB OTG) that have reverse order.
 | 
			
		||||
    async fn accept_set_address(&mut self, addr: u8);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
pub trait EndpointIn: Endpoint {
 | 
			
		||||
 
 | 
			
		||||
@@ -122,10 +122,9 @@ struct Inner<'d, D: Driver<'d>> {
 | 
			
		||||
 | 
			
		||||
    /// Our device address, or 0 if none.
 | 
			
		||||
    address: u8,
 | 
			
		||||
    /// When receiving a set addr control request, we have to apply it AFTER we've
 | 
			
		||||
    /// finished handling the control request, as the status stage still has to be
 | 
			
		||||
    /// handled with addr 0.
 | 
			
		||||
    /// If true, do a set_addr after finishing the current control req.
 | 
			
		||||
    /// SET_ADDRESS requests have special handling depending on the driver.
 | 
			
		||||
    /// This flag indicates that requests must be handled by `ControlPipe::accept_set_address()`
 | 
			
		||||
    /// instead of regular `accept()`.
 | 
			
		||||
    set_address_pending: bool,
 | 
			
		||||
 | 
			
		||||
    interfaces: Vec<Interface<'d>, MAX_INTERFACE_COUNT>,
 | 
			
		||||
@@ -254,11 +253,6 @@ impl<'d, D: Driver<'d>> UsbDevice<'d, D> {
 | 
			
		||||
            Direction::In => self.handle_control_in(req).await,
 | 
			
		||||
            Direction::Out => self.handle_control_out(req).await,
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        if self.inner.set_address_pending {
 | 
			
		||||
            self.inner.bus.set_address(self.inner.address);
 | 
			
		||||
            self.inner.set_address_pending = false;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    async fn handle_control_in(&mut self, req: Request) {
 | 
			
		||||
@@ -328,7 +322,14 @@ impl<'d, D: Driver<'d>> UsbDevice<'d, D> {
 | 
			
		||||
        trace!("  control out data: {:02x?}", data);
 | 
			
		||||
 | 
			
		||||
        match self.inner.handle_control_out(req, data) {
 | 
			
		||||
            OutResponse::Accepted => self.control.accept().await,
 | 
			
		||||
            OutResponse::Accepted => {
 | 
			
		||||
                if self.inner.set_address_pending {
 | 
			
		||||
                    self.control.accept_set_address(self.inner.address).await;
 | 
			
		||||
                    self.inner.set_address_pending = false;
 | 
			
		||||
                } else {
 | 
			
		||||
                    self.control.accept().await
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
            OutResponse::Rejected => self.control.reject().await,
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
@@ -655,7 +656,7 @@ impl<'d, D: Driver<'d>> Inner<'d, D> {
 | 
			
		||||
                        buf[1] = descriptor_type::STRING;
 | 
			
		||||
                        let mut pos = 2;
 | 
			
		||||
                        for c in s.encode_utf16() {
 | 
			
		||||
                            if pos >= buf.len() {
 | 
			
		||||
                            if pos + 2 >= buf.len() {
 | 
			
		||||
                                panic!("control buffer too small");
 | 
			
		||||
                            }
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -10,6 +10,7 @@ embassy-sync = { version = "0.1.0", path = "../../embassy-sync", features = ["de
 | 
			
		||||
embassy-executor = { version = "0.1.0", path = "../../embassy-executor", features = ["defmt", "integrated-timers"] }
 | 
			
		||||
embassy-time = { version = "0.1.0", path = "../../embassy-time", features = ["defmt", "defmt-timestamp-uptime", "unstable-traits", "tick-hz-32_768"] }
 | 
			
		||||
embassy-stm32 = { version = "0.1.0", path = "../../embassy-stm32", features = ["nightly", "unstable-traits", "defmt", "stm32f429zi", "unstable-pac", "memory-x", "time-driver-any", "exti"]  }
 | 
			
		||||
embassy-usb = { version = "0.1.0", path = "../../embassy-usb", features = ["defmt"] }
 | 
			
		||||
 | 
			
		||||
defmt = "0.3"
 | 
			
		||||
defmt-rtt = "0.4"
 | 
			
		||||
@@ -26,5 +27,5 @@ embedded-storage = "0.3.0"
 | 
			
		||||
micromath = "2.0.0"
 | 
			
		||||
static_cell = "1.0"
 | 
			
		||||
 | 
			
		||||
usb-device = "0.2"
 | 
			
		||||
usbd-serial = "0.1.1"
 | 
			
		||||
[profile.release]
 | 
			
		||||
debug = 2
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										106
									
								
								examples/stm32f4/src/bin/usb_serial.rs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										106
									
								
								examples/stm32f4/src/bin/usb_serial.rs
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,106 @@
 | 
			
		||||
#![no_std]
 | 
			
		||||
#![no_main]
 | 
			
		||||
#![feature(type_alias_impl_trait)]
 | 
			
		||||
 | 
			
		||||
use defmt::{panic, *};
 | 
			
		||||
use embassy_executor::Spawner;
 | 
			
		||||
use embassy_stm32::time::mhz;
 | 
			
		||||
use embassy_stm32::usb_otg::{Driver, Instance};
 | 
			
		||||
use embassy_stm32::{interrupt, Config};
 | 
			
		||||
use embassy_usb::class::cdc_acm::{CdcAcmClass, State};
 | 
			
		||||
use embassy_usb::driver::EndpointError;
 | 
			
		||||
use embassy_usb::Builder;
 | 
			
		||||
use futures::future::join;
 | 
			
		||||
use {defmt_rtt as _, panic_probe as _};
 | 
			
		||||
 | 
			
		||||
#[embassy_executor::main]
 | 
			
		||||
async fn main(_spawner: Spawner) {
 | 
			
		||||
    info!("Hello World!");
 | 
			
		||||
 | 
			
		||||
    let mut config = Config::default();
 | 
			
		||||
    config.rcc.pll48 = true;
 | 
			
		||||
    config.rcc.sys_ck = Some(mhz(48));
 | 
			
		||||
 | 
			
		||||
    let p = embassy_stm32::init(config);
 | 
			
		||||
 | 
			
		||||
    // Create the driver, from the HAL.
 | 
			
		||||
    let irq = interrupt::take!(OTG_FS);
 | 
			
		||||
    let mut ep_out_buffer = [0u8; 256];
 | 
			
		||||
    let driver = Driver::new_fs(p.USB_OTG_FS, irq, p.PA12, p.PA11, &mut ep_out_buffer);
 | 
			
		||||
 | 
			
		||||
    // Create embassy-usb Config
 | 
			
		||||
    let mut config = embassy_usb::Config::new(0xc0de, 0xcafe);
 | 
			
		||||
    config.manufacturer = Some("Embassy");
 | 
			
		||||
    config.product = Some("USB-serial example");
 | 
			
		||||
    config.serial_number = Some("12345678");
 | 
			
		||||
 | 
			
		||||
    // Required for windows compatiblity.
 | 
			
		||||
    // https://developer.nordicsemi.com/nRF_Connect_SDK/doc/1.9.1/kconfig/CONFIG_CDC_ACM_IAD.html#help
 | 
			
		||||
    config.device_class = 0xEF;
 | 
			
		||||
    config.device_sub_class = 0x02;
 | 
			
		||||
    config.device_protocol = 0x01;
 | 
			
		||||
    config.composite_with_iads = true;
 | 
			
		||||
 | 
			
		||||
    // Create embassy-usb DeviceBuilder using the driver and config.
 | 
			
		||||
    // It needs some buffers for building the descriptors.
 | 
			
		||||
    let mut device_descriptor = [0; 256];
 | 
			
		||||
    let mut config_descriptor = [0; 256];
 | 
			
		||||
    let mut bos_descriptor = [0; 256];
 | 
			
		||||
    let mut control_buf = [0; 64];
 | 
			
		||||
 | 
			
		||||
    let mut state = State::new();
 | 
			
		||||
 | 
			
		||||
    let mut builder = Builder::new(
 | 
			
		||||
        driver,
 | 
			
		||||
        config,
 | 
			
		||||
        &mut device_descriptor,
 | 
			
		||||
        &mut config_descriptor,
 | 
			
		||||
        &mut bos_descriptor,
 | 
			
		||||
        &mut control_buf,
 | 
			
		||||
        None,
 | 
			
		||||
    );
 | 
			
		||||
 | 
			
		||||
    // Create classes on the builder.
 | 
			
		||||
    let mut class = CdcAcmClass::new(&mut builder, &mut state, 64);
 | 
			
		||||
 | 
			
		||||
    // Build the builder.
 | 
			
		||||
    let mut usb = builder.build();
 | 
			
		||||
 | 
			
		||||
    // Run the USB device.
 | 
			
		||||
    let usb_fut = usb.run();
 | 
			
		||||
 | 
			
		||||
    // Do stuff with the class!
 | 
			
		||||
    let echo_fut = async {
 | 
			
		||||
        loop {
 | 
			
		||||
            class.wait_connection().await;
 | 
			
		||||
            info!("Connected");
 | 
			
		||||
            let _ = echo(&mut class).await;
 | 
			
		||||
            info!("Disconnected");
 | 
			
		||||
        }
 | 
			
		||||
    };
 | 
			
		||||
 | 
			
		||||
    // Run everything concurrently.
 | 
			
		||||
    // If we had made everything `'static` above instead, we could do this using separate tasks instead.
 | 
			
		||||
    join(usb_fut, echo_fut).await;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
struct Disconnected {}
 | 
			
		||||
 | 
			
		||||
impl From<EndpointError> for Disconnected {
 | 
			
		||||
    fn from(val: EndpointError) -> Self {
 | 
			
		||||
        match val {
 | 
			
		||||
            EndpointError::BufferOverflow => panic!("Buffer overflow"),
 | 
			
		||||
            EndpointError::Disabled => Disconnected {},
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
async fn echo<'d, T: Instance + 'd>(class: &mut CdcAcmClass<'d, Driver<'d, T>>) -> Result<(), Disconnected> {
 | 
			
		||||
    let mut buf = [0; 64];
 | 
			
		||||
    loop {
 | 
			
		||||
        let n = class.read_packet(&mut buf).await?;
 | 
			
		||||
        let data = &buf[..n];
 | 
			
		||||
        info!("data: {:x}", data);
 | 
			
		||||
        class.write_packet(data).await?;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@@ -11,6 +11,7 @@ embassy-time = { version = "0.1.0", path = "../../embassy-time", features = ["de
 | 
			
		||||
embassy-stm32 = { version = "0.1.0", path = "../../embassy-stm32", features = ["nightly", "defmt", "stm32f767zi", "unstable-pac", "time-driver-any", "exti"]  }
 | 
			
		||||
embassy-net = { path = "../../embassy-net", features = ["defmt", "nightly", "tcp", "dhcpv4", "medium-ethernet"] }
 | 
			
		||||
embedded-io = { version = "0.4.0", features = ["async"] }
 | 
			
		||||
embassy-usb = { version = "0.1.0", path = "../../embassy-usb", features = ["defmt"] }
 | 
			
		||||
 | 
			
		||||
defmt = "0.3"
 | 
			
		||||
defmt-rtt = "0.4"
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										107
									
								
								examples/stm32f7/src/bin/usb_serial.rs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										107
									
								
								examples/stm32f7/src/bin/usb_serial.rs
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,107 @@
 | 
			
		||||
#![no_std]
 | 
			
		||||
#![no_main]
 | 
			
		||||
#![feature(type_alias_impl_trait)]
 | 
			
		||||
 | 
			
		||||
use defmt::{panic, *};
 | 
			
		||||
use embassy_executor::Spawner;
 | 
			
		||||
use embassy_stm32::time::mhz;
 | 
			
		||||
use embassy_stm32::usb_otg::{Driver, Instance};
 | 
			
		||||
use embassy_stm32::{interrupt, Config};
 | 
			
		||||
use embassy_usb::class::cdc_acm::{CdcAcmClass, State};
 | 
			
		||||
use embassy_usb::driver::EndpointError;
 | 
			
		||||
use embassy_usb::Builder;
 | 
			
		||||
use futures::future::join;
 | 
			
		||||
use {defmt_rtt as _, panic_probe as _};
 | 
			
		||||
 | 
			
		||||
#[embassy_executor::main]
 | 
			
		||||
async fn main(_spawner: Spawner) {
 | 
			
		||||
    info!("Hello World!");
 | 
			
		||||
 | 
			
		||||
    let mut config = Config::default();
 | 
			
		||||
    config.rcc.hse = Some(mhz(8));
 | 
			
		||||
    config.rcc.pll48 = true;
 | 
			
		||||
    config.rcc.sys_ck = Some(mhz(200));
 | 
			
		||||
 | 
			
		||||
    let p = embassy_stm32::init(config);
 | 
			
		||||
 | 
			
		||||
    // Create the driver, from the HAL.
 | 
			
		||||
    let irq = interrupt::take!(OTG_FS);
 | 
			
		||||
    let mut ep_out_buffer = [0u8; 256];
 | 
			
		||||
    let driver = Driver::new_fs(p.USB_OTG_FS, irq, p.PA12, p.PA11, &mut ep_out_buffer);
 | 
			
		||||
 | 
			
		||||
    // Create embassy-usb Config
 | 
			
		||||
    let mut config = embassy_usb::Config::new(0xc0de, 0xcafe);
 | 
			
		||||
    config.manufacturer = Some("Embassy");
 | 
			
		||||
    config.product = Some("USB-serial example");
 | 
			
		||||
    config.serial_number = Some("12345678");
 | 
			
		||||
 | 
			
		||||
    // Required for windows compatiblity.
 | 
			
		||||
    // https://developer.nordicsemi.com/nRF_Connect_SDK/doc/1.9.1/kconfig/CONFIG_CDC_ACM_IAD.html#help
 | 
			
		||||
    config.device_class = 0xEF;
 | 
			
		||||
    config.device_sub_class = 0x02;
 | 
			
		||||
    config.device_protocol = 0x01;
 | 
			
		||||
    config.composite_with_iads = true;
 | 
			
		||||
 | 
			
		||||
    // Create embassy-usb DeviceBuilder using the driver and config.
 | 
			
		||||
    // It needs some buffers for building the descriptors.
 | 
			
		||||
    let mut device_descriptor = [0; 256];
 | 
			
		||||
    let mut config_descriptor = [0; 256];
 | 
			
		||||
    let mut bos_descriptor = [0; 256];
 | 
			
		||||
    let mut control_buf = [0; 64];
 | 
			
		||||
 | 
			
		||||
    let mut state = State::new();
 | 
			
		||||
 | 
			
		||||
    let mut builder = Builder::new(
 | 
			
		||||
        driver,
 | 
			
		||||
        config,
 | 
			
		||||
        &mut device_descriptor,
 | 
			
		||||
        &mut config_descriptor,
 | 
			
		||||
        &mut bos_descriptor,
 | 
			
		||||
        &mut control_buf,
 | 
			
		||||
        None,
 | 
			
		||||
    );
 | 
			
		||||
 | 
			
		||||
    // Create classes on the builder.
 | 
			
		||||
    let mut class = CdcAcmClass::new(&mut builder, &mut state, 64);
 | 
			
		||||
 | 
			
		||||
    // Build the builder.
 | 
			
		||||
    let mut usb = builder.build();
 | 
			
		||||
 | 
			
		||||
    // Run the USB device.
 | 
			
		||||
    let usb_fut = usb.run();
 | 
			
		||||
 | 
			
		||||
    // Do stuff with the class!
 | 
			
		||||
    let echo_fut = async {
 | 
			
		||||
        loop {
 | 
			
		||||
            class.wait_connection().await;
 | 
			
		||||
            info!("Connected");
 | 
			
		||||
            let _ = echo(&mut class).await;
 | 
			
		||||
            info!("Disconnected");
 | 
			
		||||
        }
 | 
			
		||||
    };
 | 
			
		||||
 | 
			
		||||
    // Run everything concurrently.
 | 
			
		||||
    // If we had made everything `'static` above instead, we could do this using separate tasks instead.
 | 
			
		||||
    join(usb_fut, echo_fut).await;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
struct Disconnected {}
 | 
			
		||||
 | 
			
		||||
impl From<EndpointError> for Disconnected {
 | 
			
		||||
    fn from(val: EndpointError) -> Self {
 | 
			
		||||
        match val {
 | 
			
		||||
            EndpointError::BufferOverflow => panic!("Buffer overflow"),
 | 
			
		||||
            EndpointError::Disabled => Disconnected {},
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
async fn echo<'d, T: Instance + 'd>(class: &mut CdcAcmClass<'d, Driver<'d, T>>) -> Result<(), Disconnected> {
 | 
			
		||||
    let mut buf = [0; 64];
 | 
			
		||||
    loop {
 | 
			
		||||
        let n = class.read_packet(&mut buf).await?;
 | 
			
		||||
        let data = &buf[..n];
 | 
			
		||||
        info!("data: {:x}", data);
 | 
			
		||||
        class.write_packet(data).await?;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@@ -11,6 +11,7 @@ embassy-time = { version = "0.1.0", path = "../../embassy-time", features = ["de
 | 
			
		||||
embassy-stm32 = { version = "0.1.0", path = "../../embassy-stm32", features = ["nightly", "defmt", "stm32h743bi", "time-driver-any", "exti", "unstable-pac", "unstable-traits"] }
 | 
			
		||||
embassy-net = { path = "../../embassy-net", features = ["defmt", "nightly", "tcp", "dhcpv4", "medium-ethernet", "unstable-traits"] }
 | 
			
		||||
embedded-io = { version = "0.4.0", features = ["async"] }
 | 
			
		||||
embassy-usb = { version = "0.1.0", path = "../../embassy-usb", features = ["defmt"] }
 | 
			
		||||
 | 
			
		||||
defmt = "0.3"
 | 
			
		||||
defmt-rtt = "0.4"
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										106
									
								
								examples/stm32h7/src/bin/usb_serial.rs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										106
									
								
								examples/stm32h7/src/bin/usb_serial.rs
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,106 @@
 | 
			
		||||
#![no_std]
 | 
			
		||||
#![no_main]
 | 
			
		||||
#![feature(type_alias_impl_trait)]
 | 
			
		||||
 | 
			
		||||
use defmt::{panic, *};
 | 
			
		||||
use embassy_executor::Spawner;
 | 
			
		||||
use embassy_stm32::time::mhz;
 | 
			
		||||
use embassy_stm32::usb_otg::{Driver, Instance};
 | 
			
		||||
use embassy_stm32::{interrupt, Config};
 | 
			
		||||
use embassy_usb::class::cdc_acm::{CdcAcmClass, State};
 | 
			
		||||
use embassy_usb::driver::EndpointError;
 | 
			
		||||
use embassy_usb::Builder;
 | 
			
		||||
use futures::future::join;
 | 
			
		||||
use {defmt_rtt as _, panic_probe as _};
 | 
			
		||||
 | 
			
		||||
#[embassy_executor::main]
 | 
			
		||||
async fn main(_spawner: Spawner) {
 | 
			
		||||
    info!("Hello World!");
 | 
			
		||||
 | 
			
		||||
    let mut config = Config::default();
 | 
			
		||||
    config.rcc.sys_ck = Some(mhz(400));
 | 
			
		||||
    config.rcc.hclk = Some(mhz(200));
 | 
			
		||||
    config.rcc.pll1.q_ck = Some(mhz(100));
 | 
			
		||||
    let p = embassy_stm32::init(config);
 | 
			
		||||
 | 
			
		||||
    // Create the driver, from the HAL.
 | 
			
		||||
    let irq = interrupt::take!(OTG_FS);
 | 
			
		||||
    let mut ep_out_buffer = [0u8; 256];
 | 
			
		||||
    let driver = Driver::new_fs(p.USB_OTG_FS, irq, p.PA12, p.PA11, &mut ep_out_buffer);
 | 
			
		||||
 | 
			
		||||
    // Create embassy-usb Config
 | 
			
		||||
    let mut config = embassy_usb::Config::new(0xc0de, 0xcafe);
 | 
			
		||||
    config.manufacturer = Some("Embassy");
 | 
			
		||||
    config.product = Some("USB-serial example");
 | 
			
		||||
    config.serial_number = Some("12345678");
 | 
			
		||||
 | 
			
		||||
    // Required for windows compatiblity.
 | 
			
		||||
    // https://developer.nordicsemi.com/nRF_Connect_SDK/doc/1.9.1/kconfig/CONFIG_CDC_ACM_IAD.html#help
 | 
			
		||||
    config.device_class = 0xEF;
 | 
			
		||||
    config.device_sub_class = 0x02;
 | 
			
		||||
    config.device_protocol = 0x01;
 | 
			
		||||
    config.composite_with_iads = true;
 | 
			
		||||
 | 
			
		||||
    // Create embassy-usb DeviceBuilder using the driver and config.
 | 
			
		||||
    // It needs some buffers for building the descriptors.
 | 
			
		||||
    let mut device_descriptor = [0; 256];
 | 
			
		||||
    let mut config_descriptor = [0; 256];
 | 
			
		||||
    let mut bos_descriptor = [0; 256];
 | 
			
		||||
    let mut control_buf = [0; 64];
 | 
			
		||||
 | 
			
		||||
    let mut state = State::new();
 | 
			
		||||
 | 
			
		||||
    let mut builder = Builder::new(
 | 
			
		||||
        driver,
 | 
			
		||||
        config,
 | 
			
		||||
        &mut device_descriptor,
 | 
			
		||||
        &mut config_descriptor,
 | 
			
		||||
        &mut bos_descriptor,
 | 
			
		||||
        &mut control_buf,
 | 
			
		||||
        None,
 | 
			
		||||
    );
 | 
			
		||||
 | 
			
		||||
    // Create classes on the builder.
 | 
			
		||||
    let mut class = CdcAcmClass::new(&mut builder, &mut state, 64);
 | 
			
		||||
 | 
			
		||||
    // Build the builder.
 | 
			
		||||
    let mut usb = builder.build();
 | 
			
		||||
 | 
			
		||||
    // Run the USB device.
 | 
			
		||||
    let usb_fut = usb.run();
 | 
			
		||||
 | 
			
		||||
    // Do stuff with the class!
 | 
			
		||||
    let echo_fut = async {
 | 
			
		||||
        loop {
 | 
			
		||||
            class.wait_connection().await;
 | 
			
		||||
            info!("Connected");
 | 
			
		||||
            let _ = echo(&mut class).await;
 | 
			
		||||
            info!("Disconnected");
 | 
			
		||||
        }
 | 
			
		||||
    };
 | 
			
		||||
 | 
			
		||||
    // Run everything concurrently.
 | 
			
		||||
    // If we had made everything `'static` above instead, we could do this using separate tasks instead.
 | 
			
		||||
    join(usb_fut, echo_fut).await;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
struct Disconnected {}
 | 
			
		||||
 | 
			
		||||
impl From<EndpointError> for Disconnected {
 | 
			
		||||
    fn from(val: EndpointError) -> Self {
 | 
			
		||||
        match val {
 | 
			
		||||
            EndpointError::BufferOverflow => panic!("Buffer overflow"),
 | 
			
		||||
            EndpointError::Disabled => Disconnected {},
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
async fn echo<'d, T: Instance + 'd>(class: &mut CdcAcmClass<'d, Driver<'d, T>>) -> Result<(), Disconnected> {
 | 
			
		||||
    let mut buf = [0; 64];
 | 
			
		||||
    loop {
 | 
			
		||||
        let n = class.read_packet(&mut buf).await?;
 | 
			
		||||
        let data = &buf[..n];
 | 
			
		||||
        info!("data: {:x}", data);
 | 
			
		||||
        class.write_packet(data).await?;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@@ -12,6 +12,7 @@ embassy-executor = { version = "0.1.0", path = "../../embassy-executor", feature
 | 
			
		||||
embassy-time = { version = "0.1.0", path = "../../embassy-time", features = ["defmt", "defmt-timestamp-uptime", "tick-hz-32_768"] }
 | 
			
		||||
embassy-embedded-hal = { version = "0.1.0", path = "../../embassy-embedded-hal" }
 | 
			
		||||
embassy-stm32 = { version = "0.1.0", path = "../../embassy-stm32", features = ["nightly", "defmt", "unstable-pac", "stm32l4s5vi", "time-driver-any", "exti", "unstable-traits"]  }
 | 
			
		||||
embassy-usb = { version = "0.1.0", path = "../../embassy-usb", features = ["defmt"] }
 | 
			
		||||
 | 
			
		||||
defmt = "0.3"
 | 
			
		||||
defmt-rtt = "0.4"
 | 
			
		||||
@@ -26,6 +27,3 @@ futures = { version = "0.3.17", default-features = false, features = ["async-awa
 | 
			
		||||
heapless = { version = "0.7.5", default-features = false }
 | 
			
		||||
 | 
			
		||||
micromath = "2.0.0"
 | 
			
		||||
usb-device = "0.2"
 | 
			
		||||
usbd-serial = "0.1.1"
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										108
									
								
								examples/stm32l4/src/bin/usb_serial.rs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										108
									
								
								examples/stm32l4/src/bin/usb_serial.rs
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,108 @@
 | 
			
		||||
#![no_std]
 | 
			
		||||
#![no_main]
 | 
			
		||||
#![feature(type_alias_impl_trait)]
 | 
			
		||||
 | 
			
		||||
use defmt::{panic, *};
 | 
			
		||||
use defmt_rtt as _; // global logger
 | 
			
		||||
use embassy_executor::Spawner;
 | 
			
		||||
use embassy_stm32::rcc::*;
 | 
			
		||||
use embassy_stm32::usb_otg::{Driver, Instance};
 | 
			
		||||
use embassy_stm32::{interrupt, Config};
 | 
			
		||||
use embassy_usb::class::cdc_acm::{CdcAcmClass, State};
 | 
			
		||||
use embassy_usb::driver::EndpointError;
 | 
			
		||||
use embassy_usb::Builder;
 | 
			
		||||
use futures::future::join;
 | 
			
		||||
use panic_probe as _;
 | 
			
		||||
 | 
			
		||||
#[embassy_executor::main]
 | 
			
		||||
async fn main(_spawner: Spawner) {
 | 
			
		||||
    info!("Hello World!");
 | 
			
		||||
 | 
			
		||||
    let mut config = Config::default();
 | 
			
		||||
    config.rcc.mux = ClockSrc::PLL(PLLSource::HSI16, PLLClkDiv::Div2, PLLSrcDiv::Div1, PLLMul::Mul10, None);
 | 
			
		||||
    config.rcc.hsi48 = true;
 | 
			
		||||
 | 
			
		||||
    let p = embassy_stm32::init(config);
 | 
			
		||||
 | 
			
		||||
    // Create the driver, from the HAL.
 | 
			
		||||
    let irq = interrupt::take!(OTG_FS);
 | 
			
		||||
    let mut ep_out_buffer = [0u8; 256];
 | 
			
		||||
    let driver = Driver::new_fs(p.USB_OTG_FS, irq, p.PA12, p.PA11, &mut ep_out_buffer);
 | 
			
		||||
 | 
			
		||||
    // Create embassy-usb Config
 | 
			
		||||
    let mut config = embassy_usb::Config::new(0xc0de, 0xcafe);
 | 
			
		||||
    config.max_packet_size_0 = 64;
 | 
			
		||||
    config.manufacturer = Some("Embassy");
 | 
			
		||||
    config.product = Some("USB-serial example");
 | 
			
		||||
    config.serial_number = Some("12345678");
 | 
			
		||||
 | 
			
		||||
    // Required for windows compatiblity.
 | 
			
		||||
    // https://developer.nordicsemi.com/nRF_Connect_SDK/doc/1.9.1/kconfig/CONFIG_CDC_ACM_IAD.html#help
 | 
			
		||||
    config.device_class = 0xEF;
 | 
			
		||||
    config.device_sub_class = 0x02;
 | 
			
		||||
    config.device_protocol = 0x01;
 | 
			
		||||
    config.composite_with_iads = true;
 | 
			
		||||
 | 
			
		||||
    // Create embassy-usb DeviceBuilder using the driver and config.
 | 
			
		||||
    // It needs some buffers for building the descriptors.
 | 
			
		||||
    let mut device_descriptor = [0; 256];
 | 
			
		||||
    let mut config_descriptor = [0; 256];
 | 
			
		||||
    let mut bos_descriptor = [0; 256];
 | 
			
		||||
    let mut control_buf = [0; 64];
 | 
			
		||||
 | 
			
		||||
    let mut state = State::new();
 | 
			
		||||
 | 
			
		||||
    let mut builder = Builder::new(
 | 
			
		||||
        driver,
 | 
			
		||||
        config,
 | 
			
		||||
        &mut device_descriptor,
 | 
			
		||||
        &mut config_descriptor,
 | 
			
		||||
        &mut bos_descriptor,
 | 
			
		||||
        &mut control_buf,
 | 
			
		||||
        None,
 | 
			
		||||
    );
 | 
			
		||||
 | 
			
		||||
    // Create classes on the builder.
 | 
			
		||||
    let mut class = CdcAcmClass::new(&mut builder, &mut state, 64);
 | 
			
		||||
 | 
			
		||||
    // Build the builder.
 | 
			
		||||
    let mut usb = builder.build();
 | 
			
		||||
 | 
			
		||||
    // Run the USB device.
 | 
			
		||||
    let usb_fut = usb.run();
 | 
			
		||||
 | 
			
		||||
    // Do stuff with the class!
 | 
			
		||||
    let echo_fut = async {
 | 
			
		||||
        loop {
 | 
			
		||||
            class.wait_connection().await;
 | 
			
		||||
            info!("Connected");
 | 
			
		||||
            let _ = echo(&mut class).await;
 | 
			
		||||
            info!("Disconnected");
 | 
			
		||||
        }
 | 
			
		||||
    };
 | 
			
		||||
 | 
			
		||||
    // Run everything concurrently.
 | 
			
		||||
    // If we had made everything `'static` above instead, we could do this using separate tasks instead.
 | 
			
		||||
    join(usb_fut, echo_fut).await;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
struct Disconnected {}
 | 
			
		||||
 | 
			
		||||
impl From<EndpointError> for Disconnected {
 | 
			
		||||
    fn from(val: EndpointError) -> Self {
 | 
			
		||||
        match val {
 | 
			
		||||
            EndpointError::BufferOverflow => panic!("Buffer overflow"),
 | 
			
		||||
            EndpointError::Disabled => Disconnected {},
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
async fn echo<'d, T: Instance + 'd>(class: &mut CdcAcmClass<'d, Driver<'d, T>>) -> Result<(), Disconnected> {
 | 
			
		||||
    let mut buf = [0; 64];
 | 
			
		||||
    loop {
 | 
			
		||||
        let n = class.read_packet(&mut buf).await?;
 | 
			
		||||
        let data = &buf[..n];
 | 
			
		||||
        info!("data: {:x}", data);
 | 
			
		||||
        class.write_packet(data).await?;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@@ -9,6 +9,7 @@ embassy-sync = { version = "0.1.0", path = "../../embassy-sync", features = ["de
 | 
			
		||||
embassy-executor = { version = "0.1.0", path = "../../embassy-executor", features = ["defmt", "integrated-timers"] }
 | 
			
		||||
embassy-time = { version = "0.1.0", path = "../../embassy-time", features = ["defmt", "defmt-timestamp-uptime", "tick-hz-32_768"] }
 | 
			
		||||
embassy-stm32 = { version = "0.1.0", path = "../../embassy-stm32", features = ["nightly", "defmt", "unstable-pac", "stm32u585ai", "time-driver-any", "memory-x" ]  }
 | 
			
		||||
embassy-usb = { version = "0.1.0", path = "../../embassy-usb", features = ["defmt"] }
 | 
			
		||||
 | 
			
		||||
defmt = "0.3"
 | 
			
		||||
defmt-rtt = "0.4"
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										108
									
								
								examples/stm32u5/src/bin/usb_serial.rs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										108
									
								
								examples/stm32u5/src/bin/usb_serial.rs
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,108 @@
 | 
			
		||||
#![no_std]
 | 
			
		||||
#![no_main]
 | 
			
		||||
#![feature(type_alias_impl_trait)]
 | 
			
		||||
 | 
			
		||||
use defmt::{panic, *};
 | 
			
		||||
use defmt_rtt as _; // global logger
 | 
			
		||||
use embassy_executor::Spawner;
 | 
			
		||||
use embassy_stm32::rcc::*;
 | 
			
		||||
use embassy_stm32::usb_otg::{Driver, Instance};
 | 
			
		||||
use embassy_stm32::{interrupt, Config};
 | 
			
		||||
use embassy_usb::class::cdc_acm::{CdcAcmClass, State};
 | 
			
		||||
use embassy_usb::driver::EndpointError;
 | 
			
		||||
use embassy_usb::Builder;
 | 
			
		||||
use futures::future::join;
 | 
			
		||||
use panic_probe as _;
 | 
			
		||||
 | 
			
		||||
#[embassy_executor::main]
 | 
			
		||||
async fn main(_spawner: Spawner) {
 | 
			
		||||
    info!("Hello World!");
 | 
			
		||||
 | 
			
		||||
    let mut config = Config::default();
 | 
			
		||||
    config.rcc.mux = ClockSrc::PLL1R(PllSrc::HSI16, PllM::Div2, PllN::Mul10, PllClkDiv::NotDivided);
 | 
			
		||||
    //config.rcc.mux = ClockSrc::MSI(MSIRange::Range48mhz);
 | 
			
		||||
    config.rcc.hsi48 = true;
 | 
			
		||||
 | 
			
		||||
    let p = embassy_stm32::init(config);
 | 
			
		||||
 | 
			
		||||
    // Create the driver, from the HAL.
 | 
			
		||||
    let irq = interrupt::take!(OTG_FS);
 | 
			
		||||
    let mut ep_out_buffer = [0u8; 256];
 | 
			
		||||
    let driver = Driver::new_fs(p.USB_OTG_FS, irq, p.PA12, p.PA11, &mut ep_out_buffer);
 | 
			
		||||
 | 
			
		||||
    // Create embassy-usb Config
 | 
			
		||||
    let mut config = embassy_usb::Config::new(0xc0de, 0xcafe);
 | 
			
		||||
    config.manufacturer = Some("Embassy");
 | 
			
		||||
    config.product = Some("USB-serial example");
 | 
			
		||||
    config.serial_number = Some("12345678");
 | 
			
		||||
 | 
			
		||||
    // Required for windows compatiblity.
 | 
			
		||||
    // https://developer.nordicsemi.com/nRF_Connect_SDK/doc/1.9.1/kconfig/CONFIG_CDC_ACM_IAD.html#help
 | 
			
		||||
    config.device_class = 0xEF;
 | 
			
		||||
    config.device_sub_class = 0x02;
 | 
			
		||||
    config.device_protocol = 0x01;
 | 
			
		||||
    config.composite_with_iads = true;
 | 
			
		||||
 | 
			
		||||
    // Create embassy-usb DeviceBuilder using the driver and config.
 | 
			
		||||
    // It needs some buffers for building the descriptors.
 | 
			
		||||
    let mut device_descriptor = [0; 256];
 | 
			
		||||
    let mut config_descriptor = [0; 256];
 | 
			
		||||
    let mut bos_descriptor = [0; 256];
 | 
			
		||||
    let mut control_buf = [0; 64];
 | 
			
		||||
 | 
			
		||||
    let mut state = State::new();
 | 
			
		||||
 | 
			
		||||
    let mut builder = Builder::new(
 | 
			
		||||
        driver,
 | 
			
		||||
        config,
 | 
			
		||||
        &mut device_descriptor,
 | 
			
		||||
        &mut config_descriptor,
 | 
			
		||||
        &mut bos_descriptor,
 | 
			
		||||
        &mut control_buf,
 | 
			
		||||
        None,
 | 
			
		||||
    );
 | 
			
		||||
 | 
			
		||||
    // Create classes on the builder.
 | 
			
		||||
    let mut class = CdcAcmClass::new(&mut builder, &mut state, 64);
 | 
			
		||||
 | 
			
		||||
    // Build the builder.
 | 
			
		||||
    let mut usb = builder.build();
 | 
			
		||||
 | 
			
		||||
    // Run the USB device.
 | 
			
		||||
    let usb_fut = usb.run();
 | 
			
		||||
 | 
			
		||||
    // Do stuff with the class!
 | 
			
		||||
    let echo_fut = async {
 | 
			
		||||
        loop {
 | 
			
		||||
            class.wait_connection().await;
 | 
			
		||||
            info!("Connected");
 | 
			
		||||
            let _ = echo(&mut class).await;
 | 
			
		||||
            info!("Disconnected");
 | 
			
		||||
        }
 | 
			
		||||
    };
 | 
			
		||||
 | 
			
		||||
    // Run everything concurrently.
 | 
			
		||||
    // If we had made everything `'static` above instead, we could do this using separate tasks instead.
 | 
			
		||||
    join(usb_fut, echo_fut).await;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
struct Disconnected {}
 | 
			
		||||
 | 
			
		||||
impl From<EndpointError> for Disconnected {
 | 
			
		||||
    fn from(val: EndpointError) -> Self {
 | 
			
		||||
        match val {
 | 
			
		||||
            EndpointError::BufferOverflow => panic!("Buffer overflow"),
 | 
			
		||||
            EndpointError::Disabled => Disconnected {},
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
async fn echo<'d, T: Instance + 'd>(class: &mut CdcAcmClass<'d, Driver<'d, T>>) -> Result<(), Disconnected> {
 | 
			
		||||
    let mut buf = [0; 64];
 | 
			
		||||
    loop {
 | 
			
		||||
        let n = class.read_packet(&mut buf).await?;
 | 
			
		||||
        let data = &buf[..n];
 | 
			
		||||
        info!("data: {:x}", data);
 | 
			
		||||
        class.write_packet(data).await?;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 Submodule stm32-data updated: 14a448c318...844793fc3d
									
								
							
		Reference in New Issue
	
	Block a user