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