Skip to content

Commit b4b0a80

Browse files
Migrate SPI to embedded-hal 1.0
1 parent 23be68b commit b4b0a80

File tree

1 file changed

+69
-26
lines changed

1 file changed

+69
-26
lines changed

src/drivers/spi.rs

Lines changed: 69 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,14 @@
77
88
use core::marker::PhantomData;
99

10+
use embedded_hal::spi::SpiBus;
11+
1012
use crate::time::Hertz;
11-
pub use crate::traits::wg::spi::{FullDuplex, Mode, Phase, Polarity};
13+
pub use crate::traits::wg1::spi::{
14+
Error as ErrorTrait, ErrorKind, ErrorType, Mode, Operation, Phase, Polarity, SpiDevice,
15+
};
1216
use crate::typestates::pin::{
13-
flexcomm::{
14-
ChipSelect,
15-
// Trait marking I2C peripherals and pins
16-
Spi,
17-
SpiPins,
18-
},
17+
flexcomm::{ChipSelect, NoPio, Spi, SpiPins},
1918
PinId,
2019
};
2120

@@ -38,7 +37,7 @@ pub enum Error {
3837
Crc,
3938
}
4039

41-
pub type Result<T> = nb::Result<T, Error>;
40+
pub type Result<T, E = Error> = core::result::Result<T, E>;
4241

4342
/// SPI peripheral operating in master mode
4443
pub struct SpiMaster<SCK, MOSI, MISO, CS, SPI, PINS>
@@ -136,7 +135,17 @@ where
136135
}
137136
}
138137

139-
impl<SCK, MOSI, MISO, CS, SPI, PINS> FullDuplex<u8> for SpiMaster<SCK, MOSI, MISO, CS, SPI, PINS>
138+
impl ErrorTrait for Error {
139+
fn kind(&self) -> ErrorKind {
140+
match self {
141+
Self::Overrun => ErrorKind::Overrun,
142+
Self::ModeFault => ErrorKind::ModeFault,
143+
Self::Crc => ErrorKind::FrameFormat,
144+
}
145+
}
146+
}
147+
148+
impl<SCK, MOSI, MISO, CS, SPI, PINS> ErrorType for SpiMaster<SCK, MOSI, MISO, CS, SPI, PINS>
140149
where
141150
SCK: PinId,
142151
MOSI: PinId,
@@ -147,8 +156,18 @@ where
147156
// CSPIN: SpiSselPin<CS, SPI>,
148157
{
149158
type Error = Error;
159+
}
150160

151-
fn read(&mut self) -> Result<u8> {
161+
impl<SCK, MOSI, MISO, CS, SPI, PINS> SpiMaster<SCK, MOSI, MISO, CS, SPI, PINS>
162+
where
163+
SCK: PinId,
164+
MOSI: PinId,
165+
MISO: PinId,
166+
CS: PinId,
167+
SPI: Spi,
168+
PINS: SpiPins<SCK, MOSI, MISO, CS, SPI>,
169+
{
170+
fn read_one(&mut self) -> nb::Result<u8, Error> {
152171
// self.return_on_error()?;
153172
if self.spi.fifostat.read().rxnotempty().bit_is_set() {
154173
// TODO: not sure how to turn this from u32 (or u16??) into u8
@@ -160,7 +179,7 @@ where
160179
}
161180
}
162181

163-
fn send(&mut self, byte: u8) -> Result<()> {
182+
fn send_one(&mut self, byte: u8) -> nb::Result<(), Error> {
164183
// NB: UM says "Do not read-modify-write the register."
165184
// - writing 0 to upper-half word means: keep previous control settings
166185

@@ -241,28 +260,52 @@ where
241260
}
242261
}
243262

244-
impl<SCK, MOSI, MISO, CS, SPI, PINS> crate::traits::wg::blocking::spi::transfer::Default<u8>
245-
for SpiMaster<SCK, MOSI, MISO, CS, SPI, PINS>
263+
impl<SCK, MOSI, MISO, SPI, PINS> SpiBus<u8> for SpiMaster<SCK, MOSI, MISO, NoPio, SPI, PINS>
246264
where
247265
SCK: PinId,
248266
MOSI: PinId,
249267
MISO: PinId,
250-
CS: PinId,
251268
SPI: Spi,
252-
PINS: SpiPins<SCK, MOSI, MISO, CS, SPI>,
269+
PINS: SpiPins<SCK, MOSI, MISO, NoPio, SPI>,
270+
// CSPIN: SpiSselPin<CS, SPI>,
253271
{
254-
}
272+
fn read(&mut self, words: &mut [u8]) -> Result<()> {
273+
for w in words {
274+
*w = nb::block!(self.read_one())?;
275+
}
276+
Ok(())
277+
}
278+
fn write(&mut self, words: &[u8]) -> Result<()> {
279+
for w in words {
280+
nb::block!(self.send_one(*w))?;
281+
}
282+
Ok(())
283+
}
255284

256-
impl<SCK, MOSI, MISO, CS, SPI, PINS> crate::traits::wg::blocking::spi::write::Default<u8>
257-
for SpiMaster<SCK, MOSI, MISO, CS, SPI, PINS>
258-
where
259-
SCK: PinId,
260-
MOSI: PinId,
261-
MISO: PinId,
262-
CS: PinId,
263-
SPI: Spi,
264-
PINS: SpiPins<SCK, MOSI, MISO, CS, SPI>,
265-
{
285+
fn transfer(&mut self, read: &mut [u8], write: &[u8]) -> Result<()> {
286+
for i in 0..read.len().max(write.len()) {
287+
let w = write.get(i).copied().unwrap_or(0xFF);
288+
nb::block!(self.send_one(w))?;
289+
let r = nb::block!(self.read_one())?;
290+
if let Some(w) = read.get_mut(i) {
291+
*w = r;
292+
}
293+
}
294+
Ok(())
295+
}
296+
297+
fn transfer_in_place(&mut self, words: &mut [u8]) -> Result<()> {
298+
for w in words {
299+
// Corresponds to the previous implementation through embedded_hal_027::blocking::spi::transfer::Default
300+
nb::block!(self.send_one(*w))?;
301+
*w = nb::block!(self.read_one())?;
302+
}
303+
Ok(())
304+
}
305+
306+
fn flush(&mut self) -> core::result::Result<(), Self::Error> {
307+
Ok(())
308+
}
266309
}
267310

268311
// impl<SPI, PINS> crate::traits::wg::blocking::spi::transfer::Default<u8> for SpiMaster<SPI, PINS>

0 commit comments

Comments
 (0)