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