foxBMS  1.4.1
The foxBMS Battery Management System API Documentation
sbc.h
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 sbc.h
44  * @author foxBMS Team
45  * @date 2020-07-14 (date of creation)
46  * @updated 2022-10-27 (date of last update)
47  * @version v1.4.1
48  * @ingroup DRIVERS
49  * @prefix SBC
50  *
51  * @brief Header for the driver for the SBC module
52  *
53  * @details It must always be used when creating new c header files.
54  *
55  */
56 
57 #ifndef FOXBMS__SBC_H_
58 #define FOXBMS__SBC_H_
59 
60 /*========== Includes =======================================================*/
61 #include "general.h"
62 
63 #include "ftask_cfg.h"
64 
65 #include "nxpfs85xx.h"
66 
67 /*========== Macros and Definitions =========================================*/
68 
69 /**
70  * This define MUST represent the cycle time of the task in which context the
71  * functions run, e.g., if the #SBC_Trigger() is running in the 10 ms task
72  * then the define must be set to 10.
73  *
74  * This sets the minimum time between two subsequent executed states/substates.
75  *
76  * Define is only used for compile-time assertion, it has no programmatic
77  * influence on the actual code.
78  */
79 #define SBC_STATEMACHINE_TASK_CYCLE_CONTEXT_MS (10u)
80 
81 #if SBC_STATEMACHINE_TASK_CYCLE_CONTEXT_MS != FTSK_TASK_CYCLIC_10MS_CYCLE_TIME
82 #error "Invalid SBC configuration. Make sure that SBC timing is configured correctly!"
83 #endif
84 
85 /**
86  * SBC state machine short time definition in #SBC_Trigger() calls until next
87  * state/substate is processed
88  */
89 #define SBC_STATEMACHINE_SHORTTIME (1u)
90 
91 /**
92  * SBC state machine medium time definition in #SBC_Trigger() calls until next
93  * state/substate is processed
94  */
95 #define SBC_STATEMACHINE_MEDIUMTIME (5u)
96 
97 /**
98  * SBC state machine long time definition in #SBC_Trigger() calls until next
99  * state/substate is processed
100  */
101 #define SBC_STATEMACHINE_LONGTIME (10u)
102 
103 /**
104  * trigger period of SBC watchdog
105  */
106 #define SBC_WINDOW_WATCHDOG_PERIOD_MS (100u)
107 
108 /** State requests for the SYS state machine */
109 typedef enum {
110  SBC_STATE_INIT_REQUEST, /*!< request to begin SBC initialization */
111  SBC_STATE_ERROR_REQUEST, /*!< request to switch SBC into error state */
112  SBC_STATE_NO_REQUEST, /*!< enum to clarify that currently no new request needs to be processed */
114 
115 /** State of watchdog state */
116 typedef enum {
120 
121 /** Possible return values when state requests are made to the SYS state machine */
122 typedef enum {
123  SBC_OK, /*!< sys --> ok */
124  SBC_BUSY_OK, /*!< sys busy --> ok */
125  SBC_REQUEST_PENDING, /*!< requested to be executed */
126  SBC_ILLEGAL_REQUEST, /*!< Request can not be executed */
127  SBC_ALREADY_INITIALIZED, /*!< Initialization of SBC already finished */
128  SBC_ILLEGAL_TASK_TYPE, /*!< Illegal */
130 
131 /** States of the SBC state machine */
132 typedef enum {
133  /* Init-Sequence */
134  SBC_STATEMACHINE_UNINITIALIZED, /*!< state machine start value */
135  SBC_STATEMACHINE_INITIALIZATION, /*!< SBC startup initialization process */
136  SBC_STATEMACHINE_RUNNING, /*!< error detected communicating with SBC */
137  SBC_STATEMACHINE_ERROR, /*!< default initialization value */
140 
141 /** Substates of the SBC state machine */
142 typedef enum {
143  SBC_ENTRY, /*!< Substate entry state */
144  SBC_INIT_RESET_FAULT_ERROR_COUNTER_PART1, /*!< Substate during initialization to reset fault-error counter */
145  SBC_INIT_RESET_FAULT_ERROR_COUNTER_PART2, /*!< Substate during initialization to reset fault-error counter */
146  SBC_INITIALIZE_SAFETY_PATH_CHECK, /*!< Substate to initialize SBC */
147  SBC_INITIALIZE_VOLTAGE_SUPERVISOR_PART3, /*!< Substate to initialize SBC */
148  SBC_INITIALIZE_VOLTAGE_SUPERVISOR_PART4, /*!< Substate to initialize SBC */
150 
151 /**
152  * This structure contains all the variables relevant for the SBC state machine.
153  * The user can get the current state of the SBC state machine with this variable
154  */
155 typedef struct {
156  uint16_t timer; /*!< time in ms before the state machine processes the next state, in counts of 10ms */
157  uint16_t watchdogTrigger; /*!< time in ms before the state machine triggers watchdog, in counts of 10ms */
158  SBC_STATE_REQUEST_e stateRequest; /*!< current state request made to the state machine */
159  SBC_STATEMACHINE_e state; /*!< state of Driver State Machine */
160  SBC_STATEMACHINE_SUB_e substate; /*!< current substate of the state machine */
161  SBC_STATEMACHINE_e lastState; /*!< previous state of the state machine */
162  SBC_STATEMACHINE_SUB_e lastSubstate; /*!< previous substate of the state machine */
163  uint32_t illegalRequestsCounter; /*!< counts the number of illegal requests to the SBC state machine */
164  uint8_t retryCounter; /*!< counter to retry subsystem initialization if fails */
165  uint8_t requestWatchdogTrigger; /*!< required watchdog triggers during init to correctly initialize SBC */
166  uint8_t triggerEntry; /*!< counter for re-entrance protection (function running flag) */
167  SBC_PERIODIC_WATCHDOG_STATE_e watchdogState; /*!< state if periodic watchdog trigger is required or not */
168  FS85_STATE_s *pFs85xxInstance; /*!< pointer to FS85xx instance */
169  uint16_t watchdogPeriod_10ms; /*!< watchdog trigger frequency in 10ms */
170 } SBC_STATE_s;
171 
172 /*========== Extern Constant and Variable Declarations ======================*/
174 
175 /*========== Extern Function Prototypes =====================================*/
176 /**
177  * @brief sets the current state request of passed state variable
178  * @details This function is used to make a state request to the state machine,
179  * e.g., start initialization. It calls #SBC_CheckStateRequest()
180  * to check if the request is valid. The state request is rejected if
181  * is not valid. The result of the check is returned immediately, so
182  * that the requester can act in case it made a non-valid state
183  * request.
184  *
185  * @param[in,out] pInstance SBC instance where different state is requested
186  * @param[in] stateRequest requested state
187  * @return If the request was successfully set, it returns the #SBC_OK, else
188  * the current state of requests (type #SBC_STATE_REQUEST_e)
189  */
191 
192 /**
193  * @brief gets the current state of passed state variable
194  * @details This function is used in the functioning of the SBC state machine.
195  *
196  * @param[in] pInstance SBC instance where the current state is requested
197  *
198  * @return current state, taken from #SBC_STATEMACHINE_e
199  */
200 extern SBC_STATEMACHINE_e SBC_GetState(SBC_STATE_s *pInstance);
201 
202 /**
203  * @brief trigger function for the SYS driver state machine.
204  * @details This function contains the sequence of events in the SYS state
205  * machine. It must be called time-triggered, every 10ms.
206  *
207  * @param[in,out] pInstance SBC instance that is controlled
208  */
209 extern void SBC_Trigger(SBC_STATE_s *pInstance);
210 
211 /*========== Externalized Static Functions Prototypes (Unit Test) ===========*/
212 #ifdef UNITY_UNIT_TEST
213 extern bool TEST_SBC_TriggerWatchdogIfRequired(SBC_STATE_s *pInstance);
214 #endif
215 
216 #endif /* FOXBMS__SBC_H_ */
Task configuration header.
General macros and definitions for the whole platform.
Header for the driver for the FRAM module.
SBC_STATEMACHINE_e SBC_GetState(SBC_STATE_s *pInstance)
gets the current state of passed state variable
Definition: sbc.c:252
void SBC_Trigger(SBC_STATE_s *pInstance)
trigger function for the SYS driver state machine.
Definition: sbc.c:258
SBC_STATEMACHINE_e
Definition: sbc.h:132
@ SBC_STATEMACHINE_RUNNING
Definition: sbc.h:136
@ SBC_STATEMACHINE_UNINITIALIZED
Definition: sbc.h:134
@ SBC_STATEMACHINE_INITIALIZATION
Definition: sbc.h:135
@ SBC_STATEMACHINE_UNDEFINED
Definition: sbc.h:138
@ SBC_STATEMACHINE_ERROR
Definition: sbc.h:137
SBC_RETURN_TYPE_e
Definition: sbc.h:122
@ SBC_BUSY_OK
Definition: sbc.h:124
@ SBC_ILLEGAL_TASK_TYPE
Definition: sbc.h:128
@ SBC_ILLEGAL_REQUEST
Definition: sbc.h:126
@ SBC_REQUEST_PENDING
Definition: sbc.h:125
@ SBC_ALREADY_INITIALIZED
Definition: sbc.h:127
@ SBC_OK
Definition: sbc.h:123
SBC_STATE_REQUEST_e
Definition: sbc.h:109
@ SBC_STATE_INIT_REQUEST
Definition: sbc.h:110
@ SBC_STATE_ERROR_REQUEST
Definition: sbc.h:111
@ SBC_STATE_NO_REQUEST
Definition: sbc.h:112
SBC_PERIODIC_WATCHDOG_STATE_e
Definition: sbc.h:116
@ SBC_PERIODIC_WATCHDOG_DEACTIVATED
Definition: sbc.h:118
@ SBC_PERIODIC_WATCHDOG_ACTIVATED
Definition: sbc.h:117
SBC_STATE_s sbc_stateMcuSupervisor
Definition: sbc.c:76
SBC_STATEMACHINE_SUB_e
Definition: sbc.h:142
@ SBC_ENTRY
Definition: sbc.h:143
@ SBC_INITIALIZE_VOLTAGE_SUPERVISOR_PART3
Definition: sbc.h:147
@ SBC_INIT_RESET_FAULT_ERROR_COUNTER_PART2
Definition: sbc.h:145
@ SBC_INITIALIZE_SAFETY_PATH_CHECK
Definition: sbc.h:146
@ SBC_INITIALIZE_VOLTAGE_SUPERVISOR_PART4
Definition: sbc.h:148
@ SBC_INIT_RESET_FAULT_ERROR_COUNTER_PART1
Definition: sbc.h:144
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
SBC_STATEMACHINE_SUB_e lastSubstate
Definition: sbc.h:162
FS85_STATE_s * pFs85xxInstance
Definition: sbc.h:168
uint16_t timer
Definition: sbc.h:156
uint8_t triggerEntry
Definition: sbc.h:166
uint16_t watchdogTrigger
Definition: sbc.h:157
SBC_STATE_REQUEST_e stateRequest
Definition: sbc.h:158
SBC_STATEMACHINE_e lastState
Definition: sbc.h:161
SBC_STATEMACHINE_e state
Definition: sbc.h:159
uint8_t requestWatchdogTrigger
Definition: sbc.h:165
SBC_PERIODIC_WATCHDOG_STATE_e watchdogState
Definition: sbc.h:167
uint32_t illegalRequestsCounter
Definition: sbc.h:163
SBC_STATEMACHINE_SUB_e substate
Definition: sbc.h:160
uint16_t watchdogPeriod_10ms
Definition: sbc.h:169
uint8_t retryCounter
Definition: sbc.h:164