From 990085948ace93cf52ff17f571dcbfc4128b880b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marius=20Mei=C3=9Fner?= Date: Fri, 18 Mar 2022 10:56:27 +0100 Subject: [PATCH] Using thread send safe UART* marker, as suggested by @danielzfranklin in #Issue-284 (#314) --- rp2040-hal/src/uart/peripheral.rs | 5 +++-- rp2040-hal/src/uart/writer.rs | 14 +++++++------- 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/rp2040-hal/src/uart/peripheral.rs b/rp2040-hal/src/uart/peripheral.rs index 796731f..9740bef 100644 --- a/rp2040-hal/src/uart/peripheral.rs +++ b/rp2040-hal/src/uart/peripheral.rs @@ -16,6 +16,7 @@ use rp2040_pac::{UART0, UART1}; #[cfg(feature = "eh1_0_alpha")] use eh1_0_alpha::serial as eh1; +use pac::Peripherals; /// An UART Peripheral based on an underlying UART device. pub struct UartPeripheral> { @@ -208,7 +209,7 @@ impl> UartPeripheral { effective_baudrate: self.effective_baudrate, }; // 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 { device: device_copy, device_marker: core::marker::PhantomData, @@ -228,7 +229,7 @@ impl> UartPeripheral { effective_baudrate: self.effective_baudrate, }; // 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 { device: device_copy, device_marker: core::marker::PhantomData, diff --git a/rp2040-hal/src/uart/writer.rs b/rp2040-hal/src/uart/writer.rs index bf5a550..c67a5b6 100644 --- a/rp2040-hal/src/uart/writer.rs +++ b/rp2040-hal/src/uart/writer.rs @@ -114,7 +114,7 @@ pub(crate) fn disable_tx_interrupt(rb: &RegisterBlock) { /// [`UartPeripheral`]: struct.UartPeripheral.html /// [`UartPeripheral::split()`]: struct.UartPeripheral.html#method.split pub struct Writer> { - pub(super) device: &'static RegisterBlock, + pub(super) device: D, pub(super) device_marker: PhantomData, pub(super) pins: PhantomData

, } @@ -129,26 +129,26 @@ impl> Writer { /// /// Upon success, the remaining (unwritten) slice is returned. 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. /// /// This function blocks until the full buffer has been sent. pub fn write_full_blocking(&self, data: &[u8]) { - write_full_blocking(self.device, data); + write_full_blocking(&self.device, data); } /// Enables the Transmit Interrupt. /// /// The relevant UARTx IRQ will fire when there is space in the transmit FIFO. pub fn enable_tx_interrupt(&mut self) { - enable_tx_interrupt(self.device) + enable_tx_interrupt(&self.device) } /// Disables the Transmit Interrupt. pub fn disable_tx_interrupt(&mut self) { - disable_tx_interrupt(self.device) + disable_tx_interrupt(&self.device) } } @@ -164,7 +164,7 @@ impl> Write for Writer { } fn flush(&mut self) -> nb::Result<(), Self::Error> { - transmit_flushed(self.device) + transmit_flushed(&self.device) } } @@ -184,7 +184,7 @@ impl> eh1::nb::Write for Writer { } 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, Other(v) => match v {}, })