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