foxBMS  1.6.0
The foxBMS Battery Management System API Documentation
can_cbs_tx_cell-temperatures.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_tx_cell-temperatures.c
44  * @author foxBMS Team
45  * @date 2021-04-20 (date of creation)
46  * @updated 2023-10-12 (date of last update)
47  * @version v1.6.0
48  * @ingroup DRIVER
49  * @prefix CANTX
50  *
51  * @brief CAN driver Tx callback implementation
52  * @details CAN Tx callback for cell temperatures
53  */
54 
55 /*========== Includes =======================================================*/
56 #include "can_cbs_tx.h"
58 #include "can_helper.h"
59 #include "foxmath.h"
60 
61 #include <math.h>
62 #include <stdint.h>
63 
64 /*========== Macros and Definitions =========================================*/
65 
66 /*========== Static Constant and Variable Definitions =======================*/
67 
68 /**
69  * CAN signals used in this message
70  * Parameters:
71  * bit start, bit length, factor, offset, minimum value, maximum value
72  */
73 static const CAN_SIGNAL_TYPE_s cantx_cellTemperatureMultiplexer = {7u, 8u, 1.0f, 0.0f, 0.0f, 1.0f};
74 static const CAN_SIGNAL_TYPE_s cantx_cell0TemperatureInvalidFlag = {8u, 1u, 1.0f, 0.0f, 0.0f, 1.0f};
75 static const CAN_SIGNAL_TYPE_s cantx_cell1TemperatureInvalidFlag = {9u, 1u, 1.0f, 0.0f, 0.0f, 1.0f};
76 static const CAN_SIGNAL_TYPE_s cantx_cell2TemperatureInvalidFlag = {10u, 1u, 1.0f, 0.0f, 0.0f, 1.0f};
77 static const CAN_SIGNAL_TYPE_s cantx_cell3TemperatureInvalidFlag = {11u, 1u, 1.0f, 0.0f, 0.0f, 1.0f};
78 static const CAN_SIGNAL_TYPE_s cantx_cell4TemperatureInvalidFlag = {12u, 1u, 1.0f, 0.0f, 0.0f, 1.0f};
79 static const CAN_SIGNAL_TYPE_s cantx_cell5TemperatureInvalidFlag = {13u, 1u, 1.0f, 0.0f, 0.0f, 1.0f};
80 static const CAN_SIGNAL_TYPE_s cantx_cell0Temperature_degC = {23u, 8u, 1.0f, 0.0f, -128.0f, 127.0f};
81 static const CAN_SIGNAL_TYPE_s cantx_cell1Temperature_degC = {31u, 8u, 1.0f, 0.0f, -128.0f, 127.0f};
82 static const CAN_SIGNAL_TYPE_s cantx_cell2Temperature_degC = {39u, 8u, 1.0f, 0.0f, -128.0f, 127.0f};
83 static const CAN_SIGNAL_TYPE_s cantx_cell3Temperature_degC = {47u, 8u, 1.0f, 0.0f, -128.0f, 127.0f};
84 static const CAN_SIGNAL_TYPE_s cantx_cell4Temperature_degC = {55u, 8u, 1.0f, 0.0f, -128.0f, 127.0f};
85 static const CAN_SIGNAL_TYPE_s cantx_cell5Temperature_degC = {63u, 8u, 1.0f, 0.0f, -128.0f, 127.0f};
86 
87 /*========== Extern Constant and Variable Definitions =======================*/
88 
89 /*========== Static Function Prototypes =====================================*/
90 
91 /**
92  * @brief Helper function for CANTX_CellTemperatures()
93  *
94  * Used in the CANTX_CellTemperatures() callback to set
95  * invalid flag data and temperature data
96  * in the CAN frame.
97  *
98  * @param[in] muxId multiplexer value
99  * @param[in] pMessage pointer to CAN frame data
100  * @param[in] cellTemperatureSignal signal characteristics for temperature data
101  * @param[in] cellTemperatureInvalidFlagSignal signal characteristics for invalid flag data
102  * @param[in] endianness big or little endianness of data
103  * @param[in] kpkCanShim shim to the database entries
104  */
105 static void CANTX_TemperatureSetData(
106  uint8_t muxId,
107  uint64_t *pMessage,
108  CAN_SIGNAL_TYPE_s cellTemperatureSignal,
109  CAN_SIGNAL_TYPE_s cellTemperatureInvalidFlagSignal,
110  CAN_ENDIANNESS_e endianness,
111  const CAN_SHIM_s *const kpkCanShim);
112 
113 /*========== Static Function Implementations ================================*/
114 
116  uint8_t muxId,
117  uint64_t *pMessage,
118  CAN_SIGNAL_TYPE_s cellTemperatureSignal,
119  CAN_SIGNAL_TYPE_s cellTemperatureInvalidFlagSignal,
120  CAN_ENDIANNESS_e endianness,
121  const CAN_SHIM_s *const kpkCanShim) {
122  /* sensor index must not be greater than the number of sensors */
123  if (muxId < BS_NR_OF_TEMP_SENSORS) {
124  /* start_index end_index module
125  * 00 17 module1
126  * 18 35 module2
127  * 20 53 module3
128  * 30 71 module4
129  */
130  /* start_index end_index string
131  * 000 071 string0
132  */
133 
134  /* Get string, module and cell number */
135  const uint8_t stringNumber = DATA_GetStringNumberFromTemperatureIndex(muxId);
136  const uint8_t moduleNumber = DATA_GetModuleNumberFromTemperatureIndex(muxId);
137  const uint8_t sensorNumber = DATA_GetSensorNumberFromTemperatureIndex(muxId);
138 
139  uint32_t signalData_valid;
140  /* Valid bits data */
141  if ((kpkCanShim->pTableCellTemperature->invalidCellTemperature[stringNumber][moduleNumber] &
142  (1u << sensorNumber)) == 0u) {
143  signalData_valid = 0u;
144  } else {
145  signalData_valid = 1u;
146  }
147  /* Set valid bit data in CAN frame */
149  pMessage,
150  cellTemperatureInvalidFlagSignal.bitStart,
151  cellTemperatureInvalidFlagSignal.bitLength,
152  signalData_valid,
153  endianness);
154 
155  /* Temperature data */
156  float_t signalData_degC =
157  (float_t)kpkCanShim->pTableCellTemperature->cellTemperature_ddegC[stringNumber][moduleNumber][sensorNumber];
158  signalData_degC /= UNIT_CONVERSION_FACTOR_10_FLOAT; /* Convert temperature from decidegC to degC */
159  /* Apply offset and factor, check min/max limits */
160  CAN_TxPrepareSignalData(&signalData_degC, cellTemperatureSignal);
161  /* Set temperature data in CAN frame */
163  pMessage,
164  cellTemperatureSignal.bitStart,
165  cellTemperatureSignal.bitLength,
166  (int32_t)signalData_degC,
167  endianness);
168  }
169 }
170 
171 /*========== Extern Function Implementations ================================*/
172 extern uint32_t CANTX_CellTemperatures(
173  CAN_MESSAGE_PROPERTIES_s message,
174  uint8_t *pCanData,
175  uint8_t *pMuxId,
176  const CAN_SHIM_s *const kpkCanShim) {
180  FAS_ASSERT(pCanData != NULL_PTR);
181  FAS_ASSERT(pMuxId != NULL_PTR);
182  FAS_ASSERT(kpkCanShim != NULL_PTR);
183  uint64_t messageData = 0u;
184 
185  /* Reset mux if maximum was reached */
186  if (*pMuxId >= BS_NR_OF_TEMP_SENSORS) {
187  *pMuxId = 0u;
188  }
189 
190  /* first signal to transmit cell voltages: get database values */
191  if (*pMuxId == 0u) {
193  }
194 
195  /* Set mux signal in CAN frame */
196  uint32_t signalData = *pMuxId / 6u; /* 6 temperatures per module */
198  &messageData,
201  signalData,
202  message.endianness);
203 
204  /* Set other signals in CAN frame */
205  /* Each temperature frame contains 6 temperatures, with a correspond invalid flag*/
207  *pMuxId,
208  &messageData,
211  message.endianness,
212  kpkCanShim);
213  /* Increment multiplexer for next cell */
214  (*pMuxId)++;
216  *pMuxId,
217  &messageData,
220  message.endianness,
221  kpkCanShim);
222  /* Increment multiplexer for next cell */
223  (*pMuxId)++;
225  *pMuxId,
226  &messageData,
229  message.endianness,
230  kpkCanShim);
231  /* Increment multiplexer for next cell */
232  (*pMuxId)++;
234  *pMuxId,
235  &messageData,
238  message.endianness,
239  kpkCanShim);
240  /* Increment multiplexer for next cell */
241  (*pMuxId)++;
243  *pMuxId,
244  &messageData,
247  message.endianness,
248  kpkCanShim);
249  /* Increment multiplexer for next cell */
250  (*pMuxId)++;
252  *pMuxId,
253  &messageData,
256  message.endianness,
257  kpkCanShim);
258  /* Increment multiplexer for next cell */
259  (*pMuxId)++;
260 
261  /* All signal data copied in CAN frame, now copy data in the buffer that will be use to send the frame */
262  CAN_TxSetCanDataWithMessageData(messageData, pCanData, message.endianness);
263 
264  return 0;
265 }
266 
267 /*========== Externalized Static Function Implementations (Unit Test) =======*/
268 #ifdef UNITY_UNIT_TEST
269 #endif
#define BS_NR_OF_TEMP_SENSORS
CAN callbacks header.
static const CAN_SIGNAL_TYPE_s cantx_cell5Temperature_degC
static const CAN_SIGNAL_TYPE_s cantx_cell0Temperature_degC
static const CAN_SIGNAL_TYPE_s cantx_cell2TemperatureInvalidFlag
static const CAN_SIGNAL_TYPE_s cantx_cellTemperatureMultiplexer
static void CANTX_TemperatureSetData(uint8_t muxId, uint64_t *pMessage, CAN_SIGNAL_TYPE_s cellTemperatureSignal, CAN_SIGNAL_TYPE_s cellTemperatureInvalidFlagSignal, CAN_ENDIANNESS_e endianness, const CAN_SHIM_s *const kpkCanShim)
Helper function for CANTX_CellTemperatures()
static const CAN_SIGNAL_TYPE_s cantx_cell5TemperatureInvalidFlag
static const CAN_SIGNAL_TYPE_s cantx_cell1TemperatureInvalidFlag
static const CAN_SIGNAL_TYPE_s cantx_cell3TemperatureInvalidFlag
static const CAN_SIGNAL_TYPE_s cantx_cell2Temperature_degC
static const CAN_SIGNAL_TYPE_s cantx_cell0TemperatureInvalidFlag
static const CAN_SIGNAL_TYPE_s cantx_cell1Temperature_degC
uint32_t CANTX_CellTemperatures(CAN_MESSAGE_PROPERTIES_s message, uint8_t *pCanData, uint8_t *pMuxId, const CAN_SHIM_s *const kpkCanShim)
can tx callback function for cell temperatures
static const CAN_SIGNAL_TYPE_s cantx_cell4TemperatureInvalidFlag
static const CAN_SIGNAL_TYPE_s cantx_cell3Temperature_degC
static const CAN_SIGNAL_TYPE_s cantx_cell4Temperature_degC
CAN_ENDIANNESS_e
Definition: can_cfg.h:156
#define CAN_FOXBMS_MESSAGES_DEFAULT_DLC
Definition: can_cfg.h:106
Header for the driver for the CAN module.
#define CANTX_CELL_TEMPERATURES_ID_TYPE
#define CANTX_CELL_TEMPERATURES_ID
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:173
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:212
void CAN_TxPrepareSignalData(float_t *pSignal, CAN_SIGNAL_TYPE_s signalProperties)
Prepare signal data. This function takes the signal data and applies factor, applies offset and compa...
Definition: can_helper.c:145
Headers for the helper functions for the CAN module.
#define DATA_READ_DATA(...)
Definition: database.h:86
uint8_t DATA_GetSensorNumberFromTemperatureIndex(uint16_t sensorIndex)
Returns sensor number of passed temperature sensor index.
uint8_t DATA_GetModuleNumberFromTemperatureIndex(uint16_t sensorIndex)
Returns module number of passed temperature sensor index.
uint8_t DATA_GetStringNumberFromTemperatureIndex(uint16_t sensorIndex)
Returns string number of passed temperature sensor index.
#define FAS_ASSERT(x)
Assertion macro that asserts that x is true.
Definition: fassert.h:255
math library for often used math functions
#define UNIT_CONVERSION_FACTOR_10_FLOAT
Definition: foxmath.h:76
#define NULL_PTR
Null pointer.
Definition: fstd_types.h:77
CAN_IDENTIFIER_TYPE_e idType
Definition: can_cfg.h:201
CAN_ENDIANNESS_e endianness
Definition: can_cfg.h:203
DATA_BLOCK_CELL_TEMPERATURE_s * pTableCellTemperature
Definition: can_cfg.h:180
uint8_t bitLength
Definition: can_helper.h:84
uint8_t bitStart
Definition: can_helper.h:83
int16_t cellTemperature_ddegC[BS_NR_OF_STRINGS][BS_NR_OF_MODULES_PER_STRING][BS_NR_OF_TEMP_SENSORS_PER_MODULE]
Definition: database_cfg.h:156
uint16_t invalidCellTemperature[BS_NR_OF_STRINGS][BS_NR_OF_MODULES_PER_STRING]
Definition: database_cfg.h:158