diff options
| author | chemicstry <[email protected]> | 2022-02-08 00:32:49 +0200 |
|---|---|---|
| committer | chemicstry <[email protected]> | 2022-02-08 01:46:32 +0200 |
| commit | db0d798b4896e97bf40e42cb54344e7a3f77a5c8 (patch) | |
| tree | 1af218e26c9351fa483130d1a46f2b29a3094443 /examples | |
| parent | a4b4a7bcf995c9dcc32a2944887be853df604a1c (diff) | |
Add stm32 USB OTG peripherals
Diffstat (limited to 'examples')
| -rw-r--r-- | examples/stm32f4/Cargo.toml | 5 | ||||
| -rw-r--r-- | examples/stm32f4/src/bin/usb_uart.rs | 99 |
2 files changed, 103 insertions, 1 deletions
diff --git a/examples/stm32f4/Cargo.toml b/examples/stm32f4/Cargo.toml index 67b212d5c..8a5908e27 100644 --- a/examples/stm32f4/Cargo.toml +++ b/examples/stm32f4/Cargo.toml | |||
| @@ -8,7 +8,7 @@ resolver = "2" | |||
| 8 | 8 | ||
| 9 | [dependencies] | 9 | [dependencies] |
| 10 | embassy = { version = "0.1.0", path = "../../embassy", features = ["defmt", "unstable-traits"] } | 10 | embassy = { version = "0.1.0", path = "../../embassy", features = ["defmt", "unstable-traits"] } |
| 11 | embassy-stm32 = { version = "0.1.0", path = "../../embassy-stm32", features = ["defmt", "stm32f429zi", "unstable-pac", "memory-x", "time-driver-any", "exti"] } | 11 | embassy-stm32 = { version = "0.1.0", path = "../../embassy-stm32", features = ["defmt", "stm32f429zi", "unstable-pac", "memory-x", "time-driver-any", "exti", "usb-otg-fs"] } |
| 12 | 12 | ||
| 13 | defmt = "0.3" | 13 | defmt = "0.3" |
| 14 | defmt-rtt = "0.3" | 14 | defmt-rtt = "0.3" |
| @@ -20,3 +20,6 @@ panic-probe = { version = "0.3", features = ["print-defmt"] } | |||
| 20 | futures = { version = "0.3.17", default-features = false, features = ["async-await"] } | 20 | futures = { version = "0.3.17", default-features = false, features = ["async-await"] } |
| 21 | heapless = { version = "0.7.5", default-features = false } | 21 | heapless = { version = "0.7.5", default-features = false } |
| 22 | nb = "1.0.0" | 22 | nb = "1.0.0" |
| 23 | |||
| 24 | usb-device = "0.2" | ||
| 25 | usbd-serial = "0.1.1" | ||
diff --git a/examples/stm32f4/src/bin/usb_uart.rs b/examples/stm32f4/src/bin/usb_uart.rs new file mode 100644 index 000000000..7167a28b4 --- /dev/null +++ b/examples/stm32f4/src/bin/usb_uart.rs | |||
| @@ -0,0 +1,99 @@ | |||
| 1 | #![no_std] | ||
| 2 | #![no_main] | ||
| 3 | #![feature(type_alias_impl_trait)] | ||
| 4 | |||
| 5 | #[path = "../example_common.rs"] | ||
| 6 | mod example_common; | ||
| 7 | |||
| 8 | use defmt::{info, unwrap}; | ||
| 9 | use defmt_rtt as _; // global logger | ||
| 10 | use embassy::interrupt::InterruptExt; | ||
| 11 | use futures::pin_mut; | ||
| 12 | use panic_probe as _; // print out panic messages | ||
| 13 | |||
| 14 | use embassy::executor::Spawner; | ||
| 15 | use embassy::io::{AsyncBufReadExt, AsyncWriteExt}; | ||
| 16 | use embassy_stm32::usb_otg_fs::{State, Usb, UsbBus, UsbOtgFs, UsbSerial}; | ||
| 17 | use embassy_stm32::{interrupt, time::Hertz, Config, Peripherals}; | ||
| 18 | use usb_device::device::{UsbDeviceBuilder, UsbVidPid}; | ||
| 19 | |||
| 20 | static mut EP_MEMORY: [u32; 2048] = [0; 2048]; | ||
| 21 | |||
| 22 | // USB requires at least 48 MHz clock | ||
| 23 | fn config() -> Config { | ||
| 24 | let mut config = Config::default(); | ||
| 25 | config.rcc.sys_ck = Some(Hertz(48_000_000)); | ||
| 26 | config | ||
| 27 | } | ||
| 28 | |||
| 29 | #[embassy::main(config = "config()")] | ||
| 30 | async fn main(_spawner: Spawner, p: Peripherals) { | ||
| 31 | let mut rx_buffer = [0u8; 64]; | ||
| 32 | // we send back input + cr + lf | ||
| 33 | let mut tx_buffer = [0u8; 66]; | ||
| 34 | |||
| 35 | let peri = UsbOtgFs::new(p.USB_OTG_FS, p.PA12, p.PA11); | ||
| 36 | let usb_bus = UsbBus::new(peri, unsafe { &mut EP_MEMORY }); | ||
| 37 | |||
| 38 | let serial = UsbSerial::new(&usb_bus, &mut rx_buffer, &mut tx_buffer); | ||
| 39 | |||
| 40 | let device = UsbDeviceBuilder::new(&usb_bus, UsbVidPid(0x16c0, 0x27dd)) | ||
| 41 | .manufacturer("Fake company") | ||
| 42 | .product("Serial port") | ||
| 43 | .serial_number("TEST") | ||
| 44 | .device_class(0x02) | ||
| 45 | .build(); | ||
| 46 | |||
| 47 | let irq = interrupt::take!(OTG_FS); | ||
| 48 | irq.set_priority(interrupt::Priority::P3); | ||
| 49 | |||
| 50 | let mut state = State::new(); | ||
| 51 | let usb = unsafe { Usb::new(&mut state, device, serial, irq) }; | ||
| 52 | pin_mut!(usb); | ||
| 53 | |||
| 54 | let (mut reader, mut writer) = usb.as_ref().take_serial_0(); | ||
| 55 | |||
| 56 | info!("usb initialized!"); | ||
| 57 | |||
| 58 | unwrap!( | ||
| 59 | writer | ||
| 60 | .write_all(b"\r\nInput returned upper cased on CR+LF\r\n") | ||
| 61 | .await | ||
| 62 | ); | ||
| 63 | |||
| 64 | let mut buf = [0u8; 64]; | ||
| 65 | loop { | ||
| 66 | let mut n = 0; | ||
| 67 | |||
| 68 | async { | ||
| 69 | loop { | ||
| 70 | let char = unwrap!(reader.read_byte().await); | ||
| 71 | |||
| 72 | if char == b'\r' || char == b'\n' { | ||
| 73 | break; | ||
| 74 | } | ||
| 75 | |||
| 76 | buf[n] = char; | ||
| 77 | n += 1; | ||
| 78 | |||
| 79 | // stop if we're out of room | ||
| 80 | if n == buf.len() { | ||
| 81 | break; | ||
| 82 | } | ||
| 83 | } | ||
| 84 | } | ||
| 85 | .await; | ||
| 86 | |||
| 87 | if n > 0 { | ||
| 88 | for char in buf[..n].iter_mut() { | ||
| 89 | // upper case | ||
| 90 | if 0x61 <= *char && *char <= 0x7a { | ||
| 91 | *char &= !0x20; | ||
| 92 | } | ||
| 93 | } | ||
| 94 | unwrap!(writer.write_all(&buf[..n]).await); | ||
| 95 | unwrap!(writer.write_all(b"\r\n").await); | ||
| 96 | unwrap!(writer.flush().await); | ||
| 97 | } | ||
| 98 | } | ||
| 99 | } | ||
