judas/src/bsp/rpi3/uart.rs

127 lines
3.4 KiB
Rust
Raw Normal View History

2022-10-14 13:21:36 +02:00
//! Driver for the UART.
use core::arch::asm;
use core::fmt;
use crate::traits::console::{Write, Console};
2022-10-16 17:14:01 +02:00
use super::memory_map::uart as mm;
2022-10-14 13:21:36 +02:00
2022-10-16 17:14:01 +02:00
pub struct Uart<'a> {
2022-10-14 13:21:36 +02:00
initialized: bool,
2022-10-16 17:14:01 +02:00
memory_map: &'a mm::UartMemoryMap,
2022-10-14 13:21:36 +02:00
}
2022-10-16 17:14:01 +02:00
impl<'a> Uart<'a> {
2022-10-14 13:21:36 +02:00
// TODO: not sure this should be public outside of bsp.
/// Constructor for [`Uart`].
2022-10-16 17:14:01 +02:00
pub const fn new(memory_map: &'a mm::UartMemoryMap) -> Self {
Uart { initialized: false, memory_map }
2022-10-14 13:21:36 +02:00
}
/// Initialise the UART.
pub fn init(&mut self) {
// TODO: Recover from possible previous test.
self.flush();
// Stop UART
2022-10-16 17:14:01 +02:00
self.memory_map.cr_uarten.read_and_write(0);
2022-10-14 13:21:36 +02:00
// Flush the FIFOs
2022-10-16 17:14:01 +02:00
self.memory_map.lcrh_fen.read_and_write(0);
2022-10-14 13:21:36 +02:00
// Clear all interrupt
2022-10-16 17:14:01 +02:00
self.memory_map.icr.write_without_read(0);
2022-10-14 13:21:36 +02:00
// Config UART
// 8N1 115_200 bauds.
// divbaud = freq/16/baudrate = 48_000_000 / 16 / 115_200 = 26.041666666666668
// => IBRD = 26
// => FBRD = round(0.041666666666668 * 64) = 3 // TODO: why 64?
2022-10-16 17:14:01 +02:00
self.memory_map.ibrd.write_without_read(26);
self.memory_map.fbrd.write_without_read(3);
2022-10-14 13:21:36 +02:00
// Set word len to 8
2022-10-16 17:14:01 +02:00
let lcrh_val = self.memory_map.lcrh_wlen.read_and_write_to_u32(0b11, 0);
2022-10-14 13:21:36 +02:00
// Reenable the FIFOs
2022-10-16 17:14:01 +02:00
let lcrh_val = self.memory_map.lcrh_fen.read_and_write_to_u32(1, lcrh_val);
self.memory_map.lcrh.write_without_read(lcrh_val);
2022-10-14 13:21:36 +02:00
2022-10-16 17:14:01 +02:00
let cr_val = self.memory_map.cr_txe.read_and_write_to_u32(1, 0);
// TODO: let cr_val = self.memory_map.cr_rxe.read_and_write_to_u32(1, 0); to enable read
self.memory_map.cr.write_without_read(cr_val);
2022-10-14 13:21:36 +02:00
// Start the UART
2022-10-16 17:14:01 +02:00
self.memory_map.cr_uarten.read_and_write(1);
2022-10-14 13:21:36 +02:00
self.initialized = true;
}
/// Test if the UART is busy.
fn is_busy(&self) -> bool {
2022-10-16 17:14:01 +02:00
self.memory_map.fr_busy.read() != 0
2022-10-14 13:21:36 +02:00
}
/// Test if the the TX FIFO is full.
fn is_tx_full(&self) -> bool {
2022-10-16 17:14:01 +02:00
self.memory_map.fr_txff.read() != 0
2022-10-14 13:21:36 +02:00
}
}
// Allow the use of uart for fmt::Write::write_fmt.
2022-10-16 17:14:01 +02:00
impl fmt::Write for Uart<'_> {
2022-10-14 13:21:36 +02:00
fn write_str(&mut self, s: &str) -> fmt::Result {
for c in s.chars() {
if c == '\n' {
Write::write_char(self, '\r');
}
Write::write_char(self, c);
}
Ok(())
}
}
// TODO: add sync
2022-10-16 17:14:01 +02:00
impl Write for Uart<'_> {
2022-10-14 13:21:36 +02:00
fn write_char(&self, c: char) {
if !self.initialized {
//panic!("Cannot write to a non initialized UART");
}
while self.is_tx_full() {
use super::gpio;
let _ = gpio::set_pin_output_state(20, gpio::PinOutputState::High);
unsafe { asm!("nop") };
}
2022-10-16 17:14:01 +02:00
self.memory_map.dr_data.write_without_read(c as u32);
2022-10-14 13:21:36 +02:00
}
fn write_fmt(&self, args: fmt::Arguments) -> fmt::Result {
// TODO: use syncronisation here
2022-10-16 17:14:01 +02:00
let mut new_uart = Uart { initialized: self.initialized, memory_map: self.memory_map};
2022-10-14 13:21:36 +02:00
fmt::Write::write_fmt(&mut new_uart, args)
}
fn flush(&self) {
while self.is_busy() {
unsafe { asm!("nop") };
}
}
}
2022-10-16 17:14:01 +02:00
impl Console for Uart<'_> {}
2022-10-14 13:21:36 +02:00
2022-10-16 17:14:01 +02:00
static UART: Uart = Uart { initialized: true, memory_map: &mm::UART }; // TODO: use sync
2022-10-14 13:21:36 +02:00
pub fn init() {
2022-10-16 17:14:01 +02:00
let mut uart = Uart::new(&mm::UART);
2022-10-14 13:21:36 +02:00
uart.init();
}
// TODO: move?
/// Return a reference to the Uart Output.
pub fn console() -> &'static dyn Console {
&UART
}