|
| 1 | +use super::*; |
| 2 | + |
| 3 | +pub trait OutPort { |
| 4 | + type Target; |
| 5 | + fn outport(self) -> Self::Target; |
| 6 | +} |
| 7 | + |
| 8 | +macro_rules! out_port { |
| 9 | + ( $name:ident => $n:literal, ( $($i:literal),+ ), ( $($N:ident),+ ), ( $($d:ident),* )) => { |
| 10 | + pub struct $name<const P: char $(, const $N: u8)+> { |
| 11 | + $(pub $d: Pin<P, $N, Output<PushPull>>,)+ |
| 12 | + } |
| 13 | + |
| 14 | + impl<const P: char $(, const $N: u8)+> OutPort for ($(Pin<P, $N, Output<PushPull>>),+) { |
| 15 | + type Target = $name<P $(, $N)+>; |
| 16 | + fn outport(self) -> Self::Target { |
| 17 | + let ($($d),+) = self; |
| 18 | + Self::Target { $($d),+ } |
| 19 | + } |
| 20 | + } |
| 21 | + |
| 22 | + #[allow(clippy::too_many_arguments)] |
| 23 | + impl<const P: char $(, const $N: u8)+> $name<P $(, $N)+> { |
| 24 | + pub const fn new( |
| 25 | + $($d: Pin<P, $N, Output<PushPull>>,)+ |
| 26 | + ) -> Self { |
| 27 | + Self { $($d),+ } |
| 28 | + } |
| 29 | + const fn mask() -> u32 { |
| 30 | + 0 $( | (1 << { $N }))+ |
| 31 | + } |
| 32 | + const fn value_for_write_bsrr(val: u32) -> u32 { |
| 33 | + 0 $( | (1 << (if val & (1 << $i) != 0 { $N } else { $N + 16 })))+ |
| 34 | + } |
| 35 | + |
| 36 | + #[doc=concat!("Set/reset pins according to `", $n, "` lower bits")] |
| 37 | + #[inline(never)] |
| 38 | + pub fn write(&mut self, word: u32) { |
| 39 | + unsafe { |
| 40 | + (*Gpio::<P>::ptr()) |
| 41 | + .bsrr |
| 42 | + .write(|w| w.bits(Self::value_for_write_bsrr(word))) |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + /// Sets all pins to `PinState::High` |
| 47 | + pub fn all_high(&mut self) { |
| 48 | + unsafe { |
| 49 | + (*Gpio::<P>::ptr()) |
| 50 | + .bsrr |
| 51 | + .write(|w| w.bits(Self::mask())) |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + /// Sets all pins to `PinState::Low` |
| 56 | + pub fn all_low(&mut self) { |
| 57 | + unsafe { |
| 58 | + (*Gpio::<P>::ptr()) |
| 59 | + .bsrr |
| 60 | + .write(|w| w.bits(Self::mask() << 16)) |
| 61 | + } |
| 62 | + } |
| 63 | + } |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +out_port!(OutPort2 => 2, (0, 1), (N0, N1), (d0, d1)); |
| 68 | +out_port!(OutPort3 => 3, (0, 1, 2), (N0, N1, N2), (d0, d1, d2)); |
| 69 | +out_port!(OutPort4 => 4, (0, 1, 2, 3), (N0, N1, N2, N3), (d0, d1, d2, d3)); |
| 70 | +out_port!(OutPort5 => 5, (0, 1, 2, 3, 4), (N0, N1, N2, N3, N4), (d0, d1, d2, d3, d4)); |
| 71 | +out_port!(OutPort6 => 6, (0, 1, 2, 3, 4, 5), (N0, N1, N2, N3, N4, N5), (d0, d1, d2, d3, d4, d5)); |
| 72 | +out_port!(OutPort7 => 7, (0, 1, 2, 3, 4, 5, 6), (N0, N1, N2, N3, N4, N5, N6), (d0, d1, d2, d3, d4, d5, d6)); |
| 73 | +out_port!(OutPort8 => 8, (0, 1, 2, 3, 4, 5, 6, 7), (N0, N1, N2, N3, N4, N5, N6, N7), (d0, d1, d2, d3, d4, d5, d6, d7)); |
| 74 | + |
| 75 | +pub struct OutPortArray<const P: char, const SIZE: usize>(pub [PEPin<P, Output<PushPull>>; SIZE]); |
| 76 | + |
| 77 | +impl<const P: char, const SIZE: usize> OutPort for [PEPin<P, Output<PushPull>>; SIZE] { |
| 78 | + type Target = OutPortArray<P, SIZE>; |
| 79 | + fn outport(self) -> Self::Target { |
| 80 | + OutPortArray(self) |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +impl<const P: char, const SIZE: usize> OutPortArray<P, SIZE> { |
| 85 | + fn mask(&self) -> u32 { |
| 86 | + let mut msk = 0; |
| 87 | + let mut iter = self.0.iter(); |
| 88 | + while let Some(pin) = iter.next() { |
| 89 | + msk |= 1 << pin.i; |
| 90 | + } |
| 91 | + msk |
| 92 | + } |
| 93 | + fn value_for_write_bsrr(&self, val: u32) -> u32 { |
| 94 | + let mut msk = 0; |
| 95 | + let mut iter = 0..SIZE; |
| 96 | + while let Some(pin) = iter.next() { |
| 97 | + let n = self.0[pin].i; |
| 98 | + msk |= 1 << (1 << (if val & (1 << pin) != 0 { n } else { n + 16 })); |
| 99 | + } |
| 100 | + msk |
| 101 | + } |
| 102 | + |
| 103 | + /// Set/reset pins according to `SIZE` lower bits |
| 104 | + #[inline(never)] |
| 105 | + pub fn write(&mut self, word: u32) { |
| 106 | + unsafe { |
| 107 | + (*Gpio::<P>::ptr()) |
| 108 | + .bsrr |
| 109 | + .write(|w| w.bits(self.value_for_write_bsrr(word))) |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + /// Sets all pins to `PinState::High` |
| 114 | + pub fn all_high(&mut self) { |
| 115 | + unsafe { (*Gpio::<P>::ptr()).bsrr.write(|w| w.bits(self.mask())) } |
| 116 | + } |
| 117 | + |
| 118 | + /// Sets all pins to `PinState::Low` |
| 119 | + pub fn all_low(&mut self) { |
| 120 | + unsafe { |
| 121 | + (*Gpio::<P>::ptr()) |
| 122 | + .bsrr |
| 123 | + .write(|w| w.bits(self.mask() << 16)) |
| 124 | + } |
| 125 | + } |
| 126 | +} |
0 commit comments