aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--embassy-stm32/src/lib.rs6
-rw-r--r--embassy-stm32/src/usb/mod.rs4
-rw-r--r--embassy-stm32/src/usb/otg.rs7
-rw-r--r--examples/stm32wba/src/bin/usb_hs_serial.rs25
-rw-r--r--examples/stm32wba/src/bin/usb_serial.rs119
5 files changed, 12 insertions, 149 deletions
diff --git a/embassy-stm32/src/lib.rs b/embassy-stm32/src/lib.rs
index d893f1b54..a676677e1 100644
--- a/embassy-stm32/src/lib.rs
+++ b/embassy-stm32/src/lib.rs
@@ -546,7 +546,11 @@ fn init_hw(config: Config) -> Peripherals {
546 { 546 {
547 use crate::pac::pwr::vals; 547 use crate::pac::pwr::vals;
548 crate::pac::PWR.svmcr().modify(|w| { 548 crate::pac::PWR.svmcr().modify(|w| {
549 w.set_io2sv(if config.enable_independent_io_supply {vals::Io2sv::B_0X1} else {vals::Io2sv::B_0X0}); 549 w.set_io2sv(if config.enable_independent_io_supply {
550 vals::Io2sv::B_0X1
551 } else {
552 vals::Io2sv::B_0X0
553 });
550 }); 554 });
551 } 555 }
552 #[cfg(stm32u5)] 556 #[cfg(stm32u5)]
diff --git a/embassy-stm32/src/usb/mod.rs b/embassy-stm32/src/usb/mod.rs
index d052934f8..62c751ac8 100644
--- a/embassy-stm32/src/usb/mod.rs
+++ b/embassy-stm32/src/usb/mod.rs
@@ -111,10 +111,8 @@ fn common_init<T: Instance>() {
111 }); 111 });
112 crate::pac::PWR.vosr().modify(|w| { 112 crate::pac::PWR.vosr().modify(|w| {
113 w.set_vdd11usbdis(false); 113 w.set_vdd11usbdis(false);
114 });
115 crate::pac::PWR.vosr().modify(|w| {
116 w.set_usbpwren(true); 114 w.set_usbpwren(true);
117 }) 115 });
118 }); 116 });
119 117
120 // Wait for USB power to stabilize 118 // Wait for USB power to stabilize
diff --git a/embassy-stm32/src/usb/otg.rs b/embassy-stm32/src/usb/otg.rs
index abf54cbad..1c3b99b93 100644
--- a/embassy-stm32/src/usb/otg.rs
+++ b/embassy-stm32/src/usb/otg.rs
@@ -330,19 +330,12 @@ impl<'d, T: Instance> Bus<'d, T> {
330 #[cfg(all(stm32wba, peri_usb_otg_hs))] 330 #[cfg(all(stm32wba, peri_usb_otg_hs))]
331 { 331 {
332 critical_section::with(|_| { 332 critical_section::with(|_| {
333 // crate::pac::RCC.apb7enr().modify(|w| {
334 // w.set_syscfgen(true);
335 // });
336 crate::pac::RCC.ahb2enr().modify(|w| { 333 crate::pac::RCC.ahb2enr().modify(|w| {
337 w.set_usb_otg_hsen(true); 334 w.set_usb_otg_hsen(true);
338 w.set_usb_otg_hs_phyen(true); 335 w.set_usb_otg_hs_phyen(true);
339 }); 336 });
340 }); 337 });
341 338
342 // pub use crate::pac::rcc::vals::Otghssel;
343 // // select HSE
344 // crate::pac::RCC.ccipr2().modify(|w| {w.set_otghssel(Otghssel::HSE);});
345
346 crate::pac::SYSCFG.otghsphytuner2().modify(|w| { 339 crate::pac::SYSCFG.otghsphytuner2().modify(|w| {
347 w.set_compdistune(0b010); 340 w.set_compdistune(0b010);
348 w.set_sqrxtune(0b000); 341 w.set_sqrxtune(0b000);
diff --git a/examples/stm32wba/src/bin/usb_hs_serial.rs b/examples/stm32wba/src/bin/usb_hs_serial.rs
index 2e17e52d1..20bdeaac3 100644
--- a/examples/stm32wba/src/bin/usb_hs_serial.rs
+++ b/examples/stm32wba/src/bin/usb_hs_serial.rs
@@ -21,24 +21,16 @@ async fn main(_spawner: Spawner) {
21 21
22 let mut config = Config::default(); 22 let mut config = Config::default();
23 23
24
25 { 24 {
26 use embassy_stm32::rcc::*; 25 use embassy_stm32::rcc::*;
27 // External HSE (32 MHz) setup
28 // config.rcc.hse = Some(Hse {
29 // prescaler: HsePrescaler::DIV2,
30 // });
31
32 // Fine-tune PLL1 dividers/multipliers
33 config.rcc.pll1 = Some(Pll { 26 config.rcc.pll1 = Some(Pll {
34 source: PllSource::HSI, 27 source: PllSource::HSI,
35 prediv: PllPreDiv::DIV1, // PLLM = 1 → HSI / 1 = 16 MHz 28 prediv: PllPreDiv::DIV1, // PLLM = 1 → HSI / 1 = 16 MHz
36 mul: PllMul::MUL30, // PLLN = 30 → 16 MHz * 30 = 480 MHz VCO 29 mul: PllMul::MUL30, // PLLN = 30 → 16 MHz * 30 = 480 MHz VCO
37 divr: Some(PllDiv::DIV5), // PLLR = 5 → 96 MHz (Sysclk) 30 divr: Some(PllDiv::DIV5), // PLLR = 5 → 96 MHz (Sysclk)
38 divq: Some(PllDiv::DIV10), // PLLQ = 10 → 48 MHz (NOT USED) 31 divq: Some(PllDiv::DIV10), // PLLQ = 10 → 48 MHz
39 // divq: None, 32 divp: Some(PllDiv::DIV30), // PLLP = 30 → 16 MHz (USB_OTG_HS)
40 divp: Some(PllDiv::DIV30), // PLLP = 30 → 16 MHz (USBOTG) 33 frac: Some(0), // Fractional part (disabled)
41 frac: Some(0), // Fractional part (enabled)
42 }); 34 });
43 35
44 config.rcc.ahb_pre = AHBPrescaler::DIV1; 36 config.rcc.ahb_pre = AHBPrescaler::DIV1;
@@ -54,11 +46,6 @@ async fn main(_spawner: Spawner) {
54 46
55 let p = embassy_stm32::init(config); 47 let p = embassy_stm32::init(config);
56 48
57 // TRDT set to 5
58 // ASVLD set to 1
59 // BSVLD set to 1
60
61
62 // Create the driver, from the HAL. 49 // Create the driver, from the HAL.
63 let mut ep_out_buffer = [0u8; 256]; 50 let mut ep_out_buffer = [0u8; 256];
64 let mut config = embassy_stm32::usb::Config::default(); 51 let mut config = embassy_stm32::usb::Config::default();
diff --git a/examples/stm32wba/src/bin/usb_serial.rs b/examples/stm32wba/src/bin/usb_serial.rs
deleted file mode 100644
index 8d60aed8c..000000000
--- a/examples/stm32wba/src/bin/usb_serial.rs
+++ /dev/null
@@ -1,119 +0,0 @@
1#![no_std]
2#![no_main]
3
4use defmt::{panic, *};
5use defmt_rtt as _; // global logger
6use embassy_executor::Spawner;
7use embassy_futures::join::join;
8use embassy_stm32::usb::{Driver, Instance};
9use embassy_stm32::{bind_interrupts, peripherals, usb, Config};
10use embassy_usb::class::cdc_acm::{CdcAcmClass, State};
11use embassy_usb::driver::EndpointError;
12use embassy_usb::Builder;
13use panic_probe as _;
14
15bind_interrupts!(struct Irqs {
16 OTG_HS => usb::InterruptHandler<peripherals::USB_OTG_HS>;
17});
18
19#[embassy_executor::main]
20async fn main(_spawner: Spawner) {
21 info!("Hello World!");
22
23 let mut config = Config::default();
24 {
25 use embassy_stm32::rcc::*;
26 config.rcc.hsi = true;
27 config.rcc.pll1 = Some(Pll {
28 source: PllSource::HSI, // 16 MHz
29 prediv: PllPreDiv::DIV1,
30 mul: PllMul::MUL10,
31 divp: None,
32 divq: None,
33 divr: Some(PllDiv::DIV1), // 160 MHz
34 });
35 config.rcc.sys = Sysclk::PLL1_R;
36 config.rcc.voltage_range = VoltageScale::RANGE1;
37 config.rcc.hsi48 = Some(Hsi48Config { sync_from_usb: true }); // needed for USB
38 config.rcc.mux.iclksel = mux::Iclksel::HSI48; // USB uses ICLK
39 }
40
41 let p = embassy_stm32::init(config);
42
43 // Create the driver, from the HAL.
44 let mut ep_out_buffer = [0u8; 256];
45 let mut config = embassy_stm32::usb::Config::default();
46 // Do not enable vbus_detection. This is a safe default that works in all boards.
47 // However, if your USB device is self-powered (can stay powered on if USB is unplugged), you need
48 // to enable vbus_detection to comply with the USB spec. If you enable it, the board
49 // has to support it or USB won't work at all. See docs on `vbus_detection` for details.
50 config.vbus_detection = false;
51 let driver = Driver::new_hs(p.USB_OTG_HS, Irqs, p.PD6, p.PD7, &mut ep_out_buffer, config);
52
53 // Create embassy-usb Config
54 let mut config = embassy_usb::Config::new(0xc0de, 0xcafe);
55 config.manufacturer = Some("Embassy");
56 config.product = Some("USB-serial example");
57 config.serial_number = Some("12345678");
58
59 // Create embassy-usb DeviceBuilder using the driver and config.
60 // It needs some buffers for building the descriptors.
61 let mut config_descriptor = [0; 256];
62 let mut bos_descriptor = [0; 256];
63 let mut control_buf = [0; 64];
64
65 let mut state = State::new();
66
67 let mut builder = Builder::new(
68 driver,
69 config,
70 &mut config_descriptor,
71 &mut bos_descriptor,
72 &mut [], // no msos descriptors
73 &mut control_buf,
74 );
75
76 // Create classes on the builder.
77 let mut class = CdcAcmClass::new(&mut builder, &mut state, 64);
78
79 // Build the builder.
80 let mut usb = builder.build();
81
82 // Run the USB device.
83 let usb_fut = usb.run();
84
85 // Do stuff with the class!
86 let echo_fut = async {
87 loop {
88 class.wait_connection().await;
89 info!("Connected");
90 let _ = echo(&mut class).await;
91 info!("Disconnected");
92 }
93 };
94
95 // Run everything concurrently.
96 // If we had made everything `'static` above instead, we could do this using separate tasks instead.
97 join(usb_fut, echo_fut).await;
98}
99
100struct Disconnected {}
101
102impl From<EndpointError> for Disconnected {
103 fn from(val: EndpointError) -> Self {
104 match val {
105 EndpointError::BufferOverflow => panic!("Buffer overflow"),
106 EndpointError::Disabled => Disconnected {},
107 }
108 }
109}
110
111async fn echo<'d, T: Instance + 'd>(class: &mut CdcAcmClass<'d, Driver<'d, T>>) -> Result<(), Disconnected> {
112 let mut buf = [0; 64];
113 loop {
114 let n = class.read_packet(&mut buf).await?;
115 let data = &buf[..n];
116 info!("data: {:x}", data);
117 class.write_packet(data).await?;
118 }
119}