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