aboutsummaryrefslogtreecommitdiff
path: root/examples/stm32u5/src
diff options
context:
space:
mode:
authorDave Marples <[email protected]>2024-12-05 16:35:56 +0000
committerMarvin Drees <[email protected]>2024-12-10 10:51:28 +0100
commit501d3942e8b6b04fd1cb4c14728ba4c2e118d8b5 (patch)
treed8b71167fc51871c4d2ab44203076e2c0d241b85 /examples/stm32u5/src
parent86578acaa4d4dbed06ed4fcecec25884f6883e82 (diff)
Add support for stm32u595/5a5 OTG_HS in client mode
Diffstat (limited to 'examples/stm32u5/src')
-rw-r--r--examples/stm32u5/src/bin/usb_hs_serial.rs129
1 files changed, 129 insertions, 0 deletions
diff --git a/examples/stm32u5/src/bin/usb_hs_serial.rs b/examples/stm32u5/src/bin/usb_hs_serial.rs
new file mode 100644
index 000000000..5549e2cbb
--- /dev/null
+++ b/examples/stm32u5/src/bin/usb_hs_serial.rs
@@ -0,0 +1,129 @@
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 use embassy_stm32::time::Hertz;
27 config.rcc.hse = Some(Hse {
28 freq: Hertz(16_000_000),
29 mode: HseMode::Oscillator,
30 });
31 config.rcc.pll1 = Some(Pll {
32 source: PllSource::HSE,
33 prediv: PllPreDiv::DIV2, // HSE / 2 = 8MHz
34 mul: PllMul::MUL60, // 8MHz * 60 = 480MHz
35 divr: Some(PllDiv::DIV3), // 480MHz / 3 = 160MHz (sys_ck)
36 divq: Some(PllDiv::DIV10), // 480MHz / 10 = 48MHz (USB)
37 divp: Some(PllDiv::DIV15), // 480MHz / 15 = 32MHz (USBOTG)
38 });
39 config.rcc.mux.otghssel = mux::Otghssel::PLL1_P;
40 config.rcc.voltage_range = VoltageScale::RANGE1;
41 config.rcc.sys = Sysclk::PLL1_R;
42 }
43
44 let p = embassy_stm32::init(config);
45
46 // Create the driver, from the HAL.
47 let mut ep_out_buffer = [0u8; 256];
48 let mut config = embassy_stm32::usb::Config::default();
49 // Do not enable vbus_detection. This is a safe default that works in all boards.
50 // However, if your USB device is self-powered (can stay powered on if USB is unplugged), you need
51 // to enable vbus_detection to comply with the USB spec. If you enable it, the board
52 // has to support it or USB won't work at all. See docs on `vbus_detection` for details.
53 config.vbus_detection = false;
54 let driver = Driver::new_hs(p.USB_OTG_HS, Irqs, p.PA12, p.PA11, &mut ep_out_buffer, config);
55
56 // Create embassy-usb Config
57 let mut config = embassy_usb::Config::new(0xc0de, 0xcafe);
58 config.manufacturer = Some("Embassy");
59 config.product = Some("USB-serial example");
60 config.serial_number = Some("12345678");
61
62 // Required for windows compatibility.
63 // https://developer.nordicsemi.com/nRF_Connect_SDK/doc/1.9.1/kconfig/CONFIG_CDC_ACM_IAD.html#help
64 config.device_class = 0xEF;
65 config.device_sub_class = 0x02;
66 config.device_protocol = 0x01;
67 config.composite_with_iads = true;
68
69 // Create embassy-usb DeviceBuilder using the driver and config.
70 // It needs some buffers for building the descriptors.
71 let mut config_descriptor = [0; 256];
72 let mut bos_descriptor = [0; 256];
73 let mut control_buf = [0; 64];
74
75 let mut state = State::new();
76
77 let mut builder = Builder::new(
78 driver,
79 config,
80 &mut config_descriptor,
81 &mut bos_descriptor,
82 &mut [], // no msos descriptors
83 &mut control_buf,
84 );
85
86 // Create classes on the builder.
87 let mut class = CdcAcmClass::new(&mut builder, &mut state, 64);
88
89 // Build the builder.
90 let mut usb = builder.build();
91
92 // Run the USB device.
93 let usb_fut = usb.run();
94
95 // Do stuff with the class!
96 let echo_fut = async {
97 loop {
98 class.wait_connection().await;
99 info!("Connected");
100 let _ = echo(&mut class).await;
101 info!("Disconnected");
102 }
103 };
104
105 // Run everything concurrently.
106 // If we had made everything `'static` above instead, we could do this using separate tasks instead.
107 join(usb_fut, echo_fut).await;
108}
109
110struct Disconnected {}
111
112impl From<EndpointError> for Disconnected {
113 fn from(val: EndpointError) -> Self {
114 match val {
115 EndpointError::BufferOverflow => panic!("Buffer overflow"),
116 EndpointError::Disabled => Disconnected {},
117 }
118 }
119}
120
121async fn echo<'d, T: Instance + 'd>(class: &mut CdcAcmClass<'d, Driver<'d, T>>) -> Result<(), Disconnected> {
122 let mut buf = [0; 64];
123 loop {
124 let n = class.read_packet(&mut buf).await?;
125 let data = &buf[..n];
126 info!("data: {:x}", data);
127 class.write_packet(data).await?;
128 }
129}