foxBMS  1.4.1
The foxBMS Battery Management System API Documentation
can_cbs_rx_state-request.c
Go to the documentation of this file.
1 /**
2  *
3  * @copyright © 2010 - 2022, 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_state-request.c
44  * @author foxBMS Team
45  * @date 2021-07-28 (date of creation)
46  * @updated 2022-10-27 (date of last update)
47  * @version v1.4.1
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 /*========== Macros and Definitions =========================================*/
67 /**
68  * @brief CAN state request update time
69  * @details When a new CAN state request is received, it leads to an update
70  * of #DATA_BLOCK_STATEREQUEST_s::stateRequestViaCan if one of the
71  * following conditions is met:
72  *
73  * - The new request is different than the old request.
74  * - The old request is older than the timespan set in this define.
75  */
76 #define CANRX_CAN_REQUEST_UPDATE_TIME_ms (3000u)
77 
78 /*========== Static Constant and Variable Definitions =======================*/
79 
80 /*========== Extern Constant and Variable Definitions =======================*/
81 
82 /*========== Static Function Prototypes =====================================*/
83 /**
84  * @brief clears the persistent flags
85  * @details This function clears all persistent flags (if signalData demands it)
86  * which are:
87  * - deep-discharge flag
88  * - sys mon violation flags
89  * @param[in] signalData if it is 1u, flags are cleared
90  */
91 static void CANRX_ClearAllPersistentFlags(uint64_t signalData);
92 
93 /**
94  * @brief handles the mode request
95  * @param[in] signalData extracted signal data
96  * @param[in,out] kpkCanShim can shim with database entries
97  */
98 static void CANRX_HandleModeRequest(uint64_t signalData, const CAN_SHIM_s *const kpkCanShim);
99 
100 /**
101  * @brief handles the balancing request
102  * @param[in] signalData extracted signal data
103  */
104 static void CANRX_HandleBalancingRequest(uint64_t signalData);
105 
106 /**
107  * @brief sets the balancing threshold
108  * @param[in] signalData extracted signal data
109  */
110 static void CANRX_SetBalancingThreshold(uint64_t signalData);
111 
112 /*========== Static Function Implementations ================================*/
113 static void CANRX_ClearAllPersistentFlags(uint64_t signalData) {
114  /* AXIVION Routine Generic-MissingParameterAssert: signalData: parameter accepts whole range */
115  if (signalData == 1u) {
116  /* clear deep discharge */
117  for (uint8_t s = 0u; s < BS_NR_OF_STRINGS; s++) {
119  }
120  /* clear sys mon */
122  }
123 }
124 
125 static void CANRX_HandleModeRequest(uint64_t signalData, const CAN_SHIM_s *const kpkCanShim) {
126  FAS_ASSERT(kpkCanShim != NULL_PTR);
127  /* AXIVION Routine Generic-MissingParameterAssert: signalData: parameter accepts whole range */
128 
129  /** 0x00: Disconnect strings from HV bus
130  * 0x01: Connect strings to HV bus to start discharge
131  * 0x02: Connect strings to HV bus to start charging
132  */
133  uint8_t stateRequest = BMS_REQ_ID_NOREQ;
134 
135  switch (signalData) {
136  case 0u:
137  stateRequest = BMS_REQ_ID_STANDBY;
138  break;
139  case 1u:
140  stateRequest = BMS_REQ_ID_NORMAL;
141  break;
142  case 2u:
143  stateRequest = BMS_REQ_ID_CHARGE;
144  break;
145  default:
146  /* default value already set in initialization */
147  break;
148  }
150  kpkCanShim->pTableStateRequest->stateRequestViaCan = stateRequest;
151  if ((kpkCanShim->pTableStateRequest->stateRequestViaCan !=
154  kpkCanShim->pTableStateRequest->stateRequestViaCanPending = stateRequest;
155  }
156  if (kpkCanShim->pTableStateRequest->stateCounter == (uint8_t)UINT8_MAX) {
157  /* overflow of state counter */
158  kpkCanShim->pTableStateRequest->stateCounter = 0u;
159  } else {
160  kpkCanShim->pTableStateRequest->stateCounter++;
161  }
162 }
163 
164 static void CANRX_HandleBalancingRequest(uint64_t signalData) {
165  /* AXIVION Routine Generic-MissingParameterAssert: signalData: parameter accepts whole range */
166  /* AXIVION Next Codeline Style MisraC2012-2.2 MisraC2012-14.3: Depending on implementation STD_NOT_OK might be returned. */
168  if (signalData == 0u) {
170  } else {
172  }
173  }
174 }
175 
176 static void CANRX_SetBalancingThreshold(uint64_t signalData) {
177  /* cap signal data to UINT16_MAX */
178  int32_t cappedSignalData = (int32_t)signalData;
179  if (signalData > (uint64_t)UINT16_MAX) {
180  cappedSignalData = (uint64_t)UINT16_MAX;
181  }
182  BAL_SetBalancingThreshold(cappedSignalData);
183 }
184 
185 /*========== Extern Function Implementations ================================*/
186 extern uint32_t CANRX_BmsStateRequest(
187  CAN_MESSAGE_PROPERTIES_s message,
188  const uint8_t *const kpkCanData,
189  const CAN_SHIM_s *const kpkCanShim) {
192  FAS_ASSERT(kpkCanData != NULL_PTR);
193  FAS_ASSERT(kpkCanShim != NULL_PTR);
194 
195  DATA_READ_DATA(kpkCanShim->pTableStateRequest);
196 
197  uint64_t messageData = 0u;
198  CAN_RxGetMessageDataFromCanData(&messageData, kpkCanData, message.endianness);
199 
200  uint64_t signalData = 0;
201  /* Get mode request */
202  /* AXIVION Next Codeline Style Generic-NoMagicNumbers: Signal data defined in .dbc file. */
203  CAN_RxGetSignalDataFromMessageData(messageData, 1u, 2u, &signalData, message.endianness);
204  CANRX_HandleModeRequest(signalData, kpkCanShim);
205 
206  /* check for reset flag */
207  /* AXIVION Next Codeline Style Generic-NoMagicNumbers: Signal data defined in .dbc file. */
208  CAN_RxGetSignalDataFromMessageData(messageData, 2u, 1u, &signalData, message.endianness);
209  CANRX_ClearAllPersistentFlags(signalData);
210 
211  /* Get balancing request */
212  /* AXIVION Next Codeline Style Generic-NoMagicNumbers: Signal data defined in .dbc file. */
213  CAN_RxGetSignalDataFromMessageData(messageData, 8u, 1u, &signalData, message.endianness);
214  CANRX_HandleBalancingRequest(signalData);
215 
216  /* Get balancing threshold */
217  /* AXIVION Next Codeline Style Generic-NoMagicNumbers: Signal data defined in .dbc file. */
218  CAN_RxGetSignalDataFromMessageData(messageData, 23u, 8u, &signalData, message.endianness);
219  CANRX_SetBalancingThreshold(signalData);
220 
221  /* TODO: Implement missing signals */
222 
223  DATA_WRITE_DATA(kpkCanShim->pTableStateRequest);
224 
225  return 0;
226 }
227 
228 /*========== Externalized Static Function Implementations (Unit Test) =======*/
229 #ifdef UNITY_UNIT_TEST
230 
231 #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:106
@ BAL_STATE_GLOBAL_DISABLE_REQUEST
Definition: bal.h:105
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:73
#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:68
#define BMS_REQ_ID_NOREQ
Definition: bms_cfg.h:65
#define BMS_REQ_ID_CHARGE
Definition: bms_cfg.h:74
#define BMS_REQ_ID_NORMAL
Definition: bms_cfg.h:71
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:96
Header for the driver for the CAN module.
#define CANRX_BMS_STATE_REQUEST_ID
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:289
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:249
Headers for the helper functions for the CAN module.
#define DATA_READ_DATA(...)
Definition: database.h:83
#define DATA_WRITE_DATA(...)
Definition: database.h:93
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:243
Diagnosis driver header.
@ DIAG_EVENT_OK
Definition: diag_cfg.h:257
@ DIAG_STRING
Definition: diag_cfg.h:271
@ DIAG_ID_DEEP_DISCHARGE_DETECTED
Definition: diag_cfg.h:226
#define FAS_ASSERT(x)
Assertion macro that asserts that x is true.
Definition: fassert.h:252
@ STD_OK
Definition: fstd_types.h:82
#define NULL_PTR
Null pointer.
Definition: fstd_types.h:76
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:144
Declaration of the OS wrapper interface.
CAN_ENDIANNESS_e endianness
Definition: can_cfg.h:181
DATA_BLOCK_STATEREQUEST_s * pTableStateRequest
Definition: can_cfg.h:166
DATA_BLOCK_HEADER_s header
Definition: database_cfg.h:522
void SYSM_ClearAllTimingViolations(void)
Clears all timing violations (both current and recorded)
Definition: sys_mon.c:232
system monitoring module