]> rtime.felk.cvut.cz Git - arc.git/blob - examples/simple/simple_main.c
Again, loads of refactoring and removing and adding files.
[arc.git] / examples / simple / simple_main.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 \r
17 #include "Os.h"\r
18 #include "Mcu.h"\r
19 #include "arc.h"
20 \r
21 #define USE_DEBUG\r
22 #include "Trace.h"\r
23 \r
24 // How many errors to keep in error log.\r
25 #define ERROR_LOG_SIZE 20\r
26 \r
27 \r
28 /**\r
29  * Just an example of a basic task.\r
30  */\r
31 \r
32 void btask_3( void ) {\r
33         StackInfoType si;\r
34         TaskType currTask;\r
35         dbg_printf("[%08d] btask_3 start\n", GetOsTick() );\r
36 \r
37         GetTaskID(&currTask);\r
38         Os_Arc_GetStackInfo(currTask,&si);\r
39         dbg_printf("btask_3: Stack usage %d%%\n",OS_STACK_USAGE(&si));\r
40 \r
41         TerminateTask();\r
42 }\r
43 \r
44 /**\r
45  * An extended task is auto-started and is also triggered by an alarm\r
46  * that sets event 2.\r
47  */\r
48 \r
49 void etask_1( void ) {\r
50         volatile float tryFloatingPoint = 0.0F;\r
51         StackInfoType si;\r
52         TaskType currTask;\r
53 \r
54 \r
55         dbg_printf("etask_1 start\n");\r
56         for(;;) {\r
57                 SetEvent(TASK_ID_etask_2,1);\r
58                 WaitEvent(2);\r
59                 ClearEvent(2);\r
60                 tryFloatingPoint += 1.0F;\r
61                 GetTaskID(&currTask);\r
62                 Os_Arc_GetStackInfo(currTask,&si);\r
63                 dbg_printf("etask_1: Stack usage %d%% \n",OS_STACK_USAGE(&si));\r
64 \r
65         }\r
66 }\r
67 \r
68 /**\r
69  * An extended task that receives events from someone\r
70  * and activates task: btask_3.
71  */\r
72 void etask_2( void ) {\r
73         dbg_printf("etask_2 start\n");\r
74 \r
75         for(;;) {\r
76                 WaitEvent(1);\r
77                 ClearEvent(1);\r
78                 ActivateTask(TASK_ID_btask_3);\r
79                 {\r
80                         StackInfoType si;\r
81                         TaskType currTask;\r
82                         GetTaskID(&currTask);\r
83                         Os_Arc_GetStackInfo(currTask,&si);\r
84                         dbg_printf("etask_1: Stack usage %d%% \n",OS_STACK_USAGE(&si));\r
85                 }\r
86         }\r
87 }\r
88 \r
89 \r
90 /*\r
91  * Functions that must be supplied by the example\r
92  */\r
93 \r
94 void OsIdle( void ) {\r
95         for(;;);\r
96 }\r
97 \r
98 \r
99 /* Global hooks */\r
100 ProtectionReturnType ProtectionHook( StatusType FatalError ) {\r
101         dbg_printf("## ProtectionHook\n");\r
102         return PRO_KILLAPPL;\r
103 }\r
104 \r
105 void StartupHook( void ) {\r
106         uint32_t sys_freq = McuE_GetSystemClock();\r
107 \r
108         dbg_printf("## StartupHook\n");\r
109 \r
110         dbg_printf("Sys clock %d Hz\n",sys_freq);\r
111 }\r
112 \r
113 void ShutdownHook( StatusType Error ) {\r
114         dbg_printf("## ShutdownHook\n");\r
115         while(1);\r
116 }\r
117 \r
118 struct LogBad_s {\r
119         uint32_t param1;\r
120         uint32_t param2;\r
121         uint32_t param3;\r
122         TaskType taskId;\r
123         OsServiceIdType serviceId;\r
124         StatusType error;\r
125 };\r
126 \r
127 void ErrorHook( StatusType Error ) {\r
128 \r
129         TaskType task;\r
130         static struct LogBad_s LogBad[ERROR_LOG_SIZE];\r
131         static uint8_t ErrorCount = 0;\r
132 \r
133         GetTaskID(&task);\r
134 \r
135 \r
136         OsServiceIdType service = OSErrorGetServiceId();\r
137 \r
138         /* Grab the arguments to the functions\r
139          * This is the standard way, see 11.2 in OSEK spec
140          */\r
141         switch(service) {\r
142         case OSServiceId_SetRelAlarm:\r
143         {\r
144                 // Read the arguments to the faulty functions...
145                 /* (Commented to remove warnings)
146                 AlarmType alarm_id = OSError_SetRelAlarm_AlarmId;\r
147                 TickType increment = OSError_SetRelAlarm_Increment;\r
148                 TickType cycle = OSError_SetRelAlarm_Cycle;     */
149                 // ... Handle this some way.\r
150                 break;\r
151         }\r
152         /*\r
153          * The same pattern as above applies for all other OS functions.\r
154          * See Os.h for names and definitions.
155          */\r
156 \r
157         default:\r
158                 break;\r
159         }\r
160 \r
161         dbg_printf("## ErrorHook err=%d\n",Error);\r
162 \r
163         /* Log the errors in a buffer for later review */\r
164         LogBad[ErrorCount].param1 = os_error.param1;\r
165         LogBad[ErrorCount].param2 = os_error.param2;\r
166         LogBad[ErrorCount].param3 = os_error.param3;\r
167         LogBad[ErrorCount].serviceId = service;\r
168         LogBad[ErrorCount].taskId = task;\r
169         LogBad[ErrorCount].error = Error;\r
170 \r
171         ErrorCount++;\r
172 \r
173         // Stall if buffer is full.\r
174         while(ErrorCount >= ERROR_LOG_SIZE);\r
175 }\r
176 \r
177 void PreTaskHook( void ) {\r
178         TaskType task;\r
179         GetTaskID(&task);\r
180 //      dbg_printf("## PreTaskHook, taskid=%d\n",task);\r
181 }\r
182 \r
183 void PostTaskHook( void ) {\r
184         TaskType task;\r
185         GetTaskID(&task);\r
186 //      dbg_printf("## PostTaskHook, taskid=%d\n",task);\r
187 }\r
188 \r