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
30 static uint32 internal_data_run_state_timeout = 0;
\r
31 #if defined(USE_NVM)
\r
32 static uint32 internal_data_go_off_one_state_timeout = 0;
\r
33 static NvM_RequestResultType writeAllResult;
\r
36 #ifdef ECUM_USE_SERVICE_COMPONENT
\r
37 /** @req EcuM2749 */
\r
38 static Rte_ModeType_EcuM_Mode currentMode;
\r
40 void set_current_state(EcuM_StateType state) {
\r
41 Rte_ModeType_EcuM_Mode newMode = currentMode;
\r
43 case ECUM_STATE_WAKEUP:
\r
44 case ECUM_STATE_WAKEUP_ONE:
\r
45 case ECUM_STATE_WAKEUP_VALIDATION:
\r
46 case ECUM_STATE_WAKEUP_REACTION:
\r
47 case ECUM_STATE_WAKEUP_TWO:
\r
48 case ECUM_STATE_SLEEP:
\r
49 case ECUM_STATE_SHUTDOWN:
\r
50 newMode = RTE_MODE_EcuM_Mode_SLEEP;
\r
52 case ECUM_STATE_GO_SLEEP:
\r
53 if( internal_data.shutdown_target == ECUM_STATE_SLEEP ) {
\r
54 newMode = RTE_MODE_EcuM_Mode_SLEEP; /** @req EcuM2752 */
\r
57 case ECUM_STATE_GO_OFF_ONE:
\r
58 case ECUM_STATE_GO_OFF_TWO:
\r
59 newMode = RTE_MODE_EcuM_Mode_SHUTDOWN;
\r
61 case ECUM_STATE_WAKEUP_TTII:
\r
62 if( internal_data.shutdown_target == ECUM_STATE_SLEEP ) {
\r
63 newMode = RTE_MODE_EcuM_Mode_WAKE_SLEEP; /** @req EcuM2752 */
\r
66 case ECUM_STATE_PREP_SHUTDOWN:
\r
67 case ECUM_STATE_APP_POST_RUN: /* Assuming this is same as RUN_III */
\r
68 newMode = RTE_MODE_EcuM_Mode_POST_RUN;
\r
70 case ECUM_STATE_APP_RUN: /* Assuming this is same as RUN_II */
\r
71 newMode = RTE_MODE_EcuM_Mode_RUN;
\r
73 case ECUM_STATE_STARTUP_TWO:
\r
74 newMode = RTE_MODE_EcuM_Mode_STARTUP;
\r
81 if( newMode != currentMode ) {
\r
82 currentMode = newMode;
\r
83 Rte_Switch_EcuM_CurrentMode_currentMode(currentMode); /** @req EcuM2750 */
\r
89 void EcuM_enter_run_mode(void){
\r
90 set_current_state(ECUM_STATE_APP_RUN);
\r
91 EcuM_OnEnterRUN(); /** @req EcuM2308 */
\r
92 //TODO: Call ComM_EcuM_RunModeIndication(NetworkHandleType Channel) for all channels that have requested run.
\r
93 internal_data_run_state_timeout = internal_data.config->EcuMRunMinimumDuration / ECUM_MAIN_FUNCTION_PERIOD; /** @req EcuM2310 */
\r
97 //--------- Local functions ------------------------------------------------------------------------------------------------
\r
99 static inline void enter_go_sleep_mode(void){
\r
100 set_current_state(ECUM_STATE_GO_SLEEP);
\r
104 static inline void enter_go_off_one_mode(void){
\r
105 set_current_state(ECUM_STATE_GO_OFF_ONE);
\r
108 #if defined(USE_COMM)
\r
112 #if defined(USE_NVM)
\r
114 // Start NvM_WriteAll and timeout timer
\r
117 internal_data_go_off_one_state_timeout = internal_data.config->EcuMNvramWriteAllTimeout / ECUM_MAIN_FUNCTION_PERIOD;
\r
122 static inline boolean hasRunRequests(void){
\r
123 uint32 result = internal_data.run_requests;
\r
125 #if defined(USE_COMM)
\r
126 result |= internal_data.run_comm_requests;
\r
129 return (result != 0);
\r
132 static inline boolean hasPostRunRequests(void){
\r
133 return (internal_data.postrun_requests != 0);
\r
138 static inline void in_state_appRun(void){
\r
139 if (internal_data_run_state_timeout){
\r
140 internal_data_run_state_timeout--;
\r
143 if ((!hasRunRequests()) && (internal_data_run_state_timeout == 0)){
\r
144 EcuM_OnExitRun(); /** @req EcuM2865 */
\r
145 set_current_state(ECUM_STATE_APP_POST_RUN);/** @req EcuM2865 */
\r
150 static inline void in_state_appPostRun(void){
\r
151 if (hasRunRequests()){
\r
152 set_current_state(ECUM_STATE_APP_RUN);/** @req EcuM2866 */ /** @req EcuM2308 */
\r
153 EcuM_OnEnterRUN(); /** @req EcuM2308 */
\r
154 //TODO: Call ComM_EcuM_RunModeIndication(NetworkHandleType Channel) for all channels that have requested run.
\r
155 internal_data_run_state_timeout = internal_data.config->EcuMRunMinimumDuration / ECUM_MAIN_FUNCTION_PERIOD; /** @req EcuM2310 */
\r
157 } else if (!hasPostRunRequests()){
\r
158 EcuM_OnExitPostRun(); /** @req EcuM2761 */
\r
159 set_current_state(ECUM_STATE_PREP_SHUTDOWN);/** @req EcuM2761 */
\r
161 EcuM_OnPrepShutdown();
\r
163 // TODO: Do something?
\r
167 static inline void in_state_prepShutdown(void){
\r
168 #if defined(USE_DEM)
\r
173 // Switch shutdown mode
\r
174 switch(internal_data.shutdown_target){
\r
175 //If in state Off or Reset go into Go_Off_One:
\r
176 case ECUM_STATE_OFF:
\r
177 case ECUM_STATE_RESET:
\r
178 enter_go_off_one_mode();
\r
180 case ECUM_STATE_SLEEP:
\r
181 enter_go_sleep_mode();
\r
184 //TODO: Report error.
\r
189 static inline void in_state_goOffOne(void){
\r
190 #if defined(USE_NVM)
\r
191 if (internal_data_go_off_one_state_timeout){
\r
192 internal_data_go_off_one_state_timeout--;
\r
194 // Wait for the NVM job (NvmWriteAll) to terminate
\r
195 NvM_GetErrorStatus(0, &writeAllResult);
\r
196 if ((writeAllResult != NVM_REQ_PENDING) || (internal_data_go_off_one_state_timeout == 0)){
\r
205 //----- MAIN -----------------------------------------------------------------------------------------------------------------
\r
206 void EcuM_MainFunction(void){
\r
207 VALIDATE_NO_RV(internal_data.initiated, ECUM_MAINFUNCTION_ID, ECUM_E_NOT_INITIATED);
\r
209 switch(internal_data.current_state){
\r
211 case ECUM_STATE_APP_RUN:
\r
214 case ECUM_STATE_APP_POST_RUN:
\r
215 in_state_appPostRun();
\r
217 case ECUM_STATE_PREP_SHUTDOWN:
\r
218 in_state_prepShutdown();
\r
220 case ECUM_STATE_GO_OFF_ONE:
\r
221 in_state_goOffOne();
\r
223 case ECUM_STATE_GO_SLEEP:
\r
227 //TODO: Report error.
\r