1 /**************************************************************************/
2 /*                                                                        */
3 /*       Copyright (c) Microsoft Corporation. All rights reserved.        */
4 /*                                                                        */
5 /*       This software is licensed under the Microsoft Software License   */
6 /*       Terms for Microsoft Azure RTOS. Full text of the license can be  */
7 /*       found in the LICENSE file at https://aka.ms/AzureRTOS_EULA       */
8 /*       and in the root directory of this software.                      */
9 /*                                                                        */
10 /**************************************************************************/
11 
12 
13 /**************************************************************************/
14 /**************************************************************************/
15 /**                                                                       */
16 /**   ThreadX Component                                                   */
17 /**                                                                       */
18 /**   OSEK IMPLEMENTATION                                                 */
19 /**                                                                       */
20 /**************************************************************************/
21 /**************************************************************************/
22 
23 /**************************************************************************/
24 /*                                                                        */
25 /*  EKV DEFINITIONS                                        RELEASE        */
26 /*                                                                        */
27 /*    os.h                                                PORTABLE C      */
28 /*                                                           6.1.7        */
29 /*  AUTHOR                                                                */
30 /*                                                                        */
31 /*    William E. Lamie, Microsoft Corporation                             */
32 /*                                                                        */
33 /*  DESCRIPTION                                                           */
34 /*                                                                        */
35 /*    This file defines the constants, structures, etc. needed for the    */
36 /*    OSEK implementation.                                                */
37 /*                                                                        */
38 /*                                                                        */
39 /*  RELEASE HISTORY                                                       */
40 /*                                                                        */
41 /*    DATE              NAME                      DESCRIPTION             */
42 /*                                                                        */
43 /*  06-02-2021     William E. Lamie         Initial Version 6.1.7         */
44 /*                                                                        */
45 /**************************************************************************/
46 
47 #ifndef TX_OSEK_H
48 #define TX_OSEK_H
49 
50 #include <tx_api.h>
51 
52 #include "osek_user.h"
53 
54 #ifdef TRUE
55 #undef TRUE
56 #endif
57 
58 #ifdef FALSE
59 #undef FALSE
60 #endif
61 
62 
63 /**************************************************************************/
64 /* System Macros.                                                         */
65 /**************************************************************************/
66 
67 #define TASK(Taskname)                              void Func ## Taskname()
68 #define TaskEntry(Taskname)                         Func ## Taskname
69 #define ALARM(Alarmname)                            void Func ## Alarmname()
70 #define ISR(ISRname)                                void ISR_ ## ISRname()
71 #define ISREntry(ISRname)                           ISR_ ## ISRname
72 
73 #define ALARMCALLBACK(Alarmcallbackfunction)        void Func ## Alarmcallbackfunction()
74 #define ALARMCALLBACKEntry(Alarmcallbackfunction)   Func ## Alarmcallbackfunction
75 #define DeclareALARMCALLBACK(Alarmcallbackfunction) ALARMCALLBACK(Alarmcallbackfunction)
76 
77 #define DeclareISR(ISRname)                         ISR(ISRname)
78 #define DeclareResource(Resource_name)
79 #define DeclareEvent(Event_name)
80 #define DeclareAlarm(Alarmname)                     ALARM(Alarmname)
81 #define DeclareTask(Taskname)                       TASK(Taskname)
82 
83 #define CALL_ISR(ISRname)                           ISR_ ## ISRname()
84 
85 #define OSErrorGetServiceId()                       service_GetServiceId.id
86 #define OSError_ActivateTask_TaskID()               service_ActivateTask.TaskID
87 #define OSError_ChainTask_TaskID()                  service_ChainTask.TaskID
88 #define OSError_GetAlarm_AlarmID()                  service_GetAlarm.AlarmID
89 #define OSError_CancelAlarm_AlarmID()               service_CancelAlarm.AlarmID
90 #define OSError_SetAbsAlarm_AlarmID()               service_SetAbsAlarm.AlarmID
91 #define OSError_SetRelAlarm_AlarmID()               service_SetRelAlarm.AlarmID
92 #define OSError_GetResource_ResID()                 service_GetResource.ResID
93 #define OSError_ReleaseResource_ResID()             service_ReleaseResource.ResID
94 #define OSError_SetEvent_TaskID()                   service_SetEvent.TaskID
95 #define OSError_GetEvent_TaskID()                   service_SetEvent.TaskID
96 #define OSError_WaitEvent_EventID()                 service_SetEvent.EventID
97 #define OSError_ClearEvent_EventID()                service_ClearEvent.EventID
98 
99 /**************************************************************************/
100 /*                         OSEK data Types                                */
101 /**************************************************************************/
102 
103 typedef ULONG                   STATUS;
104 typedef void                    *VP;
105 typedef void                    (* FP)();
106 typedef ULONG                   PRI;
107 typedef ULONG                   CounterType;
108 typedef ULONG                   TaskType;
109 typedef TaskType                ISRType;                  /* TaskType and ISRType must be same as both often used interchangeably. */
110 typedef ULONG                   TaskStateType;
111 typedef ULONG                  *TaskStateRefType;
112 typedef ULONG                  *TaskRefType;
113 typedef ULONG                   TickType;
114 typedef ULONG                  *TickRefType;
115 typedef ULONG                   AppModeType;
116 typedef ULONG                   AlarmType;
117 typedef ULONG                   StatusType;
118 typedef ULONG                   OsServiceIdType;
119 typedef ULONG                   ResourceType;
120 typedef ULONG                  *ResourceRefType;
121 typedef ULONG                   EventMaskType;
122 typedef ULONG                  *EventMaskRefType;
123 typedef ULONG                   EventType;
124 typedef ULONG                  *EventRefType;
125 
126 
127 /**************************************************************************/
128 /*        OSEK  Error Codes                                               */
129 /**************************************************************************/
130 
131 #define E_OK                    (0U)
132 #define E_OS_ACCESS             (1U)
133 #define E_OS_CALLEVEL           (2U)
134 #define E_OS_ID                 (3U)
135 #define E_OS_LIMIT              (4U)
136 #define E_OS_NOFUNC             (5U)
137 #define E_OS_RESOURCE           (6U)
138 #define E_OS_STATE              (7U)
139 #define E_OS_VALUE              (8U)
140 
141 
142 /* Additional error codes defined for this Implementation.  */
143 
144 #define E_OS_EVENT              (9U)
145 #define E_OS_EXIST              (10U)   /* identical task name are found.  */
146 #define E_OS_SYSTEM             (11U)   /* Error for the OSEK system.      */
147 #define E_OS_SYS_STACK          (12U)   /* Error For OSEK Memory.          */
148 
149 /* osek_internal_error() error codes.                       */
150 
151 #define THREADX_OBJECT_CREATION_ERROR               (1U)
152 #define THREADX_THREAD_RESUME_IN_ACTIVATE_TASK      (2U)
153 #define THREADX_PREEMPTION_CHANGE_GETRESOURCE       (3U)
154 #define THREADX_PREEMPTION_CHANGE_RLSRESOURCE       (4U)
155 #define THREADX_THREAD_TERMINATE_TERMINATETASK      (5U)
156 #define THREADX_THREAD_DELETE_TERMINATETASK         (6U)
157 #define THREADX_THREAD_TERMINATE_CHAINTASK          (7U)
158 #define THREADX_THREAD_DELETE_CHAINTASK             (8U)
159 #define THREADX_THREAD_DELETE_DELETETASK            (9U)
160 #define THREADX_THREAD_TERMINATE_DELETETASK         (10U)
161 #define THREADX_PREEMPTION_CHANGE_WRAPPER           (11U)
162 #define THREADX_THREAD_RELINQUISH_SCHEDULE          (12U)
163 #define THREADX_MUTEX_CREATERESOURCE                (13U)
164 #define THREADX_EVENT_SETEVENT                      (14U)
165 #define THREADX_EVENT_CLEAREVENT                    (15U)
166 #define THREADX_EVENT_FLAG_GETEVENT                 (16U)
167 #define THREADX_EVENT_FLAG_WAITEVENT                (17U)
168 #define QUEUE_DELETETASK                            (18U)
169 #define NO_FREE_EVENT                               (20U)
170 #define SYSMGR_FATAL_ERROR                          (21U)
171 #define EVENT_DELETETASK                            (22U)
172 #define ERROR_FREEING_MEMORY                        (23U)
173 #define SYS_MGR_SEND_CHAINTASK                      (24U)
174 #define SYS_MGR_SEND_TERMINATETASK                  (25U)
175 #define INVALID_ALARMID_TIMERWRAPPER                (26U)
176 #define TASK_ENDING_WITHOUT_CHAIN_OR_TERMINATE      (27U)
177 #define SYSMGR_QUEUE_SEND                           (28U)
178 #define INVALID_OBJECT_CREATION_CALL                (29U)
179 #define SYS_MGR_START_OS                            (30U)
180 #define SYS_MGR_SEND_ACTIVATETASK                   (31U)
181 #define ERROR_OBJECT_CREATION                       (32U)
182 
183 
184 /**************************************************************************/
185 /*  OSEM SYSTEM SERVICES IDs                                              */
186 /**************************************************************************/
187 
188 #define OSServiceId_ActivateTask                (1U)
189 #define OSServiceId_TerminateTask               (2U)
190 #define OSServiceId_ChainTask                   (3U)
191 #define OSServiceId_Schedule                    (4U)
192 #define OSServiceId_GetTaskID                   (5U)
193 #define OSServiceId_GetTaskState                (6U)
194 #define OSServiceId_DisableAllInterrupts        (7U)
195 #define OSServiceId_EnableAllInterrupts         (8U)
196 #define OSServiceId_SuspendAllInterrupts        (9U)
197 #define OSServiceId_ResumeAllInterrupts         (10U)
198 #define OSServiceId_SuspendOSInterrupts         (11U)
199 #define OSServiceId_ResumeOSInterrupts          (12U)
200 #define OSServiceId_GetResource                 (13U)
201 #define OSServiceId_ReleaseResource             (14U)
202 #define OSServiceId_SetEvent                    (15U)
203 #define OSServiceId_ClearEvent                  (16U)
204 #define OSServiceId_GetEvent                    (17U)
205 #define OSServiceId_WaitEvent                   (18U)
206 #define OSServiceId_GetAlarmBase                (19U)
207 #define OSServiceId_GetAlarm                    (20U)
208 #define OSServiceId_SetRelAlarm                 (21U)
209 #define OSServiceId_SetAbsAlarm                 (22U)
210 #define OSServiceId_CancelAlarm                 (23U)
211 #define OSServiceId_GetActiveApplicationMode    (24U)
212 #define OSServiceId_StartOS                     (25U)
213 #define OSServiceId_ShutdownOS                  (26U)
214 
215 
216 /**************************************************************************/
217 /*     Implementation Specific OSEK operating modes                       */
218 /*                                                                        */
219 /**************************************************************************/
220 
221 #define NORMAL_EXECUTION_MODE                       (0U)
222 #define STARTUPHOOK_MODE                            (1U)
223 #define SHUTDOWNHOOK_MODE                           (2U)
224 #define PRETASKHOOK_MODE                            (3U)
225 #define POSTTASKHOOK_MODE                           (4U)
226 #define ERRORHOOK_MODE                              (5U)
227 #define ISR1_MODE                                   (6U)
228 #define ISR2_MODE                                   (7U)
229 #define TIMER_MODE                                  (8U)
230 #define INITSYSTEM_MODE                             (9U)
231 #define ALARM_CALLBACK_MODE                         (10U)
232 #define OSEK_INIT_NOT_DONE                          (99U)
233 
234 
235 /**************************************************************************/
236 /*        OSEK  Constants and Definitions                                 */
237 /**************************************************************************/
238 
239 /* OS APPLICATION MODES   */
240 
241 #define OSDEFAULTAPPMODE                            (1U)
242 
243 
244 /* OSEK Task Types        */
245 
246 #define EXTENDED                                    (1U)
247 #define BASIC                                       (0U)
248 #define TRUE                                        (1U)
249 #define FALSE                                       (0U)
250 
251 
252 /* OSEK Task States       */
253 
254 #define RUNNING                                     (0U)
255 #define WAITING                                     (1U)
256 #define READY                                       (2U)
257 #define SUSPENDED                                   (3U)
258 #define THREADX_STATE                               (4U)
259 
260 /* Invalid task id        */
261 #define INVALID_TASK                                (0U)
262 
263 
264 /* ALARM OPERATION MODES  */
265 
266 #define ABSOLUTE_ALARM                              (1U)
267 #define RELATIVE_ALARM                              (0U)
268 #define AUTO_START                                  (1U)
269 #define NO_START                                    (0U)
270 
271 
272 /* RESOURCE TYPE */
273 
274 #define STANDARD                                    (0U)
275 #define INTERNAL                                    (1U)
276 #define LINKED                                      (2U)
277 
278 typedef enum                                        {NON, FULL} SCHEDULE;
279 typedef enum                                        {ACTIVATETASK, SETEVENT, CALLBACK, NONE} ACTION;
280 typedef ULONG                                       ACCR_TYPE;
281 typedef ULONG                                       TASK_TYPE;
282 typedef ULONG                                       COPY;
283 typedef ULONG                                       AUTOSTART;
284 
285 
286 /* ISR types */
287 
288 #define CATEGORY1                                   (1U)
289 #define CATEGORY2                                   (2U)
290 
291 
292 /**************************************************************************/
293 /*                    SYSTEM CONFIGURATION PARAMETERS                     */
294 /**************************************************************************/
295 
296 /* OSEK Priority Definitions  */
297 
298 #define THREADX_MAX_PRIORITY        (31U)
299 #define THREADX_HIGHEST_PRIORITY    (0U)
300 #define THREADX_LOWEST_PRIORITY     (31U)
301 
302 #define OSEK_MAX_PRIORITY           (23U)
303 #define OSEK_HIGHEST_PRIORITY       (OSEK_MAX_PRIORITY)
304 #define OSEK_LOWEST_PRIORITY        (0U)
305 
306 #define OSEK_NON_SCHEDULE_PRIORITY  (OSEK_HIGHEST_PRIORITY + 1u)      /* 24 */
307 #define OSEK_ISR2_PRIORITY          (OSEK_NON_SCHEDULE_PRIORITY + 1u) /* 25 */
308 #define OSEK_ISR1_PRIORITY          (OSEK_ISR2_PRIORITY + 1u)         /* 26 */
309 
310 
311 /* Define maximum queued activation per task */
312 
313 #define OSEK_MAX_ACTIVATION          (8U)
314 
315 /* Define maximum time count for Counters / Alarms */
316 
317 #define MAXALLOWEDVALUE              (0x7FFFFFFFUL)
318 
319 #define OSEK_STACK_PADDING          (128U)
320 #define OSEK_SYSTEM_STACK_SIZE      (1024U)
321 
322 
323 /* Requests/commands to SysMgr task.  */
324 
325 #define SYSMGR_START_OS             (0U)
326 #define SYSMGR_TERMINATE_TASK       (1U)
327 #define SYSMGR_CHAIN_TASK           (2U)
328 #define SYSMGR_ACTIVATE_TASK        (3U)
329 #define SYSMGR_SCHEDULE             (4U)
330 #define SYSMGR_WAITEVENT            (5U)
331 #define SYSMGR_RELEASE_RESOURCE     (6U)
332 #define SYSMGR_SETEVENT             (7U)
333 #define SYSMGR_SHUTDOWN_OS          (8U)
334 #define SYSMGR_ERRORHOOK            (9U)
335 #define SYSMGR_GET_RESOURCE         (10U)
336 
337 /**************************************************************************/
338 /* Define size of Region 0 memory segment.                                */
339 /* NOTE:  This region should be large enough to supply the memory         */
340 /*        for all task stacks, TCBs, thread control blocks in the system. */
341 /**************************************************************************/
342 
343 #define TX_REGION0_SIZE             (OSEK_MEMORY_SIZE)
344 #define TX_REGION0_SIZE_IN_BYTES    (TX_REGION0_SIZE)
345 
346 
347 /**********************************************************************************/
348 /* OSEK System Object maximum numbers                                             */
349 /* NOTE: These are only suggested values, user may modify them as needed.         */
350 /*       Memory is the only limiting factor.                                      */
351 /**********************************************************************************/
352 
353 /* Define the maximum number of simultaneous OSEK tasks supported.  */
354 /* Interrupt Service Routines are also treated as a task.           */
355 /* Total 8 Interrupt sources are supported.                         */
356 /* That gives 24 max OSEK tasks.                                    */
357 
358 #define OSEK_MAX_TASKS              (32U)
359 
360 /* Define the maximum number of simultaneous Internal OSEK Resources supported. */
361 
362 #define OSEK_MAX_INTERNAL_RES       (8U)
363 
364 
365 /* Define the maximum number of simultaneous External OSEK Resources supported. */
366 
367 #define OSEK_MAX_EXTERNAL_RES       (16U)
368 
369 
370 /* Define the maximum number of simultaneous OSEK Resources (Internal & External Combined) supported.  */
371 
372 #define OSEK_MAX_RES                (OSEK_MAX_INTERNAL_RES + OSEK_MAX_EXTERNAL_RES)
373 
374 
375 /* Define the maximum number of simultaneous OSEK alarms supported.  */
376 
377 #define OSEK_MAX_ALARMS             (16U)
378 
379 
380 /* Define the maximum number of simultaneous OSEK counters supported.  */
381 /* This includes a counter to be assigned as a SystemTimer.            */
382 /* A system can have 8 alarms so 8 counters (one counter for each alarm) would be enough.  */
383 
384 #define OSEK_MAX_COUNTERS           (16U)
385 
386 
387 /* Define the maximum number of simultaneous OSEK events supported.  */
388 /* An event is just a bit , so a ULONG (32 bit data type) is used.    */
389 
390 #define OSEK_MAX_EVENTS             (32U)
391 
392 
393 /* Define the maximum number of  OSEK ISR sources supported.   */
394 
395 #define OSEK_MAX_ISR                (8U)
396 
397 
398 /* Define  OSEK Internal Task Queue depth per priority.   */
399 /* There is one such queue for each priority level.  */
400 /*     What is the maximum number of tasks that can go in a queue?                                       */
401 /*     Assuming that all tasks are of same priority and each task is activated to maximum                */
402 /*     allowable limits (OSEK_MAX_ACTIVATION) it should be  OSEK_MAX_ACTIVATION * OSEK_MAX_TASKS         */
403 /*     but sometimes (READY)tasks are moved from queue to queue based on its changed ceiling priority    */
404 /*     and that number could be equal to to maximum number of tasks supported.                           */
405 
406 #define TASK_QUEUE_DEPTH1            (OSEK_MAX_ACTIVATION * OSEK_MAX_TASKS)
407 #define TASK_QUEUE_DEPTH             (TASK_QUEUE_DEPTH1 + OSEK_MAX_TASKS)
408 
409 #define SYSMGR_QUEUE_MSG_LENGTH     (4U)
410 #define SYSMGR_QUEUE_MSG_COUNT      (OSEK_MAX_ALARMS)
411 #define SYSMGR_QUEUE_DEPTH          ((sizeof(ULONG)) * SYSMGR_QUEUE_MSG_COUNT)
412 
413 #define SYSMGR_PRIORITY             (0U)
414 #define SYSMGR_THRESHOLD            (0U)
415 
416 
417 /**********************************************************************************/
418 /* OSEK System Object identifier this is placed in the object's control structure */
419 /**********************************************************************************/
420 
421 #define OSEK_TASK_ID                (0x1234ABCDUL)
422 #define OSEK_ALARM_ID               (0x4567ABCDUL)
423 #define OSEK_COUNTER_ID             (0xABCD1234UL)
424 #define OSEK_EVENT_ID               (0x1234FEDCUL)
425 #define OSEK_RES_ID                 (0x5678CDEFUL)
426 #define OSEK_APPLICATION_ID         (0x789ABCDEUL)
427 #define OSEK_ISR_ID                 (0x3456CDEFUL)
428 
429 
430 /**************************************************************************/
431 /*                   OSEK SYSTEM OBJECT CONTROL STRUCTURES                */
432 /**************************************************************************/
433 
434 
435 /*                 OSEK SERVICES                                          */
436 
437 struct Service_ActivateTask
438 {
439     StatusType      TaskID;
440 };
441 
442 struct Service_TerminateTask
443 {
444     StatusType      TaskID;
445 };
446 
447 struct Service_ChainTask
448 {
449     StatusType      TaskID;
450 };
451 
452 struct Service_Schedule
453 {
454     StatusType      TaskID;
455 };
456 
457 struct Service_GetTaskID
458 {
459     TaskRefType     TaskID;
460 };
461 
462 struct Service_GetTaskState
463 {
464     StatusType      TaskID;
465 };
466 
467 struct Service_DisableAllInterrupts
468 {
469     StatusType      TaskID;
470 };
471 
472 struct Service_EnableAllInterrupts
473 {
474     StatusType      TaskID;
475 };
476 
477 struct Service_SuspendAllInterrupts
478 {
479     StatusType      TaskID;
480 };
481 
482 struct Service_ResumeAllInterrupts
483 {
484     StatusType      TaskID;
485 };
486 
487 struct Service_SuspendOSInterrupts
488 {
489     StatusType      TaskID;
490 };
491 
492 struct Service_ResumeOSInterrupts
493 {
494     StatusType      TaskID;
495 };
496 
497 struct Service_GetResource
498 {
499     ResourceType     ResID;
500 };
501 
502 struct Service_ReleaseResource
503 {
504     ResourceType      ResID;
505 };
506 
507 struct Service_SetEvent
508 {
509     StatusType      EventID;
510     TaskType        TaskID;
511 };
512 
513 struct Service_ClearEvent
514 {
515     EventMaskType      EventID;
516 };
517 
518 struct Service_GetEvent
519 {
520     StatusType      EventID;
521     TaskType        TaskID;
522 };
523 
524 struct Service_WaitEvent
525 {
526     EventMaskType      EventID;
527 };
528 
529 struct Service_GetAlarmBase
530 {
531     AlarmType     AlarmID;
532 };
533 
534 struct Service_GetAlarm
535 {
536     AlarmType      AlarmID;
537 };
538 
539 struct Service_SetRelAlarm
540 {
541     AlarmType  AlarmID;
542     TickType   increment;
543     TickType   cycle;
544 };
545 
546 struct Service_SetAbsAlarm
547 {
548     AlarmType  AlarmID;
549     TickType   start;
550     TickType   cycle;
551 };
552 
553 struct Service_CancelAlarm
554 {
555     AlarmType  AlarmID;
556 };
557 
558 struct Service_GetActiveApplicationMode
559 {
560     StatusType      TaskID;
561 };
562 
563 struct Service_StartOS
564 {
565     StatusType      TaskID;
566 };
567 
568 struct Service_ShutdownOS
569 {
570     StatusType      TaskID;
571 };
572 
573 struct Service_GetServiceId
574 {
575     OsServiceIdType id;
576 };
577 
578 
579 /**************************************************************************/
580 /*                 OSEK Resource                                          */
581 /**************************************************************************/
582 
583 typedef struct osek_resource_struct
584 {
585      /* Name.  */
586     const  CHAR                 *name;
587 
588     /* This Resource is in use.  */
589     ULONG                       res_in_use;
590 
591     /* Ceiling priority of the resource.  */
592     UINT                        c_priority;
593 
594     /* Task occupying this resource.  */
595     TaskType                    taskid;
596 
597     /* Type.  */
598     StatusType                  type;            /* Internal, External, Linked.  */
599 
600     /* Linked Resource.  */
601 
602     ResourceType                linked_res;      /* id of the resource , Linked with this res.  */
603 
604     ResourceType                resolved_res;   /* Id of the res linked to this , after all chained links.  */
605 
606     /* Resource Object id.  */
607     ULONG                       osek_res_id;
608 
609 } OSEK_RESOURCE;
610 
611 
612 /**************************************************************************/
613 /*               Task  structure                                          */
614 /**************************************************************************/
615 
616 typedef struct osek_tcb_struct
617 {
618     /* This task's ThreadX TCB.  */
619     TX_THREAD                   task;
620 
621      /* Name.  */
622     const CHAR                  *name;
623 
624     /* This field indicates if this task is in use.  */
625     ULONG                       tcb_in_use;
626 
627     /* Task type BASIC or EXTENDED.  */
628     UINT                        task_type;
629 
630     /* Task AUTOSTART mode.  */
631     UINT                        task_autostart;
632 
633     /* Design time Scheduling policy of this Task.  */
634     SCHEDULE                    policy;
635 
636     /* Task start address (entry point).  */
637     FP                          task_entry;
638 
639     /* Design time task priority.  */
640     UINT                        org_prio;
641 
642 
643     /* Current ThreadX thread preemption threshold.  */
644     UINT                        cur_threshold;
645 
646     /* task stack size.  */
647     ULONG                       stack_size;
648 
649     /* start address of the task stack.  */
650     CHAR                        *pStackBase;
651 
652 
653     /* Task status Suspended/ Ready(Running).  */
654     ULONG                       suspended;
655 
656     /* Wait status */
657     ULONG                       waiting;
658 
659     /* Task to be activated when this task calls ChainTask().  */
660     TaskType                    task_to_chain;
661 
662     /* Counter to indicate how many Resources are occupied.  */
663     ULONG                       res_ocp;
664 
665     /* Maximum multiple activation allowed for this task.  */
666     UINT                        max_active;
667 
668     /* Current (recorded) multiple activation  requests.  */
669     UINT                        current_active;
670 
671     /* List of Event assigned to this task.  */
672     EventMaskType               events;
673 
674     EventMaskType               waiting_events;
675 
676     EventMaskType               set_events;
677 
678     /* List of External resources assigned (Design time) to this task.  */
679     ResourceType                external_resource_list[OSEK_MAX_EXTERNAL_RES];
680 
681     /* List of External Resources currently occupied (Run time) by this task.  */
682     ResourceType                external_resource_occuplied_list[OSEK_MAX_EXTERNAL_RES];
683 
684     /* List of Internal resources assigned (Design Time) to this task.  */
685     ResourceType                internal_resource_list[OSEK_MAX_INTERNAL_RES];
686 
687     /* List of Internal Resources currently occupied (Run Time)  by this task.  */
688     ResourceType                internal_resource_occuplied_list[OSEK_MAX_INTERNAL_RES];
689 
690     /* Internal Resource assigned or not.  */
691     UINT                        internal_res;
692 
693     /* RES_SCHEDULER taken or not.  */
694     UINT                        resource_scheduler;
695 
696     AppModeType                 task_Appl_Mode;
697 
698     /* Task Object id.  */
699     ULONG                       osek_task_id;
700 
701 } OSEK_TCB;
702 
703 
704 /**************************************************************************/
705 /*                 Counter                                                */
706 /**************************************************************************/
707 
708 typedef struct OSEK_COUNTER_STRUCT
709 {
710 
711     /* Max. allowable value.  */
712     TickType                    maxallowedvalue;
713 
714     /* Tick base multiplier.  */
715     TickType                    ticksperbase;
716 
717     /* Minimum cyclic numbers of counter ticks allowed for a cyclic alarm.  */
718     TickType                    mincycle;
719 
720      /* Name.  */
721     const CHAR*                 name;
722 
723     /* This field indicates if this counter is in use.  */
724     UINT                        cntr_in_use;
725 
726     /* Pre-count needed to increment main count by 1.  */
727     TickType                    sub_count;
728 
729     /* Current counter value in ticks.  */
730     TickType                    counter_value;
731 
732     /* Whether attached to system timer.  */
733     UINT                        system_timer;
734 
735     /* List of all alarms attached to this counter.  */
736     AlarmType                   alarm_list[OSEK_MAX_ALARMS];
737 
738     /* Counter object id.  */
739     ULONG                       osek_counter_id;
740 
741 
742 }OSEK_COUNTER;
743 
744 
745 /**************************************************************************/
746 /*                 ALARM                                                  */
747 /**************************************************************************/
748 
749 typedef struct OSEK_ALARM_STRUCT
750 {
751 
752      /* Name.  */
753     const CHAR*                 name;
754 
755     /* This field indicates if this entry is in use.  */
756     UINT                        alarm_in_use;
757 
758     UINT                        occupied;
759 
760     /* Max allowed value of count for this alarm depends on the counter to which this alarm is attached.  */
761     TickType                    max_allowed_value;
762 
763     /* Cycles programmed depends on the counter to which this alarm is attached.  */
764     TickType                    min_cyc;
765 
766     /* Tick base multiplier.  */
767     TickType                    ticks_per_base;
768 
769     /* Cycles programmed for this alarm.  */
770     TickType                    cycle;
771 
772     /* alarm expiration count.  */
773 
774     TickType                    expiration_count;
775 
776     /* Alarm Callback function.  */
777     void                        (*alarm_callback)();
778 
779     /* Alarm Activation.  */
780     UINT                        armed;
781 
782     /* Alarm Action.  */
783     UINT                        action;
784 
785     /* Attach task here.  */
786     OSEK_TCB*                   task;
787 
788     /* Attach the counter.  */
789     OSEK_COUNTER                *cntr;
790 
791     /* Event to set in case of SET_EVENT action. The event bits should be 1
792        to SET that EVENT of the task.  */
793     EventMaskType               events;
794 
795     /* Start up action.  */
796     UINT                        auto_start;
797 
798     /* ALARM armed in relative mode or abs mode.  */
799     UINT                        rel_abs_mode;
800 
801     /* Counter roll back flag for absolute alarm mode.  */
802     UINT                        counter_rollback;
803 
804     /* Alarm object id.  */
805     ULONG                       osek_alarm_id;
806 
807 } OSEK_ALARM;
808 
809 
810 typedef struct USER_ALARM_STRUCT
811 {
812 
813     /* Max allowed value of count.  */
814     TickType                    maxallowedvalue;
815 
816     /* Cycles.  */
817     TickType                    mincycle;
818 
819     /* Tick base multiplier.  */
820     TickType                    ticksperbase;
821 
822 }AlarmBaseType;
823 
824 typedef AlarmBaseType     *AlarmBaseRefType;
825 
826 
827 typedef struct REGISTER_APPLICATION_STRUCT
828 {
829 
830     AppModeType                 application_mode;
831 
832     /* ErrorHook.  */
833     void                        (*error_hook_handler)(StatusType);
834     /* Startup hook.  */
835     void                        (*startup_hook_handler)();
836     /* Shutdown Hook.  */
837     void                        (*shutdown_hook_handler)(StatusType);
838 
839     void                        (*pretask_hook_handler)(void);
840 
841     void                        (*posttask_hook_handler)(void);
842 
843     /* Any errors generated while creating the application.  */
844     StatusType                  osek_object_creation_error;
845 
846 
847     /* Application object ID.  */
848     ULONG                       osek_application_id;
849 
850 
851 }APPLICATION_INFO;
852 
853 typedef APPLICATION_INFO    *APPLICATION_INFO_PTR;
854 
855 
856 /**************************************************************************/
857 /*      OSEK API CALLS                                                    */
858 /**************************************************************************/
859 
860 
861 /* TASK MANAGEMENT */
862 
863 StatusType     ActivateTask(TaskType TaskId);
864 StatusType     GetTaskID(TaskType *TaskID);
865 StatusType     TerminateTask(void);
866 StatusType     ChainTask(TaskType TaskID);
867 StatusType     GetTaskState(TaskType TaskID, TaskStateRefType State);
868 TaskType       CreateTask(const CHAR *name, void(*entry_function)(), UINT priority, UINT max_activation,
869                             ULONG stack_size, SCHEDULE policy, AUTOSTART start, UINT, AppModeType mode);
870 StatusType     Schedule (void);
871 
872 
873 /* RESOURCE MANAGEMENT */
874 
875 ResourceType   CreateResource(const CHAR *name, StatusType type, ResourceType linked_res);
876 StatusType     GetResource(ResourceType id);
877 StatusType     ReleaseResource(ResourceType id);
878 StatusType     RegisterTasktoResource(ResourceType Resource, TaskType TaskID);
879 
880 /* EVENT MANAGEMENT   */
881 
882 StatusType     SetEvent(TaskType task_id, EventMaskType mask);
883 StatusType     ClearEvent(EventMaskType mask);
884 StatusType     GetEvent(TaskType task_id, EventMaskRefType event);
885 StatusType     WaitEvent(EventMaskType mask);
886 EventMaskType  CreateEvent(void);
887 StatusType     RegisterEventtoTask(EventType eventid, TaskType TaskID);
888 
889 /* INTERUUPT MANAGEMENT */
890 
891 StatusType     EnableInterrupt(void);
892 StatusType     DisableInterrupt(void);
893 
894 StatusType     GetInterruptDescriptor(UINT *mask);
895 
896 void           SuspendAllInterrupts(void);
897 void           ResumeAllInterrupts(void);
898 
899 void           SuspendOSInterrupts(void);
900 void           ResumeOSInterrupts(void);
901 
902 void           DisableAllInterrupts(void);
903 void           EnableAllInterrupts(void);
904 
905 ISRType        CreateISR(const CHAR *name, void(*entry_function)(), UINT category, ULONG stack_size);
906 StatusType     RegisterISRtoResource(ResourceType Resource, ISRType ISRID);
907 
908 
909 /* COUNTER MANAGEMENT  */
910 
911 StatusType     GetCounterValue(OSEK_COUNTER *counter_ptr, TickRefType ticks);
912 CounterType    CreateCounter(const CHAR *name, TickType max_allowed_value, TickType ticks_per_base,
913                              TickType min_cycle, TickType start_value);
914 StatusType     IncrCounter(CounterType cntr);
915 StatusType     DefineSystemCounter(CounterType cntr);
916 
917 /* ALARM MANAGEMENT */
918 
919 StatusType     GetAlarmBase(AlarmType AlarmID, AlarmBaseRefType info);
920 StatusType     SetAbsAlarm(AlarmType AlarmID, TickType start, TickType cycle);
921 StatusType     SetRelAlarm(AlarmType AlarmID, TickType increment, TickType cycle);
922 StatusType     CancelAlarm(AlarmType AlarmID);
923 StatusType     GetAlarm(AlarmType AlarmID, TickRefType tick_ptr);
924 AlarmType      CreateAlarm(const CHAR *name, CounterType cntr, UINT action, ULONG events,
925                        TaskType task, void (*callback)(), UINT Startup, TickType Alarmtime, TickType Cycle);
926 
927 /* OS MANAGEMENT */
928 
929 UCHAR         *osek_initialize(void *osek_memory, APPLICATION_INFO_PTR application1);
930 UINT           osek_cleanup(APPLICATION_INFO_PTR application1);
931 void           ShutdownOS(StatusType error);
932 void           StartOS(StatusType os_mode);
933 AppModeType    GetActiveApplicationMode(void);
934 
935 
936 void           process_ISR2(ISRType isrname);
937 
938 #endif
939 
940 
941 /******************************* End of file ************************/
942