osal_rs/freertos/
ffi.rs

1#![allow(non_upper_case_globals)]
2#![allow(non_snake_case)]
3
4use core::ffi::{c_char, c_uint, c_void};
5use core::ptr;
6
7use super::types::{BaseType, StackType, UBaseType, TickType, EventBits};
8
9pub type ThreadHandle = *const c_void;
10pub type QueueHandle = *const c_void;
11pub type SemaphoreHandle = *const c_void;
12pub type EventGroupHandle = *const c_void;
13pub type TimerHandle = *const c_void;
14pub type MutexHandle = *const c_void;
15pub type TimerCallback = unsafe extern "C" fn(timer: TimerHandle);
16pub type TaskState = c_uint;
17
18pub const RUNNING: TaskState = 0;
19pub const READY: TaskState = 1;
20pub const BLOCKED: TaskState = 2;
21pub const SUSPENDED: TaskState = 3;
22pub const DELETED: TaskState = 4;
23pub const INVALID: TaskState = 5;
24
25
26pub const pdFALSE: BaseType = 0;
27
28pub const pdTRUE: BaseType = 1;
29
30pub const pdPASS: BaseType = pdTRUE;
31
32pub const pdFAIL: BaseType = pdFALSE;
33
34pub const tskDEFAULT_INDEX_TO_NOTIFY: UBaseType = 0;
35
36pub const semBINARY_SEMAPHORE_QUEUE_LENGTH: u8 = 1;
37
38pub const semSEMAPHORE_QUEUE_ITEM_LENGTH: u8 = 0;
39
40pub const semGIVE_BLOCK_TIME: TickType = 0;
41
42pub const queueSEND_TO_BACK: BaseType = 0;
43
44pub const queueSEND_TO_FRONT: BaseType = 1;
45
46pub const queueOVERWRITE: BaseType = 2;
47
48pub const queueQUEUE_TYPE_BASE: u8 = 0;
49
50pub const queueQUEUE_TYPE_MUTEX: u8 = 1;
51
52pub const queueQUEUE_TYPE_COUNTING_SEMAPHORE: u8 = 2;
53
54pub const queueQUEUE_TYPE_BINARY_SEMAPHORE: u8 = 3;
55
56pub const queueQUEUE_TYPE_RECURSIVE_MUTEX: u8 = 4;
57
58pub const queueQUEUE_TYPE_SET: u8 = 5;
59
60
61
62#[repr(C)]
63#[derive(Debug, Copy, Clone)]
64pub struct TaskStatus {
65    pub xHandle: ThreadHandle,
66    pub pcTaskName: *const c_char,
67    pub xTaskNumber: UBaseType,
68    pub eCurrentState: TaskState,
69    pub uxCurrentPriority: UBaseType,
70    pub uxBasePriority: UBaseType,
71    pub ulRunTimeCounter: u32,
72    pub pxStackBase: *mut StackType,
73    pub usStackHighWaterMark: StackType
74}
75
76impl Default for TaskStatus {
77    fn default() -> Self {
78        TaskStatus {
79            xHandle: ptr::null(),
80            pcTaskName: ptr::null(),
81            xTaskNumber: 0,
82            eCurrentState: INVALID,
83            uxCurrentPriority: 0,
84            uxBasePriority: 0,
85            ulRunTimeCounter: 0,
86            pxStackBase: ptr::null_mut(),
87            usStackHighWaterMark: 0,
88        }
89    }
90}
91
92pub type TaskFunction = Option<unsafe extern "C" fn(arg: *mut c_void)>;
93
94unsafe extern "C" {
95    
96
97    /// Allocate memory from the  heap
98    /// 
99    /// # Arguments
100    /// * `size` - The number of bytes to allocate
101    /// 
102    /// # Returns
103    /// A pointer to the allocated memory, or null if allocation fails
104    pub fn pvPortMalloc(size: usize) -> *mut c_void;
105
106    /// Free memory previously allocated by pvPortMalloc
107    /// 
108    /// # Arguments
109    /// * `pv` - Pointer to the memory to free
110    pub fn vPortFree(pv: *mut c_void);
111
112    pub fn vTaskDelay(xTicksToDelay: TickType);
113
114    pub fn xTaskDelayUntil(
115        pxPreviousWakeTime: *mut TickType,
116        xTimeIncrement: TickType,
117    ) -> BaseType;
118
119
120    pub fn xTaskGetTickCount() -> TickType;
121
122    pub fn vTaskStartScheduler();
123
124    pub fn vTaskEndScheduler();
125
126    pub fn vTaskSuspendAll();
127
128    pub fn xTaskResumeAll() -> BaseType;
129
130    pub fn xTaskGetCurrentTaskHandle() -> ThreadHandle;
131
132    pub fn eTaskGetState(xTask: ThreadHandle) -> TaskState;
133
134    pub fn uxTaskGetNumberOfTasks() -> UBaseType;
135
136    pub fn uxTaskGetSystemState(
137        pxTaskStatusArray: *mut TaskStatus,
138        uxArraySize: UBaseType,
139        pulTotalRunTime: *mut u32,
140    ) -> UBaseType;
141
142    pub fn xTaskCreate(
143        pxTaskCode: TaskFunction,
144        pcName: *const c_char,
145        uxStackDepth: StackType,
146        pvParameters: *mut c_void,
147        uxPriority: UBaseType,
148        pxCreatedTask: *mut ThreadHandle,
149    ) -> BaseType;
150
151    pub fn vTaskDelete(xTaskToDelete: ThreadHandle);
152
153    pub fn vTaskSuspend(xTaskToSuspend: ThreadHandle);
154
155    pub fn vTaskResume(xTaskToResume: ThreadHandle);
156
157    pub fn vTaskGetInfo(
158        xTask: ThreadHandle,
159        pxTaskStatus: *mut TaskStatus,
160        xGetFreeStackSpace: BaseType,
161        eState: TaskState,
162    );
163
164    pub fn ulTaskGenericNotifyTake(uxIndexToWaitOn: UBaseType, xClearCountOnExit: BaseType, xTicksToWait: TickType) -> u32;
165
166
167    pub fn xTaskGenericNotifyWait(
168        uxIndexToWaitOn: UBaseType,
169        ulBitsToClearOnEntry: u32,
170        ulBitsToClearOnExit: u32,
171        pulNotificationValue: *mut u32,
172        xTicksToWait: TickType,
173    ) -> BaseType;
174
175
176    pub fn xTaskGenericNotify(
177        xTaskToNotify: ThreadHandle,
178        uxIndexToNotify: UBaseType,
179        ulValue: u32,
180        eAction: u32,
181        pulPreviousNotificationValue: *mut u32,
182    ) -> BaseType;
183
184
185    pub fn xTaskGenericNotifyFromISR(
186        xTaskToNotify: ThreadHandle,
187        uxIndexToNotify: UBaseType,
188        ulValue: u32,
189        eAction: u32,
190        pulPreviousNotificationValue: *mut u32,
191        pxHigherPriorityTaskWoken: *mut BaseType,
192    ) -> BaseType;
193    
194    pub fn xEventGroupWaitBits(
195        xEventGroup: EventGroupHandle,
196        uxBitsToWaitFor: EventBits,
197        xClearOnExit: BaseType,
198        xWaitForAllBits: BaseType,
199        xTicksToWait: TickType,
200    ) -> EventBits;
201
202    pub fn xEventGroupClearBits(
203        xEventGroup: EventGroupHandle,
204        uxBitsToClear: EventBits,
205    ) -> EventBits;
206
207    pub fn xEventGroupClearBitsFromISR(
208        xEventGroup: EventGroupHandle,
209        uxBitsToClear: EventBits,
210    ) -> BaseType;
211
212        pub fn xEventGroupSetBits(
213        xEventGroup: EventGroupHandle,
214        uxBitsToSet: EventBits,
215    ) -> EventBits;
216
217
218    pub fn xEventGroupSetBitsFromISR(
219        xEventGroup: EventGroupHandle,
220        uxBitsToSet: EventBits,
221        pxHigherPriorityTaskWoken: *mut BaseType,
222    ) -> BaseType;
223
224    pub fn xEventGroupGetBitsFromISR(xEventGroup: EventGroupHandle) -> EventBits;
225
226    pub fn vEventGroupDelete(xEventGroup: EventGroupHandle);
227
228    pub fn xEventGroupCreate() -> EventGroupHandle;
229
230    pub fn osal_rs_critical_section_enter();
231
232    pub fn osal_rs_critical_section_exit();
233
234    pub fn osal_rs_port_yield_from_isr(pxHigherPriorityTaskWoken: BaseType);
235
236    pub fn osal_rs_port_end_switching_isr( xSwitchRequired: BaseType );
237
238    pub fn xQueueCreateMutex(ucQueueType: u8) -> QueueHandle;
239    
240    pub fn xQueueCreateCountingSemaphore(
241        uxMaxCount: UBaseType,
242        uxInitialCount: UBaseType,
243    ) -> QueueHandle;
244
245    pub fn xQueueSemaphoreTake(xQueue: QueueHandle, xTicksToWait: TickType) -> BaseType;
246
247    pub fn xQueueReceiveFromISR(
248        xQueue: QueueHandle,
249        pvBuffer: *mut c_void,
250        pxHigherPriorityTaskWoken: *mut BaseType,
251    ) -> BaseType;
252
253    pub fn xQueueGenericSend(
254        xQueue: QueueHandle,
255        pvItemToQueue: *const c_void,
256        xTicksToWait: TickType,
257        xCopyPosition: BaseType,
258    ) -> BaseType;
259
260    pub fn xQueueGiveFromISR(
261        xQueue: QueueHandle,
262        pxHigherPriorityTaskWoken: *mut BaseType,
263    ) -> BaseType;
264
265     pub fn vQueueDelete(xQueue: QueueHandle);
266
267    pub fn xQueueGenericCreate(
268        uxQueueLength: UBaseType,
269        uxItemSize: UBaseType,
270        ucQueueType: u8,
271    ) -> QueueHandle;
272
273    pub fn xQueueReceive(
274        xQueue: QueueHandle,
275        pvBuffer: *mut c_void,
276        xTicksToWait: TickType,
277    ) -> BaseType;
278
279    pub fn xQueueGenericSendFromISR(
280        xQueue: QueueHandle,
281        pvItemToQueue: *const c_void,
282        pxHigherPriorityTaskWoken: *mut BaseType,
283        xCopyPosition: BaseType,
284    ) -> BaseType;
285
286    pub fn xQueueTakeMutexRecursive(xMutex: QueueHandle, xTicksToWait: TickType) -> BaseType;
287
288    pub fn xQueueGiveMutexRecursive(xMutex: QueueHandle) -> BaseType;
289
290    pub fn xPortGetFreeHeapSize() -> usize;
291
292    pub fn xTimerCreateTimerTask() -> BaseType;
293
294    pub fn xTimerCreate(
295        pcTimerName: *const c_char,
296        xTimerPeriodInTicks: TickType,
297        xAutoReload: BaseType,
298        pvTimerID: *mut c_void,
299        pxCallbackFunction: Option<TimerCallback>,
300    ) -> TimerHandle;
301
302    pub fn osal_rs_timer_start(xTimer: TimerHandle, xTicksToWait: TickType) -> BaseType;
303
304    pub fn osal_rs_timer_stop(xTimer: TimerHandle, xTicksToWait: TickType) -> BaseType;
305
306    pub fn osal_rs_timer_reset(xTimer: TimerHandle, xTicksToWait: TickType) -> BaseType;
307
308    pub fn osal_rs_timer_change_period(
309        xTimer: TimerHandle,
310        xNewPeriodInTicks: TickType,
311        xTicksToWait: TickType,
312    ) -> BaseType;
313
314    pub fn osal_rs_timer_delete(xTimer: TimerHandle, xTicksToWait: TickType) -> BaseType;
315
316    pub fn pvTimerGetTimerID(xTimer: TimerHandle) -> *mut c_void;
317
318    pub fn printf(fmt: *const u8, ...) -> i32; 
319}
320
321#[macro_export]
322macro_rules! ulTaskNotifyTake {
323    ($xClearCountOnExit:expr, $xTicksToWait:expr) => {
324        unsafe {
325            $crate::freertos::ffi::ulTaskGenericNotifyTake(
326                $crate::freertos::ffi::tskDEFAULT_INDEX_TO_NOTIFY,
327                $xClearCountOnExit,
328                $xTicksToWait
329            )
330        }
331    };
332}
333
334#[macro_export]
335macro_rules! xTaskNotifyWait {
336    ($ulBitsToClearOnEntry:expr, $ulBitsToClearOnExit:expr, $pulNotificationValue:expr, $xTicksToWait:expr) => {
337        unsafe {
338            $crate::freertos::ffi::xTaskGenericNotifyWait(
339                $crate::freertos::ffi::tskDEFAULT_INDEX_TO_NOTIFY,
340                $ulBitsToClearOnEntry,
341                $ulBitsToClearOnExit,
342                $pulNotificationValue,
343                $xTicksToWait
344            )
345        }
346    };
347}
348
349#[macro_export]
350macro_rules! xTaskNotify {
351    ($xTaskToNotify:expr, $ulValue:expr, $eAction:expr) => {
352        unsafe {
353            $crate::freertos::ffi::xTaskGenericNotify(
354                $xTaskToNotify,
355                $crate::freertos::ffi::tskDEFAULT_INDEX_TO_NOTIFY,
356                $ulValue,
357                $eAction,
358                core::ptr::null_mut()
359            )
360        }
361    };
362}
363
364#[macro_export]
365macro_rules! xTaskNotifyFromISR {
366    ($xTaskToNotify:expr, $ulValue:expr, $eAction:expr, $pxHigherPriorityTaskWoken:expr) => {
367        unsafe {
368            $crate::freertos::ffi::xTaskGenericNotifyFromISR(
369                $xTaskToNotify,
370                $crate::freertos::ffi::tskDEFAULT_INDEX_TO_NOTIFY,
371                $ulValue,
372                $eAction,
373                core::ptr::null_mut(),
374                $pxHigherPriorityTaskWoken
375            )
376        }
377    };
378}
379
380#[macro_export]
381macro_rules! xTaskNotifyAndQuery {
382    ($xTaskToNotify:expr, $ulValue:expr, $eAction:expr, $pulPreviousNotificationValue:expr) => {
383        unsafe {
384            $crate::freertos::ffi::xTaskGenericNotify(
385                $xTaskToNotify,
386                $crate::freertos::ffi::tskDEFAULT_INDEX_TO_NOTIFY,
387                $ulValue,
388                $eAction,
389                $pulPreviousNotificationValue
390            )
391        }
392    };
393}
394
395#[macro_export]
396macro_rules! vTaskDelayUntil {
397    ($pxPreviousWakeTime:expr, $xTimeIncrement:expr) => {
398        unsafe {
399            $crate::freertos::ffi::xTaskDelayUntil(
400                $pxPreviousWakeTime,
401                $xTimeIncrement
402            );
403        }
404    };
405}
406
407#[macro_export]
408macro_rules! xEventGroupGetBits {
409    ($xEventGroup:expr) => {
410        unsafe {
411            $crate::freertos::ffi::xEventGroupClearBits($xEventGroup, 0)
412        }
413    };
414}
415
416#[macro_export]
417macro_rules! xSemaphoreCreateCounting {
418    ($uxMaxCount:expr, $uxInitialCount:expr) => {
419        unsafe {
420            $crate::freertos::ffi::xQueueCreateCountingSemaphore(
421                $uxMaxCount,
422                $uxInitialCount
423            )
424        }
425    };
426}
427
428#[macro_export]
429macro_rules! xSemaphoreTake {
430    ($xSemaphore:expr, $xBlockTime:expr) => {
431        unsafe {
432            $crate::freertos::ffi::xQueueSemaphoreTake(
433                $xSemaphore,
434                $xBlockTime
435            )
436        }
437    };
438}
439
440#[macro_export]
441macro_rules! xSemaphoreTakeFromISR {
442    ($xSemaphore:expr, $pxHigherPriorityTaskWoken:expr) => {
443        unsafe {
444            $crate::freertos::ffi::xQueueReceiveFromISR(
445                $xSemaphore,
446                core::ptr::null_mut(),
447                $pxHigherPriorityTaskWoken
448            )
449        }
450    };
451}
452
453#[macro_export]
454macro_rules! xSemaphoreGive {
455    ($xSemaphore:expr) => {
456        unsafe {
457            $crate::freertos::ffi::xQueueGenericSend(
458                $xSemaphore,
459                core::ptr::null(),
460                $crate::freertos::ffi::semGIVE_BLOCK_TIME,
461                $crate::freertos::ffi::queueSEND_TO_BACK
462            )
463        }
464    };
465}
466
467#[macro_export]
468macro_rules! xSemaphoreGiveFromISR {
469    ($xSemaphore:expr, $pxHigherPriorityTaskWoken:expr) => {
470        unsafe {
471            $crate::freertos::ffi::xQueueGiveFromISR(
472                $xSemaphore,
473                $pxHigherPriorityTaskWoken
474            )
475        }
476    };
477}
478
479#[macro_export]
480macro_rules! vSemaphoreDelete {
481    ($xSemaphore:expr) => {
482        unsafe {
483            $crate::freertos::ffi::vQueueDelete($xSemaphore)
484        }
485    };
486}
487
488#[macro_export]
489macro_rules! xQueueCreate {
490    ($uxQueueLength:expr, $uxItemSize:expr) => {
491        unsafe {
492            $crate::freertos::ffi::xQueueGenericCreate(
493                $uxQueueLength,
494                $uxItemSize,
495                $crate::freertos::ffi::queueQUEUE_TYPE_BASE
496            )
497        }
498    };
499}
500
501#[macro_export]
502macro_rules! xQueueSendToBackFromISR {
503    ($xQueue:expr, $pvItemToQueue:expr, $pxHigherPriorityTaskWoken:expr) => {
504        unsafe {
505            $crate::freertos::ffi::xQueueGenericSendFromISR(
506                $xQueue,
507                $pvItemToQueue,
508                $pxHigherPriorityTaskWoken,
509                $crate::freertos::ffi::queueSEND_TO_BACK
510            )
511        }
512    };
513}
514
515#[macro_export]
516macro_rules! xQueueSendToBack {
517    ($xQueue:expr, $pvItemToQueue:expr, $xTicksToWait:expr) => {
518        unsafe {
519            $crate::freertos::ffi::xQueueGenericSend(
520                $xQueue,
521                $pvItemToQueue,
522                $xTicksToWait,
523                $crate::freertos::ffi::queueSEND_TO_BACK
524            )
525        }
526    };
527}
528
529#[macro_export]
530macro_rules! xSemaphoreCreateRecursiveMutex {
531    () => {
532        unsafe {
533            $crate::freertos::ffi::xQueueCreateMutex(
534                $crate::freertos::ffi::queueQUEUE_TYPE_RECURSIVE_MUTEX
535            )
536        }
537    };
538}
539
540#[macro_export]
541macro_rules! xSemaphoreTakeRecursive {
542    ($xMutex:expr, $xBlockTime:expr) => {
543        unsafe {
544            $crate::freertos::ffi::xQueueTakeMutexRecursive(
545                $xMutex,
546                $xBlockTime
547            )
548        }
549    };
550}
551
552#[macro_export]
553macro_rules! xSemaphoreGiveRecursive {
554    ($xMutex:expr) => {
555        unsafe {
556            $crate::freertos::ffi::xQueueGiveMutexRecursive($xMutex)
557        }
558    };
559}