Skip to content

Commit

Permalink
Allow starting timer from interrupt.
Browse files Browse the repository at this point in the history
  • Loading branch information
reitermarkus authored and niondir committed Sep 16, 2022
1 parent 7d5bcef commit 8ec4cde
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 0 deletions.
7 changes: 7 additions & 0 deletions freertos-rust/src/freertos/shim.c
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,13 @@ BaseType_t freertos_rs_timer_start(TimerHandle_t timer, TickType_t block_time) {
return 0;
}

BaseType_t freertos_rs_timer_start_from_isr(TimerHandle_t timer, BaseType_t* xHigherPriorityTaskWoken) {
if (xTimerStartFromISR(timer, xHigherPriorityTaskWoken) != pdPASS) {
return 1;
}
return 0;
}

BaseType_t freertos_rs_timer_stop(TimerHandle_t timer, TickType_t block_time) {
if (xTimerStop(timer, block_time) != pdPASS) {
return 1;
Expand Down
4 changes: 4 additions & 0 deletions freertos-rust/src/shim.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,10 @@ extern "C" {
timer: FreeRtosTimerHandle,
block_time: FreeRtosTickType,
) -> FreeRtosBaseType;
pub fn freertos_rs_timer_start_from_isr(
timer: FreeRtosTimerHandle,
xHigherPriorityTaskWoken: FreeRtosBaseTypeMutPtr,
) -> FreeRtosBaseType;
pub fn freertos_rs_timer_stop(
timer: FreeRtosTimerHandle,
block_time: FreeRtosTickType,
Expand Down
17 changes: 17 additions & 0 deletions freertos-rust/src/timers.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::InterruptContext;
use crate::base::*;
use crate::prelude::v1::*;
use crate::shim::*;
Expand Down Expand Up @@ -69,6 +70,11 @@ impl Timer {
}
}

/// Create a timer from a raw handle.
pub unsafe fn from_raw_handle(handle: FreeRtosTimerHandle) -> Self {
Self { handle, detached: false }
}

unsafe fn spawn_inner<'a>(
name: &str,
period_ticks: FreeRtosTickType,
Expand Down Expand Up @@ -147,6 +153,17 @@ impl Timer {
}
}

/// Start the timer from an interrupt.
pub fn start_from_isr(&self, context: &InterruptContext) -> Result<(), FreeRtosError> {
unsafe {
if freertos_rs_timer_start_from_isr(self.handle, context.get_task_field_mut()) == 0 {
Ok(())
} else {
Err(FreeRtosError::QueueSendTimeout)
}
}
}

/// Stop the timer.
pub fn stop<D: DurationTicks>(&self, block_time: D) -> Result<(), FreeRtosError> {
unsafe {
Expand Down

0 comments on commit 8ec4cde

Please sign in to comment.