Skip to content

Commit 6abbaef

Browse files
authored
Merge branch 'stm32-rs:main' into main
2 parents 033b58a + c0c0805 commit 6abbaef

File tree

3 files changed

+190
-104
lines changed

3 files changed

+190
-104
lines changed

examples/spi-example.rs

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
use hal::{
99
delay::DelayFromCountDownTimer,
1010
gpio::{AF5, PA5, PA6, PA7},
11-
hal_02::spi::FullDuplex,
1211
prelude::*,
1312
pwr::PwrExt,
1413
rcc::Config,
@@ -19,8 +18,8 @@ use hal::{
1918
};
2019

2120
use cortex_m_rt::entry;
22-
use log::info;
2321
use stm32g4xx_hal as hal;
22+
use utils::logger::info;
2423

2524
#[macro_use]
2625
mod utils;
@@ -49,17 +48,20 @@ fn main() -> ! {
4948

5049
// "Hello world!"
5150
let message = b"Hello world!";
52-
let mut received_byte: u8;
51+
let mut received_msg = [0u8; 12];
5352

5453
loop {
55-
for &byte in message {
56-
cs.set_low();
57-
spi.send(byte).unwrap();
58-
received_byte = nb::block!(FullDuplex::read(&mut spi)).unwrap();
59-
cs.set_high();
54+
cs.set_low();
55+
// write only, nothing is received
56+
spi.write(message).unwrap();
57+
cs.set_high();
58+
59+
cs.set_low();
60+
// transmit and receive at the same time
61+
spi.transfer(&mut received_msg, message).unwrap();
62+
info!("{:?}", &received_msg);
63+
cs.set_high();
6064

61-
info!("{}", received_byte as char);
62-
}
6365
delay_tim2.delay_ms(1000);
6466
}
6567
}

src/spi.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,20 +186,24 @@ impl<SPI: Instance, PINS> Spi<SPI, PINS> {
186186
fn read_unchecked<W: FrameSize>(&mut self) -> W {
187187
// NOTE(read_volatile) read only 1 byte (the svd2rust API only allows
188188
// reading a half-word)
189-
unsafe { ptr::read_volatile(&self.spi.dr() as *const _ as *const W) }
189+
unsafe { ptr::read_volatile(self.spi.dr().as_ptr() as *const W) }
190190
}
191191
#[inline]
192192
fn write_unchecked<W: FrameSize>(&mut self, word: W) {
193193
// NOTE(write_volatile) see note above
194194
let dr = self.spi.dr().as_ptr() as *mut W;
195195
unsafe { ptr::write_volatile(dr, word) };
196196
}
197+
/// disables rx
197198
#[inline]
198199
pub fn set_tx_only(&mut self) {
200+
// very counter-intuitively, setting spi bidi mode on disables rx while transmitting
201+
// it's made for half-duplex spi, which they called bidirectional in the manual
199202
self.spi
200203
.cr1()
201204
.modify(|_, w| w.bidimode().bidirectional().bidioe().output_enabled());
202205
}
206+
/// re-enables rx if it was disabled
203207
#[inline]
204208
pub fn set_bidi(&mut self) {
205209
self.spi
@@ -314,6 +318,7 @@ impl<SPI: Instance, PINS: Pins<SPI>> embedded_hal::spi::SpiBus<u8> for Spi<SPI,
314318
self.set_bidi();
315319

316320
let half_len = len / 2;
321+
// leftover write/read operation because bytes are not a multiple of 2
317322
let pair_left = len % 2;
318323

319324
// prefill write fifo so that the clock doen't stop while fetch the read byte

0 commit comments

Comments
 (0)