Skip to content

Commit

Permalink
Experiment with implementing TargetAddress on a HAL structure
Browse files Browse the repository at this point in the history
As well as the PAC structure.

Also add the option to include an extra layer of
indirection for the target register
  • Loading branch information
richardeoin committed Oct 18, 2020
1 parent b72a226 commit e237b80
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 10 deletions.
5 changes: 5 additions & 0 deletions src/dma/bdma.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use super::{
use core::marker::PhantomData;

use crate::{
i2c::I2c,
pac::{self, BDMA, DMAMUX2},
rcc::{rec, rec::ResetEnable},
//serial::{Rx, Tx},
Expand Down Expand Up @@ -478,4 +479,8 @@ peripheral_target_address!(
(pac::SPI6, txdr, u8, M2P, DMAReq::SPI6_TX_DMA),
(pac::I2C4, rxdr, u8, P2M, DMAReq::I2C4_RX_DMA),
(pac::I2C4, txdr, u8, M2P, DMAReq::I2C4_TX_DMA),
(INNER, I2c<pac::I2C4>, rxdr, u8, P2M, DMAReq::I2C4_RX_DMA),
(INNER, I2c<pac::I2C4>, txdr, u8, M2P, DMAReq::I2C4_TX_DMA),
);

);
58 changes: 48 additions & 10 deletions src/dma/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,62 @@
// Convenience macro for implementing target addresses on peripherals
macro_rules! peripheral_target_address {
($(
($peripheral:ty, $register:ident $(($TRBUFF:ident))*, $size:ty,
$dir:ty $(, $mux:expr)*)
$peripheral:tt
),+ $(,)*) => {
$(
unsafe impl TargetAddress<$dir> for &mut $peripheral {
#[inline(always)]
fn address(&self) -> u32 {
&self.$register as *const _ as u32
}
peripheral_target_instance!($peripheral);
)+
};
}
macro_rules! peripheral_target_instance {
(($peripheral:ty, $register:ident $(($TRBUFF:ident))*, $size:ty,
$dir:ty $(, $mux:expr)*)) => {
unsafe impl TargetAddress<$dir> for &mut $peripheral {
#[inline(always)]
fn address(&self) -> u32 {
&self.$register as *const _ as u32
}

type MemSize = $size;
type MemSize = $size;
$(
const REQUEST_LINE: Option<u8> = Some($mux as u8);
)*
$(
const REQUEST_LINE: Option<u8> = Some($mux as u8);
const $TRBUFF: bool = true;
)*
}
};

((INNER, $peripheral:ty, $register:ident $(($TRBUFF:ident))*, $size:ty,
$dir:ty $(, $mux:expr)*)) => {
unsafe impl TargetAddress<$dir> for &mut $peripheral {
#[inline(always)]
fn address(&self) -> u32 {
&self.inner().$register as *const _ as u32
}

type MemSize = $size;
$(
const REQUEST_LINE: Option<u8> = Some($mux as u8);
)*
$(
const $TRBUFF: bool = true;
)*
}
};

(($peripheral:ty, $channel:ident.$register:ident, $size:ty,
$dir:ty $(, $mux:expr)*)) => {
unsafe impl TargetAddress<$dir> for &mut $peripheral {
#[inline(always)]
fn address(&self) -> u32 {
&self.$channel.$register as *const _ as u32
}
)+

type MemSize = $size;
$(
const REQUEST_LINE: Option<u8> = Some($mux as u8);
)*
}
};
}
5 changes: 5 additions & 0 deletions src/i2c.rs
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,11 @@ macro_rules! i2c {
I2c { i2c }
}

/// Returns a reference to the inner peripheral
pub fn inner(&self) -> &$I2CX {
&self.i2c
}

/// Start listening for `event`
pub fn listen(&mut self, event: Event) {
self.i2c.cr1.modify(|_,w| {
Expand Down

0 comments on commit e237b80

Please sign in to comment.