aboutsummaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorDario Nieuwenhuis <[email protected]>2024-09-22 23:56:17 +0000
committerGitHub <[email protected]>2024-09-22 23:56:17 +0000
commit9705f3332b7d858aee94903ce433fba93d62332c (patch)
tree987c003cf3fb4edcb7352ca41522146bc3498879 /examples
parentb9553badb314b050be705bbb24d87a33304c55ac (diff)
parent85b7c8957cce3fef6011e63d7cb6ff85912ccb50 (diff)
Merge pull request #3337 from doesnotcompete/feature/h7rs-usb
Add OTG_HS support for STM32H7R/S
Diffstat (limited to 'examples')
-rw-r--r--examples/stm32h7rs/src/bin/usb_serial.rs140
1 files changed, 140 insertions, 0 deletions
diff --git a/examples/stm32h7rs/src/bin/usb_serial.rs b/examples/stm32h7rs/src/bin/usb_serial.rs
new file mode 100644
index 000000000..6773f7843
--- /dev/null
+++ b/examples/stm32h7rs/src/bin/usb_serial.rs
@@ -0,0 +1,140 @@
1#![no_std]
2#![no_main]
3
4use defmt::{panic, *};
5use embassy_executor::Spawner;
6use embassy_futures::join::join;
7use embassy_stm32::time::Hertz;
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 {defmt_rtt as _, panic_probe as _};
14
15bind_interrupts!(struct Irqs {
16 OTG_HS => usb::InterruptHandler<peripherals::USB_OTG_HS>;
17});
18
19// If you are trying this and your USB device doesn't connect, the most
20// common issues are the RCC config and vbus_detection
21//
22// See https://embassy.dev/book/#_the_usb_examples_are_not_working_on_my_board_is_there_anything_else_i_need_to_configure
23// for more information.
24#[embassy_executor::main]
25async fn main(_spawner: Spawner) {
26 info!("Hello World!");
27
28 let mut config = Config::default();
29
30 {
31 use embassy_stm32::rcc::*;
32 config.rcc.hse = Some(Hse {
33 freq: Hertz(24_000_000),
34 mode: HseMode::Oscillator,
35 });
36 config.rcc.pll1 = Some(Pll {
37 source: PllSource::HSE,
38 prediv: PllPreDiv::DIV12,
39 mul: PllMul::MUL300,
40 divp: Some(PllDiv::DIV1), //600 MHz
41 divq: Some(PllDiv::DIV2), // 300 MHz
42 divr: Some(PllDiv::DIV2), // 300 MHz
43 });
44 config.rcc.sys = Sysclk::PLL1_P; // 600 MHz
45 config.rcc.ahb_pre = AHBPrescaler::DIV2; // 300 MHz
46 config.rcc.apb1_pre = APBPrescaler::DIV2; // 150 MHz
47 config.rcc.apb2_pre = APBPrescaler::DIV2; // 150 MHz
48 config.rcc.apb4_pre = APBPrescaler::DIV2; // 150 MHz
49 config.rcc.apb5_pre = APBPrescaler::DIV2; // 150 MHz
50 config.rcc.voltage_scale = VoltageScale::HIGH;
51 config.rcc.mux.usbphycsel = mux::Usbphycsel::HSE;
52 }
53
54 let p = embassy_stm32::init(config);
55
56 // Create the driver, from the HAL.
57 let mut ep_out_buffer = [0u8; 256];
58 let mut config = embassy_stm32::usb::Config::default();
59
60 // Do not enable vbus_detection. This is a safe default that works in all boards.
61 // However, if your USB device is self-powered (can stay powered on if USB is unplugged), you need
62 // to enable vbus_detection to comply with the USB spec. If you enable it, the board
63 // has to support it or USB won't work at all. See docs on `vbus_detection` for details.
64 config.vbus_detection = false;
65
66 let driver = Driver::new_hs(p.USB_OTG_HS, Irqs, p.PM6, p.PM5, &mut ep_out_buffer, config);
67
68 // Create embassy-usb Config
69 let mut config = embassy_usb::Config::new(0xc0de, 0xcafe);
70 config.manufacturer = Some("Embassy");
71 config.product = Some("USB-serial example");
72 config.serial_number = Some("12345678");
73 // Required for windows compatibility.
74 // https://developer.nordicsemi.com/nRF_Connect_SDK/doc/1.9.1/kconfig/CONFIG_CDC_ACM_IAD.html#help
75 config.device_class = 0xEF;
76 config.device_sub_class = 0x02;
77 config.device_protocol = 0x01;
78 config.composite_with_iads = true;
79
80 // Create embassy-usb DeviceBuilder using the driver and config.
81 // It needs some buffers for building the descriptors.
82 let mut config_descriptor = [0; 256];
83 let mut bos_descriptor = [0; 256];
84 let mut control_buf = [0; 64];
85
86 let mut state = State::new();
87
88 let mut builder = Builder::new(
89 driver,
90 config,
91 &mut config_descriptor,
92 &mut bos_descriptor,
93 &mut [], // no msos descriptors
94 &mut control_buf,
95 );
96
97 // Create classes on the builder.
98 let mut class = CdcAcmClass::new(&mut builder, &mut state, 64);
99
100 // Build the builder.
101 let mut usb = builder.build();
102
103 // Run the USB device.
104 let usb_fut = usb.run();
105
106 // Do stuff with the class!
107 let echo_fut = async {
108 loop {
109 class.wait_connection().await;
110 info!("Connected");
111 let _ = echo(&mut class).await;
112 info!("Disconnected");
113 }
114 };
115
116 // Run everything concurrently.
117 // If we had made everything `'static` above instead, we could do this using separate tasks instead.
118 join(usb_fut, echo_fut).await;
119}
120
121struct Disconnected {}
122
123impl From<EndpointError> for Disconnected {
124 fn from(val: EndpointError) -> Self {
125 match val {
126 EndpointError::BufferOverflow => panic!("Buffer overflow"),
127 EndpointError::Disabled => Disconnected {},
128 }
129 }
130}
131
132async fn echo<'d, T: Instance + 'd>(class: &mut CdcAcmClass<'d, Driver<'d, T>>) -> Result<(), Disconnected> {
133 let mut buf = [0; 64];
134 loop {
135 let n = class.read_packet(&mut buf).await?;
136 let data = &buf[..n];
137 info!("data: {:x}", data);
138 class.write_packet(data).await?;
139 }
140}