foxBMS  1.6.0
The foxBMS Battery Management System API Documentation
can_cbs_tx_pack-values-p0.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_pack-values-p0.c
44  * @author foxBMS Team
45  * @date 2021-07-21 (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 pack value and string value messages
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 
67 /*========== Extern Constant and Variable Definitions =======================*/
68 
69 /*========== Static Function Prototypes =====================================*/
70 
71 /*========== Static Function Implementations ================================*/
72 
73 /*========== Extern Function Implementations ================================*/
74 extern uint32_t CANTX_PackValues(
76  uint8_t *pCanData,
77  uint8_t *pMuxId,
78  const CAN_SHIM_s *const kpkCanShim) {
79  /* pMuxId is not used here, therefore has to be NULL_PTR */
80  FAS_ASSERT(pMuxId == NULL_PTR);
81 
85  FAS_ASSERT(pCanData != NULL_PTR);
86  FAS_ASSERT(kpkCanShim != NULL_PTR);
87  uint64_t messageData = 0u;
88 
89  /* Read database entry */
90  DATA_READ_DATA(kpkCanShim->pTablePackValues);
91 
92  /* AXIVION Disable Style Generic-NoMagicNumbers: Signal data defined in .dbc file. */
93  /* Battery voltage */
94  float_t signalData = kpkCanShim->pTablePackValues->batteryVoltage_mV;
95  float_t offset = 0.0f;
96  float_t factor = 0.01f; /* convert mV to 100mV */
97  signalData = (signalData + offset) * factor;
98  uint64_t data = (uint64_t)signalData;
99  /* set data in CAN frame */
100  CAN_TxSetMessageDataWithSignalData(&messageData, 7u, 14u, data, message.endianness);
101 
102  /* System voltage */
103  signalData = kpkCanShim->pTablePackValues->highVoltageBusVoltage_mV;
104  offset = 0.0f;
105  factor = 0.01f; /* convert mV to 100mV */
106  signalData = (signalData + offset) * factor;
107  data = (uint64_t)signalData;
108  /* set data in CAN frame */
109  CAN_TxSetMessageDataWithSignalData(&messageData, 9u, 14u, data, message.endianness);
110 
111  /* System current */
112  signalData = kpkCanShim->pTablePackValues->packCurrent_mA;
113  offset = 0.0f;
114  factor = 0.1f; /* convert mA to 10mA */
115  signalData = (signalData + offset) * factor;
116  data = (int64_t)signalData;
117  /* set data in CAN frame */
118  CAN_TxSetMessageDataWithSignalData(&messageData, 41u, 18u, data, message.endianness);
119 
120  /* System power */
121  signalData = kpkCanShim->pTablePackValues->packPower_W;
122  offset = 0.0f;
123  factor = 0.1f; /* convert W to 10W */
124  signalData = (signalData + offset) * factor;
125  data = (int64_t)signalData;
126  /* set data in CAN frame */
127  CAN_TxSetMessageDataWithSignalData(&messageData, 27u, 18u, data, message.endianness);
128  /* AXIVION Enable Style Generic-NoMagicNumbers: */
129 
130  /* now copy data in the buffer that will be used to send data */
131  CAN_TxSetCanDataWithMessageData(messageData, pCanData, message.endianness);
132 
133  return 0;
134 }
135 
136 extern uint32_t CANTX_StringValuesP0(
137  CAN_MESSAGE_PROPERTIES_s message,
138  uint8_t *pCanData,
139  uint8_t *pMuxId,
140  const CAN_SHIM_s *const kpkCanShim) {
144  FAS_ASSERT(pCanData != NULL_PTR);
145  FAS_ASSERT(pMuxId != NULL_PTR);
146  FAS_ASSERT(*pMuxId < BS_NR_OF_STRINGS);
147  FAS_ASSERT(kpkCanShim != NULL_PTR);
148  uint64_t messageData = 0u;
149  uint64_t data = 0;
150  float_t signalData = 0.0f;
151  float_t offset = 0.0f;
152  float_t factor = 0.0f;
153 
154  const uint8_t stringNumber = *pMuxId;
155 
156  /* First signal to transmit cell voltages: get database values */
157  if (stringNumber == 0u) {
158  /* Do not read pTableMsl and pTableErrorState as they already are read
159  * with a higher frequency from CANTX_BmsState callback */
160  DATA_READ_DATA(kpkCanShim->pTablePackValues);
161  }
162 
163  /* mux value */
164  data = (uint64_t)stringNumber;
165  /* set data in CAN frame */
166  /* AXIVION Disable Style Generic-NoMagicNumbers: Signal data defined in .dbc file. */
167  CAN_TxSetMessageDataWithSignalData(&messageData, 7u, 3u, data, message.endianness);
168 
169  /* String voltage */
170  signalData = (float_t)kpkCanShim->pTablePackValues->stringVoltage_mV[stringNumber];
171  offset = 0.0f;
172  factor = 0.1f; /* convert mV to 10mV */
173  signalData = (signalData + offset) * factor;
174  data = (int64_t)signalData;
175  /* set data in CAN frame */
176  CAN_TxSetMessageDataWithSignalData(&messageData, 4u, 17u, data, message.endianness);
177 
178  /* String current */
179  signalData = (float_t)kpkCanShim->pTablePackValues->stringCurrent_mA[stringNumber];
180  offset = 0.0f;
181  factor = 0.1f; /* convert mA to 10mA */
182  signalData = (signalData + offset) * factor;
183  data = (int64_t)signalData;
184  /* set data in CAN frame */
185  CAN_TxSetMessageDataWithSignalData(&messageData, 19u, 18u, data, message.endianness);
186 
187  /* String power */
188  signalData = (float_t)kpkCanShim->pTablePackValues->stringPower_W[stringNumber];
189  offset = 0.0f;
190  factor = 0.1f; /* convert W to 10W */
191  signalData = (signalData + offset) * factor;
192  data = (int64_t)signalData;
193  /* set data in CAN frame */
194  CAN_TxSetMessageDataWithSignalData(&messageData, 33u, 18u, data, message.endianness);
195  /* AXIVION Enable Style Generic-NoMagicNumbers: */
196 
197  /* now copy data in the buffer that will be used to send data */
198  CAN_TxSetCanDataWithMessageData(messageData, pCanData, message.endianness);
199 
200  /* Increment multiplexer for next cell */
201  (*pMuxId)++;
202 
203  /* Check mux value */
204  if (*pMuxId >= BS_NR_OF_STRINGS) {
205  *pMuxId = 0u;
206  }
207 
208  return 0;
209 }
210 
211 extern uint32_t CANTX_StringValuesP1(
212  CAN_MESSAGE_PROPERTIES_s message,
213  uint8_t *pCanData,
214  uint8_t *pMuxId,
215  const CAN_SHIM_s *const kpkCanShim) {
219  FAS_ASSERT(pCanData != NULL_PTR);
220  FAS_ASSERT(pMuxId != NULL_PTR);
221  FAS_ASSERT(*pMuxId < BS_NR_OF_STRINGS);
222  FAS_ASSERT(kpkCanShim != NULL_PTR);
223  uint64_t messageData = 0u;
224  uint64_t signalData = 0u;
225 
226  const uint8_t stringNumber = *pMuxId;
227 
228  /* First signal to transmit cell voltages: get database values */
229  if (stringNumber == 0u) {
230  /* Do not read pTableMsl and pTableErrorState as they already are read
231  * with a higher frequency from CANTX_BmsState callback */
232  DATA_READ_DATA(kpkCanShim->pTableCurrentSensor);
233  }
234 
235  /* mux value */
236  signalData = (uint64_t)stringNumber;
237  /* set data in CAN frame */
238  /* AXIVION Disable Style Generic-NoMagicNumbers: Signal data defined in .dbc file. */
239  CAN_TxSetMessageDataWithSignalData(&messageData, 7u, 4u, signalData, message.endianness);
240 
241  /* String voltage */
242  OS_EnterTaskCritical(); /* this access has to be protected as it conflicts with the 1ms task */
243  signalData = (int64_t)kpkCanShim->pTableCurrentSensor->energyCounter_Wh[stringNumber];
245  /* set data in CAN frame */
246  CAN_TxSetMessageDataWithSignalData(&messageData, 15u, 32u, signalData, message.endianness);
247  /* AXIVION Enable Style Generic-NoMagicNumbers: */
248 
249  /* now copy data in the buffer that will be used to send data */
250  CAN_TxSetCanDataWithMessageData(messageData, pCanData, message.endianness);
251 
252  /* Increment multiplexer for next cell */
253  (*pMuxId)++;
254 
255  /* Check mux value */
256  if (*pMuxId >= BS_NR_OF_STRINGS) {
257  *pMuxId = 0u;
258  }
259 
260  return 0;
261 }
262 
263 /*========== Externalized Static Function Implementations (Unit Test) =======*/
264 #ifdef UNITY_UNIT_TEST
265 #endif
#define BS_NR_OF_STRINGS
Number of parallel strings in the battery pack.
CAN callbacks header.
uint32_t CANTX_StringValuesP0(CAN_MESSAGE_PROPERTIES_s message, uint8_t *pCanData, uint8_t *pMuxId, const CAN_SHIM_s *const kpkCanShim)
can tx callback function for string values
uint32_t CANTX_PackValues(CAN_MESSAGE_PROPERTIES_s message, uint8_t *pCanData, uint8_t *pMuxId, const CAN_SHIM_s *const kpkCanShim)
can tx callback function for pack values values
uint32_t CANTX_StringValuesP1(CAN_MESSAGE_PROPERTIES_s message, uint8_t *pCanData, uint8_t *pMuxId, const CAN_SHIM_s *const kpkCanShim)
can tx callback function for string values 2
#define CAN_FOXBMS_MESSAGES_DEFAULT_DLC
Definition: can_cfg.h:106
Header for the driver for the CAN module.
#define CANTX_PACK_VALUES_ID
#define CANTX_PACK_VALUES_ID_TYPE
#define CANTX_STRING_VALUES_P0_ID_TYPE
#define CANTX_STRING_VALUES_P1_ID
#define CANTX_STRING_VALUES_P1_ID_TYPE
#define CANTX_STRING_VALUES_P0_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
Headers for the helper functions for the CAN module.
#define DATA_READ_DATA(...)
Definition: database.h:86
#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
void OS_ExitTaskCritical(void)
Exit Critical interface function for use in FreeRTOS-Tasks and FreeRTOS-ISR.
Definition: os_freertos.c:154
void OS_EnterTaskCritical(void)
Enter Critical interface function for use in FreeRTOS-Tasks and FreeRTOS-ISR.
Definition: os_freertos.c:150
CAN_IDENTIFIER_TYPE_e idType
Definition: can_cfg.h:201
CAN_ENDIANNESS_e endianness
Definition: can_cfg.h:203
DATA_BLOCK_PACK_VALUES_s * pTablePackValues
Definition: can_cfg.h:188
DATA_BLOCK_CURRENT_SENSOR_s * pTableCurrentSensor
Definition: can_cfg.h:181
int32_t energyCounter_Wh[BS_NR_OF_STRINGS]
Definition: database_cfg.h:236
int32_t stringVoltage_mV[BS_NR_OF_STRINGS]
Definition: database_cfg.h:206
int32_t stringPower_W[BS_NR_OF_STRINGS]
Definition: database_cfg.h:210
int32_t stringCurrent_mA[BS_NR_OF_STRINGS]
Definition: database_cfg.h:208