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