Skip to main content

serial/
serial.rs

1#![no_std]
2#![no_main]
3
4use core::fmt::Write;
5use core::panic::PanicInfo;
6
7use hal_mik32::gpio::port_1::{Pin08, Pin09};
8use hal_mik32::rcc::RCC;
9use hal_mik32::usart::{Config, Serial};
10use mik32_pac::Peripherals;
11
12const MESSAGE_DELAY_SPINS: u32 = 500_000;
13
14#[unsafe(no_mangle)]
15pub extern "C" fn main() -> ! {
16    let peripherals = Peripherals::take().unwrap();
17
18    let rcc_config = RCC::default();
19    RCC::init(&rcc_config);
20
21    peripherals
22        .pm
23        .clk_apb_m_set()
24        .modify(|_, w| w.pad_config().enable().pm().enable());
25
26    let rx = Pin08::new().into_serial_port();
27    let tx = Pin09::new().into_serial_port();
28
29    let serial = Serial::new(peripherals.usart_1, (tx, rx), Config::default());
30    let (mut tx, _rx) = serial.split();
31
32    loop {
33        let _ = writeln!(tx, "Hello from MIK32 USART1");
34        delay(MESSAGE_DELAY_SPINS);
35    }
36}
37
38#[inline(always)]
39fn delay(spins: u32) {
40    for _ in 0..spins {
41        core::hint::spin_loop();
42    }
43}
44
45#[panic_handler]
46fn panic(_info: &PanicInfo) -> ! {
47    loop {
48        core::hint::spin_loop();
49    }
50}
51
52#[unsafe(no_mangle)]
53#[inline(never)]
54pub extern "C" fn trap_handler() {
55    loop {
56        core::hint::spin_loop();
57    }
58}