Skip to content

Commit 3ce300c

Browse files
kalkyljamesmunns
authored andcommitted
Change try_acquire to return Result, add SemaphoreNotAvailable error, update docs
1 parent 354af99 commit 3ce300c

File tree

1 file changed

+15
-4
lines changed

1 file changed

+15
-4
lines changed

nrf-hal-common/src/spis.rs

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@ impl<T> Spis<T>
4141
where
4242
T: Instance,
4343
{
44-
/// Takes ownership of the raw SPIS peripheral, returning a safe wrapper.
44+
/// Takes ownership of the raw SPIS peripheral and relevant pins,
45+
/// returning a safe wrapper.
4546
pub fn new(spis: T, pins: Pins) -> Self {
4647
spis.psel.sck.write(|w| {
4748
unsafe { w.pin().bits(pins.sck.pin()) };
@@ -166,11 +167,20 @@ where
166167
self
167168
}
168169

169-
/// Requests acquiring the SPIS semaphore.
170+
/// Requests acquiring the SPIS semaphore, returning an error if not
171+
/// possible.
172+
///
173+
/// Note: The semaphore will still be requested, and will be made
174+
/// available at a later point.
170175
#[inline(always)]
171-
pub fn try_acquire(&self) -> &Self {
176+
pub fn try_acquire(&self) -> Result<&Self, Error> {
177+
compiler_fence(Ordering::SeqCst);
172178
self.spis.tasks_acquire.write(|w| unsafe { w.bits(1) });
173-
self
179+
if self.spis.events_acquired.read().bits() != 0 {
180+
Ok(self)
181+
} else {
182+
Err(Error::SemaphoreNotAvailable)
183+
}
174184
}
175185

176186
/// Releases the SPIS semaphore, enabling the SPIS to acquire it.
@@ -566,6 +576,7 @@ pub enum Mode {
566576
pub enum Error {
567577
DMABufferNotInDataMemory,
568578
BufferTooLong,
579+
SemaphoreNotAvailable,
569580
}
570581

571582
/// GPIO pins for SPIS interface.

0 commit comments

Comments
 (0)