foxBMS - Unit Tests  1.2.1
The foxBMS Unit Tests API Documentation
can_cbs_tx_minmax.c
Go to the documentation of this file.
1 /**
2  *
3  * @copyright © 2010 - 2021, 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_tx_minmax.c
44  * @author foxBMS Team
45  * @date 2021-04-20 (date of creation)
46  * @updated 2021-07-21 (date of last update)
47  * @ingroup DRIVER
48  * @prefix CAN
49  *
50  * @brief CAN driver Tx callback implementation
51  * @details CAN Tx callback for min/max values
52  */
53 
54 /*========== Includes =======================================================*/
55 #include "bms.h"
56 #include "can_cbs.h"
57 #include "can_helper.h"
58 
59 /*========== Macros and Definitions =========================================*/
60 
61 /*========== Static Constant and Variable Definitions =======================*/
62 
63 /*========== Extern Constant and Variable Definitions =======================*/
64 
65 /*========== Static Function Prototypes =====================================*/
66 
67 /*========== Static Function Implementations ================================*/
68 
69 /*========== Extern Function Implementations ================================*/
71  uint32_t id,
72  uint8_t dlc,
73  CAN_ENDIANNESS_e endianness,
74  uint8_t *pCanData,
75  uint8_t *pMuxId,
76  const CAN_SHIM_s *const kpkCanShim) {
77  /* pMuxId is not used here, therefore has to be NULL_PTR */
78  FAS_ASSERT(pMuxId == NULL_PTR);
79 
80  FAS_ASSERT(id < CAN_MAX_11BIT_ID); /* Currently standard ID, 11 bit */
81  FAS_ASSERT(dlc <= CAN_MAX_DLC); /* Currently max 8 bytes in a CAN frame */
82  FAS_ASSERT(pCanData != NULL_PTR);
83  FAS_ASSERT(kpkCanShim != NULL_PTR);
84  uint64_t message = 0;
85 
86  DATA_READ_DATA(kpkCanShim->pTableMinMax);
87 
88  int16_t packMaximumVoltage_mV = INT16_MIN;
89  int16_t packMinimumVoltage_mV = INT16_MAX;
90  int16_t packMaximumTemperature_ddegC = INT16_MIN;
91  int16_t packMinimumTemperature_ddegC = INT16_MAX;
92 
93  if (0u == BMS_GetNumberOfConnectedStrings()) {
94  /* Calculate min/max values of complete pack if all slice switches are open */
95  for (uint8_t stringNumber = 0u; stringNumber < BS_NR_OF_STRINGS; stringNumber++) {
96  if (kpkCanShim->pTableMinMax->maximumCellVoltage_mV[stringNumber] >= packMaximumVoltage_mV) {
97  packMaximumVoltage_mV = kpkCanShim->pTableMinMax->maximumCellVoltage_mV[stringNumber];
98  }
99  if (kpkCanShim->pTableMinMax->minimumCellVoltage_mV[stringNumber] <= packMinimumVoltage_mV) {
100  packMinimumVoltage_mV = kpkCanShim->pTableMinMax->minimumCellVoltage_mV[stringNumber];
101  }
102  if (kpkCanShim->pTableMinMax->maximumTemperature_ddegC[stringNumber] >= packMaximumTemperature_ddegC) {
103  packMaximumTemperature_ddegC = kpkCanShim->pTableMinMax->maximumTemperature_ddegC[stringNumber];
104  }
105  if (kpkCanShim->pTableMinMax->minimumTemperature_ddegC[stringNumber] <= packMinimumTemperature_ddegC) {
106  packMinimumTemperature_ddegC = kpkCanShim->pTableMinMax->minimumTemperature_ddegC[stringNumber];
107  }
108  }
109  } else {
110  /* Calculate min/max values of connected slices */
111  for (uint8_t stringNumber = 0u; stringNumber < BS_NR_OF_STRINGS; stringNumber++) {
112  if (true == BMS_IsStringClosed(stringNumber)) {
113  if (kpkCanShim->pTableMinMax->maximumCellVoltage_mV[stringNumber] >= packMaximumVoltage_mV) {
114  packMaximumVoltage_mV = kpkCanShim->pTableMinMax->maximumCellVoltage_mV[stringNumber];
115  }
116  if (kpkCanShim->pTableMinMax->minimumCellVoltage_mV[stringNumber] <= packMinimumVoltage_mV) {
117  packMinimumVoltage_mV = kpkCanShim->pTableMinMax->minimumCellVoltage_mV[stringNumber];
118  }
119  if (kpkCanShim->pTableMinMax->maximumTemperature_ddegC[stringNumber] >= packMaximumTemperature_ddegC) {
120  packMaximumTemperature_ddegC = kpkCanShim->pTableMinMax->maximumTemperature_ddegC[stringNumber];
121  }
122  if (kpkCanShim->pTableMinMax->minimumTemperature_ddegC[stringNumber] <= packMinimumTemperature_ddegC) {
123  packMinimumTemperature_ddegC = kpkCanShim->pTableMinMax->minimumTemperature_ddegC[stringNumber];
124  }
125  }
126  }
127  }
128 
129  /* AXIVION Disable Style Generic-NoMagicNumbers: Signal data defined in .dbc file. */
130  /* Minimum cell voltage */
131  float signalData = (float)packMinimumVoltage_mV;
132  float offset = 0.0f;
133  float factor = 1.0f;
134  signalData = (signalData + offset) * factor;
135  uint64_t data = (int64_t)signalData;
136  /* set data in CAN frame */
137  CAN_TxSetMessageDataWithSignalData(&message, 10u, 13u, data, endianness);
138 
139  /* Maximum cell voltage */
140  signalData = (float)packMaximumVoltage_mV;
141  offset = 0.0f;
142  factor = 1.0f;
143  signalData = (signalData + offset) * factor;
144  data = (int64_t)signalData;
145  /* set data in CAN frame */
146  CAN_TxSetMessageDataWithSignalData(&message, 7u, 13u, data, endianness);
147 
148  /* Minimum cell temperature */
149  signalData = (float)packMinimumTemperature_ddegC;
150  offset = 0.0f;
151  factor = 0.1f; /* convert ddegC to degC */
152  signalData = (signalData + offset) * factor;
153  data = (int64_t)signalData;
154  /* set data in CAN frame */
155  CAN_TxSetMessageDataWithSignalData(&message, 63u, 8u, data, endianness);
156 
157  /* Maximum cell temperature */
158  signalData = (float)packMaximumTemperature_ddegC;
159  offset = 0.0f;
160  factor = 0.1f; /* convert ddegC to degC */
161  signalData = (signalData + offset) * factor;
162  data = (int64_t)signalData;
163  /* set data in CAN frame */
164  CAN_TxSetMessageDataWithSignalData(&message, 55u, 8u, data, endianness);
165  /* AXIVION Enable Style Generic-NoMagicNumbers: */
166 
167  /* now copy data in the buffer that will be used to send data */
168  CAN_TxSetCanDataWithMessageData(message, pCanData, endianness);
169 
170  return 0;
171 }
172 
174  uint32_t id,
175  uint8_t dlc,
176  CAN_ENDIANNESS_e endianness,
177  uint8_t *pCanData,
178  uint8_t *pMuxId,
179  const CAN_SHIM_s *const kpkCanShim) {
180  FAS_ASSERT(id < CAN_MAX_11BIT_ID); /* Currently standard ID, 11 bit */
181  FAS_ASSERT(dlc <= CAN_MAX_DLC); /* Currently max 8 bytes in a CAN frame */
182  FAS_ASSERT(pCanData != NULL_PTR);
183  FAS_ASSERT(pMuxId != NULL_PTR);
184  FAS_ASSERT(*pMuxId < BS_NR_OF_STRINGS);
185  FAS_ASSERT(kpkCanShim != NULL_PTR);
186  uint64_t message = 0u;
187 
188  /** Database entry with minimum and maximum values does not need to be read
189  * within this callback as it is already read by function
190  * #CAN_TxMinimumMaximumValues */
191  const uint8_t stringNumber = *pMuxId;
192 
193  /* AXIVION Disable Style Generic-NoMagicNumbers: Signal data defined in .dbc file. */
194  /* Minimum cell voltage */
195  float signalData = (float)kpkCanShim->pTableMinMax->minimumCellVoltage_mV[stringNumber];
196  float offset = 0.0f;
197  float factor = 1.0f;
198  signalData = (signalData + offset) * factor;
199  uint64_t data = (int64_t)signalData;
200  /* set data in CAN frame */
201  CAN_TxSetMessageDataWithSignalData(&message, 18u, 13u, data, endianness);
202 
203  /* Maximum cell voltage */
204  signalData = (float)kpkCanShim->pTableMinMax->maximumCellVoltage_mV[stringNumber];
205  offset = 0.0f;
206  factor = 1.0f;
207  signalData = (signalData + offset) * factor;
208  data = (int64_t)signalData;
209  /* set data in CAN frame */
210  CAN_TxSetMessageDataWithSignalData(&message, 15u, 13u, data, endianness);
211 
212  /* Minimum cell temperature */
213  signalData = (float)kpkCanShim->pTableMinMax->minimumTemperature_ddegC[stringNumber];
214  offset = 0.0f;
215  factor = 0.2f; /* convert ddegC to 0.5degC */
216  signalData = (signalData + offset) * factor;
217  data = (int64_t)signalData;
218  /* set data in CAN frame */
219  CAN_TxSetMessageDataWithSignalData(&message, 44u, 9u, data, endianness);
220 
221  /* Maximum cell temperature */
222  signalData = (float)kpkCanShim->pTableMinMax->maximumTemperature_ddegC[stringNumber];
223  offset = 0.0f;
224  factor = 0.2f; /* convert ddegC to 0.5degC */
225  signalData = (signalData + offset) * factor;
226  data = (int64_t)signalData;
227  /* set data in CAN frame */
228  CAN_TxSetMessageDataWithSignalData(&message, 37, 9u, data, endianness);
229  /* AXIVION Enable Style Generic-NoMagicNumbers: */
230 
231  /* now copy data in the buffer that will be use to send data */
232  CAN_TxSetCanDataWithMessageData(message, pCanData, endianness);
233 
234  /* Increment multiplexer for next cell */
235  (*pMuxId)++;
236 
237  /* Check mux value */
238  if (*pMuxId >= BS_NR_OF_STRINGS) {
239  *pMuxId = 0u;
240  }
241 
242  return 0;
243 }
244 
245 /*========== Externalized Static Function Implementations (Unit Test) =======*/
246 #ifdef UNITY_UNIT_TEST
247 
248 #endif
#define BS_NR_OF_STRINGS
uint8_t BMS_GetNumberOfConnectedStrings(void)
Returns number of connected strings.
Definition: bms.c:1306
bool BMS_IsStringClosed(uint8_t stringNumber)
Returns string state (closed or open)
Definition: bms.c:1288
bms driver header
CAN callbacks header.
uint32_t CAN_TxMinimumMaximumValues(uint32_t id, uint8_t dlc, CAN_ENDIANNESS_e endianness, uint8_t *pCanData, uint8_t *pMuxId, const CAN_SHIM_s *const kpkCanShim)
can tx callback function for min/max values
uint32_t CAN_TxStringMinimumMaximumValues(uint32_t id, uint8_t dlc, CAN_ENDIANNESS_e endianness, uint8_t *pCanData, uint8_t *pMuxId, const CAN_SHIM_s *const kpkCanShim)
can tx callback function for string minimum and maximum values
#define CAN_MAX_11BIT_ID
Definition: can_cfg.h:85
enum CAN_ENDIANNESS CAN_ENDIANNESS_e
#define CAN_MAX_DLC
Definition: can_cfg.h:87
void CAN_TxSetMessageDataWithSignalData(uint64_t *pMessage, uint64_t bitStart, uint8_t bitLength, uint64_t canSignal, CAN_ENDIANNESS_e endianness)
Puts CAN signal data in a 64-bit variable. This function is used to compose a 64-bit CAN message....
Definition: can_helper.c:166
void CAN_TxSetCanDataWithMessageData(uint64_t message, uint8_t *pCanData, CAN_ENDIANNESS_e endianness)
Copy CAN data from a 64-bit variable to 8 bytes. This function is used to copy a 64-bit CAN message t...
Definition: can_helper.c:205
Headers for the helper functions for the CAN module.
#define DATA_READ_DATA(...)
Definition: database.h:76
#define FAS_ASSERT(x)
Assertion macro that asserts that x is true.
Definition: fassert.h:235
#define NULL_PTR
Null pointer.
Definition: fstd_types.h:75
DATA_BLOCK_MIN_MAX_s * pTableMinMax
Definition: can_cfg.h:307
int16_t maximumTemperature_ddegC[BS_NR_OF_STRINGS]
Definition: database_cfg.h:166
int16_t minimumTemperature_ddegC[BS_NR_OF_STRINGS]
Definition: database_cfg.h:163
int16_t maximumCellVoltage_mV[BS_NR_OF_STRINGS]
Definition: database_cfg.h:155
int16_t minimumCellVoltage_mV[BS_NR_OF_STRINGS]
Definition: database_cfg.h:153