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