1 /* -------------------------------- Arctic Core ------------------------------
2 * Arctic Core - the open source AUTOSAR platform http://arccore.com
4 * Copyright (C) 2009 ArcCore AB <contact@arccore.com>
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>.
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
14 * -------------------------------- Arctic Core ------------------------------*/
25 extern void Oil_GetInterruptStackInfo( OsStackType *stack );
26 extern uint32_t McuE_GetSystemClock( void );
27 extern OsTickType OsTickFreq;
31 Os_IntCounterType Os_IntDisableAllCnt;
32 Os_IntCounterType Os_IntSuspendAllCnt;
33 Os_IntCounterType Os_IntSuspendOsCnt;
36 * Initialize alarms and schedule-tables for the counters
38 static void os_counter_init( void ) {
39 OsCounterType *counter;
40 OsAlarmType *alarm_obj;
41 OsSchTblType *sched_obj;
42 /* Create a list from the counter to the alarms */
43 for(int i=0; i < Oil_GetCounterCnt() ; i++) {
44 counter = Oil_GetCounter(i);
46 SLIST_INIT(&counter->alarm_head);
47 for(int j=0; j < Oil_GetAlarmCnt(); j++ ) {
48 alarm_obj = Oil_GetAlarmObj(j);
50 SLIST_INSERT_HEAD(&counter->alarm_head,alarm_obj, alarm_list);
53 SLIST_INIT(&counter->sched_head);
54 for(int j=0; j < Oil_GetSchedCnt(); j++ ) {
55 sched_obj = Oil_GetSched(j);
57 SLIST_INSERT_HEAD(&counter->sched_head,
71 static void os_resource_init( void ) {
72 //TAILQ_INIT(&pcb_p->resource_head);
74 OsResourceType *rsrc_p;
77 /* Calculate ceiling priority
78 * We make this as simple as possible. The ceiling priority
79 * is set to the same priority as the highest priority task that
82 for( int i=0; i < Oil_GetResourceCnt(); i++) {
83 rsrc_p = Oil_GetResource(i);
86 for( int pi; pi < Oil_GetTaskCnt(); pi++) {
88 pcb_p = os_get_pcb(pi);
89 if(pcb_p->resourceAccess & (1<<i) ) {
90 topPrio = MAX(topPrio,pcb_p->prio);
93 rsrc_p->ceiling_priority = topPrio;
96 /* Assign an internal resource with prio 32 to the tasks
99 for( int i; i < Oil_GetTaskCnt(); i++) {
100 pcb_p = os_get_pcb(i);
101 if(pcb_p->scheduling == NON ) {
102 pcb_p->prio = OS_RES_SCHEDULER_PRIO;
109 * Copy rom pcb data(r_pcb) to ram data
111 * @param pcb ram data
112 * @param r_pcb rom data
115 static void os_pcb_rom_copy( OsPcbType *pcb, const OsRomPcbType *r_pcb ) {
118 // Check to that the memory is ok
120 int cnt = sizeof(OsPcbType);
121 for(int i=0;i<cnt;i++) {
122 if( *((unsigned char *)pcb) != 0 ) {
129 // memset(pcb,sizeof(OsPcbType),0);
130 pcb->pid = r_pcb->pid;
131 pcb->prio = r_pcb->prio;
132 #if ( OS_SC1 == STD_ON ) || ( OS_SC4 == STD_ON )
133 pcb->application = Oil_GetApplObj(r_pcb->application_id);
135 pcb->entry = r_pcb->entry;
136 pcb->proc_type = r_pcb->proc_type;
137 pcb->autostart = r_pcb->autostart;
138 pcb->stack= r_pcb->stack;
139 pcb->pcb_rom_p = r_pcb;
140 pcb->resource_int_p = r_pcb->resource_int_p;
141 pcb->scheduling = r_pcb->scheduling;
142 pcb->resourceAccess = r_pcb->resourceAccess;
143 pcb->activationLimit = r_pcb->activationLimit;
144 // pcb->app = &app_list[r_pcb->app];
145 // pcb->app_mask = app_mask[r_pcb->app];
146 strncpy(pcb->name,r_pcb->name,16);
149 static _Bool init_os_called = 0;
152 * Initialization of kernel structures and start of the first
156 void InitOS( void ) {
159 OsStackType int_stack;
163 DEBUG(DEBUG_LOW,"os_init");
166 memset(&os_sys,0,sizeof(sys_t));
170 // Assign pcb list and init ready queue
171 os_sys.pcb_list = pcb_list;
172 TAILQ_INIT(& os_sys.ready_head);
173 TAILQ_INIT(& os_sys.pcb_head);
175 // Calc interrupt stack
176 Oil_GetInterruptStackInfo(&int_stack);
177 os_sys.int_stack = int_stack.top + int_stack.size - 16; // TODO: 16 is arch dependent
179 // Init counter.. with alarms and schedule tables
184 // Put all tasks in the pcb list
185 // Put the one that belong in the ready queue there
186 // TODO: we should really hash on priority here to get speed, but I don't care for the moment
187 // TODO: Isn't this just EXTENED tasks ???
188 for( i=0; i < Oil_GetTaskCnt(); i++) {
189 tmp_pcb = os_get_pcb(i);
191 assert(tmp_pcb->prio<=OS_TASK_PRIORITY_MAX);
193 os_pcb_rom_copy(tmp_pcb,os_get_rom_pcb(i));
194 if( !(tmp_pcb->proc_type & PROC_ISR) ) {
195 Os_ContextInit(tmp_pcb);
200 DEBUG(DEBUG_LOW,"pid:%d name:%s prio:%d\n",tmp_pcb->pid,tmp_pcb->name,tmp_pcb->prio);
203 // Now all tasks should be created.
206 static void os_start( void ) {
209 assert(init_os_called);
212 /* Call the startup hook */
213 extern struct OsHooks os_conf_global_hooks;
214 os_sys.hooks = &os_conf_global_hooks;
215 if( os_sys.hooks->StartupHook!=NULL ) {
216 os_sys.hooks->StartupHook();
219 /* handle autostart */
220 for(int j=0; j < Oil_GetAlarmCnt(); j++ ) {
221 OsAlarmType *alarmPtr;
222 alarmPtr = Oil_GetAlarmObj(j);
223 if(alarmPtr->autostart.active) {
224 OsAlarmAutostartType *autoPtr = &alarmPtr->autostart;
226 SetAbsAlarm(j,autoPtr->alarmtime, autoPtr->cycletime);
230 // Activate the systick interrupt
232 uint32_t sys_freq = McuE_GetSystemClock();
234 Frt_Start(sys_freq/OsTickFreq);
237 /* Find highest Autostart task */
239 OsPcbType *iterPcbPtr;
240 OsPriorityType topPrio = 0;
242 TAILQ_FOREACH(iterPcbPtr,& os_sys.pcb_head,pcb_list) {
243 if( iterPcbPtr->autostart ) {
244 if( iterPcbPtr->prio > topPrio ) {
245 tmp_pcb = iterPcbPtr;
251 // Swap in prio proc.
253 // FIXME: Do this in a more structured way.. setting os_sys.curr_pcb manually is not the way to go..
254 os_sys.curr_pcb = tmp_pcb;
255 // NOTE! We don't go for os_swap_context() here..
256 // first arg(NULL) is dummy only
257 Os_TaskSwapContextTo(NULL,tmp_pcb);
258 // We should not return here
263 static void os_start( void ) {
268 #define TEST_DATA 12345
269 int test_data = TEST_DATA;
277 extern void EcuM_Init();
287 * @param Mode - Application mode to start in
290 void StartOS(AppModeType Mode) {
292 /* Check link file */
293 if (TEST_DATA != test_data) {
310 * @param Error - Reason for shutdown
314 void ShutdownOS( StatusType Error ) {
316 if( os_sys.hooks->ShutdownHook != NULL ) {
317 os_sys.hooks->ShutdownHook(Error);