aboutsummaryrefslogtreecommitdiff
path: root/examples/stm32u5/src
diff options
context:
space:
mode:
authorchemicstry <[email protected]>2023-01-11 17:58:15 +0100
committerDario Nieuwenhuis <[email protected]>2023-01-11 17:58:15 +0100
commit1af102a1aaa11d03bfa37831a3284546b605efd8 (patch)
treed416bfda40860e61898e5599809c503c7149285a /examples/stm32u5/src
parent041531c82911053671e71b7554d1020021f45921 (diff)
stm32 otg: add examples.
Diffstat (limited to 'examples/stm32u5/src')
-rw-r--r--examples/stm32u5/src/bin/usb_serial.rs108
1 files changed, 108 insertions, 0 deletions
diff --git a/examples/stm32u5/src/bin/usb_serial.rs b/examples/stm32u5/src/bin/usb_serial.rs
new file mode 100644
index 000000000..c846836b0
--- /dev/null
+++ b/examples/stm32u5/src/bin/usb_serial.rs
@@ -0,0 +1,108 @@
1#![no_std]
2#![no_main]
3#![feature(type_alias_impl_trait)]
4
5use defmt::{panic, *};
6use defmt_rtt as _; // global logger
7use embassy_executor::Spawner;
8use embassy_stm32::rcc::*;
9use embassy_stm32::usb_otg::{Driver, Instance};
10use embassy_stm32::{interrupt, Config};
11use embassy_usb::class::cdc_acm::{CdcAcmClass, State};
12use embassy_usb::driver::EndpointError;
13use embassy_usb::Builder;
14use futures::future::join;
15use panic_probe as _;
16
17#[embassy_executor::main]
18async fn main(_spawner: Spawner) {
19 info!("Hello World!");
20
21 let mut config = Config::default();
22 config.rcc.mux = ClockSrc::PLL1R(PllSrc::HSI16, PllM::Div2, PllN::Mul10, PllClkDiv::NotDivided);
23 //config.rcc.mux = ClockSrc::MSI(MSIRange::Range48mhz);
24 config.rcc.hsi48 = true;
25
26 let p = embassy_stm32::init(config);
27
28 // Create the driver, from the HAL.
29 let irq = interrupt::take!(OTG_FS);
30 let mut ep_out_buffer = [0u8; 256];
31 let driver = Driver::new_fs(p.USB_OTG_FS, irq, p.PA12, p.PA11, &mut ep_out_buffer);
32
33 // Create embassy-usb Config
34 let mut config = embassy_usb::Config::new(0xc0de, 0xcafe);
35 config.manufacturer = Some("Embassy");
36 config.product = Some("USB-serial example");
37 config.serial_number = Some("12345678");
38
39 // Required for windows compatiblity.
40 // https://developer.nordicsemi.com/nRF_Connect_SDK/doc/1.9.1/kconfig/CONFIG_CDC_ACM_IAD.html#help
41 config.device_class = 0xEF;
42 config.device_sub_class = 0x02;
43 config.device_protocol = 0x01;
44 config.composite_with_iads = true;
45
46 // Create embassy-usb DeviceBuilder using the driver and config.
47 // It needs some buffers for building the descriptors.
48 let mut device_descriptor = [0; 256];
49 let mut config_descriptor = [0; 256];
50 let mut bos_descriptor = [0; 256];
51 let mut control_buf = [0; 64];
52
53 let mut state = State::new();
54
55 let mut builder = Builder::new(
56 driver,
57 config,
58 &mut device_descriptor,
59 &mut config_descriptor,
60 &mut bos_descriptor,
61 &mut control_buf,
62 None,
63 );
64
65 // Create classes on the builder.
66 let mut class = CdcAcmClass::new(&mut builder, &mut state, 64);
67
68 // Build the builder.
69 let mut usb = builder.build();
70
71 // Run the USB device.
72 let usb_fut = usb.run();
73
74 // Do stuff with the class!
75 let echo_fut = async {
76 loop {
77 class.wait_connection().await;
78 info!("Connected");
79 let _ = echo(&mut class).await;
80 info!("Disconnected");
81 }
82 };
83
84 // Run everything concurrently.
85 // If we had made everything `'static` above instead, we could do this using separate tasks instead.
86 join(usb_fut, echo_fut).await;
87}
88
89struct Disconnected {}
90
91impl From<EndpointError> for Disconnected {
92 fn from(val: EndpointError) -> Self {
93 match val {
94 EndpointError::BufferOverflow => panic!("Buffer overflow"),
95 EndpointError::Disabled => Disconnected {},
96 }
97 }
98}
99
100async fn echo<'d, T: Instance + 'd>(class: &mut CdcAcmClass<'d, Driver<'d, T>>) -> Result<(), Disconnected> {
101 let mut buf = [0; 64];
102 loop {
103 let n = class.read_packet(&mut buf).await?;
104 let data = &buf[..n];
105 info!("data: {:x}", data);
106 class.write_packet(data).await?;
107 }
108}