foxBMS  1.5.0
The foxBMS Battery Management System API Documentation
sys.c
Go to the documentation of this file.
1 /**
2  *
3  * @copyright © 2010 - 2023, 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 2023-02-03 (date of last update)
47  * @version v1.5.0
48  * @ingroup ENGINE
49  * @prefix SYS
50  *
51  * @brief Sys driver implementation
52  */
53 
54 /*========== Includes =======================================================*/
55 #include "general.h"
56 
57 #include "sys.h"
58 
59 #include "algorithm.h"
60 #include "bal.h"
61 #include "bms.h"
62 #include "can.h"
63 #include "contactor.h"
64 #include "diag.h"
65 #include "fram.h"
66 #include "fstd_types.h"
67 #include "imd.h"
68 #include "interlock.h"
69 #include "meas.h"
70 #include "os.h"
71 #include "sbc.h"
72 #include "sof_trapezoid.h"
73 #include "state_estimation.h"
74 
75 #include <stdint.h>
76 
77 /*========== Macros and Definitions =========================================*/
78 
79 /** Saves the last state and the last substate */
80 #define SYS_SAVELASTSTATES(x) \
81  (x)->lastState = (x)->state; \
82  (x)->lastSubstate = (x)->substate
83 
84 /** Magic number that is searched by the #SYS_GeneralMacroBist(). */
85 #define SYS_BIST_GENERAL_MAGIC_NUMBER (42u)
86 
87 /*========== Static Constant and Variable Definitions =======================*/
88 
89 /*========== Extern Constant and Variable Definitions =======================*/
90 
91 /** Symbolic names to check for multiple calls of #SYS_Trigger() */
92 typedef enum {
93  SYS_MULTIPLE_CALLS_NO, /*!< no multiple calls, OK */
94  SYS_MULTIPLE_CALLS_YES, /*!< multiple calls, not OK */
96 
97 /** contains the state of the contactor state machine */
99  .timer = 0,
100  .triggerEntry = 0,
101  .stateRequest = SYS_STATE_NO_REQUEST,
103  .substate = SYS_ENTRY,
104  .lastState = SYS_STATEMACH_UNINITIALIZED,
105  .lastSubstate = SYS_ENTRY,
106  .illegalRequestsCounter = 0,
107  .initializationTimeout = 0,
108 };
109 
110 /*========== Static Function Prototypes =====================================*/
111 /**
112  * @brief check for multiple calls of state machine trigger function
113  * @details The trigger function is not reentrant, which means it cannot
114  * be called multiple times. This functions increments the
115  * triggerEntry counter once and must be called each time the
116  * trigger function is called. If triggerEntry is greater than
117  * one, there were multiple calls. For this function to work,
118  * triggerEntry must be decremented each time the trigger function
119  * is called, even if no processing do because the timer is
120  * non-zero.
121  * @param pSystemState state of the fake state machine
122  * @return #SYS_MULTIPLE_CALLS_YES if there were multiple calls,
123  * #SYS_MULTIPLE_CALLS_NO otherwise
124  */
126 
127 /**
128  * @brief Sets the next state, the next substate and the timer value
129  * of the state variable.
130  * @param pSystemState state of the example state machine
131  * @param nextState state to be transferred into
132  * @param nextSubstate substate to be transferred into
133  * @param idleTime wait time for the state machine
134  */
135 static void SYS_SetState(
136  SYS_STATE_s *pSystemState,
137  SYS_FSM_STATES_e nextState,
138  SYS_FSM_SUBSTATES_e nextSubstate,
139  uint16_t idleTime);
140 
141 /**
142  * @brief Sets the next substate and the timer value
143  * of the state variable.
144  * @param pSystemState state of the example state machine
145  * @param nextSubstate substate to be transferred into
146  * @param idleTime wait time for the state machine
147  */
148 static void SYS_SetSubstate(SYS_STATE_s *pSystemState, SYS_FSM_SUBSTATES_e nextSubstate, uint16_t idleTime);
149 
150 /**
151  * @brief Defines the state transitions
152  * @details This function contains the implementation of the state
153  * machine, i.e., the sequence of states and substates.
154  * It is called by the trigger function every time
155  * the state machine timer has a non-zero value.
156  * @param pSystemState state of the example state machine
157  * @return TODO
158  */
160 
163 
164 /*========== Static Function Implementations ================================*/
166  FAS_ASSERT(pSystemState != NULL_PTR);
169  if (pSystemState->triggerEntry == 0u) {
170  pSystemState->triggerEntry++;
171  } else {
172  multipleCalls = SYS_MULTIPLE_CALLS_YES;
173  }
175  return multipleCalls;
176 }
177 
178 #pragma diag_push
179 #pragma diag_suppress 179
180 #pragma WEAK(SYS_SetState)
181 static void SYS_SetState(
182  SYS_STATE_s *pSystemState,
183  SYS_FSM_STATES_e nextState,
184  SYS_FSM_SUBSTATES_e nextSubstate,
185  uint16_t idleTime) {
186  FAS_ASSERT(pSystemState != NULL_PTR);
187 
188  pSystemState->timer = idleTime;
189 }
190 #pragma diag_pop
191 
192 #pragma diag_push
193 #pragma diag_suppress 179
194 #pragma WEAK(SYS_SetSubstate)
195 static void SYS_SetSubstate(SYS_STATE_s *pSystemState, SYS_FSM_SUBSTATES_e nextSubstate, uint16_t idleTime) {
196  FAS_ASSERT(pSystemState != NULL_PTR);
197 }
198 #pragma diag_push
199 
201  STD_RETURN_TYPE_e ranStateMachine = STD_OK;
202 
205  STD_RETURN_TYPE_e balancingInitializationState = STD_OK;
206  BAL_RETURN_TYPE_e balancingGlobalEnableState = BAL_ERROR;
207  STD_RETURN_TYPE_e bmsState = STD_NOT_OK;
208  bool imdInitialized = false;
209 
210  switch (pSystemState->state) {
211  /****************************UNINITIALIZED***********************************/
213  /* waiting for Initialization Request */
214  stateRequest = SYS_TransferStateRequest();
215  if (stateRequest == SYS_STATE_INITIALIZATION_REQUEST) {
216  SYS_SAVELASTSTATES(pSystemState);
217  pSystemState->timer = SYS_FSM_SHORT_TIME;
218  pSystemState->state = SYS_STATEMACH_INITIALIZATION;
219  pSystemState->substate = SYS_ENTRY;
220  } else if (stateRequest == SYS_STATE_NO_REQUEST) {
221  /* no actual request pending */
222  } else {
223  pSystemState->illegalRequestsCounter++; /* illegal request pending */
224  }
225  break;
226  /****************************INITIALIZATION**********************************/
228 
229  SYS_SAVELASTSTATES(pSystemState);
230  /* Initializations done here */
232  for (uint8_t s = 0u; s < BS_NR_OF_STRINGS; s++) {
235  }
236  }
237 
238  pSystemState->timer = SYS_FSM_SHORT_TIME;
239  pSystemState->state = SYS_STATEMACH_INITIALIZE_SBC;
240  pSystemState->substate = SYS_ENTRY;
241  break;
242 
243  /**************************** INITIALIZE SBC *************************************/
245  SYS_SAVELASTSTATES(pSystemState);
246 
247  if (pSystemState->substate == SYS_ENTRY) {
249  pSystemState->timer = SYS_FSM_SHORT_TIME;
250  pSystemState->substate = SYS_WAIT_INITIALIZATION_SBC;
251  pSystemState->initializationTimeout = 0;
252  break;
253  } else if (pSystemState->substate == SYS_WAIT_INITIALIZATION_SBC) {
255  if (sbcState == SBC_STATEMACHINE_RUNNING) {
256  pSystemState->timer = SYS_FSM_SHORT_TIME;
257  pSystemState->state = SYS_STATEMACH_INITIALIZE_CAN;
258  pSystemState->substate = SYS_ENTRY;
259  break;
260  } else {
261  if (pSystemState->initializationTimeout >
263  pSystemState->timer = SYS_FSM_SHORT_TIME;
264  pSystemState->state = SYS_STATEMACH_ERROR;
265  pSystemState->substate = SYS_SBC_INITIALIZATION_ERROR;
266  break;
267  }
268  pSystemState->timer = SYS_FSM_SHORT_TIME;
269  pSystemState->initializationTimeout++;
270  break;
271  }
272  } else {
273  FAS_ASSERT(FAS_TRAP); /* substate does not exist in this state */
274  }
275  break;
276 
277  /**************************** INITIALIZE CAN TRANSCEIVER ****************************/
279  CAN_Initialize();
280  pSystemState->timer = SYS_FSM_SHORT_TIME;
281  pSystemState->state = SYS_STATEMACH_SYSTEM_BIST;
282  pSystemState->substate = SYS_ENTRY;
283  break;
284 
285  /**************************** EXECUTE STARTUP BIST **********************************/
287  SYS_SAVELASTSTATES(pSystemState);
288  /* run BIST functions */
291 
292  pSystemState->timer = SYS_FSM_SHORT_TIME;
293  pSystemState->state = SYS_STATEMACH_INITIALIZED;
294  pSystemState->substate = SYS_ENTRY;
295  break;
296 
297  /****************************INITIALIZED*************************************/
299  SYS_SAVELASTSTATES(pSystemState);
300  /* Send CAN boot message directly on CAN */
302 
303  pSystemState->timer = SYS_FSM_SHORT_TIME;
305  pSystemState->substate = SYS_ENTRY;
306  break;
307 
308  /****************************INITIALIZE INTERLOCK*************************************/
310  SYS_SAVELASTSTATES(pSystemState);
312  pSystemState->timer = SYS_FSM_SHORT_TIME;
314  pSystemState->substate = SYS_ENTRY;
315  pSystemState->initializationTimeout = 0;
316  break;
317 
318  /****************************INITIALIZE CONTACTORS*************************************/
319  /* TODO: check if necessary and add */
320 
321  /****************************INITIALIZE BALANCING*************************************/
323  SYS_SAVELASTSTATES(pSystemState);
324  if (pSystemState->substate == SYS_ENTRY) {
326  pSystemState->timer = SYS_FSM_SHORT_TIME;
327  pSystemState->substate = SYS_WAIT_INITIALIZATION_BAL;
328  pSystemState->initializationTimeout = 0;
329  break;
330  } else if (pSystemState->substate == SYS_WAIT_INITIALIZATION_BAL) {
331  balancingInitializationState = BAL_GetInitializationState();
332  if (balancingInitializationState == STD_OK) {
333  pSystemState->timer = SYS_FSM_SHORT_TIME;
335  break;
336  } else {
337  if (pSystemState->initializationTimeout >
339  pSystemState->timer = SYS_FSM_SHORT_TIME;
340  pSystemState->state = SYS_STATEMACH_ERROR;
341  pSystemState->substate = SYS_BAL_INITIALIZATION_ERROR;
342  break;
343  }
344  pSystemState->timer = SYS_FSM_SHORT_TIME;
345  pSystemState->initializationTimeout++;
346  break;
347  }
348  } else if (pSystemState->substate == SYS_WAIT_INITIALIZATION_BAL_GLOBAL_ENABLE) {
349  if (BS_BALANCING_DEFAULT_INACTIVE == true) {
350  balancingGlobalEnableState = BAL_SetStateRequest(BAL_STATE_GLOBAL_DISABLE_REQUEST);
351  } else {
352  balancingGlobalEnableState = BAL_SetStateRequest(BAL_STATE_GLOBAL_ENABLE_REQUEST);
353  }
354  if (balancingGlobalEnableState == BAL_OK) {
355  pSystemState->timer = SYS_FSM_SHORT_TIME;
357  pSystemState->substate = SYS_ENTRY;
358  break;
359  } else {
360  if (pSystemState->initializationTimeout >
362  pSystemState->timer = SYS_FSM_SHORT_TIME;
363  pSystemState->state = SYS_STATEMACH_ERROR;
364  pSystemState->substate = SYS_BAL_INITIALIZATION_ERROR;
365  break;
366  }
367  pSystemState->timer = SYS_FSM_SHORT_TIME;
368  pSystemState->initializationTimeout++;
369  break;
370  }
371  }
372  break;
373 
374  /****************************START FIRST MEAS CYCLE**************************/
376  SYS_SAVELASTSTATES(pSystemState);
377  if (pSystemState->substate == SYS_ENTRY) {
379  pSystemState->initializationTimeout = 0;
381  } else if (pSystemState->substate == SYS_WAIT_FIRST_MEASUREMENT_CYCLE) {
382  if (MEAS_IsFirstMeasurementCycleFinished() == true) {
383  /* allow initialization of algorithm module */
385  /* MEAS_RequestOpenWireCheck(); */ /*TODO: check with strings */
386  pSystemState->timer = SYS_FSM_SHORT_TIME;
387  if (BS_CURRENT_SENSOR_PRESENT == true) {
389  } else {
390  pSystemState->state = SYS_STATEMACH_INITIALIZE_MISC;
391  }
392  pSystemState->substate = SYS_ENTRY;
393  break;
394  } else {
395  if (pSystemState->initializationTimeout >
397  pSystemState->timer = SYS_FSM_SHORT_TIME;
398  pSystemState->state = SYS_STATEMACH_ERROR;
399  pSystemState->substate = SYS_MEAS_INITIALIZATION_ERROR;
400  break;
401  } else {
402  pSystemState->timer = SYS_FSM_MEDIUM_TIME;
403  pSystemState->initializationTimeout++;
404  break;
405  }
406  }
407  }
408  break;
409 
410  /****************************CHECK CURRENT SENSOR PRESENCE*************************************/
412  SYS_SAVELASTSTATES(pSystemState);
413 
414  if (pSystemState->substate == SYS_ENTRY) {
415  pSystemState->initializationTimeout = 0;
416  CAN_EnablePeriodic(true);
417 #if defined(CURRENT_SENSOR_ISABELLENHUETTE_TRIGGERED)
418  /* If triggered mode is used, CAN trigger message needs to
419  * be transmitted and current sensor response has to be
420  * received afterwards. This may take some time, therefore
421  * delay has to be increased.
422  */
423  pSystemState->timer = SYS_FSM_LONG_TIME_MS;
424 #else /* defined(CURRENT_SENSOR_ISABELLENHUETTE_TRIGGERED) */
425  pSystemState->timer = SYS_FSM_LONG_TIME;
426 #endif /* defined(CURRENT_SENSOR_ISABELLENHUETTE_TRIGGERED) */
428  } else if (pSystemState->substate == SYS_WAIT_CURRENT_SENSOR_PRESENCE) {
429  bool allSensorsPresent = true;
430  for (uint8_t s = 0u; s < BS_NR_OF_STRINGS; s++) {
431  if (CAN_IsCurrentSensorPresent(s) == true) {
432  if (CAN_IsCurrentSensorCcPresent(s) == true) {
433  SE_InitializeSoc(true, s);
434  } else {
435  SE_InitializeSoc(false, s);
436  }
437  if (CAN_IsCurrentSensorEcPresent(s) == true) {
438  SE_InitializeSoe(true, s);
439  } else {
440  SE_InitializeSoe(false, s);
441  }
442  SE_InitializeSoh(s);
443  } else {
444  allSensorsPresent = false;
445  }
446  }
447 
448  if (allSensorsPresent == true) {
449  SOF_Init();
450 
451  pSystemState->timer = SYS_FSM_SHORT_TIME;
452  pSystemState->state = SYS_STATEMACH_INITIALIZE_MISC;
453  pSystemState->substate = SYS_ENTRY;
454  break;
455  } else {
456  if (pSystemState->initializationTimeout >
458  pSystemState->timer = SYS_FSM_SHORT_TIME;
459  pSystemState->state = SYS_STATEMACH_ERROR;
461  break;
462  } else {
463  pSystemState->timer = SYS_FSM_MEDIUM_TIME;
464  pSystemState->initializationTimeout++;
465  break;
466  }
467  }
468  } else {
469  FAS_ASSERT(FAS_TRAP); /* substate does not exist in this state */
470  }
471  break;
472 
473  /****************************INITIALIZED_MISC*************************************/
475  SYS_SAVELASTSTATES(pSystemState);
476 
477  if (BS_CURRENT_SENSOR_PRESENT == false) {
478  CAN_EnablePeriodic(true);
479  for (uint8_t s = 0u; s < BS_NR_OF_STRINGS; s++) {
480  SE_InitializeSoc(false, s);
481  SE_InitializeSoe(false, s);
482  SE_InitializeSoh(s);
483  }
484  }
485 
486  pSystemState->initializationTimeout = 0u;
487  pSystemState->timer = SYS_FSM_SHORT_TIME;
488  pSystemState->state = SYS_STATEMACH_INITIALIZE_IMD;
489  pSystemState->substate = SYS_ENTRY;
490  break;
491 
492  /****************************INITIALIZED_IMD*************************************/
493 
495  SYS_SAVELASTSTATES(pSystemState);
496 
497  if (pSystemState->substate == SYS_ENTRY) {
499  /* Request inquired successfully */
500  pSystemState->timer = SYS_FSM_MEDIUM_TIME;
501  pSystemState->state = SYS_STATEMACH_INITIALIZE_BMS;
502  pSystemState->substate = SYS_ENTRY;
503  pSystemState->initializationTimeout = 0u;
504  } else {
505  /* Request declined -> retry max. 3 times */
506  pSystemState->initializationTimeout++;
507  pSystemState->timer = SYS_FSM_SHORT_TIME;
509  /* Switch to error state */
510  pSystemState->timer = SYS_FSM_SHORT_TIME;
511  pSystemState->state = SYS_STATEMACH_ERROR;
512  pSystemState->substate = SYS_IMD_INITIALIZATION_ERROR;
513  }
514  }
515  break;
516  } else if (pSystemState->substate == SYS_WAIT_INITIALIZATION_IMD) {
517  imdInitialized = IMD_GetInitializationState();
518  if (imdInitialized == true) {
519  pSystemState->timer = SYS_FSM_SHORT_TIME;
520  pSystemState->state = SYS_STATEMACH_INITIALIZE_BMS;
521  pSystemState->substate = SYS_ENTRY;
522  break;
523  } else {
524  if (pSystemState->initializationTimeout >
526  pSystemState->timer = SYS_FSM_SHORT_TIME;
527  pSystemState->state = SYS_STATEMACH_ERROR;
528  pSystemState->substate = SYS_IMD_INITIALIZATION_ERROR;
529  break;
530  }
531  pSystemState->timer = SYS_FSM_SHORT_TIME;
532  pSystemState->initializationTimeout++;
533  break;
534  }
535  } else {
536  FAS_ASSERT(FAS_TRAP); /* substate does not exist in this state */
537  }
538  break;
539 
540  /****************************INITIALIZE BMS*************************************/
542  SYS_SAVELASTSTATES(pSystemState);
543 
544  if (pSystemState->substate == SYS_ENTRY) {
546  pSystemState->timer = SYS_FSM_SHORT_TIME;
547  pSystemState->substate = SYS_WAIT_INITIALIZATION_BMS;
548  pSystemState->initializationTimeout = 0;
549  break;
550  } else if (pSystemState->substate == SYS_WAIT_INITIALIZATION_BMS) {
551  bmsState = BMS_GetInitializationState();
552  if (bmsState == STD_OK) {
553  pSystemState->timer = SYS_FSM_SHORT_TIME;
554  pSystemState->state = SYS_STATEMACH_RUNNING;
555  pSystemState->substate = SYS_ENTRY;
556  break;
557  } else {
558  if (pSystemState->initializationTimeout >
560  pSystemState->timer = SYS_FSM_SHORT_TIME;
561  pSystemState->state = SYS_STATEMACH_ERROR;
562  pSystemState->substate = SYS_BMS_INITIALIZATION_ERROR;
563  break;
564  }
565  pSystemState->timer = SYS_FSM_SHORT_TIME;
566  pSystemState->initializationTimeout++;
567  break;
568  }
569  } else {
570  FAS_ASSERT(FAS_TRAP); /* substate does not exist in this state */
571  }
572  break;
573 
574  /****************************RUNNING*************************************/
576  SYS_SAVELASTSTATES(pSystemState);
577  pSystemState->timer = SYS_FSM_LONG_TIME;
578  break;
579 
580  /****************************ERROR*************************************/
581  case SYS_STATEMACH_ERROR:
582  SYS_SAVELASTSTATES(pSystemState);
583  pSystemState->timer = SYS_FSM_LONG_TIME;
584  break;
585  /***************************DEFAULT CASE*************************************/
586  default:
587  /* invalid state */
589  break;
590  }
591  return ranStateMachine;
592 }
593 
594 /**
595  * @brief transfers the current state request to the state machine.
596  *
597  * This function takes the current state request from #sys_state and transfers it to the state machine.
598  * It resets the value from #sys_state to #SYS_STATE_NO_REQUEST
599  *
600  * @return retVal current state request, taken from #SYS_STATE_REQUEST_e
601  *
602  */
605 
607  retval = sys_state.stateRequest;
610 
611  return (retval);
612 }
613 
616 
618  retVal = SYS_CheckStateRequest(stateRequest);
619 
620  if (retVal == SYS_OK) {
621  sys_state.stateRequest = stateRequest;
622  }
624 
625  return (retVal);
626 }
627 
628 /**
629  * @brief checks the state requests that are made.
630  *
631  * This function checks the validity of the state requests.
632  * The results of the checked is returned immediately.
633  *
634  * @param stateRequest state request to be checked
635  *
636  * @return result of the state request that was made, taken from SYS_RETURN_TYPE_e
637  */
640  if (stateRequest == SYS_STATE_ERROR_REQUEST) {
641  retval = SYS_OK;
642  } else {
644  /* init only allowed from the uninitialized state */
645  if (stateRequest == SYS_STATE_INITIALIZATION_REQUEST) {
647  retval = SYS_OK;
648  } else {
649  retval = SYS_ALREADY_INITIALIZED;
650  }
651  } else {
652  retval = SYS_ILLEGAL_REQUEST;
653  }
654  } else {
655  retval = SYS_REQUEST_PENDING;
656  }
657  }
658  return retval;
659 }
660 
661 /*========== Extern Function Implementations ================================*/
662 
663 extern void SYS_GeneralMacroBist(void) {
664  const uint8_t dummy[GEN_REPEAT_MAXIMUM_REPETITIONS] = {
666  for (uint8_t i = 0u; i < GEN_REPEAT_MAXIMUM_REPETITIONS; i++) {
668  }
669 }
670 
672  FAS_ASSERT(pSystemState != NULL_PTR);
673  bool earlyExit = false;
674  STD_RETURN_TYPE_e returnValue = STD_OK;
675 
676  /* Check multiple calls of function */
677  if (SYS_MULTIPLE_CALLS_YES == SYS_CheckMultipleCalls(pSystemState)) {
678  returnValue = STD_NOT_OK;
679  earlyExit = true;
680  }
681 
682  if (earlyExit == false) {
683  if (pSystemState->timer > 0u) {
684  if ((--pSystemState->timer) > 0u) {
685  pSystemState->triggerEntry--;
686  returnValue = STD_OK;
687  earlyExit = true;
688  }
689  }
690  }
691 
692  if (earlyExit == false) {
693  SYS_RunStateMachine(pSystemState);
694  pSystemState->triggerEntry--;
695  }
696  return returnValue;
697 }
698 
699 /*========== Externalized Static Function Implementations (Unit Test) =======*/
700 #ifdef UNITY_UNIT_TEST
701 #endif
void ALGO_UnlockInitialization(void)
Calling this function sets a signal that lets ALGO_Initialization() know that the initialization has ...
Definition: algorithm.c:110
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:115
@ BAL_ERROR
Definition: bal.h:122
@ BAL_OK
Definition: bal.h:116
@ BAL_STATE_GLOBAL_ENABLE_REQUEST
Definition: bal.h:108
@ BAL_STATE_INIT_REQUEST
Definition: bal.h:103
@ BAL_STATE_GLOBAL_DISABLE_REQUEST
Definition: bal.h:107
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 BS_BALANCING_DEFAULT_INACTIVE
Defines whether balancing shall be available or not.
#define BS_CURRENT_SENSOR_PRESENT
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:811
STD_RETURN_TYPE_e BMS_GetInitializationState(void)
Gets the initialization state.
Definition: bms.c:803
bms driver header
@ BMS_STATE_INIT_REQUEST
Definition: bms.h:164
bool CAN_IsCurrentSensorCcPresent(uint8_t stringNumber)
get flag if CC message from current sensor is received.
Definition: can.c:862
bool CAN_IsCurrentSensorEcPresent(uint8_t stringNumber)
get flag if EC message from current sensor is received
Definition: can.c:867
bool CAN_IsCurrentSensorPresent(uint8_t stringNumber)
set flag for presence of current sensor.
Definition: can.c:857
void CAN_Initialize(void)
Enables the CAN transceiver.. This function sets th pins to enable the CAN transceiver....
Definition: can.c:771
void CAN_EnablePeriodic(bool command)
Enables periodic sending per CAN. This is used to prevent sending uninitialized data per CAN (e....
Definition: can.c:849
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:363
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:246
Diagnosis driver header.
@ DIAG_EVENT_NOT_OK
Definition: diag_cfg.h:266
@ DIAG_STRING
Definition: diag_cfg.h:279
@ DIAG_ID_DEEP_DISCHARGE_DETECTED
Definition: diag_cfg.h:228
#define FAS_ASSERT(x)
Assertion macro that asserts that x is true.
Definition: fassert.h:254
#define FAS_TRAP
Define that evaluates to essential boolean false thus tripping an assert.
Definition: fassert.h:129
FRAM_RETURN_TYPE_e FRAM_ReadData(FRAM_BLOCK_ID_e blockId)
Reads a variable from the FRAM.
Definition: fram.c:211
Header for the driver for the FRAM module.
FRAM_DEEP_DISCHARGE_FLAG_s fram_deepDischargeFlags
Definition: fram_cfg.c:77
@ FRAM_BLOCK_ID_DEEP_DISCHARGE_FLAG
Definition: fram_cfg.h:108
Definition of foxBMS standard types.
STD_RETURN_TYPE_e
Definition: fstd_types.h:82
@ STD_NOT_OK
Definition: fstd_types.h:84
@ STD_OK
Definition: fstd_types.h:83
#define NULL_PTR
Null pointer.
Definition: fstd_types.h:77
General macros and definitions for the whole platform.
#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:524
IMD_RETURN_TYPE_e IMD_RequestInitialization(void)
Request initialization of IMD statemachine.
Definition: imd.c:512
API header for the insulation monitoring device.
@ IMD_REQUEST_OK
Definition: imd.h:78
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:254
Headers for the driver for the interlock.
@ ILCK_STATE_INITIALIZATION_REQUEST
Definition: interlock.h:86
STD_RETURN_TYPE_e MEAS_StartMeasurement(void)
Makes the initialization request to the AFE state machine.
Definition: meas.c:117
bool MEAS_IsFirstMeasurementCycleFinished(void)
Checks if the first AFE measurement cycle was made.
Definition: meas.c:113
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:138
void OS_EnterTaskCritical(void)
Enter Critical interface function for use in FreeRTOS-Tasks and FreeRTOS-ISR.
Definition: os_freertos.c:134
SBC_STATEMACHINE_e SBC_GetState(SBC_STATE_s *pInstance)
gets the current state of passed state variable
Definition: sbc.c:254
SBC_STATE_s sbc_stateMcuSupervisor
Definition: sbc.c:78
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:238
Header for the driver for the SBC module.
SBC_STATEMACHINE_e
Definition: sbc.h:133
@ SBC_STATEMACHINE_RUNNING
Definition: sbc.h:137
@ SBC_STATEMACHINE_UNDEFINED
Definition: sbc.h:139
@ SBC_STATE_INIT_REQUEST
Definition: sbc.h:111
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:159
uint16_t timer
Definition: sys.h:171
uint16_t initializationTimeout
Definition: sys.h:178
uint32_t illegalRequestsCounter
Definition: sys.h:177
SYS_STATE_REQUEST_e stateRequest
Definition: sys.h:172
SYS_STATEMACH_SUB_e substate
Definition: sys.h:174
SYS_STATEMACH_e state
Definition: sys.h:173
uint8_t triggerEntry
Definition: sys.h:179
static SYS_STATE_REQUEST_e SYS_TransferStateRequest(void)
transfers the current state request to the state machine.
Definition: sys.c:603
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:181
SYS_CHECK_MULTIPLE_CALLS_e
Definition: sys.c:92
@ SYS_MULTIPLE_CALLS_YES
Definition: sys.c:94
@ SYS_MULTIPLE_CALLS_NO
Definition: sys.c:93
#define SYS_SAVELASTSTATES(x)
Definition: sys.c:80
#define SYS_BIST_GENERAL_MAGIC_NUMBER
Definition: sys.c:85
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:614
static STD_RETURN_TYPE_e SYS_RunStateMachine(SYS_STATE_s *pSystemState)
Defines the state transitions.
Definition: sys.c:200
static SYS_RETURN_TYPE_e SYS_CheckStateRequest(SYS_STATE_REQUEST_e stateRequest)
checks the state requests that are made.
Definition: sys.c:638
SYS_STATE_s sys_state
Definition: sys.c:98
STD_RETURN_TYPE_e SYS_Trigger(SYS_STATE_s *pSystemState)
tick function, call this to advance the state machine
Definition: sys.c:671
void SYS_GeneralMacroBist(void)
Definition: sys.c:663
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:195
static SYS_CHECK_MULTIPLE_CALLS_e SYS_CheckMultipleCalls(SYS_STATE_s *pSystemState)
check for multiple calls of state machine trigger function
Definition: sys.c:165
Sys driver header.
@ SYS_BAL_INITIALIZATION_ERROR
Definition: sys.h:141
@ SYS_WAIT_FIRST_MEASUREMENT_CYCLE
Definition: sys.h:137
@ SYS_WAIT_INITIALIZATION_IMD
Definition: sys.h:135
@ SYS_SBC_INITIALIZATION_ERROR
Definition: sys.h:139
@ SYS_WAIT_INITIALIZATION_BAL_GLOBAL_ENABLE
Definition: sys.h:134
@ SYS_BMS_INITIALIZATION_ERROR
Definition: sys.h:144
@ SYS_WAIT_INITIALIZATION_BMS
Definition: sys.h:136
@ SYS_WAIT_INITIALIZATION_SBC
Definition: sys.h:130
@ SYS_WAIT_INITIALIZATION_BAL
Definition: sys.h:133
@ SYS_CURRENT_SENSOR_PRESENCE_ERROR
Definition: sys.h:146
@ SYS_MEAS_INITIALIZATION_ERROR
Definition: sys.h:145
@ SYS_WAIT_CURRENT_SENSOR_PRESENCE
Definition: sys.h:138
@ SYS_IMD_INITIALIZATION_ERROR
Definition: sys.h:143
@ SYS_ENTRY
Definition: sys.h:127
SYS_RETURN_TYPE_e
Definition: sys.h:157
@ SYS_OK
Definition: sys.h:158
@ SYS_REQUEST_PENDING
Definition: sys.h:160
@ SYS_ALREADY_INITIALIZED
Definition: sys.h:162
@ SYS_ILLEGAL_REQUEST
Definition: sys.h:161
SYS_STATE_REQUEST_e
Definition: sys.h:150
@ SYS_STATE_INITIALIZATION_REQUEST
Definition: sys.h:151
@ SYS_STATE_NO_REQUEST
Definition: sys.h:153
@ SYS_STATE_ERROR_REQUEST
Definition: sys.h:152
SYS_FSM_SUBSTATES_e
Definition: sys.h:86
SYS_FSM_STATES_e
Definition: sys.h:76
@ SYS_STATEMACH_INITIALIZE_CAN
Definition: sys.h:112
@ SYS_STATEMACH_SYSTEM_BIST
Definition: sys.h:109
@ SYS_STATEMACH_INITIALIZE_SBC
Definition: sys.h:111
@ SYS_STATEMACH_UNINITIALIZED
Definition: sys.h:107
@ SYS_STATEMACH_INITIALIZE_BALANCING
Definition: sys.h:115
@ SYS_STATEMACH_INITIALIZATION
Definition: sys.h:108
@ SYS_STATEMACH_INITIALIZE_IMD
Definition: sys.h:121
@ SYS_STATEMACH_INITIALIZE_MISC
Definition: sys.h:119
@ SYS_STATEMACH_INITIALIZED
Definition: sys.h:110
@ SYS_STATEMACH_RUNNING
Definition: sys.h:117
@ SYS_STATEMACH_ERROR
Definition: sys.h:122
@ SYS_STATEMACH_FIRST_MEASUREMENT_CYCLE
Definition: sys.h:118
@ SYS_STATEMACH_INITIALIZE_BMS
Definition: sys.h:116
@ SYS_STATEMACH_CHECK_CURRENT_SENSOR_PRESENCE
Definition: sys.h:120
@ SYS_STATEMACH_INITIALIZE_INTERLOCK
Definition: sys.h:113
void SYS_SendBootMessage(void)
Function to send out boot message with SW version.
Definition: sys_cfg.c:74
#define SYS_FSM_MEDIUM_TIME
Definition: sys_cfg.h:82
#define SYS_STATEMACH_BAL_INITIALIZATION_TIMEOUT_MS
Definition: sys_cfg.h:100
#define SYS_STATEMACH_INITIALIZATION_TIMEOUT_MS
Definition: sys_cfg.h:94
#define SYS_FSM_LONG_TIME
Definition: sys_cfg.h:88
#define SYS_STATEMACHINE_SBC_INIT_TIMEOUT_MS
Definition: sys_cfg.h:103
#define SYS_STATEMACH_INITIALIZATION_REQUEST_RETRY_COUNTER
Definition: sys_cfg.h:91
#define SYS_STATEMACH_IMD_INITIALIZATION_TIMEOUT_MS
Definition: sys_cfg.h:97
#define SYS_TASK_CYCLE_CONTEXT_MS
Definition: sys_cfg.h:70
#define SYS_FSM_SHORT_TIME
Definition: sys_cfg.h:76