foxBMS  1.3.0
The foxBMS Battery Management System API Documentation
can_cfg.h
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_cfg.h
44  * @author foxBMS Team
45  * @date 2019-12-04 (date of creation)
46  * @updated 2022-05-30 (date of last update)
47  * @version v1.3.0
48  * @ingroup DRIVERS
49  * @prefix CAN
50  *
51  * @brief Headers for the configuration for the CAN module
52  *
53  * The activation and the length of the message buffers as well as the number of
54  * 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 #include "general.h"
63 
64 #include "HL_can.h"
65 
66 #include "database.h"
67 
68 /*========== Macros and Definitions =========================================*/
69 
70 /** register on which the CAN interface is connected @{*/
71 #define CAN1_NODE (canREG1)
72 #define CAN2_NODE (canREG2)
73 /**@}*/
74 
75 /**
76  * Configuration of CAN transceiver pins to the respective port expander pins.
77  * @{
78  */
79 #define CAN1_ENABLE_PIN (PEX_PIN00)
80 #define CAN1_STANDBY_PIN (PEX_PIN01)
81 #define CAN2_ENABLE_PIN (PEX_PIN02)
82 #define CAN2_STANDBY_PIN (PEX_PIN03)
83 /**@}*/
84 
85 /** Maximum ID if 11 bits are used */
86 #define CAN_MAX_11BIT_ID (2048u)
87 /** Maximum length of Data Length Code */
88 #define CAN_MAX_DLC (8u)
89 
90 /** Default DLC */
91 #define CAN_DLC (8u)
92 
93 /** TX messages - pack related */
94 
95 /** CAN message ID for boot message */
96 #define CAN_ID_BOOT_MESSAGE (0x199u)
97 /** CAN message ID for die id message */
98 #define CAN_ID_DIE_ID (0x227u)
99 /** CAN message ID to send state */
100 #define CAN_ID_TX_STATE (0x220u)
101 /** Periodicity of CAN state messages in ms */
102 #define CAN_TX_STATE_PERIOD_MS (100u)
103 /** Phase of CAN state messages in ms */
104 #define CAN_TX_STATE_PHASE_MS (0u)
105 /** CAN message ID to send detail state */
106 #define CAN_ID_TX_DETAIL_STATE (0x226u)
107 /** Periodicity of CAN detail state messages in ms */
108 #define CAN_TX_DETAIL_STATE_PERIOD_MS (1000u)
109 /** Phase of CAN detail state messages in ms */
110 #define CAN_TX_DETAIL_STATE_PHASE_MS (100u)
111 
112 /** CAN message ID to send voltages */
113 #define CAN_ID_TX_VOLTAGES (0x240u)
114 /** Periodicity of CAN voltage messages in ms */
115 #define CAN_TX_VOLTAGES_PERIOD_MS (100u)
116 /** Phase of CAN voltage messages in ms */
117 #define CAN_TX_VOLTAGES_PHASE_MS (10u)
118 
119 /** CAN message ID to send temperatures */
120 #define CAN_ID_TX_TEMPERATURES (0x250u)
121 /** Periodicity of CAN temperature messages in ms */
122 #define CAN_TX_TEMPERATURES_PERIOD_MS (200u)
123 /** Phase of CAN temperature messages in ms */
124 #define CAN_TX_TEMPERATURES_PHASE_MS (20u)
125 
126 /** CAN message ID to send limit values */
127 #define CAN_ID_TX_LIMIT_VALUES (0x224u)
128 /** Periodicity of CAN limit messages in ms */
129 #define CAN_TX_LIMIT_VALUES_PERIOD_MS (100u)
130 /** Phase of CAN limit messages in ms */
131 #define CAN_TX_LIMIT_VALUES_PHASE_MS (30u)
132 
133 /** CAN message ID to send minimum and maximum values */
134 #define CAN_ID_TX_MINIMUM_MAXIMUM_VALUES (0x223u)
135 /** Periodicity of CAN minimum and maximum value messages in ms */
136 #define CAN_TX_MINIMUM_MAXIMUM_VALUES_PERIOD_MS (100u)
137 /** Phase of CAN minimum and maximum value messages in ms */
138 #define CAN_TX_MINIMUM_MAXIMUM_VALUES_PHASE_MS (40u)
139 
140 /** CAN message ID to send pack state estimation values */
141 #define CAN_ID_TX_PACK_STATE_ESTIMATION (0x225u)
142 /** Periodicity of CAN pack state estimation messages in ms */
143 #define CAN_TX_PACK_STATE_ESTIMATION_PERIOD_MS (1000u)
144 /** Phase of CAN pack state estimation messages in ms */
145 #define CAN_TX_PACK_STATE_ESTIMATION_PHASE_MS (50u)
146 
147 /** CAN message ID to send pack values */
148 #define CAN_ID_TX_PACK_VALUES (0x222u)
149 /** Periodicity of CAN pack values messages in ms */
150 #define CAN_TX_PACK_VALUES_PERIOD_MS (100u)
151 /** Phase of CAN pack values messages in ms */
152 #define CAN_TX_PACK_VALUES_PHASE_MS (60u)
153 
154 /** TX messages - string related */
155 
156 /** CAN message ID to send string state */
157 #define CAN_ID_TX_STRING_STATE (0x221u)
158 /** Periodicity of CAN string state messages in ms */
159 #define CAN_TX_STRING_STATE_PERIOD_MS (100u)
160 /** Phase of CAN string state messages in ms */
161 #define CAN_TX_STRING_STATE_PHASE_MS (70u)
162 
163 /** CAN message ID to send string values */
164 #define CAN_ID_TX_STRING_VALUES (0x280u)
165 /** Periodicity of CAN string state messages in ms */
166 #define CAN_TX_STRING_VALUES_PERIOD_MS (100u)
167 /** Phase of CAN string state messages in ms */
168 #define CAN_TX_STRING_VALUES_PHASE_MS (80u)
169 
170 /** CAN message ID to send string minimum and maximum values */
171 #define CAN_ID_TX_STRING_MINIMUM_MAXIMUM (0x281u)
172 /** Periodicity of CAN string state messages in ms */
173 #define CAN_TX_STRING_MINIMUM_MAXIMUM_PERIOD_MS (100u)
174 /** Phase of CAN string state messages in ms */
175 #define CAN_TX_STRING_MINIMUM_MAXIMUM_PHASE_MS (90u)
176 
177 /** CAN message ID to send string minimum and maximum values */
178 #define CAN_ID_TX_STRING_STATE_ESTIMATION (0x282u)
179 /** Periodicity of CAN string state messages in ms */
180 #define CAN_TX_STRING_STATE_ESTIMATION_PERIOD_MS (1000u)
181 /** Phase of CAN string state messages in ms */
182 #define CAN_TX_STRING_STATE_ESTIMATION_PHASE_MS (0u)
183 
184 /** CAN message ID to send string values 2 */
185 #define CAN_ID_TX_STRING_VALUES_2 (0x283u)
186 /** Periodicity of CAN string state messages in ms */
187 #define CAN_TX_STRING_VALUES_2_PERIOD_MS (100u)
188 /** Phase of CAN string state messages in ms */
189 #define CAN_TX_STRING_VALUES_2_PHASE_MS (10u)
190 
191 /** RX messages */
192 
193 /** CAN message ID to perform a software reset */
194 #define CAN_ID_SOFTWARE_RESET (0x95u)
195 
196 /** CAN message ID to perform a state request */
197 #define CAN_ID_COMMAND (0x230u)
198 
199 /** CAN message ID for debug message */
200 #define CAN_ID_DEBUG (0x200u)
201 
202 /** CAN message ID to get software version */
203 #define CAN_ID_SW_VERSION (0x777u)
204 
205 /**
206  * -------------------------CAUTION-------------------------
207  * The 3 following defines are used by the insulation monitoring device (IMD).
208  * If they are changed, the IMD will not work anymore
209  * -------------------------CAUTION-------------------------
210  */
211 /** CAN message ID for info message from iso165c */
212 #define CAN_ID_IMD_INFO (0x37u)
213 /** CAN message ID for request message to iso165c */
214 #define CAN_ID_IMD_REQUEST (0x22u)
215 /** CAN message ID for response message from iso165c */
216 #define CAN_ID_IMD_RESPONSE (0x23u)
217 
218 /** IDs for the messages from the current sensors */
219 /** String 0 @{*/
220 #define CAN_ID_STRING0_CURRENT (0x521u)
221 #define CAN_ID_STRING0_VOLTAGE1 (0x522u)
222 #define CAN_ID_STRING0_VOLTAGE2 (0x523u)
223 #define CAN_ID_STRING0_VOLTAGE3 (0x524u)
224 #define CAN_ID_STRING0_TEMPERATURE (0x525u)
225 #define CAN_ID_STRING0_POWER (0x526u)
226 #define CAN_ID_STRING0_CURRENT_COUNTER (0x527u)
227 #define CAN_ID_STRING0_ENERGY_COUNTER (0x528u)
228 /**@}
229  * String 1 @{*/
230 #define CAN_ID_STRING1_CURRENT (0x621u)
231 #define CAN_ID_STRING1_VOLTAGE1 (0x622u)
232 #define CAN_ID_STRING1_VOLTAGE2 (0x623u)
233 #define CAN_ID_STRING1_VOLTAGE3 (0x624u)
234 #define CAN_ID_STRING1_TEMPERATURE (0x625u)
235 #define CAN_ID_STRING1_POWER (0x626u)
236 #define CAN_ID_STRING1_CURRENT_COUNTER (0x627u)
237 #define CAN_ID_STRING1_ENERGY_COUNTER (0x628u)
238 /**@}
239  * String 2 @{*/
240 #define CAN_ID_STRING2_CURRENT (0x721u)
241 #define CAN_ID_STRING2_VOLTAGE1 (0x722u)
242 #define CAN_ID_STRING2_VOLTAGE2 (0x723u)
243 #define CAN_ID_STRING2_VOLTAGE3 (0x724u)
244 #define CAN_ID_STRING2_TEMPERATURE (0x725u)
245 #define CAN_ID_STRING2_POWER (0x726u)
246 #define CAN_ID_STRING2_CURRENT_COUNTER (0x727u)
247 #define CAN_ID_STRING2_ENERGY_COUNTER (0x728u)
248 /**@}*/
249 
250 /* **************************************************************************************
251  * CAN BUFFER OPTIONS
252  *****************************************************************************************/
253 
254 /** Enum for byte order (endianness)
255  * \verbatim
256  * CAN data example:
257  *
258  * LittleEndian
259  * bitStart = 27; bitLength = 19
260  * DataLE: 45-44-43-42-41-40-39-38-37-36-35-34-33-32-31-30-29-28-27
261  * MSB LSB
262  *
263  * BigEndian
264  * bitStart = 21; bitLength = 19
265  * DataBE: 21-20-19-18-17-16-31-30-29-28-27-26-25-24-39-38-37-36-35
266  * MSB LSB
267  * |||
268  * Receive data on CAN bus
269  * |||
270  * |||
271  * \_/
272  * LE | BE
273  * CAN Data Byte 0 07 06 05 04 03 02 01 00 | CAN Data Byte 0 07 06 05 04 03 02 01 00
274  * CAN Data Byte 1 15 14 13 12 11 10 09 08 | CAN Data Byte 1 15 14 13 12 11 10 09 08
275  * 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
276  * 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
277  * 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
278  * 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
279  * CAN Data Byte 6 55 54 53 52 51 50 49 48 | CAN Data Byte 6 55 54 53 52 51 50 49 48
280  * CAN Data Byte 7 63 62 61 60 59 58 57 56 | CAN Data Byte 7 63 62 61 60 59 58 57 56
281  * |||
282  * Store received data in RAM
283  * |||
284  * |||
285  * \_/
286  * LE | BE
287  * 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]
288  * 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]
289  * 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]
290  * 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]
291  * 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]
292  * 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]
293  * 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]
294  * 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]
295  * DataLE = DataBE
296  * \endverbatim
297  */
298 typedef enum {
302 
303 /** Buffer element used to store the ID and data of a CAN RX message */
304 typedef struct {
305  canBASE_t *canNode; /*!< CAN node on which the message has been received */
306  uint32_t id; /*!< ID of the CAN message */
307  uint8_t data[CAN_MAX_DLC]; /*!< payload of the CAN message */
309 
310 /** composite type for storing and passing on the local database table handles */
311 typedef struct {
312  OS_QUEUE *pQueueImd; /*!< handle of the message queue */
313  DATA_BLOCK_CELL_VOLTAGE_s *pTableCellVoltage; /*!< database table with cell voltages */
314  DATA_BLOCK_CELL_TEMPERATURE_s *pTableCellTemperature; /*!< database table with cell temperatures */
315  DATA_BLOCK_MIN_MAX_s *pTableMinMax; /*!< database table with min/max values */
316  DATA_BLOCK_CURRENT_SENSOR_s *pTableCurrentSensor; /*!< database table with current sensor measurements */
317  DATA_BLOCK_OPEN_WIRE_s *pTableOpenWire; /*!< database table with open wire status */
318  DATA_BLOCK_STATEREQUEST_s *pTableStateRequest; /*!< database table with state requests */
319  DATA_BLOCK_PACK_VALUES_s *pTablePackValues; /*!< database table with pack values */
320  DATA_BLOCK_SOF_s *pTableSof; /*!< database table with SOF values */
321  DATA_BLOCK_SOX_s *pTableSox; /*!< database table with SOC and SOE values */
322  DATA_BLOCK_ERRORSTATE_s *pTableErrorState; /*!< database table with error state variables */
323  DATA_BLOCK_INSULATION_MONITORING_s *pTableInsulation; /*!< database table with insulation monitoring info */
324  DATA_BLOCK_MSL_FLAG_s *pTableMsl; /*!< database table with MSL flags */
325  DATA_BLOCK_RSL_FLAG_s *pTableRsl; /*!< database table with RSL flags */
326  DATA_BLOCK_MOL_FLAG_s *pTableMol; /*!< database table with MOL flags */
327 } CAN_SHIM_s;
328 
329 /** type definition for tx callback functions used in CAN messages */
330 typedef uint32_t (*CAN_TxCallbackFunction_f)(
331  uint32_t ID,
332  uint8_t DLC,
333  CAN_ENDIANNESS_e endianness,
334  uint8_t *candata,
335  uint8_t *pMuxId,
336  const CAN_SHIM_s *const kpkCanShim);
337 
338 /** type definition for rx callback functions used in CAN messages */
339 typedef uint32_t (*CAN_RxCallbackFunction_f)(
340  uint32_t ID,
341  uint8_t DLC,
342  CAN_ENDIANNESS_e endianness,
343  const uint8_t *const kpkCanData,
344  const CAN_SHIM_s *const kpkCanShim);
345 
346 /** type definition for structure of a TX CAN message */
347 typedef struct {
348  canBASE_t *canNode; /*!< CAN node on which the message is transmitted */
349  uint32_t id; /*!< CAN message id */
350  uint8_t dlc; /*!< CAN message data length code */
351  uint32_t repetitionTime; /*!< CAN message cycle time */
352  uint32_t repetitionPhase; /*!< CAN message startup (first send) offset */
353  CAN_ENDIANNESS_e endianness; /*!< Byte order (big or little endian) */
354  CAN_TxCallbackFunction_f callbackFunction; /*!< CAN message callback after message is sent */
355  uint8_t *pMuxId; /*!< for multiplexed signals: callback can use this as pointer to a mux variable, NULL_PTR if
356  unused*/
358 
359 /** type definition for structure of an RX CAN message */
360 typedef struct {
361  canBASE_t *canNode; /*!< CAN node on which the message is received */
362  uint32_t id; /*!< message ID */
363  uint8_t dlc; /*!< data length */
364  CAN_ENDIANNESS_e endianness; /*!< Byte order (big or little endian) */
365  CAN_RxCallbackFunction_f callbackFunction; /*!< CAN message callback after message is received */
367 
368 /** variable for storing and passing on the local database table handles */
369 extern const CAN_SHIM_s can_kShim;
370 
371 /*========== Extern Constant and Variable Declarations ======================*/
372 extern const CAN_MSG_TX_TYPE_s can_txMessages[];
373 extern const CAN_MSG_RX_TYPE_s can_rxMessages[];
374 
375 /** array length for transmission CAN0 message definition @{*/
376 extern const uint8_t can_txLength;
377 extern const uint8_t can_rxLength;
378 /**@}*/
379 
380 /*========== Extern Function Prototypes =====================================*/
381 
382 /*========== Externalized Static Function Implementations (Unit Test) =======*/
383 
384 /*========== Externalized Static Functions Prototypes (Unit Test) ===========*/
385 
386 #endif /* FOXBMS__CAN_CFG_H_ */
const CAN_MSG_RX_TYPE_s can_rxMessages[]
Definition: can_cfg.c:202
CAN_ENDIANNESS_e
Definition: can_cfg.h:298
@ CAN_LITTLE_ENDIAN
Definition: can_cfg.h:299
@ CAN_BIG_ENDIAN
Definition: can_cfg.h:300
uint32_t(* CAN_RxCallbackFunction_f)(uint32_t ID, uint8_t DLC, CAN_ENDIANNESS_e endianness, const uint8_t *const kpkCanData, const CAN_SHIM_s *const kpkCanShim)
Definition: can_cfg.h:339
const uint8_t can_txLength
Definition: can_cfg.c:346
const CAN_MSG_TX_TYPE_s can_txMessages[]
Definition: can_cfg.c:89
const CAN_SHIM_s can_kShim
Definition: can_cfg.c:369
uint32_t(* CAN_TxCallbackFunction_f)(uint32_t ID, uint8_t DLC, CAN_ENDIANNESS_e endianness, uint8_t *candata, uint8_t *pMuxId, const CAN_SHIM_s *const kpkCanShim)
Definition: can_cfg.h:330
const uint8_t can_rxLength
Definition: can_cfg.c:347
#define CAN_MAX_DLC
Definition: can_cfg.h:88
Database module header.
General macros and definitions for the whole platform.
canBASE_t * canNode
Definition: can_cfg.h:305
CAN_RxCallbackFunction_f callbackFunction
Definition: can_cfg.h:365
canBASE_t * canNode
Definition: can_cfg.h:361
uint32_t id
Definition: can_cfg.h:362
CAN_ENDIANNESS_e endianness
Definition: can_cfg.h:364
CAN_TxCallbackFunction_f callbackFunction
Definition: can_cfg.h:354
uint8_t * pMuxId
Definition: can_cfg.h:355
uint32_t repetitionTime
Definition: can_cfg.h:351
canBASE_t * canNode
Definition: can_cfg.h:348
uint32_t id
Definition: can_cfg.h:349
CAN_ENDIANNESS_e endianness
Definition: can_cfg.h:353
uint32_t repetitionPhase
Definition: can_cfg.h:352
DATA_BLOCK_STATEREQUEST_s * pTableStateRequest
Definition: can_cfg.h:318
DATA_BLOCK_CELL_VOLTAGE_s * pTableCellVoltage
Definition: can_cfg.h:313
DATA_BLOCK_PACK_VALUES_s * pTablePackValues
Definition: can_cfg.h:319
DATA_BLOCK_RSL_FLAG_s * pTableRsl
Definition: can_cfg.h:325
DATA_BLOCK_SOX_s * pTableSox
Definition: can_cfg.h:321
DATA_BLOCK_CURRENT_SENSOR_s * pTableCurrentSensor
Definition: can_cfg.h:316
DATA_BLOCK_ERRORSTATE_s * pTableErrorState
Definition: can_cfg.h:322
DATA_BLOCK_CELL_TEMPERATURE_s * pTableCellTemperature
Definition: can_cfg.h:314
DATA_BLOCK_MSL_FLAG_s * pTableMsl
Definition: can_cfg.h:324
DATA_BLOCK_MOL_FLAG_s * pTableMol
Definition: can_cfg.h:326
DATA_BLOCK_MIN_MAX_s * pTableMinMax
Definition: can_cfg.h:315
DATA_BLOCK_OPEN_WIRE_s * pTableOpenWire
Definition: can_cfg.h:317
DATA_BLOCK_INSULATION_MONITORING_s * pTableInsulation
Definition: can_cfg.h:323
OS_QUEUE * pQueueImd
Definition: can_cfg.h:312
DATA_BLOCK_SOF_s * pTableSof
Definition: can_cfg.h:320