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