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