foxBMS  1.4.0
The foxBMS Battery Management System API Documentation
can_cbs_tx_voltage.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_voltage.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 voltages
53  */
54 
55 /*========== Includes =======================================================*/
56 #include "can_cbs.h"
57 #include "can_helper.h"
58 
59 /*========== Macros and Definitions =========================================*/
60 
61 /*========== Static Constant and Variable Definitions =======================*/
62 /** the number of voltages per message-frame */
63 #define NUMBER_OF_MUX_VOLTAGES_PER_MESSAGE (4u)
64 
65 /**
66  * CAN signals used in this message
67  * Parameters:
68  * bit start, bit length, factor, offset, minimum value, maximum value
69  */
70 static const CAN_SIGNAL_TYPE_s can_cellVoltageMultiplexer = {7u, 8u, 1.0f, 0.0f, 0.0f, 1.0f};
71 static const CAN_SIGNAL_TYPE_s can_cellVoltage0InvalidFlag = {12u, 1u, 1.0f, 0.0f, 0.0f, 1.0f};
72 static const CAN_SIGNAL_TYPE_s can_cellVoltage1InvalidFlag = {13u, 1u, 1.0f, 0.0f, 0.0f, 1.0f};
73 static const CAN_SIGNAL_TYPE_s can_cellVoltage2InvalidFlag = {14u, 1u, 1.0f, 0.0f, 0.0f, 1.0f};
74 static const CAN_SIGNAL_TYPE_s can_cellVoltage3InvalidFlag = {15u, 1u, 1.0f, 0.0f, 0.0f, 1.0f};
75 static const CAN_SIGNAL_TYPE_s can_cellVoltage0_mV = {11u, 13u, 1.0f, 0.0f, 0.0f, 8192.0f};
76 static const CAN_SIGNAL_TYPE_s can_cellVoltage1_mV = {30u, 13u, 1.0f, 0.0f, 0.0f, 8192.0f};
77 static const CAN_SIGNAL_TYPE_s can_cellVoltage2_mV = {33u, 13u, 1.0f, 0.0f, 0.0f, 8192.0f};
78 static const CAN_SIGNAL_TYPE_s can_cellVoltage3_mV = {52u, 13u, 1.0f, 0.0f, 0.0f, 8192.0f};
79 
80 /*========== Extern Constant and Variable Definitions =======================*/
81 
82 /*========== Static Function Prototypes =====================================*/
83 /**
84  * @brief Helper function for CAN_TxVoltage()
85  *
86  * Used in the CAN_TxVoltage() callback to set
87  * invalid flag data and voltage data
88  * in the CAN frame.
89  *
90  * @param[in] muxId multiplexer value
91  * @param[in] pMessage pointer to CAN frame data
92  * @param[in] cellVoltageSignal signal characteristics for voltage data
93  * @param[in] cellVoltageInvalidFlagSignal signal characteristics for invalid flag data
94  * @param[in] endianness big or little endianness of data
95  * @param[in] kpkCanShim shim to the database entries
96  */
97 static void CAN_TxVoltageSetData(
98  uint8_t muxId,
99  uint64_t *pMessage,
100  CAN_SIGNAL_TYPE_s cellVoltageSignal,
101  CAN_SIGNAL_TYPE_s cellVoltageInvalidFlagSignal,
102  CAN_ENDIANNESS_e endianness,
103  const CAN_SHIM_s *const kpkCanShim);
104 
105 /*========== Static Function Implementations ================================*/
106 
108  uint8_t muxId,
109  uint64_t *pMessage,
110  CAN_SIGNAL_TYPE_s cellVoltageSignal,
111  CAN_SIGNAL_TYPE_s cellVoltageInvalidFlagSignal,
112  CAN_ENDIANNESS_e endianness,
113  const CAN_SHIM_s *const kpkCanShim) {
114  /* cell index must not be greater than the number of cells */
116  /* Get string, module and cell number */
117  const uint8_t stringNumber = DATA_GetStringNumberFromVoltageIndex(muxId);
118  const uint8_t moduleNumber = DATA_GetModuleNumberFromVoltageIndex(muxId);
119  const uint8_t cellNumber = DATA_GetCellNumberFromVoltageIndex(muxId);
120 
121  uint32_t signalData_valid = 0u;
122  /* Valid bits data */
123  if ((kpkCanShim->pTableCellVoltage->invalidCellVoltage[stringNumber][moduleNumber] & (0x01u << cellNumber)) ==
124  0u) {
125  signalData_valid = 0u;
126  } else {
127  signalData_valid = 1u;
128  }
129  /* Set valid bit data in CAN frame */
131  pMessage,
132  cellVoltageInvalidFlagSignal.bitStart,
133  cellVoltageInvalidFlagSignal.bitLength,
134  signalData_valid,
135  endianness);
136 
137  /*Voltage data */
138  float signalData_mV =
139  (float)(kpkCanShim->pTableCellVoltage
140  ->cellVoltage_mV[stringNumber][(moduleNumber * BS_NR_OF_CELL_BLOCKS_PER_MODULE) + cellNumber]);
141  /* Apply offset and factor */
142  CAN_TxPrepareSignalData(&signalData_mV, cellVoltageSignal);
143  /* Set voltage data in CAN frame */
145  pMessage, cellVoltageSignal.bitStart, cellVoltageSignal.bitLength, (uint32_t)signalData_mV, endianness);
146  }
147 }
148 
149 /*========== Extern Function Implementations ================================*/
150 extern uint32_t CAN_TxVoltage(
151  uint32_t id,
152  uint8_t dlc,
153  CAN_ENDIANNESS_e endianness,
154  uint8_t *pCanData,
155  uint8_t *pMuxId,
156  const CAN_SHIM_s *const kpkCanShim) {
157  FAS_ASSERT(id < CAN_MAX_11BIT_ID); /* Currently standard ID, 11 bit */
158  FAS_ASSERT(dlc <= CAN_MAX_DLC); /* Currently max 8 bytes in a CAN frame */
159  FAS_ASSERT(pCanData != NULL_PTR);
160  FAS_ASSERT(pMuxId != NULL_PTR);
161  FAS_ASSERT(kpkCanShim != NULL_PTR);
162  uint64_t message = 0;
163 
164  /* Reset mux if maximum was reached */
166  *pMuxId = 0u;
167  }
168  /* First signal to transmit cell voltages: get database values */
169  if (*pMuxId == 0u) {
170  DATA_READ_DATA(kpkCanShim->pTableCellVoltage);
171  }
172 
173  /* Set mux signal in CAN frame */
174  uint32_t signalData = *pMuxId / NUMBER_OF_MUX_VOLTAGES_PER_MESSAGE;
176  &message,
179  (uint32_t)signalData,
180  endianness);
181 
182  /* Set other signals in CAN frame */
183  CAN_TxVoltageSetData(*pMuxId, &message, can_cellVoltage0_mV, can_cellVoltage0InvalidFlag, endianness, kpkCanShim);
184  /* Increment multiplexer for next cell */
185  (*pMuxId)++;
186  CAN_TxVoltageSetData(*pMuxId, &message, can_cellVoltage1_mV, can_cellVoltage1InvalidFlag, endianness, kpkCanShim);
187  /* Increment multiplexer for next cell */
188  (*pMuxId)++;
189  CAN_TxVoltageSetData(*pMuxId, &message, can_cellVoltage2_mV, can_cellVoltage2InvalidFlag, endianness, kpkCanShim);
190  /* Increment multiplexer for next cell */
191  (*pMuxId)++;
192  CAN_TxVoltageSetData(*pMuxId, &message, can_cellVoltage3_mV, can_cellVoltage3InvalidFlag, endianness, kpkCanShim);
193  /* Increment multiplexer for next cell */
194  (*pMuxId)++;
195 
196  /* All signal data copied in CAN frame, now copy data in the buffer that will be use to send the frame */
197  CAN_TxSetCanDataWithMessageData(message, pCanData, endianness);
198 
199  return 0;
200 }
201 
202 /*========== Externalized Static Function Implementations (Unit Test) =======*/
203 #ifdef UNITY_UNIT_TEST
204 
205 #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 can_cellVoltage1InvalidFlag
static const CAN_SIGNAL_TYPE_s can_cellVoltage3InvalidFlag
static void CAN_TxVoltageSetData(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 CAN_TxVoltage()
static const CAN_SIGNAL_TYPE_s can_cellVoltage3_mV
static const CAN_SIGNAL_TYPE_s can_cellVoltage2InvalidFlag
uint32_t CAN_TxVoltage(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 voltages
#define NUMBER_OF_MUX_VOLTAGES_PER_MESSAGE
static const CAN_SIGNAL_TYPE_s can_cellVoltage0_mV
static const CAN_SIGNAL_TYPE_s can_cellVoltage2_mV
static const CAN_SIGNAL_TYPE_s can_cellVoltageMultiplexer
static const CAN_SIGNAL_TYPE_s can_cellVoltage1_mV
static const CAN_SIGNAL_TYPE_s can_cellVoltage0InvalidFlag
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_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
DATA_BLOCK_CELL_VOLTAGE_s * pTableCellVoltage
Definition: can_cfg.h:313
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