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
19 #include "EcuM_Cbk.h"
\r
20 #include "EcuM_Internals.h"
\r
21 #if defined(USE_DEM)
\r
24 #if defined(USE_NVM)
\r
28 static uint32 internal_data_run_state_timeout = 0;
\r
29 #if defined(USE_NVM)
\r
30 static uint32 internal_data_go_off_one_state_timeout = 0;
\r
34 void EcuM_enter_run_mode(void)
\r
36 internal_data.current_state = ECUM_STATE_APP_RUN;
\r
38 internal_data_run_state_timeout = internal_data.config->EcuMRunMinimumDuration / ECUM_MAIN_FUNCTION_PERIOD;
\r
41 static inline void enter_post_run_mode(void)
\r
43 internal_data.current_state = ECUM_STATE_APP_POST_RUN;
\r
46 static inline void enter_prep_shutdown_mode(void)
\r
48 internal_data.current_state = ECUM_STATE_PREP_SHUTDOWN;
\r
49 EcuM_OnPrepShutdown();
\r
52 static inline void enter_go_sleep_mode(void)
\r
54 internal_data.current_state = ECUM_STATE_GO_SLEEP;
\r
58 static inline void enter_go_off_one_mode(void)
\r
60 internal_data.current_state = ECUM_STATE_GO_OFF_ONE;
\r
63 #if defined(USE_COMM)
\r
67 #if defined(USE_NVM)
\r
69 // Start NvM_WriteAll and timeout timer
\r
72 internal_data_go_off_one_state_timeout = internal_data.config->EcuMNvramWriteAllTimeout / ECUM_MAIN_FUNCTION_PERIOD;
\r
76 static inline boolean hasRunRequests(void)
\r
78 uint32 result = internal_data.run_requests;
\r
80 #if defined(USE_COMM)
\r
81 result |= internal_data.run_comm_requests;
\r
84 return (result != 0);
\r
87 static inline boolean hasPostRunRequests(void)
\r
89 return (internal_data.postrun_requests != 0);
\r
92 void EcuM_MainFunction(void)
\r
94 #if defined(USE_NVM)
\r
95 static NvM_RequestResultType writeAllResult;
\r
98 VALIDATE_NO_RV(internal_data.initiated, ECUM_MAINFUNCTION_ID, ECUM_E_NOT_INITIATED);
\r
100 if (internal_data.current_state == ECUM_STATE_APP_RUN)
\r
102 if (internal_data_run_state_timeout)
\r
104 internal_data_run_state_timeout--;
\r
107 if ((!hasRunRequests()) && (internal_data_run_state_timeout == 0))
\r
109 EcuM_OnExitRun(); // ECUM_2865
\r
110 enter_post_run_mode();
\r
111 /*lint --e(904)*/ return;
\r
115 if (internal_data.current_state == ECUM_STATE_APP_POST_RUN)
\r
117 if (hasRunRequests())
\r
119 EcuM_enter_run_mode(); // ECUM_2866
\r
120 /*lint --e(904)*/ return;
\r
123 if (!hasPostRunRequests())
\r
125 EcuM_OnExitPostRun(); // ECUM_2761
\r
126 enter_prep_shutdown_mode();
\r
127 /*lint --e(904)*/ return;
\r
131 if (internal_data.current_state == ECUM_STATE_PREP_SHUTDOWN)
\r
133 #if defined(USE_DEM)
\r
138 // Switch shutdown mode
\r
139 if ((internal_data.shutdown_target == ECUM_STATE_OFF) || (internal_data.shutdown_target == ECUM_STATE_RESET)) {
\r
140 enter_go_off_one_mode();
\r
143 if (internal_data.shutdown_target == ECUM_STATE_SLEEP) {
\r
144 enter_go_sleep_mode();
\r
148 if (internal_data.current_state == ECUM_STATE_GO_OFF_ONE)
\r
150 #if defined(USE_NVM)
\r
151 if (internal_data_go_off_one_state_timeout)
\r
152 internal_data_go_off_one_state_timeout--;
\r
154 // Wait for the NVM job (NvmWriteAll) to terminate
\r
155 NvM_GetErrorStatus(0, &writeAllResult);
\r
156 if ((writeAllResult != NVM_REQ_PENDING) || (internal_data_go_off_one_state_timeout == 0))
\r
165 if (internal_data.current_state == ECUM_STATE_GO_SLEEP)
\r