]> rtime.felk.cvut.cz Git - arc.git/blob - include/Os.h
Cleanup of makefiles. Cleanup of merge.
[arc.git] / include / Os.h
1 /* -------------------------------- Arctic Core ------------------------------\r
2  * Arctic Core - the open source AUTOSAR platform http://arccore.com\r
3  *\r
4  * Copyright (C) 2009  ArcCore AB <contact@arccore.com>\r
5  *\r
6  * This source code is free software; you can redistribute it and/or modify it\r
7  * under the terms of the GNU General Public License version 2 as published by the\r
8  * Free Software Foundation; See <http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt>.\r
9  *\r
10  * This program is distributed in the hope that it will be useful, but\r
11  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY\r
12  * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License\r
13  * for more details.\r
14  * -------------------------------- Arctic Core ------------------------------*/\r
15 \r
16 \r
17 \r
18 #ifndef OS_H_\r
19 #define OS_H_\r
20 \r
21 #define OS_SW_MAJOR_VERSION    2\r
22 #define OS_SW_MINOR_VERSION    0\r
23 #define OS_SW_PATCH_VERSION    0\r
24 \r
25 #include <assert.h>\r
26 #include "Std_Types.h"\r
27 #include "Os_Cfg.h"\r
28 #include "MemMap.h"\r
29 #include "Cpu.h"\r
30 \r
31 typedef uint8 StatusType;\r
32 \r
33 #define E_OS_ACCESS (StatusType)1               /**< STD OSEK */\r
34 #define E_OS_CALLEVEL (StatusType)2             /**< STD OSEK */\r
35 #define E_OS_ID (StatusType)3                   /**< STD OSEK */\r
36 #define E_OS_LIMIT (StatusType)4                /**< STD OSEK */\r
37 #define E_OS_NOFUNC (StatusType)5               /**< STD OSEK */\r
38 #define E_OS_RESOURCE (StatusType)6             /**< STD OSEK */\r
39 #define E_OS_STATE (StatusType)7                /**< STD OSEK */\r
40 #define E_OS_VALUE (StatusType)8                /**< STD OSEK */\r
41 \r
42 #define E_OS_SERVICEID (StatusType)9                /**< AUTOSAR, see 7.10 */\r
43 #define E_OS_RATE (StatusType)10                    /**< AUTOSAR, see 7.10 */\r
44 #define E_OS_ILLEGAL_ADDRESS (StatusType)11         /**< AUTOSAR, see 7.10 */\r
45 #define E_OS_MISSINGEND (StatusType)12              /**< AUTOSAR, see 7.10 */\r
46 #define E_OS_DISABLEDINT (StatusType)13             /**< AUTOSAR, see 7.10 */\r
47 #define E_OS_STACKFAULT (StatusType)14              /**< AUTOSAR, see 7.10 */\r
48 #define E_OS_PROTECTION_MEMORY (StatusType)15       /**< AUTOSAR, see 7.10 */\r
49 #define E_OS_PROTECTION_TIME (StatusType)16         /**< AUTOSAR, see 7.10 */\r
50 #define E_OS_PROTECTION_LOCKED (StatusType)17       /**< AUTOSAR, see 7.10 */\r
51 #define E_OS_PROTECTION_EXCEPTION (StatusType)18    /**< AUTOSAR, see 7.10 */\r
52 #define E_OS_PROTECTION_RATE (StatusType)19          /**< AUTOSAR, see 7.10 */\r
53 \r
54 #define E_COM_ID 255 // TODO: var ska E_COM_ID vara?"\r
55 \r
56 \r
57 typedef uint32_t                EventMaskType;\r
58 typedef EventMaskType * EventMaskRefType;\r
59 typedef uint16_t                TaskType;\r
60 typedef TaskType *              TaskRefType;\r
61 \r
62 typedef enum {\r
63         TASK_STATE_WAITING,\r
64         TASK_STATE_READY,\r
65         TASK_STATE_SUSPENDED,\r
66         TASK_STATE_RUNNING\r
67 } TaskStateType;\r
68 \r
69 #define INVALID_TASK    0xdeadU\r
70 \r
71 typedef TaskStateType *TaskStateRefType;\r
72 \r
73 /* FIXME: OSMEMORY_IS__ , see 8.2*/\r
74 \r
75 #define OSMEMORY_IS_READABLE(_access)\r
76 #define OSMEMORY_IS_WRITEABLE(_access)\r
77 #define OSMEMORY_IS_EXECUTABLE(_access)\r
78 #define OSMEMORY_IS_STACKSPACE(_access)\r
79 \r
80 #define OSDEFAULTAPPMODE  1\r
81 \r
82 #define INVALID_OSAPPLICATION (-1)\r
83 \r
84 /* TODO, I have no idea what this should be*/\r
85 #if (OS_USE_APPLICATIONS == STD_ON)\r
86 typedef sint32 ApplicationType;\r
87 \r
88 typedef enum {\r
89         APPLICATION_ACCESSIBLE,\r
90         APPLICATION_RESTARTING,\r
91         APPLICATION_TERMINATED\r
92 } ApplicationStateType;\r
93 \r
94 typedef ApplicationStateType *ApplicationStateRefType;\r
95 #endif\r
96 \r
97 /* See oil config for defines */\r
98 typedef sint32 AppModeType;\r
99 \r
100 /* FIXME: more types here */\r
101 typedef uint16 ScheduleTableType;\r
102 typedef uint16 GlobalTimeTickType;\r
103 \r
104 typedef enum {\r
105         SCHEDULETABLE_STOPPED,\r
106         SCHEDULETABLE_NEXT,\r
107         SCHEDULETABLE_WAITING,\r
108         SCHEDULETABLE_RUNNING,\r
109         SCHEDULETABLE_RUNNING_AND_SYNCHRONOUS\r
110 } ScheduleTableStatusType;\r
111 \r
112 typedef ScheduleTableStatusType *ScheduleTableStatusRefType;\r
113 \r
114 /* Flags for AccessType */\r
115 #define ACCESSTYPE_W    1\r
116 #define ACCESSTYPE_R    (1<<1)\r
117 \r
118 typedef uint32 AccessType;\r
119 \r
120 typedef enum {\r
121         ACCESS,\r
122         NO_ACCESS\r
123 } ObjectAccessType;\r
124 \r
125 typedef enum {\r
126         OBJECT_TASK,\r
127         OBJECT_ISR,\r
128         OBJECT_ALARM,\r
129         OBJECT_RESOURCE,\r
130         OBJECT_COUNTER,\r
131         OBJECT_SCHEDULETABLE,\r
132         OBJECT_MESSAGE\r
133 } ObjectTypeType;\r
134 \r
135 typedef enum {\r
136         PRO_KILLTASKISR,\r
137         PRO_KILLAPPL,\r
138         PRO_KILLAPPL_RESTART,\r
139         PRO_SHUTDOWN\r
140 } ProtectionReturnType;\r
141 \r
142 typedef enum {\r
143         RESTART,\r
144         NO_RESTART\r
145 } RestartType;\r
146 \r
147 \r
148 typedef ProtectionReturnType (*ProtectionHookType)( StatusType);\r
149 typedef void (*StartupHookType)( void );\r
150 typedef void (*ShutdownHookType)( StatusType );\r
151 typedef void (*ErrorHookType)( StatusType );\r
152 typedef void (*PreTaskHookType)( void );\r
153 typedef void (*PostTaskHookType)( void );\r
154 \r
155 ProtectionReturnType ProtectionHook( StatusType FatalError );\r
156 void StartupHook( void );\r
157 void ShutdownHook( StatusType Error );\r
158 void ErrorHook( StatusType Error );\r
159 void PreTaskHook( void );\r
160 void PostTaskHook( void );\r
161 \r
162 typedef uint16 TrustedFunctionIndexType;\r
163 typedef  void * TrustedFunctionParameterRefType;\r
164 \r
165 /* See 8.3.9 */\r
166 #define INVALID_ISR             ((sint16)(-1))\r
167 typedef sint16 ISRType;\r
168 \r
169 #define APP_NO_OWNER    (-1UL)\r
170 \r
171 typedef void * MemoryStartAddressType;\r
172 typedef uint32 MemorySizeType;\r
173 \r
174 #if 0\r
175 #define WaitEvent(...) SC_CALL(WaitEvent,1,1,__VA_ARGS__)\r
176 #define SetEvent(...) SC_CALL(SetEvent,2,2,__VA_ARGS__)\r
177 #define ClearEvent(...) SC_CALL(ClearEvent,3,1,__VA_ARGS__)\r
178 #endif\r
179 StatusType WaitEvent( EventMaskType Mask );\r
180 StatusType SetEvent( TaskType TaskID, EventMaskType Mask );\r
181 StatusType ClearEvent( EventMaskType Mask);\r
182 StatusType GetEvent( TaskType TaskId, EventMaskRefType Mask);\r
183 \r
184 void InitOS( void );\r
185 void StartOS( AppModeType Mode );\r
186 \r
187 \r
188 \r
189 ISRType GetISRID( void );\r
190 StatusType GetActiveApplicationMode( AppModeType* mode);\r
191 \r
192 typedef int8_t Os_IntCounterType;\r
193 \r
194 /* requirements here: OS368(ISR2), OS092 */\r
195 \r
196 extern Os_IntCounterType Os_IntDisableAllCnt;\r
197 extern Os_IntCounterType Os_IntSuspendAllCnt;\r
198 extern Os_IntCounterType Os_IntSuspendOsCnt;\r
199 \r
200 \r
201 \r
202 /** @req OS299 */\r
203 /* OSEK: States that " service  does  not  support  nesting", but says\r
204  * nothing what to do about it.\r
205  *\r
206  * OS092: "  If EnableAllInterrupts()/ResumeAllInterrupts()/ResumeOSInterrupts()\r
207  * are called and no corresponding DisableAllInterupts()/SuspendAllInterrupts()/\r
208  * SuspendOSInterrupts()  was  done  before,  the Operating System shall not\r
209  * perform this OS service.\r
210  */\r
211 static inline void DisableAllInterrupts( void ) {\r
212         Irq_Disable();\r
213         Os_IntDisableAllCnt++;\r
214         /* No nesting allowed */\r
215         assert(Os_IntDisableAllCnt==1);\r
216 }\r
217 \r
218 static inline void EnableAllInterrupts( void ) {\r
219 \r
220         if(Os_IntDisableAllCnt==0) {\r
221                 /** @req OS092 EnableAllInterrupts */\r
222         } else {\r
223                 Os_IntDisableAllCnt--;\r
224                 Irq_Enable();\r
225         }\r
226 }\r
227 \r
228 static inline void SuspendAllInterrupts( void ) {\r
229         Irq_SuspendAll();\r
230         Os_IntSuspendAllCnt++;\r
231 }\r
232 \r
233 static inline void ResumeAllInterrupts( void ) {\r
234 \r
235         if(Os_IntSuspendAllCnt==0) {\r
236                 /** @req OS092 ResumeAllInterrupts */\r
237         } else {\r
238                 Os_IntSuspendAllCnt--;\r
239                 Irq_ResumeAll();\r
240         }\r
241 }\r
242 \r
243 /* Only ISR2 interrupts should be suspended but this can NEVER be\r
244  * done in a more efficient way than to disable all, so let's\r
245  * do that for all targets.\r
246  */\r
247 \r
248 static inline void SuspendOSInterrupts( void ) {\r
249         Irq_SuspendOs();\r
250         Os_IntSuspendOsCnt++;\r
251 }\r
252 \r
253 static inline void ResumeOSInterrupts( void ) {\r
254 \r
255         if(Os_IntSuspendOsCnt==0) {\r
256                 /** @req OS092 ResumeOSInterrupts */\r
257         } else {\r
258                 Os_IntSuspendOsCnt--;\r
259                 Irq_ResumeOs();\r
260         }\r
261 }\r
262 \r
263 /*\r
264  * Class 2,3 and 4 API\r
265  */\r
266 \r
267 #if (OS_USE_APPLICATIONS == STD_ON)\r
268 \r
269 ApplicationType GetApplicationID( void );\r
270 AccessType      CheckISRMemoryAccess(   ISRType ISRID,\r
271                                                                         MemoryStartAddressType Address,\r
272                                                                         MemorySizeType Size );\r
273 \r
274 AccessType      CheckTaskMemoryAccess(  TaskType TaskID,\r
275                                                                         MemoryStartAddressType Address,\r
276                                                                         MemorySizeType Size );\r
277 \r
278 ObjectAccessType CheckObjectAccess( ApplicationType ApplId,\r
279                                                                         ObjectTypeType ObjectType,\r
280                                                                         uint32_t objectId );\r
281 ApplicationType CheckObjectOwnership( ObjectTypeType ObjectType,\r
282                                                                         uint32_t objectId );\r
283 StatusType TerminateApplication(  ApplicationType Application, RestartType RestartOption );\r
284 StatusType AllowAccess( void );\r
285 StatusType GetApplicationState(   ApplicationType Application,  ApplicationStateRefType Value );\r
286 StatusType      CallTrustedFunction(    TrustedFunctionIndexType FunctionIndex,\r
287                                                                         TrustedFunctionParameterRefType FunctionParams );\r
288 #endif\r
289 \r
290 StatusType      GetTaskID(              TaskRefType TaskID );\r
291 StatusType      GetTaskState(   TaskType task_id, TaskStateRefType state);\r
292 \r
293 void            ShutdownOS( StatusType );\r
294 StatusType      ActivateTask( TaskType TaskID );\r
295 StatusType      TerminateTask( void );\r
296 StatusType      ChainTask( TaskType TaskID );\r
297 StatusType      Schedule( void );\r
298 \r
299 typedef uint8 ResourceType;\r
300 #define DeclareResource(x) extern ResourceType (x);\r
301 StatusType GetResource( ResourceType ResID );\r
302 StatusType ReleaseResource( ResourceType ResID);\r
303 \r
304 /*\r
305  * Define scheduler as topmost\r
306  */\r
307 #define RES_SCHEDULER                   OS_RESOURCE_CNT\r
308 \r
309 /*\r
310  * Priorities of tasks and resources\r
311  */\r
312 #define OS_TASK_PRIORITY_MIN    0\r
313 #define OS_TASK_PRIORITY_MAX    31\r
314 /* Logical priority is higher higher than normal tasks */\r
315 #define OS_RES_SCHEDULER_PRIO   32\r
316 \r
317 typedef struct OsDriver_s {\r
318         int     OsGptChannelRef;\r
319 } OsDriver;\r
320 \r
321 \r
322 \r
323 /*-------------------------------------------------------------------\r
324  * Counters\r
325  *-----------------------------------------------------------------*/\r
326 typedef sint16 CounterType;\r
327 \r
328 typedef uint32 TickType;\r
329 typedef TickType *TickRefType;\r
330 \r
331 StatusType IncrementCounter( CounterType );\r
332 StatusType GetCounterValue( CounterType, TickRefType );\r
333 StatusType GetElapsedCounterValue( CounterType, TickRefType val, TickRefType elapsed_val);\r
334 \r
335 \r
336 /*-------------------------------------------------------------------\r
337  * System timer\r
338  *-----------------------------------------------------------------*/\r
339 typedef const uint32 OsTickType;\r
340 void Os_SysTickInit( void );\r
341 void Os_SysTickStart(TickType period_ticks);\r
342 TickType Os_SysTickGetValue( void );\r
343 TickType Os_SysTickGetElapsedValue( TickType preValue );\r
344 \r
345 /*-------------------------------------------------------------------\r
346  * Schedule Tables\r
347  *-----------------------------------------------------------------*/\r
348 \r
349 StatusType StartScheduleTableRel(ScheduleTableType sid, TickType offset);\r
350 StatusType StartScheduleTableAbs(ScheduleTableType sid, TickType val );\r
351 StatusType StartScheduleTableSynchron(ScheduleTableType sid );\r
352 StatusType StopScheduleTable(ScheduleTableType sid);\r
353 StatusType NextScheduleTable( ScheduleTableType sid_curr, ScheduleTableType sid_next);\r
354 StatusType SyncScheduleTable( ScheduleTableType sid, GlobalTimeTickType global_time  );\r
355 StatusType GetScheduleTableStatus( ScheduleTableType sid, ScheduleTableStatusRefType status );\r
356 StatusType SetScheduleTableAsync( ScheduleTableType sid );\r
357 \r
358 \r
359 /*-------------------------------------------------------------------\r
360  * Alarms\r
361  *-----------------------------------------------------------------*/\r
362 \r
363 typedef uint16 AlarmType;\r
364 \r
365 typedef struct {\r
366         TickType maxallowedvalue;\r
367         TickType tickperbase;\r
368         TickType mincycle;\r
369 } AlarmBaseType;\r
370 \r
371 typedef AlarmBaseType *AlarmBaseRefType;\r
372 \r
373 StatusType GetAlarmBase( AlarmType AlarmId, AlarmBaseRefType Info );\r
374 StatusType GetAlarm(AlarmType AlarmId, TickRefType Tick);\r
375 StatusType SetRelAlarm(AlarmType AlarmId, TickType Increment, TickType Cycle);\r
376 StatusType SetAbsAlarm(AlarmType AlarmId, TickType Start, TickType Cycle);\r
377 StatusType CancelAlarm(AlarmType AlarmId);\r
378 \r
379 \r
380 /*-------------------------------------------------------------------\r
381  * Error's\r
382  *-----------------------------------------------------------------*/\r
383 typedef enum {\r
384     OSServiceId_ActivateTask,\r
385     OSServiceId_TerminateTask,\r
386     OSServiceId_ChainTask,\r
387     OSServiceId_Schedule,\r
388     OSServiceId_GetTaskID,\r
389     OSServiceId_GetResource,\r
390     OSServiceId_ReleaseResource,\r
391     OSServiceId_SetEvent,\r
392     OSServiceId_ClearEvent,\r
393     OSServiceId_GetEvent,\r
394     OSServiceId_WaitEvent,\r
395     OSServiceId_GetAlarmBase,\r
396     OSServiceId_GetAlarm,\r
397     OSServiceId_SetRelAlarm,\r
398     OSServiceId_SetAbsAlarm,\r
399     OSServiceId_CancelAlarm,\r
400     OSServiceId_StartOS,\r
401     OSServiceId_ShutdownOS,\r
402     OSServiceId_ErrorHook,\r
403     OSServiceId_PreTaskHook,\r
404     OSServiceId_PostTaskHook,\r
405     OSServiceId_StartupHook,\r
406     OSServiceId_ShutdownHook,\r
407     OSServiceId_GetTaskState\r
408 } OsServiceIdType;\r
409 \r
410 typedef struct OsError {\r
411         OsServiceIdType serviceId;\r
412         uint32_t param1;\r
413         uint32_t param2;\r
414         uint32_t param3;\r
415 } OsErrorType;\r
416 \r
417 extern OsErrorType os_error;\r
418 \r
419 // TODO: Add the service id to all OS service methods.\r
420 static inline OsServiceIdType OSErrorGetServiceId(void)  {\r
421         return os_error.serviceId;\r
422 }\r
423 \r
424 extern OsErrorType os_error;\r
425 \r
426 #define OSError_ActivateTask_TaskID ((TaskType) os_error.param1)\r
427 #define OSError_ChainTask_TaskID ((TaskType) os_error.param1)\r
428 #define OSError_GetTaskID_TaskID ((TaskRefType) os_error.param1)\r
429 #define OSError_GetResource_ResID ((ResourceType) os_error.param1)\r
430 #define OSError_ReleaseResource_ResID ((ResourceType) os_error.param1)\r
431 #define OSError_SetEvent_TaskID ((TaskType) os_error.param1)\r
432 #define OSError_SetEvent_Mask ((EventMaskType) os_error.param2)\r
433 #define OSError_ClearEvent_Mask ((EventMaskType) os_error.param1)\r
434 #define OSError_GetEvent_TaskId ((TaskType) os_error.param1)\r
435 #define OSError_GetEvent_Mask ((EventMaskRefType) os_error.param2)\r
436 #define OSError_WaitEvent_Mask ((EventMaskType) os_error.param1)\r
437 #define OSError_GetAlarmBase_AlarmId ((AlarmType) os_error.param1)\r
438 #define OSError_GetAlarmBase_Info ((AlarmBaseRefType) os_error.param2)\r
439 #define OSError_GetAlarm_AlarmId ((AlarmType) os_error.param1)\r
440 #define OSError_GetAlarm_Tick ((TickRefType) os_error.param2)\r
441 #define OSError_SetRelAlarm_AlarmId ((AlarmType) os_error.param1)\r
442 #define OSError_SetRelAlarm_Increment ((TickType) os_error.param2)\r
443 #define OSError_SetRelAlarm_Cycle ((TickType) os_error.param3)\r
444 #define OSError_SetAbsAlarm_AlarmId ((AlarmType) os_error.param1)\r
445 #define OSError_SetAbsAlarm_Start ((TickType) os_error.param2)\r
446 #define OSError_SetAbsAlarm_Cycle ((TickType) os_error.param3)\r
447 #define OSError_CancelAlarm_AlarmId ((AlarmType) os_error.param1)\r
448 #define OSError_StartOS_Mode ((AppModeType) os_error.param1)\r
449 #define OSError_ErrorHook_Error ((StatusType) os_error.param1)\r
450 #define OSError_ShutdownHook_Error ((StatusType) os_error.param1)\r
451 #define OSError_GetTaskState_TaskId ((TaskType) os_error.param1)\r
452 #define OSError_GetTaskState_State ((TaskStateRefType) os_error.param2)\r
453 \r
454 /** @req OS398 */\r
455 #if defined(LOCALMESSAGESONLY)\r
456 #error LOCALMESSAGESONLY shall not be defined\r
457 #endif\r
458 \r
459 \r
460 /*-------------------------------------------------------------------\r
461  * COM ( TODO : move )\r
462  *-----------------------------------------------------------------*/\r
463 \r
464 /*\r
465  * The only information about the COM that is valid is\r
466  * in the COM specification ..SWS_COM.pdf.\r
467  *\r
468  * The most important requirements are COM010 and COM013\r
469  *\r
470  * Com_Init()\r
471  * Com_DeInit()\r
472  *\r
473  * No error hooks..\r
474  * No. GetMessageStatus()\r
475  * No. SendZeroMessage()\r
476  * No. SendDynamicMessage(), RecieveDynamicMessage()\r
477  *\r
478  * Yes. SendMessage()\r
479  *\r
480  * */\r
481 \r
482 \r
483 typedef uint32 MessageType;\r
484 typedef void *ApplicationDataRef;\r
485 \r
486 \r
487 StatusType SendMessage( MessageType message_id, ApplicationDataRef dataRef );\r
488 StatusType ReceiveMessage( MessageType message_id, ApplicationDataRef dataRef );\r
489 \r
490 /*\r
491  * ArcCore extensions\r
492  */\r
493 TickType GetOsTick();\r
494 void OsTick(void);\r
495 void OsIdle(void);\r
496 \r
497 #define OS_ISR_TYPE2    0\r
498 #define OS_ISR_TYPE1    1\r
499 \r
500 union isr_attr {\r
501         TaskType tid;\r
502         void (*entry)(void);\r
503 };\r
504 \r
505 #define TTY_NONE                1\r
506 #define TTY_T32         2\r
507 #define TTY_WINIDEA     4\r
508 #define TTY_SERIAL              8\r
509 #define RAMLOG          16\r
510 \r
511 #endif /*OS_H_*/\r