foxBMS  1.6.0
The foxBMS Battery Management System API Documentation
can_cbs_rx_bms-state-request.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 can_cbs_rx_bms-state-request.c
44  * @author foxBMS Team
45  * @date 2021-07-28 (date of creation)
46  * @updated 2023-10-12 (date of last update)
47  * @version v1.6.0
48  * @ingroup DRIVER
49  * @prefix CANRX
50  *
51  * @brief CAN driver Rx callback implementation
52  * @details CAN Rx callback for command message
53  */
54 
55 /*========== Includes =======================================================*/
56 #include "bms_cfg.h"
57 
58 #include "bal.h"
59 #include "can_cbs_rx.h"
61 #include "can_helper.h"
62 #include "diag.h"
63 #include "os.h"
64 #include "sys_mon.h"
65 
66 #include <stdint.h>
67 
68 /*========== Macros and Definitions =========================================*/
69 /**
70  * @brief CAN state request update time
71  * @details When a new CAN state request is received, it leads to an update
72  * of #DATA_BLOCK_STATE_REQUEST_s::stateRequestViaCan if one of the
73  * following conditions is met:
74  *
75  * - The new request is different than the old request.
76  * - The old request is older than the timespan set in this define.
77  */
78 #define CANRX_CAN_REQUEST_UPDATE_TIME_ms (3000u)
79 
80 /*========== Static Constant and Variable Definitions =======================*/
81 
82 /*========== Extern Constant and Variable Definitions =======================*/
83 
84 /*========== Static Function Prototypes =====================================*/
85 /**
86  * @brief clears the persistent flags
87  * @details This function clears all persistent flags (if signalData demands it)
88  * which are:
89  * - deep-discharge flag
90  * - sys mon violation flags
91  * @param[in] signalData if it is 1u, flags are cleared
92  */
93 static void CANRX_ClearAllPersistentFlags(uint64_t signalData);
94 
95 /**
96  * @brief handles the mode request
97  * @param[in] signalData extracted signal data
98  * @param[in,out] kpkCanShim can shim with database entries
99  */
100 static void CANRX_HandleModeRequest(uint64_t signalData, const CAN_SHIM_s *const kpkCanShim);
101 
102 /**
103  * @brief handles the balancing request
104  * @param[in] signalData extracted signal data
105  */
106 static void CANRX_HandleBalancingRequest(uint64_t signalData);
107 
108 /**
109  * @brief sets the balancing threshold
110  * @param[in] signalData extracted signal data
111  */
112 static void CANRX_SetBalancingThreshold(uint64_t signalData);
113 
114 /*========== Static Function Implementations ================================*/
115 static void CANRX_ClearAllPersistentFlags(uint64_t signalData) {
116  /* AXIVION Routine Generic-MissingParameterAssert: signalData: parameter accepts whole range */
117  if (signalData == 1u) {
118  /* clear deep discharge */
119  for (uint8_t s = 0u; s < BS_NR_OF_STRINGS; s++) {
121  }
122  /* clear sys mon */
124  }
125 }
126 
127 static void CANRX_HandleModeRequest(uint64_t signalData, const CAN_SHIM_s *const kpkCanShim) {
128  FAS_ASSERT(kpkCanShim != NULL_PTR);
129  /* AXIVION Routine Generic-MissingParameterAssert: signalData: parameter accepts whole range */
130 
131  /** 0x00: Disconnect strings from HV bus
132  * 0x01: Connect strings to HV bus to start discharge
133  * 0x02: Connect strings to HV bus to start charging
134  */
135  uint8_t stateRequest = BMS_REQ_ID_NOREQ;
136 
137  switch (signalData) {
138  case 0u:
139  stateRequest = BMS_REQ_ID_STANDBY;
140  break;
141  case 1u:
142  stateRequest = BMS_REQ_ID_NORMAL;
143  break;
144  case 2u:
145  stateRequest = BMS_REQ_ID_CHARGE;
146  break;
147  default:
148  /* default value already set in initialization */
149  break;
150  }
152  kpkCanShim->pTableStateRequest->stateRequestViaCan = stateRequest;
153  if ((kpkCanShim->pTableStateRequest->stateRequestViaCan !=
156  kpkCanShim->pTableStateRequest->stateRequestViaCanPending = stateRequest;
157  }
158  if (kpkCanShim->pTableStateRequest->stateCounter == (uint8_t)UINT8_MAX) {
159  /* overflow of state counter */
160  kpkCanShim->pTableStateRequest->stateCounter = 0u;
161  } else {
162  kpkCanShim->pTableStateRequest->stateCounter++;
163  }
164 }
165 
166 static void CANRX_HandleBalancingRequest(uint64_t signalData) {
167  /* AXIVION Routine Generic-MissingParameterAssert: signalData: parameter accepts whole range */
168  /* AXIVION Next Codeline Style MisraC2012-2.2 MisraC2012-14.3: Depending on implementation STD_NOT_OK might be returned. */
170  if (signalData == 0u) {
172  } else {
174  }
175  }
176 }
177 
178 static void CANRX_SetBalancingThreshold(uint64_t signalData) {
179  /* cap signal data to UINT16_MAX */
180  int32_t cappedSignalData = (int32_t)signalData;
181  if (signalData > (uint64_t)UINT16_MAX) {
182  cappedSignalData = (uint64_t)UINT16_MAX;
183  }
184  BAL_SetBalancingThreshold(cappedSignalData);
185 }
186 
187 /*========== Extern Function Implementations ================================*/
188 extern uint32_t CANRX_BmsStateRequest(
189  CAN_MESSAGE_PROPERTIES_s message,
190  const uint8_t *const kpkCanData,
191  const CAN_SHIM_s *const kpkCanShim) {
195  FAS_ASSERT(kpkCanData != NULL_PTR);
196  FAS_ASSERT(kpkCanShim != NULL_PTR);
197 
198  DATA_READ_DATA(kpkCanShim->pTableStateRequest);
199 
200  uint64_t messageData = 0u;
201  CAN_RxGetMessageDataFromCanData(&messageData, kpkCanData, message.endianness);
202 
203  uint64_t signalData = 0;
204  /* Get mode request */
205  /* AXIVION Next Codeline Style Generic-NoMagicNumbers: Signal data defined in .dbc file. */
206  CAN_RxGetSignalDataFromMessageData(messageData, 1u, 2u, &signalData, message.endianness);
207  CANRX_HandleModeRequest(signalData, kpkCanShim);
208 
209  /* check for reset flag */
210  /* AXIVION Next Codeline Style Generic-NoMagicNumbers: Signal data defined in .dbc file. */
211  CAN_RxGetSignalDataFromMessageData(messageData, 2u, 1u, &signalData, message.endianness);
212  CANRX_ClearAllPersistentFlags(signalData);
213 
214  /* Get balancing request */
215  /* AXIVION Next Codeline Style Generic-NoMagicNumbers: Signal data defined in .dbc file. */
216  CAN_RxGetSignalDataFromMessageData(messageData, 8u, 1u, &signalData, message.endianness);
217  CANRX_HandleBalancingRequest(signalData);
218 
219  /* Get balancing threshold */
220  /* AXIVION Next Codeline Style Generic-NoMagicNumbers: Signal data defined in .dbc file. */
221  CAN_RxGetSignalDataFromMessageData(messageData, 23u, 8u, &signalData, message.endianness);
222  CANRX_SetBalancingThreshold(signalData);
223 
224  /* TODO: Implement missing signals */
225 
226  DATA_WRITE_DATA(kpkCanShim->pTableStateRequest);
227 
228  return 0;
229 }
230 
231 /*========== Externalized Static Function Implementations (Unit Test) =======*/
232 #ifdef UNITY_UNIT_TEST
233 #endif
Header for the driver for balancing.
STD_RETURN_TYPE_e BAL_GetInitializationState(void)
gets the initialization state.
@ BAL_STATE_GLOBAL_ENABLE_REQUEST
Definition: bal.h:108
@ BAL_STATE_GLOBAL_DISABLE_REQUEST
Definition: bal.h:107
BAL_RETURN_TYPE_e BAL_SetStateRequest(BAL_STATE_REQUEST_e stateRequest)
sets the current state request of the state variable bal_state.
void BAL_SetBalancingThreshold(int32_t threshold_mV)
set balancing threshold
Definition: bal_cfg.c:75
#define BS_NR_OF_STRINGS
Number of parallel strings in the battery pack.
bms driver configuration header
#define BMS_REQ_ID_STANDBY
Definition: bms_cfg.h:69
#define BMS_REQ_ID_NOREQ
Definition: bms_cfg.h:66
#define BMS_REQ_ID_CHARGE
Definition: bms_cfg.h:75
#define BMS_REQ_ID_NORMAL
Definition: bms_cfg.h:72
CAN callbacks header.
static void CANRX_ClearAllPersistentFlags(uint64_t signalData)
clears the persistent flags
#define CANRX_CAN_REQUEST_UPDATE_TIME_ms
CAN state request update time.
static void CANRX_SetBalancingThreshold(uint64_t signalData)
sets the balancing threshold
uint32_t CANRX_BmsStateRequest(CAN_MESSAGE_PROPERTIES_s message, const uint8_t *const kpkCanData, const CAN_SHIM_s *const kpkCanShim)
can rx callback function for state requests
static void CANRX_HandleBalancingRequest(uint64_t signalData)
handles the balancing request
static void CANRX_HandleModeRequest(uint64_t signalData, const CAN_SHIM_s *const kpkCanShim)
handles the mode request
#define CAN_FOXBMS_MESSAGES_DEFAULT_DLC
Definition: can_cfg.h:106
Header for the driver for the CAN module.
#define CANRX_BMS_STATE_REQUEST_ID
#define CANRX_BMS_STATE_REQUEST_ID_TYPE
void CAN_RxGetMessageDataFromCanData(uint64_t *pMessage, const uint8_t *const kpkCanData, CAN_ENDIANNESS_e endianness)
Copy CAN data from 8 bytes to a 64-bit variable.
Definition: can_helper.c:295
void CAN_RxGetSignalDataFromMessageData(uint64_t message, uint64_t bitStart, uint8_t bitLength, uint64_t *pCanSignal, CAN_ENDIANNESS_e endianness)
Gets CAN signal data from a 64-bit variable. This function is used to get signal data from a 64-bit C...
Definition: can_helper.c:255
Headers for the helper functions for the CAN module.
#define DATA_READ_DATA(...)
Definition: database.h:86
#define DATA_WRITE_DATA(...)
Definition: database.h:96
DIAG_RETURNTYPE_e DIAG_Handler(DIAG_ID_e diagId, DIAG_EVENT_e event, DIAG_IMPACT_LEVEL_e impact, uint32_t data)
DIAG_Handler provides generic error handling, based on diagnosis group.
Definition: diag.c:246
Diagnosis driver header.
@ DIAG_EVENT_OK
Definition: diag_cfg.h:267
@ DIAG_STRING
Definition: diag_cfg.h:281
@ DIAG_ID_DEEP_DISCHARGE_DETECTED
Definition: diag_cfg.h:229
#define FAS_ASSERT(x)
Assertion macro that asserts that x is true.
Definition: fassert.h:255
@ STD_OK
Definition: fstd_types.h:83
#define NULL_PTR
Null pointer.
Definition: fstd_types.h:77
bool OS_CheckTimeHasPassed(uint32_t oldTimeStamp_ms, uint32_t timeToPass_ms)
This function checks if timeToPass has passed since the last timestamp to now.
Definition: os.c:150
Declaration of the OS wrapper interface.
CAN_IDENTIFIER_TYPE_e idType
Definition: can_cfg.h:201
CAN_ENDIANNESS_e endianness
Definition: can_cfg.h:203
DATA_BLOCK_STATE_REQUEST_s * pTableStateRequest
Definition: can_cfg.h:194
DATA_BLOCK_HEADER_s header
Definition: database_cfg.h:553
void SYSM_ClearAllTimingViolations(void)
Clears all timing violations (both current and recorded)
Definition: sys_mon.c:234
system monitoring module