ChibiOS/HAL 9.0.0
osal.c
Go to the documentation of this file.
1/*
2 ChibiOS - Copyright (C) 2006..2018 Giovanni Di Sirio
3
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15*/
16
17/**
18 * @file osal.c
19 * @brief OSAL module code.
20 *
21 * @addtogroup OSAL
22 * @{
23 */
24
25#include "osal.h"
26
27/*===========================================================================*/
28/* Module local definitions. */
29/*===========================================================================*/
30
31/*===========================================================================*/
32/* Module exported variables. */
33/*===========================================================================*/
34
35/**
36 * @brief Pointer to a halt error message.
37 * @note The message is meant to be retrieved by the debugger after the
38 * system halt caused by an unexpected error.
39 */
40const char *osal_halt_msg;
41
42/*===========================================================================*/
43/* Module local types. */
44/*===========================================================================*/
45
46/*===========================================================================*/
47/* Module local variables. */
48/*===========================================================================*/
49
50/*===========================================================================*/
51/* Module local functions. */
52/*===========================================================================*/
53
54/*===========================================================================*/
55/* Module exported functions. */
56/*===========================================================================*/
57
58/**
59 * @brief OSAL module initialization.
60 *
61 * @api
62 */
63void osalInit(void) {
64
65}
66
67/**
68 * @brief System halt with error message.
69 *
70 * @param[in] reason the halt message pointer
71 *
72 * @api
73 */
74#if !defined(__DOXYGEN__)
75__attribute__((weak, noreturn))
76#endif
77void osalSysHalt(const char *reason) {
78
80 osal_halt_msg = reason;
81 while (true) {
82 }
83}
84
85/**
86 * @brief Polled delay.
87 * @note The real delay is always few cycles in excess of the specified
88 * value.
89 *
90 * @param[in] cycles number of cycles
91 *
92 * @xclass
93 */
95
96 (void)cycles;
97}
98
99/**
100 * @brief System timer handler.
101 * @details The handler is used for scheduling and Virtual Timers management.
102 *
103 * @iclass
104 */
106
108}
109
110/**
111 * @brief Checks if a reschedule is required and performs it.
112 * @note I-Class functions invoked from thread context must not reschedule
113 * by themselves, an explicit reschedule using this function is
114 * required in this scenario.
115 * @note Not implemented in this simplified OSAL.
116 *
117 * @sclass
118 */
120
121}
122
123/**
124 * @brief Current system time.
125 * @details Returns the number of system ticks since the @p osalInit()
126 * invocation.
127 * @note The counter can reach its maximum and then restart from zero.
128 * @note This function can be called from any context but its atomicity
129 * is not guaranteed on architectures whose word size is less than
130 * @p systime_t size.
131 *
132 * @return The system time in ticks.
133 *
134 * @xclass
135 */
137
138 return (systime_t)0;
139}
140
141/**
142 * @brief Suspends the invoking thread for the specified time.
143 *
144 * @param[in] time the delay in system ticks, the special values are
145 * handled as follow:
146 * - @a TIME_INFINITE is allowed but interpreted as a
147 * normal time specification.
148 * - @a TIME_IMMEDIATE this value is not allowed.
149 *
150 * @sclass
151 */
153
154 (void)time;
155}
156
157/**
158 * @brief Suspends the invoking thread for the specified time.
159 *
160 * @param[in] time the delay in system ticks, the special values are
161 * handled as follow:
162 * - @a TIME_INFINITE is allowed but interpreted as a
163 * normal time specification.
164 * - @a TIME_IMMEDIATE this value is not allowed.
165 *
166 * @api
167 */
169
170 (void)time;
171}
172
173/**
174 * @brief Sends the current thread sleeping and sets a reference variable.
175 * @note This function must reschedule, it can only be called from thread
176 * context.
177 *
178 * @param[in] trp a pointer to a thread reference object
179 * @return The wake up message.
180 *
181 * @sclass
182 */
184
185 osalDbgCheck(trp != NULL);
186
187 return MSG_OK;
188}
189
190/**
191 * @brief Sends the current thread sleeping and sets a reference variable.
192 * @note This function must reschedule, it can only be called from thread
193 * context.
194 *
195 * @param[in] trp a pointer to a thread reference object
196 * @param[in] timeout the timeout in system ticks, the special values are
197 * handled as follow:
198 * - @a TIME_INFINITE the thread enters an infinite sleep
199 * state.
200 * - @a TIME_IMMEDIATE the thread is not enqueued and
201 * the function returns @p MSG_TIMEOUT as if a timeout
202 * occurred.
203 * @return The wake up message.
204 * @retval MSG_TIMEOUT if the operation timed out.
205 *
206 * @sclass
207 */
209
210 osalDbgCheck(trp != NULL);
211
212 (void)timeout;
213
214 return MSG_OK;
215}
216
217/**
218 * @brief Wakes up a thread waiting on a thread reference object.
219 * @note This function must not reschedule because it can be called from
220 * ISR context.
221 *
222 * @param[in] trp a pointer to a thread reference object
223 * @param[in] msg the message code
224 *
225 * @iclass
226 */
228
229 osalDbgCheck(trp != NULL);
230
231 (void)msg;
232}
233
234/**
235 * @brief Wakes up a thread waiting on a thread reference object.
236 * @note This function must reschedule, it can only be called from thread
237 * context.
238 *
239 * @param[in] trp a pointer to a thread reference object
240 * @param[in] msg the message code
241 *
242 * @iclass
243 */
245
246 osalDbgCheck(trp != NULL);
247
248 (void)msg;
249}
250
251/**
252 * @brief Enqueues the caller thread.
253 * @details The caller thread is enqueued and put to sleep until it is
254 * dequeued or the specified timeouts expires.
255 *
256 * @param[in] tqp pointer to the threads queue object
257 * @param[in] timeout the timeout in system ticks, the special values are
258 * handled as follow:
259 * - @a TIME_INFINITE the thread enters an infinite sleep
260 * state.
261 * - @a TIME_IMMEDIATE the thread is not enqueued and
262 * the function returns @p MSG_TIMEOUT as if a timeout
263 * occurred.
264 * @return The message from @p osalQueueWakeupOneI() or
265 * @p osalQueueWakeupAllI() functions.
266 * @retval MSG_TIMEOUT if the thread has not been dequeued within the
267 * specified timeout or if the function has been
268 * invoked with @p TIME_IMMEDIATE as timeout
269 * specification.
270 *
271 * @sclass
272 */
274
275 osalDbgCheck(tqp != NULL);
276
277 (void)timeout;
278
279 return MSG_OK;
280}
281
282/**
283 * @brief Dequeues and wakes up one thread from the queue, if any.
284 *
285 * @param[in] tqp pointer to the threads queue object
286 * @param[in] msg the message code
287 *
288 * @iclass
289 */
291
292 osalDbgCheck(tqp != NULL);
293
294 (void)msg;
295}
296
297/**
298 * @brief Dequeues and wakes up all threads from the queue.
299 *
300 * @param[in] tqp pointer to the threads queue object
301 * @param[in] msg the message code
302 *
303 * @iclass
304 */
306
307 (void)tqp;
308 (void)msg;
309}
310
311/**
312 * @brief Add flags to an event source object.
313 *
314 * @param[in] esp pointer to the event flags object
315 * @param[in] flags flags to be ORed to the flags mask
316 *
317 * @iclass
318 */
320
321 osalDbgCheck(esp != NULL);
322
323 esp->flags |= flags;
324 if (esp->cb != NULL) {
325 esp->cb(esp);
326 }
327}
328
329/**
330 * @brief Add flags to an event source object.
331 *
332 * @param[in] esp pointer to the event flags object
333 * @param[in] flags flags to be ORed to the flags mask
334 *
335 * @iclass
336 */
338
339 osalDbgCheck(esp != NULL);
340
341 osalSysLock();
342 osalEventBroadcastFlagsI(esp, flags);
344}
345
346/**
347 * @brief Event callback setup.
348 * @note The callback is invoked from ISR context and can
349 * only invoke I-Class functions. The callback is meant
350 * to wakeup the task that will handle the event by
351 * calling @p osalEventGetAndClearFlagsI().
352 * @note This function is not part of the OSAL API and is provided
353 * exclusively as an example and for convenience.
354 *
355 * @param[in] esp pointer to the event flags object
356 * @param[in] cb pointer to the callback function
357 * @param[in] param parameter to be passed to the callback function
358 *
359 * @api
360 */
363 void *param) {
364
365 osalDbgCheck(esp != NULL);
366
367 esp->cb = cb;
368 esp->param = param;
369}
370
371/**
372 * @brief Locks the specified mutex.
373 * @post The mutex is locked and inserted in the per-thread stack of owned
374 * mutexes.
375 *
376 * @param[in,out] mp pointer to the @p mutex_t object
377 *
378 * @api
379 */
381
382 osalDbgCheck(mp != NULL);
383
384 *mp = 1;
385}
386
387/**
388 * @brief Unlocks the specified mutex.
389 * @note The HAL guarantees to release mutex in reverse lock order. The
390 * mutex being unlocked is guaranteed to be the last locked mutex
391 * by the invoking thread.
392 * The implementation can rely on this behavior and eventually
393 * ignore the @p mp parameter which is supplied in order to support
394 * those OSes not supporting a stack of the owned mutexes.
395 *
396 * @param[in,out] mp pointer to the @p mutex_t object
397 *
398 * @api
399 */
401
402 osalDbgCheck(mp != NULL);
403
404 *mp = 0;
405}
406
407/** @} */
msg_t osalThreadEnqueueTimeoutS(threads_queue_t *tqp, sysinterval_t timeout)
Enqueues the caller thread.
Definition osal.c:273
void osalSysHalt(const char *reason)
System halt with error message.
Definition osal.c:77
static void osalSysLock(void)
Enters a critical zone from thread context.
Definition osal.h:601
void osalEventSetCallback(event_source_t *esp, eventcallback_t cb, void *param)
Event callback setup.
Definition osal.c:361
uint32_t eventflags_t
Type of an event flags mask.
Definition osal.h:191
static void osalSysUnlock(void)
Leaves a critical zone from thread context.
Definition osal.h:611
const char * osal_halt_msg
Pointer to a halt error message.
Definition osal.c:40
void osalThreadDequeueAllI(threads_queue_t *tqp, msg_t msg)
Dequeues and wakes up all threads from the queue.
Definition osal.c:305
struct event_source event_source_t
Type of an event flags object.
Definition osal.h:201
uint32_t rtcnt_t
Type of realtime counter.
Definition osal.h:181
int32_t msg_t
Type of a message.
Definition osal.h:159
uint32_t sysinterval_t
Type of system time interval.
Definition osal.h:169
msg_t osalThreadSuspendS(thread_reference_t *trp)
Sends the current thread sleeping and sets a reference variable.
Definition osal.c:183
void(* eventcallback_t)(event_source_t *esp)
Type of an event source callback.
Definition osal.h:208
#define MSG_OK
Definition osal.h:56
void osalThreadResumeS(thread_reference_t *trp, msg_t msg)
Wakes up a thread waiting on a thread reference object.
Definition osal.c:244
void * thread_reference_t
Type of a thread reference.
Definition osal.h:186
void osalOsRescheduleS(void)
Checks if a reschedule is required and performs it.
Definition osal.c:119
void osalThreadSleep(sysinterval_t time)
Suspends the invoking thread for the specified time.
Definition osal.c:168
static void osalSysDisable(void)
Disables interrupts globally.
Definition osal.h:582
void osalMutexLock(mutex_t *mp)
Locks the specified mutex.
Definition osal.c:380
void osalOsTimerHandlerI(void)
System timer handler.
Definition osal.c:105
void osalThreadResumeI(thread_reference_t *trp, msg_t msg)
Wakes up a thread waiting on a thread reference object.
Definition osal.c:227
uint32_t mutex_t
Type of a mutex.
Definition osal.h:229
void osalInit(void)
OSAL module initialization.
Definition osal.c:63
void osalThreadSleepS(sysinterval_t time)
Suspends the invoking thread for the specified time.
Definition osal.c:152
void osalEventBroadcastFlags(event_source_t *esp, eventflags_t flags)
Add flags to an event source object.
Definition osal.c:337
systime_t osalOsGetSystemTimeX(void)
Current system time.
Definition osal.c:136
void osalMutexUnlock(mutex_t *mp)
Unlocks the specified mutex.
Definition osal.c:400
msg_t osalThreadSuspendTimeoutS(thread_reference_t *trp, sysinterval_t timeout)
Sends the current thread sleeping and sets a reference variable.
Definition osal.c:208
#define osalDbgCheck(c)
Function parameters check.
Definition osal.h:284
uint32_t systime_t
Type of system time counter.
Definition osal.h:164
void osalSysPolledDelayX(rtcnt_t cycles)
Polled delay.
Definition osal.c:94
void osalThreadDequeueNextI(threads_queue_t *tqp, msg_t msg)
Dequeues and wakes up one thread from the queue, if any.
Definition osal.c:290
void osalEventBroadcastFlagsI(event_source_t *esp, eventflags_t flags)
Add flags to an event source object.
Definition osal.c:319
#define osalDbgCheckClassI()
I-Class state check.
Definition osal.h:298
OSAL module header.
void * param
User defined field.
Definition osal.h:221
eventcallback_t cb
Event source callback.
Definition osal.h:220
volatile eventflags_t flags
Stored event flags.
Definition osal.h:219
Type of a thread queue.
Definition osal.h:238