Skip to content

Commit

Permalink
aliases for serial tx/rx (#59)
Browse files Browse the repository at this point in the history
  • Loading branch information
burrbull authored and TheZoq2 committed Jun 3, 2019
1 parent 1ba8681 commit 5ad17fc
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 14 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

## [Unreleased]

- Added type aliases `Tx1` for `Tx<USART1>`, `RxDma1` for `RxDma<USART1, dma1::C5>`, etc.

## [v0.3.0] - 2019-04-27

### Added
Expand Down
10 changes: 5 additions & 5 deletions src/dma.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ macro_rules! dma {
}

/// Returns `true` if there's a transfer in progress
fn in_progress(&self) -> bool {
pub fn in_progress(&self) -> bool {
self.isr().$tcifX().bit_is_clear()
}
}
Expand Down Expand Up @@ -449,20 +449,20 @@ dma! {
/// DMA Receiver
pub struct RxDma<PAYLOAD, RXCH> {
pub(crate) _payload: PhantomData<PAYLOAD>,
pub(crate) channel: RXCH,
pub channel: RXCH,
}

/// DMA Transmitter
pub struct TxDma<PAYLOAD, TXCH> {
pub(crate) _payload: PhantomData<PAYLOAD>,
pub(crate) channel: TXCH,
pub channel: TXCH,
}

/// DMA Receiver/Transmitter
pub struct RxTxDma<PAYLOAD, RXCH, TXCH> {
pub(crate) _payload: PhantomData<PAYLOAD>,
pub(crate) rxchannel: RXCH,
pub(crate) txchannel: TXCH,
pub rxchannel: RXCH,
pub txchannel: TXCH,
}

pub trait Receive {
Expand Down
36 changes: 27 additions & 9 deletions src/serial.rs
Original file line number Diff line number Diff line change
Expand Up @@ -441,28 +441,40 @@ hal! {
),
}

pub type Rx1 = Rx<USART1>;
pub type Tx1 = Tx<USART1>;
pub type Rx2 = Rx<USART2>;
pub type Tx2 = Tx<USART2>;
pub type Rx3 = Rx<USART3>;
pub type Tx3 = Tx<USART3>;

use crate::dma::{Transmit, Receive};

macro_rules! serialdma {
($(
$USARTX:ident: (
$rxdma:ident,
$txdma:ident,
$dmarxch:ty,
$dmatxch:ty,
),
)+) => {
$(
impl Receive for RxDma<$USARTX, $dmarxch> {
pub type $rxdma = RxDma<$USARTX, $dmarxch>;
pub type $txdma = TxDma<$USARTX, $dmatxch>;

impl Receive for $rxdma {
type RxChannel = $dmarxch;
type TransmittedWord = u8;
}

impl Transmit for TxDma<$USARTX, $dmatxch> {
impl Transmit for $txdma {
type TxChannel = $dmatxch;
type ReceivedWord = u8;
}

impl Rx<$USARTX> {
pub fn with_dma(self, channel: $dmarxch) -> RxDma<$USARTX, $dmarxch> {
pub fn with_dma(self, channel: $dmarxch) -> $rxdma {
RxDma {
_payload: PhantomData,
channel,
Expand All @@ -471,15 +483,15 @@ macro_rules! serialdma {
}

impl Tx<$USARTX> {
pub fn with_dma(self, channel: $dmatxch) -> TxDma<$USARTX, $dmatxch> {
pub fn with_dma(self, channel: $dmatxch) -> $txdma {
TxDma {
_payload: PhantomData,
channel,
}
}
}

impl RxDma<$USARTX, $dmarxch> {
impl $rxdma {
pub fn split(mut self) -> (Rx<$USARTX>, $dmarxch) {
self.stop();
let RxDma {_payload, channel} = self;
Expand All @@ -490,7 +502,7 @@ macro_rules! serialdma {
}
}

impl TxDma<$USARTX, $dmatxch> {
impl $txdma {
pub fn split(mut self) -> (Tx<$USARTX>, $dmatxch) {
self.stop();
let TxDma {_payload, channel} = self;
Expand All @@ -501,7 +513,7 @@ macro_rules! serialdma {
}
}

impl<B> crate::dma::CircReadDma<B, u8> for RxDma<$USARTX, $dmarxch> where B: AsMut<[u8]> {
impl<B> crate::dma::CircReadDma<B, u8> for $rxdma where B: AsMut<[u8]> {
fn circ_read(mut self, buffer: &'static mut [B; 2],
) -> CircBuffer<B, $dmarxch>
{
Expand Down Expand Up @@ -531,7 +543,7 @@ macro_rules! serialdma {
}
}

impl<B> crate::dma::ReadDma<B, u8> for RxDma<$USARTX, $dmarxch> where B: AsMut<[u8]> {
impl<B> crate::dma::ReadDma<B, u8> for $rxdma where B: AsMut<[u8]> {
fn read(mut self, buffer: &'static mut B,
) -> Transfer<W, &'static mut B, Self>
{
Expand All @@ -556,7 +568,7 @@ macro_rules! serialdma {
}
}

impl<A, B> crate::dma::WriteDma<A, B, u8> for TxDma<$USARTX, $dmatxch> where A: AsRef<[u8]>, B: Static<A> {
impl<A, B> crate::dma::WriteDma<A, B, u8> for $txdma where A: AsRef<[u8]>, B: Static<A> {
fn write(mut self, buffer: B
) -> Transfer<R, B, Self>
{
Expand Down Expand Up @@ -590,14 +602,20 @@ macro_rules! serialdma {

serialdma! {
USART1: (
RxDma1,
TxDma1,
dma1::C5,
dma1::C4,
),
USART2: (
RxDma2,
TxDma2,
dma1::C6,
dma1::C7,
),
USART3: (
RxDma3,
TxDma3,
dma1::C3,
dma1::C2,
),
Expand Down

0 comments on commit 5ad17fc

Please sign in to comment.