foxBMS  1.6.0
The foxBMS Battery Management System API Documentation
bal.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 bal.c
44  * @author foxBMS Team
45  * @date 2020-02-24 (date of creation)
46  * @updated 2023-10-12 (date of last update)
47  * @version v1.6.0
48  * @ingroup APPLICATION
49  * @prefix BAL
50  *
51  * @brief Driver for the Balancing module
52  *
53  */
54 
55 /*========== Includes =======================================================*/
56 #include "bal.h"
57 
58 #include <stdbool.h>
59 #include <stdint.h>
60 
61 /*========== Macros and Definitions =========================================*/
62 
63 /*========== Static Constant and Variable Definitions =======================*/
64 
65 /*========== Extern Constant and Variable Definitions =======================*/
66 
67 /*========== Static Function Prototypes =====================================*/
68 
69 /*========== Static Function Implementations ================================*/
70 
71 /*========== Extern Function Implementations ================================*/
72 #pragma WEAK(BAL_SaveLastStates)
73 extern void BAL_SaveLastStates(BAL_STATE_s *pBalancingState) {
74  FAS_ASSERT(pBalancingState != NULL_PTR);
75  if (pBalancingState->lastState != pBalancingState->state) {
76  pBalancingState->lastState = pBalancingState->state;
77  pBalancingState->lastSubstate = pBalancingState->substate;
78  } else if (pBalancingState->lastSubstate != pBalancingState->substate) {
79  pBalancingState->lastSubstate = pBalancingState->substate;
80  } else {
81  /* Do not set new substate as nothing changed */
82  ;
83  }
84 }
85 
86 #pragma WEAK(BAL_CheckReEntrance)
87 extern uint8_t BAL_CheckReEntrance(BAL_STATE_s *currentState) {
88  FAS_ASSERT(currentState != NULL_PTR);
89  uint8_t retval = 0;
90 
92  if (!currentState->triggerEntry) {
93  currentState->triggerEntry++;
94  } else {
95  retval = 0xFF; /* multiple calls of function */
96  }
98 
99  return retval;
100 }
101 
102 #pragma WEAK(BAL_TransferStateRequest)
104  FAS_ASSERT(currentState != NULL_PTR);
106 
108  retval = currentState->stateRequest;
109  currentState->stateRequest = BAL_STATE_NO_REQUEST;
111 
112  return retval;
113 }
114 
115 #pragma WEAK(BAL_CheckStateRequest)
117  FAS_ASSERT(pCurrentState != NULL_PTR);
118  if (stateRequest == BAL_STATE_ERROR_REQUEST) {
119  return BAL_OK;
120  }
121  if (stateRequest == BAL_STATE_GLOBAL_ENABLE_REQUEST) {
122  pCurrentState->balancingGlobalAllowed = true;
123  return BAL_OK;
124  }
125  if (stateRequest == BAL_STATE_GLOBAL_DISABLE_REQUEST) {
126  pCurrentState->balancingGlobalAllowed = false;
127  return BAL_OK;
128  }
129  if ((stateRequest == BAL_STATE_NO_BALANCING_REQUEST) || (stateRequest == BAL_STATE_ALLOWBALANCING_REQUEST)) {
130  return BAL_OK;
131  }
132 
133  if (pCurrentState->stateRequest == BAL_STATE_NO_REQUEST) {
134  /* init only allowed from the uninitialized state */
135  if (stateRequest == BAL_STATE_INIT_REQUEST) {
136  if (pCurrentState->state == BAL_STATEMACH_UNINITIALIZED) {
137  return BAL_OK;
138  } else {
140  }
141  /* request to forbid balancing */
142  } else {
143  return BAL_ILLEGAL_REQUEST;
144  }
145  } else {
146  return BAL_REQUEST_PENDING;
147  }
148 }
149 
150 #pragma WEAK(BAL_Init)
152  FAS_ASSERT(pControl != NULL_PTR);
153  DATA_READ_DATA(pControl);
154  pControl->enableBalancing = 0;
155  DATA_WRITE_DATA(pControl);
156  return STD_OK;
157 }
158 
159 #pragma WEAK(BAL_ProcessStateUninitalized)
160 extern void BAL_ProcessStateUninitalized(BAL_STATE_s *pCurrentState, BAL_STATE_REQUEST_e stateRequest) {
161  FAS_ASSERT(pCurrentState != NULL_PTR);
162  if (stateRequest == BAL_STATE_INIT_REQUEST) {
163  pCurrentState->timer = BAL_STATEMACH_SHORTTIME_100ms;
164  pCurrentState->state = BAL_STATEMACH_INITIALIZATION;
165  pCurrentState->substate = BAL_ENTRY;
166  } else if (stateRequest == BAL_STATE_NO_REQUEST) {
167  /* no actual request pending */
168  } else {
169  pCurrentState->errorRequestCounter++; /* illegal request pending */
170  }
171 }
172 
173 #pragma WEAK(BAL_ProcessStateInitialization)
174 extern void BAL_ProcessStateInitialization(BAL_STATE_s *currentState) {
175  FAS_ASSERT(currentState != NULL_PTR);
176  currentState->timer = BAL_STATEMACH_SHORTTIME_100ms;
177  currentState->state = BAL_STATEMACH_INITIALIZED;
178  currentState->substate = BAL_ENTRY;
179 }
180 
181 #pragma WEAK(BAL_ProcessStateInitialized)
182 extern void BAL_ProcessStateInitialized(BAL_STATE_s *currentState) {
183  FAS_ASSERT(currentState != NULL_PTR);
184  currentState->initializationFinished = STD_OK;
185  currentState->timer = BAL_STATEMACH_SHORTTIME_100ms;
186  currentState->state = BAL_STATEMACH_CHECK_BALANCING;
187  currentState->substate = BAL_ENTRY;
188 }
189 
190 /*========== Externalized Static Function Implementations (Unit Test) =======*/
191 
192 /*========== Getter for static Variables (Unit Test) ========================*/
193 #ifdef UNITY_UNIT_TEST
194 #endif
STD_RETURN_TYPE_e BAL_Init(DATA_BLOCK_BALANCING_CONTROL_s *pControl)
Generic initialization function for the balancing module.
Definition: bal.c:151
void BAL_SaveLastStates(BAL_STATE_s *pBalancingState)
Saves the last state and the last substate.
Definition: bal.c:73
void BAL_ProcessStateInitialization(BAL_STATE_s *currentState)
State machine subfunction to initialize the balancing state machine.
Definition: bal.c:174
void BAL_ProcessStateUninitalized(BAL_STATE_s *pCurrentState, BAL_STATE_REQUEST_e stateRequest)
Substate handling function for BAL_Trigger()
Definition: bal.c:160
BAL_STATE_REQUEST_e BAL_TransferStateRequest(BAL_STATE_s *currentState)
transfers the current state request to the state machine.
Definition: bal.c:103
void BAL_ProcessStateInitialized(BAL_STATE_s *currentState)
State machine subfunction to transfer from an initalized state to "running" states of th state machin...
Definition: bal.c:182
uint8_t BAL_CheckReEntrance(BAL_STATE_s *currentState)
re-entrance check of BAL state machine trigger function
Definition: bal.c:87
BAL_RETURN_TYPE_e BAL_CheckStateRequest(BAL_STATE_s *pCurrentState, BAL_STATE_REQUEST_e stateRequest)
checks the state requests that are made.
Definition: bal.c:116
Header for the driver for balancing.
BAL_RETURN_TYPE_e
Definition: bal.h:115
@ BAL_ALREADY_INITIALIZED
Definition: bal.h:123
@ BAL_OK
Definition: bal.h:116
@ BAL_ILLEGAL_REQUEST
Definition: bal.h:119
@ BAL_REQUEST_PENDING
Definition: bal.h:118
BAL_STATE_REQUEST_e
Definition: bal.h:102
@ BAL_STATE_GLOBAL_ENABLE_REQUEST
Definition: bal.h:108
@ BAL_STATE_ERROR_REQUEST
Definition: bal.h:104
@ BAL_STATE_NO_REQUEST
Definition: bal.h:109
@ BAL_STATE_ALLOWBALANCING_REQUEST
Definition: bal.h:106
@ BAL_STATE_NO_BALANCING_REQUEST
Definition: bal.h:105
@ BAL_STATE_INIT_REQUEST
Definition: bal.h:103
@ BAL_STATE_GLOBAL_DISABLE_REQUEST
Definition: bal.h:107
@ BAL_STATEMACH_UNINITIALIZED
Definition: bal.h:73
@ BAL_STATEMACH_INITIALIZATION
Definition: bal.h:74
@ BAL_STATEMACH_INITIALIZED
Definition: bal.h:75
@ BAL_STATEMACH_CHECK_BALANCING
Definition: bal.h:76
@ BAL_ENTRY
Definition: bal.h:91
#define BAL_STATEMACH_SHORTTIME_100ms
Definition: bal_cfg.h:65
#define DATA_READ_DATA(...)
Definition: database.h:86
#define DATA_WRITE_DATA(...)
Definition: database.h:96
#define FAS_ASSERT(x)
Assertion macro that asserts that x is true.
Definition: fassert.h:255
STD_RETURN_TYPE_e
Definition: fstd_types.h:82
@ STD_OK
Definition: fstd_types.h:83
#define NULL_PTR
Null pointer.
Definition: fstd_types.h:77
void OS_ExitTaskCritical(void)
Exit Critical interface function for use in FreeRTOS-Tasks and FreeRTOS-ISR.
Definition: os_freertos.c:154
void OS_EnterTaskCritical(void)
Enter Critical interface function for use in FreeRTOS-Tasks and FreeRTOS-ISR.
Definition: os_freertos.c:150
uint32_t errorRequestCounter
Definition: bal.h:139
BAL_STATE_REQUEST_e stateRequest
Definition: bal.h:133
STD_RETURN_TYPE_e initializationFinished
Definition: bal.h:140
uint8_t lastSubstate
Definition: bal.h:137
uint8_t triggerEntry
Definition: bal.h:138
BAL_STATEMACH_e lastState
Definition: bal.h:136
bool balancingGlobalAllowed
Definition: bal.h:144
BAL_STATEMACH_SUB_e substate
Definition: bal.h:135
uint16_t timer
Definition: bal.h:132
BAL_STATEMACH_e state
Definition: bal.h:134