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