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