-/* -------------------------------- Arctic Core ------------------------------
- * Arctic Core - the open source AUTOSAR platform http://arccore.com
- *
- * Copyright (C) 2009 ArcCore AB <contact@arccore.com>
- *
- * This source code is free software; you can redistribute it and/or modify it
- * under the terms of the GNU General Public License version 2 as published by the
- * Free Software Foundation; See <http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt>.
- *
- * This program is distributed in the hope that it will be useful, but
- * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
- * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
- * for more details.
- * -------------------------------- Arctic Core ------------------------------*/
-
-
-#include <stdlib.h>
-#include <string.h>
-#include "Os.h"
-#include "internal.h"
-#include "arc.h"
-#include "debug.h"
-#include "arch.h"
-
-extern void Os_CfgGetInterruptStackInfo( OsStackType *stack );
-extern uint32_t McuE_GetSystemClock( void );
-extern OsTickType OsTickFreq;
-
-sys_t os_sys;
-
-Os_IntCounterType Os_IntDisableAllCnt;
-Os_IntCounterType Os_IntSuspendAllCnt;
-Os_IntCounterType Os_IntSuspendOsCnt;
-
-
-/**
- * Copy rom pcb data(r_pcb) to ram data
- *
- * @param pcb ram data
- * @param r_pcb rom data
- */
-
-static void os_pcb_rom_copy( OsPcbType *pcb, const OsRomPcbType *r_pcb ) {
-
-#if 0 //?????
- // Check to that the memory is ok
- {
- int cnt = sizeof(OsPcbType);
- for(int i=0;i<cnt;i++) {
- if( *((unsigned char *)pcb) != 0 ) {
- while(1);
- }
- }
- }
-#endif
-
-// memset(pcb,sizeof(OsPcbType),0);
- pcb->pid = r_pcb->pid;
- pcb->prio = r_pcb->prio;
-#if ( OS_SC3 == STD_ON ) || ( OS_SC4 == STD_ON )
- pcb->application = Os_CfgGetApplObj(r_pcb->application_id);
-#endif
- pcb->entry = r_pcb->entry;
- pcb->proc_type = r_pcb->proc_type;
- pcb->autostart = r_pcb->autostart;
- pcb->stack= r_pcb->stack;
- pcb->pcb_rom_p = r_pcb;
- pcb->resource_int_p = r_pcb->resource_int_p;
- pcb->scheduling = r_pcb->scheduling;
- pcb->resourceAccess = r_pcb->resourceAccess;
- pcb->activationLimit = r_pcb->activationLimit;
-// pcb->app = &app_list[r_pcb->app];
-// pcb->app_mask = app_mask[r_pcb->app];
- strncpy(pcb->name,r_pcb->name,16);
- pcb->name[15] = '\0';
-}
-
-static _Bool init_os_called = 0;
-
-/**
- * Initialization of kernel structures and start of the first
- * task.
- */
-
-void InitOS( void ) {
- int i;
- OsPcbType *tmp_pcb;
- OsStackType int_stack;
-
- init_os_called = 1;
-
- DEBUG(DEBUG_LOW,"os_init");
-
- /* Clear sys */
- memset(&os_sys,0,sizeof(sys_t));
-
- Os_ArchInit();
-
- // Assign pcb list and init ready queue
- os_sys.pcb_list = pcb_list;
- TAILQ_INIT(& os_sys.ready_head);
- TAILQ_INIT(& os_sys.pcb_head);
-#if defined(USE_KERNEL_EXTRA)
- TAILQ_INIT(& os_sys.timerHead);
-#endif
-
- // Calc interrupt stack
- Os_CfgGetInterruptStackInfo(&int_stack);
- // TODO: 16 is arch dependent
- os_sys.int_stack = int_stack.top + int_stack.size - 16;
-
- // Init counter.. with alarms and schedule tables
- Os_CounterInit();
- Os_SchTblInit();
-
- // Put all tasks in the pcb list
- // Put the one that belong in the ready queue there
- // TODO: we should really hash on priority here to get speed, but I don't care for the moment
- // TODO: Isn't this just EXTENED tasks ???
- for( i=0; i < Os_CfgGetTaskCnt(); i++) {
- tmp_pcb = os_get_pcb(i);
-
- assert(tmp_pcb->prio<=OS_TASK_PRIORITY_MAX);
-
- os_pcb_rom_copy(tmp_pcb,os_get_rom_pcb(i));
- if( !(tmp_pcb->proc_type & PROC_ISR) ) {
- Os_ContextInit(tmp_pcb);
- }
-
- TAILQ_INIT(&tmp_pcb->resource_head);
-
- Os_AddTask(tmp_pcb);
-
- DEBUG(DEBUG_LOW,"pid:%d name:%s prio:%d\n",tmp_pcb->pid,tmp_pcb->name,tmp_pcb->prio);
- }
-
- Os_ResourceInit();
-
- // Now all tasks should be created.
-}
-
-static void os_start( void ) {
- OsPcbType *tmp_pcb;
-
- // We will be setting up interrupts,
- // but we don't want them to fire just yet
- Irq_Disable();
-
- assert(init_os_called);
-
- /* TODO: fix ugly */
- /* Call the startup hook */
- extern struct OsHooks os_conf_global_hooks;
- os_sys.hooks = &os_conf_global_hooks;
- if( os_sys.hooks->StartupHook!=NULL ) {
- os_sys.hooks->StartupHook();
- }
-
- /* Alarm autostart */
- for(int j=0; j < Os_CfgGetAlarmCnt(); j++ ) {
- OsAlarmType *alarmPtr;
- alarmPtr = Os_CfgGetAlarmObj(j);
- if(alarmPtr->autostartPtr != NULL ) {
- const OsAlarmAutostartType *autoPtr = alarmPtr->autostartPtr;
-
- if( os_sys.appMode & autoPtr->appModeRef) {
- if( autoPtr->autostartType == ALARM_AUTOSTART_ABSOLUTE ) {
- SetAbsAlarm(j,autoPtr->alarmTime, autoPtr->cycleTime);
- } else {
- SetRelAlarm(j,autoPtr->alarmTime, autoPtr->cycleTime);
- }
- }
- }
- }
-
- Os_SchTblAutostart();
-
- // Set up the systick interrupt
- {
- uint32_t sys_freq = McuE_GetSystemClock();
- Os_SysTickInit();
- Os_SysTickStart(sys_freq/OsTickFreq);
- }
-
- /* Find highest Autostart task */
- {
- OsPcbType *iterPcbPtr;
- OsPriorityType topPrio = -1;
-
- TAILQ_FOREACH(iterPcbPtr,& os_sys.pcb_head,pcb_list) {
- if( iterPcbPtr->autostart ) {
- if( iterPcbPtr->prio > topPrio ) {
- tmp_pcb = iterPcbPtr;
- topPrio = iterPcbPtr->prio;
- }
- }
- }
- }
-
- // Swap in prio proc.
- {
- // FIXME: Do this in a more structured way.. setting os_sys.curr_pcb manually is not the way to go..
- os_sys.curr_pcb = tmp_pcb;
-
- // register this auto-start activation
- assert(tmp_pcb->activations < tmp_pcb->activationLimit);
- tmp_pcb->activations++;
-
- // NOTE! We don't go for os_swap_context() here..
- // first arg(NULL) is dummy only
- Os_TaskSwapContextTo(NULL,tmp_pcb);
- // We should not return here
- assert(0);
- }
-}
-#if 0
-static void os_start( void ) {
-
-}
-#endif
-
-#define TEST_DATA 12345
-int test_data = TEST_DATA;
-int test_bss = 0;
-
-
-void noooo( void ) {
- while(1);
-}
-
-extern void EcuM_Init();
-int main( void )
-{
- EcuM_Init();
-
-}
-
-/**
- * Starts the OS
- *
- * @param Mode - Application mode to start in
- *
- */
-void StartOS(AppModeType Mode) {
-
- /* Check link file */
- if (TEST_DATA != test_data) {
- noooo();
- }
-
- if (test_bss != 0) {
- noooo();
- }
-
- os_sys.appMode = Mode;
-
- Os_CfgValidate();
-
- os_start();
-
- /** @req OS424 */
- assert(0);
-}
-
-/**
- * OS shutdown
- *
- * @param Error - Reason for shutdown
- */
-
-/** @req OS071 */
-void ShutdownOS( StatusType Error ) {
-
- if( os_sys.hooks->ShutdownHook != NULL ) {
- os_sys.hooks->ShutdownHook(Error);
- }
-
- Irq_Disable();
- /** @req OS425 */
- while(1) { }
-
-}
-
-
-
+/* -------------------------------- Arctic Core ------------------------------\r
+ * Arctic Core - the open source AUTOSAR platform http://arccore.com\r
+ *\r
+ * Copyright (C) 2009 ArcCore AB <contact@arccore.com>\r
+ *\r
+ * This source code is free software; you can redistribute it and/or modify it\r
+ * under the terms of the GNU General Public License version 2 as published by the\r
+ * Free Software Foundation; See <http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt>.\r
+ *\r
+ * This program is distributed in the hope that it will be useful, but\r
+ * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY\r
+ * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License\r
+ * for more details.\r
+ * -------------------------------- Arctic Core ------------------------------*/\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
+/* ----------------------------[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
+ *\r
+ * @param pcb ram data\r
+ * @param r_pcb rom data\r
+ */\r
+\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->constPtr = r_pcb;\r
+}\r
+\r
+static _Bool init_os_called = 0;\r
+\r
+/**\r
+ * Initialization of kernel structures and start of the first\r
+ * task.\r
+ */\r
+\r
+void InitOS( void ) {\r
+ int i;\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(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 = 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_IsrGetStackInfo(&intStack);\r
+ // TODO: 16 is arch dependent\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_TASK_CNT; i++) {\r
+ tmpPcbPtr = Os_TaskGet(i);\r
+\r
+ copyPcbParts(tmpPcbPtr,&Os_TaskConstList[i]);\r
+ Os_TaskContextInit(tmpPcbPtr);\r
+ TAILQ_INIT(&tmpPcbPtr->resourceHead);\r
+\r
+#if 0\r
+ Os_AddTask(tmpPcbPtr);\r
+#endif\r
+\r
+ DEBUG(DEBUG_LOW,"pid:%d name:%s prio:%d\n",tmpPcbPtr->pid,tmpPcbPtr->name,tmpPcbPtr->prio);\r
+ }\r
+\r
+ Os_ResourceInit();\r
+\r
+ // Now all tasks should be created.\r
+}\r
+\r
+static void os_start( void ) {\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
+ Irq_Disable();\r
+\r
+ assert(init_os_called);\r
+\r
+ /* 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
+ }\r
+\r
+\r
+#if (OS_USE_APPLICATIONS == STD_ON)\r
+ /* Start applications */\r
+ Os_ApplStart();\r
+#endif\r
+\r
+\r
+ /* Alarm autostart */\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
+ uint32_t sys_freq = McuE_GetSystemClock();\r
+ Os_SysTickInit();\r
+ Os_SysTickStart(sys_freq/OsTickFreq);\r
+ }\r
+\r
+ /* Find highest Autostart task */\r
+ {\r
+ OsTaskVarType *iterPcbPtr;\r
+ OsPriorityType topPrio = -1;\r
+\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.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,tmpPcbPtr);\r
+ // We should not return here\r
+ assert(0);\r
+ }\r
+}\r
+#if 0\r
+static void os_start( void ) {\r
+\r
+}\r
+#endif\r
+\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
+#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
+\r
+/**\r
+ * Starts the OS\r
+ *\r
+ * @param Mode - Application mode to start in\r
+ *\r
+ */\r
+void StartOS(AppModeType Mode) {\r
+\r
+ Os_Sys.appMode = Mode;\r
+\r
+ Os_CfgValidate();\r
+\r
+ os_start();\r
+\r
+ /** @req OS424 */\r
+ assert(0);\r
+}\r
+\r
+/**\r
+ * OS shutdown\r
+ *\r
+ * @param Error - Reason for shutdown\r
+ */\r
+\r
+/** @req OS071 */\r
+void ShutdownOS( StatusType Error ) {\r
+\r
+ if( Os_Sys.hooks->ShutdownHook != NULL ) {\r
+ Os_Sys.hooks->ShutdownHook(Error);\r
+ }\r
+\r
+ Irq_Disable();\r
+ /** @req OS425 */\r
+ while(1) { }\r
+\r
+}\r
+\r
+\r
+\r