1 /* -------------------------------- Arctic Core ------------------------------
\r
2 * Arctic Core - the open source AUTOSAR platform http://arccore.com
\r
4 * Copyright (C) 2009 ArcCore AB <contact@arccore.com>
\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
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
14 * -------------------------------- Arctic Core ------------------------------*/
\r
16 //lint -emacro(904,VALIDATE,VALIDATE_RV,VALIDATE_NO_RV) //904 PC-Lint exception to MISRA 14.7 (validate macros).
\r
18 #include "Std_Types.h"
\r
20 #include "EcuM_Generated_Types.h"
\r
21 #include "EcuM_Internals.h"
\r
23 #if defined(USE_DEM)
\r
26 #if defined(USE_NVM)
\r
31 static uint32 internal_data_run_state_timeout = 0;
\r
32 #if defined(USE_NVM)
\r
33 static uint32 internal_data_go_off_one_state_timeout = 0;
\r
34 static NvM_RequestResultType writeAllResult;
\r
37 static uint32 internal_data_go_sleep_state_timeout = 0;
\r
39 #ifdef CFG_ECUM_USE_SERVICE_COMPONENT
\r
40 /** @req EcuM2749 */
\r
41 static Rte_ModeType_EcuM_Mode currentMode;
\r
43 void set_current_state(EcuM_StateType state) {
\r
44 /* Update the state */
\r
45 internal_data.current_state = state;
\r
47 Rte_ModeType_EcuM_Mode newMode = currentMode;
\r
49 case ECUM_STATE_WAKEUP:
\r
50 case ECUM_STATE_WAKEUP_ONE:
\r
51 case ECUM_STATE_WAKEUP_VALIDATION:
\r
52 case ECUM_STATE_WAKEUP_REACTION:
\r
53 case ECUM_STATE_WAKEUP_TWO:
\r
54 case ECUM_STATE_SLEEP:
\r
55 case ECUM_STATE_SHUTDOWN:
\r
56 newMode = RTE_MODE_EcuM_Mode_SLEEP;
\r
58 case ECUM_STATE_GO_SLEEP:
\r
59 if( internal_data.shutdown_target == ECUM_STATE_SLEEP ) {
\r
60 newMode = RTE_MODE_EcuM_Mode_SLEEP; /** @req EcuM2752 */
\r
63 case ECUM_STATE_GO_OFF_ONE:
\r
64 case ECUM_STATE_GO_OFF_TWO:
\r
65 newMode = RTE_MODE_EcuM_Mode_SHUTDOWN;
\r
67 case ECUM_STATE_WAKEUP_TTII:
\r
68 if( internal_data.shutdown_target == ECUM_STATE_SLEEP ) {
\r
69 newMode = RTE_MODE_EcuM_Mode_WAKE_SLEEP; /** @req EcuM2752 */
\r
72 case ECUM_STATE_PREP_SHUTDOWN:
\r
73 case ECUM_STATE_APP_POST_RUN: /* Assuming this is same as RUN_III */
\r
74 newMode = RTE_MODE_EcuM_Mode_POST_RUN;
\r
76 case ECUM_STATE_APP_RUN: /* Assuming this is same as RUN_II */
\r
77 newMode = RTE_MODE_EcuM_Mode_RUN;
\r
79 case ECUM_STATE_STARTUP_TWO:
\r
80 newMode = RTE_MODE_EcuM_Mode_STARTUP;
\r
87 if( newMode != currentMode ) {
\r
88 currentMode = newMode;
\r
89 Rte_Switch_EcuM_CurrentMode_currentMode(currentMode); /** @req EcuM2750 */
\r
97 * - Called from EcuM_StartupTwo()
\r
102 void EcuM_enter_run_mode(void){
\r
103 set_current_state(ECUM_STATE_APP_RUN);
\r
104 EcuM_OnEnterRun(); /** @req EcuM2308 */
\r
106 #if defined(USE_WDGM)
\r
107 /* This seems strange, should be in FW instead */
\r
108 WdgM_SetMode(internal_data.config->EcuMWdgMConfig->EcuMWdgMRunMode);
\r
111 #if defined(USE_COMM)
\r
113 * Loop over all channels that have requested run,
\r
114 * ie EcuM_ComM_RequestRUN()
\r
117 uint32 cMask = internal_data.run_comm_requests;
\r
120 for (; cMask; cMask &= ~(1ul << channel)) {
\r
121 channel = ilog2(cMask);
\r
122 ComM_EcuM_RunModeIndication(channel);
\r
127 /* We have a configurable minimum time (EcuMRunMinimumDuration)
\r
128 * we have to stay in RUN state */
\r
129 internal_data_run_state_timeout = internal_data.config->EcuMRunMinimumDuration / ECUM_MAIN_FUNCTION_PERIOD; /** @req EcuM2310 */
\r
133 //--------- Local functions ------------------------------------------------------------------------------------------------
\r
138 * Enter GO SLEEP state ( soon in state ECUM_STATE_GO_SLEEP)
\r
140 static inline void enter_go_sleep_mode(void){
\r
141 EcuM_WakeupSourceType wakeupSource;
\r
142 set_current_state(ECUM_STATE_GO_SLEEP);
\r
146 #if defined(USE_NVM)
\r
150 internal_data_go_sleep_state_timeout = internal_data.config->EcuMNvramWriteAllTimeout / ECUM_MAIN_FUNCTION_PERIOD;
\r
152 wakeupSource = EcuM_GetPendingWakeupEvents();
\r
154 wakeupSource = EcuM_GetPendingWakeupEvents();
\r
159 In GO SLEEP state (in state ECUM_STATE_GO_SLEEP)
\r
161 static void in_state_goSleep( void ) {
\r
163 /* We only wait for NvM_WriteAll() for so long */
\r
164 if (internal_data_go_sleep_state_timeout){
\r
165 internal_data_go_sleep_state_timeout--;
\r
168 if( (internal_data_go_sleep_state_timeout == 0) ) {
\r
170 * We should go to sleep , enable source that should wake us
\r
174 const EcuM_SleepModeType *sleepModePtr;
\r
176 /* Get the current sleep mode */
\r
178 sleepModePtr = &internal_data.config->EcuMSleepModeConfig[internal_data.sleep_mode];
\r
180 cMask = sleepModePtr->EcuMWakeupSourceMask;
\r
182 /* Loop over the WKSOURCE for this sleep mode */
\r
185 for (; cMask; cMask &= ~(1ul << source)) {
\r
186 source = ilog2(cMask);
\r
187 /* @req 3.1.5/ECUM2389 */
\r
188 EcuM_EnableWakeupSources( 1<< source );
\r
191 WdgM_SetMode(sleepModePtr->EcuMSleepModeWdgMMode);
\r
194 /* Let no one else run */
\r
195 GetResource(RES_SCHEDULER);
\r
198 for (source = 0; cMask; source++) {
\r
199 if(cMask & (1ul << source)){
\r
200 /* @req 3.1.5/ECUM2389 */
\r
201 EcuM_EnableWakeupSources( 1 << source );
\r
202 cMask &= ~(1ul << source);
\r
204 WdgM_SetMode(sleepModePtr->EcuMSleepModeWdgMMode);
\r
207 /* Let no one else run */
\r
208 GetResource(RES_SCHEDULER);
\r
212 } else if( EcuM_GetPendingWakeupEvents() != 0 ) {
\r
213 /* We have pending wakeup events, need to startup again */
\r
214 #if defined(USE_NVM)
\r
215 NvM_CancelWriteAll();
\r
222 * In "Sleep Sequence I" (in state ECUM_STATE_SLEEP)
\r
224 static void in_state_sleep ( void ) {
\r
225 const EcuM_SleepModeType *sleepModePtr;
\r
226 sleepModePtr = &internal_data.config->EcuMSleepModeConfig[internal_data.sleep_mode];
\r
228 EcuM_GenerateRamHash();
\r
230 Mcu_SetMode(sleepModePtr->EcuMSleepModeMcuMode);
\r
232 /* @req 3.1.5/ECUM2863 */
\r
233 if( EcuM_CheckRamHash() == 0) {
\r
234 #if defined(USE_DEM)
\r
236 EcuM_ErrorHook(ECUM_E_RAM_CHECK_FAILED);
\r
240 set_current_state(ECUM_STATE_WAKEUP_ONE);
\r
243 static inline void enter_go_off_one_mode(void){
\r
244 set_current_state(ECUM_STATE_GO_OFF_ONE);
\r
247 #if defined(USE_COMM)
\r
251 #if defined(USE_NVM)
\r
253 // Start NvM_WriteAll and timeout timer
\r
256 internal_data_go_off_one_state_timeout = internal_data.config->EcuMNvramWriteAllTimeout / ECUM_MAIN_FUNCTION_PERIOD;
\r
261 static inline boolean hasRunRequests(void){
\r
262 uint32 result = internal_data.run_requests;
\r
264 #if defined(USE_COMM)
\r
265 result |= internal_data.run_comm_requests;
\r
268 return (result != 0);
\r
271 static inline boolean hasPostRunRequests(void){
\r
272 return (internal_data.postrun_requests != 0);
\r
277 * RUN II Loop (in state ECUM_STATE_APP_RUN)
\r
278 * - The entry to RUN II is done in
\r
280 static inline void in_state_appRun(void){
\r
281 if (internal_data_run_state_timeout){
\r
282 internal_data_run_state_timeout--;
\r
285 if ((!hasRunRequests()) && (internal_data_run_state_timeout == 0)){
\r
286 EcuM_OnExitRun(); /** @req EcuM2865 */
\r
288 #if defined(USE_WDGM)
\r
289 WdgM_SetMode(internal_data.config->EcuMWdgMConfig->EcuMWdgMPostRunMode);
\r
292 #if defined(USE_RTE) && defined(CFG_ECUM_USE_SERVICE_COMPONENT)
\r
293 Rte_Switch_currentMode_currentMode(RTE_MODE_EcuM_Mode_POSTRUN);
\r
296 set_current_state(ECUM_STATE_APP_POST_RUN);/** @req EcuM2865 */
\r
302 * RUN III states (in state ECUM_STATE_APP_POST_RUN)
\r
304 static inline void in_state_appPostRun(void){
\r
306 /* @req 3.1.5/ECUM2866 */
\r
307 if (hasRunRequests()){
\r
308 /* We have run requests, return to RUN II */
\r
309 EcuM_enter_run_mode();
\r
311 } else if (!hasPostRunRequests()){
\r
312 EcuM_OnExitPostRun(); /** @req EcuM2761 */
\r
313 set_current_state(ECUM_STATE_PREP_SHUTDOWN);/** @req EcuM2761 */
\r
315 /* TODO: We have postrun requests */
\r
321 * PREP SHUTDOWN state (in state ECUM_STATE_PREP_SHUTDOWN)
\r
323 static inline void in_state_prepShutdown(void){
\r
325 // TODO: The specification does not state what events to clear
\r
326 EcuM_ClearWakeupEvent(ECUM_WKSTATUS_NONE);
\r
328 EcuM_OnPrepShutdown();
\r
330 #if defined(USE_DEM)
\r
335 // Switch shutdown mode
\r
336 switch(internal_data.shutdown_target){
\r
337 //If in state Off or Reset go into Go_Off_One:
\r
338 case ECUM_STATE_OFF:
\r
339 case ECUM_STATE_RESET:
\r
340 enter_go_off_one_mode();
\r
342 case ECUM_STATE_SLEEP:
\r
343 enter_go_sleep_mode();
\r
346 //TODO: Report error.
\r
351 static inline void in_state_goOffOne(void){
\r
352 #if defined(USE_NVM)
\r
353 if (internal_data_go_off_one_state_timeout){
\r
354 internal_data_go_off_one_state_timeout--;
\r
356 // Wait for the NVM job (NvmWriteAll) to terminate
\r
357 NvM_GetErrorStatus(0, &writeAllResult);
\r
358 if ((writeAllResult != NVM_REQ_PENDING) || (internal_data_go_off_one_state_timeout == 0)){
\r
360 #if defined(USE_WDGM)
\r
361 WdgM_SetMode(internal_data.config->EcuMWdgMConfig->EcuMWdgMShutdownMode);
\r
367 #if defined(USE_WDGM)
\r
368 WdgM_SetMode(internal_data.config->EcuMWdgMConfig->EcuMWdgMShutdownMode);
\r
375 //----- MAIN -----------------------------------------------------------------------------------------------------------------
\r
376 void EcuM_MainFunction(void){
\r
377 EcuM_WakeupSourceType wMask;
\r
379 VALIDATE_NO_RV(internal_data.initiated, ECUM_MAINFUNCTION_ID, ECUM_E_NOT_INITIATED);
\r
381 switch(internal_data.current_state){
\r
383 case ECUM_STATE_APP_RUN:
\r
387 case ECUM_STATE_APP_POST_RUN:
\r
388 /* RUN III state */
\r
389 in_state_appPostRun();
\r
391 case ECUM_STATE_PREP_SHUTDOWN:
\r
392 in_state_prepShutdown();
\r
394 case ECUM_STATE_GO_OFF_ONE:
\r
395 in_state_goOffOne();
\r
397 case ECUM_STATE_GO_SLEEP:
\r
398 in_state_goSleep();
\r
400 case ECUM_STATE_SLEEP:
\r
403 case ECUM_STATE_WAKEUP_ONE:
\r
405 /* TODO: we must have a normal RUN mode.. can't find any
\r
406 * in the A3.1.5 spec. */
\r
407 Mcu_SetMode(MCU_MODE_NORMAL);
\r
409 #if defined(USE_WDGM)
\r
410 WdgM_SetMode(internal_data.config->EcuMWdgMConfig->EcuMWdgMWakeupMode);
\r
413 wMask = EcuM_GetPendingWakeupEvents();
\r
415 EcuM_DisableWakeupSources(wMask);
\r
417 EcuM_AL_DriverRestart();
\r
419 ReleaseResource(RES_SCHEDULER);
\r
421 set_current_state(ECUM_STATE_WAKEUP_VALIDATION);
\r
426 case ECUM_STATE_WAKEUP_VALIDATION:
\r
428 wMask = EcuM_GetPendingWakeupEvents();
\r
430 EcuM_StartWakeupSources(wMask);
\r
432 EcuM_CheckValidation( wMask );
\r
435 // ComM_EcuM_WakeupIndication( network handle )
\r
437 set_current_state(ECUM_STATE_WAKEUP_REACTION);
\r
441 case ECUM_STATE_WAKEUP_REACTION:
\r
444 * At this stage we want to know how to react to the wakeup, e.g. go
\r
445 * back to RUN or SHUTDOWN, etc.
\r
447 EcuM_WakeupReactionType wReaction;
\r
449 wMask = EcuM_GetValidatedWakeupEvents();
\r
451 /* TODO: We have skipped the TTII timer here */
\r
453 /* If the wakeup mask here is != 0 we have a validated wakeup event ->
\r
454 * go back to RUN */
\r
455 wReaction = ( 0 == wMask ) ? ECUM_WKACT_SHUTDOWN : ECUM_WKACT_RUN;
\r
456 wReaction = EcuM_OnWakeupReaction(wReaction);
\r
458 if( wReaction == ECUM_WKACT_RUN) {
\r
459 set_current_state(ECUM_STATE_WAKEUP_TWO);
\r
461 /* From figure 28 it seems that we should go to SHUTDOWN/GO SLEEP) again from wakeup
\r
462 * not going up to RUN/RUN II state again. */
\r
463 set_current_state(ECUM_STATE_GO_SLEEP);
\r
468 case ECUM_STATE_WAKEUP_TWO:
\r
469 #if defined(USE_DEM)
\r
472 set_current_state(ECUM_STATE_RUN);
\r
476 //TODO: Report error.
\r