foxBMS  1.0.0
The foxBMS Battery Management System API Documentation
sys.h
Go to the documentation of this file.
1 /**
2  *
3  * @copyright © 2010 - 2021, Fraunhofer-Gesellschaft zur Foerderung der
4  * angewandten Forschung e.V. All rights reserved.
5  *
6  * BSD 3-Clause License
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions are met:
9  * 1. Redistributions of source code must retain the above copyright notice,
10  * this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  * notice, this list of conditions and the following disclaimer in the
13  * documentation and/or other materials provided with the distribution.
14  * 3. Neither the name of the copyright holder nor the names of its
15  * contributors may be used to endorse or promote products derived from
16  * this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
22  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28  * POSSIBILITY OF SUCH DAMAGE.
29  *
30  * We kindly request you to use one or more of the following phrases to refer
31  * to foxBMS in your hardware, software, documentation or advertising
32  * materials:
33  *
34  * ″This product uses parts of foxBMS®″
35  *
36  * ″This product includes parts of foxBMS®″
37  *
38  * ″This product is derived from foxBMS®″
39  *
40  */
41 
42 /**
43  * @file sys.h
44  * @author foxBMS Team
45  * @date 2020-02-24 (date of creation)
46  * @updated 2020-05-08 (date of last update)
47  * @ingroup ENGINE
48  * @prefix SYS
49  *
50  * @brief Sys driver header
51  *
52  *
53  */
54 
55 #ifndef FOXBMS__SYS_H_
56 #define FOXBMS__SYS_H_
57 
58 /*========== Includes =======================================================*/
59 #include "sys_cfg.h"
60 
61 /*========== Macros and Definitions =========================================*/
62 
63 /** Symbolic names for busyness of the system */
64 typedef enum SYS_CHECK {
65  SYS_CHECK_OK, /*!< system ok */
66  SYS_CHECK_BUSY, /*!< system busy */
67  SYS_CHECK_NOT_OK, /*!< system not ok */
69 
70 /** States of the state machine */
71 typedef enum SYS_FSM_STATES {
72  SYS_FSM_STATE_DUMMY, /*!< dummy state - always the first state */
73  SYS_FSM_STATE_HAS_NEVER_RUN, /*!< never run state - always the second state */
74  SYS_FSM_STATE_UNINITIALIZED, /*!< uninitialized state */
75  SYS_FSM_STATE_INITIALIZATION, /*!< initializing the state machine */
76  SYS_FSM_STATE_RUNNING, /*!< operational mode of the state machine */
77  SYS_FSM_STATE_ERROR, /*!< state for error processing */
79 
80 /** Substates of the state machine */
81 typedef enum SYS_FSM_SUBSTATES {
82  SYS_FSM_SUBSTATE_DUMMY, /*!< dummy state - always the first substate */
83  SYS_FSM_SUBSTATE_ENTRY, /*!< entry state - always the second substate */
94  SYS_FSM_SUBSTATE_RUNNING, /*!< fist running substate */
96 
97 /*================== Constant and Variable Definitions ======================*/
98 
99 /** States of the SYS state machine */
100 typedef enum SYS_STATEMACH {
101  /* Init-Sequence */
115  SYS_STATEMACH_ERROR, /*!< Error-State */
117 
118 /** Substates of the SYS state machine */
119 typedef enum SYS_STATEMACH_SUB {
120  SYS_ENTRY, /*!< Substate entry state */
121  SYS_CHECK_ERROR_FLAGS, /*!< Substate check if any error flag set */
122  SYS_CHECK_STATE_REQUESTS, /*!< Substate check if there is a state request */
123  SYS_WAIT_INITIALIZATION_SBC, /*!< Substate to wait for initialization of the sbc state machine */
124  SYS_WAIT_INITIALIZATION_INTERLOCK, /*!< Substate to wait for initialization of the interlock state machine */
125  SYS_WAIT_INITIALIZATION_CONT, /*!< Substate to wait for initialization of the contactor state machine */
126  SYS_WAIT_INITIALIZATION_BAL, /*!< Substate to wait for initialization of the balancing state machine */
127  SYS_WAIT_INITIALIZATION_BAL_GLOBAL_ENABLE, /*!< Substate to enable/disable balancing globally */
128  SYS_WAIT_INITIALIZATION_BMS, /*!< Substate to wait for initialization of the bms state machine */
129  SYS_WAIT_FIRST_MEASUREMENT_CYCLE, /*!< Substate to wait for first measurement cycle to complete */
130  SYS_WAIT_CURRENT_SENSOR_PRESENCE, /*!< Substate to wait for first measurement cycle to complete */
131  SYS_SBC_INIT_ERROR, /*!< Substate error of SBC initialization */
132  SYS_CONT_INIT_ERROR, /*!< Substate error of contactor state machine initialization */
133  SYS_BAL_INIT_ERROR, /*!< Substate error of balancing state machine initialization */
134  SYS_ILCK_INIT_ERROR, /*!< Substate error of contactor state machine initialization */
135  SYS_BMS_INIT_ERROR, /*!< Substate error of bms state machine initialization */
136  SYS_MEAS_INIT_ERROR, /*!< Substate error if first measurement cycle does not complete */
137  SYS_CURRENT_SENSOR_PRESENCE_ERROR, /*!< Substate error if first measurement cycle does not complete */
139 
140 /** State requests for the SYS statemachine */
141 typedef enum SYS_STATE_REQUEST {
142  SYS_STATE_INIT_REQUEST, /*!< initialization request */
143  SYS_STATE_ERROR_REQUEST, /*!< error state requested */
144  SYS_STATE_NO_REQUEST, /*!< no request */
146 
147 /** Possible return values when state requests are made to the SYS statemachine */
148 typedef enum SYS_RETURN_TYPE {
149  SYS_OK, /*!< sys --> ok */
150  SYS_BUSY_OK, /*!< sys busy --> ok */
151  SYS_REQUEST_PENDING, /*!< requested to be executed */
152  SYS_ILLEGAL_REQUEST, /*!< Request can not be executed */
153  SYS_ALREADY_INITIALIZED, /*!< Initialization of LTC already finished */
154  SYS_ILLEGAL_TASK_TYPE, /*!< Illegal */
156 
157 /**
158  * This structure contains all the variables relevant for the CONT state machine.
159  * The user can get the current state of the CONT state machine with this variable
160  */
161 typedef struct SYS_STATE {
162  uint16_t timer; /*!< time in ms before the state machine processes the next state, e.g. in counts of 1ms */
163  SYS_STATE_REQUEST_e stateRequest; /*!< current state request made to the state machine */
164  SYS_STATEMACH_e state; /*!< state of Driver State Machine */
165  SYS_STATEMACH_SUB_e substate; /*!< current substate of the state machine */
166  SYS_STATEMACH_e lastState; /*!< previous state of the state machine */
167  SYS_STATEMACH_SUB_e lastSubstate; /*!< previous substate of the state machine */
168  uint32_t illegalRequestsCounter; /*!< counts the number of illegal requests to the SYS state machine */
169  uint16_t initializationTimeout; /*!< Timeout to wait for initialization of state machine state machine */
170  uint8_t triggerEntry; /*!< counter for re-entrance protection (function running flag) */
172 
173 /*========== Extern Constant and Variable Declarations ======================*/
174 
175 extern SYS_STATE_s sys_state;
176 
177 /*========== Extern Function Prototypes =====================================*/
178 /**
179  * @brief sets the current state request of the state variable sys_state.
180  * @details This function is used to make a state request to the state machine,
181  * e.g., start voltage measurement, read result of voltage
182  * measurement, re-initialization. It calls #SYS_CheckStateRequest()
183  * to check if the request is valid. The state request is rejected if
184  * is not valid. The result of the check is returned immediately, so
185  * that the requester can act in case it made a non-valid state
186  * request.
187  * @param stateRequest state requested to set
188  * @return If the request was successfully set, it returns the SYS_OK, else
189  * the current state of requests (type #SYS_STATE_REQUEST_e)
190  */
192 
193 /**
194  * @brief tick function, call this to advance the state machine
195  * @details This function contains the sequence of events in the SYS state
196  * machine. It must be called time-triggered, every 10ms.
197  */
198 extern STD_RETURN_TYPE_e SYS_Trigger(SYS_STATE_s *pSystemState);
199 
200 /*========== Externalized Static Functions Prototypes (Unit Test) ===========*/
201 
202 #endif /* FOXBMS__SYS_H_ */
SYS_STATEMACH_ERROR
@ SYS_STATEMACH_ERROR
Definition: sys.h:115
SYS_FSM_SUBSTATE_INITIALIZATION_MISC
@ SYS_FSM_SUBSTATE_INITIALIZATION_MISC
Definition: sys.h:92
SYS_FSM_SUBSTATE_INITIALIZATION_FIRST_MEASUREMENT_CYCLE
@ SYS_FSM_SUBSTATE_INITIALIZATION_FIRST_MEASUREMENT_CYCLE
Definition: sys.h:90
SYS_STATEMACH_INITIALIZATION
@ SYS_STATEMACH_INITIALIZATION
Definition: sys.h:103
SYS_ILLEGAL_REQUEST
@ SYS_ILLEGAL_REQUEST
Definition: sys.h:152
SYS_WAIT_CURRENT_SENSOR_PRESENCE
@ SYS_WAIT_CURRENT_SENSOR_PRESENCE
Definition: sys.h:130
SYS_STATE_s
struct SYS_STATE SYS_STATE_s
SYS_STATE::lastState
SYS_STATEMACH_e lastState
Definition: sys.h:166
SYS_CHECK_BUSY
@ SYS_CHECK_BUSY
Definition: sys.h:66
sys_state
SYS_STATE_s sys_state
Definition: sys.c:89
SYS_CHECK_NOT_OK
@ SYS_CHECK_NOT_OK
Definition: sys.h:67
SYS_STATE::lastSubstate
SYS_STATEMACH_SUB_e lastSubstate
Definition: sys.h:167
SYS_FSM_SUBSTATE_RUNNING
@ SYS_FSM_SUBSTATE_RUNNING
Definition: sys.h:94
SYS_MEAS_INIT_ERROR
@ SYS_MEAS_INIT_ERROR
Definition: sys.h:136
SYS_STATE::timer
uint16_t timer
Definition: sys.h:162
SYS_STATE::triggerEntry
uint8_t triggerEntry
Definition: sys.h:170
SYS_Trigger
STD_RETURN_TYPE_e SYS_Trigger(SYS_STATE_s *pSystemState)
tick function, call this to advance the state machine
Definition: sys.c:599
SYS_FSM_STATE_ERROR
@ SYS_FSM_STATE_ERROR
Definition: sys.h:77
SYS_STATEMACH_INITIALIZE_MISC
@ SYS_STATEMACH_INITIALIZE_MISC
Definition: sys.h:112
SYS_ILLEGAL_TASK_TYPE
@ SYS_ILLEGAL_TASK_TYPE
Definition: sys.h:154
SYS_BMS_INIT_ERROR
@ SYS_BMS_INIT_ERROR
Definition: sys.h:135
SYS_FSM_SUBSTATES_e
enum SYS_FSM_SUBSTATES SYS_FSM_SUBSTATES_e
STD_RETURN_TYPE_e
enum STD_RETURN_TYPE STD_RETURN_TYPE_e
SYS_CHECK_e
enum SYS_CHECK SYS_CHECK_e
SYS_RETURN_TYPE
SYS_RETURN_TYPE
Definition: sys.h:148
SYS_WAIT_INITIALIZATION_BAL_GLOBAL_ENABLE
@ SYS_WAIT_INITIALIZATION_BAL_GLOBAL_ENABLE
Definition: sys.h:127
SYS_FSM_SUBSTATE_INITIALIZATION_INTERLOCK
@ SYS_FSM_SUBSTATE_INITIALIZATION_INTERLOCK
Definition: sys.h:86
SYS_FSM_SUBSTATE_INITIALIZATION_CONTACTORS
@ SYS_FSM_SUBSTATE_INITIALIZATION_CONTACTORS
Definition: sys.h:87
SYS_STATE_REQUEST_e
enum SYS_STATE_REQUEST SYS_STATE_REQUEST_e
SYS_FSM_SUBSTATE_INITIALIZATION_BALANCING
@ SYS_FSM_SUBSTATE_INITIALIZATION_BALANCING
Definition: sys.h:88
SYS_ILCK_INIT_ERROR
@ SYS_ILCK_INIT_ERROR
Definition: sys.h:134
SYS_FSM_SUBSTATE_INITIALIZATION_SBC
@ SYS_FSM_SUBSTATE_INITIALIZATION_SBC
Definition: sys.h:84
SYS_STATE::substate
SYS_STATEMACH_SUB_e substate
Definition: sys.h:165
SYS_WAIT_INITIALIZATION_CONT
@ SYS_WAIT_INITIALIZATION_CONT
Definition: sys.h:125
SYS_STATEMACH
SYS_STATEMACH
Definition: sys.h:100
SYS_STATE
Definition: sys.h:161
SYS_STATE_REQUEST
SYS_STATE_REQUEST
Definition: sys.h:141
SYS_STATEMACH_SUB_e
enum SYS_STATEMACH_SUB SYS_STATEMACH_SUB_e
SYS_CHECK_STATE_REQUESTS
@ SYS_CHECK_STATE_REQUESTS
Definition: sys.h:122
SYS_STATE::state
SYS_STATEMACH_e state
Definition: sys.h:164
SYS_STATEMACH_e
enum SYS_STATEMACH SYS_STATEMACH_e
SYS_STATEMACH_INITIALIZE_CONTACTORS
@ SYS_STATEMACH_INITIALIZE_CONTACTORS
Definition: sys.h:107
SYS_STATE::illegalRequestsCounter
uint32_t illegalRequestsCounter
Definition: sys.h:168
SYS_STATEMACH_INITIALIZE_BMS
@ SYS_STATEMACH_INITIALIZE_BMS
Definition: sys.h:109
SYS_STATEMACH_SUB
SYS_STATEMACH_SUB
Definition: sys.h:119
SYS_STATEMACH_CHECK_CURRENT_SENSOR_PRESENCE
@ SYS_STATEMACH_CHECK_CURRENT_SENSOR_PRESENCE
Definition: sys.h:113
SYS_OK
@ SYS_OK
Definition: sys.h:149
SYS_WAIT_FIRST_MEASUREMENT_CYCLE
@ SYS_WAIT_FIRST_MEASUREMENT_CYCLE
Definition: sys.h:129
sys_cfg.h
Sys driver configuration header.
SYS_FSM_SUBSTATE_INITIALIZATION_BMS
@ SYS_FSM_SUBSTATE_INITIALIZATION_BMS
Definition: sys.h:93
SYS_CONT_INIT_ERROR
@ SYS_CONT_INIT_ERROR
Definition: sys.h:132
SYS_STATE::initializationTimeout
uint16_t initializationTimeout
Definition: sys.h:169
SYS_FSM_STATES_e
enum SYS_FSM_STATES SYS_FSM_STATES_e
SYS_STATE::stateRequest
SYS_STATE_REQUEST_e stateRequest
Definition: sys.h:163
SYS_CHECK
SYS_CHECK
Definition: sys.h:64
SYS_STATEMACH_UNINITIALIZED
@ SYS_STATEMACH_UNINITIALIZED
Definition: sys.h:102
SYS_STATE_ERROR_REQUEST
@ SYS_STATE_ERROR_REQUEST
Definition: sys.h:143
SYS_FSM_STATE_DUMMY
@ SYS_FSM_STATE_DUMMY
Definition: sys.h:72
SYS_STATEMACH_FIRST_MEASUREMENT_CYCLE
@ SYS_STATEMACH_FIRST_MEASUREMENT_CYCLE
Definition: sys.h:111
SYS_ALREADY_INITIALIZED
@ SYS_ALREADY_INITIALIZED
Definition: sys.h:153
SYS_STATE_INIT_REQUEST
@ SYS_STATE_INIT_REQUEST
Definition: sys.h:142
SYS_FSM_SUBSTATE_INITIALIZATION_CHECK_CURRENT_SENSOR
@ SYS_FSM_SUBSTATE_INITIALIZATION_CHECK_CURRENT_SENSOR
Definition: sys.h:91
SYS_RETURN_TYPE_e
enum SYS_RETURN_TYPE SYS_RETURN_TYPE_e
SYS_FSM_SUBSTATE_INITIALIZATION_BOOT_MESSAGE
@ SYS_FSM_SUBSTATE_INITIALIZATION_BOOT_MESSAGE
Definition: sys.h:85
SYS_WAIT_INITIALIZATION_BMS
@ SYS_WAIT_INITIALIZATION_BMS
Definition: sys.h:128
SYS_WAIT_INITIALIZATION_BAL
@ SYS_WAIT_INITIALIZATION_BAL
Definition: sys.h:126
SYS_STATEMACH_INITIALIZE_BALANCING
@ SYS_STATEMACH_INITIALIZE_BALANCING
Definition: sys.h:108
SYS_BAL_INIT_ERROR
@ SYS_BAL_INIT_ERROR
Definition: sys.h:133
SYS_FSM_STATE_RUNNING
@ SYS_FSM_STATE_RUNNING
Definition: sys.h:76
SYS_CURRENT_SENSOR_PRESENCE_ERROR
@ SYS_CURRENT_SENSOR_PRESENCE_ERROR
Definition: sys.h:137
SYS_STATEMACH_INITIALIZE_SBC
@ SYS_STATEMACH_INITIALIZE_SBC
Definition: sys.h:105
SYS_BUSY_OK
@ SYS_BUSY_OK
Definition: sys.h:150
SYS_REQUEST_PENDING
@ SYS_REQUEST_PENDING
Definition: sys.h:151
SYS_SetStateRequest
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:550
SYS_STATE_NO_REQUEST
@ SYS_STATE_NO_REQUEST
Definition: sys.h:144
SYS_FSM_STATE_INITIALIZATION
@ SYS_FSM_STATE_INITIALIZATION
Definition: sys.h:75
SYS_STATEMACH_INITIALIZE_INTERLOCK
@ SYS_STATEMACH_INITIALIZE_INTERLOCK
Definition: sys.h:106
SYS_STATEMACH_RUNNING
@ SYS_STATEMACH_RUNNING
Definition: sys.h:110
SYS_FSM_STATE_UNINITIALIZED
@ SYS_FSM_STATE_UNINITIALIZED
Definition: sys.h:74
SYS_STATEMACH_INITIALIZED
@ SYS_STATEMACH_INITIALIZED
Definition: sys.h:104
SYS_WAIT_INITIALIZATION_SBC
@ SYS_WAIT_INITIALIZATION_SBC
Definition: sys.h:123
SYS_ENTRY
@ SYS_ENTRY
Definition: sys.h:120
SYS_STATEMACH_INITIALIZE_ISOGUARD
@ SYS_STATEMACH_INITIALIZE_ISOGUARD
Definition: sys.h:114
SYS_FSM_SUBSTATE_DUMMY
@ SYS_FSM_SUBSTATE_DUMMY
Definition: sys.h:82
SYS_FSM_SUBSTATE_INITIALIZATION_INSULATION_GUARD
@ SYS_FSM_SUBSTATE_INITIALIZATION_INSULATION_GUARD
Definition: sys.h:89
SYS_WAIT_INITIALIZATION_INTERLOCK
@ SYS_WAIT_INITIALIZATION_INTERLOCK
Definition: sys.h:124
SYS_FSM_SUBSTATES
SYS_FSM_SUBSTATES
Definition: sys.h:81
SYS_SBC_INIT_ERROR
@ SYS_SBC_INIT_ERROR
Definition: sys.h:131
SYS_CHECK_OK
@ SYS_CHECK_OK
Definition: sys.h:65
SYS_FSM_STATE_HAS_NEVER_RUN
@ SYS_FSM_STATE_HAS_NEVER_RUN
Definition: sys.h:73
SYS_FSM_STATES
SYS_FSM_STATES
Definition: sys.h:71
SYS_FSM_SUBSTATE_ENTRY
@ SYS_FSM_SUBSTATE_ENTRY
Definition: sys.h:83
SYS_CHECK_ERROR_FLAGS
@ SYS_CHECK_ERROR_FLAGS
Definition: sys.h:121