pros_sys/
rtos.rs

1pub const TASK_PRIORITY_MAX: u32 = 16;
2pub const TASK_PRIORITY_MIN: u32 = 1;
3pub const TASK_PRIORITY_DEFAULT: u32 = 8;
4pub const TASK_STACK_DEPTH_DEFAULT: core::ffi::c_uint = 0x2000;
5pub const TASK_STACK_DEPTH_MIN: core::ffi::c_uint = 0x200;
6pub const TASK_NAME_MAX_LEN: core::ffi::c_uint = 32;
7pub const TIMEOUT_MAX: u32 = u32::MAX;
8
9pub const E_TASK_STATE_RUNNING: core::ffi::c_uint = 0;
10pub const E_TASK_STATE_READY: core::ffi::c_uint = 1;
11pub const E_TASK_STATE_BLOCKED: core::ffi::c_uint = 2;
12pub const E_TASK_STATE_SUSPENDED: core::ffi::c_uint = 3;
13pub const E_TASK_STATE_DELETED: core::ffi::c_uint = 4;
14pub const E_TASK_STATE_INVALID: core::ffi::c_uint = 5;
15pub type task_state_e_t = core::ffi::c_uint;
16
17pub const E_NOTIFY_ACTION_NONE: core::ffi::c_uint = 0;
18pub const E_NOTIFY_ACTION_BITS: core::ffi::c_uint = 1;
19pub const E_NOTIFY_ACTION_INCR: core::ffi::c_uint = 2;
20pub const E_NOTIFY_ACTION_OWRITE: core::ffi::c_uint = 3;
21pub const E_NOTIFY_ACTION_NO_OWRITE: core::ffi::c_uint = 4;
22pub type notify_action_e_t = core::ffi::c_uint;
23
24pub type task_t = *const core::ffi::c_void;
25pub type task_fn_t = Option<unsafe extern "C" fn(arg1: *mut ::core::ffi::c_void)>;
26pub type mutex_t = *const core::ffi::c_void;
27
28const CURRENT_TASK: task_t = core::ptr::null();
29
30extern "C" {
31    /** Gets the number of milliseconds since PROS initialized.
32
33    \return The number of milliseconds since PROS initialized*/
34    pub fn millis() -> u32;
35    /** Gets the number of microseconds since PROS initialized,
36
37    \return The number of microseconds since PROS initialized*/
38    pub fn micros() -> u64;
39    /** Creates a new task and add it to the list of tasks that are ready to run.
40
41    This function uses the following values of errno when an error state is
42    reached:
43    ENOMEM - The stack cannot be used as the TCB was not created.
44
45    \param function
46    Pointer to the task entry function
47    \param parameters
48    Pointer to memory that will be used as a parameter for the task being
49    created. This memory should not typically come from stack, but rather
50    from dynamically (i.e., malloc'd) or statically allocated memory.
51    \param prio
52    The priority at which the task should run.
53    TASK_PRIO_DEFAULT plus/minus 1 or 2 is typically used.
54    \param stack_depth
55    The number of words (i.e. 4 * stack_depth) available on the task's
56    stack. TASK_STACK_DEPTH_DEFAULT is typically sufficienct.
57    \param name
58    A descriptive name for the task.  This is mainly used to facilitate
59    debugging. The name may be up to 32 characters long.
60
61    \return A handle by which the newly created task can be referenced. If an
62    error occurred, NULL will be returned and errno can be checked for hints as
63    to why task_create failed.*/
64    pub fn task_create(
65        function: task_fn_t,
66        parameters: *const core::ffi::c_void,
67        prio: u32,
68        stack_depth: u16,
69        name: *const core::ffi::c_char,
70    ) -> task_t;
71    /** Removes a task from the RTOS real time kernel's management. The task being
72    deleted will be removed from all ready, blocked, suspended and event lists.
73
74    Memory dynamically allocated by the task is not automatically freed, and
75    should be freed before the task is deleted.
76
77    \param task
78    The handle of the task to be deleted.  Passing NULL will cause the
79    calling task to be deleted.*/
80    pub fn task_delete(task: task_t);
81    /** Delays a task for a given number of milliseconds.
82
83    This is not the best method to have a task execute code at predefined
84    intervals, as the delay time is measured from when the delay is requested.
85    To delay cyclically, use task_delay_until().
86
87    \param milliseconds
88    The number of milliseconds to wait (1000 milliseconds per second)*/
89    pub fn task_delay(milliseconds: u32);
90    /** Delays a task for a given number of milliseconds.
91
92    This is not the best method to have a task execute code at predefined
93    intervals, as the delay time is measured from when the delay is requested.
94    To delay cyclically, use task_delay_until().
95
96    \param milliseconds
97    The number of milliseconds to wait (1000 milliseconds per second)*/
98    pub fn delay(milliseconds: u32);
99    /** Delays a task until a specified time.  This function can be used by periodic
100    tasks to ensure a constant execution frequency.
101
102    The task will be woken up at the time *prev_time + delta, and *prev_time will
103    be updated to reflect the time at which the task will unblock.
104
105    \param prev_time
106    A pointer to the location storing the setpoint time. This should
107    typically be initialized to the return value of millis().
108    \param delta
109    The number of milliseconds to wait (1000 milliseconds per second)*/
110    pub fn task_delay_until(prev_time: *const u32, delta: u32);
111    /** Gets the priority of the specified task.
112
113    \param task
114    The task to check
115
116    \return The priority of the task*/
117    pub fn task_get_priority(task: task_t) -> u32;
118    /** Sets the priority of the specified task.
119
120    If the specified task's state is available to be scheduled (e.g. not blocked)
121    and new priority is higher than the currently running task, a context switch
122    may occur.
123
124    \param task
125    The task to set
126    \param prio
127    The new priority of the task*/
128    pub fn task_set_priority(task: task_t, prio: u32);
129    /** Gets the state of the specified task.
130
131    \param task
132    The task to check
133
134    \return The state of the task*/
135    pub fn task_get_state(task: task_t) -> task_state_e_t;
136    /** Suspends the specified task, making it ineligible to be scheduled.
137
138    \param task
139    The task to suspend*/
140    pub fn task_suspend(task: task_t);
141    /** Resumes the specified task, making it eligible to be scheduled.
142
143    \param task
144    The task to resume*/
145    pub fn task_resume(task: task_t);
146    /** Gets the number of tasks the kernel is currently managing, including all
147    ready, blocked, or suspended tasks. A task that has been deleted, but not yet
148    reaped by the idle task will also be included in the count. Tasks recently
149    created may take one context switch to be counted.
150
151    \return The number of tasks that are currently being managed by the kernel.*/
152    pub fn task_get_count() -> u32;
153    /** Gets the name of the specified task.
154
155    \param task
156    The task to check
157
158    \return A pointer to the name of the task*/
159    pub fn task_get_name(task: task_t) -> *const core::ffi::c_char;
160    /** Gets a task handle from the specified name
161
162    The operation takes a relatively long time and should be used sparingly.
163
164    \param name
165    The name to query
166
167    \return A task handle with a matching name, or NULL if none were found.*/
168    pub fn task_get_by_name(name: *const core::ffi::c_char) -> task_t;
169    /** Get the currently running task handle. This could be useful if a task
170    wants to tell another task about itself.
171
172    \return The currently running task handle.*/
173    pub fn task_get_current() -> task_t;
174    /** Sends a simple notification to task and increments the notification counter.
175
176    See <https://pros.cs.purdue.edu/v5/tutorials/topical/notifications.html> for
177    details.
178
179    \param task
180    The task to notify
181
182    \return Always returns true.*/
183    pub fn task_notify(task: task_t) -> u32;
184    /** Utilizes task notifications to wait until specified task is complete and deleted,
185    then continues to execute the program. Analogous to std::thread::join in C++.
186
187    See <https://pros.cs.purdue.edu/v5/tutorials/topical/notifications.html> for
188    details.
189
190    \param task
191    The task to wait on.
192
193    \return void*/
194    pub fn task_join(task: task_t);
195    /** Sends a notification to a task, optionally performing some action. Will also
196    retrieve the value of the notification in the target task before modifying
197    the notification value.
198
199    See <https://pros.cs.purdue.edu/v5/tutorials/topical/notifications.html> for
200    details.
201
202    \param task
203    The task to notify
204    \param value
205    The value used in performing the action
206    \param action
207    An action to optionally perform on the receiving task's notification
208    value
209    \param prev_value
210    A pointer to store the previous value of the target task's
211    notification, may be NULL
212
213    \return Dependent on the notification action.
214    For NOTIFY_ACTION_NO_WRITE: return 0 if the value could be written without
215    needing to overwrite, 1 otherwise.
216    For all other NOTIFY_ACTION values: always return 0*/
217    pub fn task_notify_ext(
218        task: task_t,
219        value: u32,
220        action: notify_action_e_t,
221        prev_value: *const u32,
222    ) -> u32;
223    /** Waits for a notification to be nonzero.
224
225    See <https://pros.cs.purdue.edu/v5/tutorials/topical/notifications.html> for
226    details.
227
228    \param clear_on_exit
229    If true (1), then the notification value is cleared.
230    If false (0), then the notification value is decremented.
231    \param timeout
232    Specifies the amount of time to be spent waiting for a notification
233    to occur.
234
235    \return The value of the task's notification value before it is decremented
236    or cleared*/
237    pub fn task_notify_take(clear_on_exit: bool, timeout: u32) -> u32;
238    /** Clears the notification for a task.
239
240    See <https://pros.cs.purdue.edu/v5/tutorials/topical/notifications.html> for
241    details.
242
243    \param task
244    The task to clear
245
246    \return False if there was not a notification waiting, true if there was*/
247    pub fn task_notify_clear(task: task_t) -> bool;
248    /** Creates a mutex.
249
250    See <https://pros.cs.purdue.edu/v5/tutorials/topical/multitasking.html#mutexes>
251    for details.
252
253    \return A handle to a newly created mutex. If an error occurred, NULL will be
254    returned and errno can be checked for hints as to why mutex_create failed.*/
255    pub fn mutex_create() -> mutex_t;
256    /** Takes and locks a mutex, waiting for up to a certain number of milliseconds
257    before timing out.
258
259    See <https://pros.cs.purdue.edu/v5/tutorials/topical/multitasking.html#mutexes>
260    for details.
261
262    \param mutex
263    Mutex to attempt to lock.
264    \param timeout
265    Time to wait before the mutex becomes available. A timeout of 0 can
266    be used to poll the mutex. TIMEOUT_MAX can be used to block
267    indefinitely.
268
269    \return True if the mutex was successfully taken, false otherwise. If false
270    is returned, then errno is set with a hint about why the the mutex
271    couldn't be taken.*/
272    pub fn mutex_take(mutex: mutex_t, timeout: u32) -> bool;
273    /** Deletes a mutex
274
275    \param mutex
276    Mutex to unlock.*/
277    pub fn mutex_give(mutex: mutex_t) -> bool;
278    /** Deletes a mutex
279
280    \param mutex
281    Mutex to unlock.*/
282    pub fn mutex_delete(mutex: mutex_t);
283
284    /** Sets a value in a task's thread local storage array.
285
286    This function is intended for advanced users only.
287
288    Parameters:
289        xTaskToSet  The handle of the task to which the thread local data is being written. A task can write to its own thread local data by using NULL as the parameter value.
290        xIndex  The index into the thread local storage array to which data is being written.
291
292        The number of available array indexes is set by the configNUM_THREAD_LOCAL_STORAGE_POINTERS compile time configuration constant in FreeRTOSConfig.h.
293        pvValue  The value to write into the index specified by the xIndex parameter.
294
295    Example usage:
296
297    See the examples provided on the thread local storage array documentation page. */
298    pub fn vTaskSetThreadLocalStoragePointer(
299        xTaskToSet: task_t,
300        xIndex: i32,
301        pvValue: *const core::ffi::c_void,
302    );
303
304    /** Retrieves a value from a task's thread local storage array.
305
306    This function is intended for advanced users only.
307
308    Parameters:
309        xTaskToQuery  The handle of the task from which the thread local data is being read. A task can read its own thread local data by using NULL as the parameter value.
310        xIndex  The index into the thread local storage array from which data is being read.
311
312        The number of available array indexes is set by the configNUM_THREAD_LOCAL_STORAGE_POINTERS compile time configuration constant in FreeRTOSConfig.h.
313
314    Returns:
315        The values stored in index position xIndex of the thread local storage array of task xTaskToQuery.
316
317    Example usage:
318
319        See the examples provided on the thread local storage array documentation page. */
320    pub fn pvTaskGetThreadLocalStoragePointer(
321        xTaskToQuery: task_t,
322        xIndex: i32,
323    ) -> *const core::ffi::c_void;
324
325    /// Suspends the scheduler.  Suspending the scheduler prevents a context switch from occurring but leaves interrupts enabled.  If an interrupt requests a context switch while the scheduler is suspended, then the request is held pending and is performed only when the scheduler is resumed (un-suspended).
326    ///
327    ///
328    /// Calls to xTaskResumeAll() transition the scheduler out of the Suspended state following a previous call to vTaskSuspendAll().
329    ///
330    ///
331    /// Calls to vTaskSuspendAll() can be nested.  The same number of calls must be made to xTaskResumeAll() as have previously been made to vTaskSuspendAll() before the scheduler will leave the Suspended state and re-enter the Active state.
332    ///
333    ///
334    /// xTaskResumeAll() must only be called from an executing task and therefore must not be called while the scheduler is in the Initialization state (prior to the scheduler being started).
335    ///
336    ///
337    /// Other FreeRTOS API functions must not be called while the scheduler is suspended.
338    ///
339    /// API functions that have the potential to cause a context switch (for example, vTaskDelayUntil(), xQueueSend(), etc.) must not be called while the
340    /// scheduler is suspended.
341    pub fn rtos_suspend_all();
342
343    /// Resumes the scheduler after it was suspended using a call to vTaskSuspendAll().
344    ///
345    ///
346    /// xTaskResumeAll() only resumes the scheduler.  It does not unsuspend tasks
347    /// that were previously suspended by a call to vTaskSuspend().
348    pub fn rtos_resume_all() -> i32;
349}