Skip to content

Commit

Permalink
Add methods to use semaphore from ISR (#52)
Browse files Browse the repository at this point in the history
  • Loading branch information
agerasev committed Mar 21, 2023
1 parent 091340a commit 21bf3be
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
11 changes: 11 additions & 0 deletions freertos-rust/src/semaphore.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::base::*;
use crate::isr::*;
use crate::shim::*;
use crate::units::*;

Expand Down Expand Up @@ -59,6 +60,16 @@ impl Semaphore {
Ok(())
}
}

/// Returns `true` on success, `false` when semaphore count already reached its limit
pub fn give_from_isr(&self, context: &mut InterruptContext) -> bool {
unsafe { freertos_rs_give_semaphore_isr(self.semaphore, context.get_task_field_mut()) == 0 }
}

/// Returns `true` on success, `false` if the semaphore was not successfully taken because it was not available
pub fn take_from_isr(&self, context: &mut InterruptContext) -> bool {
unsafe { freertos_rs_take_semaphore_isr(self.semaphore, context.get_task_field_mut()) == 0 }
}
}

impl Drop for Semaphore {
Expand Down
9 changes: 9 additions & 0 deletions freertos-rust/src/shim.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,15 @@ extern "C" {
pub fn freertos_rs_give_mutex(mutex: FreeRtosQueueHandle) -> FreeRtosBaseType;
pub fn freertos_rs_give_recursive_mutex(mutex: FreeRtosQueueHandle) -> FreeRtosBaseType;

pub fn freertos_rs_take_semaphore_isr(
semaphore: FreeRtosQueueHandle,
xHigherPriorityTaskWoken: FreeRtosBaseTypeMutPtr,
) -> FreeRtosBaseType;
pub fn freertos_rs_give_semaphore_isr(
semaphore: FreeRtosQueueHandle,
xHigherPriorityTaskWoken: FreeRtosBaseTypeMutPtr,
) -> FreeRtosBaseType;

pub fn freertos_rs_delete_semaphore(mutex: FreeRtosQueueHandle);

pub fn freertos_rs_create_binary_semaphore() -> FreeRtosQueueHandle;
Expand Down

0 comments on commit 21bf3be

Please sign in to comment.