Skip to content

Commit 4ea569f

Browse files
committed
both embedded-hals for SPI
1 parent ebaecba commit 4ea569f

File tree

2 files changed

+192
-0
lines changed

2 files changed

+192
-0
lines changed

src/spi.rs

+1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ pub struct Mode {
3333
}
3434

3535
mod hal_02;
36+
mod hal_1;
3637

3738
use crate::pac::{spi1, RCC, SPI1, SPI2};
3839
use crate::rcc;

src/spi/hal_1.rs

+191
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,191 @@
1+
pub use embedded_hal_one::spi::{Error, ErrorKind, Mode, Phase, Polarity};
2+
3+
impl From<Polarity> for super::Polarity {
4+
fn from(p: Polarity) -> Self {
5+
match p {
6+
Polarity::IdleLow => Self::IdleLow,
7+
Polarity::IdleHigh => Self::IdleHigh,
8+
}
9+
}
10+
}
11+
12+
impl From<Phase> for super::Phase {
13+
fn from(p: Phase) -> Self {
14+
match p {
15+
Phase::CaptureOnFirstTransition => Self::CaptureOnFirstTransition,
16+
Phase::CaptureOnSecondTransition => Self::CaptureOnSecondTransition,
17+
}
18+
}
19+
}
20+
21+
impl From<Mode> for super::Mode {
22+
fn from(m: Mode) -> Self {
23+
Self {
24+
polarity: m.polarity.into(),
25+
phase: m.phase.into(),
26+
}
27+
}
28+
}
29+
30+
impl Error for super::Error {
31+
fn kind(&self) -> ErrorKind {
32+
match self {
33+
Self::Overrun => ErrorKind::Overrun,
34+
Self::ModeFault => ErrorKind::ModeFault,
35+
Self::Crc => ErrorKind::Other,
36+
}
37+
}
38+
}
39+
40+
mod nb {
41+
use super::super::{Error, Instance, Spi, TransferModeBidi, TransferModeNormal};
42+
use embedded_hal_one::spi::nb::FullDuplex;
43+
44+
impl<SPI, PINS> FullDuplex<u8> for Spi<SPI, PINS, TransferModeNormal>
45+
where
46+
SPI: Instance,
47+
{
48+
type Error = Error;
49+
50+
fn read(&mut self) -> nb::Result<u8, Error> {
51+
self.check_read()
52+
}
53+
54+
fn write(&mut self, byte: u8) -> nb::Result<(), Error> {
55+
self.check_send(byte)
56+
}
57+
}
58+
59+
impl<SPI, PINS> FullDuplex<u8> for Spi<SPI, PINS, TransferModeBidi>
60+
where
61+
SPI: Instance,
62+
{
63+
type Error = Error;
64+
65+
fn read(&mut self) -> nb::Result<u8, Error> {
66+
self.spi.cr1.modify(|_, w| w.bidioe().clear_bit());
67+
self.check_read()
68+
}
69+
70+
fn write(&mut self, byte: u8) -> nb::Result<(), Error> {
71+
self.spi.cr1.modify(|_, w| w.bidioe().set_bit());
72+
self.check_send(byte)
73+
}
74+
}
75+
}
76+
77+
mod blocking {
78+
use super::super::{Error, Instance, Spi, TransferModeBidi, TransferModeNormal};
79+
use embedded_hal_one::spi::{
80+
blocking::{Operation, Transactional, TransferInplace, Write, WriteIter},
81+
nb::FullDuplex,
82+
};
83+
84+
impl<SPI, PINS, TRANSFER_MODE> TransferInplace<u8> for Spi<SPI, PINS, TRANSFER_MODE>
85+
where
86+
Self: FullDuplex<u8, Error = Error>,
87+
SPI: Instance,
88+
{
89+
type Error = Error;
90+
91+
fn transfer_inplace(&mut self, words: &mut [u8]) -> Result<(), Self::Error> {
92+
for word in words.iter_mut() {
93+
nb::block!(self.write(*word))?;
94+
*word = nb::block!(self.read())?;
95+
}
96+
97+
Ok(())
98+
}
99+
}
100+
101+
impl<SPI, PINS> Write<u8> for Spi<SPI, PINS, TransferModeNormal>
102+
where
103+
Self: FullDuplex<u8, Error = Error>,
104+
SPI: Instance,
105+
{
106+
type Error = Error;
107+
108+
fn write(&mut self, words: &[u8]) -> Result<(), Self::Error> {
109+
for word in words {
110+
nb::block!(<Self as FullDuplex<u8>>::write(self, *word))?;
111+
nb::block!(self.read())?;
112+
}
113+
114+
Ok(())
115+
}
116+
}
117+
118+
impl<SPI, PINS> Write<u8> for Spi<SPI, PINS, TransferModeBidi>
119+
where
120+
Self: FullDuplex<u8, Error = Error>,
121+
SPI: Instance,
122+
{
123+
type Error = Error;
124+
125+
fn write(&mut self, words: &[u8]) -> Result<(), Self::Error> {
126+
for word in words {
127+
nb::block!(<Self as FullDuplex<u8>>::write(self, *word))?;
128+
}
129+
130+
Ok(())
131+
}
132+
}
133+
134+
impl<SPI, PINS> WriteIter<u8> for Spi<SPI, PINS, TransferModeNormal>
135+
where
136+
Self: FullDuplex<u8, Error = Error>,
137+
SPI: Instance,
138+
{
139+
type Error = Error;
140+
141+
fn write_iter<WI>(&mut self, words: WI) -> Result<(), Self::Error>
142+
where
143+
WI: IntoIterator<Item = u8>,
144+
{
145+
for word in words.into_iter() {
146+
nb::block!(<Self as FullDuplex<u8>>::write(self, word))?;
147+
nb::block!(self.read())?;
148+
}
149+
150+
Ok(())
151+
}
152+
}
153+
154+
impl<SPI, PINS> WriteIter<u8> for Spi<SPI, PINS, TransferModeBidi>
155+
where
156+
Self: FullDuplex<u8, Error = Error>,
157+
SPI: Instance,
158+
{
159+
type Error = Error;
160+
161+
fn write_iter<WI>(&mut self, words: WI) -> Result<(), Self::Error>
162+
where
163+
WI: IntoIterator<Item = u8>,
164+
{
165+
for word in words.into_iter() {
166+
nb::block!(<Self as FullDuplex<u8>>::write(self, word))?;
167+
}
168+
169+
Ok(())
170+
}
171+
}
172+
173+
impl<SPI, PINS, TRANSFER_MODE, W: 'static> Transactional<W> for Spi<SPI, PINS, TRANSFER_MODE>
174+
where
175+
Self: Write<W, Error = Error> + TransferInplace<W, Error = Error>,
176+
{
177+
type Error = Error;
178+
179+
fn exec<'a>(&mut self, operations: &mut [Operation<'a, W>]) -> Result<(), Error> {
180+
for op in operations {
181+
match op {
182+
Operation::Write(w) => self.write(w)?,
183+
Operation::TransferInplace(t) => self.transfer_inplace(t)?,
184+
_ => todo!(),
185+
}
186+
}
187+
188+
Ok(())
189+
}
190+
}
191+
}

0 commit comments

Comments
 (0)