]> rtime.felk.cvut.cz Git - arc.git/blob - include/Os.h
Again, loads of refactoring and removing and adding files.
[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 #if !defined(CC_KERNEL)\r
28 #include "Os_Cfg.h"\r
29 #endif\r
30 #include "MemMap.h"\r
31 #include "Cpu.h"\r
32 \r
33 typedef uint32_t                EventMaskType;\r
34 typedef EventMaskType * EventMaskRefType;\r
35 typedef uint16_t                TaskType;\r
36 typedef TaskType *              TaskRefType;\r
37 \r
38 typedef enum {\r
39         TASK_STATE_WAITING,\r
40         TASK_STATE_READY,\r
41         TASK_STATE_SUSPENDED,\r
42         TASK_STATE_RUNNING,\r
43 } TaskStateType;\r
44 \r
45 \r
46 typedef TaskStateType *TaskStateRefType;\r
47 \r
48 /* FIXME: OSMEMORY_IS__ , see 8.2*/\r
49 \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
141 void InitOS( void );
142 void StartOS( AppModeType Mode );
143
144 \r
145 \r
146 ApplicationType GetApplicationID( void );\r
147 ISRType GetISRID( void );\r
148
149 typedef int8_t Os_IntCounterType;
150 \r
151 /* requirements here: OS368(ISR2), OS092 */
152
153 extern Os_IntCounterType Os_IntDisableAllCnt;
154 extern Os_IntCounterType Os_IntSuspendAllCnt;
155 extern Os_IntCounterType Os_IntSuspendOsCnt;
156
157
158
159 /** @req OS299 */
160 /* OSEK: States that " service  does  not  support  nesting", but says
161  * nothing what to do about it.
162  *
163  * OS092: "  If EnableAllInterrupts()/ResumeAllInterrupts()/ResumeOSInterrupts()
164  * are called and no corresponding DisableAllInterupts()/SuspendAllInterrupts()/
165  * SuspendOSInterrupts()  was  done  before,  the Operating System shall not
166  * perform this OS service.
167  */
168 static inline void DisableAllInterrupts( void ) {
169         Irq_Disable();
170         Os_IntDisableAllCnt++;
171         /* No nesting allowed */
172         assert(Os_IntDisableAllCnt>1);
173 }
174
175 static inline void EnableAllInterrupts( void ) {
176
177         if(Os_IntDisableAllCnt==0) {
178                 /** @req OS092 EnableAllInterrupts */
179         } else {
180                 Os_IntDisableAllCnt--;
181                 Irq_Enable();
182         }
183 }
184
185 static inline void SuspendAllInterrupts( void ) {
186         Irq_SuspendAll();
187         Os_IntSuspendAllCnt++;
188 }
189
190 static inline void ResumeAllInterrupts( void ) {
191
192         if(Os_IntSuspendAllCnt==0) {
193                 /** @req OS092 ResumeAllInterrupts */
194         } else {
195                 Os_IntSuspendAllCnt--;
196                 Irq_ResumeAll();
197         }
198 }
199
200 /* Only ISR2 interrupts should be suspended but this can NEVER be
201  * done in a more efficient way than to disable all, so let's
202  * do that for all targets.
203  */
204
205 static inline void SuspendOSInterrupts( void ) {
206         Irq_SuspendOs();
207         Os_IntSuspendOsCnt++;
208 }
209
210 static inline void ResumeOSInterrupts( void ) {
211
212         if(Os_IntSuspendOsCnt==0) {
213                 /** @req OS092 ResumeOSInterrupts */
214         } else {
215                 Os_IntSuspendOsCnt--;
216                 Irq_ResumeOs();
217         }
218 }
219
220 #if 0
221 /* Can't find these anymore, removed from Autosar? */\r
222 StatusType EnableInterruptSource( ISRType EnableISR );\r
223 StatusType DisableInterruptSource( ISRType EnableISR );\r
224 #endif
225 \r
226 /*
227  * Class 2,3 and 4 API
228  */\r
229 ApplicationType GetApplicationID( void );\r
230 AccessType      CheckISRMemoryAccess(   ISRType ISRID,\r
231                                                                         MemoryStartAddressType Address,\r
232                                                                         MemorySizeType Size );\r
233 \r
234 AccessType      CheckTaskMemoryAccess(  TaskType TaskID,\r
235                                                                         MemoryStartAddressType Address,\r
236                                                                         MemorySizeType Size );
237 \r
238 StatusType      CallTrustedFunction(    TrustedFunctionIndexType FunctionIndex,\r
239                                                                         TrustedFunctionParameterRefType FunctionParams );\r
240 \r
241 StatusType      GetTaskID(              TaskRefType TaskID );\r
242 StatusType      GetTaskState(   TaskType task_id, TaskStateRefType state);\r
243 \r
244 void            ShutdownOS( StatusType );\r
245 StatusType      ActivateTask( TaskType TaskID );\r
246 StatusType      TerminateTask( void );\r
247 StatusType      ChainTask( TaskType TaskID );\r
248 StatusType      Schedule( void );\r
249 \r
250 typedef uint32 ResourceType;\r
251 #define DeclareResource(x) extern ResourceType (x);\r
252 StatusType GetResource( ResourceType ResID );\r
253 StatusType ReleaseResource( ResourceType ResID);\r
254
255 /*
256  * Define the scheduler resource as 0
257  */
258 #define RES_SCHEDULER                   0\r
259
260 /*
261  * Priorities of tasks and resources
262  */
263 #define OS_TASK_PRIORITY_MIN    0
264 #define OS_TASK_PRIORITY_MAX    31
265 /* Logical priority is higher higher than normal tasks */
266 #define OS_RES_SCHEDULER_PRIO   32\r
267 \r
268 typedef struct OsDriver_s {\r
269         int     OsGptChannelRef;\r
270 } OsDriver;\r
271 \r
272 /*-------------------------------------------------------------------\r
273  * Free running timer\r
274  *-----------------------------------------------------------------*/
275 typedef const uint32 OsTickType;
276 void Frt_Init( void );\r
277 void Frt_Start(uint32_t period_ticks);\r
278 uint32_t Frt_GetTimeElapsed( void );\r
279 \r
280 /*-------------------------------------------------------------------\r
281  * Counters\r
282  *-----------------------------------------------------------------*/\r
283 typedef sint16 CounterType;
284 \r
285 typedef uint32 TickType;\r
286 typedef TickType *TickRefType;\r
287 \r
288 StatusType IncrementCounter( CounterType );\r
289 StatusType GetCounterValue( CounterType, TickRefType );\r
290 StatusType GetElapsedCounterValue( CounterType, TickRefType val, TickRefType elapsed_val);\r
291 \r
292 /*-------------------------------------------------------------------\r
293  * Schedule Tables\r
294  *-----------------------------------------------------------------*/\r
295 \r
296 StatusType StartScheduleTableRel(ScheduleTableType sid, TickType offset);\r
297 StatusType StartScheduleTableAbs(ScheduleTableType sid, TickType val );\r
298 StatusType StartScheduleTableSynchron(ScheduleTableType sid );\r
299 StatusType StopScheduleTable(ScheduleTableType sid);\r
300 StatusType NextScheduleTable( ScheduleTableType sid_curr, ScheduleTableType sid_next);\r
301 StatusType SyncScheduleTable( ScheduleTableType sid, GlobalTimeTickType global_time  );\r
302 StatusType GetScheduleTableStatus( ScheduleTableType sid, ScheduleTableStatusRefType status );\r
303 StatusType SetScheduleTableAsync( ScheduleTableType sid );\r
304 \r
305 \r
306 /*-------------------------------------------------------------------\r
307  * Alarms\r
308  *-----------------------------------------------------------------*/\r
309 \r
310 typedef uint16 AlarmType;\r
311 \r
312 typedef struct {\r
313         TickType maxallowedvalue;\r
314         TickType tickperbase;\r
315         TickType mincycle;\r
316 } AlarmBaseType;\r
317 \r
318 typedef AlarmBaseType *AlarmBaseRefType;\r
319 \r
320 StatusType GetAlarmBase( AlarmType AlarmId, AlarmBaseRefType Info );\r
321 StatusType GetAlarm(AlarmType AlarmId, TickRefType Tick);\r
322 StatusType SetRelAlarm(AlarmType AlarmId, TickType Increment, TickType Cycle);\r
323 StatusType SetAbsAlarm(AlarmType AlarmId, TickType Start, TickType Cycle);\r
324 StatusType CancelAlarm(AlarmType AlarmId);\r
325 \r
326 \r
327 /*-------------------------------------------------------------------\r
328  * Error's\r
329  *-----------------------------------------------------------------*/\r
330 typedef enum {\r
331     OSServiceId_ActivateTask,\r
332     OSServiceId_TerminateTask,\r
333     OSServiceId_ChainTask,\r
334     OSServiceId_Schedule,\r
335     OSServiceId_GetTaskID,\r
336     OSServiceId_GetResource,\r
337     OSServiceId_ReleaseResource,\r
338     OSServiceId_SetEvent,\r
339     OSServiceId_ClearEvent,\r
340     OSServiceId_GetEvent,\r
341     OSServiceId_WaitEvent,\r
342     OSServiceId_GetAlarmBase,\r
343     OSServiceId_GetAlarm,\r
344     OSServiceId_SetRelAlarm,\r
345     OSServiceId_SetAbsAlarm,\r
346     OSServiceId_CancelAlarm,\r
347     OSServiceId_StartOS,\r
348     OSServiceId_ShutdownOS,\r
349     OSServiceId_ErrorHook,\r
350     OSServiceId_PreTaskHook,\r
351     OSServiceId_PostTaskHook,\r
352     OSServiceId_StartupHook,\r
353     OSServiceId_ShutdownHook,\r
354     OSServiceId_GetTaskState,\r
355 } OsServiceIdType;;\r
356 \r
357 typedef struct os_error_s {\r
358         OsServiceIdType serviceId;\r
359         uint32_t param1;\r
360         uint32_t param2;\r
361         uint32_t param3;\r
362 } os_error_t;\r
363 \r
364 extern os_error_t os_error;\r
365 \r
366 // TODO: Add the service id to all OS service methods.\r
367 static inline OsServiceIdType OSErrorGetServiceId(void)  {\r
368         return os_error.serviceId;\r
369 }\r
370 \r
371 extern os_error_t os_error;\r
372 \r
373 #define OSError_ActivateTask_TaskID ((TaskType) os_error.param1)\r
374 #define OSError_ChainTask_TaskID ((TaskType) os_error.param1)\r
375 #define OSError_GetTaskID_TaskID ((TaskRefType) os_error.param1)\r
376 #define OSError_GetResource_ResID ((ResourceType) os_error.param1)\r
377 #define OSError_ReleaseResource_ResID ((ResourceType) os_error.param1)\r
378 #define OSError_SetEvent_TaskID ((TaskType) os_error.param1)\r
379 #define OSError_SetEvent_Mask ((EventMaskType) os_error.param2)\r
380 #define OSError_ClearEvent_Mask ((EventMaskType) os_error.param1)\r
381 #define OSError_GetEvent_TaskId ((TaskType) os_error.param1)\r
382 #define OSError_GetEvent_Mask ((EventMaskRefType) os_error.param2)\r
383 #define OSError_WaitEvent_Mask ((EventMaskType) os_error.param1)\r
384 #define OSError_GetAlarmBase_AlarmId ((AlarmType) os_error.param1)\r
385 #define OSError_GetAlarmBase_Info ((AlarmBaseRefType) os_error.param2)\r
386 #define OSError_GetAlarm_AlarmId ((AlarmType) os_error.param1)\r
387 #define OSError_GetAlarm_Tick ((TickRefType) os_error.param2)\r
388 #define OSError_SetRelAlarm_AlarmId ((AlarmType) os_error.param1)\r
389 #define OSError_SetRelAlarm_Increment ((TickType) os_error.param2)\r
390 #define OSError_SetRelAlarm_Cycle ((TickType) os_error.param3)\r
391 #define OSError_SetAbsAlarm_AlarmId ((AlarmType) os_error.param1)\r
392 #define OSError_SetAbsAlarm_Start ((TickType) os_error.param2)\r
393 #define OSError_SetAbsAlarm_Cycle ((TickType) os_error.param3)\r
394 #define OSError_CancelAlarm_AlarmId ((AlarmType) os_error.param1)\r
395 #define OSError_StartOS_Mode ((AppModeType) os_error.param1)\r
396 #define OSError_ErrorHook_Error ((StatusType) os_error.param1)\r
397 #define OSError_ShutdownHook_Error ((StatusType) os_error.param1)\r
398 #define OSError_GetTaskState_TaskId ((TaskType) os_error.param1)\r
399 #define OSError_GetTaskState_State ((TaskStateRefType) os_error.param2)\r
400
401 /** @req OS398 */
402 #if defined(LOCALMESSAGESONLY)
403 #error LOCALMESSAGESONLY shall not be defined
404 #endif
405
406 \r
407 /*-------------------------------------------------------------------\r
408  * COM ( TODO : move )\r
409  *-----------------------------------------------------------------*/\r
410 \r
411 /*\r
412  * The only information about the COM that is valid is\r
413  * in the COM specification ..SWS_COM.pdf.\r
414  *\r
415  * The most important requirements are COM010 and COM013\r
416  *\r
417  * Com_Init()\r
418  * Com_DeInit()\r
419  *\r
420  * No error hooks..\r
421  * No. GetMessageStatus()\r
422  * No. SendZeroMessage()\r
423  * No. SendDynamicMessage(), RecieveDynamicMessage()\r
424  *\r
425  * Yes. SendMessage()\r
426  *\r
427  * */\r
428 \r
429 \r
430 typedef uint32 MessageType;\r
431 typedef void *ApplicationDataRef;\r
432 \r
433 \r
434 StatusType SendMessage( MessageType message_id, ApplicationDataRef dataRef );\r
435 StatusType ReceiveMessage( MessageType message_id, ApplicationDataRef dataRef );\r
436 \r
437 /*\r
438  * ArcCore extensions\r
439  */\r
440 TickType GetOsTick();\r
441 void OsTick(void);\r
442 void OsIdle(void);\r
443
444 #define OS_ISR_TYPE2    0\r
445 #define OS_ISR_TYPE1    1\r
446 \r
447 union isr_attr {\r
448         TaskType tid;\r
449         void (*entry)(void);\r
450 };\r
451 \r
452 #endif /*OS_H_*/\r