Skip to content

Commit

Permalink
chips: apollo3: update to new UART HIL
Browse files Browse the repository at this point in the history
  • Loading branch information
bradjc committed Sep 30, 2022
1 parent abbceb3 commit f2a1cd9
Showing 1 changed file with 92 additions and 9 deletions.
101 changes: 92 additions & 9 deletions chips/apollo3/src/uart.rs
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,15 @@ impl Uart<'_> {
regs.fbrd.write(FBRD::DIVFRAC.val(fraction_divisor_long));
}

fn get_baud_rate(&self) -> u32 {
let regs = self.registers;

let integer_divisor = regs.ibrd.read(IBRD::DIVINT);

let baud_rate = (self.clock_frequency / integer_divisor) / 16;
baud_rate
}

fn enable_tx_interrupt(&self) {
let regs = self.registers;

Expand Down Expand Up @@ -291,7 +300,71 @@ impl Uart<'_> {
}
}

impl hil::uart::Configuration for Uart<'_> {
fn get_baud_rate(&self) -> u32 {
self.get_baud_rate()
}

fn get_width(&self) -> hil::uart::Width {
hil::uart::Width::Eight
}

fn get_parity(&self) -> hil::uart::Parity {
hil::uart::Parity::None
}

fn get_stop_bits(&self) -> hil::uart::StopBits {
hil::uart::StopBits::One
}

fn get_flow_control(&self) -> bool {
// Hardware flow control not supported
false
}
}

impl hil::uart::Configure for Uart<'_> {
fn set_baud_rate(&self, rate: u32) -> Result<u32, ErrorCode> {
let params = hil::uart::Parameters {
baud_rate: rate,
width: hil::uart::Width::Eight,
parity: hil::uart::Parity::None,
stop_bits: hil::uart::StopBits::One,
hw_flow_control: false,
};

self.configure(params)?;
Ok(self.get_baud_rate())
}

fn set_width(&self, width: hil::uart::Width) -> Result<(), ErrorCode> {
if width != hil::uart::Width::Eight {
Err(ErrorCode::NOSUPPORT)
} else {
Ok(())
}
}

fn set_parity(&self, parity: hil::uart::Parity) -> Result<(), ErrorCode> {
if parity != hil::uart::Parity::None {
Err(ErrorCode::NOSUPPORT)
} else {
Ok(())
}
}

fn set_stop_bits(&self, _stop_bits: hil::uart::StopBits) -> Result<(), ErrorCode> {
Err(ErrorCode::NOSUPPORT)
}

fn set_flow_control(&self, on: bool) -> Result<(), ErrorCode> {
if on {
Err(ErrorCode::NOSUPPORT)
} else {
Ok(())
}
}

fn configure(&self, params: hil::uart::Parameters) -> Result<(), ErrorCode> {
let regs = self.registers;

Expand All @@ -307,7 +380,7 @@ impl hil::uart::Configure for Uart<'_> {

// Setup the UART
regs.cr.modify(CR::RTSEN::CLEAR + CR::CTSEN::CLEAR);
// Enalbe FIFO
// Enable FIFO
regs.lcrh.write(LCRH::FEN::SET);
// Set 8 data bits, no parity, 1 stop bit and no flow control
regs.lcrh.modify(LCRH::WLEN.val(3) + LCRH::FEN::SET);
Expand Down Expand Up @@ -345,12 +418,20 @@ impl<'a> hil::uart::Transmit<'a> for Uart<'a> {
}
}

fn transmit_abort(&self) -> Result<(), ErrorCode> {
Err(ErrorCode::FAIL)
fn transmit_character(&self, _character: u32) -> Result<(), ErrorCode> {
Err(ErrorCode::NOSUPPORT)
}

fn transmit_word(&self, _word: u32) -> Result<(), ErrorCode> {
Err(ErrorCode::FAIL)
fn transmit_abort(&self) -> hil::uart::AbortResult {
// TODO: aborting a transmission is not currently supported.
if self.tx_buffer.is_some() {
// A transmission is currently ongoing, report it has not been
// cancelled.
hil::uart::AbortResult::Callback(false)
} else {
// There is nothing to abort.
hil::uart::AbortResult::NoCallback
}
}
}

Expand All @@ -367,11 +448,13 @@ impl<'a> hil::uart::Receive<'a> for Uart<'a> {
Err((ErrorCode::FAIL, rx_buffer))
}

fn receive_abort(&self) -> Result<(), ErrorCode> {
Err(ErrorCode::FAIL)
fn receive_character(&self) -> Result<(), ErrorCode> {
Err(ErrorCode::NOSUPPORT)
}

fn receive_word(&self) -> Result<(), ErrorCode> {
Err(ErrorCode::FAIL)
fn receive_abort(&self) -> hil::uart::AbortResult {
// Given receive is not supported, this will never have an ongoing
// operation to cancel.
hil::uart::AbortResult::NoCallback
}
}

0 comments on commit f2a1cd9

Please sign in to comment.