-
Notifications
You must be signed in to change notification settings - Fork 10
/
debug.rs
60 lines (48 loc) · 1.55 KB
/
debug.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
//! The debug module.
//!
//! See-also: https://github.com/openwch/ch32v003/blob/main/EVT/EXAM/SDI_Printf/SDI_Printf/Debug/debug.c
use qingke::riscv;
const DEBUG_DATA0_ADDRESS: *mut u32 = 0xE000_00F4 as *mut u32;
const DEBUG_DATA1_ADDRESS: *mut u32 = 0xE000_00F8 as *mut u32;
pub struct SDIPrint;
impl SDIPrint {
pub fn enable() {
unsafe {
// Enable SDI print
core::ptr::write_volatile(DEBUG_DATA0_ADDRESS, 0);
riscv::asm::delay(100000);
}
}
#[inline]
fn is_busy() -> bool {
unsafe { core::ptr::read_volatile(DEBUG_DATA0_ADDRESS) != 0 }
}
}
impl core::fmt::Write for SDIPrint {
fn write_str(&mut self, s: &str) -> core::fmt::Result {
let mut data = [0u8; 8];
for chunk in s.as_bytes().chunks(7) {
data[1..chunk.len() + 1].copy_from_slice(chunk);
data[0] = chunk.len() as u8;
// data1 is the last 4 bytes of data
let data1 = u32::from_le_bytes(data[4..].try_into().unwrap());
let data0 = u32::from_le_bytes(data[..4].try_into().unwrap());
while SDIPrint::is_busy() {}
unsafe {
core::ptr::write_volatile(DEBUG_DATA1_ADDRESS, data1);
core::ptr::write_volatile(DEBUG_DATA0_ADDRESS, data0);
}
}
Ok(())
}
}
#[macro_export]
macro_rules! println {
($($arg:tt)*) => {
{
use core::fmt::Write;
use core::writeln;
writeln!(&mut $crate::debug::SDIPrint, $($arg)*).unwrap();
}
}
}