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
24 #include "EcuM_Cbk.h"
\r
25 #include "EcuM_Internals.h"
\r
26 #if defined(USE_DEM)
\r
29 #if defined(USE_NVM)
\r
33 static uint32 internal_data_run_state_timeout = 0;
\r
34 #if defined(USE_NVM)
\r
35 static uint32 internal_data_go_off_one_state_timeout = 0;
\r
39 void EcuM_enter_run_mode(void)
\r
41 internal_data.current_state = ECUM_STATE_APP_RUN;
\r
43 internal_data_run_state_timeout = internal_data.config->EcuMRunMinimumDuration / ECUM_MAIN_FUNCTION_PERIOD;
\r
46 static inline void enter_post_run_mode(void)
\r
48 internal_data.current_state = ECUM_STATE_APP_POST_RUN;
\r
51 static inline void enter_prep_shutdown_mode(void)
\r
53 internal_data.current_state = ECUM_STATE_PREP_SHUTDOWN;
\r
54 EcuM_OnPrepShutdown();
\r
57 static inline void enter_go_sleep_mode(void)
\r
59 internal_data.current_state = ECUM_STATE_GO_SLEEP;
\r
60 void EcuM_OnGoSleep();
\r
63 static inline void enter_go_off_one_mode(void)
\r
65 internal_data.current_state = ECUM_STATE_GO_OFF_ONE;
\r
68 #if defined(USE_COMM)
\r
72 #if defined(USE_NVM)
\r
74 // Start NvM_WriteAll and timeout timer
\r
77 internal_data_go_off_one_state_timeout = internal_data.config->EcuMNvramWriteAllTimeout / ECUM_MAIN_FUNCTION_PERIOD;
\r
81 static inline boolean hasRunRequests(void)
\r
83 boolean result = internal_data.run_requests;
\r
85 #if defined(USE_COMM)
\r
86 result |= internal_data.run_comm_requests;
\r
89 return (result != 0);
\r
92 static inline boolean hasPostRunRequests(void)
\r
94 return (internal_data.postrun_requests != 0);
\r
97 void EcuM_MainFunction(void)
\r
99 #if defined(USE_NVM)
\r
100 static NvM_RequestResultType writeAllResult;
\r
103 VALIDATE_NO_RV(internal_data.initiated, ECUM_MAINFUNCTION_ID, ECUM_E_NOT_INITIATED);
\r
105 if (internal_data.current_state == ECUM_STATE_APP_RUN)
\r
107 if (internal_data_run_state_timeout)
\r
108 internal_data_run_state_timeout--;
\r
110 if (!hasRunRequests() && (internal_data_run_state_timeout == 0))
\r
112 EcuM_OnExitRun(); // ECUM_2865
\r
113 enter_post_run_mode();
\r
118 if (internal_data.current_state == ECUM_STATE_APP_POST_RUN)
\r
120 if (hasRunRequests())
\r
122 EcuM_enter_run_mode(); // ECUM_2866
\r
126 if (!hasPostRunRequests())
\r
128 EcuM_OnExitPostRun(); // ECUM_2761
\r
129 enter_prep_shutdown_mode();
\r
134 if (internal_data.current_state == ECUM_STATE_PREP_SHUTDOWN)
\r
136 #if defined(USE_DEM)
\r
141 // Switch shutdown mode
\r
142 if ((internal_data.shutdown_target == ECUM_STATE_OFF) || (internal_data.shutdown_target == ECUM_STATE_RESET)) {
\r
143 enter_go_off_one_mode();
\r
146 if (internal_data.shutdown_target == ECUM_STATE_SLEEP) {
\r
147 enter_go_sleep_mode();
\r
151 if (internal_data.current_state == ECUM_STATE_GO_OFF_ONE)
\r
153 #if defined(USE_NVM)
\r
154 if (internal_data_go_off_one_state_timeout)
\r
155 internal_data_go_off_one_state_timeout--;
\r
157 // Wait for the NVM job (NvmWriteAll) to terminate
\r
158 NvM_GetErrorStatus(0, &writeAllResult);
\r
159 if ((writeAllResult != NVM_REQ_PENDING) || (internal_data_go_off_one_state_timeout == 0))
\r
168 if (internal_data.current_state == ECUM_STATE_GO_SLEEP)
\r