]> rtime.felk.cvut.cz Git - arc.git/blob - examples/tiny/tiny.c
d7f7e7ff5a320e9cd83c594ad42e8aded29c5e97
[arc.git] / examples / tiny / tiny.c
1 /* -------------------------------- Arctic Core ------------------------------\r
2  * Arctic Core - the open source AUTOSAR platform http://arccore.com\r
3  *\r
4  * Copyright (C) 2009  ArcCore AB <contact@arccore.com>\r
5  *\r
6  * This source code is free software; you can redistribute it and/or modify it\r
7  * under the terms of the GNU General Public License version 2 as published by the\r
8  * Free Software Foundation; See <http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt>.\r
9  *\r
10  * This program is distributed in the hope that it will be useful, but\r
11  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY\r
12  * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License\r
13  * for more details.\r
14  * -------------------------------- Arctic Core ------------------------------*/\r
15 \r
16 \r
17 #include "Os.h"\r
18 #include "Mcu.h"\r
19 #include "arc.h"\r
20 \r
21 #define USE_LDEBUG_PRINTF\r
22 #include "debug.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         LDEBUG_PRINTF("[%08u] btask_3 start\n", (unsigned)GetOsTick() );\r
36 \r
37         GetTaskID(&currTask);\r
38         Os_Arc_GetStackInfo(currTask,&si);\r
39         LDEBUG_PRINTF("btask_3: %u%% stack usage\n",\r
40                         (unsigned)OS_STACK_USAGE(&si));\r
41 \r
42         TerminateTask();\r
43 }\r
44 \r
45 /**\r
46  * An extended task is auto-started and is also triggered by an alarm\r
47  * that sets event 2.\r
48  */\r
49 \r
50 void etask_1( void ) {\r
51         volatile float tryFloatingPoint = 0.0F;\r
52         StackInfoType si;\r
53         TaskType currTask;\r
54 \r
55         LDEBUG_PRINTF("etask_1 start\n");\r
56         for(;;) {\r
57                 SetEvent(TASK_ID_etask_2,EVENT_MASK_EVENT_1);\r
58                 WaitEvent(EVENT_MASK_EVENT_2);\r
59                 ClearEvent(EVENT_MASK_EVENT_2);\r
60                 tryFloatingPoint += 1.0F;\r
61                 GetTaskID(&currTask);\r
62                 Os_Arc_GetStackInfo(currTask,&si);\r
63                 LDEBUG_PRINTF("etask_1: %u%% stack usage\n",\r
64                                 (unsigned)OS_STACK_USAGE(&si));\r
65 \r
66         }\r
67 }\r
68 \r
69 /**\r
70  * An extended task that receives events from someone\r
71  * and activates task: btask_3.\r
72  */\r
73 void etask_2( void ) {\r
74         LDEBUG_PRINTF("etask_2 start\n");\r
75 \r
76         for(;;) {\r
77                 WaitEvent(EVENT_MASK_EVENT_1);\r
78                 ClearEvent(EVENT_MASK_EVENT_1);\r
79                 ActivateTask(TASK_ID_btask_3);\r
80                 {\r
81                         StackInfoType si;\r
82                         TaskType currTask;\r
83                         GetTaskID(&currTask);\r
84                         Os_Arc_GetStackInfo(currTask,&si);\r
85                         LDEBUG_PRINTF("etask_2: %u%% stack usage\n",\r
86                                         (unsigned)OS_STACK_USAGE(&si));\r
87                 }\r
88         }\r
89 }\r
90 \r
91 \r
92 /*\r
93  * Functions that must be supplied by the example\r
94  */\r
95 \r
96 void OsIdle( void ) {\r
97         for(;;);\r
98 }\r
99 \r
100 \r
101 /* Global hooks */\r
102 ProtectionReturnType ProtectionHook( StatusType FatalError ) {\r
103         LDEBUG_PRINTF("## ProtectionHook\n");\r
104         return PRO_KILLAPPL;\r
105 }\r
106 \r
107 void StartupHook( void ) {\r
108         uint32_t sys_freq = McuE_GetSystemClock();\r
109 \r
110         LDEBUG_PRINTF("## StartupHook\n");\r
111 \r
112         LDEBUG_PRINTF("Sys clock %u Hz\n",(unsigned)sys_freq);\r
113 }\r
114 \r
115 void ShutdownHook( StatusType Error ) {\r
116         LDEBUG_PRINTF("## ShutdownHook\n");\r
117         while(1);\r
118 }\r
119 \r
120 struct LogBad_s {\r
121         uint32_t param1;\r
122         uint32_t param2;\r
123         uint32_t param3;\r
124         TaskType taskId;\r
125         OsServiceIdType serviceId;\r
126         StatusType error;\r
127 };\r
128 \r
129 void ErrorHook( StatusType Error ) {\r
130 \r
131         TaskType task;\r
132         static struct LogBad_s LogBad[ERROR_LOG_SIZE];\r
133         static uint8_t ErrorCount = 0;\r
134 \r
135         GetTaskID(&task);\r
136 \r
137 \r
138         OsServiceIdType service = OSErrorGetServiceId();\r
139 \r
140         /* Grab the arguments to the functions\r
141          * This is the standard way, see 11.2 in OSEK spec\r
142          */\r
143         switch(service) {\r
144         case OSServiceId_SetRelAlarm:\r
145         {\r
146                 // Read the arguments to the faulty functions...\r
147                 AlarmType alarm_id = OSError_SetRelAlarm_AlarmId;\r
148                 TickType increment = OSError_SetRelAlarm_Increment;\r
149                 TickType cycle = OSError_SetRelAlarm_Cycle;\r
150                 (void)alarm_id;\r
151                 (void)increment;\r
152                 (void)cycle;\r
153 \r
154                 // ... Handle this some way.\r
155                 break;\r
156         }\r
157         /*\r
158          * The same pattern as above applies for all other OS functions.\r
159          * See Os.h for names and definitions.\r
160          */\r
161 \r
162         default:\r
163                 break;\r
164         }\r
165 \r
166         LDEBUG_PRINTF("## ErrorHook err=%u\n",Error);\r
167 \r
168         /* Log the errors in a buffer for later review */\r
169         LogBad[ErrorCount].param1 = os_error.param1;\r
170         LogBad[ErrorCount].param2 = os_error.param2;\r
171         LogBad[ErrorCount].param3 = os_error.param3;\r
172         LogBad[ErrorCount].serviceId = service;\r
173         LogBad[ErrorCount].taskId = task;\r
174         LogBad[ErrorCount].error = Error;\r
175 \r
176         ErrorCount++;\r
177 \r
178         // Stall if buffer is full.\r
179         while(ErrorCount >= ERROR_LOG_SIZE);\r
180 }\r
181 \r
182 void PreTaskHook( void ) {\r
183         TaskType task;\r
184         GetTaskID(&task);\r
185 //      LDEBUG_PRINTF("## PreTaskHook, taskid=%u\n",task);\r
186 }\r
187 \r
188 void PostTaskHook( void ) {\r
189         TaskType task;\r
190         GetTaskID(&task);\r
191 //      LDEBUG_PRINTF("## PostTaskHook, taskid=%u\n",task);\r
192 }\r
193 \r