]> rtime.felk.cvut.cz Git - arc.git/blob - system/kernel/init.c
rebase fixes from mahi-application
[arc.git] / system / kernel / init.c
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 /* ----------------------------[includes]------------------------------------*/
17 #include <stdlib.h>
18 #include <string.h>
19 #include "Os.h"
20 #include "internal.h"
21 #include "arc.h"
22 #include "debug.h"
23 #include "arch.h"
24
25 /* ----------------------------[private define]------------------------------*/
26 /* ----------------------------[private macro]-------------------------------*/
27 /* ----------------------------[private typedef]-----------------------------*/
28 /* ----------------------------[private function prototypes]-----------------*/
29 /* ----------------------------[private variables]---------------------------*/
30 Os_SysType Os_Sys;
31
32 Os_IntCounterType Os_IntDisableAllCnt;
33 Os_IntCounterType Os_IntSuspendAllCnt;
34 Os_IntCounterType Os_IntSuspendOsCnt;
35
36 /* ----------------------------[private functions]---------------------------*/
37 /* ----------------------------[public functions]----------------------------*/
38
39 extern uint32_t McuE_GetSystemClock( void );
40 extern OsTickType OsTickFreq;
41
42
43 /**
44  * Copy rom pcb data(r_pcb) to ram data
45  *
46  * @param       pcb             ram data
47  * @param       r_pcb   rom data
48  */
49
50 static void copyPcbParts( OsTaskVarType *pcb, const OsTaskConstType *r_pcb ) {
51
52         /* Copy VAR stuff first */
53
54
55
56
57 #if 0 //?????
58         // Check to that the memory is ok
59         {
60                 int cnt = sizeof(OsTaskVarType);
61                 for(int i=0;i<cnt;i++) {
62                         if( *((unsigned char *)pcb) != 0 ) {
63                                 while(1);
64                         }
65                 }
66         }
67 #endif
68
69 //      memset(pcb,sizeof(OsTaskVarType),0);
70 //      pcb->pid = r_pcb->pid;
71         assert(r_pcb->prio<=OS_TASK_PRIORITY_MAX);
72         pcb->activePriority = r_pcb->prio;
73 #if     (OS_USE_APPLICATIONS == STD_ON)
74 //      pcb->accessingApp = Os_CfgGetApplObj(r_pcb->application_id);
75 #endif
76 //      pcb->entry = r_pcb->entry;
77 //      pcb->proc_type = r_pcb->proc_type;
78 //      pcb->autostart =  r_pcb->autostart;
79         pcb->stack= r_pcb->stack;
80         pcb->constPtr = r_pcb;
81 //      pcb->resourceIntPtr = r_pcb->resourceIntPtr;
82 //      pcb->scheduling = r_pcb->scheduling;
83 //      pcb->resourceAccess = r_pcb->resourceAccess;
84 //      pcb->activationLimit = r_pcb->activationLimit;
85 //      pcb->app = &app_list[r_pcb->app];
86 //      pcb->app_mask = app_mask[r_pcb->app];
87 //      strncpy(pcb->name,r_pcb->name,16);
88 //      pcb->name[15] = '\0';
89 }
90
91 static _Bool init_os_called = 0;
92
93 /**
94  * Initialization of kernel structures and start of the first
95  * task.
96  */
97
98 void InitOS( void ) {
99         int i;
100         OsTaskVarType *tmpPcbPtr;
101         OsIsrStackType intStack;
102
103         init_os_called = 1;
104
105         DEBUG(DEBUG_LOW,"os_init");
106
107         /* Clear sys */
108         memset(&Os_Sys,0,sizeof(Os_SysType));
109
110         Os_ArchInit();
111
112         // Assign pcb list and init ready queue
113         Os_Sys.pcb_list = Os_TaskVarList;
114         TAILQ_INIT(& Os_Sys.ready_head);
115 //      TAILQ_INIT(& Os_Sys.pcb_head);
116 #if defined(USE_KERNEL_EXTRA)
117         TAILQ_INIT(& Os_Sys.timerHead);
118 #endif
119
120         // Calc interrupt stack
121         Os_IsrGetStackInfo(&intStack);
122         // TODO: 16 is arch dependent
123         Os_Sys.intStack = (void *)((size_t)intStack.top + (size_t)intStack.size - 16);
124
125         // Init counter.. with alarms and schedule tables
126 #if OS_COUNTER_CNT!=0
127         Os_CounterInit();
128 #endif
129 #if OS_SCHTBL_CNT!=0
130         Os_SchTblInit();
131 #endif
132
133         // Put all tasks in the pcb list
134         // Put the one that belong in the ready queue there
135         // TODO: we should really hash on priority here to get speed, but I don't care for the moment
136         // TODO: Isn't this just EXTENED tasks ???
137         for( i=0; i < OS_TASK_CNT; i++) {
138                 tmpPcbPtr = Os_TaskGet(i);
139
140                 copyPcbParts(tmpPcbPtr,&Os_TaskConstList[i]);
141
142 #if 1
143                 Os_TaskContextInit(tmpPcbPtr);
144 #else
145                 if( !(tmpPcbPtr->constPtr->proc_type & PROC_ISR) ) {
146                         Os_TaskContextInit(tmpPcbPtr);
147                 }
148 #endif
149
150                 TAILQ_INIT(&tmpPcbPtr->resourceHead);
151
152 #if 0
153                 Os_AddTask(tmpPcbPtr);
154 #endif
155
156                 DEBUG(DEBUG_LOW,"pid:%d name:%s prio:%d\n",tmpPcbPtr->pid,tmpPcbPtr->name,tmpPcbPtr->prio);
157         }
158
159         Os_ResourceInit();
160
161         // Now all tasks should be created.
162 }
163
164 static void os_start( void ) {
165         uint16_t i;
166         OsTaskVarType *tmpPcbPtr = NULL;
167
168         // We will be setting up interrupts,
169         // but we don't want them to fire just yet
170         Irq_Disable();
171
172         assert(init_os_called);
173
174         /* TODO: fix ugly */
175         /* Call the startup hook */
176         extern struct OsHooks os_conf_global_hooks;
177         Os_Sys.hooks = &os_conf_global_hooks;
178         if( Os_Sys.hooks->StartupHook!=NULL ) {
179                 Os_Sys.hooks->StartupHook();
180         }
181
182
183 #if     (OS_USE_APPLICATIONS == STD_ON)
184         /* Start applications */
185         Os_ApplStart();
186 #endif
187
188
189         /* Alarm autostart */
190 #if OS_ALARM_CNT!=0
191         Os_AlarmAutostart();
192 #endif
193
194 #if OS_SCHTBL_CNT!=0
195         Os_SchTblAutostart();
196 #endif
197
198         // Set up the systick interrupt
199         {
200                 uint32_t sys_freq = McuE_GetSystemClock();
201                 Os_SysTickInit();
202                 Os_SysTickStart(sys_freq/OsTickFreq);
203         }
204
205         /* Find highest Autostart task */
206         {
207                 OsTaskVarType *iterPcbPtr;
208                 OsPriorityType topPrio = -1;
209
210                 for(i=0;i<OS_TASK_CNT;i++) {
211                         iterPcbPtr = Os_TaskGet(i);
212                         if(     iterPcbPtr->constPtr->autostart ) {
213                                 if( iterPcbPtr->activePriority > topPrio ) {
214                                         tmpPcbPtr = iterPcbPtr;
215                                         topPrio = iterPcbPtr->activePriority;
216                                 }
217                         }
218                 }
219 #if 0
220                 TAILQ_FOREACH(iterPcbPtr,& Os_Sys.pcb_head,pcb_list) {
221                         if(     iterPcbPtr->constPtr->autostart ) {
222                                 if( iterPcbPtr->activePriority > topPrio ) {
223                                         tmpPcbPtr = iterPcbPtr;
224                                         topPrio = iterPcbPtr->activePriority;
225                                 }
226                         }
227                 }
228 #endif
229         }
230
231         // Swap in prio proc.
232         {
233                 // FIXME: Do this in a more structured way.. setting Os_Sys.currTaskPtr manually is not the way to go..
234                 Os_Sys.currTaskPtr = tmpPcbPtr;
235 #if     (OS_USE_APPLICATIONS == STD_ON)
236                 /* Set current application */
237                 Os_Sys.currApplId = tmpPcbPtr->constPtr->applOwnerId;
238 #endif
239
240                 // register this auto-start activation
241                 assert(tmpPcbPtr->activations < tmpPcbPtr->constPtr->activationLimit);
242                 tmpPcbPtr->activations++;
243
244                 // NOTE! We don't go for os_swap_context() here..
245                 // first arg(NULL) is dummy only
246                 Os_TaskSwapContextTo(NULL,tmpPcbPtr);
247                 // We should not return here
248                 assert(0);
249         }
250 }
251 #if 0
252 static void os_start( void ) {
253
254 }
255 #endif
256
257 #define TEST_DATA  12345
258 int test_data = TEST_DATA;
259 int test_bss = 0;
260
261
262 void noooo( void ) {
263         while(1);
264 }
265
266 extern void EcuM_Init();
267 int main( void )
268 {
269         EcuM_Init();
270
271 }
272
273 /**
274  * Starts the OS
275  *
276  * @param Mode - Application mode to start in
277  *
278  */
279 void StartOS(AppModeType Mode) {
280
281         /* Check link file */
282         if (TEST_DATA != test_data) {
283                 noooo();
284         }
285
286         if (test_bss != 0) {
287                 noooo();
288         }
289
290         Os_Sys.appMode = Mode;
291
292         Os_CfgValidate();
293
294         os_start();
295
296         /** @req OS424 */
297         assert(0);
298 }
299
300 /**
301  * OS shutdown
302  *
303  * @param Error - Reason for shutdown
304  */
305
306 /** @req OS071 */
307 void ShutdownOS( StatusType Error ) {
308
309         if( Os_Sys.hooks->ShutdownHook != NULL ) {
310                 Os_Sys.hooks->ShutdownHook(Error);
311         }
312
313         Irq_Disable();
314         /** @req OS425 */
315         while(1) {      }
316
317 }
318
319
320