]> rtime.felk.cvut.cz Git - arc.git/blob - arch/ppc/mpc55xx/drivers/WdgM.c
Initial commit.
[arc.git] / arch / ppc / mpc55xx / drivers / WdgM.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
18
19
20
21
22
23 #include "WdgM.h"\r
24 #include "Mcu.h"\r
25 \r
26 const WdgM_ConfigType *wdgMConfigPtr;\r
27 static WdgM_SupervisedStatusType WdgM_GlobalSupervisionStatus = WDBG_ALIVE_OK;\r
28 \r
29 Std_ReturnType WdgM_UpdateAliveCounter (WdgM_SupervisedEntityIdType SEid)\r
30 {\r
31   Wdgm_SupervisionType *supervisionPtr = &(wdgMConfigPtr->Wdgm_SupervisionPtr)[SEid];\r
32 \r
33   if (supervisionPtr->ActivationStatus == WDBG_SUPERVISION_ENABLED)\r
34   {\r
35     supervisionPtr->AliveCounter++;\r
36   }\r
37   return (E_OK);\r
38 }\r
39 \r
40 Std_ReturnType WdgM_ActivateAliveSupervision (WdgM_SupervisedEntityIdType SEid)\r
41 {\r
42   Wdgm_SupervisionType *supervisionPtr = &(wdgMConfigPtr->Wdgm_SupervisionPtr)[SEid];\r
43 \r
44   supervisionPtr->ActivationStatus = WDBG_SUPERVISION_ENABLED;\r
45 \r
46   return (E_OK);\r
47 }\r
48 \r
49 Std_ReturnType WdgM_DeactivateAliveSupervision (WdgM_SupervisedEntityIdType SEid)\r
50 {\r
51   Wdgm_SupervisionType *supervisionPtr = &(wdgMConfigPtr->Wdgm_SupervisionPtr)[SEid];\r
52 \r
53   supervisionPtr->ActivationStatus = WDBG_SUPERVISION_DISABLED;\r
54   return (E_OK);\r
55 }\r
56 \r
57 void WdgM_Init(const WdgM_ConfigType *ConfigPtr)\r
58 {\r
59   WdgM_SupervisedEntityIdType SEid;\r
60   Wdgm_SupervisionType *supervisionPtr;\r
61   WdgM_SupervisedEntityType* supervisedEntityPtr;\r
62 \r
63   for (SEid = 0; SEid < WDBG_NBR_OF_ALIVE_SIGNALS; SEid++)\r
64   {\r
65     supervisionPtr = &(ConfigPtr->Wdgm_SupervisionPtr)[SEid];\r
66     supervisedEntityPtr = (WdgM_SupervisedEntityType*)&(ConfigPtr->WdgM_SupervisedEntityPtr)[SEid];\r
67     supervisionPtr->ActivationStatus = supervisedEntityPtr->WdgM_ActivationStatus;\r
68   }\r
69   wdgMConfigPtr = ConfigPtr;\r
70 \r
71 }\r
72 \r
73 void WdgM_MainFunction_AliveSupervision (void)\r
74 {\r
75   WdgM_SupervisedEntityIdType SEid;\r
76   Wdgm_SupervisionType *supervisionPtr;\r
77   const WdgM_SupervisedEntityType *entityPtr;\r
78   WdgM_SupervisionCounterType aliveCalc, nSC, nAl, eai;\r
79   WdgM_SupervisedStatusType maxLocal = WDBG_ALIVE_OK;\r
80   static WdgM_SupervisionCounterType expiredSupervisionCycles = 0;\r
81 \r
82   for (SEid = 0; SEid < WDBG_NBR_OF_ALIVE_SIGNALS; SEid++)\r
83   {\r
84     supervisionPtr = &(wdgMConfigPtr->Wdgm_SupervisionPtr)[SEid];\r
85     entityPtr      = &(wdgMConfigPtr->WdgM_SupervisedEntityPtr)[SEid];\r
86 \r
87     if (WDBG_SUPERVISION_ENABLED == supervisionPtr->ActivationStatus)\r
88     {\r
89       supervisionPtr->SupervisionCycle++;\r
90       /* Only perform supervision on the reference cycle. */\r
91       if (supervisionPtr->SupervisionCycle == entityPtr->WdgM_SupervisionReferenceCycle)\r
92       {\r
93         /* Alive algorithm. *\r
94          * n (Al) - n(SC) + EAI == 0 */\r
95         if (entityPtr->WdgM_ExpectedAliveIndications > entityPtr->WdgM_SupervisionReferenceCycle)\r
96         {\r
97           /* Scenario A */\r
98           eai = -entityPtr->WdgM_ExpectedAliveIndications + 1;\r
99 \r
100         }\r
101         else\r
102         {\r
103           /* Scenario B */\r
104           eai = entityPtr->WdgM_SupervisionReferenceCycle - 1;\r
105         }\r
106         nSC = supervisionPtr->SupervisionCycle;\r
107         nAl = supervisionPtr->AliveCounter;\r
108         aliveCalc = nAl - nSC + eai;\r
109 \r
110         if ((aliveCalc <= entityPtr->WdgM_MaxMargin) &&\r
111             (aliveCalc >= -entityPtr->WdgM_MinMargin))\r
112         {\r
113           /* Entity alive OK. */\r
114           supervisionPtr->SupervisionStatus = WDBG_ALIVE_OK;\r
115         }\r
116         else\r
117         {\r
118           /* Entity alive NOK. */\r
119           supervisionPtr->SupervisionStatus = WDBG_ALIVE_FAILED;\r
120           if (WDBG_ALIVE_FAILED > maxLocal)\r
121           {\r
122             maxLocal = WDBG_ALIVE_FAILED;\r
123           }\r
124         }\r
125 \r
126         if (WDBG_ALIVE_FAILED == supervisionPtr->SupervisionStatus)\r
127         {\r
128           if (supervisionPtr->NbrOfFailedRefCycles > entityPtr->WdgM_FailedSupervisionReferenceCycleTolerance)\r
129           {\r
130             supervisionPtr->SupervisionStatus = WDBG_ALIVE_EXPIRED;\r
131             if (WDBG_ALIVE_EXPIRED > maxLocal)\r
132             {\r
133               maxLocal = WDBG_ALIVE_EXPIRED;\r
134             }\r
135           }\r
136           else\r
137           {\r
138             supervisionPtr->NbrOfFailedRefCycles++;\r
139           }\r
140         }\r
141 \r
142         /* Reset counters. */\r
143         supervisionPtr->SupervisionCycle = 0;\r
144         supervisionPtr->AliveCounter = 0;\r
145       }\r
146     }\r
147   }\r
148 \r
149   /* Try to heal global status. */\r
150   if (WDBG_ALIVE_EXPIRED != WdgM_GlobalSupervisionStatus)\r
151   {\r
152     WdgM_GlobalSupervisionStatus = maxLocal;\r
153   }\r
154   else\r
155   {\r
156     WdgM_GlobalSupervisionStatus = WDBG_ALIVE_EXPIRED;\r
157   }\r
158 \r
159   if (WDBG_ALIVE_EXPIRED == WdgM_GlobalSupervisionStatus)\r
160   {\r
161     expiredSupervisionCycles++;\r
162   }\r
163 \r
164   if (expiredSupervisionCycles >= wdgMConfigPtr->WdgM_ExpiredSupervisionCycleTolerance)\r
165   {\r
166     WdgM_GlobalSupervisionStatus = WDBG_ALIVE_STOPPED;\r
167   }\r
168 }\r
169 \r
170 boolean WdgM_IsAlive(void)\r
171 {\r
172 \r
173   if ( WDBG_ALIVE_STOPPED > WdgM_GlobalSupervisionStatus )\r
174   {\r
175     return (TRUE);\r
176   }\r
177   else\r
178   {\r
179     return (FALSE);\r
180   }\r
181 }\r
182 \r
183 void WdgM_MainFunction_Trigger (void)\r
184 {\r
185   if ( WdgM_IsAlive() )\r
186   {\r
187     KickWatchdog();\r
188   }\r
189 }\r
190 \r
191 \r