use pastey::paste;
use crate::gpio::{
Uart0CtsPin, Uart0RtsPin, Uart0RxPin, Uart0TxPin, Uart1CtsPin, Uart1RtsPin, Uart1RxPin,
Uart1TxPin,
};
use crate::reset::Resettable;
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
#[repr(u8)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub enum DataBits {
Five = 0,
Six = 1,
Seven = 2,
Eight = 3,
}
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub enum StopBits {
One,
Two,
}
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub enum Parity {
None,
Even,
Odd,
}
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub enum Error {
Overrun,
ParityError,
FramingError,
BreakError,
Timeout,
}
macro_rules! impl_uart {
($UartName:ident, $uart_num:literal, $UartType:ty, $uart_mod:ident, $TxPin:ident, $RxPin:ident, $CtsPin:ident, $RtsPin:ident) => {
paste! {
#[doc = concat!("# UART", $uart_num, " Pin Mapping")]
#[doc = concat!("See [`", stringify!($TxPin), "`] for valid TX pins.")]
#[doc = concat!("See [`", stringify!($RxPin), "`] for valid RX pins.")]
pub struct $UartName<TX, RX>
where
TX: $TxPin,
RX: $RxPin,
{
uart: $UartType,
_tx: TX,
_rx: RX,
}
impl<TX, RX> $UartName<TX, RX>
where
TX: $TxPin,
RX: $RxPin,
{
pub const NUM: u8 = $uart_num;
#[doc = concat!("- TX must implement [`", stringify!($TxPin), "`]")]
#[doc = concat!("- RX must implement [`", stringify!($RxPin), "`]")]
#[doc = concat!("let uart = ", stringify!($UartName), "::init(")]
#[doc = concat!(" dp.UART", $uart_num, ",")]
pub fn init(uart: $UartType, mut tx: TX, mut rx: RX, baudrate: u32, peri_clk: u32) -> Self {
tx.[<configure_uart $uart_num _tx>]();
rx.[<configure_uart $uart_num _rx>]();
<$UartType as Resettable>::init();
let mut uart_dev = Self {
uart,
_tx: tx,
_rx: rx,
};
uart_dev.configure_baudrate(baudrate, peri_clk);
uart_dev.set_format(DataBits::Eight, StopBits::One, Parity::None);
uart_dev.uart.uartcr.set_uarten();
uart_dev.uart.uartcr.set_txe();
uart_dev.uart.uartcr.set_rxe();
uart_dev.uart.uartlcr_h.set_fen();
uart_dev.uart.uartifls.write_txiflsel(2);
uart_dev.uart.uartifls.write_rxiflsel(2);
uart_dev
}
#[doc = concat!("// In init code: create the full UART with pins")]
#[doc = concat!("let uart = ", stringify!($UartName), "::init(")]
#[doc = concat!(" dp.UART", $uart_num, ", tx_pin, rx_pin, 115200, sys_clk,")]
#[doc = concat!("let uart = unsafe { ", stringify!($UartName), "::<ErasedPin, ErasedPin>::steal() };")]
#[inline]
pub unsafe fn steal() -> crate::Steal<$UartName<crate::gpio::pins::ErasedPin, crate::gpio::pins::ErasedPin>> {
crate::Steal($UartName {
uart: unsafe { <$UartType>::steal() },
_tx: crate::gpio::pins::ErasedPin,
_rx: crate::gpio::pins::ErasedPin,
})
}
#[inline]
pub unsafe fn regs(&self) -> &rp2350_pac::$uart_mod::RegisterBlock {
&self.uart
}
#[inline]
pub fn release(self) -> ($UartType, TX, RX) {
(self.uart, self._tx, self._rx)
}
fn configure_baudrate(&mut self, baudrate: u32, peri_clk: u32) -> u32 {
if baudrate == 0 {
return 0;
}
let baud_div_fixed = (4 * peri_clk as u64) / baudrate as u64;
let ibrd = (baud_div_fixed >> 6) as u32;
let fbrd = (baud_div_fixed & 0x3f) as u32;
self.uart.uartibrd.write_baud_divint(ibrd);
self.uart.uartfbrd.write_baud_divfrac(fbrd);
let lcr = self.uart.uartlcr_h.read_bits();
self.uart.uartlcr_h.write_bits(lcr);
let divisor = 64 * ibrd as u64 + fbrd as u64;
if divisor == 0 { 0 } else { (4 * peri_clk as u64 / divisor) as u32 }
}
pub fn set_baudrate(&mut self, baudrate: u32, peri_clk: u32) -> u32 {
self.flush();
self.uart.uartcr.clr_uarten();
let actual = self.configure_baudrate(baudrate, peri_clk);
self.uart.uartcr.set_uarten();
actual
}
pub fn set_format(&mut self, data_bits: DataBits, stop_bits: StopBits, parity: Parity) {
self.uart.uartlcr_h.write_wlen(data_bits as u32);
if stop_bits == StopBits::Two {
self.uart.uartlcr_h.set_stp2();
} else {
self.uart.uartlcr_h.clr_stp2();
}
match parity {
Parity::None => {
self.uart.uartlcr_h.clr_pen();
self.uart.uartlcr_h.clr_eps();
}
Parity::Even => {
self.uart.uartlcr_h.set_pen();
self.uart.uartlcr_h.set_eps();
}
Parity::Odd => {
self.uart.uartlcr_h.set_pen();
self.uart.uartlcr_h.clr_eps();
}
}
}
#[inline]
pub fn enable(&self) {
self.uart.uartcr.set_uarten();
}
#[inline]
pub fn disable(&self) {
self.uart.uartcr.clr_uarten();
}
#[inline]
pub fn is_enabled(&self) -> bool {
self.uart.uartcr.uarten()
}
#[inline]
pub fn enable_tx(&self) {
self.uart.uartcr.set_txe();
}
#[inline]
pub fn disable_tx(&self) {
self.uart.uartcr.clr_txe();
}
#[inline]
pub fn tx_enabled(&self) -> bool {
self.uart.uartcr.txe()
}
#[inline]
pub fn enable_rx(&self) {
self.uart.uartcr.set_rxe();
}
#[inline]
pub fn disable_rx(&self) {
self.uart.uartcr.clr_rxe();
}
#[inline]
pub fn rx_enabled(&self) -> bool {
self.uart.uartcr.rxe()
}
pub fn set_flow_control(&self, cts: bool, rts: bool) {
if cts {
self.uart.uartcr.set_ctsen();
} else {
self.uart.uartcr.clr_ctsen();
}
if rts {
self.uart.uartcr.set_rtsen();
} else {
self.uart.uartcr.clr_rtsen();
}
}
#[inline]
pub fn is_cts_asserted(&self) -> bool {
self.uart.uartfr.cts()
}
pub fn set_fifo_levels(&mut self, tx_level: u8, rx_level: u8) {
self.uart.uartifls.write_txiflsel(tx_level.min(4) as u32);
self.uart.uartifls.write_rxiflsel(rx_level.min(4) as u32);
}
pub fn write_byte(&self, byte: u8) {
while self.uart.uartfr.txff() {
core::hint::spin_loop();
}
self.uart.uartdr.write_data(byte as u32);
}
pub fn write(&self, data: &[u8]) {
let mut i = 0;
while i < data.len() {
while self.uart.uartfr.txff() {
core::hint::spin_loop();
}
while i < data.len() && !self.uart.uartfr.txff() {
self.uart.uartdr.write_data(data[i] as u32);
i += 1;
}
}
}
pub fn write_str(&self, s: &str) {
self.write(s.as_bytes());
}
pub fn read_byte(&self) -> Option<u8> {
if self.uart.uartfr.rxfe() {
return None;
}
Some(self.uart.uartdr.data() as u8)
}
pub fn read_byte_with_error(&self) -> Option<Result<u8, (u8, Error)>> {
if self.uart.uartfr.rxfe() {
return None;
}
let v = self.uart.uartdr.read_val();
let byte = v.data() as u8;
if v.oe() {
Some(Err((byte, Error::Overrun)))
} else if v.be() {
Some(Err((byte, Error::BreakError)))
} else if v.pe() {
Some(Err((byte, Error::ParityError)))
} else if v.fe() {
Some(Err((byte, Error::FramingError)))
} else {
Some(Ok(byte))
}
}
pub fn read_byte_blocking(&self) -> u8 {
while self.uart.uartfr.rxfe() {
core::hint::spin_loop();
}
self.uart.uartdr.data() as u8
}
pub fn read(&self, buffer: &mut [u8]) -> Result<(), Error> {
for slot in buffer.iter_mut() {
if !crate::spin::try_spin_wait(|| !self.uart.uartfr.rxfe()) {
return Err(Error::Timeout);
}
match self.read_byte_with_error() {
Some(Ok(byte)) => *slot = byte,
Some(Err((_, err))) => return Err(err),
None => unreachable!(),
}
}
Ok(())
}
pub fn read_raw(&self, buffer: &mut [u8]) -> usize {
let mut count = 0;
for slot in buffer.iter_mut() {
if let Some(byte) = self.read_byte() {
*slot = byte;
count += 1;
} else {
break;
}
}
count
}
pub fn try_write_byte(&self, byte: u8) -> bool {
if self.uart.uartfr.txff() {
return false;
}
self.uart.uartdr.write_data(byte as u32);
true
}
#[inline]
pub fn is_tx_full(&self) -> bool {
self.uart.uartfr.txff()
}
#[inline]
pub fn is_tx_empty(&self) -> bool {
self.uart.uartfr.txfe()
}
#[inline]
pub fn is_rx_full(&self) -> bool {
self.uart.uartfr.rxff()
}
#[inline]
pub fn is_rx_empty(&self) -> bool {
self.uart.uartfr.rxfe()
}
#[inline]
pub fn is_busy(&self) -> bool {
self.uart.uartfr.busy()
}
#[inline]
pub fn is_readable(&self) -> bool {
!self.is_rx_empty()
}
#[inline]
pub fn is_writable(&self) -> bool {
!self.is_tx_full()
}
pub fn flush(&self) {
while !self.uart.uartfr.txfe() {
core::hint::spin_loop();
}
while self.uart.uartfr.busy() {
core::hint::spin_loop();
}
}
pub fn clear_errors(&self) {
self.uart.uartrsr.set_oe();
self.uart.uartrsr.set_be();
self.uart.uartrsr.set_pe();
self.uart.uartrsr.set_fe();
}
#[inline]
pub fn baudrate(&self, peri_clk: u32) -> u32 {
let ibrd = self.uart.uartibrd.baud_divint() as u64;
let fbrd = self.uart.uartfbrd.baud_divfrac() as u64;
let divisor = 64 * ibrd + fbrd;
if divisor == 0 {
return 0;
}
(4 * peri_clk as u64 / divisor) as u32
}
pub fn enable_interrupts(&self, mask: u16) {
self.uart.uartimsc.set_bits(mask as u32);
}
pub fn disable_interrupts(&self, mask: u16) {
self.uart.uartimsc.clr_bits(mask as u32);
}
#[inline]
pub fn enable_rx_interrupt(&self) {
self.uart.uartimsc.set_rxim();
}
#[inline]
pub fn disable_rx_interrupt(&self) {
self.uart.uartimsc.clr_rxim();
}
#[inline]
pub fn enable_tx_interrupt(&self) {
self.uart.uartimsc.set_txim();
}
#[inline]
pub fn disable_tx_interrupt(&self) {
self.uart.uartimsc.clr_txim();
}
#[inline]
pub fn enable_rx_timeout_interrupt(&self) {
self.uart.uartimsc.set_rtim();
}
#[inline]
pub fn disable_rx_timeout_interrupt(&self) {
self.uart.uartimsc.clr_rtim();
}
#[inline]
pub fn enable_overrun_error_interrupt(&self) {
self.uart.uartimsc.set_oeim();
}
#[inline]
pub fn disable_overrun_error_interrupt(&self) {
self.uart.uartimsc.clr_oeim();
}
#[inline]
pub fn enable_break_error_interrupt(&self) {
self.uart.uartimsc.set_beim();
}
#[inline]
pub fn disable_break_error_interrupt(&self) {
self.uart.uartimsc.clr_beim();
}
#[inline]
pub fn enable_parity_error_interrupt(&self) {
self.uart.uartimsc.set_peim();
}
#[inline]
pub fn disable_parity_error_interrupt(&self) {
self.uart.uartimsc.clr_peim();
}
#[inline]
pub fn enable_framing_error_interrupt(&self) {
self.uart.uartimsc.set_feim();
}
#[inline]
pub fn disable_framing_error_interrupt(&self) {
self.uart.uartimsc.clr_feim();
}
#[inline]
pub fn enable_error_interrupts(&self) {
self.uart.uartimsc.set_oeim();
self.uart.uartimsc.set_beim();
self.uart.uartimsc.set_peim();
self.uart.uartimsc.set_feim();
}
#[inline]
pub fn disable_error_interrupts(&self) {
self.uart.uartimsc.clr_oeim();
self.uart.uartimsc.clr_beim();
self.uart.uartimsc.clr_peim();
self.uart.uartimsc.clr_feim();
}
#[inline]
pub fn disable_all_interrupts(&self) {
self.uart.uartimsc.clr_bits(0x7ff);
}
#[inline]
pub fn is_rx_interrupt_enabled(&self) -> bool {
self.uart.uartimsc.rxim()
}
#[inline]
pub fn is_tx_interrupt_enabled(&self) -> bool {
self.uart.uartimsc.txim()
}
#[inline]
pub fn raw_interrupt_status(&self) -> u16 {
(self.uart.uartris.read_bits() & 0x7ff) as u16
}
#[inline]
pub fn masked_interrupt_status(&self) -> u16 {
(self.uart.uartmis.read_bits() & 0x7ff) as u16
}
#[inline]
pub fn is_rx_interrupt_pending(&self) -> bool {
self.uart.uartmis.rxmis()
}
#[inline]
pub fn is_tx_interrupt_pending(&self) -> bool {
self.uart.uartmis.txmis()
}
#[inline]
pub fn is_rx_timeout_interrupt_pending(&self) -> bool {
self.uart.uartmis.rtmis()
}
#[inline]
pub fn is_error_interrupt_pending(&self) -> bool {
self.uart.uartmis.oemis() ||
self.uart.uartmis.bemis() ||
self.uart.uartmis.pemis() ||
self.uart.uartmis.femis()
}
pub fn clear_interrupts(&self, mask: u16) {
self.uart.uarticr.set_bits(mask as u32);
}
#[inline]
pub fn clear_rx_interrupt(&self) {
self.uart.uarticr.set_rxic();
}
#[inline]
pub fn clear_tx_interrupt(&self) {
self.uart.uarticr.set_txic();
}
#[inline]
pub fn clear_rx_timeout_interrupt(&self) {
self.uart.uarticr.set_rtic();
}
#[inline]
pub fn clear_error_interrupts(&self) {
self.uart.uarticr.set_oeic();
self.uart.uarticr.set_beic();
self.uart.uarticr.set_peic();
self.uart.uarticr.set_feic();
}
#[inline]
pub fn clear_all_interrupts(&self) {
self.uart.uarticr.set_bits(0x7ff);
}
pub const TX_DREQ: crate::dma::TreqSel = crate::dma::TreqSel::[<Uart $uart_num Tx>];
pub const RX_DREQ: crate::dma::TreqSel = crate::dma::TreqSel::[<Uart $uart_num Rx>];
#[inline]
pub fn data_register_address(&self) -> u32 {
<$UartType>::PTR as u32
}
#[inline]
pub fn enable_dma_tx(&self) {
self.uart.uartdmacr.set_txdmae();
}
#[inline]
pub fn disable_dma_tx(&self) {
self.uart.uartdmacr.clr_txdmae();
}
#[inline]
pub fn enable_dma_rx(&self) {
self.uart.uartdmacr.set_rxdmae();
}
#[inline]
pub fn disable_dma_rx(&self) {
self.uart.uartdmacr.clr_rxdmae();
}
}
impl<TX, RX> core::fmt::Write for $UartName<TX, RX>
where
TX: $TxPin,
RX: $RxPin,
{
fn write_str(&mut self, s: &str) -> core::fmt::Result {
self.write(s.as_bytes());
Ok(())
}
}
#[doc = concat!("# UART", $uart_num, " with Flow Control")]
pub struct [<$UartName WithFlowControl>]<TX, RX, CTS, RTS>
where
TX: $TxPin,
RX: $RxPin,
CTS: $CtsPin,
RTS: $RtsPin,
{
inner: $UartName<TX, RX>,
_cts: CTS,
_rts: RTS,
}
impl<TX, RX, CTS, RTS> [<$UartName WithFlowControl>]<TX, RX, CTS, RTS>
where
TX: $TxPin,
RX: $RxPin,
CTS: $CtsPin,
RTS: $RtsPin,
{
pub const NUM: u8 = $uart_num;
#[inline]
pub unsafe fn steal() -> crate::Steal<[<$UartName WithFlowControl>]<
crate::gpio::pins::ErasedPin,
crate::gpio::pins::ErasedPin,
crate::gpio::pins::ErasedPin,
crate::gpio::pins::ErasedPin,
>> {
crate::Steal([<$UartName WithFlowControl>] {
inner: $UartName {
uart: unsafe { <$UartType>::steal() },
_tx: crate::gpio::pins::ErasedPin,
_rx: crate::gpio::pins::ErasedPin,
},
_cts: crate::gpio::pins::ErasedPin,
_rts: crate::gpio::pins::ErasedPin,
})
}
#[inline]
pub unsafe fn regs(&self) -> &rp2350_pac::$uart_mod::RegisterBlock {
&self.inner.uart
}
pub fn init(
uart: $UartType,
tx: TX,
rx: RX,
mut cts: CTS,
mut rts: RTS,
baudrate: u32,
peri_clk: u32,
) -> Self {
let inner = $UartName::init(uart, tx, rx, baudrate, peri_clk);
cts.[<configure_uart $uart_num _cts>]();
rts.[<configure_uart $uart_num _rts>]();
inner.set_flow_control(true, true);
Self {
inner,
_cts: cts,
_rts: rts,
}
}
#[inline]
pub fn release(self) -> ($UartType, TX, RX, CTS, RTS) {
let (uart, tx, rx) = self.inner.release();
(uart, tx, rx, self._cts, self._rts)
}
}
impl<TX, RX, CTS, RTS> core::ops::Deref for [<$UartName WithFlowControl>]<TX, RX, CTS, RTS>
where
TX: $TxPin,
RX: $RxPin,
CTS: $CtsPin,
RTS: $RtsPin,
{
type Target = $UartName<TX, RX>;
fn deref(&self) -> &Self::Target {
&self.inner
}
}
impl<TX, RX, CTS, RTS> core::ops::DerefMut for [<$UartName WithFlowControl>]<TX, RX, CTS, RTS>
where
TX: $TxPin,
RX: $RxPin,
CTS: $CtsPin,
RTS: $RtsPin,
{
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.inner
}
}
impl<TX, RX, CTS, RTS> core::fmt::Write for [<$UartName WithFlowControl>]<TX, RX, CTS, RTS>
where
TX: $TxPin,
RX: $RxPin,
CTS: $CtsPin,
RTS: $RtsPin,
{
fn write_str(&mut self, s: &str) -> core::fmt::Result {
self.inner.write(s.as_bytes());
Ok(())
}
}
}
};
}
impl_uart!(
Uart0,
0,
rp2350_pac::UART0,
uart0,
Uart0TxPin,
Uart0RxPin,
Uart0CtsPin,
Uart0RtsPin
);
impl_uart!(
Uart1,
1,
rp2350_pac::UART1,
uart1,
Uart1TxPin,
Uart1RxPin,
Uart1CtsPin,
Uart1RtsPin
);
pub const UART_IRQ_RI: u16 = 1 << 0;
pub const UART_IRQ_CTS: u16 = 1 << 1;
pub const UART_IRQ_DCD: u16 = 1 << 2;
pub const UART_IRQ_DSR: u16 = 1 << 3;
pub const UART_IRQ_RX: u16 = 1 << 4;
pub const UART_IRQ_TX: u16 = 1 << 5;
pub const UART_IRQ_RT: u16 = 1 << 6;
pub const UART_IRQ_FE: u16 = 1 << 7;
pub const UART_IRQ_PE: u16 = 1 << 8;
pub const UART_IRQ_BE: u16 = 1 << 9;
pub const UART_IRQ_OE: u16 = 1 << 10;