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