foxBMS  1.4.0
The foxBMS Battery Management System API Documentation
can_cbs_tx_temperature.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_tx_temperature.c
44  * @author foxBMS Team
45  * @date 2021-04-20 (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 Tx callback implementation
52  * @details CAN Tx callback for cell temperatures
53  */
54 
55 /*========== Includes =======================================================*/
56 #include "can_cbs.h"
57 #include "can_helper.h"
58 #include "foxmath.h"
59 
60 /*========== Macros and Definitions =========================================*/
61 
62 /*========== Static Constant and Variable Definitions =======================*/
63 
64 /**
65  * CAN signals used in this message
66  * Parameters:
67  * bit start, bit length, factor, offset, minimum value, maximum value
68  */
69 static const CAN_SIGNAL_TYPE_s can_cellTemperatureMultiplexer = {7u, 8u, 1.0f, 0.0f, 0.0f, 1.0f};
70 static const CAN_SIGNAL_TYPE_s can_cell0TemperatureInvalidFlag = {8u, 1u, 1.0f, 0.0f, 0.0f, 1.0f};
71 static const CAN_SIGNAL_TYPE_s can_cell1TemperatureInvalidFlag = {9u, 1u, 1.0f, 0.0f, 0.0f, 1.0f};
72 static const CAN_SIGNAL_TYPE_s can_cell2TemperatureInvalidFlag = {10u, 1u, 1.0f, 0.0f, 0.0f, 1.0f};
73 static const CAN_SIGNAL_TYPE_s can_cell3TemperatureInvalidFlag = {11u, 1u, 1.0f, 0.0f, 0.0f, 1.0f};
74 static const CAN_SIGNAL_TYPE_s can_cell4TemperatureInvalidFlag = {12u, 1u, 1.0f, 0.0f, 0.0f, 1.0f};
75 static const CAN_SIGNAL_TYPE_s can_cell5TemperatureInvalidFlag = {13u, 1u, 1.0f, 0.0f, 0.0f, 1.0f};
76 static const CAN_SIGNAL_TYPE_s can_cell0Temperature_degC = {23u, 8u, 1.0f, 0.0f, -128.0f, 127.0f};
77 static const CAN_SIGNAL_TYPE_s can_cell1Temperature_degC = {31u, 8u, 1.0f, 0.0f, -128.0f, 127.0f};
78 static const CAN_SIGNAL_TYPE_s can_cell2Temperature_degC = {39u, 8u, 1.0f, 0.0f, -128.0f, 127.0f};
79 static const CAN_SIGNAL_TYPE_s can_cell3Temperature_degC = {47u, 8u, 1.0f, 0.0f, -128.0f, 127.0f};
80 static const CAN_SIGNAL_TYPE_s can_cell4Temperature_degC = {55u, 8u, 1.0f, 0.0f, -128.0f, 127.0f};
81 static const CAN_SIGNAL_TYPE_s can_cell5Temperature_degC = {63u, 8u, 1.0f, 0.0f, -128.0f, 127.0f};
82 
83 /*========== Extern Constant and Variable Definitions =======================*/
84 
85 /*========== Static Function Prototypes =====================================*/
86 
87 /**
88  * @brief Helper function for CAN_TxCellTemperature()
89  *
90  * Used in the CAN_TxCellTemperature() callback to set
91  * invalid flag data and temperature data
92  * in the CAN frame.
93  *
94  * @param[in] muxId multiplexer value
95  * @param[in] pMessage pointer to CAN frame data
96  * @param[in] cellTemperatureSignal signal characteristics for temperature data
97  * @param[in] cellTemperatureInvalidFlagSignal signal characteristics for invalid flag data
98  * @param[in] endianness big or little endianness of data
99  * @param[in] kpkCanShim shim to the database entries
100  */
101 static void CAN_TxTemperatureSetData(
102  uint8_t muxId,
103  uint64_t *pMessage,
104  CAN_SIGNAL_TYPE_s cellTemperatureSignal,
105  CAN_SIGNAL_TYPE_s cellTemperatureInvalidFlagSignal,
106  CAN_ENDIANNESS_e endianness,
107  const CAN_SHIM_s *const kpkCanShim);
108 
109 /*========== Static Function Implementations ================================*/
110 
112  uint8_t muxId,
113  uint64_t *pMessage,
114  CAN_SIGNAL_TYPE_s cellTemperatureSignal,
115  CAN_SIGNAL_TYPE_s cellTemperatureInvalidFlagSignal,
116  CAN_ENDIANNESS_e endianness,
117  const CAN_SHIM_s *const kpkCanShim) {
118  /* sensor index must not be greater than the number of sensors */
119  if (muxId < BS_NR_OF_TEMP_SENSORS) {
120  /* start_index end_index module
121  * 00 17 module1
122  * 18 35 module2
123  * 20 53 module3
124  * 30 71 module4
125  */
126  /* start_index end_index string
127  * 000 071 string0
128  * 072 143 string1
129  */
130 
131  /* Get string, module and cell number */
132  const uint8_t stringNumber = DATA_GetStringNumberFromTemperatureIndex(muxId);
133  const uint8_t moduleNumber = DATA_GetModuleNumberFromTemperatureIndex(muxId);
134  const uint8_t sensorNumber = DATA_GetSensorNumberFromTemperatureIndex(muxId);
135 
136  uint32_t signalData_valid;
137  /* Valid bits data */
138  if ((kpkCanShim->pTableCellTemperature->invalidCellTemperature[stringNumber][moduleNumber] &
139  (1u << sensorNumber)) == 0u) {
140  signalData_valid = 0u;
141  } else {
142  signalData_valid = 1u;
143  }
144  /* Set valid bit data in CAN frame */
146  pMessage,
147  cellTemperatureInvalidFlagSignal.bitStart,
148  cellTemperatureInvalidFlagSignal.bitLength,
149  signalData_valid,
150  endianness);
151 
152  /* Temperature data */
153  float signalData_degC =
154  (float)kpkCanShim->pTableCellTemperature
155  ->cellTemperature_ddegC[stringNumber][(moduleNumber * BS_NR_OF_TEMP_SENSORS_PER_MODULE) + sensorNumber];
156  signalData_degC /= UNIT_CONVERSION_FACTOR_10_FLOAT; /* Convert temperature from decidegC to degC */
157  /* Apply offset and factor, check min/max limits */
158  CAN_TxPrepareSignalData(&signalData_degC, cellTemperatureSignal);
159  /* Set temperature data in CAN frame */
161  pMessage,
162  cellTemperatureSignal.bitStart,
163  cellTemperatureSignal.bitLength,
164  (int32_t)signalData_degC,
165  endianness);
166  }
167 }
168 
169 /*========== Extern Function Implementations ================================*/
170 extern uint32_t CAN_TxCellTemperature(
171  uint32_t id,
172  uint8_t dlc,
173  CAN_ENDIANNESS_e endianness,
174  uint8_t *pCanData,
175  uint8_t *pMuxId,
176  const CAN_SHIM_s *const kpkCanShim) {
177  FAS_ASSERT(id < CAN_MAX_11BIT_ID); /* Currently standard ID, 11 bit */
178  FAS_ASSERT(dlc <= CAN_MAX_DLC); /* Currently max 8 bytes in a CAN frame */
179  FAS_ASSERT(pCanData != NULL_PTR);
180  FAS_ASSERT(pMuxId != NULL_PTR);
181  FAS_ASSERT(kpkCanShim != NULL_PTR);
182  uint64_t message = 0;
183 
184  /* Reset mux if maximum was reached */
185  if (*pMuxId >= BS_NR_OF_TEMP_SENSORS) {
186  *pMuxId = 0u;
187  }
188 
189  /* first signal to transmit cell voltages: get database values */
190  if (*pMuxId == 0u) {
192  }
193 
194  /* Set mux signal in CAN frame */
195  uint32_t signalData = *pMuxId / 6u; /* 6 temperatures per module */
197  &message,
200  signalData,
201  endianness);
202 
203  /* Set other signals in CAN frame */
204  /* Each temperature frame contains 6 temperatures, with a correspond invalid flag*/
206  *pMuxId, &message, can_cell0Temperature_degC, can_cell0TemperatureInvalidFlag, endianness, kpkCanShim);
207  /* Increment multiplexer for next cell */
208  (*pMuxId)++;
210  *pMuxId, &message, can_cell1Temperature_degC, can_cell1TemperatureInvalidFlag, endianness, kpkCanShim);
211  /* Increment multiplexer for next cell */
212  (*pMuxId)++;
214  *pMuxId, &message, can_cell2Temperature_degC, can_cell2TemperatureInvalidFlag, endianness, kpkCanShim);
215  /* Increment multiplexer for next cell */
216  (*pMuxId)++;
218  *pMuxId, &message, can_cell3Temperature_degC, can_cell3TemperatureInvalidFlag, endianness, kpkCanShim);
219  /* Increment multiplexer for next cell */
220  (*pMuxId)++;
222  *pMuxId, &message, can_cell4Temperature_degC, can_cell4TemperatureInvalidFlag, endianness, kpkCanShim);
223  /* Increment multiplexer for next cell */
224  (*pMuxId)++;
226  *pMuxId, &message, can_cell5Temperature_degC, can_cell5TemperatureInvalidFlag, endianness, kpkCanShim);
227  /* Increment multiplexer for next cell */
228  (*pMuxId)++;
229 
230  /* All signal data copied in CAN frame, now copy data in the buffer that will be use to send the frame */
231  CAN_TxSetCanDataWithMessageData(message, pCanData, endianness);
232 
233  return 0;
234 }
235 
236 /*========== Externalized Static Function Implementations (Unit Test) =======*/
237 #ifdef UNITY_UNIT_TEST
238 
239 #endif
#define BS_NR_OF_TEMP_SENSORS_PER_MODULE
number of temperature sensors per battery module
#define BS_NR_OF_TEMP_SENSORS
CAN callbacks header.
static const CAN_SIGNAL_TYPE_s can_cell2Temperature_degC
static const CAN_SIGNAL_TYPE_s can_cell2TemperatureInvalidFlag
static const CAN_SIGNAL_TYPE_s can_cell5Temperature_degC
uint32_t CAN_TxCellTemperature(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 cell temperatures
static const CAN_SIGNAL_TYPE_s can_cell3TemperatureInvalidFlag
static const CAN_SIGNAL_TYPE_s can_cell5TemperatureInvalidFlag
static const CAN_SIGNAL_TYPE_s can_cellTemperatureMultiplexer
static const CAN_SIGNAL_TYPE_s can_cell4Temperature_degC
static const CAN_SIGNAL_TYPE_s can_cell0TemperatureInvalidFlag
static const CAN_SIGNAL_TYPE_s can_cell1Temperature_degC
static const CAN_SIGNAL_TYPE_s can_cell1TemperatureInvalidFlag
static const CAN_SIGNAL_TYPE_s can_cell0Temperature_degC
static const CAN_SIGNAL_TYPE_s can_cell4TemperatureInvalidFlag
static void CAN_TxTemperatureSetData(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 CAN_TxCellTemperature()
static const CAN_SIGNAL_TYPE_s can_cell3Temperature_degC
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_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:167
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:206
void CAN_TxPrepareSignalData(float *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:142
Headers for the helper functions for the CAN module.
#define DATA_READ_DATA(...)
Definition: database.h:83
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:252
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:76
DATA_BLOCK_CELL_TEMPERATURE_s * pTableCellTemperature
Definition: can_cfg.h:314
uint8_t bitLength
Definition: can_helper.h:82
uint8_t bitStart
Definition: can_helper.h:81
int16_t cellTemperature_ddegC[BS_NR_OF_STRINGS][BS_NR_OF_TEMP_SENSORS_PER_STRING]
Definition: database_cfg.h:148
uint16_t invalidCellTemperature[BS_NR_OF_STRINGS][BS_NR_OF_MODULES_PER_STRING]
Definition: database_cfg.h:150