diff options
| author | chemicstry <[email protected]> | 2023-01-11 17:58:15 +0100 |
|---|---|---|
| committer | Dario Nieuwenhuis <[email protected]> | 2023-01-11 17:58:15 +0100 |
| commit | 1af102a1aaa11d03bfa37831a3284546b605efd8 (patch) | |
| tree | d416bfda40860e61898e5599809c503c7149285a /examples/stm32l4/src | |
| parent | 041531c82911053671e71b7554d1020021f45921 (diff) | |
stm32 otg: add examples.
Diffstat (limited to 'examples/stm32l4/src')
| -rw-r--r-- | examples/stm32l4/src/bin/usb_serial.rs | 108 |
1 files changed, 108 insertions, 0 deletions
diff --git a/examples/stm32l4/src/bin/usb_serial.rs b/examples/stm32l4/src/bin/usb_serial.rs new file mode 100644 index 000000000..3e38b10a3 --- /dev/null +++ b/examples/stm32l4/src/bin/usb_serial.rs | |||
| @@ -0,0 +1,108 @@ | |||
| 1 | #![no_std] | ||
| 2 | #![no_main] | ||
| 3 | #![feature(type_alias_impl_trait)] | ||
| 4 | |||
| 5 | use defmt::{panic, *}; | ||
| 6 | use defmt_rtt as _; // global logger | ||
| 7 | use embassy_executor::Spawner; | ||
| 8 | use embassy_stm32::rcc::*; | ||
| 9 | use embassy_stm32::usb_otg::{Driver, Instance}; | ||
| 10 | use embassy_stm32::{interrupt, Config}; | ||
| 11 | use embassy_usb::class::cdc_acm::{CdcAcmClass, State}; | ||
| 12 | use embassy_usb::driver::EndpointError; | ||
| 13 | use embassy_usb::Builder; | ||
| 14 | use futures::future::join; | ||
| 15 | use panic_probe as _; | ||
| 16 | |||
| 17 | #[embassy_executor::main] | ||
| 18 | async fn main(_spawner: Spawner) { | ||
| 19 | info!("Hello World!"); | ||
| 20 | |||
| 21 | let mut config = Config::default(); | ||
| 22 | config.rcc.mux = ClockSrc::PLL(PLLSource::HSI16, PLLClkDiv::Div2, PLLSrcDiv::Div1, PLLMul::Mul10, None); | ||
| 23 | config.rcc.hsi48 = true; | ||
| 24 | |||
| 25 | let p = embassy_stm32::init(config); | ||
| 26 | |||
| 27 | // Create the driver, from the HAL. | ||
| 28 | let irq = interrupt::take!(OTG_FS); | ||
| 29 | let mut ep_out_buffer = [0u8; 256]; | ||
| 30 | let driver = Driver::new_fs(p.USB_OTG_FS, irq, p.PA12, p.PA11, &mut ep_out_buffer); | ||
| 31 | |||
| 32 | // Create embassy-usb Config | ||
| 33 | let mut config = embassy_usb::Config::new(0xc0de, 0xcafe); | ||
| 34 | config.max_packet_size_0 = 64; | ||
| 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 | |||
| 89 | struct Disconnected {} | ||
| 90 | |||
| 91 | impl 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 | |||
| 100 | async 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 | } | ||
