foxBMS  1.6.0
The foxBMS Battery Management System API Documentation
can_helper.h
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_helper.h
44  * @author foxBMS Team
45  * @date 2021-04-22 (date of creation)
46  * @updated 2023-10-12 (date of last update)
47  * @version v1.6.0
48  * @ingroup DRIVERS
49  * @prefix CAN
50  *
51  * @brief Headers for the helper functions for the CAN module
52  *
53  *
54  */
55 
56 #ifndef FOXBMS__CAN_HELPER_H_
57 #define FOXBMS__CAN_HELPER_H_
58 
59 /*========== Includes =======================================================*/
60 
61 #include "can_cfg.h"
62 
63 #include <math.h>
64 #include <stdint.h>
65 
66 /*========== Macros and Definitions =========================================*/
67 
68 /** position for CAN byte 0 to 7
69  * @{
70  */
71 #define CAN_BYTE_0_POSITION (0u)
72 #define CAN_BYTE_1_POSITION (1u)
73 #define CAN_BYTE_2_POSITION (2u)
74 #define CAN_BYTE_3_POSITION (3u)
75 #define CAN_BYTE_4_POSITION (4u)
76 #define CAN_BYTE_5_POSITION (5u)
77 #define CAN_BYTE_6_POSITION (6u)
78 #define CAN_BYTE_7_POSITION (7u)
79 /**@}*/
80 
81 /** type definition for fields of a CAN signal */
82 typedef struct {
83  uint8_t bitStart; /*!< start bit of signal in CAN message */
84  uint8_t bitLength; /*!< length bit of signal in CAN message */
85  float_t factor; /*!< scaling factor applied to data */
86  float_t offset; /*!< offset applied to data */
87  float_t min; /*!< minimum allowed value for signal */
88  float_t max; /*!< maximum allowed value for signal */
90 
91 /*========== Extern Constant and Variable Declarations ======================*/
92 
93 /*========== Extern Function Prototypes =====================================*/
94 
95 /**
96  * @brief Prepare signal data.
97  * This function takes the signal data and
98  * applies factor, applies offset and compares
99  * with signal minimum and maximum values.
100  * @param[in,out] pSignal signal data
101  * @param[in] signalProperties signal properties (factor, offset, min, max)
102  */
103 extern void CAN_TxPrepareSignalData(float_t *pSignal, CAN_SIGNAL_TYPE_s signalProperties);
104 
105 /**
106  * @brief Convert raw signal data.
107  * This function takes the raw signal data and
108  * applies offset and factor to convert
109  * the raw value.
110  * @param[out] pSignalConverted converted signal data
111  * @param[in] signalRaw raw signal data
112  * @param[in] signalProperties signal properties (factor, offset, min, max)
113  */
114 extern void CAN_RxConvertRawSignalData(
115  float_t *pSignalConverted,
116  float_t signalRaw,
117  CAN_SIGNAL_TYPE_s signalProperties);
118 
119 /**
120  * @brief Puts CAN signal data in a 64-bit variable.
121  * This function is used to compose a 64-bit CAN message.
122  * It takes signal data, signal bit start, signal bit length
123  * and puts the data in the 64-bit variable.
124  * @param[out] pMessage 64-bit variable containing the CAN data
125  * @param[in] bitStart starting bit were the signal data must be put
126  * @param[in] bitLength length of the signal data to be put in CAN message
127  * @param[in] canSignal signal data to be put in message
128  * @param[in] endianness big or little endianness of data
129  */
131  uint64_t *pMessage,
132  uint64_t bitStart,
133  uint8_t bitLength,
134  uint64_t canSignal,
135  CAN_ENDIANNESS_e endianness);
136 
137 /**
138  * @brief Copy CAN data from a 64-bit variable to 8 bytes.
139  * This function is used to copy a 64-bit CAN message to 8 bytes.
140  * @param[in] message 64-bit variable containing the CAN data
141  * @param[out] pCanData 8 bytes where the data is copied
142  * @param[in] endianness big or little endianness of data
143  */
144 extern void CAN_TxSetCanDataWithMessageData(uint64_t message, uint8_t *pCanData, CAN_ENDIANNESS_e endianness);
145 
146 /**
147  * @brief Gets CAN signal data from a 64-bit variable.
148  * This function is used to get signal data from a 64-bit
149  * CAN message.
150  * It takes signal bit start, signal bit length and extract
151  * signal data from the the 64-bit variable.
152  * @param[in] message 64-bit variable containing the CAN data
153  * @param[in] bitStart starting bit were the signal data must be put
154  * @param[in] bitLength length of the signal data to be put in CAN message
155  * @param[out] pCanSignal signal data to be retrieved from message
156  * @param[in] endianness big or little endianness of data
157  */
159  uint64_t message,
160  uint64_t bitStart,
161  uint8_t bitLength,
162  uint64_t *pCanSignal,
163  CAN_ENDIANNESS_e endianness);
164 
165 /**
166  * @brief Copy CAN data from 8 bytes to a 64-bit variable.
167  * @details This function is used to copy data from a 64-bit variable to 8
168  * bytes.
169  * @param[in] pMessage 64-bit where the data is copied
170  * @param[out] kpkCanData 8 bytes containing the data
171  * @param[in] endianness big or little endianness of data
172  */
174  uint64_t *pMessage,
175  const uint8_t *const kpkCanData,
176  CAN_ENDIANNESS_e endianness);
177 
178 /**
179  * @brief Transform a bool to a bit (set if true)
180  * @param[in] input input boolean (true will become 1u)
181  * @returns unsigned integer indicating the state of the input
182  */
183 extern uint8_t CAN_ConvertBooleanToInteger(bool input);
184 
185 /*========== Externalized Static Functions Prototypes (Unit Test) ===========*/
186 #ifdef UNITY_UNIT_TEST
187 #endif
188 
189 #endif /* FOXBMS__CAN_HELPER_H_ */
Headers for the configuration for the CAN module.
CAN_ENDIANNESS_e
Definition: can_cfg.h:156
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
void CAN_RxGetMessageDataFromCanData(uint64_t *pMessage, const uint8_t *const kpkCanData, CAN_ENDIANNESS_e endianness)
Copy CAN data from 8 bytes to a 64-bit variable.
Definition: can_helper.c:295
uint8_t CAN_ConvertBooleanToInteger(bool input)
Transform a bool to a bit (set if true)
Definition: can_helper.c:342
void CAN_TxPrepareSignalData(float_t *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:145
void CAN_RxGetSignalDataFromMessageData(uint64_t message, uint64_t bitStart, uint8_t bitLength, uint64_t *pCanSignal, CAN_ENDIANNESS_e endianness)
Gets CAN signal data from a 64-bit variable. This function is used to get signal data from a 64-bit C...
Definition: can_helper.c:255
void CAN_RxConvertRawSignalData(float_t *pSignalConverted, float_t signalRaw, CAN_SIGNAL_TYPE_s signalProperties)
Convert raw signal data. This function takes the raw signal data and applies offset and factor to con...
Definition: can_helper.c:164
uint8_t bitLength
Definition: can_helper.h:84
uint8_t bitStart
Definition: can_helper.h:83