-
-
Save cosmikwolf/f2ff77bc6e4d32e7ef8b07f859c6185d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#![deny(warnings)] | |
#![no_std] | |
#![no_main] | |
#[macro_use] | |
mod utilities; | |
#[rtic::app(device = stm32h7xx_hal::stm32, peripherals = true)] | |
mod app { | |
use stm32h7xx_hal::gpio::gpioc::PC7; | |
use stm32h7xx_hal::gpio::{Output, PushPull}; | |
use stm32h7xx_hal::prelude::*; | |
use stm32h7xx_hal::rcc::rec::UsbClkSel; | |
use stm32h7xx_hal::usb_hs::{UsbBus, USB2}; | |
use usb_device::prelude::*; | |
static mut EP_MEMORY2: [u32; 1024] = [0; 1024]; | |
use super::utilities; | |
#[shared] | |
struct SharedResources {} | |
#[local] | |
struct LocalResources { | |
usb2: ( | |
UsbDevice<'static, UsbBus<USB2>>, | |
usbd_serial::SerialPort<'static, UsbBus<USB2>>, | |
), | |
led: PC7<Output<PushPull>>, | |
} | |
#[init] | |
fn init(ctx: init::Context) -> (SharedResources, LocalResources, init::Monotonics) { | |
utilities::logger::init(); | |
let pwr = ctx.device.PWR.constrain(); | |
let pwrcfg = example_power!(pwr).freeze(); | |
// RCC | |
let rcc = ctx.device.RCC.constrain(); | |
let mut ccdr = rcc.sys_ck(80.MHz()).freeze(pwrcfg, &ctx.device.SYSCFG); | |
// 48MHz CLOCK | |
let _ = ccdr.clocks.hsi48_ck().expect("HSI48 must run"); | |
ccdr.peripheral.kernel_usb_clk_mux(UsbClkSel::Hsi48); | |
// LED | |
let led = ctx.device.GPIOC.split(ccdr.peripheral.GPIOC).pc7; | |
// IO | |
// let gpiob = ctx.device.GPIOB.split(ccdr.peripheral.GPIOB); | |
let gpioa = ctx.device.GPIOA.split(ccdr.peripheral.GPIOA); | |
let _pa10 = gpioa.pa10.into_alternate::<10>(); | |
let usb2 = USB2::new( | |
ctx.device.OTG2_HS_GLOBAL, | |
ctx.device.OTG2_HS_DEVICE, | |
ctx.device.OTG2_HS_PWRCLK, | |
gpioa.pa11.into_alternate::<10>(), | |
gpioa.pa12.into_alternate::<10>(), | |
ccdr.peripheral.USB2OTG, | |
&ccdr.clocks, | |
); | |
let usb_bus2 = cortex_m::singleton!( | |
: usb_device::class_prelude::UsbBusAllocator<UsbBus<USB2>> = | |
UsbBus::new(usb2, unsafe { &mut EP_MEMORY2 }) | |
) | |
.unwrap(); | |
let serial2 = usbd_serial::SerialPort::new(usb_bus2); | |
let usb_dev2 = UsbDeviceBuilder::new(usb_bus2, UsbVidPid(0x16c0, 0x27dd)) | |
.manufacturer("Fake company") | |
.product("Serial port") | |
.serial_number("TSSasasEST") | |
.device_class(usbd_serial::USB_CLASS_CDC) | |
.build(); | |
let usb2 = (usb_dev2, serial2); | |
( | |
SharedResources {}, | |
LocalResources { | |
// usb, | |
usb2, | |
led: led.into_push_pull_output(), | |
}, | |
init::Monotonics(), | |
) | |
} | |
#[task(binds = OTG_HS, local = [usb2,led])] | |
fn usb_event(mut ctx: usb_event::Context) { | |
// let (usb_dev, serial) = &mut ctx.local.usb; | |
ctx.local.led.set_high(); | |
let (usb_dev, serial) = &mut ctx.local.usb2; | |
ctx.local.led.set_high(); | |
loop { | |
if !usb_dev.poll(&mut [serial]) { | |
ctx.local.led.set_low(); | |
return; | |
} | |
let mut buf = [0u8; 64]; | |
match serial.read(&mut buf) { | |
Ok(count) if count > 0 => { | |
// Echo back in upper case | |
for c in buf[0..count].iter_mut() { | |
if 0x61 <= *c && *c <= 0x7a { | |
*c &= !0x20; | |
} | |
} | |
let mut write_offset = 0; | |
while write_offset < count { | |
match serial.write(&buf[write_offset..count]) { | |
Ok(len) if len > 0 => { | |
write_offset += len; | |
} | |
_ => {} | |
} | |
} | |
} | |
_ => {} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment