From 98b2c8a0b93e586a6da497ff69a750ac89fc764b Mon Sep 17 00:00:00 2001 From: jazzpi Date: Fri, 17 Oct 2025 08:06:53 +0200 Subject: [PATCH] Add some UART output (via ST-Link VCP) --- src/main.rs | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/src/main.rs b/src/main.rs index 588522d..439ff8a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,6 +1,8 @@ #![no_std] #![no_main] +use core::fmt::Write; + // pick a panicking behavior use panic_halt as _; // you can put a breakpoint on `rust_begin_unwind` to catch panics // use panic_abort as _; // requires nightly @@ -10,17 +12,24 @@ use panic_halt as _; // you can put a breakpoint on `rust_begin_unwind` to catch use cortex_m::peripheral::{syst, Peripherals}; use cortex_m_rt::entry; +use stm32f3xx_hal::serial::Serial; use stm32f3xx_hal::{self as hal, pac, prelude::*}; #[entry] fn main() -> ! { let cortex_peripherals = Peripherals::take().unwrap(); + let dp = pac::Peripherals::take().unwrap(); + let mut rcc = dp.RCC.constrain(); + let mut flash = dp.FLASH.constrain(); + let clocks = rcc.cfgr.sysclk(8.MHz()).freeze(&mut flash.acr); + + // The HAL Serial example does this -- but where does Systick come from? + // https://github.com/stm32-rs/stm32f3xx-hal/blob/b0cead18208099b94a085fb634d0db4e75ac6e4d/examples/serial_echo_rtic.rs + // let mono = Systick::new(systick, 8_000_000); let mut systick = cortex_peripherals.SYST; systick.set_clock_source(syst::SystClkSource::Core); systick.set_reload(8_000_000); // 1s systick.clear_current(); - let dp = pac::Peripherals::take().unwrap(); - let mut rcc = dp.RCC.constrain(); // PE9: LD3 (red) // PE8: LD4 (blue) @@ -36,6 +45,17 @@ fn main() -> ! { .pe10 .into_push_pull_output(&mut gpioe.moder, &mut gpioe.otyper); + let mut gpioc = dp.GPIOC.split(&mut rcc.ahb); + let pins = ( + gpioc + .pc4 + .into_af_push_pull(&mut gpioc.moder, &mut gpioc.otyper, &mut gpioc.afrl), + gpioc + .pc5 + .into_af_push_pull(&mut gpioc.moder, &mut gpioc.otyper, &mut gpioc.afrl), + ); + + let mut uart = Serial::new(dp.USART1, pins, 115200.Bd(), clocks, &mut rcc.apb2); systick.enable_counter(); let mut enabled = true; loop { @@ -44,5 +64,6 @@ fn main() -> ! { ld3.toggle().unwrap(); ld4.toggle().unwrap(); ld5.toggle().unwrap(); + uart.write_str("Loop\r\n").unwrap(); } }