-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
249 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
#![no_std] | ||
#![no_main] | ||
|
||
use ch32v_rt::highcode; | ||
use ch58x_hal as hal; | ||
use embedded_hal_1::delay::DelayUs; | ||
use hal::gpio::{Level, Output, OutputDrive}; | ||
use hal::peripherals; | ||
use hal::rtc::Rtc; | ||
use hal::uart::{UartRx, UartTx}; | ||
|
||
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(); | ||
} | ||
} | ||
} | ||
} | ||
|
||
macro_rules! print { | ||
($($arg:tt)*) => { | ||
unsafe { | ||
use core::fmt::Write; | ||
use core::write; | ||
|
||
if let Some(uart) = SERIAL.as_mut() { | ||
write!(uart, $($arg)*).unwrap(); | ||
} | ||
} | ||
} | ||
} | ||
|
||
#[panic_handler] | ||
fn panic(info: &core::panic::PanicInfo) -> ! { | ||
use core::fmt::Write; | ||
|
||
let pa9 = unsafe { peripherals::PA9::steal() }; | ||
let uart1 = unsafe { peripherals::UART1::steal() }; | ||
let mut serial = UartTx::new(uart1, pa9, Default::default()).unwrap(); | ||
|
||
let _ = writeln!(&mut serial, "\n\n\n{}", info); | ||
|
||
loop {} | ||
} | ||
|
||
#[ch32v_rt::entry] | ||
#[highcode] | ||
fn main() -> ! { | ||
let mut config = hal::Config::default(); | ||
config.clock.use_pll_60mhz().enable_lse(); | ||
let p = hal::init(config); | ||
|
||
// GPIO | ||
let mut led = Output::new(p.PB4, Level::Low, OutputDrive::Standard); | ||
// let boot_btn = Input::new(p.PB22, Pull::Up); | ||
// let rst_btn = Input::new(p.PB23, Pull::Up); | ||
|
||
let uart = UartTx::new(p.UART1, p.PA9, Default::default()).unwrap(); | ||
unsafe { | ||
SERIAL.replace(uart); | ||
} | ||
|
||
let mut rx = UartRx::new(p.UART2, p.PA6, Default::default()).unwrap(); | ||
|
||
let rtc = Rtc::new(p.RTC); | ||
|
||
println!("\n\nHello World!"); | ||
println!("System Clocks: {}", hal::sysctl::clocks().hclk); | ||
println!("ChipID: 0x{:02x}", hal::signature::get_chip_id()); | ||
println!("RTC datetime: {}", rtc.now()); | ||
println!("Now, type something to echo:"); | ||
|
||
loop { | ||
let b = nb::block!(rx.nb_read()).unwrap(); | ||
if b == '\r' as u8 { | ||
println!(); | ||
} else { | ||
print!("{}", b as char); | ||
} | ||
|
||
led.toggle(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters