ch58x-hal 0.0.2

HAL for the CH583/CH582/CH581 RISC-V BLE microcotrollers from WCH
Documentation
#![no_std]
#![no_main]

use embedded_hal_1::delay::DelayNs;
use hal::gpio::{Input, Level, Output, OutputDrive, Pull};
use hal::rtc::{DateTime, Rtc};
use hal::uart::UartTx;
use hal::{peripherals, with_safe_access};
use qingke_rt::highcode;
use {ch58x_hal as hal, panic_halt as _};

static mut SERIAL: Option<UartTx<peripherals::UART1>> = None;

macro_rules! println {
    ($($arg:tt)*) => {
        unsafe {
            use core::fmt::Write;
            use core::writeln;

            if let Some(uart) = SERIAL.as_mut() {
                writeln!(uart, $($arg)*).unwrap();
            }
        }
    }
}

#[no_mangle]
extern "C" fn RTC() {
    let mut rtc = Rtc;

    println!("Entering IRQ...");
    rtc.ack_timing();

    let now = rtc.now();
    println!("Current time: {} weekday={}", now, now.isoweekday());
}

#[qingke_rt::entry]
#[highcode]
fn main() -> ! {
    let p = hal::init(Default::default());

    // GPIO
    let mut led = Output::new(p.PA8, Level::Low, OutputDrive::_5mA);
    let download_button = Input::new(p.PB22, Pull::Up);
    let reset_button = Input::new(p.PB23, Pull::Up);

    let uart = UartTx::new(p.UART1, p.PA9, Default::default()).unwrap();
    unsafe {
        SERIAL.replace(uart);
    }

    let mut rtc = Rtc::new(p.RTC);
    rtc.set_datatime(DateTime {
        year: 2023,
        month: 10,
        day: 25,
        hour: 23,
        minute: 54,
        second: 30,
        millisecond: 0,
    });

    println!("\nHello World!");
    println!("System Clocks: {}", hal::sysctl::clocks().hclk);
    println!("ChipID: 0x{:02x}", hal::signature::get_chip_id());
    println!("RTC datetime: {}", rtc.now());

    unsafe {
        use ch58x_hal::interrupt::Interrupt;
        hal::interrupt::RTC::enable();
    }

    rtc.enable_timing(hal::rtc::TimingMode::_2S);

    loop {
        led.toggle();
        println!("tick");

        hal::delay_ms(1000);
    }
}