foxBMS  1.1.2
The foxBMS Battery Management System API Documentation
can.h
Go to the documentation of this file.
1 /**
2  *
3  * @copyright © 2010 - 2021, 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 2021-07-23 (date of last update)
47  * @ingroup DRIVERS
48  * @prefix CAN
49  *
50  * @brief Header for the driver for the CAN module
51  *
52  * Provides the interfaces for initialization, receive
53  * and transmit handling
54  *
55  */
56 
57 #ifndef FOXBMS__CAN_H_
58 #define FOXBMS__CAN_H_
59 
60 /*========== Includes =======================================================*/
61 #include "general.h"
62 
63 #include "can_cfg.h"
64 
65 #include "HL_can.h"
66 
67 /*========== Macros and Definitions =========================================*/
68 
69 /** register on which the CAN interface is connected */
70 #define CAN0_NODE (canREG1)
71 
72 /** Half of the 64 messageboxes are defined for TX
73  * This is used to determined in the CAN interrupt routine if TX or RX case
74  */
75 #define CAN_NR_OF_TX_MESSAGE_BOX (32U)
76 
77 /** length of the RX buffer */
78 #define CAN0_RX_BUFFER_LENGTH (32U)
79 
80 /** Task time slot where the CAN TX function is called. Repetition time of
81  * periodic CAN messages must be multiple of this (e.g., 1u, 10u or 100u)
82  */
83 #define CAN_TICK_MS (10U)
84 
85 /** Buffer containing all the CAN RX elements */
86 typedef struct CAN_RX_BUFFER {
87  CAN_BUFFERELEMENT_s *pRead; /*!< read pointer */
88  CAN_BUFFERELEMENT_s *pWrite; /*!< write pointer */
89  uint8_t length; /*!< length of buffer */
91 
92 /** This structure contains variables relevant for the CAN signal module. */
93 typedef struct CAN_STATE {
94  bool periodicEnable; /*!< defines if periodic transmit and receive should run */
95  bool currentSensorPresent[BS_NR_OF_STRINGS]; /*!< defines if a current sensor is detected */
96  bool currentSensorCCPresent[BS_NR_OF_STRINGS]; /*!< defines if a CC info is being sent */
97  bool currentSensorECPresent[BS_NR_OF_STRINGS]; /*!< defines if a EC info is being sent */
99 
100 /*========== Extern Constant and Variable Declarations ======================*/
101 
102 /*========== Extern Function Prototypes =====================================*/
103 
104 /**
105  * @brief Sends over CAN the data passed in parameters.
106  * This function goes over the messageboxes and marks the ones that should
107  * be sent.
108  * @param[in,out] pNode CAN interface to use
109  * @param[in] id ID of message to send
110  * @param[out] pData data to send (8 bytes)
111  * @return #STD_OK if a message box was free to send, #STD_NOT_OK otherwise
112  */
113 extern STD_RETURN_TYPE_e CAN_DataSend(canBASE_t *pNode, uint32_t id, uint8 *pData);
114 
115 /**
116  * @brief Calls the functions to drive the CAN interface.
117  * Makes the CAN timing checks and sends the periodic messages.
118  */
119 extern void CAN_MainFunction(void);
120 
121 /**
122  * @brief Checks the data received per CAN.
123  * A receive buffer is used because CAN frames are received in an interrupt routine.
124  * The TMS570LC4357 does not allow nested interrupts, so interrupts are deactivated during receive.
125  * Calls to the database do not work when interrupts are disabled.
126  * Receive callbacks are made within this function: as it is not called during an interrupt routine,
127  * calls to the database can be made.
128  */
129 extern void CAN_ReadRxBuffer(void);
130 
131 /**
132  * @brief Enables the CAN transceiver..
133  * This function sets th pins to enable the CAN transceiver.
134  * It must be called before using the CAN interface.
135  */
136 extern void CAN_Initialize(void);
137 
138 /**
139  * @brief Enables periodic sending per CAN.
140  * This is used to prevent sending uninitialized data per CAN
141  * (e.g., before the first LTC measurement cycle was completed).
142  */
143 extern void CAN_EnablePeriodic(bool command);
144 
145 /**
146  * @brief set flag for presence of current sensor.
147  * @return retval true if a current sensor is present, false otherwise
148  */
149 extern bool CAN_IsCurrentSensorPresent(uint8_t stringNumber);
150 
151 /**
152  * @brief get flag if CC message from current sensor is received.
153  * @param stringNumber addressed string
154  * @return true if CC message is being received, false otherwise
155  */
156 extern bool CAN_IsCurrentSensorCcPresent(uint8_t stringNumber);
157 
158 /**
159  * @brief get flag if EC message from current sensor is received
160  * @param stringNumber addressed string
161  * @return true if EC message is being received, false otherwise
162  */
163 extern bool CAN_IsCurrentSensorEcPresent(uint8_t stringNumber);
164 
165 /**
166  * @brief Transmit startup boot message
167  * @return #STD_OK if transmission successful, otherweise #STD_NOT_OK
168  */
170 
171 /*========== Externalized Static Functions Prototypes (Unit Test) ===========*/
172 #ifdef UNITY_UNIT_TEST
173 extern CAN_STATE_s *TEST_CAN_GetCANState(void);
174 #endif
175 
176 #endif /* FOXBMS__CAN_H_ */
#define BS_NR_OF_STRINGS
struct CAN_STATE CAN_STATE_s
bool CAN_IsCurrentSensorCcPresent(uint8_t stringNumber)
get flag if CC message from current sensor is received.
Definition: can.c:425
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:237
bool CAN_IsCurrentSensorEcPresent(uint8_t stringNumber)
get flag if EC message from current sensor is received
Definition: can.c:430
STD_RETURN_TYPE_e CAN_DataSend(canBASE_t *pNode, uint32_t id, uint8 *pData)
Sends over CAN the data passed in parameters. This function goes over the messageboxes and marks the ...
Definition: can.c:211
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:349
bool CAN_IsCurrentSensorPresent(uint8_t stringNumber)
set flag for presence of current sensor.
Definition: can.c:420
STD_RETURN_TYPE_e CAN_TransmitBootMessage(void)
Transmit startup boot message.
Definition: can.c:481
struct CAN_RX_BUFFER CAN_RX_BUFFER_s
void CAN_Initialize(void)
Enables the CAN transceiver.. This function sets th pins to enable the CAN transceiver....
Definition: can.c:207
void CAN_EnablePeriodic(bool command)
Enables periodic sending per CAN. This is used to prevent sending uninitialized data per CAN (e....
Definition: can.c:374
Headers for the configuration for the CAN module.
enum STD_RETURN_TYPE STD_RETURN_TYPE_e
General macros and definitions for the whole platform.
uint8_t length
Definition: can.h:89
CAN_BUFFERELEMENT_s * pWrite
Definition: can.h:88
CAN_BUFFERELEMENT_s * pRead
Definition: can.h:87
Definition: can.h:93
bool currentSensorECPresent[BS_NR_OF_STRINGS]
Definition: can.h:97
bool periodicEnable
Definition: can.h:94
bool currentSensorCCPresent[BS_NR_OF_STRINGS]
Definition: can.h:96
bool currentSensorPresent[BS_NR_OF_STRINGS]
Definition: can.h:95