Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 11 additions & 9 deletions examples/spi-example.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
use hal::{
delay::DelayFromCountDownTimer,
gpio::{AF5, PA5, PA6, PA7},
hal_02::spi::FullDuplex,
prelude::*,
pwr::PwrExt,
rcc::Config,
Expand Down Expand Up @@ -49,17 +48,20 @@ fn main() -> ! {

// "Hello world!"
let message = b"Hello world!";
let mut received_byte: u8;
let mut received_msg = [0u8; 12];

loop {
for &byte in message {
cs.set_low();
spi.send(byte).unwrap();
received_byte = nb::block!(FullDuplex::read(&mut spi)).unwrap();
cs.set_high();
cs.set_low();
// write only, nothing is received
spi.write(message).unwrap();
cs.set_high();

cs.set_low();
// transmit and receive at the same time
spi.transfer(&mut received_msg, message).unwrap();
info!("{received_msg:?}");
cs.set_high();

info!("{}", received_byte as char);
}
delay_tim2.delay_ms(1000);
}
}
7 changes: 6 additions & 1 deletion src/spi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,20 +186,24 @@ impl<SPI: Instance, PINS> Spi<SPI, PINS> {
fn read_unchecked<W: FrameSize>(&mut self) -> W {
// NOTE(read_volatile) read only 1 byte (the svd2rust API only allows
// reading a half-word)
unsafe { ptr::read_volatile(&self.spi.dr() as *const _ as *const W) }
unsafe { ptr::read_volatile(self.spi.dr().as_ptr() as *const W) }
}
#[inline]
fn write_unchecked<W: FrameSize>(&mut self, word: W) {
// NOTE(write_volatile) see note above
let dr = self.spi.dr().as_ptr() as *mut W;
unsafe { ptr::write_volatile(dr, word) };
}
/// disables rx
#[inline]
pub fn set_tx_only(&mut self) {
// very counter-intuitively, setting spi bidi mode on disables rx while transmitting
// it's made for half-duplex spi, which they called bidirectional in the manual
self.spi
.cr1()
.modify(|_, w| w.bidimode().bidirectional().bidioe().output_enabled());
}
/// re-enables rx if it was disabled
#[inline]
pub fn set_bidi(&mut self) {
self.spi
Expand Down Expand Up @@ -314,6 +318,7 @@ impl<SPI: Instance, PINS: Pins<SPI>> embedded_hal::spi::SpiBus<u8> for Spi<SPI,
self.set_bidi();

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

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