foxBMS  1.1.2
The foxBMS Battery Management System API Documentation
can_cbs_tx_system_values.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_system_values.c
44  * @author foxBMS Team
45  * @date 2021-07-21 (date of creation)
46  * @updated 2021-07-21 (date of last update)
47  * @ingroup DRIVER
48  * @prefix CAN
49  *
50  * @brief CAN driver Tx callback implementation
51  * @details CAN Tx callback for pack value and string value messages
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 
62 /*========== Extern Constant and Variable Definitions =======================*/
63 
64 /*========== Static Function Prototypes =====================================*/
65 
66 /*========== Static Function Implementations ================================*/
67 
68 /*========== Extern Function Implementations ================================*/
69 extern uint32_t CAN_TxPackValues(
70  uint32_t id,
71  uint8_t dlc,
72  CAN_ENDIANNESS_e endianness,
73  uint8_t *pCanData,
74  uint8_t *pMuxId,
75  const CAN_SHIM_s *const kpkCanShim) {
76  /* pMuxId is not used here, therefore has to be NULL_PTR */
77  FAS_ASSERT(pMuxId == NULL_PTR);
78 
79  FAS_ASSERT(id < CAN_MAX_11BIT_ID); /* Currently standard ID, 11 bit */
80  FAS_ASSERT(dlc <= CAN_MAX_DLC); /* Currently max 8 bytes in a CAN frame */
81  FAS_ASSERT(pCanData != NULL_PTR);
82  FAS_ASSERT(kpkCanShim != NULL_PTR);
83  uint64_t message = 0;
84 
85  /* Read database entry */
86  DATA_READ_DATA(kpkCanShim->pTablePackValues);
87 
88  /* AXIVION Disable Style Generic-NoMagicNumbers: Signal data defined in .dbc file. */
89  /* Battery voltage */
90  float signalData = kpkCanShim->pTablePackValues->batteryVoltage_mV;
91  float offset = 0.0f;
92  float factor = 0.01f; /* convert mV -> 100mV */
93  signalData = (signalData + offset) * factor;
94  uint64_t data = (uint64_t)signalData;
95  /* set data in CAN frame */
96  CAN_TxSetMessageDataWithSignalData(&message, 7u, 14u, data, endianness);
97 
98  /* System voltage */
99  signalData = kpkCanShim->pTablePackValues->highVoltageBusVoltage_mV;
100  offset = 0.0f;
101  factor = 0.01f; /* convert mV -> 100mV */
102  signalData = (signalData + offset) * factor;
103  data = (uint64_t)signalData;
104  /* set data in CAN frame */
105  CAN_TxSetMessageDataWithSignalData(&message, 9u, 14u, data, endianness);
106 
107  /* System current */
108  signalData = kpkCanShim->pTablePackValues->packCurrent_mA;
109  offset = 0.0f;
110  factor = 0.1f; /* convert mA -> 10mA */
111  signalData = (signalData + offset) * factor;
112  data = (int64_t)signalData;
113  /* set data in CAN frame */
114  CAN_TxSetMessageDataWithSignalData(&message, 41u, 18u, data, endianness);
115 
116  /* System power */
117  signalData = kpkCanShim->pTablePackValues->packPower_W;
118  offset = 0.0f;
119  factor = 0.1f; /* convert W -> 10W */
120  signalData = (signalData + offset) * factor;
121  data = (int64_t)signalData;
122  /* set data in CAN frame */
123  CAN_TxSetMessageDataWithSignalData(&message, 27u, 18u, data, endianness);
124  /* AXIVION Enable Style Generic-NoMagicNumbers: */
125 
126  /* now copy data in the buffer that will be used to send data */
127  CAN_TxSetCanDataWithMessageData(message, pCanData, endianness);
128 
129  return 0;
130 }
131 
132 extern uint32_t CAN_TxStringValues(
133  uint32_t id,
134  uint8_t dlc,
135  CAN_ENDIANNESS_e endianness,
136  uint8_t *pCanData,
137  uint8_t *pMuxId,
138  const CAN_SHIM_s *const kpkCanShim) {
139  FAS_ASSERT(id < CAN_MAX_11BIT_ID); /* Currently standard ID, 11 bit */
140  FAS_ASSERT(dlc <= CAN_MAX_DLC); /* Currently max 8 bytes in a CAN frame */
141  FAS_ASSERT(pCanData != NULL_PTR);
142  FAS_ASSERT(pMuxId != NULL_PTR);
143  FAS_ASSERT(*pMuxId < BS_NR_OF_STRINGS);
144  FAS_ASSERT(kpkCanShim != NULL_PTR);
145  uint64_t message = 0;
146  uint64_t data = 0;
147  float signalData = 0.0f;
148  float offset = 0.0f;
149  float factor = 0.0f;
150 
151  const uint8_t stringNumber = *pMuxId;
152 
153  /* First signal to transmit cell voltages: get database values */
154  if (stringNumber == 0u) {
155  /* Do not read pTableMsl and pTableErrorState as they already are read
156  * with a higher frequency from CAN_TxState callback */
157  DATA_READ_DATA(kpkCanShim->pTablePackValues);
158  }
159 
160  /* mux value */
161  data = (uint64_t)stringNumber;
162  /* set data in CAN frame */
163  /* AXIVION Disable Style Generic-NoMagicNumbers: Signal data defined in .dbc file. */
164  CAN_TxSetMessageDataWithSignalData(&message, 7u, 3u, data, endianness);
165 
166  /* String voltage */
167  signalData = (float)kpkCanShim->pTablePackValues->stringVoltage_mV[stringNumber];
168  offset = 0.0f;
169  factor = 0.1f; /* convert mV to 10mV */
170  signalData = (signalData + offset) * factor;
171  data = (int64_t)signalData;
172  /* set data in CAN frame */
173  CAN_TxSetMessageDataWithSignalData(&message, 4u, 17u, data, endianness);
174 
175  /* String current */
176  signalData = (float)kpkCanShim->pTablePackValues->stringCurrent_mA[stringNumber];
177  offset = 0.0f;
178  factor = 0.1f; /* convert mA to 10mA */
179  signalData = (signalData + offset) * factor;
180  data = (int64_t)signalData;
181  /* set data in CAN frame */
182  CAN_TxSetMessageDataWithSignalData(&message, 19u, 18u, data, endianness);
183 
184  /* String power */
185  signalData = (float)kpkCanShim->pTablePackValues->stringPower_W[stringNumber];
186  offset = 0.0f;
187  factor = 0.1f; /* convert W to 10W */
188  signalData = (signalData + offset) * factor;
189  data = (int64_t)signalData;
190  /* set data in CAN frame */
191  CAN_TxSetMessageDataWithSignalData(&message, 33u, 18u, data, endianness);
192  /* AXIVION Enable Style Generic-NoMagicNumbers: */
193 
194  /* now copy data in the buffer that will be used to send data */
195  CAN_TxSetCanDataWithMessageData(message, pCanData, endianness);
196 
197  /* Increment multiplexer for next cell */
198  (*pMuxId)++;
199 
200  /* Check mux value */
201  if (*pMuxId >= BS_NR_OF_STRINGS) {
202  *pMuxId = 0u;
203  }
204 
205  return 0;
206 }
207 
208 extern uint32_t CAN_TxStringValues2(
209  uint32_t id,
210  uint8_t dlc,
211  CAN_ENDIANNESS_e endianness,
212  uint8_t *pCanData,
213  uint8_t *pMuxId,
214  const CAN_SHIM_s *const kpkCanShim) {
215  FAS_ASSERT(id < CAN_MAX_11BIT_ID); /* Currently standard ID, 11 bit */
216  FAS_ASSERT(dlc <= CAN_MAX_DLC); /* Currently max 8 bytes in a CAN frame */
217  FAS_ASSERT(pCanData != NULL_PTR);
218  FAS_ASSERT(pMuxId != NULL_PTR);
219  FAS_ASSERT(*pMuxId < BS_NR_OF_STRINGS);
220  FAS_ASSERT(kpkCanShim != NULL_PTR);
221  uint64_t message = 0u;
222  uint64_t signalData = 0u;
223 
224  const uint8_t stringNumber = *pMuxId;
225 
226  /* First signal to transmit cell voltages: get database values */
227  if (stringNumber == 0u) {
228  /* Do not read pTableMsl and pTableErrorState as they already are read
229  * with a higher frequency from CAN_TxState callback */
230  DATA_READ_DATA(kpkCanShim->pTableCurrentSensor);
231  }
232 
233  /* mux value */
234  signalData = (uint64_t)stringNumber;
235  /* set data in CAN frame */
236  /* AXIVION Disable Style Generic-NoMagicNumbers: Signal data defined in .dbc file. */
237  CAN_TxSetMessageDataWithSignalData(&message, 7u, 4u, signalData, endianness);
238 
239  /* String voltage */
240  signalData = (int64_t)kpkCanShim->pTableCurrentSensor->energyCounter_Wh[stringNumber];
241  /* set data in CAN frame */
242  CAN_TxSetMessageDataWithSignalData(&message, 15u, 32u, signalData, endianness);
243  /* AXIVION Enable Style Generic-NoMagicNumbers: */
244 
245  /* now copy data in the buffer that will be used to send data */
246  CAN_TxSetCanDataWithMessageData(message, pCanData, endianness);
247 
248  /* Increment multiplexer for next cell */
249  (*pMuxId)++;
250 
251  /* Check mux value */
252  if (*pMuxId >= BS_NR_OF_STRINGS) {
253  *pMuxId = 0u;
254  }
255 
256  return 0;
257 }
258 
259 /*========== Externalized Static Function Implementations (Unit Test) =======*/
260 #ifdef UNITY_UNIT_TEST
261 
262 #endif
#define BS_NR_OF_STRINGS
CAN callbacks header.
uint32_t CAN_TxStringValues2(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 string values 2
uint32_t CAN_TxPackValues(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 pack values values
uint32_t CAN_TxStringValues(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 string values
#define CAN_MAX_11BIT_ID
Definition: can_cfg.h:68
enum CAN_ENDIANNESS CAN_ENDIANNESS_e
#define CAN_MAX_DLC
Definition: can_cfg.h:70
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
Headers for the helper functions for the CAN module.
#define DATA_READ_DATA(...)
Definition: database.h:76
#define FAS_ASSERT(x)
Assertion macro that asserts that x is true.
Definition: fassert.h:237
#define NULL_PTR
Null pointer.
Definition: fstd_types.h:66
DATA_BLOCK_CURRENT_SENSOR_s * pTableCurrentSensor
Definition: can_cfg.h:299
DATA_BLOCK_PACK_VALUES_s * pTablePackValues
Definition: can_cfg.h:302
int32_t energyCounter_Wh[BS_NR_OF_STRINGS]
Definition: database_cfg.h:232
int32_t stringCurrent_mA[BS_NR_OF_STRINGS]
Definition: database_cfg.h:204
int32_t stringPower_W[BS_NR_OF_STRINGS]
Definition: database_cfg.h:206
int32_t highVoltageBusVoltage_mV
Definition: database_cfg.h:198
int32_t stringVoltage_mV[BS_NR_OF_STRINGS]
Definition: database_cfg.h:202