foxBMS  1.6.0
The foxBMS Battery Management System API Documentation
can_cfg.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_cfg.h
44  * @author foxBMS Team
45  * @date 2019-12-04 (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 configuration for the CAN module
52  *
53  * @details The activation and the length of the message buffers as well as the
54  * number of the messages that are received are to be configured here.
55  *
56  */
57 
58 #ifndef FOXBMS__CAN_CFG_H_
59 #define FOXBMS__CAN_CFG_H_
60 
61 /*========== Includes =======================================================*/
62 
63 #include "HL_can.h"
64 
65 #include "database.h"
66 
67 #include <stdint.h>
68 
69 /*========== Macros and Definitions =========================================*/
70 
71 /* ****************************************************************************
72  * CAN NODE OPTIONS
73  *****************************************************************************/
74 /** CAN node configuration struct */
75 typedef struct {
76  canBASE_t *canNodeRegister; /*!< register on which the CAN interface is connected */
77 } CAN_NODE_s;
78 
79 /** CAN node defines @{*/
80 #define CAN_NODE_1 ((CAN_NODE_s *)&can_node1)
81 #define CAN_NODE_2 ((CAN_NODE_s *)&can_node2Isolated)
82 
83 #define CAN_NODE_DEBUG_MESSAGE (CAN_NODE_1)
84 #define CAN_NODE_IMD (CAN_NODE_1)
85 #define CAN_NODE_CURRENT_SENSOR (CAN_NODE_1)
86 /**@}*/
87 
88 /**
89  * Configuration of CAN transceiver pins to the respective port expander pins.
90  * @{
91  */
92 #define CAN1_ENABLE_PIN (PEX_PIN00)
93 #define CAN1_STANDBY_PIN (PEX_PIN01)
94 #define CAN2_ENABLE_PIN (PEX_PIN02)
95 #define CAN2_STANDBY_PIN (PEX_PIN03)
96 /**@}*/
97 
98 /** Maximum ID if 11 bits are used */
99 #define CAN_MAX_11BIT_ID (2048u)
100 /** Maximum length of Data Length Code */
101 #define CAN_MAX_DLC (8u)
102 /** Default DLC */
103 #define CAN_DEFAULT_DLC (8u)
104 /** Default DLC for messages that are defined by the foxBMS project, i.e., not
105  * defined by third party software and/or hardware. */
106 #define CAN_FOXBMS_MESSAGES_DEFAULT_DLC (8u)
107 
108 /* **************************************************************************************
109  * CAN BUFFER OPTIONS
110  *****************************************************************************************/
111 
112 /** Enum for byte order (endianness)
113  * \verbatim
114  * CAN data example:
115  *
116  * LittleEndian
117  * bitStart = 27; bitLength = 19
118  * DataLE: 45-44-43-42-41-40-39-38-37-36-35-34-33-32-31-30-29-28-27
119  * MSB LSB
120  *
121  * BigEndian
122  * bitStart = 21; bitLength = 19
123  * DataBE: 21-20-19-18-17-16-31-30-29-28-27-26-25-24-39-38-37-36-35
124  * MSB LSB
125  * |||
126  * Receive data on CAN bus
127  * |||
128  * |||
129  * \_/
130  * LE | BE
131  * CAN Data Byte 0 07 06 05 04 03 02 01 00 | CAN Data Byte 0 07 06 05 04 03 02 01 00
132  * CAN Data Byte 1 15 14 13 12 11 10 09 08 | CAN Data Byte 1 15 14 13 12 11 10 09 08
133  * CAN Data Byte 2 23 22 21 20 19 18 17 16 | CAN Data Byte 2 23 22 21-20-19-18-17-16 MSB
134  * CAN Data Byte 3 31-30-29-28-27 26 25 24 LSB | CAN Data Byte 3 31-30-29-28-27-26-25-24
135  * CAN Data Byte 4 39-38-37-36-35-34-33-32 | CAN Data Byte 4 39-38-37-36-35 34 33 32 LSB
136  * CAN Data Byte 5 47 46 45-44-43-42-41-40 MSB | CAN Data Byte 5 47 46 45 44 43 42 41 40
137  * CAN Data Byte 6 55 54 53 52 51 50 49 48 | CAN Data Byte 6 55 54 53 52 51 50 49 48
138  * CAN Data Byte 7 63 62 61 60 59 58 57 56 | CAN Data Byte 7 63 62 61 60 59 58 57 56
139  * |||
140  * Store received data in RAM
141  * |||
142  * |||
143  * \_/
144  * LE | BE
145  * CAN Data Byte 7 63 62 61 60 59 58 57 56 | CAN Data Byte 0 07 06 05 04 03 02 01 00 | RAM data[7]
146  * CAN Data Byte 6 55 54 53 52 51 50 49 48 | CAN Data Byte 1 15 14 13 12 11 10 09 08 | RAM data[6]
147  * CAN Data Byte 5 47 46 45-44-43-42-41-40 MSB | CAN Data Byte 2 23 22 21-20-19-18-17-16 MSB | RAM data[5]
148  * CAN Data Byte 4 39-38-37-36-35-34-33-32 | CAN Data Byte 3 31-30-29-28-27-26-25-24 | RAM data[4]
149  * CAN Data Byte 3 31-30-29-28-27 26 25 24 LSB | CAN Data Byte 4 39-38-37-36-35 34 33 32 LSB | RAM data[3]
150  * CAN Data Byte 2 23 22 21 20 19 18 17 16 | CAN Data Byte 5 47 46 45 44 43 42 41 40 | RAM data[2]
151  * CAN Data Byte 1 15 14 13 12 11 10 09 08 | CAN Data Byte 6 55 54 53 52 51 50 49 48 | RAM data[1]
152  * CAN Data Byte 0 07 06 05 04 03 02 01 00 | CAN Data Byte 7 63 62 61 60 59 58 57 56 | RAM data[0]
153  * DataLE = DataBE
154  * \endverbatim
155  */
156 typedef enum {
160 
161 /** CAN identifier type. Standard or extended identifier */
162 typedef enum {
167 
168 /** Buffer element used to store the ID and data of a CAN RX message */
169 typedef struct {
170  CAN_NODE_s *canNode; /*!< CAN node on which the message has been received */
171  uint32_t id; /*!< ID of the CAN message */
172  CAN_IDENTIFIER_TYPE_e idType; /*!< Standard or Extended identifier */
173  uint8_t data[CAN_MAX_DLC]; /*!< payload of the CAN message */
175 
176 /** composite type for storing and passing on the local database table handles */
177 typedef struct {
178  OS_QUEUE *pQueueImd; /*!< handle of the message queue */
179  DATA_BLOCK_CELL_VOLTAGE_s *pTableCellVoltage; /*!< database table with cell voltages */
180  DATA_BLOCK_CELL_TEMPERATURE_s *pTableCellTemperature; /*!< database table with cell temperatures */
181  DATA_BLOCK_CURRENT_SENSOR_s *pTableCurrentSensor; /*!< database table with current sensor measurements */
182  DATA_BLOCK_ERROR_STATE_s *pTableErrorState; /*!< database table with error state variables */
183  DATA_BLOCK_INSULATION_MONITORING_s *pTableInsulation; /*!< database table with insulation monitoring info */
184  DATA_BLOCK_MIN_MAX_s *pTableMinMax; /*!< database table with min/max values */
185  DATA_BLOCK_MOL_FLAG_s *pTableMol; /*!< database table with MOL flags */
186  DATA_BLOCK_MSL_FLAG_s *pTableMsl; /*!< database table with MSL flags */
187  DATA_BLOCK_OPEN_WIRE_s *pTableOpenWire; /*!< database table with open wire status */
188  DATA_BLOCK_PACK_VALUES_s *pTablePackValues; /*!< database table with pack values */
189  DATA_BLOCK_RSL_FLAG_s *pTableRsl; /*!< database table with RSL flags */
190  DATA_BLOCK_SOC_s *pTableSoc; /*!< database table with SOC values */
191  DATA_BLOCK_SOE_s *pTableSoe; /*!< database table with SOE values */
192  DATA_BLOCK_SOF_s *pTableSof; /*!< database table with SOF values */
193  DATA_BLOCK_SOH_s *pTableSoh; /*!< database table with SOH values */
194  DATA_BLOCK_STATE_REQUEST_s *pTableStateRequest; /*!< database table with state requests */
195  DATA_BLOCK_AEROSOL_SENSOR_s *pTableAerosolSensor; /*!< database table with aerosol sensor measurements */
196 } CAN_SHIM_s;
197 
198 /** definition of a CAN message (without data) */
199 typedef struct {
200  uint32_t id; /*!< message ID */
201  CAN_IDENTIFIER_TYPE_e idType; /*!< Standard or Extended identifier */
202  uint8_t dlc; /*!< data length */
203  CAN_ENDIANNESS_e endianness; /*!< Byte order (big or little endian) */
205 
206 /** timing information of a CAN TX message */
207 typedef struct {
208  uint32_t period; /*!< CAN message cycle time */
209  uint32_t phase; /*!< CAN message startup (first send) offset */
211 
212 /** timing information of a CAN RX message */
213 typedef struct {
214  uint32_t period; /*!< expected CAN message cycle time */
216 
217 /** type definition for tx callback functions used in CAN messages */
218 typedef uint32_t (*CAN_TxCallbackFunction_f)(
219  CAN_MESSAGE_PROPERTIES_s message,
220  uint8_t *canData,
221  uint8_t *pMuxId,
222  const CAN_SHIM_s *const kpkCanShim);
223 
224 /** type definition for rx callback functions used in CAN messages */
225 typedef uint32_t (*CAN_RxCallbackFunction_f)(
226  CAN_MESSAGE_PROPERTIES_s message,
227  const uint8_t *const kpkCanData,
228  const CAN_SHIM_s *const kpkCanShim);
229 
230 /** type definition for structure of a TX CAN message */
231 typedef struct {
232  CAN_NODE_s *canNode; /*!< CAN node on which the message is transmitted */
233  CAN_MESSAGE_PROPERTIES_s message; /*!< CAN message */
234  CAN_TX_MESSAGE_TIMING_s timing; /*!< time and phase */
235  CAN_TxCallbackFunction_f callbackFunction; /*!< CAN message callback after message is sent */
236  uint8_t *pMuxId; /*!< for multiplexed signals: callback can use this as pointer to a mux variable, NULL_PTR if
237  unused*/
239 
240 /* TODO: timing check not implemented for RX messages! */
241 /** type definition for structure of an RX CAN message */
242 typedef struct {
243  CAN_NODE_s *canNode; /*!< CAN node on which the message is received */
244  CAN_MESSAGE_PROPERTIES_s message; /*!< CAN message */
245  CAN_RX_MESSAGE_TIMING_s timing; /*!< time and phase */
246  CAN_RxCallbackFunction_f callbackFunction; /*!< CAN message callback after message is received */
248 
249 /*========== Extern Constant and Variable Declarations ======================*/
250 /** variable for storing and passing on the local database table handles */
251 extern const CAN_SHIM_s can_kShim;
252 
253 /** CAN node configurations for CAN1 and CAN2 (isolated) @{*/
254 extern const CAN_NODE_s can_node1;
255 extern const CAN_NODE_s can_node2Isolated;
256 /**@}*/
257 
258 /** CAN RX and TX message configuration structs @{*/
261 /**@}*/
262 
263 /** array length for transmission CAN0 message definition @{*/
264 extern const uint8_t can_txMessagesLength;
265 extern const uint8_t can_rxMessagesLength;
266 /**@}*/
267 
268 /*========== Extern Function Prototypes =====================================*/
269 
270 /*========== Externalized Static Functions Prototypes (Unit Test) ===========*/
271 #ifdef UNITY_UNIT_TEST
272 #endif
273 
274 #endif /* FOXBMS__CAN_CFG_H_ */
const CAN_RX_MESSAGE_TYPE_s can_rxMessages[]
Definition: can_cfg_rx.c:76
CAN_ENDIANNESS_e
Definition: can_cfg.h:156
@ CAN_LITTLE_ENDIAN
Definition: can_cfg.h:157
@ CAN_BIG_ENDIAN
Definition: can_cfg.h:158
uint32_t(* CAN_TxCallbackFunction_f)(CAN_MESSAGE_PROPERTIES_s message, uint8_t *canData, uint8_t *pMuxId, const CAN_SHIM_s *const kpkCanShim)
Definition: can_cfg.h:218
const uint8_t can_rxMessagesLength
Definition: can_cfg_rx.c:93
const uint8_t can_txMessagesLength
Definition: can_cfg_tx.c:103
const CAN_TX_MESSAGE_TYPE_s can_txMessages[]
Definition: can_cfg_tx.c:86
const CAN_SHIM_s can_kShim
Definition: can_cfg.c:105
const CAN_NODE_s can_node1
Definition: can_cfg.c:74
uint32_t(* CAN_RxCallbackFunction_f)(CAN_MESSAGE_PROPERTIES_s message, const uint8_t *const kpkCanData, const CAN_SHIM_s *const kpkCanShim)
Definition: can_cfg.h:225
#define CAN_MAX_DLC
Definition: can_cfg.h:101
const CAN_NODE_s can_node2Isolated
Definition: can_cfg.c:78
CAN_IDENTIFIER_TYPE_e
Definition: can_cfg.h:162
@ CAN_EXTENDED_IDENTIFIER_29_BIT
Definition: can_cfg.h:164
@ CAN_INVALID_TYPE
Definition: can_cfg.h:165
@ CAN_STANDARD_IDENTIFIER_11_BIT
Definition: can_cfg.h:163
Database module header.
CAN_NODE_s * canNode
Definition: can_cfg.h:170
CAN_IDENTIFIER_TYPE_e idType
Definition: can_cfg.h:172
CAN_IDENTIFIER_TYPE_e idType
Definition: can_cfg.h:201
CAN_ENDIANNESS_e endianness
Definition: can_cfg.h:203
canBASE_t * canNodeRegister
Definition: can_cfg.h:76
CAN_RX_MESSAGE_TIMING_s timing
Definition: can_cfg.h:245
CAN_RxCallbackFunction_f callbackFunction
Definition: can_cfg.h:246
CAN_MESSAGE_PROPERTIES_s message
Definition: can_cfg.h:244
CAN_NODE_s * canNode
Definition: can_cfg.h:243
DATA_BLOCK_ERROR_STATE_s * pTableErrorState
Definition: can_cfg.h:182
DATA_BLOCK_CELL_VOLTAGE_s * pTableCellVoltage
Definition: can_cfg.h:179
DATA_BLOCK_STATE_REQUEST_s * pTableStateRequest
Definition: can_cfg.h:194
DATA_BLOCK_AEROSOL_SENSOR_s * pTableAerosolSensor
Definition: can_cfg.h:195
DATA_BLOCK_PACK_VALUES_s * pTablePackValues
Definition: can_cfg.h:188
DATA_BLOCK_RSL_FLAG_s * pTableRsl
Definition: can_cfg.h:189
DATA_BLOCK_SOE_s * pTableSoe
Definition: can_cfg.h:191
DATA_BLOCK_CURRENT_SENSOR_s * pTableCurrentSensor
Definition: can_cfg.h:181
DATA_BLOCK_CELL_TEMPERATURE_s * pTableCellTemperature
Definition: can_cfg.h:180
DATA_BLOCK_MSL_FLAG_s * pTableMsl
Definition: can_cfg.h:186
DATA_BLOCK_MOL_FLAG_s * pTableMol
Definition: can_cfg.h:185
DATA_BLOCK_MIN_MAX_s * pTableMinMax
Definition: can_cfg.h:184
DATA_BLOCK_SOH_s * pTableSoh
Definition: can_cfg.h:193
DATA_BLOCK_OPEN_WIRE_s * pTableOpenWire
Definition: can_cfg.h:187
DATA_BLOCK_INSULATION_MONITORING_s * pTableInsulation
Definition: can_cfg.h:183
OS_QUEUE * pQueueImd
Definition: can_cfg.h:178
DATA_BLOCK_SOC_s * pTableSoc
Definition: can_cfg.h:190
DATA_BLOCK_SOF_s * pTableSof
Definition: can_cfg.h:192
CAN_TxCallbackFunction_f callbackFunction
Definition: can_cfg.h:235
CAN_MESSAGE_PROPERTIES_s message
Definition: can_cfg.h:233
uint8_t * pMuxId
Definition: can_cfg.h:236
CAN_TX_MESSAGE_TIMING_s timing
Definition: can_cfg.h:234
CAN_NODE_s * canNode
Definition: can_cfg.h:232