diff options
Diffstat (limited to 'examples')
| -rw-r--r-- | examples/stm32wba/src/bin/usb_hs_serial.rs | 25 | ||||
| -rw-r--r-- | examples/stm32wba/src/bin/usb_serial.rs | 119 |
2 files changed, 6 insertions, 138 deletions
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 | |||
| 4 | use defmt::{panic, *}; | ||
| 5 | use defmt_rtt as _; // global logger | ||
| 6 | use embassy_executor::Spawner; | ||
| 7 | use embassy_futures::join::join; | ||
| 8 | use embassy_stm32::usb::{Driver, Instance}; | ||
| 9 | use embassy_stm32::{bind_interrupts, peripherals, usb, Config}; | ||
| 10 | use embassy_usb::class::cdc_acm::{CdcAcmClass, State}; | ||
| 11 | use embassy_usb::driver::EndpointError; | ||
| 12 | use embassy_usb::Builder; | ||
| 13 | use panic_probe as _; | ||
| 14 | |||
| 15 | bind_interrupts!(struct Irqs { | ||
| 16 | OTG_HS => usb::InterruptHandler<peripherals::USB_OTG_HS>; | ||
| 17 | }); | ||
| 18 | |||
| 19 | #[embassy_executor::main] | ||
| 20 | async 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 | |||
| 100 | struct Disconnected {} | ||
| 101 | |||
| 102 | impl 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 | |||
| 111 | async 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 | } | ||
