aboutsummaryrefslogtreecommitdiff
path: root/examples/stm32f4
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/stm32f4
parent041531c82911053671e71b7554d1020021f45921 (diff)
stm32 otg: add examples.
Diffstat (limited to 'examples/stm32f4')
-rw-r--r--examples/stm32f4/Cargo.toml5
-rw-r--r--examples/stm32f4/src/bin/usb_serial.rs106
2 files changed, 109 insertions, 2 deletions
diff --git a/examples/stm32f4/Cargo.toml b/examples/stm32f4/Cargo.toml
index 62d3f08df..252d60855 100644
--- a/examples/stm32f4/Cargo.toml
+++ b/examples/stm32f4/Cargo.toml
@@ -10,6 +10,7 @@ embassy-sync = { version = "0.1.0", path = "../../embassy-sync", features = ["de
10embassy-executor = { version = "0.1.0", path = "../../embassy-executor", features = ["defmt", "integrated-timers"] } 10embassy-executor = { version = "0.1.0", path = "../../embassy-executor", features = ["defmt", "integrated-timers"] }
11embassy-time = { version = "0.1.0", path = "../../embassy-time", features = ["defmt", "defmt-timestamp-uptime", "unstable-traits", "tick-hz-32_768"] } 11embassy-time = { version = "0.1.0", path = "../../embassy-time", features = ["defmt", "defmt-timestamp-uptime", "unstable-traits", "tick-hz-32_768"] }
12embassy-stm32 = { version = "0.1.0", path = "../../embassy-stm32", features = ["nightly", "unstable-traits", "defmt", "stm32f429zi", "unstable-pac", "memory-x", "time-driver-any", "exti"] } 12embassy-stm32 = { version = "0.1.0", path = "../../embassy-stm32", features = ["nightly", "unstable-traits", "defmt", "stm32f429zi", "unstable-pac", "memory-x", "time-driver-any", "exti"] }
13embassy-usb = { version = "0.1.0", path = "../../embassy-usb", features = ["defmt"] }
13 14
14defmt = "0.3" 15defmt = "0.3"
15defmt-rtt = "0.4" 16defmt-rtt = "0.4"
@@ -26,5 +27,5 @@ embedded-storage = "0.3.0"
26micromath = "2.0.0" 27micromath = "2.0.0"
27static_cell = "1.0" 28static_cell = "1.0"
28 29
29usb-device = "0.2" 30[profile.release]
30usbd-serial = "0.1.1" 31debug = 2
diff --git a/examples/stm32f4/src/bin/usb_serial.rs b/examples/stm32f4/src/bin/usb_serial.rs
new file mode 100644
index 000000000..014647762
--- /dev/null
+++ b/examples/stm32f4/src/bin/usb_serial.rs
@@ -0,0 +1,106 @@
1#![no_std]
2#![no_main]
3#![feature(type_alias_impl_trait)]
4
5use defmt::{panic, *};
6use embassy_executor::Spawner;
7use embassy_stm32::time::mhz;
8use embassy_stm32::usb_otg::{Driver, Instance};
9use embassy_stm32::{interrupt, Config};
10use embassy_usb::class::cdc_acm::{CdcAcmClass, State};
11use embassy_usb::driver::EndpointError;
12use embassy_usb::Builder;
13use futures::future::join;
14use {defmt_rtt as _, panic_probe as _};
15
16#[embassy_executor::main]
17async fn main(_spawner: Spawner) {
18 info!("Hello World!");
19
20 let mut config = Config::default();
21 config.rcc.pll48 = true;
22 config.rcc.sys_ck = Some(mhz(48));
23
24 let p = embassy_stm32::init(config);
25
26 // Create the driver, from the HAL.
27 let irq = interrupt::take!(OTG_FS);
28 let mut ep_out_buffer = [0u8; 256];
29 let driver = Driver::new_fs(p.USB_OTG_FS, irq, p.PA12, p.PA11, &mut ep_out_buffer);
30
31 // Create embassy-usb Config
32 let mut config = embassy_usb::Config::new(0xc0de, 0xcafe);
33 config.manufacturer = Some("Embassy");
34 config.product = Some("USB-serial example");
35 config.serial_number = Some("12345678");
36
37 // Required for windows compatiblity.
38 // https://developer.nordicsemi.com/nRF_Connect_SDK/doc/1.9.1/kconfig/CONFIG_CDC_ACM_IAD.html#help
39 config.device_class = 0xEF;
40 config.device_sub_class = 0x02;
41 config.device_protocol = 0x01;
42 config.composite_with_iads = true;
43
44 // Create embassy-usb DeviceBuilder using the driver and config.
45 // It needs some buffers for building the descriptors.
46 let mut device_descriptor = [0; 256];
47 let mut config_descriptor = [0; 256];
48 let mut bos_descriptor = [0; 256];
49 let mut control_buf = [0; 64];
50
51 let mut state = State::new();
52
53 let mut builder = Builder::new(
54 driver,
55 config,
56 &mut device_descriptor,
57 &mut config_descriptor,
58 &mut bos_descriptor,
59 &mut control_buf,
60 None,
61 );
62
63 // Create classes on the builder.
64 let mut class = CdcAcmClass::new(&mut builder, &mut state, 64);
65
66 // Build the builder.
67 let mut usb = builder.build();
68
69 // Run the USB device.
70 let usb_fut = usb.run();
71
72 // Do stuff with the class!
73 let echo_fut = async {
74 loop {
75 class.wait_connection().await;
76 info!("Connected");
77 let _ = echo(&mut class).await;
78 info!("Disconnected");
79 }
80 };
81
82 // Run everything concurrently.
83 // If we had made everything `'static` above instead, we could do this using separate tasks instead.
84 join(usb_fut, echo_fut).await;
85}
86
87struct Disconnected {}
88
89impl From<EndpointError> for Disconnected {
90 fn from(val: EndpointError) -> Self {
91 match val {
92 EndpointError::BufferOverflow => panic!("Buffer overflow"),
93 EndpointError::Disabled => Disconnected {},
94 }
95 }
96}
97
98async fn echo<'d, T: Instance + 'd>(class: &mut CdcAcmClass<'d, Driver<'d, T>>) -> Result<(), Disconnected> {
99 let mut buf = [0; 64];
100 loop {
101 let n = class.read_packet(&mut buf).await?;
102 let data = &buf[..n];
103 info!("data: {:x}", data);
104 class.write_packet(data).await?;
105 }
106}