Skip to content

Instantly share code, notes, and snippets.

@bsodmike

bsodmike/main.rs Secret

Last active February 2, 2024 07:50
Show Gist options
  • Save bsodmike/42a4d83b4c80f870b4195b9adb1c8e15 to your computer and use it in GitHub Desktop.
Save bsodmike/42a4d83b4c80f870b4195b9adb1c8e15 to your computer and use it in GitHub Desktop.
Attempting USB Serial with the stm32h7xx_hal example on an Ardunio GIGA Wifi R1 board (it has a Cortex M4/M7 chip STM32H747XI, same as the Portenta H7) - the Blue LED blinks, but there is no USB device listed in /dev/cu.*
//! Basic example that produces a 1Hz square-wave on Pin PE1
// #![deny(warnings)]
#![no_main]
#![no_std]
use core::mem::MaybeUninit;
use chrono::prelude::*;
use cortex_m_rt::entry;
use log::info;
use stm32h7xx_hal::rcc::rec::UsbClkSel;
use stm32h7xx_hal::usb_hs::{UsbBus, USB1};
use stm32h7xx_hal::{pac, prelude::*, rtc, stm32};
use usb_device::prelude::*;
use core::fmt::Write;
use nb::block;
use core::panic::PanicInfo;
/// This function is called on panic.
#[panic_handler]
fn panic(_info: &PanicInfo) -> ! {
loop {}
}
static mut EP_MEMORY: MaybeUninit<[u32; 1024]> = MaybeUninit::uninit();
#[entry]
fn main() -> ! {
let cp = cortex_m::Peripherals::take().unwrap();
let dp = pac::Peripherals::take().unwrap();
// Constrain and Freeze power
info!("Setup PWR... ");
let pwr = dp.PWR.constrain();
let mut pwrcfg = pwr.ldo().freeze();
// Take the backup power domain
let backup = pwrcfg.backup().unwrap();
// Constrain and Freeze clock
info!("Setup RCC... ");
let rcc = dp.RCC.constrain();
let mut ccdr = rcc.sys_ck(80.MHz()).freeze(pwrcfg, &dp.SYSCFG);
// 48MHz CLOCK
let _ = ccdr.clocks.hsi48_ck().expect("HSI48 must run");
ccdr.peripheral.kernel_usb_clk_mux(UsbClkSel::Hsi48);
let (pin_dm, pin_dp) = {
let gpiob = dp.GPIOB.split(ccdr.peripheral.GPIOB);
(gpiob.pb14.into_alternate(), gpiob.pb15.into_alternate())
};
let usb = USB1::new(
dp.OTG1_HS_GLOBAL,
dp.OTG1_HS_DEVICE,
dp.OTG1_HS_PWRCLK,
pin_dm,
pin_dp,
ccdr.peripheral.USB1OTG,
&ccdr.clocks,
);
// Initialise EP_MEMORY to zero
unsafe {
let buf: &mut [MaybeUninit<u32>; 1024] =
&mut *(core::ptr::addr_of_mut!(EP_MEMORY) as *mut _);
for value in buf.iter_mut() {
value.as_mut_ptr().write(0);
}
}
// Now we may assume that EP_MEMORY is initialised
let usb_bus = UsbBus::new(usb, unsafe { EP_MEMORY.assume_init_mut() });
let mut serial = usbd_serial::SerialPort::new(&usb_bus);
let mut usb_dev = UsbDeviceBuilder::new(&usb_bus, UsbVidPid(0x16c0, 0x27dd))
.strings(&[usb_device::device::StringDescriptors::default()
.manufacturer("Fake company")
.product("Serial port")
.serial_number("TEST PORT 1")])
.unwrap()
.device_class(usbd_serial::USB_CLASS_CDC)
.build();
info!("");
info!("stm32h7xx-hal example - Blinky");
info!("");
let gpioe = dp.GPIOE.split(ccdr.peripheral.GPIOE);
// Configure PE3 as output.
let mut led = gpioe.pe3.into_push_pull_output();
// Get the delay provider.
let mut delay = cp.SYST.delay(ccdr.clocks);
// let mut rtc = rtc::Rtc::open_or_init(dp.RTC, backup.RTC, rtc::RtcClock::Lsi, &ccdr.clocks);
// // TODO: Get current time from some source
// let now = NaiveDate::from_ymd_opt(2024, 1, 29)
// .unwrap()
// .and_hms_opt(0, 0, 0)
// .unwrap();
// rtc.set_date_time(now);
// info!("Time: {}", rtc.date_time().unwrap());
loop {
led.set_high();
delay.delay_ms(100_u16);
led.set_low();
delay.delay_ms(100_u16);
if !usb_dev.poll(&mut [&mut serial]) {
continue;
}
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