freertos_sys2/
lib.rs

1//! freertos-sys2 provides low-level bindings to FreeRTOS functions
2//!
3//! NOTE: this is currently a very incomplete selection of function signatures, that match the
4//! selection we needed. Additionally, the presence of some of these functions vary based on
5//! FreeRTOS configuration, as does the types used.
6#![no_std]
7// The types, values, functions, and other items should match the naming used in FreeRTOS
8#![allow(non_camel_case_types, non_upper_case_globals, non_snake_case)]
9#![forbid(unsafe_op_in_unsafe_fn)]
10
11use core::ffi::c_void;
12
13extern "C" {
14    pub fn xTaskGetTickCountFromISR() -> TickType_t;
15    pub fn xTaskGetTickCount() -> TickType_t;
16
17    pub fn xTaskGetCurrentTaskHandle() -> TaskHandle_t;
18
19    pub fn vTaskResume(xTaskToResume: TaskHandle_t);
20    pub fn vTaskSuspend(xTaskToSuspend: TaskHandle_t);
21    pub fn vTaskDelayUntil(pxPreviousWakeTime: *mut TickType_t, xTimeIncrement: TickType_t);
22    pub fn xTaskDelayUntil(
23        pxPreviousWakeTime: *mut TickType_t,
24        xTimeIncrement: TickType_t,
25    ) -> BaseType_t;
26    pub fn vTaskDelay(xTicksToDelay: TickType_t);
27
28    pub fn pvTaskGetThreadLocalStoragePointer(
29        xTaskToQuery: TaskHandle_t,
30        xIndex: BaseType_t,
31    ) -> *mut c_void;
32
33    pub fn vTaskSetThreadLocalStoragePointer(
34        xTaskToSet: TaskHandle_t,
35        xIndex: BaseType_t,
36        pvValue: *mut c_void,
37    );
38
39    pub fn xQueueGenericReceive(
40        xQueue: QueueHandle_t,
41        pvBuffer: *const c_void,
42        xTicksToWait: TickType_t,
43        xJustPeek: BaseType_t,
44    ) -> BaseType_t;
45
46    pub fn xQueueGenericSend(
47        xQueue: QueueHandle_t,
48        pvItemToQueue: *const c_void,
49        xTicksToWait: TickType_t,
50        xCopyPosition: BaseType_t,
51    ) -> BaseType_t;
52
53    /*
54    pub fn xQueueGenericCreateStatic(
55        uxQueueLength: UBaseType_t,
56        uxItemSize: UBaseType_t,
57        pucQueueStorage: *mut u8,
58        pxStaticQueue: *mut StaticQueue_t,
59        ucQueueType: u8,
60    ) -> QueueHandle_t;
61    */
62
63    pub fn xQueueCreateMutex(ucQueueType: u8) -> QueueHandle_t;
64
65    pub fn vQueueDelete(xQueue: QueueHandle_t);
66
67    pub fn pvPortMalloc(xWantedSize: usize) -> *mut c_void;
68    pub fn vPortFree(pv: *mut c_void);
69}
70
71pub type BaseType_t = i32;
72pub type UBaseType_t = u32;
73
74// FIXME: these function definitions rely on TickType_t being uint32_t, freertos can be configured
75// to use different tick sizes. check `FreeRTOS_config.h`. We may need to provide a feature to
76// adjust this.
77pub type TickType_t = u32;
78
79pub type TaskHandle_t = *mut c_void;
80pub type QueueHandle_t = *mut c_void;
81pub type SemaphoreHandle_t = *mut c_void;
82
83// NOTE: items below here are defined as macros in freertos9, and should be scrutinized if one makes
84// changes to freertos
85
86pub const pdFALSE: BaseType_t = 0;
87pub const pdTRUE: BaseType_t = 1;
88
89pub const queueSEND_TO_BACK: BaseType_t = 0;
90pub const queueSEND_TO_FRONT: BaseType_t = 1;
91pub const queueOVERWRITE: BaseType_t = 2;
92
93pub const semGIVE_BLOCK_TIME: TickType_t = 0;
94
95pub const queueQUEUE_TYPE_BASE: u8 = 0;
96pub const queueQUEUE_TYPE_SET: u8 = 0;
97pub const queueQUEUE_TYPE_MUTEX: u8 = 1;
98pub const queueQUEUE_TYPE_COUNTING_SEMAPHORE: u8 = 2;
99pub const queueQUEUE_TYPE_BINARY_SEMAPHORE: u8 = 3;
100pub const queueQUEUE_TYPE_RECURSIVE_MUTEX: u8 = 4;
101
102pub const portMAX_DELAY: TickType_t = TickType_t::MAX;
103
104pub unsafe fn xSemaphoreTake(xSemaphore: SemaphoreHandle_t, xBlockTime: TickType_t) -> BaseType_t {
105    unsafe { xQueueGenericReceive(xSemaphore, core::ptr::null(), xBlockTime, pdFALSE) }
106}
107
108pub unsafe fn xSemaphoreGive(xSemaphore: SemaphoreHandle_t) -> BaseType_t {
109    unsafe {
110        xQueueGenericSend(
111            xSemaphore,
112            core::ptr::null(),
113            semGIVE_BLOCK_TIME,
114            queueSEND_TO_BACK,
115        )
116    }
117}
118
119pub unsafe fn xSemaphoreCreateMutex() -> SemaphoreHandle_t {
120    unsafe { xQueueCreateMutex(queueQUEUE_TYPE_MUTEX) }
121}
122
123pub unsafe fn vSemaphoreDelete(xSemaphore: SemaphoreHandle_t) {
124    unsafe { vQueueDelete(xSemaphore) }
125}