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