foxBMS  1.4.1
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 - 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_cell-voltages.c
44  * @author foxBMS Team
45  * @date 2021-04-20 (date of creation)
46  * @updated 2022-10-27 (date of last update)
47  * @version v1.4.1
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 /*========== Macros and Definitions =========================================*/
61 
62 /*========== Static Constant and Variable Definitions =======================*/
63 /** the number of voltages per message-frame */
64 #define CANTX_NUMBER_OF_MUX_VOLTAGES_PER_MESSAGE (4u)
65 
66 /**
67  * CAN signals used in this message
68  * Parameters:
69  * bit start, bit length, factor, offset, minimum value, maximum value
70  */
71 static const CAN_SIGNAL_TYPE_s cantx_cellVoltageMultiplexer = {7u, 8u, 1.0f, 0.0f, 0.0f, 1.0f};
72 static const CAN_SIGNAL_TYPE_s cantx_cellVoltage0InvalidFlag = {12u, 1u, 1.0f, 0.0f, 0.0f, 1.0f};
73 static const CAN_SIGNAL_TYPE_s cantx_cellVoltage1InvalidFlag = {13u, 1u, 1.0f, 0.0f, 0.0f, 1.0f};
74 static const CAN_SIGNAL_TYPE_s cantx_cellVoltage2InvalidFlag = {14u, 1u, 1.0f, 0.0f, 0.0f, 1.0f};
75 static const CAN_SIGNAL_TYPE_s cantx_cellVoltage3InvalidFlag = {15u, 1u, 1.0f, 0.0f, 0.0f, 1.0f};
76 static const CAN_SIGNAL_TYPE_s cantx_cellVoltage0_mV = {11u, 13u, 1.0f, 0.0f, 0.0f, 8192.0f};
77 static const CAN_SIGNAL_TYPE_s cantx_cellVoltage1_mV = {30u, 13u, 1.0f, 0.0f, 0.0f, 8192.0f};
78 static const CAN_SIGNAL_TYPE_s cantx_cellVoltage2_mV = {33u, 13u, 1.0f, 0.0f, 0.0f, 8192.0f};
79 static const CAN_SIGNAL_TYPE_s cantx_cellVoltage3_mV = {52u, 13u, 1.0f, 0.0f, 0.0f, 8192.0f};
80 
81 /*========== Extern Constant and Variable Definitions =======================*/
82 
83 /*========== Static Function Prototypes =====================================*/
84 /**
85  * @brief Helper function for CANTX_CellVoltages()
86  *
87  * Used in the CANTX_CellVoltages() callback to set
88  * invalid flag data and voltage data
89  * in the CAN frame.
90  *
91  * @param[in] muxId multiplexer value
92  * @param[in] pMessage pointer to CAN frame data
93  * @param[in] cellVoltageSignal signal characteristics for voltage data
94  * @param[in] cellVoltageInvalidFlagSignal signal characteristics for invalid flag data
95  * @param[in] endianness big or little endianness of data
96  * @param[in] kpkCanShim shim to the database entries
97  */
98 static void CANTX_VoltageSetData(
99  uint8_t muxId,
100  uint64_t *pMessage,
101  CAN_SIGNAL_TYPE_s cellVoltageSignal,
102  CAN_SIGNAL_TYPE_s cellVoltageInvalidFlagSignal,
103  CAN_ENDIANNESS_e endianness,
104  const CAN_SHIM_s *const kpkCanShim);
105 
106 /*========== Static Function Implementations ================================*/
107 
109  uint8_t muxId,
110  uint64_t *pMessage,
111  CAN_SIGNAL_TYPE_s cellVoltageSignal,
112  CAN_SIGNAL_TYPE_s cellVoltageInvalidFlagSignal,
113  CAN_ENDIANNESS_e endianness,
114  const CAN_SHIM_s *const kpkCanShim) {
115  /* cell index must not be greater than the number of cells */
117  /* Get string, module and cell number */
118  const uint8_t stringNumber = DATA_GetStringNumberFromVoltageIndex(muxId);
119  const uint8_t moduleNumber = DATA_GetModuleNumberFromVoltageIndex(muxId);
120  const uint8_t cellNumber = DATA_GetCellNumberFromVoltageIndex(muxId);
121 
122  uint32_t signalData_valid = 0u;
123  /* Valid bits data */
124  if ((kpkCanShim->pTableCellVoltage->invalidCellVoltage[stringNumber][moduleNumber] & (0x01u << cellNumber)) ==
125  0u) {
126  signalData_valid = 0u;
127  } else {
128  signalData_valid = 1u;
129  }
130  /* Set valid bit data in CAN frame */
132  pMessage,
133  cellVoltageInvalidFlagSignal.bitStart,
134  cellVoltageInvalidFlagSignal.bitLength,
135  signalData_valid,
136  endianness);
137 
138  /*Voltage data */
139  float signalData_mV =
140  (float)(kpkCanShim->pTableCellVoltage
141  ->cellVoltage_mV[stringNumber][(moduleNumber * BS_NR_OF_CELL_BLOCKS_PER_MODULE) + cellNumber]);
142  /* Apply offset and factor */
143  CAN_TxPrepareSignalData(&signalData_mV, cellVoltageSignal);
144  /* Set voltage data in CAN frame */
146  pMessage, cellVoltageSignal.bitStart, cellVoltageSignal.bitLength, (uint32_t)signalData_mV, endianness);
147  }
148 }
149 
150 /*========== Extern Function Implementations ================================*/
151 extern uint32_t CANTX_CellVoltages(
152  CAN_MESSAGE_PROPERTIES_s message,
153  uint8_t *pCanData,
154  uint8_t *pMuxId,
155  const CAN_SHIM_s *const kpkCanShim) {
158  FAS_ASSERT(pCanData != NULL_PTR);
159  FAS_ASSERT(pMuxId != NULL_PTR);
160  FAS_ASSERT(kpkCanShim != NULL_PTR);
161  uint64_t messageData = 0u;
162 
163  /* Reset mux if maximum was reached */
165  *pMuxId = 0u;
166  }
167  /* First signal to transmit cell voltages: get database values */
168  if (*pMuxId == 0u) {
169  DATA_READ_DATA(kpkCanShim->pTableCellVoltage);
170  }
171 
172  /* Set mux signal in CAN frame */
173  uint32_t signalData = *pMuxId / CANTX_NUMBER_OF_MUX_VOLTAGES_PER_MESSAGE;
175  &messageData,
178  (uint32_t)signalData,
179  message.endianness);
180 
181  /* Set other signals in CAN frame */
183  *pMuxId, &messageData, cantx_cellVoltage0_mV, cantx_cellVoltage0InvalidFlag, message.endianness, kpkCanShim);
184  /* Increment multiplexer for next cell */
185  (*pMuxId)++;
187  *pMuxId, &messageData, cantx_cellVoltage1_mV, cantx_cellVoltage1InvalidFlag, message.endianness, kpkCanShim);
188  /* Increment multiplexer for next cell */
189  (*pMuxId)++;
191  *pMuxId, &messageData, cantx_cellVoltage2_mV, cantx_cellVoltage2InvalidFlag, message.endianness, kpkCanShim);
192  /* Increment multiplexer for next cell */
193  (*pMuxId)++;
195  *pMuxId, &messageData, cantx_cellVoltage3_mV, cantx_cellVoltage3InvalidFlag, message.endianness, kpkCanShim);
196  /* Increment multiplexer for next cell */
197  (*pMuxId)++;
198 
199  /* All signal data copied in CAN frame, now copy data in the buffer that will be use to send the frame */
200  CAN_TxSetCanDataWithMessageData(messageData, pCanData, message.endianness);
201 
202  return 0;
203 }
204 
205 /*========== Externalized Static Function Implementations (Unit Test) =======*/
206 #ifdef UNITY_UNIT_TEST
207 
208 #endif
#define BS_NR_OF_CELL_BLOCKS_PER_MODULE
number of cells per module
#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:146
#define CAN_FOXBMS_MESSAGES_DEFAULT_DLC
Definition: can_cfg.h:96
Header for the driver for the CAN module.
#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: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_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:252
#define NULL_PTR
Null pointer.
Definition: fstd_types.h:76
CAN_ENDIANNESS_e endianness
Definition: can_cfg.h:181
DATA_BLOCK_CELL_VOLTAGE_s * pTableCellVoltage
Definition: can_cfg.h:161
uint8_t bitLength
Definition: can_helper.h:82
uint8_t bitStart
Definition: can_helper.h:81
uint64_t invalidCellVoltage[BS_NR_OF_STRINGS][BS_NR_OF_MODULES_PER_STRING]
Definition: database_cfg.h:135
int16_t cellVoltage_mV[BS_NR_OF_STRINGS][BS_NR_OF_CELL_BLOCKS_PER_STRING]
Definition: database_cfg.h:132