foxBMS  1.6.0
The foxBMS Battery Management System API Documentation
can.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.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 Header for the driver for the CAN module
52  *
53  * @details Provides the interfaces for initialization, receive
54  * and transmit handling
55  *
56  */
57 
58 #ifndef FOXBMS__CAN_H_
59 #define FOXBMS__CAN_H_
60 
61 /*========== Includes =======================================================*/
62 
63 #include "can_cfg.h"
64 
65 #include <stdint.h>
66 
67 /*========== Macros and Definitions =========================================*/
68 /** Total number of message boxes available for TMS570LC4357 */
69 #define CAN_TOTAL_NUMBER_OF_MESSAGE_BOXES (64u)
70 
71 /** Half of the 64 message boxes are defined for TX
72  * This is used to determined in the CAN interrupt routine if TX or RX case
73  */
74 #define CAN_NR_OF_TX_MESSAGE_BOX (32u)
75 
76 /** Task time slot where the CAN TX function is called. Repetition time of
77  * periodic CAN messages must be multiple of this (e.g., 1u, 10u or 100u)
78  */
79 #define CAN_TICK_ms (10u)
80 
81 /** This structure contains variables relevant for the CAN signal module. */
82 typedef struct {
83  bool periodicEnable; /*!< defines if periodic transmit and receive should run */
84  bool currentSensorPresent[BS_NR_OF_STRINGS]; /*!< defines if a current sensor is detected */
85  bool currentSensorCCPresent[BS_NR_OF_STRINGS]; /*!< defines if a CC info is being sent */
86  bool currentSensorECPresent[BS_NR_OF_STRINGS]; /*!< defines if a EC info is being sent */
87 } CAN_STATE_s;
88 
89 /*========== Extern Constant and Variable Declarations ======================*/
90 
91 /*========== Extern Function Prototypes =====================================*/
92 
93 /**
94  * @brief Sends over CAN the data passed in parameters.
95  * This function goes over the message boxes and marks the ones that should
96  * be sent.
97  * @param[in,out] pNode CAN interface to use
98  * @param id ID of message to send
99  * @param idType standard or extended identifier
100  * @param[out] pData data to send (8 bytes)
101  * @return #STD_OK if a message box was free to send, #STD_NOT_OK otherwise
102  */
103 extern STD_RETURN_TYPE_e CAN_DataSend(CAN_NODE_s *pNode, uint32_t id, CAN_IDENTIFIER_TYPE_e idType, uint8 *pData);
104 
105 /**
106  * @brief Reads messages from TX Queue and sends them via CAN.
107 */
108 extern void CAN_SendMessagesFromQueue(void);
109 
110 /**
111  * @brief Calls the functions to drive the CAN interface.
112  * Makes the CAN timing checks and sends the periodic messages.
113  */
114 extern void CAN_MainFunction(void);
115 
116 /**
117  * @brief Checks the data received per CAN.
118  * A receive buffer is used because CAN frames are received in an interrupt routine.
119  * The TMS570LC4357 does not allow nested interrupts, so interrupts are deactivated during receive.
120  * Calls to the database do not work when interrupts are disabled.
121  * Receive callbacks are made within this function: as it is not called during an interrupt routine,
122  * calls to the database can be made.
123  */
124 extern void CAN_ReadRxBuffer(void);
125 
126 /**
127  * @brief Enables the CAN transceiver..
128  * This function sets th pins to enable the CAN transceiver.
129  * It must be called before using the CAN interface.
130  */
131 extern void CAN_Initialize(void);
132 
133 /**
134  * @brief Enables periodic sending per CAN.
135  * This is used to prevent sending uninitialized data per CAN
136  * (e.g., before the first LTC measurement cycle was completed).
137  */
138 extern void CAN_EnablePeriodic(bool command);
139 
140 /**
141  * @brief set flag for presence of current sensor.
142  * @return retval true if a current sensor is present, false otherwise
143  */
144 extern bool CAN_IsCurrentSensorPresent(uint8_t stringNumber);
145 
146 /**
147  * @brief get flag if CC message from current sensor is received.
148  * @param stringNumber addressed string
149  * @return true if CC message is being received, false otherwise
150  */
151 extern bool CAN_IsCurrentSensorCcPresent(uint8_t stringNumber);
152 
153 /**
154  * @brief get flag if EC message from current sensor is received
155  * @param stringNumber addressed string
156  * @return true if EC message is being received, false otherwise
157  */
158 extern bool CAN_IsCurrentSensorEcPresent(uint8_t stringNumber);
159 
160 /*========== Externalized Static Functions Prototypes (Unit Test) ===========*/
161 #ifdef UNITY_UNIT_TEST
162 extern CAN_STATE_s *TEST_CAN_GetCANState(void);
163 extern void TEST_CAN_ValidateConfiguredTxMessagePeriod(void);
164 extern void TEST_CAN_ValidateConfiguredTxMessagePhase(void);
165 extern void TEST_CAN_CheckDatabaseNullPointer(CAN_SHIM_s canShim);
166 extern void TEST_CAN_TxInterrupt(canBASE_t *pNode, uint32 messageBox);
167 extern void TEST_CAN_RxInterrupt(canBASE_t *pNode, uint32 messageBox);
168 extern STD_RETURN_TYPE_e TEST_CAN_PeriodicTransmit(void);
169 extern uint32_t TEST_CAN_CalculateCounterResetValue(void);
170 extern void TEST_CAN_CheckCanTiming(void);
171 extern bool TEST_CAN_IsMessagePeriodElapsed(uint32_t ticksSinceStart, uint16_t messageIndex);
172 #if BS_CURRENT_SENSOR_PRESENT == true
173 extern void TEST_CAN_SetCurrentSensorPresent(bool command, uint8_t stringNumber);
174 extern void TEST_CAN_SetCurrentSensorCcPresent(bool command, uint8_t stringNumber);
175 extern void TEST_CAN_SetCurrentSensorEcPresent(bool command, uint8_t stringNumber);
176 extern void TEST_CAN_CheckCanTimingOfCurrentSensor(void);
177 #endif /* BS_CURRENT_SENSOR_PRESENT == true */
178 extern void TEST_CAN_ConfigureRxMailboxesForExtendedIdentifiers(void);
179 extern void TEST_CAN_InitializeTransceiver(void);
180 extern CAN_NODE_s *TEST_CAN_GetNodeConfigurationStructFromRegisterAddress(canBASE_t *pNodeRegister);
181 extern STD_RETURN_TYPE_e TEST_CAN_PeriodicTransmit(void);
182 #endif
183 
184 #endif /* FOXBMS__CAN_H_ */
#define BS_NR_OF_STRINGS
Number of parallel strings in the battery pack.
bool CAN_IsCurrentSensorCcPresent(uint8_t stringNumber)
get flag if CC message from current sensor is received.
Definition: can.c:1019
void CAN_MainFunction(void)
Calls the functions to drive the CAN interface. Makes the CAN timing checks and sends the periodic me...
Definition: can.c:981
bool CAN_IsCurrentSensorEcPresent(uint8_t stringNumber)
get flag if EC message from current sensor is received
Definition: can.c:1024
void CAN_SendMessagesFromQueue(void)
Reads messages from TX Queue and sends them via CAN.
Definition: can.c:966
void CAN_ReadRxBuffer(void)
Checks the data received per CAN. A receive buffer is used because CAN frames are received in an inte...
Definition: can.c:988
bool CAN_IsCurrentSensorPresent(uint8_t stringNumber)
set flag for presence of current sensor.
Definition: can.c:1014
STD_RETURN_TYPE_e CAN_DataSend(CAN_NODE_s *pNode, uint32_t id, CAN_IDENTIFIER_TYPE_e idType, uint8 *pData)
Sends over CAN the data passed in parameters. This function goes over the message boxes and marks the...
Definition: can.c:923
void CAN_Initialize(void)
Enables the CAN transceiver.. This function sets th pins to enable the CAN transceiver....
Definition: can.c:910
void CAN_EnablePeriodic(bool command)
Enables periodic sending per CAN. This is used to prevent sending uninitialized data per CAN (e....
Definition: can.c:1006
Headers for the configuration for the CAN module.
CAN_IDENTIFIER_TYPE_e
Definition: can_cfg.h:162
STD_RETURN_TYPE_e
Definition: fstd_types.h:82
bool periodicEnable
Definition: can.h:83