foxBMS  1.4.0
The foxBMS Battery Management System API Documentation
i2c.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 i2c.h
44  * @author foxBMS Team
45  * @date 2021-07-22 (date of creation)
46  * @updated 2022-07-28 (date of last update)
47  * @version v1.4.0
48  * @ingroup DRIVERS
49  * @prefix I2C
50  *
51  * @brief Header for the driver for the I2C module
52  *
53  */
54 
55 #ifndef FOXBMS__I2C_H_
56 #define FOXBMS__I2C_H_
57 
58 /*========== Includes =======================================================*/
59 #include "general.h"
60 
61 #include "HL_i2c.h"
62 
63 /*========== Macros and Definitions =========================================*/
64 
65 /** Timeout to avoid infinite loops when waiting for flags */
66 #define I2C_TIMEOUT_ITERATIONS (200u)
67 
68 /** Transmitter DMA enable */
69 #define I2C_TXDMAEN (0x2u)
70 /** Receiver DMA enable */
71 #define I2C_RXDMAEN (0x1u)
72 
73 /*========== Extern Constant and Variable Declarations ======================*/
74 
75 /*========== Extern Function Prototypes =====================================*/
76 /** Initialize the I2C hardware
77  *
78  * This function has to be called before any call to the rest of this API
79  */
80 extern void I2C_Initialize(void);
81 
82 /**
83  * @brief reads from an I2C slave, blocking.
84  *
85  * @param slaveAddress: address of slave to communicate with
86  * @param readAddress: address of first register to read from
87  * @param nrBytes: number of registers to read
88  * @param readData: buffer containing the read data
89  *
90  * @return retVal: STD_OK if transmission OK, STD_NOT_OK otherwise
91  *
92  */
93 extern STD_RETURN_TYPE_e I2C_Read(uint32_t slaveAddress, uint8_t readAddress, uint32_t nrBytes, uint8_t *readData);
94 
95 /**
96  * @brief reads from an I2C slave, no register address written first, blocking.
97  *
98  * @param slaveAddress: address of slave to communicate with
99  * @param nrBytes: number of registers to read
100  * @param readData: buffer containing the read data
101  *
102  * @return retVal: STD_OK if transmission OK, STD_NOT_OK otherwise
103  *
104  */
105 extern STD_RETURN_TYPE_e I2C_ReadDirect(uint32_t slaveAddress, uint32_t nrBytes, uint8_t *readData);
106 
107 /**
108  * @brief writes to an I2C slave, blocking.
109  *
110  * @param slaveAddress: address of slave to communicate with
111  * @param writeAddress: address of first register to write to
112  * @param nrBytes: number of registers to write
113  * @param writeData: buffer containing the data to write
114  *
115  * @return retVal: STD_OK if transmission OK, STD_NOT_OK otherwise
116  *
117  */
118 extern STD_RETURN_TYPE_e I2C_Write(uint32_t slaveAddress, uint8_t writeAddress, uint32_t nrBytes, uint8_t *writeData);
119 
120 /**
121  * @brief writes to an I2C slave, no register address written first, blocking.
122  *
123  * @param slaveAddress: address of slave to communicate with
124  * @param nrBytes: number of registers to write
125  * @param writeData: buffer containing the data to write
126  *
127  * @return retVal: STD_OK if transmission OK, STD_NOT_OK otherwise
128  *
129  */
130 extern STD_RETURN_TYPE_e I2C_WriteDirect(uint32_t slaveAddress, uint32_t nrBytes, uint8_t *writeData);
131 
132 /**
133  * @brief reads from an I2C slave, using DMA.
134  *
135  * @param slaveAddress: address of slave to communicate with
136  * @param readAddress: address of first register to read from
137  * @param nrBytes: number of registers to read
138  * @param readData: buffer containing the read data
139  *
140  * @return retVal: STD_OK if transmission OK, STD_NOT_OK otherwise
141  *
142  */
143 extern STD_RETURN_TYPE_e I2C_ReadDma(uint32_t slaveAddress, uint8_t readAddress, uint32_t nrBytes, uint8_t *readData);
144 
145 /**
146  * @brief writes to an I2C slave, using DMA.
147  *
148  * @param slaveAddress: address of slave to communicate with
149  * @param writeAddress: address of first register to write to
150  * @param nrBytes: number of registers to write
151  * @param writeData: buffer containing the data to write
152  *
153  * @return retVal: STD_OK if transmission OK, STD_NOT_OK otherwise
154  *
155  */
157  uint32_t slaveAddress,
158  uint8_t writeAddress,
159  uint32_t nrBytes,
160  uint8_t *writeData);
161 
162 /**
163  * @brief sets stop condition.
164  *
165  */
166 extern void I2C_SetStopNow(void);
167 
168 /*========== Externalized Static Functions Prototypes (Unit Test) ===========*/
169 #ifdef UNITY_UNIT_TEST
170 
171 #endif
172 
173 #endif /* FOXBMS__I2C_H_ */
STD_RETURN_TYPE_e
Definition: fstd_types.h:81
General macros and definitions for the whole platform.
STD_RETURN_TYPE_e I2C_WriteDirect(uint32_t slaveAddress, uint32_t nrBytes, uint8_t *writeData)
writes to an I2C slave, no register address written first, blocking.
Definition: i2c.c:357
void I2C_SetStopNow(void)
sets stop condition.
Definition: i2c.c:567
STD_RETURN_TYPE_e I2C_ReadDma(uint32_t slaveAddress, uint8_t readAddress, uint32_t nrBytes, uint8_t *readData)
reads from an I2C slave, using DMA.
Definition: i2c.c:434
STD_RETURN_TYPE_e I2C_Write(uint32_t slaveAddress, uint8_t writeAddress, uint32_t nrBytes, uint8_t *writeData)
writes to an I2C slave, blocking.
Definition: i2c.c:257
STD_RETURN_TYPE_e I2C_ReadDirect(uint32_t slaveAddress, uint32_t nrBytes, uint8_t *readData)
reads from an I2C slave, no register address written first, blocking.
Definition: i2c.c:184
void I2C_Initialize(void)
Definition: i2c.c:75
STD_RETURN_TYPE_e I2C_WriteDma(uint32_t slaveAddress, uint8_t writeAddress, uint32_t nrBytes, uint8_t *writeData)
writes to an I2C slave, using DMA.
Definition: i2c.c:501
STD_RETURN_TYPE_e I2C_Read(uint32_t slaveAddress, uint8_t readAddress, uint32_t nrBytes, uint8_t *readData)
reads from an I2C slave, blocking.
Definition: i2c.c:79