diff options
| author | Dario Nieuwenhuis <[email protected]> | 2024-03-19 21:32:22 +0100 |
|---|---|---|
| committer | Dario Nieuwenhuis <[email protected]> | 2024-03-19 22:10:59 +0100 |
| commit | daa64bd5400c4db7da25e3f538b4ee0c31435029 (patch) | |
| tree | 3489eab5223894f1c698c04c3b995debcda30eb5 | |
| parent | 530ff9d4d33abff34edb718d650c71a41dd68f8c (diff) | |
stm32/usb: extract common init code.
| -rw-r--r-- | embassy-stm32/src/usb/mod.rs | 50 | ||||
| -rw-r--r-- | embassy-stm32/src/usb/otg.rs | 38 | ||||
| -rw-r--r-- | embassy-stm32/src/usb/usb.rs | 13 |
3 files changed, 53 insertions, 48 deletions
diff --git a/embassy-stm32/src/usb/mod.rs b/embassy-stm32/src/usb/mod.rs index 974880900..130c728e6 100644 --- a/embassy-stm32/src/usb/mod.rs +++ b/embassy-stm32/src/usb/mod.rs | |||
| @@ -4,3 +4,53 @@ | |||
| 4 | #[cfg_attr(otg, path = "otg.rs")] | 4 | #[cfg_attr(otg, path = "otg.rs")] |
| 5 | mod _version; | 5 | mod _version; |
| 6 | pub use _version::*; | 6 | pub use _version::*; |
| 7 | |||
| 8 | use crate::interrupt::typelevel::Interrupt; | ||
| 9 | use crate::rcc::sealed::RccPeripheral; | ||
| 10 | |||
| 11 | /// clock, power initialization stuff that's common for USB and OTG. | ||
| 12 | fn common_init<T: Instance>() { | ||
| 13 | #[cfg(any(stm32l4, stm32l5, stm32wb))] | ||
| 14 | critical_section::with(|_| crate::pac::PWR.cr2().modify(|w| w.set_usv(true))); | ||
| 15 | |||
| 16 | #[cfg(pwr_h5)] | ||
| 17 | critical_section::with(|_| crate::pac::PWR.usbscr().modify(|w| w.set_usb33sv(true))); | ||
| 18 | |||
| 19 | #[cfg(stm32h7)] | ||
| 20 | { | ||
| 21 | // If true, VDD33USB is generated by internal regulator from VDD50USB | ||
| 22 | // If false, VDD33USB and VDD50USB must be suplied directly with 3.3V (default on nucleo) | ||
| 23 | // TODO: unhardcode | ||
| 24 | let internal_regulator = false; | ||
| 25 | |||
| 26 | // Enable USB power | ||
| 27 | critical_section::with(|_| { | ||
| 28 | crate::pac::PWR.cr3().modify(|w| { | ||
| 29 | w.set_usb33den(true); | ||
| 30 | w.set_usbregen(internal_regulator); | ||
| 31 | }) | ||
| 32 | }); | ||
| 33 | |||
| 34 | // Wait for USB power to stabilize | ||
| 35 | while !crate::pac::PWR.cr3().read().usb33rdy() {} | ||
| 36 | } | ||
| 37 | |||
| 38 | #[cfg(stm32u5)] | ||
| 39 | { | ||
| 40 | // Enable USB power | ||
| 41 | critical_section::with(|_| { | ||
| 42 | crate::pac::PWR.svmcr().modify(|w| { | ||
| 43 | w.set_usv(true); | ||
| 44 | w.set_uvmen(true); | ||
| 45 | }) | ||
| 46 | }); | ||
| 47 | |||
| 48 | // Wait for USB power to stabilize | ||
| 49 | while !crate::pac::PWR.svmsr().read().vddusbrdy() {} | ||
| 50 | } | ||
| 51 | |||
| 52 | T::Interrupt::unpend(); | ||
| 53 | unsafe { T::Interrupt::enable() }; | ||
| 54 | |||
| 55 | <T as RccPeripheral>::enable_and_reset(); | ||
| 56 | } | ||
diff --git a/embassy-stm32/src/usb/otg.rs b/embassy-stm32/src/usb/otg.rs index d15d929f6..80a08f3c5 100644 --- a/embassy-stm32/src/usb/otg.rs +++ b/embassy-stm32/src/usb/otg.rs | |||
| @@ -560,8 +560,7 @@ impl<'d, T: Instance> Bus<'d, T> { | |||
| 560 | 560 | ||
| 561 | impl<'d, T: Instance> Bus<'d, T> { | 561 | impl<'d, T: Instance> Bus<'d, T> { |
| 562 | fn init(&mut self) { | 562 | fn init(&mut self) { |
| 563 | #[cfg(stm32l4)] | 563 | super::common_init::<T>(); |
| 564 | critical_section::with(|_| crate::pac::PWR.cr2().modify(|w| w.set_usv(true))); | ||
| 565 | 564 | ||
| 566 | #[cfg(stm32f7)] | 565 | #[cfg(stm32f7)] |
| 567 | { | 566 | { |
| @@ -589,22 +588,6 @@ impl<'d, T: Instance> Bus<'d, T> { | |||
| 589 | 588 | ||
| 590 | #[cfg(stm32h7)] | 589 | #[cfg(stm32h7)] |
| 591 | { | 590 | { |
| 592 | // If true, VDD33USB is generated by internal regulator from VDD50USB | ||
| 593 | // If false, VDD33USB and VDD50USB must be suplied directly with 3.3V (default on nucleo) | ||
| 594 | // TODO: unhardcode | ||
| 595 | let internal_regulator = false; | ||
| 596 | |||
| 597 | // Enable USB power | ||
| 598 | critical_section::with(|_| { | ||
| 599 | crate::pac::PWR.cr3().modify(|w| { | ||
| 600 | w.set_usb33den(true); | ||
| 601 | w.set_usbregen(internal_regulator); | ||
| 602 | }) | ||
| 603 | }); | ||
| 604 | |||
| 605 | // Wait for USB power to stabilize | ||
| 606 | while !crate::pac::PWR.cr3().read().usb33rdy() {} | ||
| 607 | |||
| 608 | // Enable ULPI clock if external PHY is used | 591 | // Enable ULPI clock if external PHY is used |
| 609 | let ulpien = !self.phy_type.internal(); | 592 | let ulpien = !self.phy_type.internal(); |
| 610 | critical_section::with(|_| { | 593 | critical_section::with(|_| { |
| @@ -625,25 +608,6 @@ impl<'d, T: Instance> Bus<'d, T> { | |||
| 625 | }); | 608 | }); |
| 626 | } | 609 | } |
| 627 | 610 | ||
| 628 | #[cfg(stm32u5)] | ||
| 629 | { | ||
| 630 | // Enable USB power | ||
| 631 | critical_section::with(|_| { | ||
| 632 | crate::pac::PWR.svmcr().modify(|w| { | ||
| 633 | w.set_usv(true); | ||
| 634 | w.set_uvmen(true); | ||
| 635 | }) | ||
| 636 | }); | ||
| 637 | |||
| 638 | // Wait for USB power to stabilize | ||
| 639 | while !crate::pac::PWR.svmsr().read().vddusbrdy() {} | ||
| 640 | } | ||
| 641 | |||
| 642 | <T as RccPeripheral>::enable_and_reset(); | ||
| 643 | |||
| 644 | T::Interrupt::unpend(); | ||
| 645 | unsafe { T::Interrupt::enable() }; | ||
| 646 | |||
| 647 | let r = T::regs(); | 611 | let r = T::regs(); |
| 648 | let core_id = r.cid().read().0; | 612 | let core_id = r.cid().read().0; |
| 649 | trace!("Core id {:08x}", core_id); | 613 | trace!("Core id {:08x}", core_id); |
diff --git a/embassy-stm32/src/usb/usb.rs b/embassy-stm32/src/usb/usb.rs index b3b3470e4..1fb2c9ebb 100644 --- a/embassy-stm32/src/usb/usb.rs +++ b/embassy-stm32/src/usb/usb.rs | |||
| @@ -12,7 +12,6 @@ use embassy_usb_driver::{ | |||
| 12 | Direction, EndpointAddress, EndpointAllocError, EndpointError, EndpointInfo, EndpointType, Event, Unsupported, | 12 | Direction, EndpointAddress, EndpointAllocError, EndpointError, EndpointInfo, EndpointType, Event, Unsupported, |
| 13 | }; | 13 | }; |
| 14 | 14 | ||
| 15 | use crate::interrupt::typelevel::Interrupt; | ||
| 16 | use crate::pac::usb::regs; | 15 | use crate::pac::usb::regs; |
| 17 | use crate::pac::usb::vals::{EpType, Stat}; | 16 | use crate::pac::usb::vals::{EpType, Stat}; |
| 18 | use crate::pac::USBRAM; | 17 | use crate::pac::USBRAM; |
| @@ -258,18 +257,10 @@ impl<'d, T: Instance> Driver<'d, T> { | |||
| 258 | dm: impl Peripheral<P = impl DmPin<T>> + 'd, | 257 | dm: impl Peripheral<P = impl DmPin<T>> + 'd, |
| 259 | ) -> Self { | 258 | ) -> Self { |
| 260 | into_ref!(dp, dm); | 259 | into_ref!(dp, dm); |
| 261 | T::Interrupt::unpend(); | ||
| 262 | unsafe { T::Interrupt::enable() }; | ||
| 263 | 260 | ||
| 264 | let regs = T::regs(); | 261 | super::common_init::<T>(); |
| 265 | |||
| 266 | #[cfg(any(stm32l4, stm32l5, stm32wb))] | ||
| 267 | crate::pac::PWR.cr2().modify(|w| w.set_usv(true)); | ||
| 268 | 262 | ||
| 269 | #[cfg(pwr_h5)] | 263 | let regs = T::regs(); |
| 270 | crate::pac::PWR.usbscr().modify(|w| w.set_usb33sv(true)); | ||
| 271 | |||
| 272 | <T as RccPeripheral>::enable_and_reset(); | ||
| 273 | 264 | ||
| 274 | regs.cntr().write(|w| { | 265 | regs.cntr().write(|w| { |
| 275 | w.set_pdwn(false); | 266 | w.set_pdwn(false); |
