foxBMS  1.4.0
The foxBMS Battery Management System API Documentation
can_cbs_rx_command.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_command.c
44  * @author foxBMS Team
45  * @date 2021-07-28 (date of creation)
46  * @updated 2022-07-28 (date of last update)
47  * @version v1.4.0
48  * @ingroup DRIVER
49  * @prefix CAN
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.h"
60 #include "can_helper.h"
61 #include "diag.h"
62 #include "os.h"
63 #include "sys_mon.h"
64 
65 /*========== Macros and Definitions =========================================*/
66 /**
67  * @brief CAN state request update time
68  * @details When a new CAN state request is received, it leads to an update
69  * of #DATA_BLOCK_STATEREQUEST_s::stateRequestViaCan if one of the
70  * following conditions is met:
71  *
72  * - The new request is different than the old request.
73  * - The old request is older than the timespan set in this define.
74  */
75 #define CAN_CAN_REQUEST_UPDATE_TIME_ms (3000u)
76 
77 /*========== Static Constant and Variable Definitions =======================*/
78 
79 /*========== Extern Constant and Variable Definitions =======================*/
80 
81 /*========== Static Function Prototypes =====================================*/
82 /**
83  * @brief clears the persistent flags
84  * @details This function clears all persistent flags (if signalData demands it)
85  * which are:
86  * - deep-discharge flag
87  * - sys mon violation flags
88  * @param[in] signalData if it is 1u, flags are cleared
89  */
90 static void CAN_ClearAllPersistentFlags(uint64_t signalData);
91 
92 /**
93  * @brief handles the mode request
94  * @param[in] signalData extracted signal data
95  * @param[in,out] kpkCanShim can shim with database entries
96  */
97 static void CAN_HandleModeRequest(uint64_t signalData, const CAN_SHIM_s *const kpkCanShim);
98 
99 /**
100  * @brief handles the balancing request
101  * @param[in] signalData extracted signal data
102  */
103 static void CAN_HandleBalancingRequest(uint64_t signalData);
104 
105 /**
106  * @brief sets the balancing threshold
107  * @param[in] signalData extracted signal data
108  */
109 static void CAN_SetBalancingThreshold(uint64_t signalData);
110 
111 /*========== Static Function Implementations ================================*/
112 static void CAN_ClearAllPersistentFlags(uint64_t signalData) {
113  /* AXIVION Routine Generic-MissingParameterAssert: signalData: parameter accepts whole range */
114  if (signalData == 1u) {
115  /* clear deep discharge */
116  for (uint8_t s = 0u; s < BS_NR_OF_STRINGS; s++) {
118  }
119  /* clear sys mon */
121  }
122 }
123 
124 static void CAN_HandleModeRequest(uint64_t signalData, const CAN_SHIM_s *const kpkCanShim) {
125  FAS_ASSERT(kpkCanShim != NULL_PTR);
126  /* AXIVION Routine Generic-MissingParameterAssert: signalData: parameter accepts whole range */
127 
128  /** 0x00: Disconnect strings from HV bus
129  * 0x01: Connect strings to HV bus to start discharge
130  * 0x02: Connect strings to HV bus to start charging
131  */
132  uint8_t stateRequest = BMS_REQ_ID_NOREQ;
133 
134  switch (signalData) {
135  case 0u:
136  stateRequest = BMS_REQ_ID_STANDBY;
137  break;
138  case 1u:
139  stateRequest = BMS_REQ_ID_NORMAL;
140  break;
141  case 2u:
142  stateRequest = BMS_REQ_ID_CHARGE;
143  break;
144  default:
145  /* default value already set in initialization */
146  break;
147  }
149  kpkCanShim->pTableStateRequest->stateRequestViaCan = stateRequest;
150  if ((kpkCanShim->pTableStateRequest->stateRequestViaCan !=
153  kpkCanShim->pTableStateRequest->stateRequestViaCanPending = stateRequest;
154  }
155  if (kpkCanShim->pTableStateRequest->stateCounter == (uint8_t)UINT8_MAX) {
156  /* overflow of state counter */
157  kpkCanShim->pTableStateRequest->stateCounter = 0u;
158  } else {
159  kpkCanShim->pTableStateRequest->stateCounter++;
160  }
161 }
162 
163 static void CAN_HandleBalancingRequest(uint64_t signalData) {
164  /* AXIVION Routine Generic-MissingParameterAssert: signalData: parameter accepts whole range */
165  /* AXIVION Next Codeline Style MisraC2012-2.2 MisraC2012-14.3: Depending on implementation STD_NOT_OK might be returned. */
167  if (signalData == 0u) {
169  } else {
171  }
172  }
173 }
174 
175 static void CAN_SetBalancingThreshold(uint64_t signalData) {
176  /* cap signal data to UINT16_MAX */
177  int32_t cappedSignalData = (int32_t)signalData;
178  if (signalData > (uint64_t)UINT16_MAX) {
179  cappedSignalData = (uint64_t)UINT16_MAX;
180  }
181  BAL_SetBalancingThreshold(cappedSignalData);
182 }
183 
184 /*========== Extern Function Implementations ================================*/
185 extern uint32_t CAN_RxRequest(
186  uint32_t id,
187  uint8_t dlc,
188  CAN_ENDIANNESS_e endianness,
189  const uint8_t *const kpkCanData,
190  const CAN_SHIM_s *const kpkCanShim) {
191  FAS_ASSERT(id < CAN_MAX_11BIT_ID); /* Currently standard ID, 11 bit */
192  FAS_ASSERT(dlc <= CAN_MAX_DLC); /* Currently max 8 bytes in a CAN frame */
193  FAS_ASSERT(kpkCanData != NULL_PTR);
194  FAS_ASSERT(kpkCanShim != NULL_PTR);
195 
196  DATA_READ_DATA(kpkCanShim->pTableStateRequest);
197 
198  uint64_t message = 0;
199  CAN_RxGetMessageDataFromCanData(&message, kpkCanData, endianness);
200 
201  uint64_t signalData = 0;
202  /* Get mode request */
203  /* AXIVION Next Codeline Style Generic-NoMagicNumbers: Signal data defined in .dbc file. */
204  CAN_RxGetSignalDataFromMessageData(message, 1u, 2u, &signalData, endianness);
205  CAN_HandleModeRequest(signalData, kpkCanShim);
206 
207  /* check for reset flag */
208  /* AXIVION Next Codeline Style Generic-NoMagicNumbers: Signal data defined in .dbc file. */
209  CAN_RxGetSignalDataFromMessageData(message, 2u, 1u, &signalData, endianness);
210  CAN_ClearAllPersistentFlags(signalData);
211 
212  /* Get balancing request */
213  /* AXIVION Next Codeline Style Generic-NoMagicNumbers: Signal data defined in .dbc file. */
214  CAN_RxGetSignalDataFromMessageData(message, 8u, 1u, &signalData, endianness);
215  CAN_HandleBalancingRequest(signalData);
216 
217  /* Get balancing threshold */
218  /* AXIVION Next Codeline Style Generic-NoMagicNumbers: Signal data defined in .dbc file. */
219  CAN_RxGetSignalDataFromMessageData(message, 23u, 8u, &signalData, endianness);
220  CAN_SetBalancingThreshold(signalData);
221 
222  /* TODO: Implement missing signals */
223 
224  DATA_WRITE_DATA(kpkCanShim->pTableStateRequest);
225 
226  return 0;
227 }
228 
229 /*========== Externalized Static Function Implementations (Unit Test) =======*/
230 #ifdef UNITY_UNIT_TEST
231 
232 #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.
uint32_t CAN_RxRequest(uint32_t id, uint8_t dlc, CAN_ENDIANNESS_e endianness, const uint8_t *const kpkCanData, const CAN_SHIM_s *const kpkCanShim)
can rx callback function for state requests
static void CAN_ClearAllPersistentFlags(uint64_t signalData)
clears the persistent flags
static void CAN_SetBalancingThreshold(uint64_t signalData)
sets the balancing threshold
static void CAN_HandleBalancingRequest(uint64_t signalData)
handles the balancing request
static void CAN_HandleModeRequest(uint64_t signalData, const CAN_SHIM_s *const kpkCanShim)
handles the mode request
#define CAN_CAN_REQUEST_UPDATE_TIME_ms
CAN state request update time.
CAN_ENDIANNESS_e
Definition: can_cfg.h:298
#define CAN_MAX_11BIT_ID
Definition: can_cfg.h:86
#define CAN_MAX_DLC
Definition: can_cfg.h:88
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:231
Diagnosis driver header.
@ DIAG_EVENT_OK
Definition: diag_cfg.h:238
@ DIAG_STRING
Definition: diag_cfg.h:252
@ DIAG_ID_DEEP_DISCHARGE_DETECTED
Definition: diag_cfg.h:209
#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.
DATA_BLOCK_STATEREQUEST_s * pTableStateRequest
Definition: can_cfg.h:318
DATA_BLOCK_HEADER_s header
Definition: database_cfg.h:532
void SYSM_ClearAllTimingViolations(void)
Clears all timing violations (both current and recorded)
Definition: sys_mon.c:232
system monitoring module