Add some UART output (via ST-Link VCP)

This commit is contained in:
2025-10-17 08:06:53 +02:00
parent 6e1c5370ba
commit 98b2c8a0b9

View File

@@ -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();
}
}