foxBMS  1.4.0
The foxBMS Battery Management System API Documentation
sys.c
Go to the documentation of this file.
1 /**
2  *
3  * @copyright © 2010 - 2022, Fraunhofer-Gesellschaft zur Foerderung der angewandten Forschung e.V.
4  * All rights reserved.
5  *
6  * SPDX-License-Identifier: BSD-3-Clause
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions are met:
10  *
11  * 1. Redistributions of source code must retain the above copyright notice, this
12  * list of conditions and the following disclaimer.
13  *
14  * 2. Redistributions in binary form must reproduce the above copyright notice,
15  * this list of conditions and the following disclaimer in the documentation
16  * and/or other materials provided with the distribution.
17  *
18  * 3. Neither the name of the copyright holder nor the names of its
19  * contributors may be used to endorse or promote products derived from
20  * this software without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
23  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
25  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
28  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  *
33  * We kindly request you to use one or more of the following phrases to refer to
34  * foxBMS in your hardware, software, documentation or advertising materials:
35  *
36  * - ″This product uses parts of foxBMS®″
37  * - ″This product includes parts of foxBMS®″
38  * - ″This product is derived from foxBMS®″
39  *
40  */
41 
42 /**
43  * @file sys.c
44  * @author foxBMS Team
45  * @date 2020-02-24 (date of creation)
46  * @updated 2022-07-28 (date of last update)
47  * @version v1.4.0
48  * @ingroup ENGINE
49  * @prefix SYS
50  *
51  * @brief Sys driver implementation
52  */
53 
54 /*========== Includes =======================================================*/
55 #include "sys.h"
56 
57 #include "algorithm.h"
58 #include "bal.h"
59 #include "bms.h"
60 #include "can.h"
61 #include "contactor.h"
62 #include "diag.h"
63 #include "fram.h"
64 #include "imd.h"
65 #include "interlock.h"
66 #include "meas.h"
67 #include "os.h"
68 #include "sbc.h"
69 #include "sof_trapezoid.h"
70 #include "state_estimation.h"
71 
72 /*========== Macros and Definitions =========================================*/
73 
74 /** Saves the last state and the last substate */
75 #define SYS_SAVELASTSTATES(x) \
76  (x)->lastState = (x)->state; \
77  (x)->lastSubstate = (x)->substate
78 
79 /** Magic number that is searched by the #SYS_GeneralMacroBist(). */
80 #define SYS_BIST_GENERAL_MAGIC_NUMBER (42u)
81 
82 /*========== Static Constant and Variable Definitions =======================*/
83 
84 /*========== Extern Constant and Variable Definitions =======================*/
85 
86 /** Symbolic names to check for multiple calls of #SYS_Trigger() */
87 typedef enum {
88  SYS_MULTIPLE_CALLS_NO, /*!< no multiple calls, OK */
89  SYS_MULTIPLE_CALLS_YES, /*!< multiple calls, not OK */
91 
92 /** contains the state of the contactor state machine */
94  .timer = 0,
95  .triggerEntry = 0,
96  .stateRequest = SYS_STATE_NO_REQUEST,
98  .substate = SYS_ENTRY,
99  .lastState = SYS_STATEMACH_UNINITIALIZED,
100  .lastSubstate = SYS_ENTRY,
101  .illegalRequestsCounter = 0,
102  .initializationTimeout = 0,
103 };
104 
105 /*========== Static Function Prototypes =====================================*/
106 /**
107  * @brief check for multiple calls of state machine trigger function
108  * @details The trigger function is not reentrant, which means it cannot
109  * be called multiple times. This functions increments the
110  * triggerEntry counter once and must be called each time the
111  * trigger function is called. If triggerEntry is greater than
112  * one, there were multiple calls. For this function to work,
113  * triggerEntry must be decremented each time the trigger function
114  * is called, even if no processing do because the timer is
115  * non-zero.
116  * @param pSystemState state of the fake state machine
117  * @return #SYS_MULTIPLE_CALLS_YES if there were multiple calls,
118  * #SYS_MULTIPLE_CALLS_NO otherwise
119  */
121 
122 /**
123  * @brief Sets the next state, the next substate and the timer value
124  * of the state variable.
125  * @param pSystemState state of the example state machine
126  * @param nextState state to be transferred into
127  * @param nextSubstate substate to be transferred into
128  * @param idleTime wait time for the state machine
129  */
130 static void SYS_SetState(
131  SYS_STATE_s *pSystemState,
132  SYS_FSM_STATES_e nextState,
133  SYS_FSM_SUBSTATES_e nextSubstate,
134  uint16_t idleTime);
135 
136 /**
137  * @brief Sets the next substate and the timer value
138  * of the state variable.
139  * @param pSystemState state of the example state machine
140  * @param nextSubstate substate to be transferred into
141  * @param idleTime wait time for the state machine
142  */
143 static void SYS_SetSubstate(SYS_STATE_s *pSystemState, SYS_FSM_SUBSTATES_e nextSubstate, uint16_t idleTime);
144 
145 /**
146  * @brief Defines the state transitions
147  * @details This function contains the implementation of the state
148  * machine, i.e., the sequence of states and substates.
149  * It is called by the trigger function every time
150  * the state machine timer has a non-zero value.
151  * @param pSystemState state of the example state machine
152  * @return TODO
153  */
155 
158 
159 /*========== Static Function Implementations ================================*/
161  FAS_ASSERT(pSystemState != NULL_PTR);
164  if (pSystemState->triggerEntry == 0u) {
165  pSystemState->triggerEntry++;
166  } else {
167  multipleCalls = SYS_MULTIPLE_CALLS_YES;
168  }
170  return multipleCalls;
171 }
172 
173 #pragma diag_push
174 #pragma diag_suppress 179
175 #pragma WEAK(SYS_SetState)
176 static void SYS_SetState(
177  SYS_STATE_s *pSystemState,
178  SYS_FSM_STATES_e nextState,
179  SYS_FSM_SUBSTATES_e nextSubstate,
180  uint16_t idleTime) {
181  FAS_ASSERT(pSystemState != NULL_PTR);
182 
183  pSystemState->timer = idleTime;
184 }
185 #pragma diag_pop
186 
187 #pragma diag_push
188 #pragma diag_suppress 179
189 #pragma WEAK(SYS_SetSubstate)
190 static void SYS_SetSubstate(SYS_STATE_s *pSystemState, SYS_FSM_SUBSTATES_e nextSubstate, uint16_t idleTime) {
191  FAS_ASSERT(pSystemState != NULL_PTR);
192 }
193 #pragma diag_push
194 
196  STD_RETURN_TYPE_e ranStateMachine = STD_OK;
197 
200  STD_RETURN_TYPE_e balancingInitializationState = STD_OK;
201  BAL_RETURN_TYPE_e balancingGlobalEnableState = BAL_ERROR;
202  STD_RETURN_TYPE_e bmsState = STD_NOT_OK;
203  bool imdInitialized = false;
204 
205  switch (pSystemState->state) {
206  /****************************UNINITIALIZED***********************************/
208  /* waiting for Initialization Request */
209  stateRequest = SYS_TransferStateRequest();
210  if (stateRequest == SYS_STATE_INITIALIZATION_REQUEST) {
211  SYS_SAVELASTSTATES(pSystemState);
212  pSystemState->timer = SYS_FSM_SHORT_TIME;
213  pSystemState->state = SYS_STATEMACH_INITIALIZATION;
214  pSystemState->substate = SYS_ENTRY;
215  } else if (stateRequest == SYS_STATE_NO_REQUEST) {
216  /* no actual request pending */
217  } else {
218  pSystemState->illegalRequestsCounter++; /* illegal request pending */
219  }
220  break;
221  /****************************INITIALIZATION**********************************/
223 
224  SYS_SAVELASTSTATES(pSystemState);
225  /* Initializations done here */
227  for (uint8_t s = 0u; s < BS_NR_OF_STRINGS; s++) {
230  }
231  }
232 
233  pSystemState->timer = SYS_FSM_SHORT_TIME;
234  pSystemState->state = SYS_STATEMACH_INITIALIZE_SBC;
235  pSystemState->substate = SYS_ENTRY;
236  break;
237 
238  /**************************** INITIALIZE SBC *************************************/
240  SYS_SAVELASTSTATES(pSystemState);
241 
242  if (pSystemState->substate == SYS_ENTRY) {
244  pSystemState->timer = SYS_FSM_SHORT_TIME;
245  pSystemState->substate = SYS_WAIT_INITIALIZATION_SBC;
246  pSystemState->initializationTimeout = 0;
247  break;
248  } else if (pSystemState->substate == SYS_WAIT_INITIALIZATION_SBC) {
250  if (sbcState == SBC_STATEMACHINE_RUNNING) {
251  pSystemState->timer = SYS_FSM_SHORT_TIME;
252  pSystemState->state = SYS_STATEMACH_INITIALIZE_CAN;
253  pSystemState->substate = SYS_ENTRY;
254  break;
255  } else {
256  if (pSystemState->initializationTimeout >
258  pSystemState->timer = SYS_FSM_SHORT_TIME;
259  pSystemState->state = SYS_STATEMACH_ERROR;
260  pSystemState->substate = SYS_SBC_INITIALIZATION_ERROR;
261  break;
262  }
263  pSystemState->timer = SYS_FSM_SHORT_TIME;
264  pSystemState->initializationTimeout++;
265  break;
266  }
267  } else {
268  FAS_ASSERT(FAS_TRAP); /* substate does not exist in this state */
269  }
270  break;
271 
272  /**************************** INITIALIZE CAN TRANSCEIVER ****************************/
274  CAN_Initialize();
275  pSystemState->timer = SYS_FSM_SHORT_TIME;
276  pSystemState->state = SYS_STATEMACH_SYSTEM_BIST;
277  pSystemState->substate = SYS_ENTRY;
278  break;
279 
280  /**************************** EXECUTE STARTUP BIST **********************************/
282  SYS_SAVELASTSTATES(pSystemState);
283  /* run BIST functions */
286 
287  pSystemState->timer = SYS_FSM_SHORT_TIME;
288  pSystemState->state = SYS_STATEMACH_INITIALIZED;
289  pSystemState->substate = SYS_ENTRY;
290  break;
291 
292  /****************************INITIALIZED*************************************/
294  SYS_SAVELASTSTATES(pSystemState);
295  /* Send CAN boot message directly on CAN */
297 
298  pSystemState->timer = SYS_FSM_SHORT_TIME;
300  pSystemState->substate = SYS_ENTRY;
301  break;
302 
303  /****************************INITIALIZE INTERLOCK*************************************/
305  SYS_SAVELASTSTATES(pSystemState);
307  pSystemState->timer = SYS_FSM_SHORT_TIME;
309  pSystemState->substate = SYS_ENTRY;
310  pSystemState->initializationTimeout = 0;
311  break;
312 
313  /****************************INITIALIZE CONTACTORS*************************************/
314  /* TODO: check if necessary and add */
315 
316  /****************************INITIALIZE BALANCING*************************************/
318  SYS_SAVELASTSTATES(pSystemState);
319  if (pSystemState->substate == SYS_ENTRY) {
321  pSystemState->timer = SYS_FSM_SHORT_TIME;
322  pSystemState->substate = SYS_WAIT_INITIALIZATION_BAL;
323  pSystemState->initializationTimeout = 0;
324  break;
325  } else if (pSystemState->substate == SYS_WAIT_INITIALIZATION_BAL) {
326  balancingInitializationState = BAL_GetInitializationState();
327  if (balancingInitializationState == STD_OK) {
328  pSystemState->timer = SYS_FSM_SHORT_TIME;
330  break;
331  } else {
332  if (pSystemState->initializationTimeout >
334  pSystemState->timer = SYS_FSM_SHORT_TIME;
335  pSystemState->state = SYS_STATEMACH_ERROR;
336  pSystemState->substate = SYS_BAL_INITIALIZATION_ERROR;
337  break;
338  }
339  pSystemState->timer = SYS_FSM_SHORT_TIME;
340  pSystemState->initializationTimeout++;
341  break;
342  }
343  } else if (pSystemState->substate == SYS_WAIT_INITIALIZATION_BAL_GLOBAL_ENABLE) {
344  if (BALANCING_DEFAULT_INACTIVE == true) {
345  balancingGlobalEnableState = BAL_SetStateRequest(BAL_STATE_GLOBAL_DISABLE_REQUEST);
346  } else {
347  balancingGlobalEnableState = BAL_SetStateRequest(BAL_STATE_GLOBAL_ENABLE_REQUEST);
348  }
349  if (balancingGlobalEnableState == BAL_OK) {
350  pSystemState->timer = SYS_FSM_SHORT_TIME;
352  pSystemState->substate = SYS_ENTRY;
353  break;
354  } else {
355  if (pSystemState->initializationTimeout >
357  pSystemState->timer = SYS_FSM_SHORT_TIME;
358  pSystemState->state = SYS_STATEMACH_ERROR;
359  pSystemState->substate = SYS_BAL_INITIALIZATION_ERROR;
360  break;
361  }
362  pSystemState->timer = SYS_FSM_SHORT_TIME;
363  pSystemState->initializationTimeout++;
364  break;
365  }
366  }
367  break;
368 
369  /****************************START FIRST MEAS CYCLE**************************/
371  SYS_SAVELASTSTATES(pSystemState);
372  if (pSystemState->substate == SYS_ENTRY) {
374  pSystemState->initializationTimeout = 0;
376  } else if (pSystemState->substate == SYS_WAIT_FIRST_MEASUREMENT_CYCLE) {
377  if (MEAS_IsFirstMeasurementCycleFinished() == true) {
378  /* allow initialization of algorithm module */
380  /* MEAS_RequestOpenWireCheck(); */ /*TODO: check with strings */
381  pSystemState->timer = SYS_FSM_SHORT_TIME;
382  if (CURRENT_SENSOR_PRESENT == true) {
384  } else {
385  pSystemState->state = SYS_STATEMACH_INITIALIZE_MISC;
386  }
387  pSystemState->substate = SYS_ENTRY;
388  break;
389  } else {
390  if (pSystemState->initializationTimeout >
392  pSystemState->timer = SYS_FSM_SHORT_TIME;
393  pSystemState->state = SYS_STATEMACH_ERROR;
394  pSystemState->substate = SYS_MEAS_INITIALIZATION_ERROR;
395  break;
396  } else {
397  pSystemState->timer = SYS_FSM_MEDIUM_TIME;
398  pSystemState->initializationTimeout++;
399  break;
400  }
401  }
402  }
403  break;
404 
405  /****************************CHECK CURRENT SENSOR PRESENCE*************************************/
407  SYS_SAVELASTSTATES(pSystemState);
408 
409  if (pSystemState->substate == SYS_ENTRY) {
410  pSystemState->initializationTimeout = 0;
411  CAN_EnablePeriodic(true);
412 #if defined(CURRENT_SENSOR_ISABELLENHUETTE_TRIGGERED)
413  /* If triggered mode is used, CAN trigger message needs to
414  * be transmitted and current sensor response has to be
415  * received afterwards. This may take some time, therefore
416  * delay has to be increased.
417  */
418  pSystemState->timer = SYS_FSM_LONG_TIME_MS;
419 #else /* defined(CURRENT_SENSOR_ISABELLENHUETTE_TRIGGERED) */
420  pSystemState->timer = SYS_FSM_LONG_TIME;
421 #endif /* defined(CURRENT_SENSOR_ISABELLENHUETTE_TRIGGERED) */
423  } else if (pSystemState->substate == SYS_WAIT_CURRENT_SENSOR_PRESENCE) {
424  bool allSensorsPresent = true;
425  for (uint8_t s = 0u; s < BS_NR_OF_STRINGS; s++) {
426  if (CAN_IsCurrentSensorPresent(s) == true) {
427  if (CAN_IsCurrentSensorCcPresent(s) == true) {
428  SE_InitializeSoc(true, s);
429  } else {
430  SE_InitializeSoc(false, s);
431  }
432  if (CAN_IsCurrentSensorEcPresent(s) == true) {
433  SE_InitializeSoe(true, s);
434  } else {
435  SE_InitializeSoe(false, s);
436  }
437  SE_InitializeSoh(s);
438  } else {
439  allSensorsPresent = false;
440  }
441  }
442 
443  if (allSensorsPresent == true) {
444  SOF_Init();
445 
446  pSystemState->timer = SYS_FSM_SHORT_TIME;
447  pSystemState->state = SYS_STATEMACH_INITIALIZE_MISC;
448  pSystemState->substate = SYS_ENTRY;
449  break;
450  } else {
451  if (pSystemState->initializationTimeout >
453  pSystemState->timer = SYS_FSM_SHORT_TIME;
454  pSystemState->state = SYS_STATEMACH_ERROR;
456  break;
457  } else {
458  pSystemState->timer = SYS_FSM_MEDIUM_TIME;
459  pSystemState->initializationTimeout++;
460  break;
461  }
462  }
463  } else {
464  FAS_ASSERT(FAS_TRAP); /* substate does not exist in this state */
465  }
466  break;
467 
468  /****************************INITIALIZED_MISC*************************************/
470  SYS_SAVELASTSTATES(pSystemState);
471 
472  if (CURRENT_SENSOR_PRESENT == false) {
473  CAN_EnablePeriodic(true);
474  for (uint8_t s = 0u; s < BS_NR_OF_STRINGS; s++) {
475  SE_InitializeSoc(false, s);
476  SE_InitializeSoe(false, s);
477  SE_InitializeSoh(s);
478  }
479  }
480 
481  pSystemState->initializationTimeout = 0u;
482  pSystemState->timer = SYS_FSM_SHORT_TIME;
483  pSystemState->state = SYS_STATEMACH_INITIALIZE_IMD;
484  pSystemState->substate = SYS_ENTRY;
485  break;
486 
487  /****************************INITIALIZED_IMD*************************************/
488 
490  SYS_SAVELASTSTATES(pSystemState);
491 
492  if (pSystemState->substate == SYS_ENTRY) {
494  /* Request inquired successfully */
495  pSystemState->timer = SYS_FSM_MEDIUM_TIME;
496  pSystemState->state = SYS_STATEMACH_INITIALIZE_BMS;
497  pSystemState->substate = SYS_ENTRY;
498  pSystemState->initializationTimeout = 0u;
499  } else {
500  /* Request declined -> retry max. 3 times */
501  pSystemState->initializationTimeout++;
502  pSystemState->timer = SYS_FSM_SHORT_TIME;
504  /* Switch to error state */
505  pSystemState->timer = SYS_FSM_SHORT_TIME;
506  pSystemState->state = SYS_STATEMACH_ERROR;
507  pSystemState->substate = SYS_IMD_INITIALIZATION_ERROR;
508  }
509  }
510  break;
511  } else if (pSystemState->substate == SYS_WAIT_INITIALIZATION_IMD) {
512  imdInitialized = IMD_GetInitializationState();
513  if (imdInitialized == true) {
514  pSystemState->timer = SYS_FSM_SHORT_TIME;
515  pSystemState->state = SYS_STATEMACH_INITIALIZE_BMS;
516  pSystemState->substate = SYS_ENTRY;
517  break;
518  } else {
519  if (pSystemState->initializationTimeout >
521  pSystemState->timer = SYS_FSM_SHORT_TIME;
522  pSystemState->state = SYS_STATEMACH_ERROR;
523  pSystemState->substate = SYS_IMD_INITIALIZATION_ERROR;
524  break;
525  }
526  pSystemState->timer = SYS_FSM_SHORT_TIME;
527  pSystemState->initializationTimeout++;
528  break;
529  }
530  } else {
531  FAS_ASSERT(FAS_TRAP); /* substate does not exist in this state */
532  }
533  break;
534 
535  /****************************INITIALIZE BMS*************************************/
537  SYS_SAVELASTSTATES(pSystemState);
538 
539  if (pSystemState->substate == SYS_ENTRY) {
541  pSystemState->timer = SYS_FSM_SHORT_TIME;
542  pSystemState->substate = SYS_WAIT_INITIALIZATION_BMS;
543  pSystemState->initializationTimeout = 0;
544  break;
545  } else if (pSystemState->substate == SYS_WAIT_INITIALIZATION_BMS) {
546  bmsState = BMS_GetInitializationState();
547  if (bmsState == STD_OK) {
548  pSystemState->timer = SYS_FSM_SHORT_TIME;
549  pSystemState->state = SYS_STATEMACH_RUNNING;
550  pSystemState->substate = SYS_ENTRY;
551  break;
552  } else {
553  if (pSystemState->initializationTimeout >
555  pSystemState->timer = SYS_FSM_SHORT_TIME;
556  pSystemState->state = SYS_STATEMACH_ERROR;
557  pSystemState->substate = SYS_BMS_INITIALIZATION_ERROR;
558  break;
559  }
560  pSystemState->timer = SYS_FSM_SHORT_TIME;
561  pSystemState->initializationTimeout++;
562  break;
563  }
564  } else {
565  FAS_ASSERT(FAS_TRAP); /* substate does not exist in this state */
566  }
567  break;
568 
569  /****************************RUNNING*************************************/
571  SYS_SAVELASTSTATES(pSystemState);
572  pSystemState->timer = SYS_FSM_LONG_TIME;
573  break;
574 
575  /****************************ERROR*************************************/
576  case SYS_STATEMACH_ERROR:
577  SYS_SAVELASTSTATES(pSystemState);
578  pSystemState->timer = SYS_FSM_LONG_TIME;
579  break;
580  /***************************DEFAULT CASE*************************************/
581  default:
582  /* invalid state */
584  break;
585  }
586  return ranStateMachine;
587 }
588 
589 /**
590  * @brief transfers the current state request to the state machine.
591  *
592  * This function takes the current state request from #sys_state and transfers it to the state machine.
593  * It resets the value from #sys_state to #SYS_STATE_NO_REQUEST
594  *
595  * @return retVal current state request, taken from #SYS_STATE_REQUEST_e
596  *
597  */
600 
602  retval = sys_state.stateRequest;
605 
606  return (retval);
607 }
608 
611 
613  retVal = SYS_CheckStateRequest(stateRequest);
614 
615  if (retVal == SYS_OK) {
616  sys_state.stateRequest = stateRequest;
617  }
619 
620  return (retVal);
621 }
622 
623 /**
624  * @brief checks the state requests that are made.
625  *
626  * This function checks the validity of the state requests.
627  * The results of the checked is returned immediately.
628  *
629  * @param stateRequest state request to be checked
630  *
631  * @return result of the state request that was made, taken from SYS_RETURN_TYPE_e
632  */
635  if (stateRequest == SYS_STATE_ERROR_REQUEST) {
636  retval = SYS_OK;
637  } else {
639  /* init only allowed from the uninitialized state */
640  if (stateRequest == SYS_STATE_INITIALIZATION_REQUEST) {
642  retval = SYS_OK;
643  } else {
644  retval = SYS_ALREADY_INITIALIZED;
645  }
646  } else {
647  retval = SYS_ILLEGAL_REQUEST;
648  }
649  } else {
650  retval = SYS_REQUEST_PENDING;
651  }
652  }
653  return retval;
654 }
655 
656 /*========== Extern Function Implementations ================================*/
657 
658 extern void SYS_GeneralMacroBist(void) {
659  const uint8_t dummy[GEN_REPEAT_MAXIMUM_REPETITIONS] = {
661  for (uint8_t i = 0u; i < GEN_REPEAT_MAXIMUM_REPETITIONS; i++) {
663  }
664 }
665 
667  FAS_ASSERT(pSystemState != NULL_PTR);
668  bool earlyExit = false;
669  STD_RETURN_TYPE_e returnValue = STD_OK;
670 
671  /* Check multiple calls of function */
672  if (SYS_MULTIPLE_CALLS_YES == SYS_CheckMultipleCalls(pSystemState)) {
673  returnValue = STD_NOT_OK;
674  earlyExit = true;
675  }
676 
677  if (earlyExit == false) {
678  if (pSystemState->timer > 0u) {
679  if ((--pSystemState->timer) > 0u) {
680  pSystemState->triggerEntry--;
681  returnValue = STD_OK;
682  earlyExit = true;
683  }
684  }
685  }
686 
687  if (earlyExit == false) {
688  SYS_RunStateMachine(pSystemState);
689  pSystemState->triggerEntry--;
690  }
691  return returnValue;
692 }
693 
694 /*========== Externalized Static Function Implementations (Unit Test) =======*/
void ALGO_UnlockInitialization(void)
Calling this function sets a signal that lets ALGO_Initialization() know that the initialization has ...
Definition: algorithm.c:106
Headers for the driver for the storage in the EEPROM memory.
Header for the driver for balancing.
STD_RETURN_TYPE_e BAL_GetInitializationState(void)
gets the initialization state.
BAL_RETURN_TYPE_e
Definition: bal.h:113
@ BAL_ERROR
Definition: bal.h:120
@ BAL_OK
Definition: bal.h:114
@ BAL_STATE_GLOBAL_ENABLE_REQUEST
Definition: bal.h:106
@ BAL_STATE_INIT_REQUEST
Definition: bal.h:101
@ BAL_STATE_GLOBAL_DISABLE_REQUEST
Definition: bal.h:105
BAL_RETURN_TYPE_e BAL_SetStateRequest(BAL_STATE_REQUEST_e stateRequest)
sets the current state request of the state variable bal_state.
#define BS_NR_OF_STRINGS
Number of parallel strings in the battery pack.
#define CURRENT_SENSOR_PRESENT
#define BALANCING_DEFAULT_INACTIVE
BMS_RETURN_TYPE_e BMS_SetStateRequest(BMS_STATE_REQUEST_e statereq)
sets the current state request of the state variable bms_state.
Definition: bms.c:635
STD_RETURN_TYPE_e BMS_GetInitializationState(void)
Gets the initialization state.
Definition: bms.c:627
bms driver header
@ BMS_STATE_INIT_REQUEST
Definition: bms.h:155
bool CAN_IsCurrentSensorCcPresent(uint8_t stringNumber)
get flag if CC message from current sensor is received.
Definition: can.c:430
bool CAN_IsCurrentSensorEcPresent(uint8_t stringNumber)
get flag if EC message from current sensor is received
Definition: can.c:435
bool CAN_IsCurrentSensorPresent(uint8_t stringNumber)
set flag for presence of current sensor.
Definition: can.c:425
void CAN_Initialize(void)
Enables the CAN transceiver.. This function sets th pins to enable the CAN transceiver....
Definition: can.c:213
void CAN_EnablePeriodic(bool command)
enable/disable the periodic transmit/receive.
Definition: can.c:379
Header for the driver for the CAN module.
Headers for the driver for the contactors.
void DATA_ExecuteDataBist(void)
Executes a built-in self-test for the database module.
Definition: database.c:360
DIAG_RETURNTYPE_e DIAG_Handler(DIAG_ID_e diagId, DIAG_EVENT_e event, DIAG_IMPACT_LEVEL_e impact, uint32_t data)
DIAG_Handler provides generic error handling, based on diagnosis group.
Definition: diag.c:231
Diagnosis driver header.
@ DIAG_EVENT_NOT_OK
Definition: diag_cfg.h:239
@ DIAG_STRING
Definition: diag_cfg.h:252
@ DIAG_ID_DEEP_DISCHARGE_DETECTED
Definition: diag_cfg.h:209
#define FAS_ASSERT(x)
Assertion macro that asserts that x is true.
Definition: fassert.h:252
#define FAS_TRAP
Define that evaluates to essential boolean false thus tripping an assert.
Definition: fassert.h:126
FRAM_RETURN_TYPE_e FRAM_ReadData(FRAM_BLOCK_ID_e blockId)
Reads a variable from the FRAM.
Definition: fram.c:193
Header for the driver for the FRAM module.
FRAM_DEEP_DISCHARGE_FLAG_s fram_deepDischargeFlags
Definition: fram_cfg.c:75
@ FRAM_BLOCK_ID_DEEP_DISCHARGE_FLAG
Definition: fram_cfg.h:104
STD_RETURN_TYPE_e
Definition: fstd_types.h:81
@ STD_NOT_OK
Definition: fstd_types.h:83
@ STD_OK
Definition: fstd_types.h:82
#define NULL_PTR
Null pointer.
Definition: fstd_types.h:76
#define GEN_REPEAT_U(x, n)
Macro that helps to generate a series of literals (for array initializers).
Definition: general.h:250
#define GEN_STRIP(x)
Definition: general.h:261
#define GEN_REPEAT_MAXIMUM_REPETITIONS
Definition: general.h:225
bool IMD_GetInitializationState(void)
Gets the initialization state.
Definition: imd.c:521
IMD_RETURN_TYPE_e IMD_RequestInitialization(void)
Request initialization of IMD statemachine.
Definition: imd.c:509
API header for the insulation monitoring device.
@ IMD_REQUEST_OK
Definition: imd.h:77
ILCK_RETURN_TYPE_e ILCK_SetStateRequest(ILCK_STATE_REQUEST_e statereq)
sets the current state request of the state variable ilck_state.
Definition: interlock.c:251
Headers for the driver for the interlock.
@ ILCK_STATE_INITIALIZATION_REQUEST
Definition: interlock.h:84
STD_RETURN_TYPE_e MEAS_StartMeasurement(void)
Makes the initialization request to the AFE state machine.
Definition: meas.c:112
bool MEAS_IsFirstMeasurementCycleFinished(void)
Checks if the first AFE measurement cycle was made.
Definition: meas.c:108
Headers for the driver for the measurements needed by the BMS (e.g., I,V,T).
Declaration of the OS wrapper interface.
void OS_ExitTaskCritical(void)
Exit Critical interface function for use in FreeRTOS-Tasks and FreeRTOS-ISR.
Definition: os_freertos.c:135
void OS_EnterTaskCritical(void)
Enter Critical interface function for use in FreeRTOS-Tasks and FreeRTOS-ISR.
Definition: os_freertos.c:131
SBC_STATEMACHINE_e SBC_GetState(SBC_STATE_s *pInstance)
gets the current state of passed state variable
Definition: sbc.c:252
SBC_STATE_s sbc_stateMcuSupervisor
Definition: sbc.c:76
SBC_RETURN_TYPE_e SBC_SetStateRequest(SBC_STATE_s *pInstance, SBC_STATE_REQUEST_e stateRequest)
sets the current state request of passed state variable
Definition: sbc.c:236
Header for the driver for the SBC module.
SBC_STATEMACHINE_e
Definition: sbc.h:132
@ SBC_STATEMACHINE_RUNNING
Definition: sbc.h:136
@ SBC_STATEMACHINE_UNDEFINED
Definition: sbc.h:138
@ SBC_STATE_INIT_REQUEST
Definition: sbc.h:110
void SOF_Init(void)
initializes the area for SOF (where derating starts and is fully active).
Header for SOX module, responsible for current derating calculation.
void SE_InitializeSoe(bool ec_present, uint8_t stringNumber)
Wrapper for algorithm specific SOE initialization.
void SE_InitializeSoc(bool ccPresent, uint8_t stringNumber)
Wrapper for algorithm specific SOC initialization.
void SE_InitializeSoh(uint8_t stringNumber)
Wrapper for algorithm specific SOH initialization.
Header for state-estimation module responsible for the estimation of state-of-charge (SOC),...
bool deepDischargeFlag[BS_NR_OF_STRINGS]
Definition: fram_cfg.h:153
uint16_t timer
Definition: sys.h:167
uint16_t initializationTimeout
Definition: sys.h:174
uint32_t illegalRequestsCounter
Definition: sys.h:173
SYS_STATE_REQUEST_e stateRequest
Definition: sys.h:168
SYS_STATEMACH_SUB_e substate
Definition: sys.h:170
SYS_STATEMACH_e state
Definition: sys.h:169
uint8_t triggerEntry
Definition: sys.h:175
static SYS_STATE_REQUEST_e SYS_TransferStateRequest(void)
transfers the current state request to the state machine.
Definition: sys.c:598
static void SYS_SetState(SYS_STATE_s *pSystemState, SYS_FSM_STATES_e nextState, SYS_FSM_SUBSTATES_e nextSubstate, uint16_t idleTime)
Sets the next state, the next substate and the timer value of the state variable.
Definition: sys.c:176
SYS_CHECK_MULTIPLE_CALLS_e
Definition: sys.c:87
@ SYS_MULTIPLE_CALLS_YES
Definition: sys.c:89
@ SYS_MULTIPLE_CALLS_NO
Definition: sys.c:88
#define SYS_SAVELASTSTATES(x)
Definition: sys.c:75
#define SYS_BIST_GENERAL_MAGIC_NUMBER
Definition: sys.c:80
SYS_RETURN_TYPE_e SYS_SetStateRequest(SYS_STATE_REQUEST_e stateRequest)
sets the current state request of the state variable sys_state.
Definition: sys.c:609
static STD_RETURN_TYPE_e SYS_RunStateMachine(SYS_STATE_s *pSystemState)
Defines the state transitions.
Definition: sys.c:195
static SYS_RETURN_TYPE_e SYS_CheckStateRequest(SYS_STATE_REQUEST_e stateRequest)
checks the state requests that are made.
Definition: sys.c:633
SYS_STATE_s sys_state
Definition: sys.c:93
STD_RETURN_TYPE_e SYS_Trigger(SYS_STATE_s *pSystemState)
tick function, call this to advance the state machine
Definition: sys.c:666
void SYS_GeneralMacroBist(void)
Definition: sys.c:658
static void SYS_SetSubstate(SYS_STATE_s *pSystemState, SYS_FSM_SUBSTATES_e nextSubstate, uint16_t idleTime)
Sets the next substate and the timer value of the state variable.
Definition: sys.c:190
static SYS_CHECK_MULTIPLE_CALLS_e SYS_CheckMultipleCalls(SYS_STATE_s *pSystemState)
check for multiple calls of state machine trigger function
Definition: sys.c:160
Sys driver header.
@ SYS_BAL_INITIALIZATION_ERROR
Definition: sys.h:137
@ SYS_WAIT_FIRST_MEASUREMENT_CYCLE
Definition: sys.h:133
@ SYS_WAIT_INITIALIZATION_IMD
Definition: sys.h:131
@ SYS_SBC_INITIALIZATION_ERROR
Definition: sys.h:135
@ SYS_WAIT_INITIALIZATION_BAL_GLOBAL_ENABLE
Definition: sys.h:130
@ SYS_BMS_INITIALIZATION_ERROR
Definition: sys.h:140
@ SYS_WAIT_INITIALIZATION_BMS
Definition: sys.h:132
@ SYS_WAIT_INITIALIZATION_SBC
Definition: sys.h:126
@ SYS_WAIT_INITIALIZATION_BAL
Definition: sys.h:129
@ SYS_CURRENT_SENSOR_PRESENCE_ERROR
Definition: sys.h:142
@ SYS_MEAS_INITIALIZATION_ERROR
Definition: sys.h:141
@ SYS_WAIT_CURRENT_SENSOR_PRESENCE
Definition: sys.h:134
@ SYS_IMD_INITIALIZATION_ERROR
Definition: sys.h:139
@ SYS_ENTRY
Definition: sys.h:123
SYS_RETURN_TYPE_e
Definition: sys.h:153
@ SYS_OK
Definition: sys.h:154
@ SYS_REQUEST_PENDING
Definition: sys.h:156
@ SYS_ALREADY_INITIALIZED
Definition: sys.h:158
@ SYS_ILLEGAL_REQUEST
Definition: sys.h:157
SYS_STATE_REQUEST_e
Definition: sys.h:146
@ SYS_STATE_INITIALIZATION_REQUEST
Definition: sys.h:147
@ SYS_STATE_NO_REQUEST
Definition: sys.h:149
@ SYS_STATE_ERROR_REQUEST
Definition: sys.h:148
SYS_FSM_SUBSTATES_e
Definition: sys.h:82
SYS_FSM_STATES_e
Definition: sys.h:72
@ SYS_STATEMACH_INITIALIZE_CAN
Definition: sys.h:108
@ SYS_STATEMACH_SYSTEM_BIST
Definition: sys.h:105
@ SYS_STATEMACH_INITIALIZE_SBC
Definition: sys.h:107
@ SYS_STATEMACH_UNINITIALIZED
Definition: sys.h:103
@ SYS_STATEMACH_INITIALIZE_BALANCING
Definition: sys.h:111
@ SYS_STATEMACH_INITIALIZATION
Definition: sys.h:104
@ SYS_STATEMACH_INITIALIZE_IMD
Definition: sys.h:117
@ SYS_STATEMACH_INITIALIZE_MISC
Definition: sys.h:115
@ SYS_STATEMACH_INITIALIZED
Definition: sys.h:106
@ SYS_STATEMACH_RUNNING
Definition: sys.h:113
@ SYS_STATEMACH_ERROR
Definition: sys.h:118
@ SYS_STATEMACH_FIRST_MEASUREMENT_CYCLE
Definition: sys.h:114
@ SYS_STATEMACH_INITIALIZE_BMS
Definition: sys.h:112
@ SYS_STATEMACH_CHECK_CURRENT_SENSOR_PRESENCE
Definition: sys.h:116
@ SYS_STATEMACH_INITIALIZE_INTERLOCK
Definition: sys.h:109
void SYS_SendBootMessage(void)
Function to send out boot message with SW version.
Definition: sys_cfg.c:70
#define SYS_FSM_MEDIUM_TIME
Definition: sys_cfg.h:81
#define SYS_STATEMACH_BAL_INITIALIZATION_TIMEOUT_MS
Definition: sys_cfg.h:99
#define SYS_STATEMACH_INITIALIZATION_TIMEOUT_MS
Definition: sys_cfg.h:93
#define SYS_FSM_LONG_TIME
Definition: sys_cfg.h:87
#define SYS_STATEMACHINE_SBC_INIT_TIMEOUT_MS
Definition: sys_cfg.h:102
#define SYS_STATEMACH_INITIALIZATION_REQUEST_RETRY_COUNTER
Definition: sys_cfg.h:90
#define SYS_STATEMACH_IMD_INITIALIZATION_TIMEOUT_MS
Definition: sys_cfg.h:96
#define SYS_TASK_CYCLE_CONTEXT_MS
Definition: sys_cfg.h:69
#define SYS_FSM_SHORT_TIME
Definition: sys_cfg.h:75