ChibiOS/HAL  6.1.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  */
40 const 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  */
63 void 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
77 void 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  */
119 void osalOsRescheduleS(void) {
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  *
151  * @sclass
152  */
154 
155  (void)time;
156 }
157 
158 /**
159  * @brief Suspends the invoking thread for the specified time.
160  *
161  * @param[in] time the delay in system ticks, the special values are
162  * handled as follow:
163  * - @a TIME_INFINITE is allowed but interpreted as a
164  * normal time specification.
165  * - @a TIME_IMMEDIATE this value is not allowed.
166  * .
167  *
168  * @api
169  */
171 
172  (void)time;
173 }
174 
175 /**
176  * @brief Sends the current thread sleeping and sets a reference variable.
177  * @note This function must reschedule, it can only be called from thread
178  * context.
179  *
180  * @param[in] trp a pointer to a thread reference object
181  * @return The wake up message.
182  *
183  * @sclass
184  */
186 
187  osalDbgCheck(trp != NULL);
188 
189  return MSG_OK;
190 }
191 
192 /**
193  * @brief Sends the current thread sleeping and sets a reference variable.
194  * @note This function must reschedule, it can only be called from thread
195  * context.
196  *
197  * @param[in] trp a pointer to a thread reference object
198  * @param[in] timeout the timeout in system ticks, the special values are
199  * handled as follow:
200  * - @a TIME_INFINITE the thread enters an infinite sleep
201  * state.
202  * - @a TIME_IMMEDIATE the thread is not enqueued and
203  * the function returns @p MSG_TIMEOUT as if a timeout
204  * occurred.
205  * .
206  * @return The wake up message.
207  * @retval MSG_TIMEOUT if the operation timed out.
208  *
209  * @sclass
210  */
212 
213  osalDbgCheck(trp != NULL);
214 
215  (void)timeout;
216 
217  return MSG_OK;
218 }
219 
220 /**
221  * @brief Wakes up a thread waiting on a thread reference object.
222  * @note This function must not reschedule because it can be called from
223  * ISR context.
224  *
225  * @param[in] trp a pointer to a thread reference object
226  * @param[in] msg the message code
227  *
228  * @iclass
229  */
231 
232  osalDbgCheck(trp != NULL);
233 
234  (void)msg;
235 }
236 
237 /**
238  * @brief Wakes up a thread waiting on a thread reference object.
239  * @note This function must reschedule, it can only be called from thread
240  * context.
241  *
242  * @param[in] trp a pointer to a thread reference object
243  * @param[in] msg the message code
244  *
245  * @iclass
246  */
248 
249  osalDbgCheck(trp != NULL);
250 
251  (void)msg;
252 }
253 
254 /**
255  * @brief Enqueues the caller thread.
256  * @details The caller thread is enqueued and put to sleep until it is
257  * dequeued or the specified timeouts expires.
258  *
259  * @param[in] tqp pointer to the threads queue object
260  * @param[in] timeout the timeout in system ticks, the special values are
261  * handled as follow:
262  * - @a TIME_INFINITE the thread enters an infinite sleep
263  * state.
264  * - @a TIME_IMMEDIATE the thread is not enqueued and
265  * the function returns @p MSG_TIMEOUT as if a timeout
266  * occurred.
267  * .
268  * @return The message from @p osalQueueWakeupOneI() or
269  * @p osalQueueWakeupAllI() functions.
270  * @retval MSG_TIMEOUT if the thread has not been dequeued within the
271  * specified timeout or if the function has been
272  * invoked with @p TIME_IMMEDIATE as timeout
273  * specification.
274  *
275  * @sclass
276  */
278 
279  osalDbgCheck(tqp != NULL);
280 
281  (void)timeout;
282 
283  return MSG_OK;
284 }
285 
286 /**
287  * @brief Dequeues and wakes up one thread from the queue, if any.
288  *
289  * @param[in] tqp pointer to the threads queue object
290  * @param[in] msg the message code
291  *
292  * @iclass
293  */
295 
296  osalDbgCheck(tqp != NULL);
297 
298  (void)msg;
299 }
300 
301 /**
302  * @brief Dequeues and wakes up all threads from the queue.
303  *
304  * @param[in] tqp pointer to the threads queue object
305  * @param[in] msg the message code
306  *
307  * @iclass
308  */
310 
311  (void)tqp;
312  (void)msg;
313 }
314 
315 /**
316  * @brief Add flags to an event source object.
317  *
318  * @param[in] esp pointer to the event flags object
319  * @param[in] flags flags to be ORed to the flags mask
320  *
321  * @iclass
322  */
324 
325  osalDbgCheck(esp != NULL);
326 
327  esp->flags |= flags;
328  if (esp->cb != NULL) {
329  esp->cb(esp);
330  }
331 }
332 
333 /**
334  * @brief Add flags to an event source object.
335  *
336  * @param[in] esp pointer to the event flags object
337  * @param[in] flags flags to be ORed to the flags mask
338  *
339  * @iclass
340  */
342 
343  osalDbgCheck(esp != NULL);
344 
345  osalSysLock();
346  osalEventBroadcastFlagsI(esp, flags);
347  osalSysUnlock();
348 }
349 
350 /**
351  * @brief Event callback setup.
352  * @note The callback is invoked from ISR context and can
353  * only invoke I-Class functions. The callback is meant
354  * to wakeup the task that will handle the event by
355  * calling @p osalEventGetAndClearFlagsI().
356  * @note This function is not part of the OSAL API and is provided
357  * exclusively as an example and for convenience.
358  *
359  * @param[in] esp pointer to the event flags object
360  * @param[in] cb pointer to the callback function
361  * @param[in] param parameter to be passed to the callback function
362  *
363  * @api
364  */
366  eventcallback_t cb,
367  void *param) {
368 
369  osalDbgCheck(esp != NULL);
370 
371  esp->cb = cb;
372  esp->param = param;
373 }
374 
375 /**
376  * @brief Locks the specified mutex.
377  * @post The mutex is locked and inserted in the per-thread stack of owned
378  * mutexes.
379  *
380  * @param[in,out] mp pointer to the @p mutex_t object
381  *
382  * @api
383  */
385 
386  osalDbgCheck(mp != NULL);
387 
388  *mp = 1;
389 }
390 
391 /**
392  * @brief Unlocks the specified mutex.
393  * @note The HAL guarantees to release mutex in reverse lock order. The
394  * mutex being unlocked is guaranteed to be the last locked mutex
395  * by the invoking thread.
396  * The implementation can rely on this behavior and eventually
397  * ignore the @p mp parameter which is supplied in order to support
398  * those OSes not supporting a stack of the owned mutexes.
399  *
400  * @param[in,out] mp pointer to the @p mutex_t object
401  *
402  * @api
403  */
405 
406  osalDbgCheck(mp != NULL);
407 
408  *mp = 0;
409 }
410 
411 /** @} */
OSAL module header.
uint32_t eventflags_t
Type of an event flags mask.
Definition: osal.h:185
msg_t osalThreadSuspendS(thread_reference_t *trp)
Sends the current thread sleeping and sets a reference variable.
Definition: osal.c:185
void osalThreadDequeueAllI(threads_queue_t *tqp, msg_t msg)
Dequeues and wakes up all threads from the queue.
Definition: osal.c:309
void osalThreadSleep(sysinterval_t time)
Suspends the invoking thread for the specified time.
Definition: osal.c:170
void osalSysHalt(const char *reason)
System halt with error message.
Definition: osal.c:77
Events source object.
Definition: osal.h:212
void osalOsTimerHandlerI(void)
System timer handler.
Definition: osal.c:105
static void osalSysDisable(void)
Disables interrupts globally.
Definition: osal.h:511
#define osalDbgCheckClassI()
I-Class state check.
Definition: osal.h:292
void osalMutexLock(mutex_t *mp)
Locks the specified mutex.
Definition: osal.c:384
static void osalSysUnlock(void)
Leaves a critical zone from thread context.
Definition: osal.h:540
void osalOsRescheduleS(void)
Checks if a reschedule is required and performs it.
Definition: osal.c:119
uint32_t systime_t
Type of system time counter.
Definition: osal.h:165
void osalSysPolledDelayX(rtcnt_t cycles)
Polled delay.
Definition: osal.c:94
int32_t msg_t
Type of a message.
Definition: osal.h:160
void osalEventBroadcastFlagsI(event_source_t *esp, eventflags_t flags)
Add flags to an event source object.
Definition: osal.c:323
Type of a thread queue.
Definition: osal.h:232
void osalEventBroadcastFlags(event_source_t *esp, eventflags_t flags)
Add flags to an event source object.
Definition: osal.c:341
void osalThreadSleepS(sysinterval_t time)
Suspends the invoking thread for the specified time.
Definition: osal.c:153
void(* eventcallback_t)(event_source_t *esp)
Type of an event source callback.
Definition: osal.h:202
uint32_t rtcnt_t
Type of realtime counter.
Definition: osal.h:175
#define osalDbgCheck(c)
Function parameters check.
Definition: osal.h:278
void osalEventSetCallback(event_source_t *esp, eventcallback_t cb, void *param)
Event callback setup.
Definition: osal.c:365
volatile eventflags_t flags
Stored event flags.
Definition: osal.h:213
uint32_t sysinterval_t
Type of system time interval.
Definition: osal.h:170
systime_t osalOsGetSystemTimeX(void)
Current system time.
Definition: osal.c:136
void osalThreadDequeueNextI(threads_queue_t *tqp, msg_t msg)
Dequeues and wakes up one thread from the queue, if any.
Definition: osal.c:294
void osalInit(void)
OSAL module initialization.
Definition: osal.c:63
msg_t osalThreadEnqueueTimeoutS(threads_queue_t *tqp, sysinterval_t timeout)
Enqueues the caller thread.
Definition: osal.c:277
void * param
User defined field.
Definition: osal.h:215
msg_t osalThreadSuspendTimeoutS(thread_reference_t *trp, sysinterval_t timeout)
Sends the current thread sleeping and sets a reference variable.
Definition: osal.c:211
void * thread_reference_t
Type of a thread reference.
Definition: osal.h:180
void osalThreadResumeS(thread_reference_t *trp, msg_t msg)
Wakes up a thread waiting on a thread reference object.
Definition: osal.c:247
static void osalSysLock(void)
Enters a critical zone from thread context.
Definition: osal.h:530
const char * osal_halt_msg
Pointer to a halt error message.
Definition: osal.c:40
eventcallback_t cb
Event source callback.
Definition: osal.h:214
void osalMutexUnlock(mutex_t *mp)
Unlocks the specified mutex.
Definition: osal.c:404
void osalThreadResumeI(thread_reference_t *trp, msg_t msg)
Wakes up a thread waiting on a thread reference object.
Definition: osal.c:230
uint32_t mutex_t
Type of a mutex.
Definition: osal.h:223