]> rtime.felk.cvut.cz Git - arc.git/blobdiff - system/kernel/init.c
EcuM: More EcuM fixes.
[arc.git] / system / kernel / init.c
index 0db2b4a2f15fa84f8f9a2d99fc1cfa3b94678f62..fe5f9da4878662cff751d209e7b83d9d903bf205 100644 (file)
  * for more details.\r
  * -------------------------------- Arctic Core ------------------------------*/\r
 \r
-\r
+/* ----------------------------[includes]------------------------------------*/\r
 #include <stdlib.h>\r
 #include <string.h>\r
 #include "Os.h"\r
 #include "internal.h"\r
 #include "arc.h"\r
 #include "debug.h"\r
+#include "task_i.h"\r
+#include "sys.h"\r
+#include "isr.h"\r
+#include "counter_i.h"\r
+#include "application.h"\r
+#include "sched_table_i.h"\r
+#include "alarm_i.h"\r
 #include "arch.h"\r
 \r
-extern void Os_CfgGetInterruptStackInfo( OsStackType *stack );\r
-extern uint32_t McuE_GetSystemClock( void );\r
-extern OsTickType OsTickFreq;\r
-\r
-sys_t os_sys;\r
+/* ----------------------------[private define]------------------------------*/\r
+/* ----------------------------[private macro]-------------------------------*/\r
+#define OS_VALIDATE(_a,_b)   if((_a)!=(_b) ) { \\r
+                                                               assert(#_a  #_b); \\r
+                                                         }\r
+/* ----------------------------[private typedef]-----------------------------*/\r
+/* ----------------------------[private function prototypes]-----------------*/\r
+/* ----------------------------[private variables]---------------------------*/\r
+Os_SysType Os_Sys;\r
 \r
 Os_IntCounterType Os_IntDisableAllCnt;\r
 Os_IntCounterType Os_IntSuspendAllCnt;\r
 Os_IntCounterType Os_IntSuspendOsCnt;\r
 \r
+OsErrorType os_error;\r
+\r
+/* ----------------------------[private functions]---------------------------*/\r
+\r
+\r
+static void Os_CfgValidate(void ) {\r
+#if (OS_COUNTER_CNT!=0)\r
+       OS_VALIDATE(OS_COUNTER_CNT,ARRAY_SIZE(counter_list));\r
+#endif\r
+#if (OS_RESOURCE_CNT!=0)\r
+       OS_VALIDATE(OS_RESOURCE_CNT,ARRAY_SIZE(resource_list));\r
+#endif\r
+       OS_VALIDATE(OS_TASK_CNT ,ARRAY_SIZE( Os_TaskConstList));\r
+#if (OS_ALARM_CNT!=0)\r
+       OS_VALIDATE(OS_ALARM_CNT,ARRAY_SIZE(alarm_list));\r
+#endif\r
+#if (OS_SCHTBL_CNT!=0)\r
+       OS_VALIDATE(OS_SCHTBL_CNT, ARRAY_SIZE(sched_list));\r
+#endif\r
+}\r
+\r
+/* ----------------------------[public functions]----------------------------*/\r
+\r
+extern uint32_t McuE_GetSystemClock( void );\r
+\r
 \r
 /**\r
  * Copy rom pcb data(r_pcb) to ram data\r
@@ -40,38 +76,11 @@ Os_IntCounterType Os_IntSuspendOsCnt;
  * @param      r_pcb   rom data\r
  */\r
 \r
-static void os_pcb_rom_copy( OsPcbType *pcb, const OsRomPcbType *r_pcb ) {\r
-\r
-#if 0 //?????\r
-       // Check to that the memory is ok\r
-       {\r
-               int cnt = sizeof(OsPcbType);\r
-               for(int i=0;i<cnt;i++) {\r
-                       if( *((unsigned char *)pcb) != 0 ) {\r
-                               while(1);\r
-                       }\r
-               }\r
-       }\r
-#endif\r
-\r
-//     memset(pcb,sizeof(OsPcbType),0);\r
-       pcb->pid = r_pcb->pid;\r
-       pcb->prio = r_pcb->prio;\r
-#if ( OS_SC3 == STD_ON ) || ( OS_SC4 == STD_ON )\r
-       pcb->application = Os_CfgGetApplObj(r_pcb->application_id);\r
-#endif\r
-       pcb->entry = r_pcb->entry;\r
-       pcb->proc_type = r_pcb->proc_type;\r
-       pcb->autostart =  r_pcb->autostart;\r
+static void copyPcbParts( OsTaskVarType *pcb, const OsTaskConstType *r_pcb ) {\r
+       assert(r_pcb->prio<=OS_TASK_PRIORITY_MAX);\r
+       pcb->activePriority = r_pcb->prio;\r
        pcb->stack= r_pcb->stack;\r
-       pcb->pcb_rom_p = r_pcb;\r
-       pcb->resource_int_p = r_pcb->resource_int_p;\r
-       pcb->scheduling = r_pcb->scheduling;\r
-       pcb->resourceAccess = r_pcb->resourceAccess;\r
-       pcb->activationLimit = r_pcb->activationLimit;\r
-//     pcb->app = &app_list[r_pcb->app];\r
-//     pcb->app_mask = app_mask[r_pcb->app];\r
-       strncpy(pcb->name,r_pcb->name,16);\r
+       pcb->constPtr = r_pcb;\r
 }\r
 \r
 static _Bool init_os_called = 0;\r
@@ -83,51 +92,60 @@ static _Bool init_os_called = 0;
 \r
 void InitOS( void ) {\r
        int i;\r
-       OsPcbType *tmp_pcb;\r
-       OsStackType int_stack;\r
+       OsTaskVarType *tmpPcbPtr;\r
+       OsIsrStackType intStack;\r
 \r
        init_os_called = 1;\r
 \r
+       Os_CfgValidate();\r
+\r
        DEBUG(DEBUG_LOW,"os_init");\r
 \r
        /* Clear sys */\r
-       memset(&os_sys,0,sizeof(sys_t));\r
+       memset(&Os_Sys,0,sizeof(Os_SysType));\r
 \r
        Os_ArchInit();\r
 \r
+       /* Get the numbers defined in the editor */\r
+       Os_Sys.isrCnt = OS_ISR_CNT;\r
+\r
        // Assign pcb list and init ready queue\r
-       os_sys.pcb_list = pcb_list;\r
-       TAILQ_INIT(& os_sys.ready_head);\r
-       TAILQ_INIT(& os_sys.pcb_head);\r
+       Os_Sys.pcb_list = Os_TaskVarList;\r
+       TAILQ_INIT(& Os_Sys.ready_head);\r
+//     TAILQ_INIT(& Os_Sys.pcb_head);\r
+#if defined(USE_KERNEL_EXTRA)\r
+       TAILQ_INIT(& Os_Sys.timerHead);\r
+#endif\r
 \r
        // Calc interrupt stack\r
-       Os_CfgGetInterruptStackInfo(&int_stack);\r
+       Os_IsrGetStackInfo(&intStack);\r
        // TODO: 16 is arch dependent\r
-       os_sys.int_stack = int_stack.top + int_stack.size - 16;\r
+       Os_Sys.intStack = (void *)((size_t)intStack.top + (size_t)intStack.size - 16);\r
 \r
        // Init counter.. with alarms and schedule tables\r
+#if OS_COUNTER_CNT!=0\r
        Os_CounterInit();\r
+#endif\r
+#if OS_SCHTBL_CNT!=0\r
        Os_SchTblInit();\r
+#endif\r
 \r
        // Put all tasks in the pcb list\r
        // Put the one that belong in the ready queue there\r
        // TODO: we should really hash on priority here to get speed, but I don't care for the moment\r
        // TODO: Isn't this just EXTENED tasks ???\r
-       for( i=0; i < Os_CfgGetTaskCnt(); i++) {\r
-               tmp_pcb = os_get_pcb(i);\r
+       for( i=0; i < OS_TASK_CNT; i++) {\r
+               tmpPcbPtr = Os_TaskGet(i);\r
 \r
-               assert(tmp_pcb->prio<=OS_TASK_PRIORITY_MAX);\r
+               copyPcbParts(tmpPcbPtr,&Os_TaskConstList[i]);\r
+               Os_TaskContextInit(tmpPcbPtr);\r
+               TAILQ_INIT(&tmpPcbPtr->resourceHead);\r
 \r
-               os_pcb_rom_copy(tmp_pcb,os_get_rom_pcb(i));\r
-               if( !(tmp_pcb->proc_type & PROC_ISR) ) {\r
-                       Os_ContextInit(tmp_pcb);\r
-               }\r
-\r
-               TAILQ_INIT(&tmp_pcb->resource_head);\r
-\r
-               Os_AddTask(tmp_pcb);\r
+#if 0\r
+               Os_AddTask(tmpPcbPtr);\r
+#endif\r
 \r
-               DEBUG(DEBUG_LOW,"pid:%d name:%s prio:%d\n",tmp_pcb->pid,tmp_pcb->name,tmp_pcb->prio);\r
+               DEBUG(DEBUG_LOW,"pid:%d name:%s prio:%d\n",tmpPcbPtr->pid,tmpPcbPtr->name,tmpPcbPtr->prio);\r
        }\r
 \r
        Os_ResourceInit();\r
@@ -136,7 +154,8 @@ void InitOS( void ) {
 }\r
 \r
 static void os_start( void ) {\r
-       OsPcbType *tmp_pcb;\r
+       uint16_t i;\r
+       OsTaskVarType *tmpPcbPtr = NULL;\r
 \r
        // We will be setting up interrupts,\r
        // but we don't want them to fire just yet\r
@@ -147,29 +166,26 @@ static void os_start( void ) {
        /* TODO: fix ugly */\r
        /* Call the startup hook */\r
        extern struct OsHooks os_conf_global_hooks;\r
-       os_sys.hooks = &os_conf_global_hooks;\r
-       if( os_sys.hooks->StartupHook!=NULL ) {\r
-               os_sys.hooks->StartupHook();\r
+       Os_Sys.hooks = &os_conf_global_hooks;\r
+       if( Os_Sys.hooks->StartupHook!=NULL ) {\r
+               Os_Sys.hooks->StartupHook();\r
        }\r
 \r
+\r
+#if    (OS_USE_APPLICATIONS == STD_ON)\r
+       /* Start applications */\r
+       Os_ApplStart();\r
+#endif\r
+\r
+\r
        /* Alarm autostart */\r
-       for(int j=0; j < Os_CfgGetAlarmCnt(); j++ ) {\r
-               OsAlarmType *alarmPtr;\r
-               alarmPtr = Os_CfgGetAlarmObj(j);\r
-               if(alarmPtr->autostartPtr != NULL ) {\r
-                       const OsAlarmAutostartType *autoPtr = alarmPtr->autostartPtr;\r
-\r
-                       if( os_sys.appMode & autoPtr->appModeRef) {\r
-                       if( autoPtr->autostartType == ALARM_AUTOSTART_ABSOLUTE ) {\r
-                               SetAbsAlarm(j,autoPtr->alarmTime, autoPtr->cycleTime);\r
-                       } else {\r
-                               SetRelAlarm(j,autoPtr->alarmTime, autoPtr->cycleTime);\r
-                       }\r
-               }\r
-       }\r
-       }\r
+#if OS_ALARM_CNT!=0\r
+       Os_AlarmAutostart();\r
+#endif\r
 \r
+#if OS_SCHTBL_CNT!=0\r
        Os_SchTblAutostart();\r
+#endif\r
 \r
        // Set up the systick interrupt\r
        {\r
@@ -180,26 +196,46 @@ static void os_start( void ) {
 \r
        /* Find highest Autostart task */\r
        {\r
-               OsPcbType *iterPcbPtr;\r
+               OsTaskVarType *iterPcbPtr;\r
                OsPriorityType topPrio = -1;\r
 \r
-               TAILQ_FOREACH(iterPcbPtr,& os_sys.pcb_head,pcb_list) {\r
-                       if(     iterPcbPtr->autostart ) {\r
-                               if( iterPcbPtr->prio > topPrio ) {\r
-                                       tmp_pcb = iterPcbPtr;\r
-                                       topPrio = iterPcbPtr->prio;\r
+               for(i=0;i<OS_TASK_CNT;i++) {\r
+                       iterPcbPtr = Os_TaskGet(i);\r
+                       if(     iterPcbPtr->constPtr->autostart ) {\r
+                               if( iterPcbPtr->activePriority > topPrio ) {\r
+                                       tmpPcbPtr = iterPcbPtr;\r
+                                       topPrio = iterPcbPtr->activePriority;\r
+                               }\r
+                       }\r
+               }\r
+#if 0\r
+               TAILQ_FOREACH(iterPcbPtr,& Os_Sys.pcb_head,pcb_list) {\r
+                       if(     iterPcbPtr->constPtr->autostart ) {\r
+                               if( iterPcbPtr->activePriority > topPrio ) {\r
+                                       tmpPcbPtr = iterPcbPtr;\r
+                                       topPrio = iterPcbPtr->activePriority;\r
                                }\r
                        }\r
                }\r
+#endif\r
        }\r
 \r
        // Swap in prio proc.\r
        {\r
-               // FIXME: Do this in a more structured way.. setting os_sys.curr_pcb manually is not the way to go..\r
-               os_sys.curr_pcb = tmp_pcb;\r
+               // FIXME: Do this in a more structured way.. setting Os_Sys.currTaskPtr manually is not the way to go..\r
+               Os_Sys.currTaskPtr = tmpPcbPtr;\r
+#if    (OS_USE_APPLICATIONS == STD_ON)\r
+               /* Set current application */\r
+               Os_Sys.currApplId = tmpPcbPtr->constPtr->applOwnerId;\r
+#endif\r
+\r
+               // register this auto-start activation\r
+               assert(tmpPcbPtr->activations < tmpPcbPtr->constPtr->activationLimit);\r
+               tmpPcbPtr->activations++;\r
+\r
                // NOTE! We don't go for os_swap_context() here..\r
                // first arg(NULL) is dummy only\r
-               Os_TaskSwapContextTo(NULL,tmp_pcb);\r
+               Os_TaskSwapContextTo(NULL,tmpPcbPtr);\r
                // We should not return here\r
                assert(0);\r
        }\r
@@ -210,18 +246,54 @@ static void os_start( void ) {
 }\r
 #endif\r
 \r
-#define TEST_DATA  12345\r
-int test_data = TEST_DATA;\r
-int test_bss = 0;\r
+#define TEST_DATA  0x12345\r
+#define TEST_SDATA2    0x3344\r
+volatile uint32_t test_data = TEST_DATA;\r
+volatile uint32_t test_bss = 0;\r
+/* Define if compiler is set to use small data section */\r
+/* #define CC_USE_SMALL_DATA */\r
+\r
+#if defined(CFG_PPC) && defined(__CWCC__)\r
+/* Note! It does not matter if the data is initialized to 0,\r
+ * it still sbss2.
+ */\r
+#if defined(CC_USE_SMALL_DATA)\r
+volatile const int test_sbss2;\r
+#endif\r
+/* Initialized small data */\r
+volatile const int test_sdata2 = TEST_SDATA2;\r
+\r
+#endif\r
 \r
 \r
-void noooo( void ) {\r
-       while(1);\r
-}\r
+#define BAD_LINK_FILE()        while(1) {}\r
 \r
 extern void EcuM_Init();\r
 int main( void )\r
 {\r
+       /* Check link file */\r
+       if (TEST_DATA != test_data) {\r
+               BAD_LINK_FILE();\r
+       }\r
+\r
+       if (test_bss != 0) {\r
+               BAD_LINK_FILE();\r
+       }\r
+\r
+#if defined(CFG_PPC) && defined(__CWCC__)\r
+       /* check sdata2 */\r
+       if (test_sdata2 != TEST_SDATA2) {\r
+               BAD_LINK_FILE();\r
+       }\r
+#endif\r
+\r
+#if defined(CC_USE_SMALL_DATA) && defined(CFG_PPC) && defined(__CWCC__)\r
+       /* check sdata2 */\r
+       if (test_sbss2 != 0) {\r
+               BAD_LINK_FILE();\r
+       }\r
+#endif\r
+\r
        EcuM_Init();\r
 \r
 }\r
@@ -234,16 +306,7 @@ int main( void )
  */\r
 void StartOS(AppModeType Mode) {\r
 \r
-       /* Check link file */\r
-       if (TEST_DATA != test_data) {\r
-               noooo();\r
-       }\r
-\r
-       if (test_bss != 0) {\r
-               noooo();\r
-       }\r
-\r
-       os_sys.appMode = Mode;\r
+       Os_Sys.appMode = Mode;\r
 \r
        Os_CfgValidate();\r
 \r
@@ -262,8 +325,8 @@ void StartOS(AppModeType Mode) {
 /** @req OS071 */\r
 void ShutdownOS( StatusType Error ) {\r
 \r
-       if( os_sys.hooks->ShutdownHook != NULL ) {\r
-               os_sys.hooks->ShutdownHook(Error);\r
+       if( Os_Sys.hooks->ShutdownHook != NULL ) {\r
+               Os_Sys.hooks->ShutdownHook(Error);\r
        }\r
 \r
        Irq_Disable();\r