diff options
| author | Kevin <[email protected]> | 2024-09-15 02:44:16 +0200 |
|---|---|---|
| committer | Kevin <[email protected]> | 2024-09-22 00:23:07 +0200 |
| commit | 2f60d78ea318f51ff59868c348b77cf880012198 (patch) | |
| tree | db390bf1d2091f67ea21f47dde567a96a7558553 /examples/stm32h7rs | |
| parent | afd8a869620f420f9ccae3b40654c4a515b78044 (diff) | |
Add OTG_HS support for STM32H7R/S
Diffstat (limited to 'examples/stm32h7rs')
| -rw-r--r-- | examples/stm32h7rs/src/bin/usb_serial.rs | 139 |
1 files changed, 139 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..5a234e898 --- /dev/null +++ b/examples/stm32h7rs/src/bin/usb_serial.rs | |||
| @@ -0,0 +1,139 @@ | |||
| 1 | #![no_std] | ||
| 2 | #![no_main] | ||
| 3 | |||
| 4 | use defmt::{panic, *}; | ||
| 5 | use embassy_executor::Spawner; | ||
| 6 | use embassy_futures::join::join; | ||
| 7 | use embassy_stm32::time::Hertz; | ||
| 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 {defmt_rtt as _, panic_probe as _}; | ||
| 14 | |||
| 15 | bind_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] | ||
| 25 | async 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 | } | ||
| 52 | |||
| 53 | let p = embassy_stm32::init(config); | ||
| 54 | |||
| 55 | // Create the driver, from the HAL. | ||
| 56 | let mut ep_out_buffer = [0u8; 256]; | ||
| 57 | let mut config = embassy_stm32::usb::Config::default(); | ||
| 58 | |||
| 59 | // Do not enable vbus_detection. This is a safe default that works in all boards. | ||
| 60 | // However, if your USB device is self-powered (can stay powered on if USB is unplugged), you need | ||
| 61 | // to enable vbus_detection to comply with the USB spec. If you enable it, the board | ||
| 62 | // has to support it or USB won't work at all. See docs on `vbus_detection` for details. | ||
| 63 | config.vbus_detection = false; | ||
| 64 | |||
| 65 | let driver = Driver::new_hs(p.USB_OTG_HS, Irqs, p.PM6, p.PM5, &mut ep_out_buffer, config); | ||
| 66 | |||
| 67 | // Create embassy-usb Config | ||
| 68 | let mut config = embassy_usb::Config::new(0xc0de, 0xcafe); | ||
| 69 | config.manufacturer = Some("Embassy"); | ||
| 70 | config.product = Some("USB-serial example"); | ||
| 71 | config.serial_number = Some("12345678"); | ||
| 72 | // Required for windows compatibility. | ||
| 73 | // https://developer.nordicsemi.com/nRF_Connect_SDK/doc/1.9.1/kconfig/CONFIG_CDC_ACM_IAD.html#help | ||
| 74 | config.device_class = 0xEF; | ||
| 75 | config.device_sub_class = 0x02; | ||
| 76 | config.device_protocol = 0x01; | ||
| 77 | config.composite_with_iads = true; | ||
| 78 | |||
| 79 | // Create embassy-usb DeviceBuilder using the driver and config. | ||
| 80 | // It needs some buffers for building the descriptors. | ||
| 81 | let mut config_descriptor = [0; 256]; | ||
| 82 | let mut bos_descriptor = [0; 256]; | ||
| 83 | let mut control_buf = [0; 64]; | ||
| 84 | |||
| 85 | let mut state = State::new(); | ||
| 86 | |||
| 87 | let mut builder = Builder::new( | ||
| 88 | driver, | ||
| 89 | config, | ||
| 90 | &mut config_descriptor, | ||
| 91 | &mut bos_descriptor, | ||
| 92 | &mut [], // no msos descriptors | ||
| 93 | &mut control_buf, | ||
| 94 | ); | ||
| 95 | |||
| 96 | // Create classes on the builder. | ||
| 97 | let mut class = CdcAcmClass::new(&mut builder, &mut state, 64); | ||
| 98 | |||
| 99 | // Build the builder. | ||
| 100 | let mut usb = builder.build(); | ||
| 101 | |||
| 102 | // Run the USB device. | ||
| 103 | let usb_fut = usb.run(); | ||
| 104 | |||
| 105 | // Do stuff with the class! | ||
| 106 | let echo_fut = async { | ||
| 107 | loop { | ||
| 108 | class.wait_connection().await; | ||
| 109 | info!("Connected"); | ||
| 110 | let _ = echo(&mut class).await; | ||
| 111 | info!("Disconnected"); | ||
| 112 | } | ||
| 113 | }; | ||
| 114 | |||
| 115 | // Run everything concurrently. | ||
| 116 | // If we had made everything `'static` above instead, we could do this using separate tasks instead. | ||
| 117 | join(usb_fut, echo_fut).await; | ||
| 118 | } | ||
| 119 | |||
| 120 | struct Disconnected {} | ||
| 121 | |||
| 122 | impl From<EndpointError> for Disconnected { | ||
| 123 | fn from(val: EndpointError) -> Self { | ||
| 124 | match val { | ||
| 125 | EndpointError::BufferOverflow => panic!("Buffer overflow"), | ||
| 126 | EndpointError::Disabled => Disconnected {}, | ||
| 127 | } | ||
| 128 | } | ||
| 129 | } | ||
| 130 | |||
| 131 | async fn echo<'d, T: Instance + 'd>(class: &mut CdcAcmClass<'d, Driver<'d, T>>) -> Result<(), Disconnected> { | ||
| 132 | let mut buf = [0; 64]; | ||
| 133 | loop { | ||
| 134 | let n = class.read_packet(&mut buf).await?; | ||
| 135 | let data = &buf[..n]; | ||
| 136 | info!("data: {:x}", data); | ||
| 137 | class.write_packet(data).await?; | ||
| 138 | } | ||
| 139 | } | ||
