foxBMS  1.6.0
The foxBMS Battery Management System API Documentation
can_cbs_tx_cell-voltages.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-voltages.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 voltages
53  */
54 
55 /*========== Includes =======================================================*/
56 #include "can_cbs_tx.h"
58 #include "can_helper.h"
59 
60 #include <math.h>
61 #include <stdint.h>
62 
63 /*========== Macros and Definitions =========================================*/
64 
65 /*========== Static Constant and Variable Definitions =======================*/
66 /** the number of voltages per message-frame */
67 #define CANTX_NUMBER_OF_MUX_VOLTAGES_PER_MESSAGE (4u)
68 
69 /**
70  * CAN signals used in this message
71  * Parameters:
72  * bit start, bit length, factor, offset, minimum value, maximum value
73  */
74 static const CAN_SIGNAL_TYPE_s cantx_cellVoltageMultiplexer = {7u, 8u, 1.0f, 0.0f, 0.0f, 1.0f};
75 static const CAN_SIGNAL_TYPE_s cantx_cellVoltage0InvalidFlag = {12u, 1u, 1.0f, 0.0f, 0.0f, 1.0f};
76 static const CAN_SIGNAL_TYPE_s cantx_cellVoltage1InvalidFlag = {13u, 1u, 1.0f, 0.0f, 0.0f, 1.0f};
77 static const CAN_SIGNAL_TYPE_s cantx_cellVoltage2InvalidFlag = {14u, 1u, 1.0f, 0.0f, 0.0f, 1.0f};
78 static const CAN_SIGNAL_TYPE_s cantx_cellVoltage3InvalidFlag = {15u, 1u, 1.0f, 0.0f, 0.0f, 1.0f};
79 static const CAN_SIGNAL_TYPE_s cantx_cellVoltage0_mV = {11u, 13u, 1.0f, 0.0f, 0.0f, 8192.0f};
80 static const CAN_SIGNAL_TYPE_s cantx_cellVoltage1_mV = {30u, 13u, 1.0f, 0.0f, 0.0f, 8192.0f};
81 static const CAN_SIGNAL_TYPE_s cantx_cellVoltage2_mV = {33u, 13u, 1.0f, 0.0f, 0.0f, 8192.0f};
82 static const CAN_SIGNAL_TYPE_s cantx_cellVoltage3_mV = {52u, 13u, 1.0f, 0.0f, 0.0f, 8192.0f};
83 
84 /*========== Extern Constant and Variable Definitions =======================*/
85 
86 /*========== Static Function Prototypes =====================================*/
87 /**
88  * @brief Helper function for CANTX_CellVoltages()
89  *
90  * Used in the CANTX_CellVoltages() callback to set
91  * invalid flag data and voltage 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] cellVoltageSignal signal characteristics for voltage data
97  * @param[in] cellVoltageInvalidFlagSignal 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 CANTX_VoltageSetData(
102  uint8_t muxId,
103  uint64_t *pMessage,
104  CAN_SIGNAL_TYPE_s cellVoltageSignal,
105  CAN_SIGNAL_TYPE_s cellVoltageInvalidFlagSignal,
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 cellVoltageSignal,
115  CAN_SIGNAL_TYPE_s cellVoltageInvalidFlagSignal,
116  CAN_ENDIANNESS_e endianness,
117  const CAN_SHIM_s *const kpkCanShim) {
118  /* AXIVION Routine Generic-MissingParameterAssert: muxId: parameter checked in calling function */
119  /* AXIVION Routine Generic-MissingParameterAssert: pMessage: passed parameter created from calling function */
120  /* AXIVION Routine Generic-MissingParameterAssert: cellVoltageSignal: Assertion done in CAN_TxPrepareSignalData */
121  /* AXIVION Routine Generic-MissingParameterAssert: cellVoltageInvalidFlagSignal: Assertion done by caller */
122  /* AXIVION Routine Generic-MissingParameterAssert: endianness: parameter checked in calling function */
123  /* AXIVION Routine Generic-MissingParameterAssert: kpkCanShim: parameter checked in calling function */
124 
125  /* cell index must not be greater than the number of cells */
127  /* Get string, module and cell number */
128  const uint8_t stringNumber = DATA_GetStringNumberFromVoltageIndex(muxId);
129  const uint8_t moduleNumber = DATA_GetModuleNumberFromVoltageIndex(muxId);
130  const uint8_t cellBlockNumber = DATA_GetCellNumberFromVoltageIndex(muxId);
131 
132  uint32_t signalData_valid = 0u;
133  /* Valid bits data */
134  if ((kpkCanShim->pTableCellVoltage->invalidCellVoltage[stringNumber][moduleNumber] &
135  (0x01u << cellBlockNumber)) == 0u) {
136  signalData_valid = 0u;
137  } else {
138  signalData_valid = 1u;
139  }
140  /* Set valid bit data in CAN frame */
142  pMessage,
143  cellVoltageInvalidFlagSignal.bitStart,
144  cellVoltageInvalidFlagSignal.bitLength,
145  signalData_valid,
146  endianness);
147 
148  /*Voltage data */
149  float_t signalData_mV =
150  (float_t)(kpkCanShim->pTableCellVoltage->cellVoltage_mV[stringNumber][moduleNumber][cellBlockNumber]);
151  /* Apply offset and factor */
152  CAN_TxPrepareSignalData(&signalData_mV, cellVoltageSignal);
153  /* Set voltage data in CAN frame */
155  pMessage, cellVoltageSignal.bitStart, cellVoltageSignal.bitLength, (uint32_t)signalData_mV, endianness);
156  }
157 }
158 
159 /*========== Extern Function Implementations ================================*/
160 extern uint32_t CANTX_CellVoltages(
161  CAN_MESSAGE_PROPERTIES_s message,
162  uint8_t *pCanData,
163  uint8_t *pMuxId,
164  const CAN_SHIM_s *const kpkCanShim) {
168  FAS_ASSERT((message.endianness == CAN_LITTLE_ENDIAN) || (message.endianness == CAN_BIG_ENDIAN));
169  FAS_ASSERT(pCanData != NULL_PTR);
170  FAS_ASSERT(pMuxId != NULL_PTR);
171  FAS_ASSERT(kpkCanShim != NULL_PTR);
172  uint64_t messageData = 0u;
173 
174  /* Reset mux if maximum was reached */
176  *pMuxId = 0u;
177  }
178  /* First signal to transmit cell voltages: get database values */
179  if (*pMuxId == 0u) {
180  DATA_READ_DATA(kpkCanShim->pTableCellVoltage);
181  }
182 
183  /* Set mux signal in CAN frame */
184  uint32_t signalData = *pMuxId / CANTX_NUMBER_OF_MUX_VOLTAGES_PER_MESSAGE;
186  &messageData,
189  (uint32_t)signalData,
190  message.endianness);
191 
192  /* Set other signals in CAN frame */
194  *pMuxId, &messageData, cantx_cellVoltage0_mV, cantx_cellVoltage0InvalidFlag, message.endianness, kpkCanShim);
195  /* Increment multiplexer for next cell */
196  (*pMuxId)++;
198  *pMuxId, &messageData, cantx_cellVoltage1_mV, cantx_cellVoltage1InvalidFlag, message.endianness, kpkCanShim);
199  /* Increment multiplexer for next cell */
200  (*pMuxId)++;
202  *pMuxId, &messageData, cantx_cellVoltage2_mV, cantx_cellVoltage2InvalidFlag, message.endianness, kpkCanShim);
203  /* Increment multiplexer for next cell */
204  (*pMuxId)++;
206  *pMuxId, &messageData, cantx_cellVoltage3_mV, cantx_cellVoltage3InvalidFlag, message.endianness, kpkCanShim);
207  /* Increment multiplexer for next cell */
208  (*pMuxId)++;
209 
210  /* All signal data copied in CAN frame, now copy data in the buffer that will be use to send the frame */
211  CAN_TxSetCanDataWithMessageData(messageData, pCanData, message.endianness);
212 
213  return 0;
214 }
215 
216 /*========== Externalized Static Function Implementations (Unit Test) =======*/
217 #ifdef UNITY_UNIT_TEST
218 #endif
#define BS_NR_OF_STRINGS
Number of parallel strings in the battery pack.
#define BS_NR_OF_CELL_BLOCKS_PER_STRING
CAN callbacks header.
static const CAN_SIGNAL_TYPE_s cantx_cellVoltage0InvalidFlag
static const CAN_SIGNAL_TYPE_s cantx_cellVoltage1_mV
uint32_t CANTX_CellVoltages(CAN_MESSAGE_PROPERTIES_s message, uint8_t *pCanData, uint8_t *pMuxId, const CAN_SHIM_s *const kpkCanShim)
can tx callback function for cell voltages
static const CAN_SIGNAL_TYPE_s cantx_cellVoltage3_mV
static const CAN_SIGNAL_TYPE_s cantx_cellVoltage2_mV
static const CAN_SIGNAL_TYPE_s cantx_cellVoltageMultiplexer
#define CANTX_NUMBER_OF_MUX_VOLTAGES_PER_MESSAGE
static const CAN_SIGNAL_TYPE_s cantx_cellVoltage1InvalidFlag
static const CAN_SIGNAL_TYPE_s cantx_cellVoltage0_mV
static const CAN_SIGNAL_TYPE_s cantx_cellVoltage3InvalidFlag
static const CAN_SIGNAL_TYPE_s cantx_cellVoltage2InvalidFlag
static void CANTX_VoltageSetData(uint8_t muxId, uint64_t *pMessage, CAN_SIGNAL_TYPE_s cellVoltageSignal, CAN_SIGNAL_TYPE_s cellVoltageInvalidFlagSignal, CAN_ENDIANNESS_e endianness, const CAN_SHIM_s *const kpkCanShim)
Helper function for CANTX_CellVoltages()
CAN_ENDIANNESS_e
Definition: can_cfg.h:156
@ CAN_LITTLE_ENDIAN
Definition: can_cfg.h:157
@ CAN_BIG_ENDIAN
Definition: can_cfg.h:158
#define CAN_FOXBMS_MESSAGES_DEFAULT_DLC
Definition: can_cfg.h:106
Header for the driver for the CAN module.
#define CANTX_CELL_VOLTAGES_ID_TYPE
#define CANTX_CELL_VOLTAGES_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_GetModuleNumberFromVoltageIndex(uint16_t cellIndex)
Returns module number of passed cell index.
uint8_t DATA_GetStringNumberFromVoltageIndex(uint16_t cellIndex)
Returns string number of passed cell index.
uint8_t DATA_GetCellNumberFromVoltageIndex(uint16_t cellIndex)
Returns cell number of passed cell index.
#define FAS_ASSERT(x)
Assertion macro that asserts that x is true.
Definition: fassert.h:255
#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_VOLTAGE_s * pTableCellVoltage
Definition: can_cfg.h:179
uint8_t bitLength
Definition: can_helper.h:84
uint8_t bitStart
Definition: can_helper.h:83
int16_t cellVoltage_mV[BS_NR_OF_STRINGS][BS_NR_OF_MODULES_PER_STRING][BS_NR_OF_CELL_BLOCKS_PER_MODULE]
Definition: database_cfg.h:139
uint64_t invalidCellVoltage[BS_NR_OF_STRINGS][BS_NR_OF_MODULES_PER_STRING]
Definition: database_cfg.h:142