diff options
author | Akshay <[email protected]> | 2022-04-10 12:13:40 +0100 |
---|---|---|
committer | Akshay <[email protected]> | 2022-04-10 12:13:40 +0100 |
commit | dc90387ce7d8ba7b607d9c48540bf6d8b560f14d (patch) | |
tree | 4ccb8fa5886b66fa9d480edef74236c27f035e16 /lib/chibios/os/common/abstractions |
Diffstat (limited to 'lib/chibios/os/common/abstractions')
22 files changed, 5610 insertions, 0 deletions
diff --git a/lib/chibios/os/common/abstractions/cmsis_os/cmsis_os.c b/lib/chibios/os/common/abstractions/cmsis_os/cmsis_os.c new file mode 100644 index 000000000..9bd63da8f --- /dev/null +++ b/lib/chibios/os/common/abstractions/cmsis_os/cmsis_os.c | |||
@@ -0,0 +1,558 @@ | |||
1 | /* | ||
2 | ChibiOS - Copyright (C) 2006,2007,2008,2009,2010,2011,2012,2013,2014, | ||
3 | 2015,2016,2017,2018,2019,2020,2021 Giovanni Di Sirio. | ||
4 | |||
5 | This file is part of ChibiOS. | ||
6 | |||
7 | ChibiOS is free software; you can redistribute it and/or modify | ||
8 | it under the terms of the GNU General Public License as published by | ||
9 | the Free Software Foundation version 3 of the License. | ||
10 | |||
11 | ChibiOS is distributed in the hope that it will be useful, | ||
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
14 | GNU General Public License for more details. | ||
15 | |||
16 | You should have received a copy of the GNU General Public License | ||
17 | along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
18 | */ | ||
19 | /* | ||
20 | Concepts and parts of this file have been contributed by Andre R. | ||
21 | */ | ||
22 | |||
23 | /** | ||
24 | * @file cmsis_os.c | ||
25 | * @brief CMSIS RTOS module code. | ||
26 | * | ||
27 | * @addtogroup CMSIS_OS | ||
28 | * @{ | ||
29 | */ | ||
30 | |||
31 | #include "cmsis_os.h" | ||
32 | #include <string.h> | ||
33 | |||
34 | /*===========================================================================*/ | ||
35 | /* Module local definitions. */ | ||
36 | /*===========================================================================*/ | ||
37 | |||
38 | /*===========================================================================*/ | ||
39 | /* Module exported variables. */ | ||
40 | /*===========================================================================*/ | ||
41 | |||
42 | int32_t cmsis_os_started; | ||
43 | |||
44 | /*===========================================================================*/ | ||
45 | /* Module local types. */ | ||
46 | /*===========================================================================*/ | ||
47 | |||
48 | /*===========================================================================*/ | ||
49 | /* Module local variables. */ | ||
50 | /*===========================================================================*/ | ||
51 | |||
52 | static memory_pool_t sempool; | ||
53 | static semaphore_t semaphores[CMSIS_CFG_NUM_SEMAPHORES]; | ||
54 | |||
55 | static memory_pool_t timpool; | ||
56 | static struct os_timer_cb timers[CMSIS_CFG_NUM_TIMERS]; | ||
57 | |||
58 | /*===========================================================================*/ | ||
59 | /* Module local functions. */ | ||
60 | /*===========================================================================*/ | ||
61 | |||
62 | /** | ||
63 | * @brief Virtual timers common callback. | ||
64 | */ | ||
65 | static void timer_cb(void const *arg) { | ||
66 | |||
67 | osTimerId timer_id = (osTimerId)arg; | ||
68 | timer_id->ptimer(timer_id->argument); | ||
69 | if (timer_id->type == osTimerPeriodic) { | ||
70 | chSysLockFromISR(); | ||
71 | chVTDoSetI(&timer_id->vt, TIME_MS2I(timer_id->millisec), | ||
72 | (vtfunc_t)timer_cb, timer_id); | ||
73 | chSysUnlockFromISR(); | ||
74 | } | ||
75 | } | ||
76 | |||
77 | /*===========================================================================*/ | ||
78 | /* Module exported functions. */ | ||
79 | /*===========================================================================*/ | ||
80 | |||
81 | /** | ||
82 | * @brief Kernel initialization. | ||
83 | */ | ||
84 | osStatus osKernelInitialize(void) { | ||
85 | |||
86 | cmsis_os_started = 0; | ||
87 | |||
88 | chSysInit(); | ||
89 | chThdSetPriority(HIGHPRIO); | ||
90 | |||
91 | chPoolObjectInit(&sempool, sizeof(semaphore_t), chCoreAllocAlignedI); | ||
92 | chPoolLoadArray(&sempool, semaphores, CMSIS_CFG_NUM_SEMAPHORES); | ||
93 | |||
94 | chPoolObjectInit(&timpool, sizeof(struct os_timer_cb), chCoreAllocAlignedI); | ||
95 | chPoolLoadArray(&timpool, timers, CMSIS_CFG_NUM_TIMERS); | ||
96 | |||
97 | return osOK; | ||
98 | } | ||
99 | |||
100 | /** | ||
101 | * @brief Kernel start. | ||
102 | */ | ||
103 | osStatus osKernelStart(void) { | ||
104 | |||
105 | cmsis_os_started = 1; | ||
106 | |||
107 | chThdSetPriority(NORMALPRIO); | ||
108 | |||
109 | return osOK; | ||
110 | } | ||
111 | |||
112 | /** | ||
113 | * @brief Creates a thread. | ||
114 | */ | ||
115 | osThreadId osThreadCreate(const osThreadDef_t *thread_def, void *argument) { | ||
116 | size_t size; | ||
117 | |||
118 | size = thread_def->stacksize == 0 ? CMSIS_CFG_DEFAULT_STACK : | ||
119 | thread_def->stacksize; | ||
120 | return (osThreadId)chThdCreateFromHeap(0, | ||
121 | THD_WORKING_AREA_SIZE(size), | ||
122 | thread_def->name, | ||
123 | NORMALPRIO+thread_def->tpriority, | ||
124 | (tfunc_t)thread_def->pthread, | ||
125 | argument); | ||
126 | } | ||
127 | |||
128 | /** | ||
129 | * @brief Thread termination. | ||
130 | * @note The thread is not really terminated but asked to terminate which | ||
131 | * is not compliant. | ||
132 | */ | ||
133 | osStatus osThreadTerminate(osThreadId thread_id) { | ||
134 | |||
135 | if (thread_id == osThreadGetId()) { | ||
136 | /* Note, no memory will be recovered unless a cleaner thread is | ||
137 | implemented using the registry.*/ | ||
138 | chThdExit(0); | ||
139 | } | ||
140 | chThdTerminate(thread_id); | ||
141 | chThdWait((thread_t *)thread_id); | ||
142 | |||
143 | return osOK; | ||
144 | } | ||
145 | |||
146 | /** | ||
147 | * @brief Change thread priority. | ||
148 | * @note This can interfere with the priority inheritance mechanism. | ||
149 | */ | ||
150 | osStatus osThreadSetPriority(osThreadId thread_id, osPriority newprio) { | ||
151 | thread_t * tp = (thread_t *)thread_id; | ||
152 | |||
153 | chSysLock(); | ||
154 | |||
155 | /* Changing priority.*/ | ||
156 | #if CH_CFG_USE_MUTEXES | ||
157 | if ((tp->hdr.pqueue.prio == tp->realprio) || | ||
158 | ((tprio_t)newprio > tp->hdr.pqueue.prio)) | ||
159 | tp->hdr.pqueue.prio = (tprio_t)newprio; | ||
160 | tp->realprio = (tprio_t)newprio; | ||
161 | #else | ||
162 | tp->hdr.pqueue.prio = (tprio_t)newprio; | ||
163 | #endif | ||
164 | |||
165 | /* The following states need priority queues reordering.*/ | ||
166 | switch (tp->state) { | ||
167 | #if CH_CFG_USE_MUTEXES | \ | ||
168 | CH_CFG_USE_CONDVARS | \ | ||
169 | (CH_CFG_USE_SEMAPHORES && CH_CFG_USE_SEMAPHORES_PRIORITY) | \ | ||
170 | (CH_CFG_USE_MESSAGES && CH_CFG_USE_MESSAGES_PRIORITY) | ||
171 | #if CH_CFG_USE_MUTEXES | ||
172 | case CH_STATE_WTMTX: | ||
173 | #endif | ||
174 | #if CH_CFG_USE_CONDVARS | ||
175 | case CH_STATE_WTCOND: | ||
176 | #endif | ||
177 | #if CH_CFG_USE_SEMAPHORES && CH_CFG_USE_SEMAPHORES_PRIORITY | ||
178 | case CH_STATE_WTSEM: | ||
179 | #endif | ||
180 | #if CH_CFG_USE_MESSAGES && CH_CFG_USE_MESSAGES_PRIORITY | ||
181 | case CH_STATE_SNDMSGQ: | ||
182 | #endif | ||
183 | /* Re-enqueues tp with its new priority on the queue.*/ | ||
184 | ch_sch_prio_insert(ch_queue_dequeue(&tp->hdr.queue), | ||
185 | (ch_queue_t *)tp->u.wtobjp); | ||
186 | break; | ||
187 | #endif | ||
188 | case CH_STATE_READY: | ||
189 | #if CH_DBG_ENABLE_ASSERTS | ||
190 | /* Prevents an assertion in chSchReadyI().*/ | ||
191 | tp->state = CH_STATE_CURRENT; | ||
192 | #endif | ||
193 | /* Re-enqueues tp with its new priority on the ready list.*/ | ||
194 | chSchReadyI((thread_t *)ch_queue_dequeue(&tp->hdr.queue)); | ||
195 | break; | ||
196 | } | ||
197 | |||
198 | /* Rescheduling.*/ | ||
199 | chSchRescheduleS(); | ||
200 | |||
201 | chSysUnlock(); | ||
202 | |||
203 | return osOK; | ||
204 | } | ||
205 | |||
206 | /** | ||
207 | * @brief Create a timer. | ||
208 | */ | ||
209 | osTimerId osTimerCreate(const osTimerDef_t *timer_def, | ||
210 | os_timer_type type, | ||
211 | void *argument) { | ||
212 | |||
213 | osTimerId timer = chPoolAlloc(&timpool); | ||
214 | chVTObjectInit(&timer->vt); | ||
215 | timer->ptimer = timer_def->ptimer; | ||
216 | timer->type = type; | ||
217 | timer->argument = argument; | ||
218 | return timer; | ||
219 | } | ||
220 | |||
221 | /** | ||
222 | * @brief Start a timer. | ||
223 | */ | ||
224 | osStatus osTimerStart(osTimerId timer_id, uint32_t millisec) { | ||
225 | |||
226 | if ((millisec == 0) || (millisec == osWaitForever)) | ||
227 | return osErrorValue; | ||
228 | |||
229 | timer_id->millisec = millisec; | ||
230 | chVTSet(&timer_id->vt, TIME_MS2I(millisec), (vtfunc_t)timer_cb, timer_id); | ||
231 | |||
232 | return osOK; | ||
233 | } | ||
234 | |||
235 | /** | ||
236 | * @brief Stop a timer. | ||
237 | */ | ||
238 | osStatus osTimerStop(osTimerId timer_id) { | ||
239 | |||
240 | chVTReset(&timer_id->vt); | ||
241 | |||
242 | return osOK; | ||
243 | } | ||
244 | |||
245 | /** | ||
246 | * @brief Delete a timer. | ||
247 | */ | ||
248 | osStatus osTimerDelete(osTimerId timer_id) { | ||
249 | |||
250 | chVTReset(&timer_id->vt); | ||
251 | chPoolFree(&timpool, (void *)timer_id); | ||
252 | |||
253 | return osOK; | ||
254 | } | ||
255 | |||
256 | /** | ||
257 | * @brief Send signals. | ||
258 | */ | ||
259 | int32_t osSignalSet(osThreadId thread_id, int32_t signals) { | ||
260 | int32_t oldsignals; | ||
261 | |||
262 | syssts_t sts = chSysGetStatusAndLockX(); | ||
263 | oldsignals = (int32_t)thread_id->epending; | ||
264 | chEvtSignalI((thread_t *)thread_id, (eventmask_t)signals); | ||
265 | chSysRestoreStatusX(sts); | ||
266 | |||
267 | return oldsignals; | ||
268 | } | ||
269 | |||
270 | /** | ||
271 | * @brief Clear signals. | ||
272 | */ | ||
273 | int32_t osSignalClear(osThreadId thread_id, int32_t signals) { | ||
274 | eventmask_t m; | ||
275 | |||
276 | chSysLock(); | ||
277 | |||
278 | m = thread_id->epending & (eventmask_t)signals; | ||
279 | thread_id->epending &= ~(eventmask_t)signals; | ||
280 | |||
281 | chSysUnlock(); | ||
282 | |||
283 | return (int32_t)m; | ||
284 | } | ||
285 | |||
286 | /** | ||
287 | * @brief Wait for signals. | ||
288 | */ | ||
289 | osEvent osSignalWait(int32_t signals, uint32_t millisec) { | ||
290 | osEvent event; | ||
291 | sysinterval_t timeout = (millisec == osWaitForever ? | ||
292 | TIME_INFINITE : (millisec == 0 ? TIME_IMMEDIATE : | ||
293 | TIME_MS2I(millisec))); | ||
294 | |||
295 | if (signals == 0) | ||
296 | event.value.signals = (uint32_t)chEvtWaitAnyTimeout(ALL_EVENTS, timeout); | ||
297 | else | ||
298 | event.value.signals = (uint32_t)chEvtWaitAllTimeout((eventmask_t)signals, | ||
299 | timeout); | ||
300 | |||
301 | /* Type of event.*/ | ||
302 | if (event.value.signals == 0) | ||
303 | event.status = osEventTimeout; | ||
304 | else | ||
305 | event.status = osEventSignal; | ||
306 | |||
307 | return event; | ||
308 | } | ||
309 | |||
310 | /** | ||
311 | * @brief Create a semaphore. | ||
312 | * @note @p semaphore_def is not used. | ||
313 | * @note Can involve memory allocation. | ||
314 | */ | ||
315 | osSemaphoreId osSemaphoreCreate(const osSemaphoreDef_t *semaphore_def, | ||
316 | int32_t count) { | ||
317 | |||
318 | (void)semaphore_def; | ||
319 | |||
320 | semaphore_t *sem = chPoolAlloc(&sempool); | ||
321 | chSemObjectInit(sem, (cnt_t)count); | ||
322 | return sem; | ||
323 | } | ||
324 | |||
325 | /** | ||
326 | * @brief Wait on a semaphore. | ||
327 | */ | ||
328 | int32_t osSemaphoreWait(osSemaphoreId semaphore_id, uint32_t millisec) { | ||
329 | sysinterval_t timeout = (millisec == osWaitForever ? | ||
330 | TIME_INFINITE : (millisec == 0 ? TIME_IMMEDIATE : | ||
331 | TIME_MS2I(millisec))); | ||
332 | |||
333 | msg_t msg = chSemWaitTimeout((semaphore_t *)semaphore_id, timeout); | ||
334 | switch (msg) { | ||
335 | case MSG_OK: | ||
336 | return osOK; | ||
337 | case MSG_TIMEOUT: | ||
338 | return osErrorTimeoutResource; | ||
339 | } | ||
340 | return osErrorResource; | ||
341 | } | ||
342 | |||
343 | /** | ||
344 | * @brief Release a semaphore. | ||
345 | */ | ||
346 | osStatus osSemaphoreRelease(osSemaphoreId semaphore_id) { | ||
347 | |||
348 | syssts_t sts = chSysGetStatusAndLockX(); | ||
349 | chSemSignalI((semaphore_t *)semaphore_id); | ||
350 | chSysRestoreStatusX(sts); | ||
351 | |||
352 | return osOK; | ||
353 | } | ||
354 | |||
355 | /** | ||
356 | * @brief Deletes a semaphore. | ||
357 | * @note After deletion there could be references in the system to a | ||
358 | * non-existent semaphore. | ||
359 | */ | ||
360 | osStatus osSemaphoreDelete(osSemaphoreId semaphore_id) { | ||
361 | |||
362 | chSemReset((semaphore_t *)semaphore_id, 0); | ||
363 | chPoolFree(&sempool, (void *)semaphore_id); | ||
364 | |||
365 | return osOK; | ||
366 | } | ||
367 | |||
368 | /** | ||
369 | * @brief Create a mutex. | ||
370 | * @note @p mutex_def is not used. | ||
371 | * @note Can involve memory allocation. | ||
372 | */ | ||
373 | osMutexId osMutexCreate(const osMutexDef_t *mutex_def) { | ||
374 | |||
375 | (void)mutex_def; | ||
376 | |||
377 | binary_semaphore_t *mtx = chPoolAlloc(&sempool); | ||
378 | chBSemObjectInit(mtx, false); | ||
379 | return mtx; | ||
380 | } | ||
381 | |||
382 | /** | ||
383 | * @brief Wait on a mutex. | ||
384 | */ | ||
385 | osStatus osMutexWait(osMutexId mutex_id, uint32_t millisec) { | ||
386 | sysinterval_t timeout = (millisec == osWaitForever ? | ||
387 | TIME_INFINITE : (millisec == 0 ? TIME_IMMEDIATE : | ||
388 | TIME_MS2I(millisec))); | ||
389 | |||
390 | msg_t msg = chBSemWaitTimeout((binary_semaphore_t *)mutex_id, timeout); | ||
391 | switch (msg) { | ||
392 | case MSG_OK: | ||
393 | return osOK; | ||
394 | case MSG_TIMEOUT: | ||
395 | return osErrorTimeoutResource; | ||
396 | } | ||
397 | return osErrorResource; | ||
398 | } | ||
399 | |||
400 | /** | ||
401 | * @brief Release a mutex. | ||
402 | */ | ||
403 | osStatus osMutexRelease(osMutexId mutex_id) { | ||
404 | |||
405 | syssts_t sts = chSysGetStatusAndLockX(); | ||
406 | chBSemSignalI((binary_semaphore_t *)mutex_id); | ||
407 | chSysRestoreStatusX(sts); | ||
408 | |||
409 | return osOK; | ||
410 | } | ||
411 | |||
412 | /** | ||
413 | * @brief Deletes a mutex. | ||
414 | * @note After deletion there could be references in the system to a | ||
415 | * non-existent semaphore. | ||
416 | */ | ||
417 | osStatus osMutexDelete(osMutexId mutex_id) { | ||
418 | |||
419 | chSemReset((semaphore_t *)mutex_id, 0); | ||
420 | chPoolFree(&sempool, (void *)mutex_id); | ||
421 | |||
422 | return osOK; | ||
423 | } | ||
424 | |||
425 | /** | ||
426 | * @brief Create a memory pool. | ||
427 | * @note The pool is not really created because it is allocated statically, | ||
428 | * this function just re-initializes it. | ||
429 | */ | ||
430 | osPoolId osPoolCreate(const osPoolDef_t *pool_def) { | ||
431 | |||
432 | chPoolObjectInit(pool_def->pool, (size_t)pool_def->item_sz, NULL); | ||
433 | chPoolLoadArray(pool_def->pool, pool_def->items, (size_t)pool_def->pool_sz); | ||
434 | |||
435 | return (osPoolId)pool_def->pool; | ||
436 | } | ||
437 | |||
438 | /** | ||
439 | * @brief Allocate an object. | ||
440 | */ | ||
441 | void *osPoolAlloc(osPoolId pool_id) { | ||
442 | void *object; | ||
443 | |||
444 | syssts_t sts = chSysGetStatusAndLockX(); | ||
445 | object = chPoolAllocI((memory_pool_t *)pool_id); | ||
446 | chSysRestoreStatusX(sts); | ||
447 | |||
448 | return object; | ||
449 | } | ||
450 | |||
451 | /** | ||
452 | * @brief Allocate an object clearing it. | ||
453 | */ | ||
454 | void *osPoolCAlloc(osPoolId pool_id) { | ||
455 | void *object; | ||
456 | |||
457 | object = chPoolAllocI((memory_pool_t *)pool_id); | ||
458 | memset(object, 0, pool_id->object_size); | ||
459 | return object; | ||
460 | } | ||
461 | |||
462 | /** | ||
463 | * @brief Free an object. | ||
464 | */ | ||
465 | osStatus osPoolFree(osPoolId pool_id, void *block) { | ||
466 | |||
467 | syssts_t sts = chSysGetStatusAndLockX(); | ||
468 | chPoolFreeI((memory_pool_t *)pool_id, block); | ||
469 | chSysRestoreStatusX(sts); | ||
470 | |||
471 | return osOK; | ||
472 | } | ||
473 | |||
474 | /** | ||
475 | * @brief Create a message queue. | ||
476 | * @note The queue is not really created because it is allocated statically, | ||
477 | * this function just re-initializes it. | ||
478 | */ | ||
479 | osMessageQId osMessageCreate(const osMessageQDef_t *queue_def, | ||
480 | osThreadId thread_id) { | ||
481 | |||
482 | /* Ignoring this parameter for now.*/ | ||
483 | (void)thread_id; | ||
484 | |||
485 | if (queue_def->item_sz > sizeof (msg_t)) | ||
486 | return NULL; | ||
487 | |||
488 | chMBObjectInit(queue_def->mailbox, | ||
489 | queue_def->items, | ||
490 | (size_t)queue_def->queue_sz); | ||
491 | |||
492 | return (osMessageQId) queue_def->mailbox; | ||
493 | } | ||
494 | |||
495 | /** | ||
496 | * @brief Put a message in the queue. | ||
497 | */ | ||
498 | osStatus osMessagePut(osMessageQId queue_id, | ||
499 | uint32_t info, | ||
500 | uint32_t millisec) { | ||
501 | msg_t msg; | ||
502 | sysinterval_t timeout = (millisec == osWaitForever ? | ||
503 | TIME_INFINITE : (millisec == 0 ? TIME_IMMEDIATE : | ||
504 | TIME_MS2I(millisec))); | ||
505 | |||
506 | if (port_is_isr_context()) { | ||
507 | |||
508 | /* Waiting makes no sense in ISRs so any value except "immediate" | ||
509 | makes no sense.*/ | ||
510 | if (millisec != 0) | ||
511 | return osErrorValue; | ||
512 | |||
513 | chSysLockFromISR(); | ||
514 | msg = chMBPostI((mailbox_t *)queue_id, (msg_t)info); | ||
515 | chSysUnlockFromISR(); | ||
516 | } | ||
517 | else | ||
518 | msg = chMBPostTimeout((mailbox_t *)queue_id, (msg_t)info, timeout); | ||
519 | |||
520 | return msg == MSG_OK ? osOK : osEventTimeout; | ||
521 | } | ||
522 | |||
523 | /** | ||
524 | * @brief Get a message from the queue. | ||
525 | */ | ||
526 | osEvent osMessageGet(osMessageQId queue_id, | ||
527 | uint32_t millisec) { | ||
528 | msg_t msg; | ||
529 | osEvent event; | ||
530 | sysinterval_t timeout = (millisec == osWaitForever ? | ||
531 | TIME_INFINITE : (millisec == 0 ? TIME_IMMEDIATE : | ||
532 | TIME_MS2I(millisec))); | ||
533 | |||
534 | event.def.message_id = queue_id; | ||
535 | |||
536 | if (port_is_isr_context()) { | ||
537 | |||
538 | /* Waiting makes no sense in ISRs so any value except "immediate" | ||
539 | makes no sense.*/ | ||
540 | if (millisec != 0) { | ||
541 | event.status = osErrorValue; | ||
542 | return event; | ||
543 | } | ||
544 | |||
545 | chSysLockFromISR(); | ||
546 | msg = chMBFetchI((mailbox_t *)queue_id, (msg_t*)&event.value.v); | ||
547 | chSysUnlockFromISR(); | ||
548 | } | ||
549 | else { | ||
550 | msg = chMBFetchTimeout((mailbox_t *)queue_id, (msg_t*)&event.value.v, timeout); | ||
551 | } | ||
552 | |||
553 | /* Returned event type.*/ | ||
554 | event.status = msg == MSG_OK ? osEventMessage : osEventTimeout; | ||
555 | return event; | ||
556 | } | ||
557 | |||
558 | /** @} */ | ||
diff --git a/lib/chibios/os/common/abstractions/cmsis_os/cmsis_os.h b/lib/chibios/os/common/abstractions/cmsis_os/cmsis_os.h new file mode 100644 index 000000000..dafe3e398 --- /dev/null +++ b/lib/chibios/os/common/abstractions/cmsis_os/cmsis_os.h | |||
@@ -0,0 +1,522 @@ | |||
1 | /* | ||
2 | ChibiOS - Copyright (C) 2006,2007,2008,2009,2010,2011,2012,2013,2014, | ||
3 | 2015,2016,2017,2018,2019,2020,2021 Giovanni Di Sirio. | ||
4 | |||
5 | This file is part of ChibiOS. | ||
6 | |||
7 | ChibiOS is free software; you can redistribute it and/or modify | ||
8 | it under the terms of the GNU General Public License as published by | ||
9 | the Free Software Foundation version 3 of the License. | ||
10 | |||
11 | ChibiOS is distributed in the hope that it will be useful, | ||
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
14 | GNU General Public License for more details. | ||
15 | |||
16 | You should have received a copy of the GNU General Public License | ||
17 | along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
18 | */ | ||
19 | /* | ||
20 | Concepts and parts of this file have been contributed by Andre R. | ||
21 | */ | ||
22 | |||
23 | /** | ||
24 | * @file cmsis_os.h | ||
25 | * @brief CMSIS RTOS module macros and structures. | ||
26 | * | ||
27 | * @addtogroup CMSIS_OS | ||
28 | * @{ | ||
29 | */ | ||
30 | |||
31 | #ifndef CMSIS_OS_H | ||
32 | #define CMSIS_OS_H | ||
33 | |||
34 | #include "ch.h" | ||
35 | |||
36 | /*===========================================================================*/ | ||
37 | /* Module constants. */ | ||
38 | /*===========================================================================*/ | ||
39 | |||
40 | /** | ||
41 | * @brief API version. | ||
42 | */ | ||
43 | #define osCMSIS 0x10002 | ||
44 | |||
45 | /** | ||
46 | * @brief Kernel version. | ||
47 | */ | ||
48 | #define osKernelSystemId "KERNEL V1.00" | ||
49 | |||
50 | /** | ||
51 | * @brief ChibiOS/RT version encoded for CMSIS. | ||
52 | */ | ||
53 | #define osCMSIS_KERNEL ((CH_KERNEL_MAJOR << 16) | \ | ||
54 | (CH_KERNEL_MINOR << 8) | \ | ||
55 | (CH_KERNEL_PATCH)) | ||
56 | |||
57 | /** | ||
58 | * @name CMSIS Capabilities | ||
59 | * @{ | ||
60 | */ | ||
61 | #define osFeature_MainThread 1 | ||
62 | #define osFeature_Pool 1 | ||
63 | #define osFeature_MailQ 0 | ||
64 | #define osFeature_MessageQ 1 | ||
65 | #define osFeature_Signals 24 | ||
66 | #define osFeature_Semaphore ((1U << 31) - 1U) | ||
67 | #define osFeature_Wait 0 | ||
68 | #define osFeature_SysTick 1 | ||
69 | /**< @} */ | ||
70 | |||
71 | /** | ||
72 | * @brief Wait forever specification for timeouts. | ||
73 | */ | ||
74 | #define osWaitForever ((uint32_t)-1) | ||
75 | |||
76 | /** | ||
77 | * @brief System tick frequency. | ||
78 | */ | ||
79 | #define osKernelSysTickFrequency CH_CFG_ST_FREQUENCY | ||
80 | |||
81 | /*===========================================================================*/ | ||
82 | /* Module pre-compile time settings. */ | ||
83 | /*===========================================================================*/ | ||
84 | |||
85 | /** | ||
86 | * @brief Number of pre-allocated static semaphores/mutexes. | ||
87 | */ | ||
88 | #if !defined(CMSIS_CFG_DEFAULT_STACK) | ||
89 | #define CMSIS_CFG_DEFAULT_STACK 256 | ||
90 | #endif | ||
91 | |||
92 | /** | ||
93 | * @brief Number of pre-allocated static semaphores/mutexes. | ||
94 | */ | ||
95 | #if !defined(CMSIS_CFG_NUM_SEMAPHORES) | ||
96 | #define CMSIS_CFG_NUM_SEMAPHORES 4 | ||
97 | #endif | ||
98 | |||
99 | /** | ||
100 | * @brief Number of pre-allocated static timers. | ||
101 | */ | ||
102 | #if !defined(CMSIS_CFG_NUM_TIMERS) | ||
103 | #define CMSIS_CFG_NUM_TIMERS 4 | ||
104 | #endif | ||
105 | |||
106 | /*===========================================================================*/ | ||
107 | /* Derived constants and error checks. */ | ||
108 | /*===========================================================================*/ | ||
109 | |||
110 | #if !CH_CFG_USE_MEMPOOLS | ||
111 | #error "CMSIS RTOS requires CH_CFG_USE_MEMPOOLS" | ||
112 | #endif | ||
113 | |||
114 | #if !CH_CFG_USE_EVENTS | ||
115 | #error "CMSIS RTOS requires CH_CFG_USE_EVENTS" | ||
116 | #endif | ||
117 | |||
118 | #if !CH_CFG_USE_EVENTS_TIMEOUT | ||
119 | #error "CMSIS RTOS requires CH_CFG_USE_EVENTS_TIMEOUT" | ||
120 | #endif | ||
121 | |||
122 | #if !CH_CFG_USE_SEMAPHORES | ||
123 | #error "CMSIS RTOS requires CH_CFG_USE_SEMAPHORES" | ||
124 | #endif | ||
125 | |||
126 | #if !CH_CFG_USE_DYNAMIC | ||
127 | #error "CMSIS RTOS requires CH_CFG_USE_DYNAMIC" | ||
128 | #endif | ||
129 | |||
130 | /*===========================================================================*/ | ||
131 | /* Module data structures and types. */ | ||
132 | /*===========================================================================*/ | ||
133 | |||
134 | /** | ||
135 | * @brief Type of priority levels. | ||
136 | */ | ||
137 | typedef enum { | ||
138 | osPriorityIdle = -3, | ||
139 | osPriorityLow = -2, | ||
140 | osPriorityBelowNormal = -1, | ||
141 | osPriorityNormal = 0, | ||
142 | osPriorityAboveNormal = +1, | ||
143 | osPriorityHigh = +2, | ||
144 | osPriorityRealtime = +3, | ||
145 | osPriorityError = 0x84 | ||
146 | } osPriority; | ||
147 | |||
148 | /** | ||
149 | * @brief Type of error codes. | ||
150 | */ | ||
151 | typedef enum { | ||
152 | osOK = 0, | ||
153 | osEventSignal = 0x08, | ||
154 | osEventMessage = 0x10, | ||
155 | osEventMail = 0x20, | ||
156 | osEventTimeout = 0x40, | ||
157 | osErrorParameter = 0x80, | ||
158 | osErrorResource = 0x81, | ||
159 | osErrorTimeoutResource = 0xC1, | ||
160 | osErrorISR = 0x82, | ||
161 | osErrorISRRecursive = 0x83, | ||
162 | osErrorPriority = 0x84, | ||
163 | osErrorNoMemory = 0x85, | ||
164 | osErrorValue = 0x86, | ||
165 | osErrorOS = 0xFF, | ||
166 | os_status_reserved = 0x7FFFFFFF | ||
167 | } osStatus; | ||
168 | |||
169 | /** | ||
170 | * @brief Type of a timer mode. | ||
171 | */ | ||
172 | typedef enum { | ||
173 | osTimerOnce = 0, | ||
174 | osTimerPeriodic = 1 | ||
175 | } os_timer_type; | ||
176 | |||
177 | /** | ||
178 | * @brief Type of thread functions. | ||
179 | */ | ||
180 | typedef void (*os_pthread) (void const *argument); | ||
181 | |||
182 | /** | ||
183 | * @brief Type of timer callback. | ||
184 | */ | ||
185 | typedef void (*os_ptimer) (void const *argument); | ||
186 | |||
187 | /** | ||
188 | * @brief Type of pointer to thread control block. | ||
189 | */ | ||
190 | typedef thread_t *osThreadId; | ||
191 | |||
192 | /** | ||
193 | * @brief Type of pointer to timer control block. | ||
194 | */ | ||
195 | typedef struct os_timer_cb { | ||
196 | virtual_timer_t vt; | ||
197 | os_timer_type type; | ||
198 | os_ptimer ptimer; | ||
199 | void *argument; | ||
200 | uint32_t millisec; | ||
201 | } *osTimerId; | ||
202 | |||
203 | /** | ||
204 | * @brief Type of pointer to mutex control block. | ||
205 | */ | ||
206 | typedef binary_semaphore_t *osMutexId; | ||
207 | |||
208 | /** | ||
209 | * @brief Type of pointer to semaphore control block. | ||
210 | */ | ||
211 | typedef semaphore_t *osSemaphoreId; | ||
212 | |||
213 | /** | ||
214 | * @brief Type of pointer to memory pool control block. | ||
215 | */ | ||
216 | typedef memory_pool_t *osPoolId; | ||
217 | |||
218 | /** | ||
219 | * @brief Type of pointer to message queue control block. | ||
220 | */ | ||
221 | typedef struct mailbox *osMessageQId; | ||
222 | |||
223 | /** | ||
224 | * @brief Type of an event. | ||
225 | */ | ||
226 | typedef struct { | ||
227 | osStatus status; | ||
228 | union { | ||
229 | uint32_t v; | ||
230 | void *p; | ||
231 | int32_t signals; | ||
232 | } value; | ||
233 | union { | ||
234 | /* osMailQId mail_id;*/ | ||
235 | osMessageQId message_id; | ||
236 | } def; | ||
237 | } osEvent; | ||
238 | |||
239 | /** | ||
240 | * @brief Type of a thread definition block. | ||
241 | */ | ||
242 | typedef struct os_thread_def { | ||
243 | os_pthread pthread; | ||
244 | osPriority tpriority; | ||
245 | uint32_t stacksize; | ||
246 | const char *name; | ||
247 | } osThreadDef_t; | ||
248 | |||
249 | /** | ||
250 | * @brief Type of a timer definition block. | ||
251 | */ | ||
252 | typedef struct os_timer_def { | ||
253 | os_ptimer ptimer; | ||
254 | } osTimerDef_t; | ||
255 | |||
256 | /** | ||
257 | * @brief Type of a mutex definition block. | ||
258 | */ | ||
259 | typedef struct os_mutex_def { | ||
260 | uint32_t dummy; | ||
261 | } osMutexDef_t; | ||
262 | |||
263 | /** | ||
264 | * @brief Type of a semaphore definition block. | ||
265 | */ | ||
266 | typedef struct os_semaphore_def { | ||
267 | uint32_t dummy; | ||
268 | } osSemaphoreDef_t; | ||
269 | |||
270 | /** | ||
271 | * @brief Type of a memory pool definition block. | ||
272 | */ | ||
273 | typedef struct os_pool_def { | ||
274 | uint32_t pool_sz; | ||
275 | uint32_t item_sz; | ||
276 | memory_pool_t *pool; | ||
277 | void *items; | ||
278 | } osPoolDef_t; | ||
279 | |||
280 | /** | ||
281 | * @brief Type of a message queue definition block. | ||
282 | */ | ||
283 | typedef struct os_messageQ_def { | ||
284 | uint32_t queue_sz; | ||
285 | uint32_t item_sz; | ||
286 | mailbox_t *mailbox; | ||
287 | void *items; | ||
288 | } osMessageQDef_t; | ||
289 | |||
290 | /*===========================================================================*/ | ||
291 | /* Module macros. */ | ||
292 | /*===========================================================================*/ | ||
293 | |||
294 | /** | ||
295 | * @brief Convert a microseconds value to a RTOS kernel system timer value. | ||
296 | */ | ||
297 | #define osKernelSysTickMicroSec(microsec) (((uint64_t)microsec * \ | ||
298 | (osKernelSysTickFrequency)) / \ | ||
299 | 1000000) | ||
300 | |||
301 | /** | ||
302 | * @brief Create a Thread definition. | ||
303 | */ | ||
304 | #if defined(osObjectsExternal) | ||
305 | #define osThreadDef(thd, priority, stacksz, name) \ | ||
306 | extern const osThreadDef_t os_thread_def_##thd | ||
307 | #else | ||
308 | #define osThreadDef(thd, priority, stacksz, name) \ | ||
309 | const osThreadDef_t os_thread_def_##thd = { \ | ||
310 | (thd), \ | ||
311 | (priority), \ | ||
312 | (stacksz), \ | ||
313 | (name) \ | ||
314 | } | ||
315 | #endif | ||
316 | |||
317 | /** | ||
318 | * @brief Access a Thread definition. | ||
319 | */ | ||
320 | #define osThread(name) &os_thread_def_##name | ||
321 | |||
322 | /** | ||
323 | * @brief Define a Timer object. | ||
324 | */ | ||
325 | #if defined(osObjectsExternal) | ||
326 | #define osTimerDef(name, function) \ | ||
327 | extern const osTimerDef_t os_timer_def_##name | ||
328 | #else | ||
329 | #define osTimerDef(name, function) \ | ||
330 | const osTimerDef_t os_timer_def_##name = { \ | ||
331 | (function) \ | ||
332 | } | ||
333 | #endif | ||
334 | |||
335 | /** | ||
336 | * @brief Access a Timer definition. | ||
337 | */ | ||
338 | #define osTimer(name) &os_timer_def_##name | ||
339 | |||
340 | /** | ||
341 | * @brief Define a Mutex. | ||
342 | */ | ||
343 | #if defined(osObjectsExternal) | ||
344 | #define osMutexDef(name) extern const osMutexDef_t os_mutex_def_##name | ||
345 | #else | ||
346 | #define osMutexDef(name) const osMutexDef_t os_mutex_def_##name = {0} | ||
347 | #endif | ||
348 | |||
349 | /** | ||
350 | * @brief Access a Mutex definition. | ||
351 | */ | ||
352 | #define osMutex(name) &os_mutex_def_##name | ||
353 | |||
354 | /** | ||
355 | * @brief Define a Semaphore. | ||
356 | */ | ||
357 | #if defined(osObjectsExternal) | ||
358 | #define osSemaphoreDef(name) \ | ||
359 | extern const osSemaphoreDef_t os_semaphore_def_##name | ||
360 | #else // define the object | ||
361 | #define osSemaphoreDef(name) \ | ||
362 | const osSemaphoreDef_t os_semaphore_def_##name = {0} | ||
363 | #endif | ||
364 | |||
365 | /** | ||
366 | * @brief Access a Semaphore definition. | ||
367 | */ | ||
368 | #define osSemaphore(name) &os_semaphore_def_##name | ||
369 | |||
370 | /** | ||
371 | * @brief Define a Memory Pool. | ||
372 | */ | ||
373 | #if defined(osObjectsExternal) | ||
374 | #define osPoolDef(name, no, type) \ | ||
375 | extern const osPoolDef_t os_pool_def_##name | ||
376 | #else | ||
377 | #define osPoolDef(name, no, type) \ | ||
378 | static const type os_pool_buf_##name[no]; \ | ||
379 | static memory_pool_t os_pool_obj_##name; \ | ||
380 | const osPoolDef_t os_pool_def_##name = { \ | ||
381 | (no), \ | ||
382 | sizeof (type), \ | ||
383 | (void *)&os_pool_obj_##name, \ | ||
384 | (void *)&os_pool_buf_##name[0] \ | ||
385 | } | ||
386 | #endif | ||
387 | |||
388 | /** | ||
389 | * @brief Access a Memory Pool definition. | ||
390 | */ | ||
391 | #define osPool(name) &os_pool_def_##name | ||
392 | |||
393 | /** | ||
394 | * @brief Define a Message Queue. | ||
395 | */ | ||
396 | #if defined(osObjectsExternal) | ||
397 | #define osMessageQDef(name, queue_sz, type) \ | ||
398 | extern const osMessageQDef_t os_messageQ_def_##name | ||
399 | #else | ||
400 | #define osMessageQDef(name, queue_sz, type) \ | ||
401 | static const msg_t os_messageQ_buf_##name[queue_sz]; \ | ||
402 | static mailbox_t os_messageQ_obj_##name; \ | ||
403 | const osMessageQDef_t os_messageQ_def_##name = { \ | ||
404 | (queue_sz), \ | ||
405 | sizeof (type), \ | ||
406 | (void *)&os_messageQ_obj_##name, \ | ||
407 | (void *)&os_messageQ_buf_##name[0] \ | ||
408 | } | ||
409 | #endif | ||
410 | |||
411 | /** | ||
412 | * @brief Access a Message Queue definition. | ||
413 | */ | ||
414 | #define osMessageQ(name) &os_messageQ_def_##name | ||
415 | |||
416 | /*===========================================================================*/ | ||
417 | /* External declarations. */ | ||
418 | /*===========================================================================*/ | ||
419 | |||
420 | extern int32_t cmsis_os_started; | ||
421 | |||
422 | #ifdef __cplusplus | ||
423 | extern "C" { | ||
424 | #endif | ||
425 | osStatus osKernelInitialize(void); | ||
426 | osStatus osKernelStart(void); | ||
427 | osThreadId osThreadCreate(const osThreadDef_t *thread_def, void *argument); | ||
428 | osStatus osThreadTerminate(osThreadId thread_id); | ||
429 | osStatus osThreadSetPriority(osThreadId thread_id, osPriority newprio); | ||
430 | /*osEvent osWait(uint32_t millisec);*/ | ||
431 | osTimerId osTimerCreate(const osTimerDef_t *timer_def, | ||
432 | os_timer_type type, | ||
433 | void *argument); | ||
434 | osStatus osTimerStart(osTimerId timer_id, uint32_t millisec); | ||
435 | osStatus osTimerStop(osTimerId timer_id); | ||
436 | osStatus osTimerDelete(osTimerId timer_id); | ||
437 | int32_t osSignalSet(osThreadId thread_id, int32_t signals); | ||
438 | int32_t osSignalClear(osThreadId thread_id, int32_t signals); | ||
439 | osEvent osSignalWait(int32_t signals, uint32_t millisec); | ||
440 | osSemaphoreId osSemaphoreCreate(const osSemaphoreDef_t *semaphore_def, | ||
441 | int32_t count); | ||
442 | int32_t osSemaphoreWait(osSemaphoreId semaphore_id, uint32_t millisec); | ||
443 | osStatus osSemaphoreRelease(osSemaphoreId semaphore_id); | ||
444 | osStatus osSemaphoreDelete(osSemaphoreId semaphore_id); | ||
445 | osMutexId osMutexCreate(const osMutexDef_t *mutex_def); | ||
446 | osStatus osMutexWait(osMutexId mutex_id, uint32_t millisec); | ||
447 | osStatus osMutexRelease(osMutexId mutex_id); | ||
448 | osStatus osMutexDelete(osMutexId mutex_id); | ||
449 | osPoolId osPoolCreate(const osPoolDef_t *pool_def); | ||
450 | void *osPoolAlloc(osPoolId pool_id); | ||
451 | void *osPoolCAlloc(osPoolId pool_id); | ||
452 | osStatus osPoolFree(osPoolId pool_id, void *block); | ||
453 | osMessageQId osMessageCreate(const osMessageQDef_t *queue_def, | ||
454 | osThreadId thread_id); | ||
455 | osStatus osMessagePut(osMessageQId queue_id, | ||
456 | uint32_t info, | ||
457 | uint32_t millisec); | ||
458 | osEvent osMessageGet(osMessageQId queue_id, | ||
459 | uint32_t millisec); | ||
460 | #ifdef __cplusplus | ||
461 | } | ||
462 | #endif | ||
463 | |||
464 | /*===========================================================================*/ | ||
465 | /* Module inline functions. */ | ||
466 | /*===========================================================================*/ | ||
467 | |||
468 | /** | ||
469 | * @brief To be or not to be. | ||
470 | */ | ||
471 | static inline int32_t osKernelRunning(void) { | ||
472 | |||
473 | return cmsis_os_started; | ||
474 | } | ||
475 | |||
476 | /** | ||
477 | * @brief System ticks since start. | ||
478 | */ | ||
479 | static inline uint32_t osKernelSysTick(void) { | ||
480 | |||
481 | return (uint32_t)chVTGetSystemTimeX(); | ||
482 | } | ||
483 | |||
484 | /** | ||
485 | * @brief Returns the current thread. | ||
486 | */ | ||
487 | static inline osThreadId osThreadGetId(void) { | ||
488 | |||
489 | return (osThreadId)chThdGetSelfX(); | ||
490 | } | ||
491 | |||
492 | /** | ||
493 | * @brief Thread time slice yield. | ||
494 | */ | ||
495 | static inline osStatus osThreadYield(void) { | ||
496 | |||
497 | chThdYield(); | ||
498 | |||
499 | return osOK; | ||
500 | } | ||
501 | |||
502 | /** | ||
503 | * @brief Returns priority of a thread. | ||
504 | */ | ||
505 | static inline osPriority osThreadGetPriority(osThreadId thread_id) { | ||
506 | |||
507 | return (osPriority)(NORMALPRIO - thread_id->hdr.pqueue.prio); | ||
508 | } | ||
509 | |||
510 | /** | ||
511 | * @brief Thread delay in milliseconds. | ||
512 | */ | ||
513 | static inline osStatus osDelay(uint32_t millisec) { | ||
514 | |||
515 | chThdSleepMilliseconds(millisec); | ||
516 | |||
517 | return osOK; | ||
518 | } | ||
519 | |||
520 | #endif /* CMSIS_OS_H */ | ||
521 | |||
522 | /** @} */ | ||
diff --git a/lib/chibios/os/common/abstractions/cmsis_os/cmsis_os.mk b/lib/chibios/os/common/abstractions/cmsis_os/cmsis_os.mk new file mode 100644 index 000000000..343f6df40 --- /dev/null +++ b/lib/chibios/os/common/abstractions/cmsis_os/cmsis_os.mk | |||
@@ -0,0 +1,8 @@ | |||
1 | # List of the ChibiOS/RT CMSIS RTOS wrapper. | ||
2 | CMSISRTOSSRC = ${CHIBIOS}/os/common/abstractions/cmsis_os/cmsis_os.c | ||
3 | |||
4 | CMSISRTOSINC = ${CHIBIOS}/os/common/abstractions/cmsis_os | ||
5 | |||
6 | # Shared variables | ||
7 | ALLCSRC += $(CMSISRTOSSRC) | ||
8 | ALLINC += $(CMSISRTOSINC) | ||
diff --git a/lib/chibios/os/common/abstractions/nasa_cfe/osal/cfe_osal.mk b/lib/chibios/os/common/abstractions/nasa_cfe/osal/cfe_osal.mk new file mode 100644 index 000000000..c0add1c26 --- /dev/null +++ b/lib/chibios/os/common/abstractions/nasa_cfe/osal/cfe_osal.mk | |||
@@ -0,0 +1,8 @@ | |||
1 | # NASA CFE OSAL files. | ||
2 | CFEOSALSRC = $(CHIBIOS)/os/common/abstractions/nasa_cfe/osal/src/osapi.c | ||
3 | |||
4 | CFEOSALINC = $(CHIBIOS)/os/common/abstractions/nasa_cfe/osal/include | ||
5 | |||
6 | # Shared variables | ||
7 | ALLCSRC += $(CFEOSALSRC) | ||
8 | ALLINC += $(CFEOSALINC) | ||
diff --git a/lib/chibios/os/common/abstractions/nasa_cfe/osal/include/common_types.h b/lib/chibios/os/common/abstractions/nasa_cfe/osal/include/common_types.h new file mode 100644 index 000000000..100a9c72e --- /dev/null +++ b/lib/chibios/os/common/abstractions/nasa_cfe/osal/include/common_types.h | |||
@@ -0,0 +1,267 @@ | |||
1 | /*--------------------------------------------------------------------------- | ||
2 | ** | ||
3 | ** Filename: | ||
4 | ** $Id: common_types.h 1.9 2014/01/14 16:28:32GMT-05:00 acudmore Exp $ | ||
5 | ** | ||
6 | ** Copyright (c) 2004-2006, United States government as represented by the | ||
7 | ** administrator of the National Aeronautics Space Administration. | ||
8 | ** All rights reserved. This software was created at NASAs Goddard | ||
9 | ** Space Flight Center pursuant to government contracts. | ||
10 | ** | ||
11 | ** This is governed by the NASA Open Source Agreement and may be used, | ||
12 | ** distributed and modified only pursuant to the terms of that agreement. | ||
13 | ** | ||
14 | ** Purpose: | ||
15 | ** Unit specification for common types. | ||
16 | ** | ||
17 | ** Design Notes: | ||
18 | ** Assumes make file has defined processor family | ||
19 | ** | ||
20 | ** References: | ||
21 | ** Flight Software Branch C Coding Standard Version 1.0a | ||
22 | ** | ||
23 | ** | ||
24 | ** Notes: | ||
25 | ** | ||
26 | ** | ||
27 | ** $Date: 2014/01/14 16:28:32GMT-05:00 $ | ||
28 | ** $Revision: 1.9 $ | ||
29 | ** $Log: common_types.h $ | ||
30 | ** Revision 1.9 2014/01/14 16:28:32GMT-05:00 acudmore | ||
31 | ** Fixed typo in macro for x86-64 | ||
32 | ** Revision 1.8 2013/08/09 13:58:04GMT-05:00 acudmore | ||
33 | ** Added int64 type, added support for ARM arch, added 64 bit x86 arch, added arch check for GCC arch macros, added check for proper data type sizes | ||
34 | ** Revision 1.7 2013/07/25 10:01:29GMT-05:00 acudmore | ||
35 | ** Added C++ support | ||
36 | ** Revision 1.6 2012/04/11 09:19:03GMT-05:00 acudmore | ||
37 | ** added OS_USED attribute | ||
38 | ** Revision 1.5 2010/02/18 16:43:29EST acudmore | ||
39 | ** Added SPARC processor section | ||
40 | ** Removed special characters from comments that cause problems with some tools. | ||
41 | ** Revision 1.4 2010/02/18 16:41:39EST acudmore | ||
42 | ** Added a block of defines for GCC specific pragmas and extensions. | ||
43 | ** Removed RTEMS boolean related ifdefs | ||
44 | ** moved OS_PACK into the GCC specific block | ||
45 | ** Revision 1.3 2010/02/01 12:31:17EST acudmore | ||
46 | ** Added uint64 type | ||
47 | ** Revision 1.2 2009/07/07 16:30:05EDT acudmore | ||
48 | ** Removed conditinal comp. around boolean for m68k. | ||
49 | ** This will need to be done for all RTEMS targets | ||
50 | ** Revision 1.1 2009/06/02 10:04:58EDT acudmore | ||
51 | ** Initial revision | ||
52 | ** Member added to project c:/MKSDATA/MKS-REPOSITORY/MKS-OSAL-REPOSITORY/src/os/inc/project.pj | ||
53 | ** Revision 1.1 2008/04/20 22:35:58EDT ruperera | ||
54 | ** Initial revision | ||
55 | ** Member added to project c:/MKSDATA/MKS-REPOSITORY/MKS-OSAL-REPOSITORY/src/inc/project.pj | ||
56 | ** Revision 1.1 2007/10/16 16:14:49EDT apcudmore | ||
57 | ** Initial revision | ||
58 | ** Member added to project d:/mksdata/MKS-OSAL-REPOSITORY/src/inc/project.pj | ||
59 | ** Revision 1.2 2006/06/08 14:28:32EDT David Kobe (dlkobe) | ||
60 | ** Added NASA Open Source Legal Statement | ||
61 | ** Revision 1.1 2005/06/09 09:57:51GMT-05:00 rperera | ||
62 | ** Initial revision | ||
63 | ** Member added to project d:/mksdata/MKS-CFE-REPOSITORY/cfe-core/inc/project.pj | ||
64 | ** Revision 1.6 2005/03/24 19:20:52 rmcgraw | ||
65 | ** Wrapped the boolean defintion for all three processors with #ifndef _USING_RTEMS_INCLUDES_ | ||
66 | ** | ||
67 | ** Revision 1.5 2005/03/10 16:59:08 acudmore | ||
68 | ** removed boolean prefix to TRUE and FALSE defintion to avoid vxWorks conflict. | ||
69 | ** | ||
70 | ** Revision 1.4 2005/03/07 20:23:34 acudmore | ||
71 | ** removed duplicate boolean definition | ||
72 | ** | ||
73 | ** Revision 1.3 2005/03/07 20:05:17 acudmore | ||
74 | ** updated with __PPC__ macro that gnu compiler uses | ||
75 | ** | ||
76 | ** Revision 1.2 2005/03/04 16:02:44 acudmore | ||
77 | ** added coldfire architecture | ||
78 | ** | ||
79 | ** Revision 1.1 2005/03/04 15:58:45 acudmore | ||
80 | ** Added common_types.h | ||
81 | ** | ||
82 | ** | ||
83 | ** | ||
84 | **-------------------------------------------------------------------------*/ | ||
85 | |||
86 | #ifndef _common_types_ | ||
87 | #define _common_types_ | ||
88 | |||
89 | #ifdef __cplusplus | ||
90 | extern "C" { | ||
91 | #endif | ||
92 | |||
93 | /* | ||
94 | ** Includes | ||
95 | */ | ||
96 | |||
97 | /* | ||
98 | ** Macro Definitions | ||
99 | */ | ||
100 | |||
101 | /* | ||
102 | ** Condition = TRUE is ok, Condition = FALSE is error | ||
103 | */ | ||
104 | #define CompileTimeAssert(Condition, Message) typedef char Message[(Condition) ? 1 : -1] | ||
105 | |||
106 | |||
107 | /* | ||
108 | ** Define compiler specific macros | ||
109 | ** The __extension__ compiler pragma is required | ||
110 | ** for the uint64 type using GCC with the ANSI C90 standard. | ||
111 | ** Other macros can go in here as needed, for example alignment | ||
112 | ** pragmas. | ||
113 | */ | ||
114 | #if defined (__GNUC__) | ||
115 | #define _EXTENSION_ __extension__ | ||
116 | #define OS_PACK __attribute__ ((packed)) | ||
117 | #define OS_ALIGN(n) __attribute__((aligned(n))) | ||
118 | #define OS_USED __attribute__((used)) | ||
119 | #else | ||
120 | #define _EXTENSION_ | ||
121 | #define OS_PACK | ||
122 | #define OS_ALIGN(n) | ||
123 | #define OS_USED | ||
124 | #endif | ||
125 | |||
126 | #if defined(_ix86_) || defined (__i386__) | ||
127 | /* ----------------------- Intel x86 processor family -------------------------*/ | ||
128 | /* Little endian */ | ||
129 | #undef _STRUCT_HIGH_BIT_FIRST_ | ||
130 | #define _STRUCT_LOW_BIT_FIRST_ | ||
131 | |||
132 | typedef unsigned char boolean; | ||
133 | typedef signed char int8; | ||
134 | typedef short int int16; | ||
135 | typedef long int int32; | ||
136 | _EXTENSION_ typedef long long int int64; | ||
137 | typedef unsigned char uint8; | ||
138 | typedef unsigned short int uint16; | ||
139 | typedef unsigned long int uint32; | ||
140 | _EXTENSION_ typedef unsigned long long int uint64; | ||
141 | |||
142 | typedef unsigned long int cpuaddr; | ||
143 | |||
144 | #elif defined (_ix64_) || defined (__x86_64__) | ||
145 | /* ----------------------- Intel/AMD x64 processor family -------------------------*/ | ||
146 | /* Little endian */ | ||
147 | #undef _STRUCT_HIGH_BIT_FIRST_ | ||
148 | #define _STRUCT_LOW_BIT_FIRST_ | ||
149 | |||
150 | typedef unsigned char boolean; | ||
151 | typedef signed char int8; | ||
152 | typedef short int int16; | ||
153 | typedef int int32; | ||
154 | typedef long int int64; | ||
155 | typedef unsigned char uint8; | ||
156 | typedef unsigned short int uint16; | ||
157 | typedef unsigned int uint32; | ||
158 | typedef unsigned long int uint64; | ||
159 | |||
160 | typedef unsigned long int cpuaddr; | ||
161 | |||
162 | #elif defined(__PPC__) || defined (__ppc__) | ||
163 | /* ----------------------- Motorola Power PC family ---------------------------*/ | ||
164 | /* The PPC can be programmed to be big or little endian, we assume native */ | ||
165 | /* Big endian */ | ||
166 | #define _STRUCT_HIGH_BIT_FIRST_ | ||
167 | #undef _STRUCT_LOW_BIT_FIRST_ | ||
168 | |||
169 | typedef unsigned char boolean; | ||
170 | typedef signed char int8; | ||
171 | typedef short int int16; | ||
172 | typedef long int int32; | ||
173 | _EXTENSION_ typedef long long int int64; | ||
174 | typedef unsigned char uint8; | ||
175 | typedef unsigned short int uint16; | ||
176 | typedef unsigned long int uint32; | ||
177 | _EXTENSION_ typedef unsigned long long int uint64; | ||
178 | |||
179 | typedef unsigned long int cpuaddr; | ||
180 | |||
181 | #elif defined(_m68k_) || defined(__m68k__) | ||
182 | /* ----------------------- Motorola m68k/Coldfire family ---------------------------*/ | ||
183 | /* Big endian */ | ||
184 | #define _STRUCT_HIGH_BIT_FIRST_ | ||
185 | #undef _STRUCT_LOW_BIT_FIRST_ | ||
186 | |||
187 | typedef unsigned char boolean; | ||
188 | typedef signed char int8; | ||
189 | typedef short int int16; | ||
190 | typedef long int int32; | ||
191 | _EXTENSION_ typedef long long int int64; | ||
192 | typedef unsigned char uint8; | ||
193 | typedef unsigned short int uint16; | ||
194 | typedef unsigned long int uint32; | ||
195 | _EXTENSION_ typedef unsigned long long int uint64; | ||
196 | |||
197 | typedef unsigned long int cpuaddr; | ||
198 | |||
199 | #elif defined (__ARM__) || defined(__arm__) | ||
200 | /* ----------------------- ARM processor family -------------------------*/ | ||
201 | /* Little endian */ | ||
202 | #undef _STRUCT_HIGH_BIT_FIRST_ | ||
203 | #define _STRUCT_LOW_BIT_FIRST_ | ||
204 | |||
205 | typedef unsigned char boolean; | ||
206 | typedef signed char int8; | ||
207 | typedef short int int16; | ||
208 | typedef long int int32; | ||
209 | _EXTENSION_ typedef long long int int64; | ||
210 | typedef unsigned char uint8; | ||
211 | typedef unsigned short int uint16; | ||
212 | typedef unsigned long int uint32; | ||
213 | _EXTENSION_ typedef unsigned long long int uint64; | ||
214 | |||
215 | typedef unsigned long int cpuaddr; | ||
216 | |||
217 | #elif defined(__SPARC__) || defined (_sparc_) | ||
218 | /* ----------------------- SPARC/LEON family ---------------------------*/ | ||
219 | /* SPARC Big endian */ | ||
220 | #define _STRUCT_HIGH_BIT_FIRST_ | ||
221 | #undef _STRUCT_LOW_BIT_FIRST_ | ||
222 | |||
223 | typedef unsigned char boolean; | ||
224 | typedef signed char int8; | ||
225 | typedef short int int16; | ||
226 | typedef long int int32; | ||
227 | _EXTENSION_ typedef long long int int64; | ||
228 | typedef unsigned char uint8; | ||
229 | typedef unsigned short int uint16; | ||
230 | typedef unsigned long int uint32; | ||
231 | _EXTENSION_ typedef unsigned long long int uint64; | ||
232 | |||
233 | typedef unsigned long int cpuaddr; | ||
234 | |||
235 | #else /* not any of the above */ | ||
236 | #error undefined processor | ||
237 | #endif /* processor types */ | ||
238 | |||
239 | #ifndef NULL /* pointer to nothing */ | ||
240 | #define NULL ((void *) 0) | ||
241 | #endif | ||
242 | |||
243 | #ifndef TRUE /* Boolean true */ | ||
244 | #define TRUE (1) | ||
245 | #endif | ||
246 | |||
247 | #ifndef FALSE /* Boolean false */ | ||
248 | #define FALSE (0) | ||
249 | #endif | ||
250 | |||
251 | /* | ||
252 | ** Check Sizes | ||
253 | */ | ||
254 | CompileTimeAssert(sizeof(uint8)==1, TypeUint8WrongSize); | ||
255 | CompileTimeAssert(sizeof(uint16)==2, TypeUint16WrongSize); | ||
256 | CompileTimeAssert(sizeof(uint32)==4, TypeUint32WrongSize); | ||
257 | CompileTimeAssert(sizeof(uint64)==8, TypeUint64WrongSize); | ||
258 | CompileTimeAssert(sizeof(int8)==1, Typeint8WrongSize); | ||
259 | CompileTimeAssert(sizeof(int16)==2, Typeint16WrongSize); | ||
260 | CompileTimeAssert(sizeof(int32)==4, Typeint32WrongSize); | ||
261 | CompileTimeAssert(sizeof(int64)==8, Typeint64WrongSize); | ||
262 | |||
263 | #ifdef __cplusplus | ||
264 | } | ||
265 | #endif | ||
266 | |||
267 | #endif /* _common_types_ */ | ||
diff --git a/lib/chibios/os/common/abstractions/nasa_cfe/osal/include/osapi-os-core.h b/lib/chibios/os/common/abstractions/nasa_cfe/osal/include/osapi-os-core.h new file mode 100644 index 000000000..8c9b13a78 --- /dev/null +++ b/lib/chibios/os/common/abstractions/nasa_cfe/osal/include/osapi-os-core.h | |||
@@ -0,0 +1,274 @@ | |||
1 | /* | ||
2 | ** File: osapi-os-core.h | ||
3 | ** | ||
4 | ** Copyright (c) 2004-2006, United States government as represented by the | ||
5 | ** administrator of the National Aeronautics Space Administration. | ||
6 | ** All rights reserved. This software was created at NASAs Goddard | ||
7 | ** Space Flight Center pursuant to government contracts. | ||
8 | ** | ||
9 | ** This is governed by the NASA Open Source Agreement and may be used, | ||
10 | ** distributed and modified only pursuant to the terms of that agreement. | ||
11 | ** | ||
12 | ** Author: Ezra Yeheksli -Code 582/Raytheon | ||
13 | ** | ||
14 | ** Purpose: Contains functions prototype definitions and variables declarations | ||
15 | ** for the OS Abstraction Layer, Core OS module | ||
16 | ** | ||
17 | ** $Revision: 1.8 $ | ||
18 | ** | ||
19 | ** $Date: 2013/07/25 10:02:00GMT-05:00 $ | ||
20 | ** | ||
21 | ** $Log: osapi-os-core.h $ | ||
22 | ** Revision 1.8 2013/07/25 10:02:00GMT-05:00 acudmore | ||
23 | ** removed circular include "osapi.h" | ||
24 | ** Revision 1.7 2012/04/11 09:30:48GMT-05:00 acudmore | ||
25 | ** Added OS_printf_enable and OS_printf_disable | ||
26 | ** Revision 1.6 2010/11/12 12:00:17EST acudmore | ||
27 | ** replaced copyright character with (c) and added open source notice where needed. | ||
28 | ** Revision 1.5 2010/11/10 15:33:14EST acudmore | ||
29 | ** Updated IntAttachHandler prototype | ||
30 | ** Revision 1.4 2010/03/08 12:06:28EST acudmore | ||
31 | ** added function pointer type to get rid of warnings | ||
32 | ** Revision 1.3 2010/02/01 12:37:15EST acudmore | ||
33 | ** added return code to OS API init | ||
34 | ** Revision 1.2 2009/08/04 10:49:09EDT acudmore | ||
35 | ** | ||
36 | */ | ||
37 | |||
38 | #ifndef _osapi_core_ | ||
39 | #define _osapi_core_ | ||
40 | |||
41 | #include <stdarg.h> /* for va_list */ | ||
42 | |||
43 | /*difines constants for OS_BinSemCreate for state of semaphore */ | ||
44 | #define OS_SEM_FULL 1 | ||
45 | #define OS_SEM_EMPTY 0 | ||
46 | |||
47 | /* #define for enabling floating point operations on a task*/ | ||
48 | #define OS_FP_ENABLED 1 | ||
49 | |||
50 | /* tables for the properties of objects */ | ||
51 | |||
52 | /*tasks */ | ||
53 | typedef struct | ||
54 | { | ||
55 | char name [OS_MAX_API_NAME]; | ||
56 | uint32 creator; | ||
57 | uint32 stack_size; | ||
58 | uint32 priority; | ||
59 | uint32 OStask_id; | ||
60 | }OS_task_prop_t; | ||
61 | |||
62 | /* queues */ | ||
63 | typedef struct | ||
64 | { | ||
65 | char name [OS_MAX_API_NAME]; | ||
66 | uint32 creator; | ||
67 | }OS_queue_prop_t; | ||
68 | |||
69 | /* Binary Semaphores */ | ||
70 | typedef struct | ||
71 | { | ||
72 | char name [OS_MAX_API_NAME]; | ||
73 | uint32 creator; | ||
74 | int32 value; | ||
75 | }OS_bin_sem_prop_t; | ||
76 | |||
77 | /* Counting Semaphores */ | ||
78 | typedef struct | ||
79 | { | ||
80 | char name [OS_MAX_API_NAME]; | ||
81 | uint32 creator; | ||
82 | int32 value; | ||
83 | }OS_count_sem_prop_t; | ||
84 | |||
85 | /* Mutexes */ | ||
86 | typedef struct | ||
87 | { | ||
88 | char name [OS_MAX_API_NAME]; | ||
89 | uint32 creator; | ||
90 | }OS_mut_sem_prop_t; | ||
91 | |||
92 | |||
93 | /* struct for OS_GetLocalTime() */ | ||
94 | |||
95 | typedef struct | ||
96 | { | ||
97 | uint32 seconds; | ||
98 | uint32 microsecs; | ||
99 | }OS_time_t; | ||
100 | |||
101 | /* heap info */ | ||
102 | typedef struct | ||
103 | { | ||
104 | uint32 free_bytes; | ||
105 | uint32 free_blocks; | ||
106 | uint32 largest_free_block; | ||
107 | }OS_heap_prop_t; | ||
108 | |||
109 | |||
110 | /* This typedef is for the OS_GetErrorName function, to ensure | ||
111 | * everyone is making an array of the same length */ | ||
112 | |||
113 | typedef char os_err_name_t[35]; | ||
114 | |||
115 | /* | ||
116 | ** These typedefs are for the task entry point | ||
117 | */ | ||
118 | typedef void osal_task; | ||
119 | typedef osal_task ((*osal_task_entry)(void)); | ||
120 | |||
121 | /* | ||
122 | ** Exported Functions | ||
123 | */ | ||
124 | |||
125 | /* | ||
126 | ** Initialization of API | ||
127 | */ | ||
128 | int32 OS_API_Init (void); | ||
129 | |||
130 | |||
131 | /* | ||
132 | ** Task API | ||
133 | */ | ||
134 | |||
135 | int32 OS_TaskCreate (uint32 *task_id, const char *task_name, | ||
136 | osal_task_entry function_pointer, | ||
137 | const uint32 *stack_pointer, | ||
138 | uint32 stack_size, | ||
139 | uint32 priority, uint32 flags); | ||
140 | |||
141 | int32 OS_TaskDelete (uint32 task_id); | ||
142 | void OS_TaskExit (void); | ||
143 | int32 OS_TaskInstallDeleteHandler(void *function_pointer); | ||
144 | int32 OS_TaskDelay (uint32 millisecond); | ||
145 | int32 OS_TaskSetPriority (uint32 task_id, uint32 new_priority); | ||
146 | int32 OS_TaskRegister (void); | ||
147 | uint32 OS_TaskGetId (void); | ||
148 | int32 OS_TaskGetIdByName (uint32 *task_id, const char *task_name); | ||
149 | int32 OS_TaskGetInfo (uint32 task_id, OS_task_prop_t *task_prop); | ||
150 | |||
151 | /* | ||
152 | ** Message Queue API | ||
153 | */ | ||
154 | |||
155 | /* | ||
156 | ** Queue Create now has the Queue ID returned to the caller. | ||
157 | */ | ||
158 | int32 OS_QueueCreate (uint32 *queue_id, const char *queue_name, | ||
159 | uint32 queue_depth, uint32 data_size, uint32 flags); | ||
160 | int32 OS_QueueDelete (uint32 queue_id); | ||
161 | int32 OS_QueueGet (uint32 queue_id, void *data, uint32 size, | ||
162 | uint32 *size_copied, int32 timeout); | ||
163 | int32 OS_QueuePut (uint32 queue_id, void *data, uint32 size, | ||
164 | uint32 flags); | ||
165 | int32 OS_QueueGetIdByName (uint32 *queue_id, const char *queue_name); | ||
166 | int32 OS_QueueGetInfo (uint32 queue_id, OS_queue_prop_t *queue_prop); | ||
167 | |||
168 | /* | ||
169 | ** Semaphore API | ||
170 | */ | ||
171 | |||
172 | int32 OS_BinSemCreate (uint32 *sem_id, const char *sem_name, | ||
173 | uint32 sem_initial_value, uint32 options); | ||
174 | int32 OS_BinSemFlush (uint32 sem_id); | ||
175 | int32 OS_BinSemGive (uint32 sem_id); | ||
176 | int32 OS_BinSemTake (uint32 sem_id); | ||
177 | int32 OS_BinSemTimedWait (uint32 sem_id, uint32 msecs); | ||
178 | int32 OS_BinSemDelete (uint32 sem_id); | ||
179 | int32 OS_BinSemGetIdByName (uint32 *sem_id, const char *sem_name); | ||
180 | int32 OS_BinSemGetInfo (uint32 sem_id, OS_bin_sem_prop_t *bin_prop); | ||
181 | |||
182 | int32 OS_CountSemCreate (uint32 *sem_id, const char *sem_name, | ||
183 | uint32 sem_initial_value, uint32 options); | ||
184 | int32 OS_CountSemGive (uint32 sem_id); | ||
185 | int32 OS_CountSemTake (uint32 sem_id); | ||
186 | int32 OS_CountSemTimedWait (uint32 sem_id, uint32 msecs); | ||
187 | int32 OS_CountSemDelete (uint32 sem_id); | ||
188 | int32 OS_CountSemGetIdByName (uint32 *sem_id, const char *sem_name); | ||
189 | int32 OS_CountSemGetInfo (uint32 sem_id, OS_count_sem_prop_t *count_prop); | ||
190 | |||
191 | /* | ||
192 | ** Mutex API | ||
193 | */ | ||
194 | |||
195 | int32 OS_MutSemCreate (uint32 *sem_id, const char *sem_name, uint32 options); | ||
196 | int32 OS_MutSemGive (uint32 sem_id); | ||
197 | int32 OS_MutSemTake (uint32 sem_id); | ||
198 | int32 OS_MutSemDelete (uint32 sem_id); | ||
199 | int32 OS_MutSemGetIdByName (uint32 *sem_id, const char *sem_name); | ||
200 | int32 OS_MutSemGetInfo (uint32 sem_id, OS_mut_sem_prop_t *mut_prop); | ||
201 | |||
202 | /* | ||
203 | ** OS Time/Tick related API | ||
204 | */ | ||
205 | |||
206 | int32 OS_Milli2Ticks (uint32 milli_seconds); | ||
207 | int32 OS_Tick2Micros (void); | ||
208 | int32 OS_GetLocalTime (OS_time_t *time_struct); | ||
209 | int32 OS_SetLocalTime (OS_time_t *time_struct); | ||
210 | |||
211 | /* | ||
212 | ** Exception API | ||
213 | */ | ||
214 | |||
215 | int32 OS_ExcAttachHandler (uint32 ExceptionNumber, | ||
216 | void (*ExceptionHandler)(uint32, uint32 *,uint32), | ||
217 | int32 parameter); | ||
218 | int32 OS_ExcEnable (int32 ExceptionNumber); | ||
219 | int32 OS_ExcDisable (int32 ExceptionNumber); | ||
220 | |||
221 | /* | ||
222 | ** Floating Point Unit API | ||
223 | */ | ||
224 | |||
225 | int32 OS_FPUExcAttachHandler (uint32 ExceptionNumber, void * ExceptionHandler , | ||
226 | int32 parameter); | ||
227 | int32 OS_FPUExcEnable (int32 ExceptionNumber); | ||
228 | int32 OS_FPUExcDisable (int32 ExceptionNumber); | ||
229 | int32 OS_FPUExcSetMask (uint32 mask); | ||
230 | int32 OS_FPUExcGetMask (uint32 *mask); | ||
231 | |||
232 | /* | ||
233 | ** Interrupt API | ||
234 | */ | ||
235 | int32 OS_IntAttachHandler (uint32 InterruptNumber, osal_task_entry InterruptHandler, int32 parameter); | ||
236 | int32 OS_IntUnlock (int32 IntLevel); | ||
237 | int32 OS_IntLock (void); | ||
238 | |||
239 | int32 OS_IntEnable (int32 Level); | ||
240 | int32 OS_IntDisable (int32 Level); | ||
241 | |||
242 | int32 OS_IntSetMask (uint32 mask); | ||
243 | int32 OS_IntGetMask (uint32 *mask); | ||
244 | int32 OS_IntAck (int32 InterruptNumber); | ||
245 | |||
246 | /* | ||
247 | ** Shared memory API | ||
248 | */ | ||
249 | int32 OS_ShMemInit (void); | ||
250 | int32 OS_ShMemCreate (uint32 *Id, uint32 NBytes, char* SegName); | ||
251 | int32 OS_ShMemSemTake (uint32 Id); | ||
252 | int32 OS_ShMemSemGive (uint32 Id); | ||
253 | int32 OS_ShMemAttach (uint32 * Address, uint32 Id); | ||
254 | int32 OS_ShMemGetIdByName (uint32 *ShMemId, const char *SegName ); | ||
255 | |||
256 | /* | ||
257 | ** Heap API | ||
258 | */ | ||
259 | int32 OS_HeapGetInfo (OS_heap_prop_t *heap_prop); | ||
260 | |||
261 | /* | ||
262 | ** API for useful debugging function | ||
263 | */ | ||
264 | int32 OS_GetErrorName (int32 error_num, os_err_name_t* err_name); | ||
265 | |||
266 | |||
267 | /* | ||
268 | ** Abstraction for printf statements | ||
269 | */ | ||
270 | void OS_printf( const char *string, ...); | ||
271 | void OS_printf_disable(void); | ||
272 | void OS_printf_enable(void); | ||
273 | |||
274 | #endif | ||
diff --git a/lib/chibios/os/common/abstractions/nasa_cfe/osal/include/osapi-os-custom.h b/lib/chibios/os/common/abstractions/nasa_cfe/osal/include/osapi-os-custom.h new file mode 100644 index 000000000..15a308848 --- /dev/null +++ b/lib/chibios/os/common/abstractions/nasa_cfe/osal/include/osapi-os-custom.h | |||
@@ -0,0 +1,68 @@ | |||
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 osapi-os-custom.h | ||
19 | * @brief Custom OSAPI extensions header. | ||
20 | * | ||
21 | * @addtogroup osapi-custom | ||
22 | * @{ | ||
23 | */ | ||
24 | |||
25 | #ifndef OSAPI_CUSTOM_H | ||
26 | #define OSAPI_CUSTOM_H | ||
27 | |||
28 | /*===========================================================================*/ | ||
29 | /* Module constants. */ | ||
30 | /*===========================================================================*/ | ||
31 | |||
32 | /*===========================================================================*/ | ||
33 | /* Module pre-compile time settings. */ | ||
34 | /*===========================================================================*/ | ||
35 | |||
36 | /*===========================================================================*/ | ||
37 | /* Derived constants and error checks. */ | ||
38 | /*===========================================================================*/ | ||
39 | |||
40 | /*===========================================================================*/ | ||
41 | /* Module data structures and types. */ | ||
42 | /*===========================================================================*/ | ||
43 | |||
44 | /*===========================================================================*/ | ||
45 | /* Module macros. */ | ||
46 | /*===========================================================================*/ | ||
47 | |||
48 | /*===========================================================================*/ | ||
49 | /* External declarations. */ | ||
50 | /*===========================================================================*/ | ||
51 | |||
52 | #ifdef __cplusplus | ||
53 | extern "C" { | ||
54 | #endif | ||
55 | void OS_set_printf(int (*printf)(const char *fmt, ...)); | ||
56 | boolean OS_TaskDeleteCheck(void); | ||
57 | int32 OS_TaskWait(uint32 task_id); | ||
58 | #ifdef __cplusplus | ||
59 | } | ||
60 | #endif | ||
61 | |||
62 | /*===========================================================================*/ | ||
63 | /* Module inline functions. */ | ||
64 | /*===========================================================================*/ | ||
65 | |||
66 | #endif /* OSAPI_CUSTOM_H */ | ||
67 | |||
68 | /** @} */ | ||
diff --git a/lib/chibios/os/common/abstractions/nasa_cfe/osal/include/osapi-os-filesys.h b/lib/chibios/os/common/abstractions/nasa_cfe/osal/include/osapi-os-filesys.h new file mode 100644 index 000000000..c46800341 --- /dev/null +++ b/lib/chibios/os/common/abstractions/nasa_cfe/osal/include/osapi-os-filesys.h | |||
@@ -0,0 +1,419 @@ | |||
1 | /* | ||
2 | ** File: osapi-os-filesys.h | ||
3 | ** | ||
4 | ** Copyright (c) 2004-2006, United States government as represented by the | ||
5 | ** administrator of the National Aeronautics Space Administration. | ||
6 | ** All rights reserved. This software was created at NASAs Goddard | ||
7 | ** Space Flight Center pursuant to government contracts. | ||
8 | ** | ||
9 | ** This is governed by the NASA Open Source Agreement and may be used, | ||
10 | ** distributed and modified only pursuant to the terms of that agreement. | ||
11 | ** | ||
12 | ** Author: Alan Cudmore Code 582 | ||
13 | ** | ||
14 | ** Purpose: Contains functions prototype definitions and variables declarations | ||
15 | ** for the OS Abstraction Layer, File System module | ||
16 | ** | ||
17 | ** $Revision: 1.11 $ | ||
18 | ** | ||
19 | ** $Date: 2013/12/16 12:57:41GMT-05:00 $ | ||
20 | ** | ||
21 | ** $Log: osapi-os-filesys.h $ | ||
22 | ** Revision 1.11 2013/12/16 12:57:41GMT-05:00 acudmore | ||
23 | ** Added macros for Volume name length and physical device name length | ||
24 | ** Revision 1.10 2013/07/29 12:05:48GMT-05:00 acudmore | ||
25 | ** Added define for device and volume name length | ||
26 | ** Revision 1.9 2013/07/25 14:31:21GMT-05:00 acudmore | ||
27 | ** Added prototype and datatype for OS_GetFsInfo | ||
28 | ** Revision 1.8 2011/12/05 12:04:21GMT-05:00 acudmore | ||
29 | ** Added OS_rewinddir API | ||
30 | ** Revision 1.7 2011/04/05 16:01:12EDT acudmore | ||
31 | ** Added OS_CloseFileByName and OS_CloseAllFiles | ||
32 | ** Revision 1.6 2010/11/15 11:04:38EST acudmore | ||
33 | ** Added OS_FileOpenCheck function. | ||
34 | ** Revision 1.5 2010/11/12 12:00:18EST acudmore | ||
35 | ** replaced copyright character with (c) and added open source notice where needed. | ||
36 | ** Revision 1.4 2010/02/01 12:28:57EST acudmore | ||
37 | ** Added OS_fsBytesFree API | ||
38 | ** Revision 1.3 2010/01/25 14:44:26EST acudmore | ||
39 | ** renamed "new" variable to avoid C++ reserved name conflict. | ||
40 | ** Revision 1.2 2009/07/14 15:16:05EDT acudmore | ||
41 | ** Added OS_TranslatePath to the API | ||
42 | ** Revision 1.1 2008/04/20 22:36:01EDT ruperera | ||
43 | ** Initial revision | ||
44 | ** Member added to project c:/MKSDATA/MKS-REPOSITORY/MKS-OSAL-REPOSITORY/src/os/inc/project.pj | ||
45 | ** Revision 1.1 2007/10/16 16:14:52EDT apcudmore | ||
46 | ** Initial revision | ||
47 | ** Member added to project d:/mksdata/MKS-OSAL-REPOSITORY/src/os/inc/project.pj | ||
48 | ** Revision 1.1 2007/08/24 13:43:24EDT apcudmore | ||
49 | ** Initial revision | ||
50 | ** Member added to project d:/mksdata/MKS-CFE-PROJECT/fsw/cfe-core/os/inc/project.pj | ||
51 | ** Revision 1.17 2007/06/07 09:59:14EDT njyanchik | ||
52 | ** I replaced the second OS_cp definition with OS_mv | ||
53 | ** Revision 1.16 2007/06/05 16:25:33EDT apcudmore | ||
54 | ** Increased Number of volume table entries from 10 to 14. | ||
55 | ** Added 2 extra EEPROM disk mappings to RAD750 Volume table + 2 spares | ||
56 | ** Added 4 spares to every other volume table. | ||
57 | ** Revision 1.15 2007/05/25 09:17:56EDT njyanchik | ||
58 | ** I added the rmfs call to the OSAL and updated the unit test stubs to match | ||
59 | ** Revision 1.14 2007/03/21 10:15:29EST njyanchik | ||
60 | ** I mistakenly put the wrong length in for the path in the OS_FDTableEntry structure, and I added | ||
61 | ** some code that will set and out of range file descriptors .IsValid flag to false in OS_FDGetInfo | ||
62 | ** Revision 1.13 2007/03/06 11:52:46EST njyanchik | ||
63 | ** This change goes with the previous CP, I forgot to include it | ||
64 | ** Revision 1.12 2007/02/28 14:57:45EST njyanchik | ||
65 | ** The updates for supporting copying and moving files are now supported | ||
66 | ** Revision 1.11 2007/02/27 15:22:11EST njyanchik | ||
67 | ** This CP has the initial import of the new file descripor table mechanism | ||
68 | ** Revision 1.10 2006/12/20 10:27:09EST njyanchik | ||
69 | ** This change package incorporates all the changes necessary for the addition | ||
70 | ** of a new API to get the real physical drive undernieth a mount point | ||
71 | ** Revision 1.9 2006/11/14 14:44:28GMT-05:00 njyanchik | ||
72 | ** Checks were added to the OS fs calls that look at the return of a function that | ||
73 | ** changes the name of paths from abstracted to local path names. | ||
74 | ** Revision 1.8 2006/10/30 16:12:19GMT-05:00 apcudmore | ||
75 | ** Updated Compact flash and RAM device names for vxWorks 6.2 changes. | ||
76 | ** Revision 1.7 2006/10/25 11:31:18EDT njyanchik | ||
77 | ** This CP incorporates changes to every bsp_voltab.c file. I increased the number | ||
78 | ** entries in the volume table to 10. I also changed the #define in the os_filesys.h | ||
79 | ** file for the number of entries to match. | ||
80 | ** | ||
81 | ** This update also includes adding the prototype for OS_initfs in os_filesys.h | ||
82 | ** Revision 1.6 2006/09/26 09:03:46GMT-05:00 njyanchik | ||
83 | ** Contains the initial import of the ES Shell commands interface | ||
84 | ** Revision 1.5 2006/07/25 15:37:52EDT njyanchik | ||
85 | ** It turns out the both the FS app and the OSAL were incorrect where file descriptors are | ||
86 | ** concerned. the file descriptors should be int32 across the board. | ||
87 | ** Revision 1.4 2006/01/20 11:56:18EST njyanchik | ||
88 | ** Fixed header file information to match api document | ||
89 | ** Revision 1.26 2005/07/12 17:13:56 nyanchik | ||
90 | ** Moved the Volume table to a bsp table in the arch directories. | ||
91 | ** | ||
92 | ** Revision 1.2 2005/07/11 16:26:57EDT apcudmore | ||
93 | ** OSAPI 2.0 integration | ||
94 | ** Revision 1.25 2005/07/06 16:11:17 nyanchik | ||
95 | ** *** empty log message *** | ||
96 | ** | ||
97 | ** Revision 1.24 2005/07/05 18:34:55 nyanchik | ||
98 | ** fixed issues found in code walkthrogh. Also removed the OS_Info* functions that are going in the BSP | ||
99 | ** | ||
100 | ** Revision 1.23 2005/06/17 19:46:34 nyanchik | ||
101 | ** added new file system style to linux and rtems. | ||
102 | ** | ||
103 | ** Revision 1.22 2005/06/15 16:43:48 nyanchik | ||
104 | ** added extra parenthesis for the .h file # defines | ||
105 | ** | ||
106 | ** Revision 1.21 2005/06/06 14:17:42 nyanchik | ||
107 | ** added headers to osapi-os-core.h and osapi-os-filesys.h | ||
108 | ** | ||
109 | ** Revision 1.20 2005/06/02 18:04:24 nyanchik | ||
110 | ** *** empty log message *** | ||
111 | ** | ||
112 | ** Revision 1.1 2005/03/15 18:26:32 nyanchik | ||
113 | ** *** empty log message *** | ||
114 | ** | ||
115 | ** | ||
116 | ** Date Written: | ||
117 | ** | ||
118 | ** | ||
119 | */ | ||
120 | |||
121 | #ifndef _osapi_filesys_ | ||
122 | #define _osapi_filesys_ | ||
123 | #include <stdio.h> | ||
124 | #include <stdlib.h> | ||
125 | #include <dirent.h> | ||
126 | #include <sys/stat.h> | ||
127 | |||
128 | #define OS_READ_ONLY 0 | ||
129 | #define OS_WRITE_ONLY 1 | ||
130 | #define OS_READ_WRITE 2 | ||
131 | |||
132 | #define OS_SEEK_SET 0 | ||
133 | #define OS_SEEK_CUR 1 | ||
134 | #define OS_SEEK_END 2 | ||
135 | |||
136 | #define OS_CHK_ONLY 0 | ||
137 | #define OS_REPAIR 1 | ||
138 | |||
139 | #define FS_BASED 0 | ||
140 | #define RAM_DISK 1 | ||
141 | #define EEPROM_DISK 2 | ||
142 | #define ATA_DISK 3 | ||
143 | |||
144 | |||
145 | /* | ||
146 | ** Number of entries in the internal volume table | ||
147 | */ | ||
148 | #define NUM_TABLE_ENTRIES 14 | ||
149 | |||
150 | /* | ||
151 | ** Length of a Device and Volume name | ||
152 | */ | ||
153 | #define OS_FS_DEV_NAME_LEN 32 | ||
154 | #define OS_FS_PHYS_NAME_LEN 64 | ||
155 | #define OS_FS_VOL_NAME_LEN 32 | ||
156 | |||
157 | |||
158 | /* | ||
159 | ** Defines for File System Calls | ||
160 | */ | ||
161 | #define OS_FS_SUCCESS 0 | ||
162 | #define OS_FS_ERROR (-1) | ||
163 | #define OS_FS_ERR_INVALID_POINTER (-2) | ||
164 | #define OS_FS_ERR_PATH_TOO_LONG (-3) | ||
165 | #define OS_FS_ERR_NAME_TOO_LONG (-4) | ||
166 | #define OS_FS_UNIMPLEMENTED (-5) | ||
167 | #define OS_FS_ERR_DRIVE_NOT_CREATED (-6) | ||
168 | #define OS_FS_ERR_DEVICE_NOT_FREE (-7) | ||
169 | #define OS_FS_ERR_PATH_INVALID (-8) | ||
170 | #define OS_FS_ERR_NO_FREE_FDS (-9) | ||
171 | #define OS_FS_ERR_INVALID_FD (-10) | ||
172 | |||
173 | /* This typedef is for the OS_FS_GetErrorName function, to ensure | ||
174 | * everyone is making an array of the same length */ | ||
175 | |||
176 | typedef char os_fs_err_name_t[35]; | ||
177 | |||
178 | |||
179 | /* | ||
180 | ** Internal structure of the OS volume table for | ||
181 | ** mounted file systems and path translation | ||
182 | */ | ||
183 | typedef struct | ||
184 | { | ||
185 | char DeviceName [OS_FS_DEV_NAME_LEN]; | ||
186 | char PhysDevName [OS_FS_PHYS_NAME_LEN]; | ||
187 | uint32 VolumeType; | ||
188 | uint8 VolatileFlag; | ||
189 | uint8 FreeFlag; | ||
190 | uint8 IsMounted; | ||
191 | char VolumeName [OS_FS_VOL_NAME_LEN]; | ||
192 | char MountPoint [OS_MAX_PATH_LEN]; | ||
193 | uint32 BlockSize; | ||
194 | |||
195 | }OS_VolumeInfo_t; | ||
196 | |||
197 | typedef struct | ||
198 | { | ||
199 | int32 OSfd; /* The underlying OS's file descriptor */ | ||
200 | char Path[OS_MAX_PATH_LEN]; /* The path of the file opened */ | ||
201 | uint32 User; /* The task id of the task who opened the file*/ | ||
202 | uint8 IsValid; /* Whether or not this entry is valid */ | ||
203 | }OS_FDTableEntry; | ||
204 | |||
205 | typedef struct | ||
206 | { | ||
207 | uint32 MaxFds; /* Total number of file descriptors */ | ||
208 | uint32 FreeFds; /* Total number that are free */ | ||
209 | uint32 MaxVolumes; /* Maximum number of volumes */ | ||
210 | uint32 FreeVolumes; /* Total number of volumes free */ | ||
211 | } os_fsinfo_t; | ||
212 | |||
213 | /* modified to posix calls, since all of the | ||
214 | * applicable OSes use the posix calls */ | ||
215 | |||
216 | typedef struct stat os_fstat_t; | ||
217 | typedef DIR* os_dirp_t; | ||
218 | typedef struct dirent os_dirent_t; | ||
219 | /* still don't know what this should be*/ | ||
220 | typedef unsigned long int os_fshealth_t; | ||
221 | |||
222 | /* | ||
223 | * Exported Functions | ||
224 | */ | ||
225 | |||
226 | |||
227 | /****************************************************************************** | ||
228 | ** Standard File system API | ||
229 | ******************************************************************************/ | ||
230 | /* | ||
231 | * Initializes the File System functions | ||
232 | */ | ||
233 | |||
234 | int32 OS_FS_Init(void); | ||
235 | |||
236 | /* | ||
237 | * Creates a file specified by path | ||
238 | */ | ||
239 | int32 OS_creat (const char *path, int32 access); | ||
240 | |||
241 | /* | ||
242 | * Opend a file for reading/writing. Returns file descriptor | ||
243 | */ | ||
244 | int32 OS_open (const char *path, int32 access, uint32 mode); | ||
245 | |||
246 | /* | ||
247 | * Closes an open file. | ||
248 | */ | ||
249 | int32 OS_close (int32 filedes); | ||
250 | |||
251 | /* | ||
252 | * Reads nbytes bytes from file into buffer | ||
253 | */ | ||
254 | int32 OS_read (int32 filedes, void *buffer, uint32 nbytes); | ||
255 | |||
256 | /* | ||
257 | * Write nybytes bytes of buffer into the file | ||
258 | */ | ||
259 | int32 OS_write (int32 filedes, void *buffer, uint32 nbytes); | ||
260 | |||
261 | /* | ||
262 | * Changes the permissions of a file | ||
263 | */ | ||
264 | int32 OS_chmod (const char *path, uint32 access); | ||
265 | |||
266 | /* | ||
267 | * Returns file status information in filestats | ||
268 | */ | ||
269 | int32 OS_stat (const char *path, os_fstat_t *filestats); | ||
270 | |||
271 | /* | ||
272 | * Seeks to the specified position of an open file | ||
273 | */ | ||
274 | int32 OS_lseek (int32 filedes, int32 offset, uint32 whence); | ||
275 | |||
276 | /* | ||
277 | * Removes a file from the file system | ||
278 | */ | ||
279 | int32 OS_remove (const char *path); | ||
280 | |||
281 | /* | ||
282 | * Renames a file in the file system | ||
283 | */ | ||
284 | int32 OS_rename (const char *old_filename, const char *new_filename); | ||
285 | |||
286 | /* | ||
287 | * copies a single file from src to dest | ||
288 | */ | ||
289 | int32 OS_cp (const char *src, const char *dest); | ||
290 | |||
291 | /* | ||
292 | * moves a single file from src to dest | ||
293 | */ | ||
294 | int32 OS_mv (const char *src, const char *dest); | ||
295 | |||
296 | /* | ||
297 | * Copies the info of an open file to the structure | ||
298 | */ | ||
299 | int32 OS_FDGetInfo (int32 filedes, OS_FDTableEntry *fd_prop); | ||
300 | |||
301 | /* | ||
302 | ** Check to see if a file is open | ||
303 | */ | ||
304 | int32 OS_FileOpenCheck(char *Filename); | ||
305 | |||
306 | /* | ||
307 | ** Close all open files | ||
308 | */ | ||
309 | int32 OS_CloseAllFiles(void); | ||
310 | |||
311 | /* | ||
312 | ** Close a file by filename | ||
313 | */ | ||
314 | int32 OS_CloseFileByName(char *Filename); | ||
315 | |||
316 | |||
317 | /****************************************************************************** | ||
318 | ** Directory API | ||
319 | ******************************************************************************/ | ||
320 | |||
321 | /* | ||
322 | * Makes a new directory | ||
323 | */ | ||
324 | int32 OS_mkdir (const char *path, uint32 access); | ||
325 | |||
326 | /* | ||
327 | * Opens a directory for searching | ||
328 | */ | ||
329 | os_dirp_t OS_opendir (const char *path); | ||
330 | |||
331 | /* | ||
332 | * Closes an open directory | ||
333 | */ | ||
334 | int32 OS_closedir(os_dirp_t directory); | ||
335 | |||
336 | /* | ||
337 | * Rewinds an open directory | ||
338 | */ | ||
339 | void OS_rewinddir(os_dirp_t directory); | ||
340 | |||
341 | /* | ||
342 | * Reads the next object in the directory | ||
343 | */ | ||
344 | os_dirent_t * OS_readdir (os_dirp_t directory); | ||
345 | |||
346 | /* | ||
347 | * Removes an empty directory from the file system. | ||
348 | */ | ||
349 | int32 OS_rmdir (const char *path); | ||
350 | |||
351 | /****************************************************************************** | ||
352 | ** System Level API | ||
353 | ******************************************************************************/ | ||
354 | /* | ||
355 | * Makes a file system | ||
356 | */ | ||
357 | int32 OS_mkfs (char *address,char *devname, char *volname, | ||
358 | uint32 blocksize, uint32 numblocks); | ||
359 | /* | ||
360 | * Mounts a file system | ||
361 | */ | ||
362 | int32 OS_mount (const char *devname, char *mountpoint); | ||
363 | |||
364 | /* | ||
365 | * Initializes an existing file system | ||
366 | */ | ||
367 | int32 OS_initfs (char *address,char *devname, char *volname, | ||
368 | uint32 blocksize, uint32 numblocks); | ||
369 | |||
370 | /* | ||
371 | * removes a file system | ||
372 | */ | ||
373 | int32 OS_rmfs (char *devname); | ||
374 | |||
375 | /* | ||
376 | * Unmounts a mounted file system | ||
377 | */ | ||
378 | int32 OS_unmount (const char *mountpoint); | ||
379 | |||
380 | /* | ||
381 | * Returns the number of free blocks in a file system | ||
382 | */ | ||
383 | int32 OS_fsBlocksFree (const char *name); | ||
384 | |||
385 | /* | ||
386 | ** Returns the number of free bytes in a file system | ||
387 | ** Note the 64 bit data type to support filesystems that | ||
388 | ** are greater than 4 Gigabytes | ||
389 | */ | ||
390 | int32 OS_fsBytesFree (const char *name, uint64 *bytes_free); | ||
391 | |||
392 | /* | ||
393 | * Checks the health of a file system and repairs it if neccesary | ||
394 | */ | ||
395 | os_fshealth_t OS_chkfs (const char *name, boolean repair); | ||
396 | |||
397 | /* | ||
398 | * Returns in the parameter the physical drive underneith the mount point | ||
399 | */ | ||
400 | int32 OS_FS_GetPhysDriveName (char * PhysDriveName, char * MountPoint); | ||
401 | |||
402 | /* | ||
403 | ** Translates a OSAL Virtual file system path to a host Local path | ||
404 | */ | ||
405 | int32 OS_TranslatePath ( const char *VirtualPath, char *LocalPath); | ||
406 | |||
407 | /* | ||
408 | ** Returns information about the file system in an os_fsinfo_t | ||
409 | */ | ||
410 | int32 OS_GetFsInfo(os_fsinfo_t *filesys_info); | ||
411 | |||
412 | /****************************************************************************** | ||
413 | ** Shell API | ||
414 | ******************************************************************************/ | ||
415 | |||
416 | /* executes the shell command passed into is and writes the output of that | ||
417 | * command to the file specified by the given OSAPI file descriptor */ | ||
418 | int32 OS_ShellOutputToFile(char* Cmd, int32 OS_fd); | ||
419 | #endif | ||
diff --git a/lib/chibios/os/common/abstractions/nasa_cfe/osal/include/osapi-os-loader.h b/lib/chibios/os/common/abstractions/nasa_cfe/osal/include/osapi-os-loader.h new file mode 100644 index 000000000..4f0b21b62 --- /dev/null +++ b/lib/chibios/os/common/abstractions/nasa_cfe/osal/include/osapi-os-loader.h | |||
@@ -0,0 +1,91 @@ | |||
1 | /* | ||
2 | ** File: osapi-os-loader.h | ||
3 | ** | ||
4 | ** Copyright (c) 2004-2006, United States government as represented by the | ||
5 | ** administrator of the National Aeronautics Space Administration. | ||
6 | ** All rights reserved. This software was created at NASAs Goddard | ||
7 | ** Space Flight Center pursuant to government contracts. | ||
8 | ** | ||
9 | ** This is governed by the NASA Open Source Agreement and may be used, | ||
10 | ** distributed and modified only pursuant to the terms of that agreement. | ||
11 | ** | ||
12 | ** Author: Alan Cudmore - Code 582 | ||
13 | ** | ||
14 | ** Purpose: Contains functions prototype definitions and variables declarations | ||
15 | ** for the OS Abstraction Layer, Object file loader API | ||
16 | ** | ||
17 | ** $Revision: 1.5 $ | ||
18 | ** | ||
19 | ** $Date: 2013/07/25 10:02:08GMT-05:00 $ | ||
20 | ** | ||
21 | ** $Log: osapi-os-loader.h $ | ||
22 | ** Revision 1.5 2013/07/25 10:02:08GMT-05:00 acudmore | ||
23 | ** removed circular include "osapi.h" | ||
24 | ** Revision 1.4 2010/11/12 12:00:18GMT-05:00 acudmore | ||
25 | ** replaced copyright character with (c) and added open source notice where needed. | ||
26 | ** Revision 1.3 2010/02/01 12:38:06EST acudmore | ||
27 | ** added return code to OS_ModuleTableInit | ||
28 | ** Revision 1.2 2008/06/20 15:13:43EDT apcudmore | ||
29 | ** Checked in new Module loader/symbol table functionality | ||
30 | ** Revision 1.1 2008/04/20 22:36:02EDT ruperera | ||
31 | ** Initial revision | ||
32 | ** Member added to project c:/MKSDATA/MKS-REPOSITORY/MKS-OSAL-REPOSITORY/src/os/inc/project.pj | ||
33 | ** Revision 1.1 2008/02/07 11:08:24EST apcudmore | ||
34 | ** Initial revision | ||
35 | ** Member added to project d:/mksdata/MKS-OSAL-REPOSITORY/src/os/inc/project.pj | ||
36 | ** | ||
37 | ** | ||
38 | */ | ||
39 | |||
40 | #ifndef _osapi_loader_ | ||
41 | #define _osapi_loader_ | ||
42 | |||
43 | /* | ||
44 | ** Defines | ||
45 | */ | ||
46 | |||
47 | |||
48 | /* | ||
49 | ** Typedefs | ||
50 | */ | ||
51 | |||
52 | typedef struct | ||
53 | { | ||
54 | uint32 valid; | ||
55 | uint32 code_address; | ||
56 | uint32 code_size; | ||
57 | uint32 data_address; | ||
58 | uint32 data_size; | ||
59 | uint32 bss_address; | ||
60 | uint32 bss_size; | ||
61 | uint32 flags; | ||
62 | } OS_module_address_t; | ||
63 | |||
64 | typedef struct | ||
65 | { | ||
66 | int free; | ||
67 | uint32 entry_point; | ||
68 | uint32 host_module_id; | ||
69 | char filename[OS_MAX_PATH_LEN]; | ||
70 | char name[OS_MAX_API_NAME]; | ||
71 | OS_module_address_t addr; | ||
72 | |||
73 | } OS_module_record_t; | ||
74 | |||
75 | /* | ||
76 | ** Loader API | ||
77 | */ | ||
78 | int32 OS_ModuleTableInit ( void ); | ||
79 | |||
80 | int32 OS_SymbolLookup (uint32 *symbol_address, char *symbol_name ); | ||
81 | |||
82 | int32 OS_SymbolTableDump ( char *filename, uint32 size_limit ); | ||
83 | |||
84 | int32 OS_ModuleLoad ( uint32 *module_id, char *module_name, char *filename ); | ||
85 | |||
86 | int32 OS_ModuleUnload ( uint32 module_id ); | ||
87 | |||
88 | int32 OS_ModuleInfo ( uint32 module_id, OS_module_record_t *module_info ); | ||
89 | |||
90 | |||
91 | #endif | ||
diff --git a/lib/chibios/os/common/abstractions/nasa_cfe/osal/include/osapi-os-net.h b/lib/chibios/os/common/abstractions/nasa_cfe/osal/include/osapi-os-net.h new file mode 100644 index 000000000..b8cc67d69 --- /dev/null +++ b/lib/chibios/os/common/abstractions/nasa_cfe/osal/include/osapi-os-net.h | |||
@@ -0,0 +1,61 @@ | |||
1 | /* | ||
2 | ** File: osapi-os-net.h | ||
3 | ** | ||
4 | ** Copyright (c) 2004-2006, United States government as represented by the | ||
5 | ** administrator of the National Aeronautics Space Administration. | ||
6 | ** All rights reserved. This software was created at NASAs Goddard | ||
7 | ** Space Flight Center pursuant to government contracts. | ||
8 | ** | ||
9 | ** This is governed by the NASA Open Source Agreement and may be used, | ||
10 | ** distributed and modified only pursuant to the terms of that agreement. | ||
11 | ** | ||
12 | ** Author: Alan Cudmore Code 582 | ||
13 | ** | ||
14 | ** Purpose: Contains functions prototype definitions and variables declarations | ||
15 | ** for the OS Abstraction Layer, Network Module | ||
16 | ** | ||
17 | ** $Revision: 1.2 $ | ||
18 | ** | ||
19 | ** $Date: 2010/11/12 12:00:19GMT-05:00 $ | ||
20 | ** | ||
21 | ** $Log: osapi-os-net.h $ | ||
22 | ** Revision 1.2 2010/11/12 12:00:19GMT-05:00 acudmore | ||
23 | ** replaced copyright character with (c) and added open source notice where needed. | ||
24 | ** Revision 1.1 2008/04/20 22:36:02EDT ruperera | ||
25 | ** Initial revision | ||
26 | ** Member added to project c:/MKSDATA/MKS-REPOSITORY/MKS-OSAL-REPOSITORY/src/os/inc/project.pj | ||
27 | ** Revision 1.1 2007/10/16 16:14:52EDT apcudmore | ||
28 | ** Initial revision | ||
29 | ** Member added to project d:/mksdata/MKS-OSAL-REPOSITORY/src/os/inc/project.pj | ||
30 | ** Revision 1.1 2007/08/24 13:43:25EDT apcudmore | ||
31 | ** Initial revision | ||
32 | ** Member added to project d:/mksdata/MKS-CFE-PROJECT/fsw/cfe-core/os/inc/project.pj | ||
33 | ** Revision 1.3 2006/01/20 11:56:18EST njyanchik | ||
34 | ** Fixed header file information to match api document | ||
35 | ** Revision 1.4 2005/06/07 16:49:31 nyanchik | ||
36 | ** changed returns code for osapi.c to all int32 from uint32 | ||
37 | ** | ||
38 | ** Revision 1.3 2005/03/22 19:04:54 acudmore | ||
39 | ** fixed uint type | ||
40 | ** | ||
41 | ** Revision 1.2 2005/03/22 18:59:33 acudmore | ||
42 | ** updated prototype | ||
43 | ** | ||
44 | ** Revision 1.1 2005/03/22 18:58:51 acudmore | ||
45 | ** added osapi network interface | ||
46 | ** | ||
47 | ** Revision 1.1 2005/03/15 18:26:32 nyanchik | ||
48 | ** *** empty log message *** | ||
49 | ** | ||
50 | ** | ||
51 | ** Date Written: | ||
52 | ** | ||
53 | ** | ||
54 | */ | ||
55 | #ifndef _osapi_network_ | ||
56 | #define _osapi_network_ | ||
57 | |||
58 | int32 OS_NetworkGetID (void); | ||
59 | int32 OS_NetworkGetHostName (char *host_name, uint32 name_len); | ||
60 | |||
61 | #endif | ||
diff --git a/lib/chibios/os/common/abstractions/nasa_cfe/osal/include/osapi-os-timer.h b/lib/chibios/os/common/abstractions/nasa_cfe/osal/include/osapi-os-timer.h new file mode 100644 index 000000000..8082c6227 --- /dev/null +++ b/lib/chibios/os/common/abstractions/nasa_cfe/osal/include/osapi-os-timer.h | |||
@@ -0,0 +1,68 @@ | |||
1 | /* | ||
2 | ** File: osapi-os-timer.h | ||
3 | ** | ||
4 | ** Copyright (c) 2004-2006, United States government as represented by the | ||
5 | ** administrator of the National Aeronautics Space Administration. | ||
6 | ** All rights reserved. This software was created at NASAs Goddard | ||
7 | ** Space Flight Center pursuant to government contracts. | ||
8 | ** | ||
9 | ** This is governed by the NASA Open Source Agreement and may be used, | ||
10 | ** distributed and modified only pursuant to the terms of that agreement. | ||
11 | ** | ||
12 | ** Author: Alan Cudmore - Code 582 | ||
13 | ** | ||
14 | ** Purpose: Contains functions prototype definitions and variable declarations | ||
15 | ** for the OS Abstraction Layer, Timer API | ||
16 | ** | ||
17 | ** $Revision: 1.5 $ | ||
18 | ** | ||
19 | ** $Date: 2013/07/25 10:02:20GMT-05:00 $ | ||
20 | ** | ||
21 | ** $Log: osapi-os-timer.h $ | ||
22 | ** Revision 1.5 2013/07/25 10:02:20GMT-05:00 acudmore | ||
23 | ** removed circular include "osapi.h" | ||
24 | ** Revision 1.4 2010/11/12 12:00:19GMT-05:00 acudmore | ||
25 | ** replaced copyright character with (c) and added open source notice where needed. | ||
26 | ** Revision 1.3 2010/02/01 12:38:34EST acudmore | ||
27 | ** Added return code to OS_TimerAPIInit | ||
28 | ** Revision 1.2 2008/08/26 13:52:52EDT apcudmore | ||
29 | ** removed linux specific define | ||
30 | ** Revision 1.1 2008/08/20 16:12:07EDT apcudmore | ||
31 | ** Initial revision | ||
32 | ** Member added to project c:/MKSDATA/MKS-REPOSITORY/MKS-OSAL-REPOSITORY/src/os/inc/project.pj | ||
33 | ** | ||
34 | ** | ||
35 | */ | ||
36 | |||
37 | #ifndef _osapi_timer_ | ||
38 | #define _osapi_timer_ | ||
39 | |||
40 | /* | ||
41 | ** Typedefs | ||
42 | */ | ||
43 | typedef void (*OS_TimerCallback_t)(uint32 timer_id); | ||
44 | |||
45 | typedef struct | ||
46 | { | ||
47 | char name[OS_MAX_API_NAME]; | ||
48 | uint32 creator; | ||
49 | uint32 start_time; | ||
50 | uint32 interval_time; | ||
51 | uint32 accuracy; | ||
52 | |||
53 | } OS_timer_prop_t; | ||
54 | |||
55 | |||
56 | /* | ||
57 | ** Timer API | ||
58 | */ | ||
59 | int32 OS_TimerAPIInit (void); | ||
60 | |||
61 | int32 OS_TimerCreate (uint32 *timer_id, const char *timer_name, uint32 *clock_accuracy, OS_TimerCallback_t callback_ptr); | ||
62 | int32 OS_TimerSet (uint32 timer_id, uint32 start_msec, uint32 interval_msec); | ||
63 | int32 OS_TimerDelete (uint32 timer_id); | ||
64 | |||
65 | int32 OS_TimerGetIdByName (uint32 *timer_id, const char *timer_name); | ||
66 | int32 OS_TimerGetInfo (uint32 timer_id, OS_timer_prop_t *timer_prop); | ||
67 | |||
68 | #endif | ||
diff --git a/lib/chibios/os/common/abstractions/nasa_cfe/osal/include/osapi-version.h b/lib/chibios/os/common/abstractions/nasa_cfe/osal/include/osapi-version.h new file mode 100644 index 000000000..331e96c94 --- /dev/null +++ b/lib/chibios/os/common/abstractions/nasa_cfe/osal/include/osapi-version.h | |||
@@ -0,0 +1,48 @@ | |||
1 | /************************************************************************ | ||
2 | ** File: | ||
3 | ** $Id: osapi-version.h 1.11 2014/05/02 13:53:14GMT-05:00 acudmore Exp $ | ||
4 | ** | ||
5 | ** Copyright (c) 2004-2006, United States government as represented by the | ||
6 | ** administrator of the National Aeronautics Space Administration. | ||
7 | ** All rights reserved. This software was created at NASAs Goddard | ||
8 | ** Space Flight Center pursuant to government contracts. | ||
9 | ** | ||
10 | ** This is governed by the NASA Open Source Agreement and may be used, | ||
11 | ** distributed and modified only pursuant to the terms of that agreement. | ||
12 | ** | ||
13 | ** Purpose: | ||
14 | ** The OSAL version numbers | ||
15 | ** | ||
16 | ** Notes: | ||
17 | ** | ||
18 | ** $Log: osapi-version.h $ | ||
19 | ** Revision 1.11 2014/05/02 13:53:14GMT-05:00 acudmore | ||
20 | ** Updated version to 4.1.1 | ||
21 | ** Revision 1.10 2014/01/23 16:33:31GMT-05:00 acudmore | ||
22 | ** Update for 4.1 release | ||
23 | ** Revision 1.9 2013/01/16 14:35:18GMT-05:00 acudmore | ||
24 | ** updated version label | ||
25 | ** Revision 1.8 2012/04/16 14:57:04GMT-05:00 acudmore | ||
26 | ** Updated version label to 3.5.0.0 | ||
27 | ** Revision 1.7 2012/01/17 16:04:29EST acudmore | ||
28 | ** Updated version to 3.4.1 | ||
29 | ** Revision 1.6 2011/12/05 15:45:16EST acudmore | ||
30 | ** Updated version label to 3.4.0 | ||
31 | ** Initial revision | ||
32 | ** Member added to project c:/MKSDATA/MKS-REPOSITORY/MKS-OSAL-REPOSITORY/src/os/inc/project.pj | ||
33 | ** | ||
34 | *************************************************************************/ | ||
35 | #ifndef _osapi_version_h_ | ||
36 | #define _osapi_version_h_ | ||
37 | |||
38 | #define OS_MAJOR_VERSION (4) | ||
39 | #define OS_MINOR_VERSION (1) | ||
40 | #define OS_REVISION (1) | ||
41 | #define OS_MISSION_REV (0) | ||
42 | |||
43 | |||
44 | #endif /* _osapi_version_h_ */ | ||
45 | |||
46 | /************************/ | ||
47 | /* End of File Comment */ | ||
48 | /************************/ | ||
diff --git a/lib/chibios/os/common/abstractions/nasa_cfe/osal/include/osapi.h b/lib/chibios/os/common/abstractions/nasa_cfe/osal/include/osapi.h new file mode 100644 index 000000000..72a7c340a --- /dev/null +++ b/lib/chibios/os/common/abstractions/nasa_cfe/osal/include/osapi.h | |||
@@ -0,0 +1,143 @@ | |||
1 | /* | ||
2 | ** File: osapi.h | ||
3 | ** | ||
4 | ** Copyright (c) 2004-2006, United States government as represented by the | ||
5 | ** administrator of the National Aeronautics Space Administration. | ||
6 | ** All rights reserved. This software was created at NASAs Goddard | ||
7 | ** Space Flight Center pursuant to government contracts. | ||
8 | ** | ||
9 | ** This is governed by the NASA Open Source Agreement and may be used, | ||
10 | ** distributed and modified only pursuant to the terms of that agreement. | ||
11 | ** | ||
12 | ** Author: Alan Cudmore - Code 582 | ||
13 | ** | ||
14 | ** Purpose: Contains functions prototype definitions and variables declarations | ||
15 | ** for the OS Abstraction Layer, Core OS module | ||
16 | ** | ||
17 | ** $Revision: 1.10 $ | ||
18 | ** | ||
19 | ** $Date: 2013/07/25 10:01:32GMT-05:00 $ | ||
20 | ** | ||
21 | ** $Log: osapi.h $ | ||
22 | ** Revision 1.10 2013/07/25 10:01:32GMT-05:00 acudmore | ||
23 | ** Added C++ support | ||
24 | ** Revision 1.9 2010/11/12 12:00:17GMT-05:00 acudmore | ||
25 | ** replaced copyright character with (c) and added open source notice where needed. | ||
26 | ** Revision 1.8 2010/03/08 15:57:20EST acudmore | ||
27 | ** include new OSAL version header file | ||
28 | ** Revision 1.7 2009/08/10 14:01:10EDT acudmore | ||
29 | ** Reset OSAL version for trunk | ||
30 | ** Revision 1.6 2009/08/10 13:55:49EDT acudmore | ||
31 | ** Updated OSAL version defines to 3.0 | ||
32 | ** Revision 1.5 2009/06/10 14:15:55EDT acudmore | ||
33 | ** Removed HAL include files. HAL code was removed from OSAL. | ||
34 | ** Revision 1.4 2008/08/20 16:12:51EDT apcudmore | ||
35 | ** Updated timer error codes | ||
36 | ** Revision 1.3 2008/08/20 15:46:27EDT apcudmore | ||
37 | ** Add support for timer API | ||
38 | ** Revision 1.2 2008/06/20 15:13:43EDT apcudmore | ||
39 | ** Checked in new Module loader/symbol table functionality | ||
40 | ** Revision 1.1 2008/04/20 22:36:02EDT ruperera | ||
41 | ** Initial revision | ||
42 | ** Member added to project c:/MKSDATA/MKS-REPOSITORY/MKS-OSAL-REPOSITORY/src/os/inc/project.pj | ||
43 | ** Revision 1.6 2008/02/14 11:29:10EST apcudmore | ||
44 | ** Updated version define ( 2.11 ) | ||
45 | ** Revision 1.5 2008/02/07 11:31:58EST apcudmore | ||
46 | ** Fixed merge problem | ||
47 | ** Revision 1.4 2008/02/07 11:07:29EST apcudmore | ||
48 | ** Added dynamic loader / Symbol lookup API | ||
49 | ** -- API only, next release will have functionality | ||
50 | ** Revision 1.2 2008/01/29 14:30:49EST njyanchik | ||
51 | ** I added code to all the ports that allow the values of both binary and counting semaphores to be | ||
52 | ** gotten through the OS_*SemGetInfo API. | ||
53 | ** Revision 1.1 2007/10/16 16:14:52EDT apcudmore | ||
54 | ** Initial revision | ||
55 | ** Member added to project d:/mksdata/MKS-OSAL-REPOSITORY/src/os/inc/project.pj | ||
56 | ** Revision 1.2 2007/09/28 15:46:49EDT rjmcgraw | ||
57 | ** Updated version numbers to 5.0 | ||
58 | ** Revision 1.1 2007/08/24 13:43:25EDT apcudmore | ||
59 | ** Initial revision | ||
60 | ** Member added to project d:/mksdata/MKS-CFE-PROJECT/fsw/cfe-core/os/inc/project.pj | ||
61 | ** Revision 1.9.1.1 2007/05/21 08:58:51EDT njyanchik | ||
62 | ** The trunk version number has been updated to version 0.0 | ||
63 | ** Revision 1.9 2006/06/12 10:20:07EDT rjmcgraw | ||
64 | ** Updated OS_MINOR_VERSION from 3 to 4 | ||
65 | ** Revision 1.8 2006/02/03 09:30:45EST njyanchik | ||
66 | ** Changed version number to 2.3 | ||
67 | ** Revision 1.7 2006/01/20 11:56:16EST njyanchik | ||
68 | ** Fixed header file information to match api document | ||
69 | ** Revision 1.15 2005/11/09 13:35:49 nyanchik | ||
70 | ** Revisions for 2.2 include: | ||
71 | ** a new scheduler mapper for Linux and OS X | ||
72 | ** addition of OS_printf function | ||
73 | ** fixed issues that would cause warnings at compile time | ||
74 | ** | ||
75 | ** | ||
76 | */ | ||
77 | |||
78 | #ifndef _osapi_ | ||
79 | #define _osapi_ | ||
80 | |||
81 | #include "common_types.h" | ||
82 | |||
83 | #ifdef __cplusplus | ||
84 | extern "C" { | ||
85 | #endif | ||
86 | |||
87 | #define OS_SUCCESS (0) | ||
88 | #define OS_ERROR (-1) | ||
89 | #define OS_INVALID_POINTER (-2) | ||
90 | #define OS_ERROR_ADDRESS_MISALIGNED (-3) | ||
91 | #define OS_ERROR_TIMEOUT (-4) | ||
92 | #define OS_INVALID_INT_NUM (-5) | ||
93 | #define OS_SEM_FAILURE (-6) | ||
94 | #define OS_SEM_TIMEOUT (-7) | ||
95 | #define OS_QUEUE_EMPTY (-8) | ||
96 | #define OS_QUEUE_FULL (-9) | ||
97 | #define OS_QUEUE_TIMEOUT (-10) | ||
98 | #define OS_QUEUE_INVALID_SIZE (-11) | ||
99 | #define OS_QUEUE_ID_ERROR (-12) | ||
100 | #define OS_ERR_NAME_TOO_LONG (-13) | ||
101 | #define OS_ERR_NO_FREE_IDS (-14) | ||
102 | #define OS_ERR_NAME_TAKEN (-15) | ||
103 | #define OS_ERR_INVALID_ID (-16) | ||
104 | #define OS_ERR_NAME_NOT_FOUND (-17) | ||
105 | #define OS_ERR_SEM_NOT_FULL (-18) | ||
106 | #define OS_ERR_INVALID_PRIORITY (-19) | ||
107 | #define OS_INVALID_SEM_VALUE (-20) | ||
108 | #define OS_ERR_FILE (-27) | ||
109 | #define OS_ERR_NOT_IMPLEMENTED (-28) | ||
110 | #define OS_TIMER_ERR_INVALID_ARGS (-29) | ||
111 | #define OS_TIMER_ERR_TIMER_ID (-30) | ||
112 | #define OS_TIMER_ERR_UNAVAILABLE (-31) | ||
113 | #define OS_TIMER_ERR_INTERNAL (-32) | ||
114 | |||
115 | /* | ||
116 | ** Defines for Queue Timeout parameters | ||
117 | */ | ||
118 | #define OS_PEND (0) | ||
119 | #define OS_CHECK (-1) | ||
120 | |||
121 | #include "osapi-version.h" | ||
122 | |||
123 | /* | ||
124 | ** Include the configuration file | ||
125 | */ | ||
126 | #include "osconfig.h" | ||
127 | |||
128 | /* | ||
129 | ** Include the OS API modules | ||
130 | */ | ||
131 | #include "osapi-os-core.h" | ||
132 | //#include "osapi-os-filesys.h" | ||
133 | //#include "osapi-os-net.h" | ||
134 | //#include "osapi-os-loader.h" | ||
135 | #include "osapi-os-timer.h" | ||
136 | #include "osapi-os-custom.h" | ||
137 | |||
138 | #ifdef __cplusplus | ||
139 | } | ||
140 | #endif | ||
141 | |||
142 | #endif | ||
143 | |||
diff --git a/lib/chibios/os/common/abstractions/nasa_cfe/osal/src/osapi.c b/lib/chibios/os/common/abstractions/nasa_cfe/osal/src/osapi.c new file mode 100644 index 000000000..ea0ac535b --- /dev/null +++ b/lib/chibios/os/common/abstractions/nasa_cfe/osal/src/osapi.c | |||
@@ -0,0 +1,2281 @@ | |||
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 osapi.c | ||
19 | * @brief OS API module code. | ||
20 | * | ||
21 | * @addtogroup nasa_osapi | ||
22 | * @{ | ||
23 | */ | ||
24 | |||
25 | #include <stdarg.h> | ||
26 | #include <string.h> | ||
27 | |||
28 | #include "ch.h" | ||
29 | |||
30 | #include "common_types.h" | ||
31 | #include "osapi.h" | ||
32 | |||
33 | #if CH_CFG_ST_FREQUENCY > 1000000 | ||
34 | #error "CH_CFG_ST_FREQUENCY limit is 1000000" | ||
35 | #endif | ||
36 | |||
37 | #if (CH_CFG_ST_FREQUENCY % 1000) != 0 | ||
38 | #error "CH_CFG_ST_FREQUENCY is not a multiple of 1000" | ||
39 | #endif | ||
40 | |||
41 | #if CH_CFG_USE_REGISTRY == FALSE | ||
42 | #error "NASA OSAL requires CH_CFG_USE_REGISTRY" | ||
43 | #endif | ||
44 | |||
45 | #if CH_CFG_USE_EVENTS == FALSE | ||
46 | #error "NASA OSAL requires CH_CFG_USE_EVENTS" | ||
47 | #endif | ||
48 | |||
49 | #if CH_CFG_USE_MUTEXES == FALSE | ||
50 | #error "NASA OSAL requires CH_CFG_USE_MUTEXES" | ||
51 | #endif | ||
52 | |||
53 | #if CH_CFG_USE_SEMAPHORES == FALSE | ||
54 | #error "NASA OSAL requires CH_CFG_USE_SEMAPHORES" | ||
55 | #endif | ||
56 | |||
57 | #if CH_CFG_USE_MEMCORE == FALSE | ||
58 | #error "NASA OSAL requires CH_CFG_USE_MEMCORE" | ||
59 | #endif | ||
60 | |||
61 | #if CH_CFG_USE_MEMPOOLS == FALSE | ||
62 | #error "NASA OSAL requires CH_CFG_USE_MEMPOOLS" | ||
63 | #endif | ||
64 | |||
65 | #if CH_CFG_USE_HEAP == FALSE | ||
66 | #error "NASA OSAL requires CH_CFG_USE_HEAP" | ||
67 | #endif | ||
68 | |||
69 | /*===========================================================================*/ | ||
70 | /* Module local definitions. */ | ||
71 | /*===========================================================================*/ | ||
72 | |||
73 | #define MIN_PRIORITY 1 | ||
74 | #define MAX_PRIORITY 255 | ||
75 | |||
76 | #define MIN_MESSAGE_SIZE 4 | ||
77 | #define MAX_MESSAGE_SIZE 16384 | ||
78 | |||
79 | #define MIN_QUEUE_DEPTH 1 | ||
80 | #define MAX_QUEUE_DEPTH 16384 | ||
81 | |||
82 | /*===========================================================================*/ | ||
83 | /* Module exported variables. */ | ||
84 | /*===========================================================================*/ | ||
85 | |||
86 | /*===========================================================================*/ | ||
87 | /* Module local types. */ | ||
88 | /*===========================================================================*/ | ||
89 | |||
90 | /** | ||
91 | * @brief Generic function pointer type. | ||
92 | */ | ||
93 | typedef void (*funcptr_t)(void); | ||
94 | |||
95 | /** | ||
96 | * @brief Type of OSAL timer. | ||
97 | */ | ||
98 | typedef struct { | ||
99 | uint32 is_free; | ||
100 | char name[OS_MAX_API_NAME]; | ||
101 | OS_TimerCallback_t callback_ptr; | ||
102 | uint32 start_time; | ||
103 | uint32 interval_time; | ||
104 | virtual_timer_t vt; | ||
105 | } osal_timer_t; | ||
106 | |||
107 | /** | ||
108 | * @brief Type of an OSAL queue. | ||
109 | */ | ||
110 | typedef struct { | ||
111 | uint32 is_free; | ||
112 | char name[OS_MAX_API_NAME]; | ||
113 | semaphore_t free_msgs; | ||
114 | memory_pool_t messages; | ||
115 | mailbox_t mb; | ||
116 | msg_t *mb_buffer; | ||
117 | void *q_buffer; | ||
118 | uint32 depth; | ||
119 | uint32 size; | ||
120 | } osal_queue_t; | ||
121 | |||
122 | /** | ||
123 | * @brief Type of an osal message with minimum size. | ||
124 | */ | ||
125 | typedef struct { | ||
126 | size_t size; | ||
127 | char buf[4]; | ||
128 | } osal_message_t; | ||
129 | |||
130 | /** | ||
131 | * @brief Type of OSAL main structure. | ||
132 | */ | ||
133 | typedef struct { | ||
134 | bool printf_enabled; | ||
135 | int (*printf)(const char *fmt, ...); | ||
136 | virtual_timer_t vt; | ||
137 | OS_time_t localtime; | ||
138 | memory_pool_t timers_pool; | ||
139 | memory_pool_t queues_pool; | ||
140 | memory_pool_t binary_semaphores_pool; | ||
141 | memory_pool_t count_semaphores_pool; | ||
142 | memory_pool_t mutexes_pool; | ||
143 | osal_timer_t timers[OS_MAX_TIMERS]; | ||
144 | osal_queue_t queues[OS_MAX_QUEUES]; | ||
145 | binary_semaphore_t binary_semaphores[OS_MAX_BIN_SEMAPHORES]; | ||
146 | semaphore_t count_semaphores[OS_MAX_COUNT_SEMAPHORES]; | ||
147 | mutex_t mutexes[OS_MAX_MUTEXES]; | ||
148 | } osal_t; | ||
149 | |||
150 | /*===========================================================================*/ | ||
151 | /* Module local variables. */ | ||
152 | /*===========================================================================*/ | ||
153 | |||
154 | static osal_t osal; | ||
155 | |||
156 | /*===========================================================================*/ | ||
157 | /* Module local functions. */ | ||
158 | /*===========================================================================*/ | ||
159 | |||
160 | /** | ||
161 | * @brief System time callback. | ||
162 | */ | ||
163 | static void systime_update(void *p) { | ||
164 | sysinterval_t delay = (sysinterval_t)p; | ||
165 | |||
166 | chSysLockFromISR(); | ||
167 | osal.localtime.microsecs += 1000; | ||
168 | if (osal.localtime.microsecs >= 1000000) { | ||
169 | osal.localtime.microsecs = 0; | ||
170 | osal.localtime.seconds++; | ||
171 | } | ||
172 | chVTDoSetI(&osal.vt, delay, systime_update, p); | ||
173 | chSysUnlockFromISR(); | ||
174 | } | ||
175 | |||
176 | /** | ||
177 | * @brief Virtual timers callback. | ||
178 | */ | ||
179 | static void timer_handler(void *p) { | ||
180 | osal_timer_t *otp = (osal_timer_t *)p; | ||
181 | |||
182 | /* Real callback.*/ | ||
183 | otp->callback_ptr((uint32)p); | ||
184 | |||
185 | /* Timer restart if an interval is defined.*/ | ||
186 | if (otp->interval_time != 0) { | ||
187 | chSysLockFromISR(); | ||
188 | chVTSetI(&otp->vt, TIME_US2I(otp->interval_time), timer_handler, p); | ||
189 | chSysUnlockFromISR(); | ||
190 | } | ||
191 | } | ||
192 | |||
193 | /** | ||
194 | * @brief Finds a queue by name. | ||
195 | */ | ||
196 | uint32 queue_find(const char *queue_name) { | ||
197 | osal_queue_t *oqp; | ||
198 | |||
199 | /* Searching the queue in the table.*/ | ||
200 | for (oqp = &osal.queues[0]; oqp < &osal.queues[OS_MAX_QUEUES]; oqp++) { | ||
201 | /* Entering a reentrant critical zone.*/ | ||
202 | syssts_t sts = chSysGetStatusAndLockX(); | ||
203 | |||
204 | if (!oqp->is_free && | ||
205 | (strncmp(oqp->name, queue_name, OS_MAX_API_NAME - 1) == 0)) { | ||
206 | /* Leaving the critical zone.*/ | ||
207 | chSysRestoreStatusX(sts); | ||
208 | return (uint32)oqp; | ||
209 | } | ||
210 | |||
211 | /* Leaving the critical zone.*/ | ||
212 | chSysRestoreStatusX(sts); | ||
213 | } | ||
214 | |||
215 | return 0; | ||
216 | } | ||
217 | |||
218 | /** | ||
219 | * @brief Finds a timer by name. | ||
220 | */ | ||
221 | uint32 timer_find(const char *timer_name) { | ||
222 | osal_timer_t *otp; | ||
223 | |||
224 | /* Searching the queue in the table.*/ | ||
225 | for (otp = &osal.timers[0]; otp < &osal.timers[OS_MAX_TIMERS]; otp++) { | ||
226 | /* Entering a reentrant critical zone.*/ | ||
227 | syssts_t sts = chSysGetStatusAndLockX(); | ||
228 | |||
229 | if (!otp->is_free && | ||
230 | (strncmp(otp->name, timer_name, OS_MAX_API_NAME - 1) == 0)) { | ||
231 | /* Leaving the critical zone.*/ | ||
232 | chSysRestoreStatusX(sts); | ||
233 | return (uint32)otp; | ||
234 | } | ||
235 | |||
236 | /* Leaving the critical zone.*/ | ||
237 | chSysRestoreStatusX(sts); | ||
238 | } | ||
239 | |||
240 | return 0; | ||
241 | } | ||
242 | |||
243 | /*===========================================================================*/ | ||
244 | /* Module exported functions. */ | ||
245 | /*===========================================================================*/ | ||
246 | |||
247 | /*-- Initialization API -----------------------------------------------------*/ | ||
248 | |||
249 | /** | ||
250 | * @brief OS initialization. | ||
251 | * @details This function returns initializes the internal data structures | ||
252 | * of the OS Abstraction Layer. It must be called in the application | ||
253 | * startup code before calling any other OS routines. | ||
254 | * | ||
255 | * @return An error code. | ||
256 | * | ||
257 | * @api | ||
258 | */ | ||
259 | int32 OS_API_Init(void) { | ||
260 | |||
261 | chSysInit(); | ||
262 | |||
263 | /* OS_printf() initially disabled.*/ | ||
264 | osal.printf_enabled = false; | ||
265 | osal.printf = NULL; | ||
266 | |||
267 | /* System time handling.*/ | ||
268 | osal.localtime.microsecs = 0; | ||
269 | osal.localtime.seconds = 0; | ||
270 | chVTObjectInit(&osal.vt); | ||
271 | chVTSet(&osal.vt, TIME_MS2I(1), systime_update, (void *)TIME_MS2I(1)); | ||
272 | |||
273 | /* Timers pool initialization.*/ | ||
274 | chPoolObjectInit(&osal.timers_pool, | ||
275 | sizeof (osal_timer_t), | ||
276 | NULL); | ||
277 | chPoolLoadArray(&osal.timers_pool, | ||
278 | &osal.timers[0], | ||
279 | OS_MAX_TIMERS); | ||
280 | |||
281 | /* Queues pool initialization.*/ | ||
282 | chPoolObjectInit(&osal.queues_pool, | ||
283 | sizeof (osal_queue_t), | ||
284 | NULL); | ||
285 | chPoolLoadArray(&osal.queues_pool, | ||
286 | &osal.queues[0], | ||
287 | OS_MAX_QUEUES); | ||
288 | |||
289 | /* Binary Semaphores pool initialization.*/ | ||
290 | chPoolObjectInit(&osal.binary_semaphores_pool, | ||
291 | sizeof (binary_semaphore_t), | ||
292 | NULL); | ||
293 | chPoolLoadArray(&osal.binary_semaphores_pool, | ||
294 | &osal.binary_semaphores[0], | ||
295 | OS_MAX_BIN_SEMAPHORES); | ||
296 | |||
297 | /* Counter Semaphores pool initialization.*/ | ||
298 | chPoolObjectInit(&osal.count_semaphores_pool, | ||
299 | sizeof (semaphore_t), | ||
300 | NULL); | ||
301 | chPoolLoadArray(&osal.count_semaphores_pool, | ||
302 | &osal.count_semaphores[0], | ||
303 | OS_MAX_COUNT_SEMAPHORES); | ||
304 | |||
305 | /* Mutexes pool initialization.*/ | ||
306 | chPoolObjectInit(&osal.mutexes_pool, | ||
307 | sizeof (mutex_t), | ||
308 | NULL); | ||
309 | chPoolLoadArray(&osal.mutexes_pool, | ||
310 | &osal.mutexes[0], | ||
311 | OS_MAX_MUTEXES); | ||
312 | |||
313 | return OS_SUCCESS; | ||
314 | } | ||
315 | |||
316 | /*-- Various API -----------------------------------------------------------*/ | ||
317 | |||
318 | /** | ||
319 | * @brief OS printf-like function. | ||
320 | * @note It is initially disabled. | ||
321 | * | ||
322 | * @param[in] string formatter string | ||
323 | * | ||
324 | * @api | ||
325 | */ | ||
326 | void OS_printf(const char *string, ...) { | ||
327 | va_list ap; | ||
328 | |||
329 | if (osal.printf_enabled && (osal.printf != NULL)) { | ||
330 | va_start(ap, string); | ||
331 | (void) osal.printf(string); | ||
332 | va_end(ap); | ||
333 | } | ||
334 | } | ||
335 | |||
336 | /** | ||
337 | * @brief Disables @p OS_printf(). | ||
338 | * | ||
339 | * @api | ||
340 | */ | ||
341 | void OS_printf_disable(void) { | ||
342 | |||
343 | osal.printf_enabled = false; | ||
344 | } | ||
345 | |||
346 | /** | ||
347 | * @brief Enables @p OS_printf(). | ||
348 | * | ||
349 | * @api | ||
350 | */ | ||
351 | void OS_printf_enable(void) { | ||
352 | |||
353 | osal.printf_enabled = true; | ||
354 | } | ||
355 | |||
356 | /** | ||
357 | * @brief Sets the system printf function. | ||
358 | * @note By default the printf function is not defined. | ||
359 | * @note This is a ChibiOS/RT extension. | ||
360 | * | ||
361 | * @param[in] printf pointer to a @p printf() like function | ||
362 | * | ||
363 | * @api | ||
364 | */ | ||
365 | void OS_set_printf(int (*printf)(const char *fmt, ...)) { | ||
366 | |||
367 | osal.printf = printf; | ||
368 | } | ||
369 | |||
370 | /** | ||
371 | * @brief System tick period in microseconds. | ||
372 | * | ||
373 | * @return The system tick period. | ||
374 | */ | ||
375 | int32 OS_Tick2Micros(void) { | ||
376 | |||
377 | return 1000000 / CH_CFG_ST_FREQUENCY; | ||
378 | } | ||
379 | |||
380 | /** | ||
381 | * @brief Returns the local time. | ||
382 | * | ||
383 | * @param[out] time_struct the system time | ||
384 | * @return An error code. | ||
385 | * | ||
386 | * @api | ||
387 | */ | ||
388 | int32 OS_GetLocalTime(OS_time_t *time_struct) { | ||
389 | |||
390 | if (time_struct == NULL) { | ||
391 | return OS_INVALID_POINTER; | ||
392 | } | ||
393 | |||
394 | chSysLock(); | ||
395 | *time_struct = osal.localtime; | ||
396 | chSysUnlock(); | ||
397 | |||
398 | return OS_SUCCESS; | ||
399 | } | ||
400 | |||
401 | /** | ||
402 | * @brief Changes the local time. | ||
403 | * | ||
404 | * @param[in] time_struct the system time | ||
405 | * @return An error code. | ||
406 | * | ||
407 | * @api | ||
408 | */ | ||
409 | int32 OS_SetLocalTime(OS_time_t *time_struct) { | ||
410 | |||
411 | if (time_struct == NULL) { | ||
412 | return OS_INVALID_POINTER; | ||
413 | } | ||
414 | |||
415 | chSysLock(); | ||
416 | osal.localtime = *time_struct; | ||
417 | chSysUnlock(); | ||
418 | |||
419 | return OS_SUCCESS; | ||
420 | } | ||
421 | |||
422 | /** | ||
423 | * @brief Conversion from milliseconds to ticks. | ||
424 | * | ||
425 | * @param[in] milli_seconds the time in milliseconds | ||
426 | * @return The system ticks. | ||
427 | * | ||
428 | * @api | ||
429 | */ | ||
430 | int32 OS_Milli2Ticks(uint32 milli_seconds) { | ||
431 | |||
432 | return (int32)TIME_MS2I(milli_seconds); | ||
433 | } | ||
434 | |||
435 | /*-- timers API -------------------------------------------------------------*/ | ||
436 | |||
437 | /** | ||
438 | * @brief Timer creation. | ||
439 | * | ||
440 | * @param[out] timer_id pointer to a timer id variable | ||
441 | * @param[in] timer_name the timer name | ||
442 | * @param[out] clock_accuracy timer accuracy in microseconds | ||
443 | * @param[in] callback_ptr timer callback | ||
444 | * @return An error code. | ||
445 | * | ||
446 | * @api | ||
447 | */ | ||
448 | int32 OS_TimerCreate(uint32 *timer_id, const char *timer_name, | ||
449 | uint32 *clock_accuracy, OS_TimerCallback_t callback_ptr) { | ||
450 | osal_timer_t *otp; | ||
451 | |||
452 | /* NULL pointer checks.*/ | ||
453 | if ((timer_id == NULL) || (timer_name == NULL) || | ||
454 | (clock_accuracy == NULL)) { | ||
455 | return OS_INVALID_POINTER; | ||
456 | } | ||
457 | |||
458 | /* NULL callback check.*/ | ||
459 | if (callback_ptr == NULL) { | ||
460 | *timer_id = 0; | ||
461 | return OS_TIMER_ERR_INVALID_ARGS; | ||
462 | } | ||
463 | |||
464 | /* Checking timer name length.*/ | ||
465 | if (strlen(timer_name) >= OS_MAX_API_NAME) { | ||
466 | *timer_id = 0; | ||
467 | return OS_ERR_NAME_TOO_LONG; | ||
468 | } | ||
469 | |||
470 | /* Checking if the name is already taken.*/ | ||
471 | if (timer_find(timer_name) > 0) { | ||
472 | *timer_id = 0; | ||
473 | return OS_ERR_NAME_TAKEN; | ||
474 | } | ||
475 | |||
476 | /* Getting object.*/ | ||
477 | otp = chPoolAlloc(&osal.timers_pool); | ||
478 | if (otp == NULL) { | ||
479 | *timer_id = 0; | ||
480 | return OS_ERR_NO_FREE_IDS; | ||
481 | } | ||
482 | |||
483 | strncpy(otp->name, timer_name, OS_MAX_API_NAME - 1); | ||
484 | chVTObjectInit(&otp->vt); | ||
485 | otp->start_time = 0; | ||
486 | otp->interval_time = 0; | ||
487 | otp->callback_ptr = callback_ptr; | ||
488 | otp->is_free = 0; /* Note, last.*/ | ||
489 | |||
490 | *timer_id = (uint32)otp; | ||
491 | *clock_accuracy = (uint32)(1000000 / CH_CFG_ST_FREQUENCY); | ||
492 | |||
493 | return OS_SUCCESS; | ||
494 | } | ||
495 | |||
496 | /** | ||
497 | * @brief Timer deletion. | ||
498 | * | ||
499 | * @param[in] timer_id timer id variable | ||
500 | * @return An error code. | ||
501 | * | ||
502 | * @api | ||
503 | */ | ||
504 | int32 OS_TimerDelete(uint32 timer_id) { | ||
505 | osal_timer_t *otp = (osal_timer_t *)timer_id; | ||
506 | |||
507 | /* Range check.*/ | ||
508 | if ((otp < &osal.timers[0]) || | ||
509 | (otp >= &osal.timers[OS_MAX_TIMERS]) || | ||
510 | (otp->is_free)) { | ||
511 | return OS_ERR_INVALID_ID; | ||
512 | } | ||
513 | |||
514 | chSysLock(); | ||
515 | |||
516 | /* Marking as no more free, will be overwritten by the pool pointer.*/ | ||
517 | otp->is_free = 1; | ||
518 | |||
519 | /* Resetting the timer.*/ | ||
520 | chVTResetI(&otp->vt); | ||
521 | otp->start_time = 0; | ||
522 | otp->interval_time = 0; | ||
523 | |||
524 | /* Flagging it as unused and returning it to the pool.*/ | ||
525 | chPoolFreeI(&osal.timers_pool, (void *)otp); | ||
526 | |||
527 | chSysUnlock(); | ||
528 | |||
529 | return OS_SUCCESS; | ||
530 | } | ||
531 | |||
532 | /** | ||
533 | * @brief Timer deletion. | ||
534 | * @note This function can be safely called from timer callbacks or ISRs. | ||
535 | * | ||
536 | * @param[in] timer_id timer id variable | ||
537 | * @param[in] start_time start time in microseconds or zero | ||
538 | * @param[in] interval_time interval time in microseconds or zero | ||
539 | * @return An error code. | ||
540 | * | ||
541 | * @api | ||
542 | */ | ||
543 | int32 OS_TimerSet(uint32 timer_id, uint32 start_time, uint32 interval_time) { | ||
544 | syssts_t sts; | ||
545 | osal_timer_t *otp = (osal_timer_t *)timer_id; | ||
546 | |||
547 | /* Range check.*/ | ||
548 | if ((otp < &osal.timers[0]) || | ||
549 | (otp >= &osal.timers[OS_MAX_TIMERS]) || | ||
550 | (otp->is_free)) { | ||
551 | return OS_ERR_INVALID_ID; | ||
552 | } | ||
553 | |||
554 | /* Entering a reentrant critical zone.*/ | ||
555 | sts = chSysGetStatusAndLockX(); | ||
556 | |||
557 | if (start_time == 0) { | ||
558 | chVTResetI(&otp->vt); | ||
559 | } | ||
560 | else { | ||
561 | otp->start_time = start_time; | ||
562 | otp->interval_time = interval_time; | ||
563 | chVTSetI(&otp->vt, TIME_US2I(start_time), timer_handler, (void *)timer_id); | ||
564 | } | ||
565 | |||
566 | /* Leaving the critical zone.*/ | ||
567 | chSysRestoreStatusX(sts); | ||
568 | |||
569 | return OS_SUCCESS; | ||
570 | } | ||
571 | |||
572 | /** | ||
573 | * @brief Retrieves a timer id by name. | ||
574 | * | ||
575 | * @param[out] timer_id pointer to a timer id variable | ||
576 | * @param[in] sem_name the timer name | ||
577 | * @return An error code. | ||
578 | * | ||
579 | * @api | ||
580 | */ | ||
581 | int32 OS_TimerGetIdByName(uint32 *timer_id, const char *timer_name) { | ||
582 | |||
583 | /* NULL pointer checks.*/ | ||
584 | if ((timer_id == NULL) || (timer_name == NULL)) { | ||
585 | return OS_INVALID_POINTER; | ||
586 | } | ||
587 | |||
588 | /* Checking name length.*/ | ||
589 | if (strlen(timer_name) >= OS_MAX_API_NAME) { | ||
590 | return OS_ERR_NAME_TOO_LONG; | ||
591 | } | ||
592 | |||
593 | /* Searching the queue.*/ | ||
594 | *timer_id = timer_find(timer_name); | ||
595 | if (*timer_id > 0) { | ||
596 | return OS_SUCCESS; | ||
597 | } | ||
598 | |||
599 | return OS_ERR_NAME_NOT_FOUND; | ||
600 | } | ||
601 | |||
602 | /** | ||
603 | * @brief Returns timer information. | ||
604 | * @note This function can be safely called from timer callbacks or ISRs. | ||
605 | * | ||
606 | * @param[in] timer_id timer id variable | ||
607 | * @param[in] timer_prop timer properties | ||
608 | * @return An error code. | ||
609 | * | ||
610 | * @api | ||
611 | */ | ||
612 | int32 OS_TimerGetInfo(uint32 timer_id, OS_timer_prop_t *timer_prop) { | ||
613 | syssts_t sts; | ||
614 | osal_timer_t *otp = (osal_timer_t *)timer_id; | ||
615 | |||
616 | /* NULL pointer checks.*/ | ||
617 | if (timer_prop == NULL) { | ||
618 | return OS_INVALID_POINTER; | ||
619 | } | ||
620 | |||
621 | /* Range check.*/ | ||
622 | if ((otp < &osal.timers[0]) || | ||
623 | (otp >= &osal.timers[OS_MAX_TIMERS]) || | ||
624 | (otp->is_free)) { | ||
625 | return OS_ERR_INVALID_ID; | ||
626 | } | ||
627 | |||
628 | /* Entering a reentrant critical zone.*/ | ||
629 | sts = chSysGetStatusAndLockX(); | ||
630 | |||
631 | /* If the timer is not in use then error.*/ | ||
632 | if (otp->is_free) { | ||
633 | /* Leaving the critical zone.*/ | ||
634 | chSysRestoreStatusX(sts); | ||
635 | return OS_ERR_INVALID_ID; | ||
636 | } | ||
637 | |||
638 | strncpy(timer_prop->name, otp->name, OS_MAX_API_NAME - 1); | ||
639 | timer_prop->creator = (uint32)0; | ||
640 | timer_prop->start_time = otp->start_time; | ||
641 | timer_prop->interval_time = otp->interval_time; | ||
642 | timer_prop->accuracy = (uint32)(1000000 / CH_CFG_ST_FREQUENCY); | ||
643 | |||
644 | /* Leaving the critical zone.*/ | ||
645 | chSysRestoreStatusX(sts); | ||
646 | |||
647 | return OS_SUCCESS; | ||
648 | } | ||
649 | |||
650 | /*-- Queues API -------------------------------------------------------------*/ | ||
651 | |||
652 | /** | ||
653 | * @brief Queue creation. | ||
654 | * | ||
655 | * @param[out] queue_id pointer to a queue id variable | ||
656 | * @param[in] queue_name the queue name | ||
657 | * @param[in] queue_depth desired queue depth | ||
658 | * @param[in] data_size maximum message size | ||
659 | * @param[in] flags queue option flags | ||
660 | * @return An error code. | ||
661 | * | ||
662 | * @api | ||
663 | */ | ||
664 | int32 OS_QueueCreate(uint32 *queue_id, const char *queue_name, | ||
665 | uint32 queue_depth, uint32 data_size, uint32 flags) { | ||
666 | osal_queue_t *oqp; | ||
667 | size_t msgsize; | ||
668 | |||
669 | (void)flags; | ||
670 | |||
671 | /* NULL pointer checks.*/ | ||
672 | if ((queue_id == NULL) || (queue_name == NULL)) { | ||
673 | return OS_INVALID_POINTER; | ||
674 | } | ||
675 | |||
676 | /* Checking queue name length.*/ | ||
677 | if (strlen(queue_name) >= OS_MAX_API_NAME) { | ||
678 | *queue_id = 0; | ||
679 | return OS_ERR_NAME_TOO_LONG; | ||
680 | } | ||
681 | |||
682 | /* Checking if the name is already taken.*/ | ||
683 | if (queue_find(queue_name) > 0) { | ||
684 | *queue_id = 0; | ||
685 | return OS_ERR_NAME_TAKEN; | ||
686 | } | ||
687 | |||
688 | /* Checks on queue limits. There is no dedicated error code.*/ | ||
689 | if ((data_size < MIN_MESSAGE_SIZE) || (data_size > MAX_MESSAGE_SIZE) || | ||
690 | (queue_depth < MIN_QUEUE_DEPTH) || (queue_depth > MAX_QUEUE_DEPTH)) { | ||
691 | *queue_id = 0; | ||
692 | return OS_ERROR; | ||
693 | } | ||
694 | |||
695 | /* Getting object.*/ | ||
696 | oqp = chPoolAlloc(&osal.queues_pool); | ||
697 | if (oqp == NULL) { | ||
698 | *queue_id = 0; | ||
699 | return OS_ERR_NO_FREE_IDS; | ||
700 | } | ||
701 | |||
702 | /* Attempting messages buffer allocation.*/ | ||
703 | msgsize = MEM_ALIGN_NEXT(data_size + sizeof (size_t), PORT_NATURAL_ALIGN); | ||
704 | oqp->mb_buffer = chHeapAllocAligned(NULL, | ||
705 | msgsize * (size_t)queue_depth, | ||
706 | PORT_NATURAL_ALIGN); | ||
707 | if (oqp->mb_buffer == NULL) { | ||
708 | *queue_id = 0; | ||
709 | return OS_ERROR; | ||
710 | } | ||
711 | |||
712 | /* Attempting queue buffer allocation.*/ | ||
713 | oqp->q_buffer = chHeapAllocAligned(NULL, | ||
714 | sizeof (msg_t) * (size_t)queue_depth, | ||
715 | PORT_NATURAL_ALIGN); | ||
716 | if (oqp->q_buffer == NULL) { | ||
717 | *queue_id = 0; | ||
718 | chHeapFree(oqp->mb_buffer); | ||
719 | return OS_ERROR; | ||
720 | } | ||
721 | |||
722 | /* Initializing object static parts.*/ | ||
723 | strncpy(oqp->name, queue_name, OS_MAX_API_NAME - 1); | ||
724 | chMBObjectInit(&oqp->mb, oqp->q_buffer, (size_t)queue_depth); | ||
725 | chSemObjectInit(&oqp->free_msgs, (cnt_t)queue_depth); | ||
726 | chPoolObjectInit(&oqp->messages, msgsize, NULL); | ||
727 | chPoolLoadArray(&oqp->messages, oqp->mb_buffer, (size_t)queue_depth); | ||
728 | oqp->depth = queue_depth; | ||
729 | oqp->size = data_size; | ||
730 | oqp->is_free = 0; /* Note, last.*/ | ||
731 | *queue_id = (uint32)oqp; | ||
732 | |||
733 | return OS_SUCCESS; | ||
734 | } | ||
735 | |||
736 | /** | ||
737 | * @brief Queue deletion. | ||
738 | * | ||
739 | * @param[in] queue_id queue id variable | ||
740 | * @return An error code. | ||
741 | * | ||
742 | * @api | ||
743 | */ | ||
744 | int32 OS_QueueDelete(uint32 queue_id) { | ||
745 | osal_queue_t *oqp = (osal_queue_t *)queue_id; | ||
746 | void *q_buffer, *mb_buffer; | ||
747 | |||
748 | /* Range check.*/ | ||
749 | if ((oqp < &osal.queues[0]) || | ||
750 | (oqp >= &osal.queues[OS_MAX_QUEUES]) || | ||
751 | (oqp->is_free)) { | ||
752 | return OS_ERR_INVALID_ID; | ||
753 | } | ||
754 | |||
755 | /* Critical zone.*/ | ||
756 | chSysLock(); | ||
757 | |||
758 | /* Marking as no more free, will be overwritten by the pool pointer.*/ | ||
759 | oqp->is_free = 1; | ||
760 | |||
761 | /* Pointers to areas to be freed.*/ | ||
762 | q_buffer = oqp->q_buffer; | ||
763 | mb_buffer = oqp->mb_buffer; | ||
764 | |||
765 | /* Resetting the queue.*/ | ||
766 | chMBResetI(&oqp->mb); | ||
767 | chSemResetI(&oqp->free_msgs, 0); | ||
768 | |||
769 | /* Flagging it as unused and returning it to the pool.*/ | ||
770 | chPoolFreeI(&osal.queues_pool, (void *)oqp); | ||
771 | |||
772 | chSchRescheduleS(); | ||
773 | |||
774 | /* Leaving critical zone.*/ | ||
775 | chSysUnlock(); | ||
776 | |||
777 | /* Freeing buffers, outside critical zone, slow heap operation.*/ | ||
778 | chHeapFree(q_buffer); | ||
779 | chHeapFree(mb_buffer); | ||
780 | |||
781 | return OS_SUCCESS; | ||
782 | } | ||
783 | |||
784 | /** | ||
785 | * @brief Retrieves a message from the queue. | ||
786 | * | ||
787 | * @param[in] queue_id queue id variable | ||
788 | * @param[out] data message buffer pointer | ||
789 | * @param[in] size size of the buffer | ||
790 | * @param[out] size_copied size of the received message | ||
791 | * @param[in] timeout timeout in ticks, the special values @p OS_PEND | ||
792 | * and @p OS_CHECK can be specified | ||
793 | * @return An error code. | ||
794 | * | ||
795 | * @api | ||
796 | */ | ||
797 | int32 OS_QueueGet(uint32 queue_id, void *data, uint32 size, | ||
798 | uint32 *size_copied, int32 timeout) { | ||
799 | osal_queue_t *oqp = (osal_queue_t *)queue_id; | ||
800 | msg_t msg, msgsts; | ||
801 | void *body; | ||
802 | |||
803 | /* NULL pointer checks.*/ | ||
804 | if ((data == NULL) || (size_copied == NULL)) { | ||
805 | return OS_INVALID_POINTER; | ||
806 | } | ||
807 | |||
808 | /* Range check.*/ | ||
809 | if ((oqp < &osal.queues[0]) || | ||
810 | (oqp >= &osal.queues[OS_MAX_QUEUES]) || | ||
811 | (oqp->is_free)) { | ||
812 | return OS_ERR_INVALID_ID; | ||
813 | } | ||
814 | |||
815 | /* Check on minimum size.*/ | ||
816 | if (size < oqp->size) { | ||
817 | return OS_QUEUE_INVALID_SIZE; | ||
818 | } | ||
819 | |||
820 | /* Special time handling.*/ | ||
821 | if (timeout == OS_PEND) { | ||
822 | msgsts = chMBFetchTimeout(&oqp->mb, &msg, TIME_INFINITE); | ||
823 | if (msgsts < MSG_OK) { | ||
824 | *size_copied = 0; | ||
825 | return OS_ERROR; | ||
826 | } | ||
827 | } | ||
828 | else if (timeout == OS_CHECK) { | ||
829 | msgsts = chMBFetchTimeout(&oqp->mb, &msg, TIME_IMMEDIATE); | ||
830 | if (msgsts < MSG_OK) { | ||
831 | *size_copied = 0; | ||
832 | return OS_QUEUE_EMPTY; | ||
833 | } | ||
834 | } | ||
835 | else { | ||
836 | msgsts = chMBFetchTimeout(&oqp->mb, &msg, (sysinterval_t)timeout); | ||
837 | if (msgsts < MSG_OK) { | ||
838 | *size_copied = 0; | ||
839 | return OS_QUEUE_TIMEOUT; | ||
840 | } | ||
841 | } | ||
842 | |||
843 | /* Message body and size.*/ | ||
844 | *size_copied = ((osal_message_t *)msg)->size; | ||
845 | body = (void *)((osal_message_t *)msg)->buf; | ||
846 | |||
847 | /* Copying the message body.*/ | ||
848 | memcpy(data, body, *size_copied); | ||
849 | |||
850 | /* Freeing the message buffer.*/ | ||
851 | chPoolFree(&oqp->messages, (void *)msg); | ||
852 | chSemSignal(&oqp->free_msgs); | ||
853 | |||
854 | return OS_SUCCESS; | ||
855 | } | ||
856 | |||
857 | /** | ||
858 | * @brief Puts a message in the queue. | ||
859 | * | ||
860 | * @param[in] queue_id queue id variable | ||
861 | * @param[in] data message buffer pointer | ||
862 | * @param[in] size size of the message | ||
863 | * @param[in] flags operation flags | ||
864 | * @return An error code. | ||
865 | * | ||
866 | * @api | ||
867 | */ | ||
868 | int32 OS_QueuePut(uint32 queue_id, void *data, uint32 size, uint32 flags) { | ||
869 | osal_queue_t *oqp = (osal_queue_t *)queue_id; | ||
870 | msg_t msgsts; | ||
871 | osal_message_t *omsg; | ||
872 | |||
873 | (void)flags; | ||
874 | |||
875 | /* NULL pointer checks.*/ | ||
876 | if (data == NULL) { | ||
877 | return OS_INVALID_POINTER; | ||
878 | } | ||
879 | |||
880 | /* Range check.*/ | ||
881 | if ((oqp < &osal.queues[0]) || | ||
882 | (oqp >= &osal.queues[OS_MAX_QUEUES]) || | ||
883 | (oqp->is_free)) { | ||
884 | return OS_ERR_INVALID_ID; | ||
885 | } | ||
886 | |||
887 | /* Check on maximum size.*/ | ||
888 | if (size > oqp->size) { | ||
889 | return OS_QUEUE_INVALID_SIZE; | ||
890 | } | ||
891 | |||
892 | /* Getting a message buffer from the pool.*/ | ||
893 | msgsts = chSemWait(&oqp->free_msgs); | ||
894 | if (msgsts < MSG_OK) { | ||
895 | return OS_ERROR; | ||
896 | } | ||
897 | omsg = chPoolAlloc(&oqp->messages); | ||
898 | |||
899 | /* Filling message size and data.*/ | ||
900 | omsg->size = (size_t)size; | ||
901 | memcpy(omsg->buf, data, size); | ||
902 | |||
903 | /* Posting the message.*/ | ||
904 | msgsts = chMBPostTimeout(&oqp->mb, (msg_t)omsg, TIME_INFINITE); | ||
905 | if (msgsts < MSG_OK) { | ||
906 | return OS_ERROR; | ||
907 | } | ||
908 | |||
909 | return OS_SUCCESS; | ||
910 | } | ||
911 | |||
912 | /** | ||
913 | * @brief Retrieves a queue id by name. | ||
914 | * | ||
915 | * @param[out] queue_id pointer to a queue id variable | ||
916 | * @param[in] sem_name the queue name | ||
917 | * @return An error code. | ||
918 | * | ||
919 | * @api | ||
920 | */ | ||
921 | int32 OS_QueueGetIdByName(uint32 *queue_id, const char *queue_name) { | ||
922 | |||
923 | /* NULL pointer checks.*/ | ||
924 | if ((queue_id == NULL) || (queue_name == NULL)) { | ||
925 | return OS_INVALID_POINTER; | ||
926 | } | ||
927 | |||
928 | /* Checking name length.*/ | ||
929 | if (strlen(queue_name) >= OS_MAX_API_NAME) { | ||
930 | return OS_ERR_NAME_TOO_LONG; | ||
931 | } | ||
932 | |||
933 | /* Searching the queue.*/ | ||
934 | *queue_id = queue_find(queue_name); | ||
935 | if (*queue_id > 0) { | ||
936 | return OS_SUCCESS; | ||
937 | } | ||
938 | |||
939 | return OS_ERR_NAME_NOT_FOUND; | ||
940 | } | ||
941 | |||
942 | /** | ||
943 | * @brief Returns queue information. | ||
944 | * @note This function can be safely called from timer callbacks or ISRs. | ||
945 | * | ||
946 | * @param[in] queue_id queue id variable | ||
947 | * @param[in] queue_prop queue properties | ||
948 | * @return An error code. | ||
949 | * | ||
950 | * @api | ||
951 | */ | ||
952 | int32 OS_QueueGetInfo (uint32 queue_id, OS_queue_prop_t *queue_prop) { | ||
953 | osal_queue_t *oqp = (osal_queue_t *)queue_id; | ||
954 | syssts_t sts; | ||
955 | |||
956 | /* NULL pointer checks.*/ | ||
957 | if (queue_prop == NULL) { | ||
958 | return OS_INVALID_POINTER; | ||
959 | } | ||
960 | |||
961 | /* Range check.*/ | ||
962 | if ((oqp < &osal.queues[0]) || | ||
963 | (oqp >= &osal.queues[OS_MAX_QUEUES]) || | ||
964 | (oqp->is_free)) { | ||
965 | return OS_ERR_INVALID_ID; | ||
966 | } | ||
967 | |||
968 | /* Entering a reentrant critical zone.*/ | ||
969 | sts = chSysGetStatusAndLockX(); | ||
970 | |||
971 | /* If the queue is not in use then error.*/ | ||
972 | if (oqp->is_free) { | ||
973 | /* Leaving the critical zone.*/ | ||
974 | chSysRestoreStatusX(sts); | ||
975 | return OS_ERR_INVALID_ID; | ||
976 | } | ||
977 | |||
978 | strncpy(queue_prop->name, oqp->name, OS_MAX_API_NAME - 1); | ||
979 | queue_prop->creator = (uint32)0; | ||
980 | |||
981 | /* Leaving the critical zone.*/ | ||
982 | chSysRestoreStatusX(sts); | ||
983 | |||
984 | return OS_SUCCESS; | ||
985 | |||
986 | return OS_ERR_NOT_IMPLEMENTED; | ||
987 | } | ||
988 | |||
989 | /*-- Binary Semaphore API ---------------------------------------------------*/ | ||
990 | |||
991 | /** | ||
992 | * @brief Binary semaphore creation. | ||
993 | * | ||
994 | * @param[out] sem_id pointer to a binary semaphore id variable | ||
995 | * @param[in] sem_name the binary semaphore name | ||
996 | * @param[in] sem_initial_value semaphore initial value | ||
997 | * @param[in] options semaphore options | ||
998 | * @return An error code. | ||
999 | * | ||
1000 | * @api | ||
1001 | */ | ||
1002 | int32 OS_BinSemCreate(uint32 *sem_id, const char *sem_name, | ||
1003 | uint32 sem_initial_value, uint32 options) { | ||
1004 | binary_semaphore_t *bsp; | ||
1005 | |||
1006 | (void)options; | ||
1007 | |||
1008 | /* NULL pointer checks.*/ | ||
1009 | if ((sem_id == NULL) || (sem_name == NULL)) { | ||
1010 | return OS_INVALID_POINTER; | ||
1011 | } | ||
1012 | |||
1013 | /* Checking semaphore name length.*/ | ||
1014 | if (strlen(sem_name) >= OS_MAX_API_NAME) { | ||
1015 | return OS_ERR_NAME_TOO_LONG; | ||
1016 | } | ||
1017 | |||
1018 | /* Semaphore counter check, it is binary so only 0 and 1.*/ | ||
1019 | if (sem_initial_value > 1) { | ||
1020 | return OS_INVALID_INT_NUM; | ||
1021 | } | ||
1022 | |||
1023 | /* Getting object.*/ | ||
1024 | bsp = chPoolAlloc(&osal.binary_semaphores_pool); | ||
1025 | if (bsp == NULL) { | ||
1026 | return OS_ERR_NO_FREE_IDS; | ||
1027 | } | ||
1028 | |||
1029 | /* Semaphore is initialized.*/ | ||
1030 | chBSemObjectInit(bsp, sem_initial_value == 0 ? true : false); | ||
1031 | |||
1032 | *sem_id = (uint32)bsp; | ||
1033 | |||
1034 | return OS_SUCCESS; | ||
1035 | } | ||
1036 | |||
1037 | /** | ||
1038 | * @brief Binary semaphore deletion. | ||
1039 | * | ||
1040 | * @param[in] sem_id binary semaphore id variable | ||
1041 | * @return An error code. | ||
1042 | * | ||
1043 | * @api | ||
1044 | */ | ||
1045 | int32 OS_BinSemDelete(uint32 sem_id) { | ||
1046 | binary_semaphore_t *bsp = (binary_semaphore_t *)sem_id; | ||
1047 | |||
1048 | /* Range check.*/ | ||
1049 | if ((bsp < &osal.binary_semaphores[0]) || | ||
1050 | (bsp >= &osal.binary_semaphores[OS_MAX_BIN_SEMAPHORES])) { | ||
1051 | return OS_ERR_INVALID_ID; | ||
1052 | } | ||
1053 | |||
1054 | chSysLock(); | ||
1055 | |||
1056 | /* Resetting the semaphore, no threads in queue.*/ | ||
1057 | chBSemResetI(bsp, true); | ||
1058 | |||
1059 | /* Flagging it as unused and returning it to the pool.*/ | ||
1060 | bsp->sem.queue.prev = NULL; | ||
1061 | chPoolFreeI(&osal.binary_semaphores_pool, (void *)bsp); | ||
1062 | |||
1063 | /* Required because some thread could have been made ready.*/ | ||
1064 | chSchRescheduleS(); | ||
1065 | |||
1066 | chSysUnlock(); | ||
1067 | |||
1068 | return OS_SUCCESS; | ||
1069 | } | ||
1070 | |||
1071 | /** | ||
1072 | * @brief Binary semaphore flush. | ||
1073 | * @note The state of the binary semaphore is not changed. | ||
1074 | * @note This function can be safely called from timer callbacks or ISRs. | ||
1075 | * | ||
1076 | * @param[in] sem_id binary semaphore id variable | ||
1077 | * @return An error code. | ||
1078 | * | ||
1079 | * @api | ||
1080 | */ | ||
1081 | int32 OS_BinSemFlush(uint32 sem_id) { | ||
1082 | syssts_t sts; | ||
1083 | binary_semaphore_t *bsp = (binary_semaphore_t *)sem_id; | ||
1084 | |||
1085 | /* Range check.*/ | ||
1086 | if ((bsp < &osal.binary_semaphores[0]) || | ||
1087 | (bsp >= &osal.binary_semaphores[OS_MAX_BIN_SEMAPHORES])) { | ||
1088 | return OS_ERR_INVALID_ID; | ||
1089 | } | ||
1090 | |||
1091 | /* Entering a reentrant critical zone.*/ | ||
1092 | sts = chSysGetStatusAndLockX(); | ||
1093 | |||
1094 | /* If the semaphore is not in use then error.*/ | ||
1095 | if (bsp->sem.queue.prev == NULL) { | ||
1096 | /* Leaving the critical zone.*/ | ||
1097 | chSysRestoreStatusX(sts); | ||
1098 | return OS_SEM_FAILURE; | ||
1099 | } | ||
1100 | |||
1101 | /* If the semaphore state is "not taken" then it is not touched.*/ | ||
1102 | if (bsp->sem.cnt < 0) { | ||
1103 | chBSemResetI(bsp, true); | ||
1104 | } | ||
1105 | |||
1106 | /* Leaving the critical zone.*/ | ||
1107 | chSysRestoreStatusX(sts); | ||
1108 | |||
1109 | return OS_SUCCESS; | ||
1110 | } | ||
1111 | |||
1112 | /** | ||
1113 | * @brief Binary semaphore give. | ||
1114 | * @note This function can be safely called from timer callbacks or ISRs. | ||
1115 | * | ||
1116 | * @param[in] sem_id binary semaphore id variable | ||
1117 | * @return An error code. | ||
1118 | * | ||
1119 | * @api | ||
1120 | */ | ||
1121 | int32 OS_BinSemGive(uint32 sem_id) { | ||
1122 | syssts_t sts; | ||
1123 | binary_semaphore_t *bsp = (binary_semaphore_t *)sem_id; | ||
1124 | |||
1125 | /* Range check.*/ | ||
1126 | if ((bsp < &osal.binary_semaphores[0]) || | ||
1127 | (bsp >= &osal.binary_semaphores[OS_MAX_BIN_SEMAPHORES])) { | ||
1128 | return OS_ERR_INVALID_ID; | ||
1129 | } | ||
1130 | |||
1131 | /* Entering a reentrant critical zone.*/ | ||
1132 | sts = chSysGetStatusAndLockX(); | ||
1133 | |||
1134 | /* If the semaphore is not in use then error.*/ | ||
1135 | if (bsp->sem.queue.prev == NULL) { | ||
1136 | /* Leaving the critical zone.*/ | ||
1137 | chSysRestoreStatusX(sts); | ||
1138 | return OS_SEM_FAILURE; | ||
1139 | } | ||
1140 | |||
1141 | chBSemSignalI(bsp); | ||
1142 | |||
1143 | /* Leaving the critical zone.*/ | ||
1144 | chSysRestoreStatusX(sts); | ||
1145 | |||
1146 | return OS_SUCCESS; | ||
1147 | } | ||
1148 | |||
1149 | /** | ||
1150 | * @brief Binary semaphore take. | ||
1151 | * | ||
1152 | * @param[in] sem_id binary semaphore id variable | ||
1153 | * @return An error code. | ||
1154 | * | ||
1155 | * @api | ||
1156 | */ | ||
1157 | int32 OS_BinSemTake(uint32 sem_id) { | ||
1158 | binary_semaphore_t *bsp = (binary_semaphore_t *)sem_id; | ||
1159 | |||
1160 | /* Range check.*/ | ||
1161 | if ((bsp < &osal.binary_semaphores[0]) || | ||
1162 | (bsp >= &osal.binary_semaphores[OS_MAX_BIN_SEMAPHORES])) { | ||
1163 | return OS_ERR_INVALID_ID; | ||
1164 | } | ||
1165 | |||
1166 | chSysLock(); | ||
1167 | |||
1168 | /* If the semaphore is not in use then error.*/ | ||
1169 | if (bsp->sem.queue.prev == NULL) { | ||
1170 | chSysUnlock(); | ||
1171 | return OS_SEM_FAILURE; | ||
1172 | } | ||
1173 | |||
1174 | (void) chBSemWaitS(bsp); | ||
1175 | |||
1176 | chSysUnlock(); | ||
1177 | |||
1178 | return OS_SUCCESS; | ||
1179 | } | ||
1180 | |||
1181 | /** | ||
1182 | * @brief Binary semaphore take with timeout. | ||
1183 | * | ||
1184 | * @param[in] sem_id binary semaphore id variable | ||
1185 | * @param[in] msecs timeout in milliseconds | ||
1186 | * @return An error code. | ||
1187 | * | ||
1188 | * @api | ||
1189 | */ | ||
1190 | int32 OS_BinSemTimedWait(uint32 sem_id, uint32 msecs) { | ||
1191 | binary_semaphore_t *bsp = (binary_semaphore_t *)sem_id; | ||
1192 | msg_t msg; | ||
1193 | |||
1194 | /* Range check.*/ | ||
1195 | if ((bsp < &osal.binary_semaphores[0]) || | ||
1196 | (bsp >= &osal.binary_semaphores[OS_MAX_BIN_SEMAPHORES])) { | ||
1197 | return OS_ERR_INVALID_ID; | ||
1198 | } | ||
1199 | |||
1200 | /* Timeouts of zero not allowed.*/ | ||
1201 | if (msecs == 0) { | ||
1202 | return OS_INVALID_INT_NUM; | ||
1203 | } | ||
1204 | |||
1205 | chSysLock(); | ||
1206 | |||
1207 | /* If the semaphore is not in use then error.*/ | ||
1208 | if (bsp->sem.queue.prev == NULL) { | ||
1209 | chSysUnlock(); | ||
1210 | return OS_SEM_FAILURE; | ||
1211 | } | ||
1212 | |||
1213 | msg = chBSemWaitTimeoutS(bsp, TIME_MS2I(msecs)); | ||
1214 | |||
1215 | chSysUnlock(); | ||
1216 | |||
1217 | return msg == MSG_TIMEOUT ? OS_SEM_TIMEOUT : OS_SUCCESS; | ||
1218 | } | ||
1219 | |||
1220 | /** | ||
1221 | * @brief Retrieves a binary semaphore id by name. | ||
1222 | * @note It is not currently implemented. | ||
1223 | * | ||
1224 | * @param[out] sem_id pointer to a binary semaphore id variable | ||
1225 | * @param[in] sem_name the binary semaphore name | ||
1226 | * @return An error code. | ||
1227 | * | ||
1228 | * @api | ||
1229 | */ | ||
1230 | int32 OS_BinSemGetIdByName(uint32 *sem_id, const char *sem_name) { | ||
1231 | |||
1232 | /* NULL pointer checks.*/ | ||
1233 | if ((sem_id == NULL) || (sem_name == NULL)) { | ||
1234 | return OS_INVALID_POINTER; | ||
1235 | } | ||
1236 | |||
1237 | /* Checking name length.*/ | ||
1238 | if (strlen(sem_name) >= OS_MAX_API_NAME) { | ||
1239 | return OS_ERR_NAME_TOO_LONG; | ||
1240 | } | ||
1241 | |||
1242 | return OS_ERR_NOT_IMPLEMENTED; | ||
1243 | } | ||
1244 | |||
1245 | /** | ||
1246 | * @brief Returns binary semaphore information. | ||
1247 | * @note This function can be safely called from timer callbacks or ISRs. | ||
1248 | * @note It is not currently implemented. | ||
1249 | * | ||
1250 | * @param[in] sem_id binary semaphore id variable | ||
1251 | * @param[in] bin_prop binary semaphore properties | ||
1252 | * @return An error code. | ||
1253 | * | ||
1254 | * @api | ||
1255 | */ | ||
1256 | int32 OS_BinSemGetInfo(uint32 sem_id, OS_bin_sem_prop_t *bin_prop) { | ||
1257 | syssts_t sts; | ||
1258 | binary_semaphore_t *bsp = (binary_semaphore_t *)sem_id; | ||
1259 | |||
1260 | /* NULL pointer checks.*/ | ||
1261 | if (bin_prop == NULL) { | ||
1262 | return OS_INVALID_POINTER; | ||
1263 | } | ||
1264 | |||
1265 | /* Range check.*/ | ||
1266 | if ((bsp < &osal.binary_semaphores[0]) || | ||
1267 | (bsp >= &osal.binary_semaphores[OS_MAX_BIN_SEMAPHORES])) { | ||
1268 | return OS_ERR_INVALID_ID; | ||
1269 | } | ||
1270 | |||
1271 | /* Entering a reentrant critical zone.*/ | ||
1272 | sts = chSysGetStatusAndLockX(); | ||
1273 | |||
1274 | /* If the semaphore is not in use then error.*/ | ||
1275 | if (bsp->sem.queue.prev == NULL) { | ||
1276 | /* Leaving the critical zone.*/ | ||
1277 | chSysRestoreStatusX(sts); | ||
1278 | return OS_ERR_INVALID_ID; | ||
1279 | } | ||
1280 | |||
1281 | /* Leaving the critical zone.*/ | ||
1282 | chSysRestoreStatusX(sts); | ||
1283 | |||
1284 | return OS_ERR_NOT_IMPLEMENTED; | ||
1285 | } | ||
1286 | |||
1287 | /*-- Counter Semaphore API --------------------------------------------------*/ | ||
1288 | |||
1289 | /** | ||
1290 | * @brief Counter semaphore creation. | ||
1291 | * | ||
1292 | * @param[out] sem_id pointer to a counter semaphore id variable | ||
1293 | * @param[in] sem_name the counter semaphore name | ||
1294 | * @param[in] sem_initial_value semaphore initial value | ||
1295 | * @param[in] options semaphore options | ||
1296 | * @return An error code. | ||
1297 | * | ||
1298 | * @api | ||
1299 | */ | ||
1300 | int32 OS_CountSemCreate(uint32 *sem_id, const char *sem_name, | ||
1301 | uint32 sem_initial_value, uint32 options) { | ||
1302 | semaphore_t *sp; | ||
1303 | |||
1304 | (void)options; | ||
1305 | |||
1306 | /* NULL pointer checks.*/ | ||
1307 | if ((sem_id == NULL) || (sem_name == NULL)) { | ||
1308 | return OS_INVALID_POINTER; | ||
1309 | } | ||
1310 | |||
1311 | /* Checking semaphore name length.*/ | ||
1312 | if (strlen(sem_name) >= OS_MAX_API_NAME) { | ||
1313 | return OS_ERR_NAME_TOO_LONG; | ||
1314 | } | ||
1315 | |||
1316 | /* Semaphore counter check, it must be non-negative.*/ | ||
1317 | if ((int32)sem_initial_value < 0) { | ||
1318 | return OS_INVALID_INT_NUM; | ||
1319 | } | ||
1320 | |||
1321 | /* Getting object.*/ | ||
1322 | sp = chPoolAlloc(&osal.count_semaphores_pool); | ||
1323 | if (sp == NULL) { | ||
1324 | return OS_ERR_NO_FREE_IDS; | ||
1325 | } | ||
1326 | |||
1327 | /* Semaphore is initialized.*/ | ||
1328 | chSemObjectInit(sp, (cnt_t)sem_initial_value); | ||
1329 | |||
1330 | *sem_id = (uint32)sp; | ||
1331 | |||
1332 | return OS_SUCCESS; | ||
1333 | } | ||
1334 | |||
1335 | /** | ||
1336 | * @brief Counter semaphore deletion. | ||
1337 | * | ||
1338 | * @param[in] sem_id counter semaphore id variable | ||
1339 | * @return An error code. | ||
1340 | * | ||
1341 | * @api | ||
1342 | */ | ||
1343 | int32 OS_CountSemDelete(uint32 sem_id) { | ||
1344 | semaphore_t *sp = (semaphore_t *)sem_id; | ||
1345 | |||
1346 | /* Range check.*/ | ||
1347 | if ((sp < &osal.count_semaphores[0]) || | ||
1348 | (sp >= &osal.count_semaphores[OS_MAX_COUNT_SEMAPHORES])) { | ||
1349 | return OS_ERR_INVALID_ID; | ||
1350 | } | ||
1351 | |||
1352 | chSysLock(); | ||
1353 | |||
1354 | /* Resetting the semaphore, no threads in queue.*/ | ||
1355 | chSemResetI(sp, 0); | ||
1356 | |||
1357 | /* Flagging it as unused and returning it to the pool.*/ | ||
1358 | sp->queue.prev = NULL; | ||
1359 | chPoolFreeI(&osal.count_semaphores_pool, (void *)sp); | ||
1360 | |||
1361 | /* Required because some thread could have been made ready.*/ | ||
1362 | chSchRescheduleS(); | ||
1363 | |||
1364 | chSysUnlock(); | ||
1365 | |||
1366 | return OS_SUCCESS; | ||
1367 | } | ||
1368 | |||
1369 | /** | ||
1370 | * @brief Counter semaphore give. | ||
1371 | * @note This function can be safely called from timer callbacks or ISRs. | ||
1372 | * | ||
1373 | * @param[in] sem_id counter semaphore id variable | ||
1374 | * @return An error code. | ||
1375 | * | ||
1376 | * @api | ||
1377 | */ | ||
1378 | int32 OS_CountSemGive(uint32 sem_id) { | ||
1379 | syssts_t sts; | ||
1380 | semaphore_t *sp = (semaphore_t *)sem_id; | ||
1381 | |||
1382 | /* Range check.*/ | ||
1383 | if ((sp < &osal.count_semaphores[0]) || | ||
1384 | (sp >= &osal.count_semaphores[OS_MAX_COUNT_SEMAPHORES])) { | ||
1385 | return OS_ERR_INVALID_ID; | ||
1386 | } | ||
1387 | |||
1388 | /* Entering a reentrant critical zone.*/ | ||
1389 | sts = chSysGetStatusAndLockX(); | ||
1390 | |||
1391 | /* If the semaphore is not in use then error.*/ | ||
1392 | if (sp->queue.prev == NULL) { | ||
1393 | /* Leaving the critical zone.*/ | ||
1394 | chSysRestoreStatusX(sts); | ||
1395 | return OS_SEM_FAILURE; | ||
1396 | } | ||
1397 | |||
1398 | chSemSignalI(sp); | ||
1399 | |||
1400 | /* Leaving the critical zone.*/ | ||
1401 | chSysRestoreStatusX(sts); | ||
1402 | |||
1403 | return OS_SUCCESS; | ||
1404 | } | ||
1405 | |||
1406 | /** | ||
1407 | * @brief Counter semaphore take. | ||
1408 | * | ||
1409 | * @param[in] sem_id counter semaphore id variable | ||
1410 | * @return An error code. | ||
1411 | * | ||
1412 | * @api | ||
1413 | */ | ||
1414 | int32 OS_CountSemTake(uint32 sem_id) { | ||
1415 | semaphore_t *sp = (semaphore_t *)sem_id; | ||
1416 | |||
1417 | /* Range check.*/ | ||
1418 | if ((sp < &osal.count_semaphores[0]) || | ||
1419 | (sp >= &osal.count_semaphores[OS_MAX_COUNT_SEMAPHORES])) { | ||
1420 | return OS_ERR_INVALID_ID; | ||
1421 | } | ||
1422 | |||
1423 | chSysLock(); | ||
1424 | |||
1425 | /* If the semaphore is not in use then error.*/ | ||
1426 | if (sp->queue.prev == NULL) { | ||
1427 | chSysUnlock(); | ||
1428 | return OS_SEM_FAILURE; | ||
1429 | } | ||
1430 | |||
1431 | (void) chSemWaitS(sp); | ||
1432 | |||
1433 | chSysUnlock(); | ||
1434 | |||
1435 | return OS_SUCCESS; | ||
1436 | } | ||
1437 | |||
1438 | /** | ||
1439 | * @brief Counter semaphore take with timeout. | ||
1440 | * | ||
1441 | * @param[in] sem_id counter semaphore id variable | ||
1442 | * @param[in] msecs timeout in milliseconds | ||
1443 | * @return An error code. | ||
1444 | * | ||
1445 | * @api | ||
1446 | */ | ||
1447 | int32 OS_CountSemTimedWait(uint32 sem_id, uint32 msecs) { | ||
1448 | semaphore_t *sp = (semaphore_t *)sem_id; | ||
1449 | msg_t msg; | ||
1450 | |||
1451 | /* Range check.*/ | ||
1452 | if ((sp < &osal.count_semaphores[0]) || | ||
1453 | (sp >= &osal.count_semaphores[OS_MAX_COUNT_SEMAPHORES])) { | ||
1454 | return OS_ERR_INVALID_ID; | ||
1455 | } | ||
1456 | |||
1457 | /* Timeouts of zero not allowed.*/ | ||
1458 | if (msecs == 0) { | ||
1459 | return OS_INVALID_INT_NUM; | ||
1460 | } | ||
1461 | |||
1462 | chSysLock(); | ||
1463 | |||
1464 | /* If the semaphore is not in use then error.*/ | ||
1465 | if (sp->queue.prev == NULL) { | ||
1466 | chSysUnlock(); | ||
1467 | return OS_SEM_FAILURE; | ||
1468 | } | ||
1469 | |||
1470 | msg = chSemWaitTimeoutS(sp, TIME_MS2I(msecs)); | ||
1471 | |||
1472 | chSysUnlock(); | ||
1473 | |||
1474 | return msg == MSG_TIMEOUT ? OS_SEM_TIMEOUT : OS_SUCCESS; | ||
1475 | } | ||
1476 | |||
1477 | /** | ||
1478 | * @brief Retrieves a counter semaphore id by name. | ||
1479 | * @note It is not currently implemented. | ||
1480 | * | ||
1481 | * @param[out] sem_id pointer to a counter semaphore id variable | ||
1482 | * @param[in] sem_name the counter semaphore name | ||
1483 | * @return An error code. | ||
1484 | * | ||
1485 | * @api | ||
1486 | */ | ||
1487 | int32 OS_CountSemGetIdByName(uint32 *sem_id, const char *sem_name) { | ||
1488 | |||
1489 | /* NULL pointer checks.*/ | ||
1490 | if ((sem_id == NULL) || (sem_name == NULL)) { | ||
1491 | return OS_INVALID_POINTER; | ||
1492 | } | ||
1493 | |||
1494 | /* Checking name length.*/ | ||
1495 | if (strlen(sem_name) >= OS_MAX_API_NAME) { | ||
1496 | return OS_ERR_NAME_TOO_LONG; | ||
1497 | } | ||
1498 | |||
1499 | return OS_ERR_NOT_IMPLEMENTED; | ||
1500 | } | ||
1501 | |||
1502 | /** | ||
1503 | * @brief Returns counter semaphore information. | ||
1504 | * @note This function can be safely called from timer callbacks or ISRs. | ||
1505 | * @note It is not currently implemented. | ||
1506 | * | ||
1507 | * @param[in] sem_id counter semaphore id variable | ||
1508 | * @param[in] sem_prop counter semaphore properties | ||
1509 | * @return An error code. | ||
1510 | * | ||
1511 | * @api | ||
1512 | */ | ||
1513 | int32 OS_CountSemGetInfo(uint32 sem_id, OS_count_sem_prop_t *sem_prop) { | ||
1514 | syssts_t sts; | ||
1515 | semaphore_t *sp = (semaphore_t *)sem_id; | ||
1516 | |||
1517 | /* NULL pointer checks.*/ | ||
1518 | if (sem_prop == NULL) { | ||
1519 | return OS_INVALID_POINTER; | ||
1520 | } | ||
1521 | |||
1522 | /* Range check.*/ | ||
1523 | if ((sp < &osal.count_semaphores[0]) || | ||
1524 | (sp >= &osal.count_semaphores[OS_MAX_BIN_SEMAPHORES])) { | ||
1525 | return OS_ERR_INVALID_ID; | ||
1526 | } | ||
1527 | |||
1528 | /* Entering a reentrant critical zone.*/ | ||
1529 | sts = chSysGetStatusAndLockX(); | ||
1530 | |||
1531 | /* If the semaphore is not in use then error.*/ | ||
1532 | if (sp->queue.prev == NULL) { | ||
1533 | /* Leaving the critical zone.*/ | ||
1534 | chSysRestoreStatusX(sts); | ||
1535 | return OS_ERR_INVALID_ID; | ||
1536 | } | ||
1537 | |||
1538 | /* Leaving the critical zone.*/ | ||
1539 | chSysRestoreStatusX(sts); | ||
1540 | |||
1541 | return OS_ERR_NOT_IMPLEMENTED; | ||
1542 | } | ||
1543 | |||
1544 | /*-- Mutex API --------------------------------------------------------------*/ | ||
1545 | |||
1546 | /** | ||
1547 | * @brief Mutex creation. | ||
1548 | * | ||
1549 | * @param[out] sem_id pointer to a mutex id variable | ||
1550 | * @param[in] sem_name the mutex name | ||
1551 | * @param[in] options mutex options | ||
1552 | * @return An error code. | ||
1553 | * | ||
1554 | * @api | ||
1555 | */ | ||
1556 | int32 OS_MutSemCreate(uint32 *sem_id, const char *sem_name, uint32 options) { | ||
1557 | mutex_t *mp; | ||
1558 | |||
1559 | (void)options; | ||
1560 | |||
1561 | /* NULL pointer checks.*/ | ||
1562 | if ((sem_id == NULL) || (sem_name == NULL)) { | ||
1563 | return OS_INVALID_POINTER; | ||
1564 | } | ||
1565 | |||
1566 | /* Checking semaphore name length.*/ | ||
1567 | if (strlen(sem_name) >= OS_MAX_API_NAME) { | ||
1568 | return OS_ERR_NAME_TOO_LONG; | ||
1569 | } | ||
1570 | |||
1571 | /* Getting object.*/ | ||
1572 | mp = chPoolAlloc(&osal.mutexes_pool); | ||
1573 | if (mp == NULL) { | ||
1574 | return OS_ERR_NO_FREE_IDS; | ||
1575 | } | ||
1576 | |||
1577 | /* Semaphore is initialized.*/ | ||
1578 | chMtxObjectInit(mp); | ||
1579 | |||
1580 | *sem_id = (uint32)mp; | ||
1581 | |||
1582 | return OS_SUCCESS; | ||
1583 | } | ||
1584 | |||
1585 | /** | ||
1586 | * @brief Mutex deletion. | ||
1587 | * | ||
1588 | * @param[in] sem_id mutex id variable | ||
1589 | * @return An error code. | ||
1590 | * | ||
1591 | * @api | ||
1592 | */ | ||
1593 | int32 OS_MutSemDelete(uint32 sem_id) { | ||
1594 | mutex_t *mp = (mutex_t *)sem_id; | ||
1595 | |||
1596 | /* Range check.*/ | ||
1597 | if ((mp < &osal.mutexes[0]) || | ||
1598 | (mp >= &osal.mutexes[OS_MAX_MUTEXES])) { | ||
1599 | return OS_ERR_INVALID_ID; | ||
1600 | } | ||
1601 | |||
1602 | chSysLock(); | ||
1603 | |||
1604 | /* Resetting the mutex, no threads in queue.*/ | ||
1605 | chMtxUnlockAllS(); | ||
1606 | |||
1607 | /* Flagging it as unused and returning it to the pool.*/ | ||
1608 | mp->queue.prev = NULL; | ||
1609 | chPoolFreeI(&osal.mutexes_pool, (void *)mp); | ||
1610 | |||
1611 | /* Required because some thread could have been made ready.*/ | ||
1612 | chSchRescheduleS(); | ||
1613 | |||
1614 | chSysUnlock(); | ||
1615 | |||
1616 | return OS_SUCCESS; | ||
1617 | } | ||
1618 | |||
1619 | /** | ||
1620 | * @brief Mutex give. | ||
1621 | * | ||
1622 | * @param[in] sem_id mutex id variable | ||
1623 | * @return An error code. | ||
1624 | * | ||
1625 | * @api | ||
1626 | */ | ||
1627 | int32 OS_MutSemGive(uint32 sem_id) { | ||
1628 | mutex_t *mp = (mutex_t *)sem_id; | ||
1629 | |||
1630 | /* Range check.*/ | ||
1631 | if ((mp < &osal.mutexes[0]) || | ||
1632 | (mp >= &osal.mutexes[OS_MAX_COUNT_SEMAPHORES])) { | ||
1633 | return OS_ERR_INVALID_ID; | ||
1634 | } | ||
1635 | |||
1636 | chSysLock(); | ||
1637 | |||
1638 | /* If the mutex is not in use then error.*/ | ||
1639 | if (mp->queue.prev == NULL) { | ||
1640 | chSysUnlock(); | ||
1641 | return OS_SEM_FAILURE; | ||
1642 | } | ||
1643 | |||
1644 | chMtxUnlockS(mp); | ||
1645 | chSchRescheduleS(); | ||
1646 | |||
1647 | chSysUnlock(); | ||
1648 | |||
1649 | return OS_SUCCESS; | ||
1650 | } | ||
1651 | |||
1652 | /** | ||
1653 | * @brief Mutex take. | ||
1654 | * | ||
1655 | * @param[in] sem_id mutex id variable | ||
1656 | * @return An error code. | ||
1657 | * | ||
1658 | * @api | ||
1659 | */ | ||
1660 | int32 OS_MutSemTake(uint32 sem_id) { | ||
1661 | mutex_t *mp = (mutex_t *)sem_id; | ||
1662 | |||
1663 | /* Rang |