diff options
| author | Dario Nieuwenhuis <[email protected]> | 2022-02-12 01:04:01 +0100 |
|---|---|---|
| committer | Dario Nieuwenhuis <[email protected]> | 2022-02-12 01:07:02 +0100 |
| commit | 6de02bb23e270141885e24719dc8fbca0bb97feb (patch) | |
| tree | 01d6d2d13c3df50fff429ec06190ef27ac412e3f /embassy-nrf/src/spim.rs | |
| parent | 5ae4e20f8654bdc129d152b5364b6864457c2e02 (diff) | |
nrf: remove OptionalPin
Diffstat (limited to 'embassy-nrf/src/spim.rs')
| -rw-r--r-- | embassy-nrf/src/spim.rs | 61 |
1 files changed, 51 insertions, 10 deletions
diff --git a/embassy-nrf/src/spim.rs b/embassy-nrf/src/spim.rs index cd43b26e6..976a546ce 100644 --- a/embassy-nrf/src/spim.rs +++ b/embassy-nrf/src/spim.rs | |||
| @@ -8,9 +8,9 @@ use embassy::util::Unborrow; | |||
| 8 | use embassy_hal_common::unborrow; | 8 | use embassy_hal_common::unborrow; |
| 9 | use futures::future::poll_fn; | 9 | use futures::future::poll_fn; |
| 10 | 10 | ||
| 11 | use crate::gpio; | ||
| 12 | use crate::gpio::sealed::Pin as _; | 11 | use crate::gpio::sealed::Pin as _; |
| 13 | use crate::gpio::{OptionalPin, Pin as GpioPin}; | 12 | use crate::gpio::{self, AnyPin}; |
| 13 | use crate::gpio::{Pin as GpioPin, PselBits}; | ||
| 14 | use crate::interrupt::Interrupt; | 14 | use crate::interrupt::Interrupt; |
| 15 | use crate::util::{slice_ptr_parts, slice_ptr_parts_mut}; | 15 | use crate::util::{slice_ptr_parts, slice_ptr_parts_mut}; |
| 16 | use crate::{pac, util::slice_in_ram_or}; | 16 | use crate::{pac, util::slice_in_ram_or}; |
| @@ -51,36 +51,77 @@ impl Default for Config { | |||
| 51 | 51 | ||
| 52 | impl<'d, T: Instance> Spim<'d, T> { | 52 | impl<'d, T: Instance> Spim<'d, T> { |
| 53 | pub fn new( | 53 | pub fn new( |
| 54 | _spim: impl Unborrow<Target = T> + 'd, | 54 | spim: impl Unborrow<Target = T> + 'd, |
| 55 | irq: impl Unborrow<Target = T::Interrupt> + 'd, | ||
| 56 | sck: impl Unborrow<Target = impl GpioPin> + 'd, | ||
| 57 | miso: impl Unborrow<Target = impl GpioPin> + 'd, | ||
| 58 | mosi: impl Unborrow<Target = impl GpioPin> + 'd, | ||
| 59 | config: Config, | ||
| 60 | ) -> Self { | ||
| 61 | unborrow!(sck, miso, mosi); | ||
| 62 | Self::new_inner( | ||
| 63 | spim, | ||
| 64 | irq, | ||
| 65 | sck.degrade(), | ||
| 66 | Some(miso.degrade()), | ||
| 67 | Some(mosi.degrade()), | ||
| 68 | config, | ||
| 69 | ) | ||
| 70 | } | ||
| 71 | |||
| 72 | pub fn new_txonly( | ||
| 73 | spim: impl Unborrow<Target = T> + 'd, | ||
| 55 | irq: impl Unborrow<Target = T::Interrupt> + 'd, | 74 | irq: impl Unborrow<Target = T::Interrupt> + 'd, |
| 56 | sck: impl Unborrow<Target = impl GpioPin> + 'd, | 75 | sck: impl Unborrow<Target = impl GpioPin> + 'd, |
| 57 | miso: impl Unborrow<Target = impl OptionalPin> + 'd, | 76 | mosi: impl Unborrow<Target = impl GpioPin> + 'd, |
| 58 | mosi: impl Unborrow<Target = impl OptionalPin> + 'd, | 77 | config: Config, |
| 78 | ) -> Self { | ||
| 79 | unborrow!(sck, mosi); | ||
| 80 | Self::new_inner(spim, irq, sck.degrade(), None, Some(mosi.degrade()), config) | ||
| 81 | } | ||
| 82 | |||
| 83 | pub fn new_rxonly( | ||
| 84 | spim: impl Unborrow<Target = T> + 'd, | ||
| 85 | irq: impl Unborrow<Target = T::Interrupt> + 'd, | ||
| 86 | sck: impl Unborrow<Target = impl GpioPin> + 'd, | ||
| 87 | miso: impl Unborrow<Target = impl GpioPin> + 'd, | ||
| 88 | config: Config, | ||
| 89 | ) -> Self { | ||
| 90 | unborrow!(sck, miso); | ||
| 91 | Self::new_inner(spim, irq, sck.degrade(), Some(miso.degrade()), None, config) | ||
| 92 | } | ||
| 93 | |||
| 94 | fn new_inner( | ||
| 95 | _spim: impl Unborrow<Target = T> + 'd, | ||
| 96 | irq: impl Unborrow<Target = T::Interrupt> + 'd, | ||
| 97 | sck: AnyPin, | ||
| 98 | miso: Option<AnyPin>, | ||
| 99 | mosi: Option<AnyPin>, | ||
| 59 | config: Config, | 100 | config: Config, |
| 60 | ) -> Self { | 101 | ) -> Self { |
| 61 | unborrow!(irq, sck, miso, mosi); | 102 | unborrow!(irq); |
| 62 | 103 | ||
| 63 | let r = T::regs(); | 104 | let r = T::regs(); |
| 64 | 105 | ||
| 65 | // Configure pins | 106 | // Configure pins |
| 66 | sck.conf().write(|w| w.dir().output().drive().h0h1()); | 107 | sck.conf().write(|w| w.dir().output().drive().h0h1()); |
| 67 | if let Some(mosi) = mosi.pin_mut() { | 108 | if let Some(mosi) = &mosi { |
| 68 | mosi.conf().write(|w| w.dir().output().drive().h0h1()); | 109 | mosi.conf().write(|w| w.dir().output().drive().h0h1()); |
| 69 | } | 110 | } |
| 70 | if let Some(miso) = miso.pin_mut() { | 111 | if let Some(miso) = &miso { |
| 71 | miso.conf().write(|w| w.input().connect().drive().h0h1()); | 112 | miso.conf().write(|w| w.input().connect().drive().h0h1()); |
| 72 | } | 113 | } |
| 73 | 114 | ||
| 74 | match config.mode.polarity { | 115 | match config.mode.polarity { |
| 75 | Polarity::IdleHigh => { | 116 | Polarity::IdleHigh => { |
| 76 | sck.set_high(); | 117 | sck.set_high(); |
| 77 | if let Some(mosi) = mosi.pin_mut() { | 118 | if let Some(mosi) = &mosi { |
| 78 | mosi.set_high(); | 119 | mosi.set_high(); |
| 79 | } | 120 | } |
| 80 | } | 121 | } |
| 81 | Polarity::IdleLow => { | 122 | Polarity::IdleLow => { |
| 82 | sck.set_low(); | 123 | sck.set_low(); |
| 83 | if let Some(mosi) = mosi.pin_mut() { | 124 | if let Some(mosi) = &mosi { |
| 84 | mosi.set_low(); | 125 | mosi.set_low(); |
| 85 | } | 126 | } |
| 86 | } | 127 | } |
