Skip to content

Commit 6889afd

Browse files
authored
Merge pull request #779 from stm32-rs/more-clean
tx rx trait default impl
2 parents ad969d2 + 6363455 commit 6889afd

File tree

4 files changed

+27
-35
lines changed

4 files changed

+27
-35
lines changed

src/lib.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,10 +200,25 @@ pub trait Listen {
200200
}
201201

202202
pub trait Ptr {
203+
/// RegisterBlock structure
203204
type RB;
205+
/// Return the pointer to the register block
204206
fn ptr() -> *const Self::RB;
205207
}
206208

207209
pub trait Steal {
210+
/// Steal an instance of this peripheral
211+
///
212+
/// # Safety
213+
///
214+
/// Ensure that the new instance of the peripheral cannot be used in a way
215+
/// that may race with any existing instances, for example by only
216+
/// accessing read-only or write-only registers, or by consuming the
217+
/// original peripheral and using critical sections to coordinate
218+
/// access between multiple new instances.
219+
///
220+
/// Additionally the HAL may rely on only one
221+
/// peripheral instance existing to ensure memory safety; ensure
222+
/// no stolen instances are passed to such software.
208223
unsafe fn steal() -> Self;
209224
}

src/serial.rs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,11 @@ pub trait SerialExt: Sized + Instance {
210210
clocks: &Clocks,
211211
) -> Result<Tx<Self, WORD>, config::InvalidConfig>
212212
where
213-
NoPin: Into<Self::Rx<PushPull>>;
213+
NoPin: Into<Self::Rx<PushPull>>,
214+
{
215+
self.serial((tx_pin, NoPin::new()), config, clocks)
216+
.map(|s| s.split().0)
217+
}
214218

215219
fn rx<WORD>(
216220
self,
@@ -219,7 +223,11 @@ pub trait SerialExt: Sized + Instance {
219223
clocks: &Clocks,
220224
) -> Result<Rx<Self, WORD>, config::InvalidConfig>
221225
where
222-
NoPin: Into<Self::Tx<PushPull>>;
226+
NoPin: Into<Self::Tx<PushPull>>,
227+
{
228+
self.serial((NoPin::new(), rx_pin), config, clocks)
229+
.map(|s| s.split().1)
230+
}
223231
}
224232

225233
impl<USART: Instance, WORD> Serial<USART, WORD> {
@@ -231,10 +239,7 @@ impl<USART: Instance, WORD> Serial<USART, WORD> {
231239
),
232240
config: impl Into<config::Config>,
233241
clocks: &Clocks,
234-
) -> Result<Self, config::InvalidConfig>
235-
where
236-
<USART as crate::Ptr>::RB: uart_impls::RegisterBlockImpl,
237-
{
242+
) -> Result<Self, config::InvalidConfig> {
238243
<USART as crate::Ptr>::RB::new(usart, pins, config, clocks)
239244
}
240245
}

src/serial/hal_1.rs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,6 @@ mod nb {
8989
}
9090

9191
mod io {
92-
use core::ops::Deref;
93-
9492
use super::super::{Error, Instance, RegisterBlockImpl, Rx, Serial, Tx};
9593
use embedded_io::Write;
9694

@@ -113,11 +111,7 @@ mod io {
113111
type Error = Error;
114112
}
115113

116-
impl<USART: Instance> Write for Tx<USART, u8>
117-
where
118-
<USART as crate::Ptr>::RB: RegisterBlockImpl,
119-
USART: Deref<Target = <USART as crate::Ptr>::RB>,
120-
{
114+
impl<USART: Instance> Write for Tx<USART, u8> {
121115
fn write(&mut self, bytes: &[u8]) -> Result<usize, Self::Error> {
122116
let mut i = 0;
123117
for byte in bytes.iter() {

src/serial/uart_impls.rs

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -650,28 +650,6 @@ impl<UART: Instance> SerialExt for UART {
650650
) -> Result<Serial<Self, WORD>, config::InvalidConfig> {
651651
Serial::new(self, pins, config, clocks)
652652
}
653-
fn tx<WORD>(
654-
self,
655-
tx_pin: impl Into<Self::Tx<PushPull>>,
656-
config: impl Into<config::Config>,
657-
clocks: &Clocks,
658-
) -> Result<Tx<Self, WORD>, config::InvalidConfig>
659-
where
660-
NoPin: Into<Self::Rx<PushPull>>,
661-
{
662-
Serial::tx(self, tx_pin, config, clocks)
663-
}
664-
fn rx<WORD>(
665-
self,
666-
rx_pin: impl Into<Self::Rx<PushPull>>,
667-
config: impl Into<config::Config>,
668-
clocks: &Clocks,
669-
) -> Result<Rx<Self, WORD>, config::InvalidConfig>
670-
where
671-
NoPin: Into<Self::Tx<PushPull>>,
672-
{
673-
Serial::rx(self, rx_pin, config, clocks)
674-
}
675653
}
676654

677655
impl<UART: Instance, WORD> Serial<UART, WORD> {

0 commit comments

Comments
 (0)