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