Skip to content

Commit f79165f

Browse files
authored
spi: fix undefined behaviour in full duplex RX DMA transfers
1 parent 52d39ef commit f79165f

File tree

2 files changed

+9
-4
lines changed

2 files changed

+9
-4
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file.
44
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
55
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
66

7+
## [Unreleased]
8+
### Fixed
9+
- Fixed undefined behaviour in SPI full duplex RX DMA transfers.
10+
711
## [0.6.0] - 2022-07-04
812
### Added
913
- Added `rcc::Lsco` to use the low-speed oscillator output.

hal/src/spi.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -148,9 +148,12 @@ const fn cpol_from_polarity(polarity: Polarity) -> bool {
148148
}
149149
}
150150

151+
// a space for the RX DMA to transfer into when TX'ing in full-duplex
152+
static mut GARBAGE: [u8; 1] = [0];
153+
151154
use sealed::SpiRegs;
152155
pub(crate) mod sealed {
153-
use super::Error;
156+
use super::{Error, GARBAGE};
154157
use crate::{
155158
dma::{self, DmaCh},
156159
pac,
@@ -261,9 +264,7 @@ pub(crate) mod sealed {
261264
.set_mem_inc(true)
262265
.set_enable(true);
263266

264-
let garbage: [u8; 1] = [0];
265-
266-
rx_dma.set_mem_addr(garbage.as_ptr() as u32);
267+
rx_dma.set_mem_addr(unsafe { GARBAGE.as_mut_ptr() } as u32);
267268
tx_dma.set_mem_addr(words.as_ptr() as u32);
268269

269270
let ndt: u32 = words.len() as u32;

0 commit comments

Comments
 (0)