Skip to content

Commit 19a010c

Browse files
bors[bot]lucab
andauthored
Merge #344
344: common/spi: add support for all nRF52 devices r=jonas-schievink a=lucab This extends the `spi` implementation to all nRF52 devices. Some features-based conditionals are required as those devices have some slightly different register names and a varying amount of supported interfaces. Manually tested locally on a nRF52840 only. Co-authored-by: Luca BRUNO <[email protected]>
2 parents 19df163 + f9ea7c1 commit 19a010c

File tree

2 files changed

+42
-14
lines changed

2 files changed

+42
-14
lines changed

nrf-hal-common/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ pub mod rng;
6060
pub mod rtc;
6161
#[cfg(not(feature = "51"))]
6262
pub mod saadc;
63-
#[cfg(feature = "51")]
63+
#[cfg(not(feature = "9160"))]
6464
pub mod spi;
6565
#[cfg(not(feature = "51"))]
6666
pub mod spim;

nrf-hal-common/src/spi.rs

Lines changed: 41 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use core::ops::Deref;
33

44
use crate::{
55
gpio::{Floating, Input, Output, Pin, PushPull},
6-
pac::{spi0, SPI0, SPI1},
6+
pac::{spi0, SPI0},
77
};
88

99
pub use embedded_hal::{
@@ -71,16 +71,8 @@ where
7171
{
7272
pub fn new(spi: T, pins: Pins, frequency: Frequency, mode: Mode) -> Self {
7373
// Select pins.
74-
spi.pselsck
75-
.write(|w| unsafe { w.bits(pins.sck.pin().into()) });
76-
77-
// Optional pins.
78-
if let Some(ref pin) = pins.mosi {
79-
spi.pselmosi.write(|w| unsafe { w.bits(pin.pin().into()) });
80-
}
81-
if let Some(ref pin) = pins.miso {
82-
spi.pselmiso.write(|w| unsafe { w.bits(pin.pin().into()) });
83-
}
74+
let mut spi = spi;
75+
Self::set_pins(&mut spi, pins);
8476

8577
// Enable SPI instance.
8678
spi.enable.write(|w| w.enable().enabled());
@@ -99,6 +91,35 @@ where
9991
Self(spi)
10092
}
10193

94+
#[cfg(feature = "51")]
95+
fn set_pins(spi: &mut T, pins: Pins) {
96+
spi.pselsck
97+
.write(|w| unsafe { w.bits(pins.sck.pin().into()) });
98+
99+
// Optional pins.
100+
if let Some(ref pin) = pins.mosi {
101+
spi.pselmosi.write(|w| unsafe { w.bits(pin.pin().into()) });
102+
}
103+
if let Some(ref pin) = pins.miso {
104+
spi.pselmiso.write(|w| unsafe { w.bits(pin.pin().into()) });
105+
}
106+
}
107+
108+
#[cfg(not(feature = "51"))]
109+
fn set_pins(spi: &mut T, pins: Pins) {
110+
spi.psel
111+
.sck
112+
.write(|w| unsafe { w.bits(pins.sck.pin().into()) });
113+
114+
// Optional pins.
115+
if let Some(ref pin) = pins.mosi {
116+
spi.psel.mosi.write(|w| unsafe { w.bits(pin.pin().into()) });
117+
}
118+
if let Some(ref pin) = pins.miso {
119+
spi.psel.miso.write(|w| unsafe { w.bits(pin.pin().into()) });
120+
}
121+
}
122+
102123
/// Return the raw interface to the underlying SPI peripheral.
103124
pub fn free(self) -> T {
104125
self.0
@@ -137,5 +158,12 @@ mod sealed {
137158
impl sealed::Sealed for SPI0 {}
138159
impl Instance for SPI0 {}
139160

140-
impl sealed::Sealed for SPI1 {}
141-
impl Instance for SPI1 {}
161+
#[cfg(not(feature = "52810"))]
162+
impl sealed::Sealed for crate::pac::SPI1 {}
163+
#[cfg(not(feature = "52810"))]
164+
impl Instance for crate::pac::SPI1 {}
165+
166+
#[cfg(any(feature = "52832", feature = "52833", feature = "52840"))]
167+
impl sealed::Sealed for crate::pac::SPI2 {}
168+
#[cfg(any(feature = "52832", feature = "52833", feature = "52840"))]
169+
impl Instance for crate::pac::SPI2 {}

0 commit comments

Comments
 (0)