diff options
| author | Thales Fragoso <[email protected]> | 2021-02-18 21:57:35 -0300 |
|---|---|---|
| committer | Thales Fragoso <[email protected]> | 2021-03-19 19:44:08 -0300 |
| commit | 890e93b4f009afe37b35e211567acfbf58be783a (patch) | |
| tree | c88b2fa578721fe25e0f0295c8d9a24c72e2ef40 /embassy-stm32f4-examples/src | |
| parent | 6719da3b6e726608fcde1ebdd27eb70ac9bb6d69 (diff) | |
Start working on usb serial
Diffstat (limited to 'embassy-stm32f4-examples/src')
| -rw-r--r-- | embassy-stm32f4-examples/src/bin/usb_serial.rs | 119 |
1 files changed, 119 insertions, 0 deletions
diff --git a/embassy-stm32f4-examples/src/bin/usb_serial.rs b/embassy-stm32f4-examples/src/bin/usb_serial.rs new file mode 100644 index 000000000..d2ccb4b21 --- /dev/null +++ b/embassy-stm32f4-examples/src/bin/usb_serial.rs | |||
| @@ -0,0 +1,119 @@ | |||
| 1 | #![no_std] | ||
| 2 | #![no_main] | ||
| 3 | #![feature(type_alias_impl_trait)] | ||
| 4 | |||
| 5 | #[path = "../example_common.rs"] | ||
| 6 | mod example_common; | ||
| 7 | use example_common::*; | ||
| 8 | |||
| 9 | use cortex_m_rt::entry; | ||
| 10 | use defmt::panic; | ||
| 11 | use embassy::executor::{task, Executor}; | ||
| 12 | use embassy::io::{AsyncBufReadExt, AsyncWriteExt}; | ||
| 13 | use embassy::time::{Duration, Timer}; | ||
| 14 | use embassy::util::Forever; | ||
| 15 | use embassy_stm32f4::interrupt::OwnedInterrupt; | ||
| 16 | use embassy_stm32f4::usb::Usb; | ||
| 17 | use embassy_stm32f4::usb_serial::UsbSerial; | ||
| 18 | use embassy_stm32f4::{interrupt, pac, rtc}; | ||
| 19 | use futures::future::{select, Either}; | ||
| 20 | use futures::pin_mut; | ||
| 21 | use stm32f4xx_hal::otg_fs::{UsbBus, USB}; | ||
| 22 | use stm32f4xx_hal::prelude::*; | ||
| 23 | use usb_device::bus::UsbBusAllocator; | ||
| 24 | use usb_device::prelude::*; | ||
| 25 | |||
| 26 | #[task] | ||
| 27 | async fn run1(bus: &'static mut UsbBusAllocator<UsbBus<USB>>) { | ||
| 28 | info!("Async task"); | ||
| 29 | |||
| 30 | let mut read_buf = [0u8; 128]; | ||
| 31 | let mut write_buf = [0u8; 128]; | ||
| 32 | let serial = UsbSerial::new(bus, &mut read_buf, &mut write_buf); | ||
| 33 | |||
| 34 | let device = UsbDeviceBuilder::new(bus, UsbVidPid(0x16c0, 0x27dd)) | ||
| 35 | .manufacturer("Fake company") | ||
| 36 | .product("Serial port") | ||
| 37 | .serial_number("TEST") | ||
| 38 | .device_class(0x02) | ||
| 39 | .build(); | ||
| 40 | |||
| 41 | let irq = interrupt::take!(OTG_FS); | ||
| 42 | irq.set_priority(interrupt::Priority::Level3); | ||
| 43 | |||
| 44 | let usb = Usb::new(device, serial, irq); | ||
| 45 | pin_mut!(usb); | ||
| 46 | |||
| 47 | let (mut read_interface, mut write_interface) = usb.as_mut().into_ref().take_serial(); | ||
| 48 | |||
| 49 | let mut buf = [0u8; 5]; | ||
| 50 | loop { | ||
| 51 | let recv_fut = read_interface.read(&mut buf); | ||
| 52 | let timeout = Timer::after(Duration::from_ticks(32768 * 3)); | ||
| 53 | |||
| 54 | match select(recv_fut, timeout).await { | ||
| 55 | Either::Left((recv, _)) => { | ||
| 56 | let recv = unwrap!(recv); | ||
| 57 | unwrap!(write_interface.write_all(&buf[..recv]).await); | ||
| 58 | } | ||
| 59 | Either::Right(_) => { | ||
| 60 | unwrap!(write_interface.write_all(b"Hello\r\n").await); | ||
| 61 | } | ||
| 62 | } | ||
| 63 | } | ||
| 64 | } | ||
| 65 | |||
| 66 | static RTC: Forever<rtc::RTC<pac::TIM2>> = Forever::new(); | ||
| 67 | static ALARM: Forever<rtc::Alarm<pac::TIM2>> = Forever::new(); | ||
| 68 | static EXECUTOR: Forever<Executor> = Forever::new(); | ||
| 69 | static USB_BUS: Forever<UsbBusAllocator<UsbBus<USB>>> = Forever::new(); | ||
| 70 | |||
| 71 | #[entry] | ||
| 72 | fn main() -> ! { | ||
| 73 | static mut EP_MEMORY: [u32; 1024] = [0; 1024]; | ||
| 74 | |||
| 75 | info!("Hello World!"); | ||
| 76 | |||
| 77 | let p = unwrap!(pac::Peripherals::take()); | ||
| 78 | |||
| 79 | p.RCC.ahb1enr.modify(|_, w| w.dma1en().enabled()); | ||
| 80 | let rcc = p.RCC.constrain(); | ||
| 81 | let clocks = rcc | ||
| 82 | .cfgr | ||
| 83 | .use_hse(25.mhz()) | ||
| 84 | .sysclk(48.mhz()) | ||
| 85 | .require_pll48clk() | ||
| 86 | .freeze(); | ||
| 87 | |||
| 88 | p.DBGMCU.cr.modify(|_, w| { | ||
| 89 | w.dbg_sleep().set_bit(); | ||
| 90 | w.dbg_standby().set_bit(); | ||
| 91 | w.dbg_stop().set_bit() | ||
| 92 | }); | ||
| 93 | |||
| 94 | let rtc = RTC.put(rtc::RTC::new(p.TIM2, interrupt::take!(TIM2), clocks)); | ||
| 95 | rtc.start(); | ||
| 96 | |||
| 97 | unsafe { embassy::time::set_clock(rtc) }; | ||
| 98 | |||
| 99 | let alarm = ALARM.put(rtc.alarm1()); | ||
| 100 | let executor = EXECUTOR.put(Executor::new()); | ||
| 101 | executor.set_alarm(alarm); | ||
| 102 | |||
| 103 | let gpioa = p.GPIOA.split(); | ||
| 104 | let usb = USB { | ||
| 105 | usb_global: p.OTG_FS_GLOBAL, | ||
| 106 | usb_device: p.OTG_FS_DEVICE, | ||
| 107 | usb_pwrclk: p.OTG_FS_PWRCLK, | ||
| 108 | pin_dm: gpioa.pa11.into_alternate_af10(), | ||
| 109 | pin_dp: gpioa.pa12.into_alternate_af10(), | ||
| 110 | hclk: clocks.hclk(), | ||
| 111 | }; | ||
| 112 | // Rust analyzer isn't recognizing the static ref magic `cortex-m` does | ||
| 113 | #[allow(unused_unsafe)] | ||
| 114 | let usb_bus = USB_BUS.put(UsbBus::new(usb, unsafe { EP_MEMORY })); | ||
| 115 | |||
| 116 | executor.run(move |spawner| { | ||
| 117 | unwrap!(spawner.spawn(run1(usb_bus))); | ||
| 118 | }); | ||
| 119 | } | ||
