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/stm32f7/src/bin | |
| parent | 041531c82911053671e71b7554d1020021f45921 (diff) | |
stm32 otg: add examples.
Diffstat (limited to 'examples/stm32f7/src/bin')
| -rw-r--r-- | examples/stm32f7/src/bin/usb_serial.rs | 107 |
1 files changed, 107 insertions, 0 deletions
diff --git a/examples/stm32f7/src/bin/usb_serial.rs b/examples/stm32f7/src/bin/usb_serial.rs new file mode 100644 index 000000000..688bd0dab --- /dev/null +++ b/examples/stm32f7/src/bin/usb_serial.rs | |||
| @@ -0,0 +1,107 @@ | |||
| 1 | #![no_std] | ||
| 2 | #![no_main] | ||
| 3 | #![feature(type_alias_impl_trait)] | ||
| 4 | |||
| 5 | use defmt::{panic, *}; | ||
| 6 | use embassy_executor::Spawner; | ||
| 7 | use embassy_stm32::time::mhz; | ||
| 8 | use embassy_stm32::usb_otg::{Driver, Instance}; | ||
| 9 | use embassy_stm32::{interrupt, Config}; | ||
| 10 | use embassy_usb::class::cdc_acm::{CdcAcmClass, State}; | ||
| 11 | use embassy_usb::driver::EndpointError; | ||
| 12 | use embassy_usb::Builder; | ||
| 13 | use futures::future::join; | ||
| 14 | use {defmt_rtt as _, panic_probe as _}; | ||
| 15 | |||
| 16 | #[embassy_executor::main] | ||
| 17 | async fn main(_spawner: Spawner) { | ||
| 18 | info!("Hello World!"); | ||
| 19 | |||
| 20 | let mut config = Config::default(); | ||
| 21 | config.rcc.hse = Some(mhz(8)); | ||
| 22 | config.rcc.pll48 = true; | ||
| 23 | config.rcc.sys_ck = Some(mhz(200)); | ||
| 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.manufacturer = Some("Embassy"); | ||
| 35 | config.product = Some("USB-serial example"); | ||
| 36 | config.serial_number = Some("12345678"); | ||
| 37 | |||
| 38 | // Required for windows compatiblity. | ||
| 39 | // https://developer.nordicsemi.com/nRF_Connect_SDK/doc/1.9.1/kconfig/CONFIG_CDC_ACM_IAD.html#help | ||
| 40 | config.device_class = 0xEF; | ||
| 41 | config.device_sub_class = 0x02; | ||
| 42 | config.device_protocol = 0x01; | ||
| 43 | config.composite_with_iads = true; | ||
| 44 | |||
| 45 | // Create embassy-usb DeviceBuilder using the driver and config. | ||
| 46 | // It needs some buffers for building the descriptors. | ||
| 47 | let mut device_descriptor = [0; 256]; | ||
| 48 | let mut config_descriptor = [0; 256]; | ||
| 49 | let mut bos_descriptor = [0; 256]; | ||
| 50 | let mut control_buf = [0; 64]; | ||
| 51 | |||
| 52 | let mut state = State::new(); | ||
| 53 | |||
| 54 | let mut builder = Builder::new( | ||
| 55 | driver, | ||
| 56 | config, | ||
| 57 | &mut device_descriptor, | ||
| 58 | &mut config_descriptor, | ||
| 59 | &mut bos_descriptor, | ||
| 60 | &mut control_buf, | ||
| 61 | None, | ||
| 62 | ); | ||
| 63 | |||
| 64 | // Create classes on the builder. | ||
| 65 | let mut class = CdcAcmClass::new(&mut builder, &mut state, 64); | ||
| 66 | |||
| 67 | // Build the builder. | ||
| 68 | let mut usb = builder.build(); | ||
| 69 | |||
| 70 | // Run the USB device. | ||
| 71 | let usb_fut = usb.run(); | ||
| 72 | |||
| 73 | // Do stuff with the class! | ||
| 74 | let echo_fut = async { | ||
| 75 | loop { | ||
| 76 | class.wait_connection().await; | ||
| 77 | info!("Connected"); | ||
| 78 | let _ = echo(&mut class).await; | ||
| 79 | info!("Disconnected"); | ||
| 80 | } | ||
| 81 | }; | ||
| 82 | |||
| 83 | // Run everything concurrently. | ||
| 84 | // If we had made everything `'static` above instead, we could do this using separate tasks instead. | ||
| 85 | join(usb_fut, echo_fut).await; | ||
| 86 | } | ||
| 87 | |||
| 88 | struct Disconnected {} | ||
| 89 | |||
| 90 | impl From<EndpointError> for Disconnected { | ||
| 91 | fn from(val: EndpointError) -> Self { | ||
| 92 | match val { | ||
| 93 | EndpointError::BufferOverflow => panic!("Buffer overflow"), | ||
| 94 | EndpointError::Disabled => Disconnected {}, | ||
| 95 | } | ||
| 96 | } | ||
| 97 | } | ||
| 98 | |||
| 99 | async fn echo<'d, T: Instance + 'd>(class: &mut CdcAcmClass<'d, Driver<'d, T>>) -> Result<(), Disconnected> { | ||
| 100 | let mut buf = [0; 64]; | ||
| 101 | loop { | ||
| 102 | let n = class.read_packet(&mut buf).await?; | ||
| 103 | let data = &buf[..n]; | ||
| 104 | info!("data: {:x}", data); | ||
| 105 | class.write_packet(data).await?; | ||
| 106 | } | ||
| 107 | } | ||
