]> rtime.felk.cvut.cz Git - arc.git/blob - examples/system_hooks.c
Fixed compile bug for TMS570. Added terminal support for Code Composer.
[arc.git] / examples / system_hooks.c
1 \r
2 /* -------------------------------- Arctic Core ------------------------------\r
3  * Arctic Core - the open source AUTOSAR platform http://arccore.com\r
4  *\r
5  * Copyright (C) 2009  ArcCore AB <contact@arccore.com>\r
6  *\r
7  * This source code is free software; you can redistribute it and/or modify it\r
8  * under the terms of the GNU General Public License version 2 as published by the\r
9  * Free Software Foundation; See <http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt>.\r
10  *\r
11  * This program is distributed in the hope that it will be useful, but\r
12  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY\r
13  * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License\r
14  * for more details.\r
15  * -------------------------------- Arctic Core ------------------------------*/\r
16 /*\r
17  * DESCRIPTION\r
18  *   Hold all OSEK system hooks.\r
19  */\r
20 \r
21 /* ----------------------------[includes]------------------------------------*/\r
22 \r
23 #include <stdint.h>\r
24 #include "Os.h"\r
25 #include <assert.h>\r
26 \r
27 #include "Mcu.h"\r
28 \r
29 /* ----------------------------[private define]------------------------------*/\r
30 #define ERROR_LOG_SIZE 1\r
31 \r
32 //#define USE_LDEBUG_PRINTF     1\r
33 #include "debug.h"\r
34 \r
35 /* ----------------------------[private macro]-------------------------------*/\r
36 \r
37 \r
38 /* ----------------------------[private typedef]-----------------------------*/\r
39 \r
40 typedef struct ErrorEntry {\r
41         StatusType              error;\r
42         OsErrorType     info;\r
43         TaskType        taskId;\r
44         OsServiceIdType serviceId;\r
45 } ErrorEntryType;\r
46 \r
47 \r
48 typedef struct ErrorLog {\r
49         int                     index;\r
50         ErrorEntryType  log[ERROR_LOG_SIZE];\r
51 } ErrorLogType;\r
52 \r
53 struct LogBad {\r
54         uint32_t param1;\r
55         uint32_t param2;\r
56         uint32_t param3;\r
57         TaskType taskId;\r
58         OsServiceIdType serviceId;\r
59         StatusType error;\r
60 } LogBadType;\r
61 \r
62 \r
63 /* ----------------------------[private function prototypes]-----------------*/\r
64 /* ----------------------------[private variables]---------------------------*/\r
65 ErrorLogType ErrorLog;\r
66 /* ----------------------------[private functions]---------------------------*/\r
67 /* ----------------------------[public functions]----------------------------*/\r
68 \r
69 /**\r
70  *
71  * @param FatalError
72  * @return
73  */\r
74 ProtectionReturnType ProtectionHook( StatusType FatalError ) {\r
75         printf("## ProtectionHook\n");\r
76         return PRO_KILLAPPL;\r
77 }\r
78 \r
79 /**\r
80  *
81  */\r
82 void StartupHook( void ) {\r
83 //      LDEBUG_PRINTF("## StartupHook\n");\r
84 \r
85         uint32_t sys_freq = McuE_GetSystemClock();\r
86         (void)sys_freq;\r
87         LDEBUG_PRINTF("Sys clock %d Hz\n",sys_freq);\r
88 }\r
89 \r
90 /**\r
91  *
92  * @param Error
93  */\r
94 void ShutdownHook( StatusType error ) {\r
95         LDEBUG_FPUTS("## ShutdownHook\n");\r
96         (void)error;\r
97         while(1) {\r
98                 //err = err;\r
99                 ;\r
100         }\r
101 }\r
102 \r
103 /**\r
104  *
105  * @param error
106  */\r
107 void ErrorHook( StatusType error ) {\r
108 \r
109         TaskType task;\r
110         static struct LogBad LogBad[ERROR_LOG_SIZE];\r
111         static uint8_t ErrorCount = 0;\r
112 \r
113         GetTaskID(&task);\r
114 \r
115 \r
116         OsServiceIdType service = OSErrorGetServiceId();\r
117 \r
118         /* Grab the arguments to the functions\r
119          * This is the standard way, see 11.2 in OSEK spec\r
120          */\r
121         switch(service) {\r
122         case OSServiceId_SetRelAlarm:\r
123         {\r
124                 // Read the arguments to the faulty functions...\r
125                 AlarmType alarm_id = OSError_SetRelAlarm_AlarmId;\r
126                 TickType increment = OSError_SetRelAlarm_Increment;\r
127                 TickType cycle = OSError_SetRelAlarm_Cycle;\r
128                 (void)alarm_id;\r
129                 (void)increment;\r
130                 (void)cycle;\r
131 \r
132                 // ... Handle this some way.\r
133                 break;\r
134         }\r
135         /*\r
136          * The same pattern as above applies for all other OS functions.\r
137          * See Os.h for names and definitions.\r
138          */\r
139 \r
140         default:\r
141                 break;\r
142         }\r
143 \r
144         LDEBUG_PRINTF("## ErrorHook err=%u\n",Error);\r
145 \r
146         /* Log the errors in a buffer for later review */\r
147         LogBad[ErrorCount].param1 = os_error.param1;\r
148         LogBad[ErrorCount].param2 = os_error.param2;\r
149         LogBad[ErrorCount].param3 = os_error.param3;\r
150         LogBad[ErrorCount].serviceId = service;\r
151         LogBad[ErrorCount].taskId = task;\r
152         LogBad[ErrorCount].error = error;\r
153 \r
154         ErrorCount++;\r
155 \r
156         // Stall if buffer is full.\r
157         while(ErrorCount >= ERROR_LOG_SIZE);\r
158 }\r
159 \r
160 \r
161 /**\r
162  *
163  */\r
164 void PreTaskHook( void ) {\r
165         StatusType rv;\r
166         TaskType task;\r
167         TaskStateType state;\r
168 \r
169         rv = GetTaskID(&task);\r
170         assert( rv == E_OK );\r
171         LDEBUG_PRINTF("## PreTaskHook, taskid=%d\n",task);\r
172         rv = GetTaskState(task,&state);\r
173         assert( rv == E_OK );\r
174         assert( state == TASK_STATE_RUNNING );\r
175 }\r
176 \r
177 /**\r
178  *
179  */\r
180 void PostTaskHook( void ) {\r
181         StatusType rv;\r
182         TaskType task;\r
183         TaskStateType state;\r
184 \r
185         rv = GetTaskID(&task);\r
186         assert( rv == E_OK );\r
187         LDEBUG_PRINTF("## PostTaskHook, taskid=%d\n",task);\r
188         rv = GetTaskState(task,&state);\r
189         assert( rv == E_OK );\r
190         assert( state == TASK_STATE_RUNNING );\r
191 \r
192 }\r
193 \r