foxBMS  1.2.1
The foxBMS Battery Management System API Documentation
can_cbs_tx_voltage.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_voltage.c
44  * @author foxBMS Team
45  * @date 2021-04-20 (date of creation)
46  * @updated 2021-07-29 (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 voltages
52  */
53 
54 /*========== Includes =======================================================*/
55 #include "can_cbs.h"
56 #include "can_helper.h"
57 
58 /*========== Macros and Definitions =========================================*/
59 
60 /*========== Static Constant and Variable Definitions =======================*/
61 /** the number of voltages per message-frame */
62 #define NUMBER_OF_MUX_VOLTAGES_PER_MESSAGE (4u)
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 cellVoltageMultiplexer = {7u, 8u, 1.0f, 0.0f, 0.0f, 1.0f};
70 static const CAN_SIGNAL_TYPE_s cellVoltage0InvalidFlag = {12u, 1u, 1.0f, 0.0f, 0.0f, 1.0f};
71 static const CAN_SIGNAL_TYPE_s cellVoltage1InvalidFlag = {13u, 1u, 1.0f, 0.0f, 0.0f, 1.0f};
72 static const CAN_SIGNAL_TYPE_s cellVoltage2InvalidFlag = {14u, 1u, 1.0f, 0.0f, 0.0f, 1.0f};
73 static const CAN_SIGNAL_TYPE_s cellVoltage3InvalidFlag = {15u, 1u, 1.0f, 0.0f, 0.0f, 1.0f};
74 static const CAN_SIGNAL_TYPE_s cellVoltage0_mV = {11u, 13u, 1.0f, 0.0f, 0.0f, 8192.0f};
75 static const CAN_SIGNAL_TYPE_s cellVoltage1_mV = {30u, 13u, 1.0f, 0.0f, 0.0f, 8192.0f};
76 static const CAN_SIGNAL_TYPE_s cellVoltage2_mV = {33u, 13u, 1.0f, 0.0f, 0.0f, 8192.0f};
77 static const CAN_SIGNAL_TYPE_s cellVoltage3_mV = {52u, 13u, 1.0f, 0.0f, 0.0f, 8192.0f};
78 
79 /*========== Extern Constant and Variable Definitions =======================*/
80 
81 /*========== Static Function Prototypes =====================================*/
82 /**
83  * @brief Helper function for CAN_TxVoltage()
84  *
85  * Used in the CAN_TxVoltage() callback to set
86  * invalid flag data and voltage data
87  * in the CAN frame.
88  *
89  * @param[in] muxId multiplexer value
90  * @param[in] pMessage pointer to CAN frame data
91  * @param[in] cellVoltageSignal signal characteristics for voltage data
92  * @param[in] cellVoltageInvalidFlagSignal signal characteristics for invalid flag data
93  * @param[in] endianness big or little endianness of data
94  * @param[in] kpkCanShim shim to the database entries
95  */
96 static void CAN_TxVoltageSetData(
97  uint8_t muxId,
98  uint64_t *pMessage,
99  CAN_SIGNAL_TYPE_s cellVoltageSignal,
100  CAN_SIGNAL_TYPE_s cellVoltageInvalidFlagSignal,
101  CAN_ENDIANNESS_e endianness,
102  const CAN_SHIM_s *const kpkCanShim);
103 
104 /*========== Static Function Implementations ================================*/
105 
107  uint8_t muxId,
108  uint64_t *pMessage,
109  CAN_SIGNAL_TYPE_s cellVoltageSignal,
110  CAN_SIGNAL_TYPE_s cellVoltageInvalidFlagSignal,
111  CAN_ENDIANNESS_e endianness,
112  const CAN_SHIM_s *const kpkCanShim) {
113  /* cell index must not be greater than the number of cells */
114  if (muxId < (BS_NR_OF_BAT_CELLS * BS_NR_OF_STRINGS)) {
115  /* Get string, module and cell number */
116  const uint8_t stringNumber = DATA_GetStringNumberFromVoltageIndex(muxId);
117  const uint8_t moduleNumber = DATA_GetModuleNumberFromVoltageIndex(muxId);
118  const uint8_t cellNumber = DATA_GetCellNumberFromVoltageIndex(muxId);
119 
120  uint32_t signalData_valid = 0u;
121  /* Valid bits data */
122  if ((kpkCanShim->pTableCellVoltage->invalidCellVoltage[stringNumber][moduleNumber] & (0x01u << cellNumber)) ==
123  0u) {
124  signalData_valid = 0u;
125  } else {
126  signalData_valid = 1u;
127  }
128  /* Set valid bit data in CAN frame */
130  pMessage,
131  cellVoltageInvalidFlagSignal.bitStart,
132  cellVoltageInvalidFlagSignal.bitLength,
133  signalData_valid,
134  endianness);
135 
136  /*Voltage data */
137  float signalData_mV =
138  (float)(kpkCanShim->pTableCellVoltage
139  ->cellVoltage_mV[stringNumber][(moduleNumber * BS_NR_OF_CELLS_PER_MODULE) + cellNumber]);
140  /* Apply offset and factor */
141  CAN_TxPrepareSignalData(&signalData_mV, cellVoltageSignal);
142  /* Set voltage data in CAN frame */
144  pMessage, cellVoltageSignal.bitStart, cellVoltageSignal.bitLength, (uint32_t)signalData_mV, endianness);
145  }
146 }
147 
148 /*========== Extern Function Implementations ================================*/
149 extern uint32_t CAN_TxVoltage(
150  uint32_t id,
151  uint8_t dlc,
152  CAN_ENDIANNESS_e endianness,
153  uint8_t *pCanData,
154  uint8_t *pMuxId,
155  const CAN_SHIM_s *const kpkCanShim) {
156  FAS_ASSERT(id < CAN_MAX_11BIT_ID); /* Currently standard ID, 11 bit */
157  FAS_ASSERT(dlc <= CAN_MAX_DLC); /* Currently max 8 bytes in a CAN frame */
158  FAS_ASSERT(pCanData != NULL_PTR);
159  FAS_ASSERT(pMuxId != NULL_PTR);
160  FAS_ASSERT(kpkCanShim != NULL_PTR);
161  uint64_t message = 0;
162 
163  /* Reset mux if maximum was reached */
164  if (*pMuxId >= (BS_NR_OF_STRINGS * BS_NR_OF_BAT_CELLS)) {
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 / NUMBER_OF_MUX_VOLTAGES_PER_MESSAGE;
175  &message, cellVoltageMultiplexer.bitStart, cellVoltageMultiplexer.bitLength, (uint32_t)signalData, endianness);
176 
177  /* Set other signals in CAN frame */
178  CAN_TxVoltageSetData(*pMuxId, &message, cellVoltage0_mV, cellVoltage0InvalidFlag, endianness, kpkCanShim);
179  /* Increment multiplexer for next cell */
180  (*pMuxId)++;
181  CAN_TxVoltageSetData(*pMuxId, &message, cellVoltage1_mV, cellVoltage1InvalidFlag, endianness, kpkCanShim);
182  /* Increment multiplexer for next cell */
183  (*pMuxId)++;
184  CAN_TxVoltageSetData(*pMuxId, &message, cellVoltage2_mV, cellVoltage2InvalidFlag, endianness, kpkCanShim);
185  /* Increment multiplexer for next cell */
186  (*pMuxId)++;
187  CAN_TxVoltageSetData(*pMuxId, &message, cellVoltage3_mV, cellVoltage3InvalidFlag, endianness, kpkCanShim);
188  /* Increment multiplexer for next cell */
189  (*pMuxId)++;
190 
191  /* All signal data copied in CAN frame, now copy data in the buffer that will be use to send the frame */
192  CAN_TxSetCanDataWithMessageData(message, pCanData, endianness);
193 
194  return 0;
195 }
196 
197 /*========== Externalized Static Function Implementations (Unit Test) =======*/
198 #ifdef UNITY_UNIT_TEST
199 
200 #endif
#define BS_NR_OF_STRINGS
#define BS_NR_OF_CELLS_PER_MODULE
number of battery cells per battery module (parallel cells are counted as one)
#define BS_NR_OF_BAT_CELLS
CAN callbacks header.
static const CAN_SIGNAL_TYPE_s cellVoltage1_mV
static const CAN_SIGNAL_TYPE_s cellVoltage3_mV
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 cellVoltage3InvalidFlag
static const CAN_SIGNAL_TYPE_s cellVoltage0_mV
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
static const CAN_SIGNAL_TYPE_s cellVoltage2InvalidFlag
#define NUMBER_OF_MUX_VOLTAGES_PER_MESSAGE
static const CAN_SIGNAL_TYPE_s cellVoltage2_mV
static const CAN_SIGNAL_TYPE_s cellVoltage1InvalidFlag
static const CAN_SIGNAL_TYPE_s cellVoltageMultiplexer
static const CAN_SIGNAL_TYPE_s cellVoltage0InvalidFlag
#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_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:239
#define NULL_PTR
Null pointer.
Definition: fstd_types.h:75
DATA_BLOCK_CELL_VOLTAGE_s * pTableCellVoltage
Definition: can_cfg.h:305
uint8_t bitStart
Definition: can_helper.h:80
uint8_t bitLength
Definition: can_helper.h:81
int16_t cellVoltage_mV[BS_NR_OF_STRINGS][BS_NR_OF_BAT_CELLS]
Definition: database_cfg.h:124
uint64_t invalidCellVoltage[BS_NR_OF_STRINGS][BS_NR_OF_MODULES]
Definition: database_cfg.h:126