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
37 EcuM_OnEnterRUN(); /** @req EcuM2308 */
\r
38 //TODO: Call ComM_EcuM_RunModeIndication(NetworkHandleType Channel) for all channels that have requested run.
\r
39 internal_data_run_state_timeout = internal_data.config->EcuMRunMinimumDuration / ECUM_MAIN_FUNCTION_PERIOD; /** @req EcuM2310 */
\r
42 static inline void enter_post_run_mode(void)
\r
44 internal_data.current_state = ECUM_STATE_APP_POST_RUN;
\r
47 static inline void enter_prep_shutdown_mode(void)
\r
49 internal_data.current_state = ECUM_STATE_PREP_SHUTDOWN;
\r
50 EcuM_OnPrepShutdown();
\r
53 static inline void enter_go_sleep_mode(void)
\r
55 internal_data.current_state = ECUM_STATE_GO_SLEEP;
\r
59 static inline void enter_go_off_one_mode(void)
\r
61 internal_data.current_state = ECUM_STATE_GO_OFF_ONE;
\r
64 #if defined(USE_COMM)
\r
68 #if defined(USE_NVM)
\r
70 // Start NvM_WriteAll and timeout timer
\r
73 internal_data_go_off_one_state_timeout = internal_data.config->EcuMNvramWriteAllTimeout / ECUM_MAIN_FUNCTION_PERIOD;
\r
77 static inline boolean hasRunRequests(void)
\r
79 uint32 result = internal_data.run_requests;
\r
81 #if defined(USE_COMM)
\r
82 result |= internal_data.run_comm_requests;
\r
85 return (result != 0);
\r
88 static inline boolean hasPostRunRequests(void)
\r
90 return (internal_data.postrun_requests != 0);
\r
93 void EcuM_MainFunction(void)
\r
95 #if defined(USE_NVM)
\r
96 static NvM_RequestResultType writeAllResult;
\r
99 VALIDATE_NO_RV(internal_data.initiated, ECUM_MAINFUNCTION_ID, ECUM_E_NOT_INITIATED);
\r
101 if (internal_data.current_state == ECUM_STATE_APP_RUN)
\r
103 if (internal_data_run_state_timeout)
\r
105 internal_data_run_state_timeout--;
\r
108 if ((!hasRunRequests()) && (internal_data_run_state_timeout == 0))
\r
110 EcuM_OnExitRun(); /** @req EcuM2865 */
\r
111 enter_post_run_mode();/** @req EcuM2865 */
\r
112 /*lint --e(904)*/ return;
\r
116 if (internal_data.current_state == ECUM_STATE_APP_POST_RUN)
\r
118 if (hasRunRequests())
\r
120 EcuM_enter_run_mode(); /** @req EcuM2866 */
\r
121 /*lint --e(904)*/ return;
\r
124 if (!hasPostRunRequests())
\r
126 EcuM_OnExitPostRun(); /** @req EcuM2761 */
\r
127 enter_prep_shutdown_mode();/** @req EcuM2761 */
\r
128 /*lint --e(904)*/ return;
\r
132 if (internal_data.current_state == ECUM_STATE_PREP_SHUTDOWN)
\r
134 #if defined(USE_DEM)
\r
139 // Switch shutdown mode
\r
140 if ((internal_data.shutdown_target == ECUM_STATE_OFF) || (internal_data.shutdown_target == ECUM_STATE_RESET)) {
\r
141 enter_go_off_one_mode();
\r
144 if (internal_data.shutdown_target == ECUM_STATE_SLEEP) {
\r
145 enter_go_sleep_mode();
\r
149 if (internal_data.current_state == ECUM_STATE_GO_OFF_ONE)
\r
151 #if defined(USE_NVM)
\r
152 if (internal_data_go_off_one_state_timeout)
\r
153 internal_data_go_off_one_state_timeout--;
\r
155 // Wait for the NVM job (NvmWriteAll) to terminate
\r
156 NvM_GetErrorStatus(0, &writeAllResult);
\r
157 if ((writeAllResult != NVM_REQ_PENDING) || (internal_data_go_off_one_state_timeout == 0))
\r
166 if (internal_data.current_state == ECUM_STATE_GO_SLEEP)
\r