2019-05-07 22:45:31 +08:00
|
|
|
use core::fmt;
|
2020-04-25 06:28:17 +08:00
|
|
|
use void::Void;
|
2019-05-07 22:45:31 +08:00
|
|
|
|
2019-12-18 06:35:58 +08:00
|
|
|
use libregister::*;
|
2019-10-22 04:19:03 +08:00
|
|
|
use super::slcr;
|
2020-01-24 05:44:10 +08:00
|
|
|
use super::clocks::Clocks;
|
2019-05-07 23:46:37 +08:00
|
|
|
|
2019-05-05 20:56:23 +08:00
|
|
|
mod regs;
|
2019-05-22 07:42:00 +08:00
|
|
|
mod baud_rate_gen;
|
2019-05-07 23:46:37 +08:00
|
|
|
|
2019-05-05 20:56:23 +08:00
|
|
|
pub struct Uart {
|
2019-05-07 23:46:37 +08:00
|
|
|
regs: &'static mut regs::RegisterBlock,
|
2019-05-05 20:56:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Uart {
|
2019-05-25 08:38:05 +08:00
|
|
|
#[cfg(feature = "target_cora_z7_10")]
|
2020-08-12 16:27:17 +08:00
|
|
|
pub fn uart0(baudrate: u32) -> Self {
|
2019-11-07 05:59:17 +08:00
|
|
|
slcr::RegisterBlock::unlocked(|slcr| {
|
|
|
|
// Route UART 0 RxD/TxD Signals to MIO Pins
|
|
|
|
// TX pin
|
|
|
|
slcr.mio_pin_15.write(
|
|
|
|
slcr::MioPin15::zeroed()
|
|
|
|
.l3_sel(0b111)
|
|
|
|
.io_type(slcr::IoBufferType::Lvcmos33)
|
|
|
|
.pullup(true)
|
|
|
|
);
|
|
|
|
// RX pin
|
|
|
|
slcr.mio_pin_14.write(
|
|
|
|
slcr::MioPin14::zeroed()
|
|
|
|
.tri_enable(true)
|
|
|
|
.l3_sel(0b111)
|
|
|
|
.io_type(slcr::IoBufferType::Lvcmos33)
|
|
|
|
.pullup(true)
|
|
|
|
);
|
|
|
|
});
|
2019-05-25 08:38:05 +08:00
|
|
|
|
2019-11-07 05:59:17 +08:00
|
|
|
slcr::RegisterBlock::unlocked(|slcr| {
|
|
|
|
slcr.uart_rst_ctrl.reset_uart0();
|
|
|
|
slcr.aper_clk_ctrl.enable_uart0();
|
|
|
|
slcr.uart_clk_ctrl.enable_uart0();
|
|
|
|
});
|
2019-05-25 08:38:05 +08:00
|
|
|
let mut self_ = Uart {
|
|
|
|
regs: regs::RegisterBlock::uart0(),
|
|
|
|
};
|
|
|
|
self_.configure(baudrate);
|
|
|
|
self_
|
|
|
|
}
|
|
|
|
|
2020-08-12 16:27:17 +08:00
|
|
|
#[cfg(feature = "target_zc706")]
|
2019-05-25 08:38:05 +08:00
|
|
|
pub fn uart1(baudrate: u32) -> Self {
|
2020-08-12 16:27:17 +08:00
|
|
|
slcr::RegisterBlock::unlocked(|slcr| {
|
|
|
|
// Route UART 1 RxD/TxD Signals to MIO Pins
|
|
|
|
// TX pin
|
|
|
|
slcr.mio_pin_48.write(
|
|
|
|
slcr::MioPin48::zeroed()
|
|
|
|
.l3_sel(0b111)
|
|
|
|
.io_type(slcr::IoBufferType::Lvcmos18)
|
|
|
|
.pullup(true)
|
|
|
|
);
|
|
|
|
// RX pin
|
|
|
|
slcr.mio_pin_49.write(
|
|
|
|
slcr::MioPin49::zeroed()
|
|
|
|
.tri_enable(true)
|
|
|
|
.l3_sel(0b111)
|
|
|
|
.io_type(slcr::IoBufferType::Lvcmos18)
|
|
|
|
.pullup(true)
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2019-11-07 05:59:17 +08:00
|
|
|
slcr::RegisterBlock::unlocked(|slcr| {
|
|
|
|
slcr.uart_rst_ctrl.reset_uart1();
|
|
|
|
slcr.aper_clk_ctrl.enable_uart1();
|
|
|
|
slcr.uart_clk_ctrl.enable_uart1();
|
|
|
|
});
|
2019-05-24 00:01:18 +08:00
|
|
|
let mut self_ = Uart {
|
2019-05-21 07:30:54 +08:00
|
|
|
regs: regs::RegisterBlock::uart1(),
|
2019-05-07 23:46:37 +08:00
|
|
|
};
|
2019-05-23 21:50:53 +08:00
|
|
|
self_.configure(baudrate);
|
2019-05-07 23:46:37 +08:00
|
|
|
self_
|
2019-05-05 20:56:23 +08:00
|
|
|
}
|
|
|
|
|
2019-05-24 00:01:18 +08:00
|
|
|
pub fn write_byte(&mut self, value: u8) {
|
2019-05-07 23:46:37 +08:00
|
|
|
while self.tx_fifo_full() {}
|
|
|
|
|
|
|
|
self.regs.tx_rx_fifo.write(
|
|
|
|
regs::TxRxFifo::zeroed()
|
|
|
|
.data(value.into())
|
|
|
|
);
|
2019-05-05 20:56:23 +08:00
|
|
|
}
|
|
|
|
|
2019-05-24 00:01:18 +08:00
|
|
|
pub fn configure(&mut self, baudrate: u32) {
|
2019-05-22 07:42:00 +08:00
|
|
|
// Configure UART character frame
|
2019-05-07 23:46:37 +08:00
|
|
|
// * Disable clock-divider
|
|
|
|
// * 8-bit
|
|
|
|
// * 1 stop bit
|
|
|
|
// * Normal channel mode
|
2019-05-22 07:42:00 +08:00
|
|
|
// * No parity
|
2019-05-07 23:46:37 +08:00
|
|
|
self.regs.mode.write(
|
|
|
|
regs::Mode::zeroed()
|
2019-05-24 00:23:51 +08:00
|
|
|
.par(regs::ParityMode::None)
|
|
|
|
.chmode(regs::ChannelMode::Normal)
|
2019-05-07 23:46:37 +08:00
|
|
|
);
|
|
|
|
|
|
|
|
// Configure the Baud Rate
|
|
|
|
self.disable_rx();
|
|
|
|
self.disable_tx();
|
|
|
|
|
2020-01-24 05:44:10 +08:00
|
|
|
let clocks = Clocks::get();
|
2019-08-17 08:55:56 +08:00
|
|
|
baud_rate_gen::configure(self.regs, clocks.uart_ref_clk(), baudrate);
|
2019-05-07 23:46:37 +08:00
|
|
|
|
2019-05-21 07:30:54 +08:00
|
|
|
// Enable controller
|
2019-05-07 23:46:37 +08:00
|
|
|
self.reset_rx();
|
|
|
|
self.reset_tx();
|
2019-05-21 08:53:59 +08:00
|
|
|
self.wait_reset();
|
2019-05-07 23:46:37 +08:00
|
|
|
self.enable_rx();
|
|
|
|
self.enable_tx();
|
2019-05-21 07:30:54 +08:00
|
|
|
|
|
|
|
self.set_rx_timeout(false);
|
|
|
|
self.set_break(false, true);
|
2019-05-07 23:46:37 +08:00
|
|
|
}
|
|
|
|
|
2019-05-24 00:01:18 +08:00
|
|
|
fn disable_rx(&mut self) {
|
2019-05-07 23:46:37 +08:00
|
|
|
self.regs.control.modify(|_, w| {
|
|
|
|
w.rxen(false)
|
|
|
|
.rxdis(true)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-05-24 00:01:18 +08:00
|
|
|
fn disable_tx(&mut self) {
|
2019-05-07 23:46:37 +08:00
|
|
|
self.regs.control.modify(|_, w| {
|
|
|
|
w.txen(false)
|
|
|
|
.txdis(true)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-05-24 00:01:18 +08:00
|
|
|
fn enable_rx(&mut self) {
|
2019-05-07 23:46:37 +08:00
|
|
|
self.regs.control.modify(|_, w| {
|
|
|
|
w.rxen(true)
|
|
|
|
.rxdis(false)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-05-24 00:01:18 +08:00
|
|
|
fn enable_tx(&mut self) {
|
2019-05-07 23:46:37 +08:00
|
|
|
self.regs.control.modify(|_, w| {
|
|
|
|
w.txen(true)
|
|
|
|
.txdis(false)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-05-24 00:01:18 +08:00
|
|
|
fn reset_rx(&mut self) {
|
2019-05-07 23:46:37 +08:00
|
|
|
self.regs.control.modify(|_, w| {
|
|
|
|
w.rxrst(true)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-05-24 00:01:18 +08:00
|
|
|
fn reset_tx(&mut self) {
|
2019-05-07 23:46:37 +08:00
|
|
|
self.regs.control.modify(|_, w| {
|
|
|
|
w.txrst(true)
|
|
|
|
})
|
|
|
|
}
|
2019-05-05 20:56:23 +08:00
|
|
|
|
2019-05-21 08:53:59 +08:00
|
|
|
/// Wait for `reset_rx()` or `reset_tx()` to complete
|
|
|
|
fn wait_reset(&self) {
|
|
|
|
let mut pending = true;
|
|
|
|
while pending {
|
|
|
|
let control = self.regs.control.read();
|
|
|
|
pending = control.rxrst() || control.txrst();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-24 00:01:18 +08:00
|
|
|
fn set_break(&mut self, startbrk: bool, stopbrk: bool) {
|
2019-05-21 07:30:54 +08:00
|
|
|
self.regs.control.modify(|_, w| {
|
|
|
|
w.sttbrk(startbrk)
|
|
|
|
.stpbrk(stopbrk)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// 0 disables
|
2019-05-24 00:01:18 +08:00
|
|
|
fn set_rx_timeout(&mut self, enable: bool) {
|
2019-05-21 07:30:54 +08:00
|
|
|
self.regs.control.modify(|_, w| {
|
|
|
|
w.rstto(enable)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-05-07 23:46:37 +08:00
|
|
|
pub fn tx_fifo_full(&self) -> bool {
|
|
|
|
self.regs.channel_sts.read().txfull()
|
2019-05-05 20:56:23 +08:00
|
|
|
}
|
2019-05-23 21:36:34 +08:00
|
|
|
|
2020-05-03 05:30:45 +08:00
|
|
|
pub fn tx_idle(&self) -> bool {
|
|
|
|
let status = self.regs.channel_sts.read();
|
|
|
|
status.txempty() && !status.tactive()
|
2019-05-23 21:36:34 +08:00
|
|
|
}
|
2019-05-05 20:56:23 +08:00
|
|
|
}
|
2019-05-07 22:45:31 +08:00
|
|
|
|
|
|
|
impl fmt::Write for Uart {
|
|
|
|
fn write_str(&mut self, s: &str) -> Result<(), fmt::Error> {
|
|
|
|
for b in s.bytes() {
|
|
|
|
self.write_byte(b);
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
2020-04-25 03:31:37 +08:00
|
|
|
|
|
|
|
/// embedded_hal async API
|
|
|
|
impl embedded_hal::serial::Write<u8> for Uart {
|
2020-04-25 06:28:17 +08:00
|
|
|
type Error = Void;
|
2020-04-25 03:31:37 +08:00
|
|
|
|
2020-04-25 06:28:17 +08:00
|
|
|
fn write(&mut self, b: u8) -> nb::Result<(), Void> {
|
2020-04-25 03:31:37 +08:00
|
|
|
if self.tx_fifo_full() {
|
|
|
|
Err(nb::Error::WouldBlock)
|
|
|
|
} else {
|
|
|
|
self.write_byte(b);
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-25 06:28:17 +08:00
|
|
|
fn flush(&mut self) -> nb::Result<(), Void> {
|
2020-05-03 05:32:01 +08:00
|
|
|
if self.tx_idle() {
|
2020-04-25 03:31:37 +08:00
|
|
|
Ok(())
|
|
|
|
} else {
|
|
|
|
Err(nb::Error::WouldBlock)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// embedded_hal sync API
|
|
|
|
impl embedded_hal::blocking::serial::write::Default<u8> for Uart {}
|