foxBMS - Unit Tests  1.5.0
The foxBMS Unit Tests 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-02-03 (date of last update)
47  * @version v1.5.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_MIN_MAX_s *pTableMinMax; /*!< database table with min/max values */
182  DATA_BLOCK_CURRENT_SENSOR_s *pTableCurrentSensor; /*!< database table with current sensor measurements */
183  DATA_BLOCK_OPEN_WIRE_s *pTableOpenWire; /*!< database table with open wire status */
184  DATA_BLOCK_STATE_REQUEST_s *pTableStateRequest; /*!< database table with state requests */
185  DATA_BLOCK_PACK_VALUES_s *pTablePackValues; /*!< database table with pack values */
186  DATA_BLOCK_SOF_s *pTableSof; /*!< database table with SOF values */
187  DATA_BLOCK_SOX_s *pTableSox; /*!< database table with SOC and SOE values */
188  DATA_BLOCK_ERROR_STATE_s *pTableErrorState; /*!< database table with error state variables */
189  DATA_BLOCK_INSULATION_MONITORING_s *pTableInsulation; /*!< database table with insulation monitoring info */
190  DATA_BLOCK_MSL_FLAG_s *pTableMsl; /*!< database table with MSL flags */
191  DATA_BLOCK_RSL_FLAG_s *pTableRsl; /*!< database table with RSL flags */
192  DATA_BLOCK_MOL_FLAG_s *pTableMol; /*!< database table with MOL flags */
193 } CAN_SHIM_s;
194 
195 /** definition of a CAN message (without data) */
196 typedef struct {
197  uint32_t id; /*!< message ID */
198  CAN_IDENTIFIER_TYPE_e idType; /*!< Standard or Extended identifier */
199  uint8_t dlc; /*!< data length */
200  CAN_ENDIANNESS_e endianness; /*!< Byte order (big or little endian) */
202 
203 /** timing information of a CAN TX message */
204 typedef struct {
205  uint32_t period; /*!< CAN message cycle time */
206  uint32_t phase; /*!< CAN message startup (first send) offset */
208 
209 /** timing information of a CAN RX message */
210 typedef struct {
211  uint32_t period; /*!< expected CAN message cycle time */
213 
214 /** type definition for tx callback functions used in CAN messages */
215 typedef uint32_t (*CAN_TxCallbackFunction_f)(
216  CAN_MESSAGE_PROPERTIES_s message,
217  uint8_t *canData,
218  uint8_t *pMuxId,
219  const CAN_SHIM_s *const kpkCanShim);
220 
221 /** type definition for rx callback functions used in CAN messages */
222 typedef uint32_t (*CAN_RxCallbackFunction_f)(
223  CAN_MESSAGE_PROPERTIES_s message,
224  const uint8_t *const kpkCanData,
225  const CAN_SHIM_s *const kpkCanShim);
226 
227 /** type definition for structure of a TX CAN message */
228 typedef struct {
229  CAN_NODE_s *canNode; /*!< CAN node on which the message is transmitted */
230  CAN_MESSAGE_PROPERTIES_s message; /*!< CAN message */
231  CAN_TX_MESSAGE_TIMING_s timing; /*!< time and phase */
232  CAN_TxCallbackFunction_f callbackFunction; /*!< CAN message callback after message is sent */
233  uint8_t *pMuxId; /*!< for multiplexed signals: callback can use this as pointer to a mux variable, NULL_PTR if
234  unused*/
236 
237 /* TODO: timing check not implemented for RX messages! */
238 /** type definition for structure of an RX CAN message */
239 typedef struct {
240  CAN_NODE_s *canNode; /*!< CAN node on which the message is received */
241  CAN_MESSAGE_PROPERTIES_s message; /*!< CAN message */
242  CAN_RX_MESSAGE_TIMING_s timing; /*!< time and phase */
243  CAN_RxCallbackFunction_f callbackFunction; /*!< CAN message callback after message is received */
245 
246 /*========== Extern Constant and Variable Declarations ======================*/
247 /** variable for storing and passing on the local database table handles */
248 extern const CAN_SHIM_s can_kShim;
249 
250 /** CAN node configurations for CAN1 and CAN2 (isolated) @{*/
251 extern const CAN_NODE_s can_node1;
252 extern const CAN_NODE_s can_node2Isolated;
253 /**@}*/
254 
255 /** CAN RX and TX message configuration structs @{*/
258 /**@}*/
259 
260 /** array length for transmission CAN0 message definition @{*/
261 extern const uint8_t can_txLength;
262 extern const uint8_t can_rxLength;
263 /**@}*/
264 
265 /*========== Extern Function Prototypes =====================================*/
266 
267 /*========== Externalized Static Functions Prototypes (Unit Test) ===========*/
268 #ifdef UNITY_UNIT_TEST
269 #endif
270 
271 #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:215
const uint8_t can_txLength
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:101
const CAN_NODE_s can_node1
Definition: can_cfg.c:74
const uint8_t can_rxLength
Definition: can_cfg_rx.c:92
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:222
#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:198
CAN_ENDIANNESS_e endianness
Definition: can_cfg.h:200
canBASE_t * canNodeRegister
Definition: can_cfg.h:76
CAN_RX_MESSAGE_TIMING_s timing
Definition: can_cfg.h:242
CAN_RxCallbackFunction_f callbackFunction
Definition: can_cfg.h:243
CAN_MESSAGE_PROPERTIES_s message
Definition: can_cfg.h:241
CAN_NODE_s * canNode
Definition: can_cfg.h:240
DATA_BLOCK_ERROR_STATE_s * pTableErrorState
Definition: can_cfg.h:188
DATA_BLOCK_CELL_VOLTAGE_s * pTableCellVoltage
Definition: can_cfg.h:179
DATA_BLOCK_STATE_REQUEST_s * pTableStateRequest
Definition: can_cfg.h:184
DATA_BLOCK_PACK_VALUES_s * pTablePackValues
Definition: can_cfg.h:185
DATA_BLOCK_RSL_FLAG_s * pTableRsl
Definition: can_cfg.h:191
DATA_BLOCK_SOX_s * pTableSox
Definition: can_cfg.h:187
DATA_BLOCK_CURRENT_SENSOR_s * pTableCurrentSensor
Definition: can_cfg.h:182
DATA_BLOCK_CELL_TEMPERATURE_s * pTableCellTemperature
Definition: can_cfg.h:180
DATA_BLOCK_MSL_FLAG_s * pTableMsl
Definition: can_cfg.h:190
DATA_BLOCK_MOL_FLAG_s * pTableMol
Definition: can_cfg.h:192
DATA_BLOCK_MIN_MAX_s * pTableMinMax
Definition: can_cfg.h:181
DATA_BLOCK_OPEN_WIRE_s * pTableOpenWire
Definition: can_cfg.h:183
DATA_BLOCK_INSULATION_MONITORING_s * pTableInsulation
Definition: can_cfg.h:189
OS_QUEUE * pQueueImd
Definition: can_cfg.h:178
DATA_BLOCK_SOF_s * pTableSof
Definition: can_cfg.h:186
CAN_TxCallbackFunction_f callbackFunction
Definition: can_cfg.h:232
CAN_MESSAGE_PROPERTIES_s message
Definition: can_cfg.h:230
uint8_t * pMuxId
Definition: can_cfg.h:233
CAN_TX_MESSAGE_TIMING_s timing
Definition: can_cfg.h:231
CAN_NODE_s * canNode
Definition: can_cfg.h:229