79 lines
1.9 KiB
Rust
79 lines
1.9 KiB
Rust
use core::ptr::{read_volatile, write_volatile};
|
|
use core::fmt;
|
|
|
|
const UART_CONTROL: *mut u32 = (0x02000000) as *mut u32;
|
|
const UART_FLAGS : *mut u32 = (0x02000004) as *mut u32;
|
|
const UART_TXDATA : *mut u32 = (0x02000008) as *mut u32;
|
|
const UART_RXDATA : *mut u32 = (0x0200000C) as *mut u32;
|
|
|
|
pub fn uart_set_div(div: u32) {
|
|
unsafe {
|
|
write_volatile(UART_CONTROL, div as u32);
|
|
}
|
|
}
|
|
pub fn uart_get_div() -> u32 {
|
|
unsafe {
|
|
return read_volatile(UART_CONTROL) as u32
|
|
}
|
|
}
|
|
pub fn uart_enable_rx() {
|
|
unsafe {
|
|
let bitmasked = read_volatile(UART_FLAGS) & (!0x0100) as u32;
|
|
write_volatile(UART_FLAGS, (0x0100 | bitmasked) as u32);
|
|
}
|
|
}
|
|
pub fn uart_disable_rx() {
|
|
unsafe {
|
|
let bitmasked = read_volatile(UART_FLAGS) & (!0x0100) as u32;
|
|
write_volatile(UART_FLAGS, (0x0000 | bitmasked) as u32);
|
|
}
|
|
}
|
|
pub fn uart_get_rxready() -> u8 {
|
|
unsafe {
|
|
let full = read_volatile(UART_FLAGS) as u32;
|
|
return (full & 0x0001) as u8;
|
|
}
|
|
}
|
|
pub fn uart_read_byte() -> u8 {
|
|
unsafe {
|
|
return read_volatile(UART_RXDATA) as u8;
|
|
}
|
|
}
|
|
pub fn uart_get_txready() -> u8 {
|
|
unsafe {
|
|
let full = read_volatile(UART_FLAGS) as u32;
|
|
return ((full & 0x0010) >> 4) as u8;
|
|
}
|
|
}
|
|
pub fn uart_write_byte(c: u8) {
|
|
unsafe {
|
|
write_volatile(UART_TXDATA, c as u32);
|
|
}
|
|
}
|
|
|
|
pub struct Console;
|
|
|
|
impl fmt::Write for Console {
|
|
fn write_str(&mut self, s: &str) -> Result<(), fmt::Error> {
|
|
for c in s.bytes() {
|
|
while uart_get_txready() == 0 {}
|
|
uart_write_byte(c);
|
|
}
|
|
Ok(())
|
|
}
|
|
}
|
|
|
|
#[macro_export]
|
|
macro_rules! print {
|
|
($($arg:tt)*) => ({
|
|
use core::fmt::Write;
|
|
write!($crate::uart::Console, $($arg)*).unwrap()
|
|
})
|
|
}
|
|
|
|
#[macro_export]
|
|
macro_rules! println {
|
|
($fmt:expr) => (print!(concat!($fmt, "\n")));
|
|
($fmt:expr, $($arg:tt)*) => (print!(concat!($fmt, "\n"), $($arg)*));
|
|
}
|