Using thread send safe UART* marker, as suggested by @danielzfranklin in #Issue-284 (#314)

This commit is contained in:
Marius Meißner 2022-03-18 10:56:27 +01:00 committed by GitHub
parent 6026ea4ae3
commit 990085948a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 10 additions and 9 deletions

View file

@ -16,6 +16,7 @@ use rp2040_pac::{UART0, UART1};
#[cfg(feature = "eh1_0_alpha")] #[cfg(feature = "eh1_0_alpha")]
use eh1_0_alpha::serial as eh1; use eh1_0_alpha::serial as eh1;
use pac::Peripherals;
/// An UART Peripheral based on an underlying UART device. /// An UART Peripheral based on an underlying UART device.
pub struct UartPeripheral<S: State, D: UartDevice, P: ValidUartPinout<D>> { pub struct UartPeripheral<S: State, D: UartDevice, P: ValidUartPinout<D>> {
@ -208,7 +209,7 @@ impl<P: ValidUartPinout<UART0>> UartPeripheral<Enabled, UART0, P> {
effective_baudrate: self.effective_baudrate, effective_baudrate: self.effective_baudrate,
}; };
// Safety: reader and writer will never write to the same address // Safety: reader and writer will never write to the same address
let device_copy = unsafe { &*UART0::ptr() }; let device_copy = unsafe { Peripherals::steal().UART0 };
let writer = Writer { let writer = Writer {
device: device_copy, device: device_copy,
device_marker: core::marker::PhantomData, device_marker: core::marker::PhantomData,
@ -228,7 +229,7 @@ impl<P: ValidUartPinout<UART1>> UartPeripheral<Enabled, UART1, P> {
effective_baudrate: self.effective_baudrate, effective_baudrate: self.effective_baudrate,
}; };
// Safety: reader and writer will never write to the same address // Safety: reader and writer will never write to the same address
let device_copy = unsafe { &*UART1::ptr() }; let device_copy = unsafe { Peripherals::steal().UART1 };
let writer = Writer { let writer = Writer {
device: device_copy, device: device_copy,
device_marker: core::marker::PhantomData, device_marker: core::marker::PhantomData,

View file

@ -114,7 +114,7 @@ pub(crate) fn disable_tx_interrupt(rb: &RegisterBlock) {
/// [`UartPeripheral`]: struct.UartPeripheral.html /// [`UartPeripheral`]: struct.UartPeripheral.html
/// [`UartPeripheral::split()`]: struct.UartPeripheral.html#method.split /// [`UartPeripheral::split()`]: struct.UartPeripheral.html#method.split
pub struct Writer<D: UartDevice, P: ValidUartPinout<D>> { pub struct Writer<D: UartDevice, P: ValidUartPinout<D>> {
pub(super) device: &'static RegisterBlock, pub(super) device: D,
pub(super) device_marker: PhantomData<D>, pub(super) device_marker: PhantomData<D>,
pub(super) pins: PhantomData<P>, pub(super) pins: PhantomData<P>,
} }
@ -129,26 +129,26 @@ impl<D: UartDevice, P: ValidUartPinout<D>> Writer<D, P> {
/// ///
/// Upon success, the remaining (unwritten) slice is returned. /// Upon success, the remaining (unwritten) slice is returned.
pub fn write_raw<'d>(&self, data: &'d [u8]) -> nb::Result<&'d [u8], Infallible> { pub fn write_raw<'d>(&self, data: &'d [u8]) -> nb::Result<&'d [u8], Infallible> {
write_raw(self.device, data) write_raw(&self.device, data)
} }
/// Writes bytes to the UART. /// Writes bytes to the UART.
/// ///
/// This function blocks until the full buffer has been sent. /// This function blocks until the full buffer has been sent.
pub fn write_full_blocking(&self, data: &[u8]) { pub fn write_full_blocking(&self, data: &[u8]) {
write_full_blocking(self.device, data); write_full_blocking(&self.device, data);
} }
/// Enables the Transmit Interrupt. /// Enables the Transmit Interrupt.
/// ///
/// The relevant UARTx IRQ will fire when there is space in the transmit FIFO. /// The relevant UARTx IRQ will fire when there is space in the transmit FIFO.
pub fn enable_tx_interrupt(&mut self) { pub fn enable_tx_interrupt(&mut self) {
enable_tx_interrupt(self.device) enable_tx_interrupt(&self.device)
} }
/// Disables the Transmit Interrupt. /// Disables the Transmit Interrupt.
pub fn disable_tx_interrupt(&mut self) { pub fn disable_tx_interrupt(&mut self) {
disable_tx_interrupt(self.device) disable_tx_interrupt(&self.device)
} }
} }
@ -164,7 +164,7 @@ impl<D: UartDevice, P: ValidUartPinout<D>> Write<u8> for Writer<D, P> {
} }
fn flush(&mut self) -> nb::Result<(), Self::Error> { fn flush(&mut self) -> nb::Result<(), Self::Error> {
transmit_flushed(self.device) transmit_flushed(&self.device)
} }
} }
@ -184,7 +184,7 @@ impl<D: UartDevice, P: ValidUartPinout<D>> eh1::nb::Write<u8> for Writer<D, P> {
} }
fn flush(&mut self) -> nb::Result<(), Self::Error> { fn flush(&mut self) -> nb::Result<(), Self::Error> {
transmit_flushed(self.device).map_err(|e| match e { transmit_flushed(&self.device).map_err(|e| match e {
WouldBlock => WouldBlock, WouldBlock => WouldBlock,
Other(v) => match v {}, Other(v) => match v {},
}) })