]> rtime.felk.cvut.cz Git - arc.git/blob - system/kernel/include/internal.h
Again, loads of refactoring and removing and adding files.
[arc.git] / system / kernel / include / internal.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 #ifndef INTERNAL_H_\r
17 #define INTERNAL_H_
18
19 /* Using internal.h
20  *
21  * Os.h
22  *  |
23  *  |--- Os_Cfg.h
24  *  |
25  *  |--- Std_Types.h
26  *  |    |--- Platform_Types.h (std?)
27  *  |    |--- Compiler.h       (std?)
28  *  |
29  *  |--- MemMap.h
30  *
31  *
32  * task.c
33  *  |--- Os.h
34  *  |--- internal.h
35  *
36  *
37  *
38  *           kernel.h (shared types between config and OS source... pointless?)
39  *              |
40  *     |------------------|
41  *  Os_Cfg.c          internal.h
42  *
43  *
44  * API's
45  * -------------------------------------
46  * Os.h                                 - OS API and types
47  * os_config_func.h     - Inline API for Os_Cfg.c
48  * ext_config.h         - API for os_config_func.h, used by kernel
49  * internal.h                   - Internal API for kernel, do NOT expose outside kernel
50  *
51  * os_types.h           - Internal types for the kernel
52  *
53  *
54  */
55
56
57
58
59 #include <assert.h>
60 #include <stdint.h>
61 #include <stdlib.h>
62
63 extern uint32_t os_dbg_mask;\r
64 \r
65 /*\r
66  * 0  master print normal, 1-print\r
67  * 1  master print isr     1-print\r
68  * 2  normal 0-stdout,1-ramlog\r
69  * 3  isr    0-stdout,\r
70  *\r
71  * 16 task_low\r
72  * 17 task high\r
73  *\r
74  * 20 alarm\r
75  *\r
76  *\r
77  * So when debugging the kernel using the ISS you want to use:\r
78  * 0xB\r
79  *\r
80  * Ramlog all the way:\r
81  * 0x7\r
82  */\r
83 \r
84 extern uint32_t os_dbg_mask;\r
85 \r
86 #define STR_TASK                "OS_TASK"\r
87 #define STR_ALARM               "OS_ALARM"\r
88 #define STR_STBL                "OS_STBL"\r
89 \r
90 \r
91 #define os_dbg_printf(format,...) \\r
92                         if( os_dbg_mask & OS_DBG_MASTER_PRINT ) { \\r
93                                         simple_printf(format,## __VA_ARGS__ );  \\r
94                         }\r
95 \r
96 #define os_dbg_isr_printf(format,...) \\r
97                         if( os_dbg_mask & OS_DBG_ISR_MASTER_PRINT ) { \\r
98                                         simple_printf(format,## __VA_ARGS__ );  \\r
99                         }\r
100 \r
101 #define os_isr_printf(_mask,format,...) \\r
102                         if( (os_dbg_mask & OS_DBG_ISR_MASTER_PRINT) && ((_mask)>255 ) ) { \\r
103                                 if( os_dbg_mask & D_ISR_STDOUT ) { \\r
104                                         simple_printf("[%08d] : ",GetOsTick()); \\r
105                                         simple_printf(format,## __VA_ARGS__ );  \\r
106                                 } else {                                    \\r
107                                         ramlog_printf("[%08d] : ",GetOsTick()); \\r
108                                         ramlog_printf(format,## __VA_ARGS__ );  \\r
109                                 }                                           \\r
110                         }\r
111 \r
112 #define os_std_printf(_mask,format,...) \\r
113                 if( (os_dbg_mask & OS_DBG_MASTER_PRINT) && ((_mask)>255 ) ) { \\r
114                         if( os_dbg_mask & D_STDOUT) { \\r
115                                 simple_printf("[%08d] : ",GetOsTick()); \\r
116                                 simple_printf(format,## __VA_ARGS__ );  \\r
117                         } else {                                    \\r
118                                 ramlog_printf("[%08d] : ",GetOsTick()); \\r
119                                 ramlog_printf(format,## __VA_ARGS__ );  \\r
120                         }                                           \\r
121                 }\r
122
123
124 #include "kernel.h"
125 #include "task_i.h"
126 #include "ext_config.h"
127
128
129
130 /*
131  * Macros for error handling
132  * Registers service id of the erroneous function and the applicable parameters
133  * to os_error. Functions that have less than three parameters do not touch
134  * os_error.param3. Same rule follows for other parameter counts.
135  */
136
137 /* Error handling for functions that take no arguments */
138 #define OS_STD_END(_service_id) \
139         goto ok;        \
140     err:                \
141         os_error.serviceId=_service_id;\
142         ERRORHOOK(rv);  \
143     ok:                 \
144         return rv;
145
146 /* Error handling for functions that take one argument */
147 #define OS_STD_END_1(_service_id, _p1) \
148         goto ok;        \
149     err:                \
150     os_error.serviceId=_service_id;\
151         os_error.param1 = (uint32_t) _p1; \
152         ERRORHOOK(rv);  \
153     ok:                 \
154         return rv;
155
156 /* Error handling for functions that take two arguments */
157 #define OS_STD_END_2(_service_id, _p1,_p2) \
158         goto ok;        \
159     err:                \
160         os_error.serviceId=_service_id;\
161         os_error.param1 = (uint32_t) _p1; \
162         os_error.param2 = (uint32_t) _p2; \
163         ERRORHOOK(rv);  \
164     ok:                 \
165         return rv;
166
167 /* Error handling for functions that take three arguments */
168 #define OS_STD_END_3(_service_id,_p1,_p2,_p3) \
169         goto ok;        \
170     err:                \
171         os_error.serviceId=_service_id;\
172         os_error.param1 = (uint32_t) _p1; \
173         os_error.param2 = (uint32_t) _p2; \
174         os_error.param3 = (uint32_t) _p3; \
175         ERRORHOOK(rv);  \
176     ok:                 \
177         return rv;
178
179
180
181 /* Called for sequence of error hook calls in case a service
182  * does not return with E_OK. Note that in this case the general error hook and the OS-
183  * Application specific error hook are called.
184  */
185
186 #define ERRORHOOK(x) \
187         if( os_sys.hooks->ErrorHook != NULL  ) { \
188                 os_sys.hooks->ErrorHook(x); \
189         }
190
191
192 #define PRETASKHOOK() \
193         if( os_sys.hooks->PreTaskHook != NULL ) { \
194                 os_sys.hooks->PreTaskHook(); \
195         }
196
197 #define POSTTASKHOOK() \
198         if( os_sys.hooks->PostTaskHook != NULL ) {      \
199                 os_sys.hooks->PostTaskHook();                   \
200         }
201 \r
202 /*\r
203  * PCB manipulating functions\r
204  */\r
205 \r
206 static inline OsTaskidType get_curr_pid( void ) {\r
207         return os_sys.curr_pcb->pid;\r
208 }\r
209 \r
210 static inline OsPcbType *get_curr_pcb( void ) {\r
211         return os_sys.curr_pcb;\r
212 }\r
213 \r
214 static inline void set_curr_pcb( OsPcbType *pcb ) {\r
215         os_sys.curr_pcb = pcb;\r
216 }\r
217 \r
218 static inline _Bool is_idle_task( OsPcbType *pcb ){\r
219         return (pcb->pid == 0);\r
220 }\r
221 \r
222 static inline OsTaskidType get_curr_prio( void ){\r
223         return os_sys.curr_pcb->prio;\r
224 }\r
225 \r
226 static inline TickType get_os_tick( void ) {\r
227         return os_sys.tick;\r
228 }\r
229
230 #if ( OS_SC1 == STD_ON ) || ( OS_SC4 == STD_ON )\r
231 static inline OsApplicationType *get_curr_application( void ) {\r
232         return get_curr_pcb()->application;\r
233 }\r
234 \r
235 static inline uint32_t get_curr_application_id( void ) {\r
236         return get_curr_pcb()->application->application_id;\r
237 }\r
238 #endif
239 \r
240 static inline struct OsResource *os_get_resource_int_p( void ) {\r
241         return get_curr_pcb()->resource_int_p;\r
242 }\r
243 \r
244 /*\r
245  * Misc
246  */\r
247 \r
248 static inline uint32_t os_task_nr_to_mask( uint32_t nr ) {\r
249         return (1<<nr);\r
250 }\r
251 \r
252 // task_i.c\r
253 OsPcbType *Os_TaskGetTop( void );\r
254 OsPcbType *os_find_task( TaskType tid );\r
255 \r
256 // resource.c\r
257 void Os_ResourceGetInternal(void );\r
258 void Os_ResourceReleaseInternal( void );
259
260
261 static inline void Os_ResourceCheckAndRelease( OsPcbType *pcb )  {
262         if( !TAILQ_EMPTY(&pcb->resource_head) ) {
263                 OsResourceType *rPtr;
264
265                 TAILQ_FOREACH(rPtr, &pcb->resource_head, listEntry ) {
266                         ReleaseResource(rPtr->nr);
267                         /* Requirements are a little fuzzy here, no explicit
268                          * requirement for this.
269                          */
270                         ERRORHOOK(E_OS_RESOURCE);
271                 }
272         }
273 }
274
275 \r
276 // Create.c\r
277 OsPcbType * os_alloc_new_pcb( void );\r
278
279 void os_dispatch(void);\r
280 \r
281 void OsTick( void );\r
282 \r
283 void *Os_Isr( void *stack, void *pcb_p );
284 void Os_Dispatch( _Bool force );
285
286 #define STACK_PATTERN   0x42
287
288 static inline void *Os_StackGetUsage( OsPcbType *pcb ) {
289
290         uint8_t *p = pcb->stack.curr;
291         uint8_t *end = pcb->stack.top;
292
293         while( (*end == STACK_PATTERN) && (end<p)) {
294                         end++;
295                 }
296         return (void *)end;
297 }
298
299 static inline void Os_StackSetEndmark( OsPcbType *pcbPtr ) {
300         uint8_t *end = pcbPtr->stack.top;
301         *end = STACK_PATTERN;
302 }
303
304 static inline _Bool Os_StackIsEndmarkOk( OsPcbType *pcbPtr ) {
305         uint8_t *end = pcbPtr->stack.top;
306         return ( *end == STACK_PATTERN);
307 }
308
309
310 int Oil_GetTaskCnt(void);
311 void Os_ContextReInit( OsPcbType *pcbPtr );
312
313
314 static inline _Bool Os_IrqAnyDisabled( void ) {
315         return ((Os_IntDisableAllCnt | Os_IntSuspendAllCnt | Os_IntSuspendOsCnt) != 0);
316 }
317
318 static inline void Os_IrqClearAll( void ) {
319         Os_IntDisableAllCnt = 0;
320         Os_IntSuspendAllCnt = 0;
321         Os_IntSuspendOsCnt = 0;
322 }
323
324 \r
325 #endif /*INTERNAL_H_*/\r