Skip to content

Commit

Permalink
Convert serial traits from unsafe to sealed
Browse files Browse the repository at this point in the history
  • Loading branch information
9names committed Jan 10, 2024
1 parent bc352d1 commit 27b5ec6
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 14 deletions.
2 changes: 1 addition & 1 deletion src/i2c.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ impl embedded_hal::i2c::Error for Error {
}

/// SDA pins
pub trait SdaPin<I2C> : Sealed {}
pub trait SdaPin<I2C>: Sealed {}

/// SCL pins
pub trait SclPin<I2C>: Sealed {}
Expand Down
60 changes: 47 additions & 13 deletions src/serial.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
//! Serial communication
use self::private::Sealed;
use crate::clock::Clocks;
use crate::pac;
use core::fmt;
Expand Down Expand Up @@ -349,27 +350,27 @@ where
}

/// Serial transmit pins - DO NOT IMPLEMENT THIS TRAIT
pub unsafe trait TxPin<UART> {}
pub trait TxPin<UART>: Sealed {}
/// Serial receive pins - DO NOT IMPLEMENT THIS TRAIT
pub unsafe trait RxPin<UART> {}
pub trait RxPin<UART>: Sealed {}
/// Serial rts pins - DO NOT IMPLEMENT THIS TRAIT
pub unsafe trait RtsPin<UART> {}
pub trait RtsPin<UART>: Sealed {}
/// Serial cts pins - DO NOT IMPLEMENT THIS TRAIT
pub unsafe trait CtsPin<UART> {}
pub trait CtsPin<UART>: Sealed {}

macro_rules! impl_uart_pin {
($(($UartSigi: ident, $UartMuxi: ident),)+) => {
use crate::gpio::*;
$(
unsafe impl<PIN: UartPin<$UartSigi>> TxPin<pac::UART0> for (PIN, $UartMuxi<Uart0Tx>) {}
unsafe impl<PIN: UartPin<$UartSigi>> RxPin<pac::UART0> for (PIN, $UartMuxi<Uart0Rx>) {}
unsafe impl<PIN: UartPin<$UartSigi>> RtsPin<pac::UART0> for (PIN, $UartMuxi<Uart0Rts>) {}
unsafe impl<PIN: UartPin<$UartSigi>> CtsPin<pac::UART0> for (PIN, $UartMuxi<Uart0Cts>) {}

unsafe impl<PIN: UartPin<$UartSigi>> TxPin<pac::UART1> for (PIN, $UartMuxi<Uart1Tx>) {}
unsafe impl<PIN: UartPin<$UartSigi>> RxPin<pac::UART1> for (PIN, $UartMuxi<Uart1Rx>) {}
unsafe impl<PIN: UartPin<$UartSigi>> RtsPin<pac::UART1> for (PIN, $UartMuxi<Uart1Rts>) {}
unsafe impl<PIN: UartPin<$UartSigi>> CtsPin<pac::UART1> for (PIN, $UartMuxi<Uart1Cts>) {}
impl<PIN: UartPin<$UartSigi>> TxPin<pac::UART0> for (PIN, $UartMuxi<Uart0Tx>) {}
impl<PIN: UartPin<$UartSigi>> RxPin<pac::UART0> for (PIN, $UartMuxi<Uart0Rx>) {}
impl<PIN: UartPin<$UartSigi>> RtsPin<pac::UART0> for (PIN, $UartMuxi<Uart0Rts>) {}
impl<PIN: UartPin<$UartSigi>> CtsPin<pac::UART0> for (PIN, $UartMuxi<Uart0Cts>) {}

impl<PIN: UartPin<$UartSigi>> TxPin<pac::UART1> for (PIN, $UartMuxi<Uart1Tx>) {}
impl<PIN: UartPin<$UartSigi>> RxPin<pac::UART1> for (PIN, $UartMuxi<Uart1Rx>) {}
impl<PIN: UartPin<$UartSigi>> RtsPin<pac::UART1> for (PIN, $UartMuxi<Uart1Rts>) {}
impl<PIN: UartPin<$UartSigi>> CtsPin<pac::UART1> for (PIN, $UartMuxi<Uart1Cts>) {}
)+
};
}
Expand Down Expand Up @@ -416,3 +417,36 @@ where
const HAS_RTS: bool = true;
const HAS_CTS: bool = true;
}

// Prevent users from implementing the Serial pin traits
mod private {
use crate::gpio;

pub trait Sealed {}
impl<TX, RX> Sealed for (TX, RX) {}
impl<TX, RX, RTS, CTS> Sealed for (TX, RX, RTS, CTS) {}

impl<MODE> Sealed for gpio::Pin0<MODE> {}
impl<MODE> Sealed for gpio::Pin1<MODE> {}
impl<MODE> Sealed for gpio::Pin2<MODE> {}
impl<MODE> Sealed for gpio::Pin3<MODE> {}
impl<MODE> Sealed for gpio::Pin4<MODE> {}
impl<MODE> Sealed for gpio::Pin5<MODE> {}
impl<MODE> Sealed for gpio::Pin6<MODE> {}
impl<MODE> Sealed for gpio::Pin7<MODE> {}
impl<MODE> Sealed for gpio::Pin8<MODE> {}
impl<MODE> Sealed for gpio::Pin9<MODE> {}
impl<MODE> Sealed for gpio::Pin10<MODE> {}
impl<MODE> Sealed for gpio::Pin11<MODE> {}
impl<MODE> Sealed for gpio::Pin12<MODE> {}
impl<MODE> Sealed for gpio::Pin13<MODE> {}
impl<MODE> Sealed for gpio::Pin14<MODE> {}
impl<MODE> Sealed for gpio::Pin15<MODE> {}
impl<MODE> Sealed for gpio::Pin16<MODE> {}
impl<MODE> Sealed for gpio::Pin17<MODE> {}
impl<MODE> Sealed for gpio::Pin18<MODE> {}
impl<MODE> Sealed for gpio::Pin19<MODE> {}
impl<MODE> Sealed for gpio::Pin20<MODE> {}
impl<MODE> Sealed for gpio::Pin21<MODE> {}
impl<MODE> Sealed for gpio::Pin22<MODE> {}
}

0 comments on commit 27b5ec6

Please sign in to comment.