foxBMS - Unit Tests  1.5.0
The foxBMS Unit Tests API Documentation
i2c.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 i2c.h
44  * @author foxBMS Team
45  * @date 2021-07-22 (date of creation)
46  * @updated 2023-02-03 (date of last update)
47  * @version v1.5.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 
60 #include "HL_i2c.h"
61 
62 #include "fstd_types.h"
63 
64 #include <stdint.h>
65 
66 /*========== Macros and Definitions =========================================*/
67 
68 /* Timeout in microseconds to avoid infinite loops when waiting for flags */
69 #define I2C_TIMEOUT_us (1000u)
70 
71 /* Transmitter DMA enable */
72 #define I2C_TXDMAEN (0x2u)
73 /* Receiver DMA enable */
74 #define I2C_RXDMAEN (0x1u)
75 
76 /* Mask to select data byte from Data Receive Register */
77 #define I2C_DDR_REGISTER_DATA_MASK (0xFFu)
78 
79 /* Handle of task in which I2C transactions with DMA are running, used for notifications */
80 #define I2C_TASK_HANDLE (ftsk_taskHandleI2c)
81 
82 /* Index used for FreeRTOS notification sent when TX DMA interrupt comes. Arbitrary unique value. */
83 #define I2C_NOTIFICATION_TX_INDEX (1u)
84 /* Index used for FreeRTOS notification sent when RX DMA interrupt comes. Arbitrary unique value. */
85 #define I2C_NOTIFICATION_RX_INDEX (2u)
86 /* Default notification value, signals that no notification was received. Arbitrary unique value. */
87 #define I2C_NO_NOTIFIED_VALUE (0x0u)
88 /* Notification value sent when TX DMA interrupt comes. Arbitrary unique value. */
89 #define I2C_TX_NOTIFIED_VALUE (0x51u)
90 /* Notification value sent when RX DMA interrupt comes. Arbitrary unique value. */
91 #define I2C_RX_NOTIFIED_VALUE (0x61u)
92 /* Notification value sent when RX DMA interrupt comes but last byte cannot be read. Arbitrary unique value. */
93 #define I2C_RX_NOTCOME_VALUE (0x62u)
94 /**
95  * Time in ms to wait for DMA notification, after this delay the transaction
96  * is considered to have failed
97  */
98 #define I2C_NOTIFICATION_TIMEOUT_ms (2u)
99 /* Mask to select prescaler value in prescaler register */
100 #define I2C_PRESCALER_MASK (0xFFu)
101 /* Factor to transform MHz to Hz */
102 #define I2C_FACTOR_MHZ_TO_HZ (1000000u)
103 /* Factor to transform seconds in microseconds to Hz */
104 #define I2C_FACTOR_S_TO_US (1000000u)
105 /* Number of bits in one I2C frame (byte + ACK bit) */
106 #define I2C_FACTOR_WORD_TO_BITS (9u)
107 /**
108  * Value of d factor in I2C configuration, depends on prescaler value
109  * See Technical Reference Manual p.1769 eq.65, (SPNU563A - March 2018)
110  * @{
111  */
112 #define I2C_DFACTOR_VALUE_PRESCALER_0 (7u)
113 #define I2C_DFACTOR_VALUE_PRESCALER_1 (6u)
114 #define I2C_DFACTOR_VALUE_PRESCALER_OTHER (5u)
115 /**@}*/
116 /* Margin to add to the computed time for the last TX byte to be sent */
117 #define I2C_TX_TIME_MARGIN_us (5u)
118 
119 /*========== Extern Constant and Variable Declarations ======================*/
120 
121 extern uint8_t i2c_rxLastByteInterface1;
122 extern uint8_t i2c_rxLastByteInterface2;
123 
124 /*========== Extern Function Prototypes =====================================*/
125 /**
126  * @brief Initialize the I2C hardware with dedicated HAL functions.
127  * Has to be called before any call to the rest of this API.
128  */
129 extern void I2C_Initialize(void);
130 
131 /**
132  * @brief reads from an I2C slave, no register address written first, blocking.
133  * @param pI2cInterface I2C interface to use
134  * @param slaveAddress address of slave to communicate with
135  * @param nrBytes number of registers to read
136  * @param readData buffer containing the read data
137  * @return STD_OK if transmission OK, STD_NOT_OK otherwise
138  */
139 extern STD_RETURN_TYPE_e I2C_Read(i2cBASE_t *pI2cInterface, uint32_t slaveAddress, uint32_t nrBytes, uint8_t *readData);
140 
141 /**
142  * @brief writes to an I2C slave, no register address written first, blocking.
143  * @param pI2cInterface I2C interface to use
144  * @param slaveAddress address of slave to communicate with
145  * @param nrBytes number of registers to write
146  * @param writeData buffer containing the data to write
147  * @return STD_OK if transmission OK, STD_NOT_OK otherwise
148  */
150  i2cBASE_t *pI2cInterface,
151  uint32_t slaveAddress,
152  uint32_t nrBytes,
153  uint8_t *writeData);
154 
155 /**
156  * @brief reads from an I2C slave, blocking.
157  * @param pI2cInterface I2C interface to use
158  * @param slaveAddress address of slave to communicate with
159  * @param nrBytesWrite number of registers to read
160  * @param writeData buffer containing the read data
161  * @param nrBytesRead number of registers to read
162  * @param readData buffer containing the read data
163  * @return STD_OK if transmission OK, STD_NOT_OK otherwise
164  */
166  i2cBASE_t *pI2cInterface,
167  uint32_t slaveAddress,
168  uint32_t nrBytesWrite,
169  uint8_t *writeData,
170  uint32_t nrBytesRead,
171  uint8_t *readData);
172 
173 /**
174  * @brief reads from an I2C slave, no register address written first, using DMA.
175  * @param pI2cInterface I2C interface to use
176  * @param slaveAddress address of slave to communicate with
177  * @param nrBytes number of registers to read
178  * @param readData buffer containing the read data
179  * @return STD_OK if transmission OK, STD_NOT_OK otherwise
180  */
182  i2cBASE_t *pI2cInterface,
183  uint32_t slaveAddress,
184  uint32_t nrBytes,
185  uint8_t *readData);
186 
187 /**
188  * @brief writes to an I2C slave, no register address written first, using DMA.
189  * @param pI2cInterface I2C interface to use
190  * @param slaveAddress address of slave to communicate with
191  * @param nrBytes number of registers to write
192  * @param writeData buffer containing the data to write
193  * @return retVal STD_OK if transmission OK, STD_NOT_OK otherwise
194  */
196  i2cBASE_t *pI2cInterface,
197  uint32_t slaveAddress,
198  uint32_t nrBytes,
199  uint8_t *writeData);
200 
201 /**
202  * @brief reads from an I2C slave, using DMA.
203  * @param pI2cInterface I2C interface to use
204  * @param slaveAddress address of slave to communicate with
205  * @param nrBytesWrite number of registers to read
206  * @param writeData buffer containing the read data
207  * @param nrBytesRead number of registers to read
208  * @param readData buffer containing the read data
209  * @return STD_OK if transmission OK, STD_NOT_OK otherwise
210  */
212  i2cBASE_t *pI2cInterface,
213  uint32_t slaveAddress,
214  uint32_t nrBytesWrite,
215  uint8_t *writeData,
216  uint32_t nrBytesRead,
217  uint8_t *readData);
218 
219 /**
220  * @brief Used to read last byte received per I2C.
221  * @details This function must be called in the DMA Rx callback.
222  * It returns the last byte received per I2C
223  * @param pI2cInterface I2C interface used
224  * @return last byte received on the I2C bus
225  */
226 extern uint8_t I2C_ReadLastRxByte(i2cBASE_t *pI2cInterface);
227 
228 /**
229  * @brief Waits for the I2C Rx buffer to be full.
230  * @details When the buffer is full, the received byte can be read.
231  * If the buffer is full before timeout_us microseconds are elapsed,
232  * the function returns true, false otherwise.
233  * The function also returns false if a NACK condition
234  * is detected.
235  * @param pI2cInterface I2C interface to use
236  * @param timeout_us time in microseconds to wait until the buffer is full
237  * @return true if buffer is full within timeout, false otherwise
238  */
239 extern bool I2C_WaitReceive(i2cBASE_t *pI2cInterface, uint32_t timeout_us);
240 
241 /*========== Externalized Static Functions Prototypes (Unit Test) ===========*/
242 #ifdef UNITY_UNIT_TEST
243 #endif
244 
245 #endif /* FOXBMS__I2C_H_ */
Definition of foxBMS standard types.
STD_RETURN_TYPE_e
Definition: fstd_types.h:82
STD_RETURN_TYPE_e I2C_WriteRead(i2cBASE_t *pI2cInterface, uint32_t slaveAddress, uint32_t nrBytesWrite, uint8_t *writeData, uint32_t nrBytesRead, uint8_t *readData)
reads from an I2C slave, blocking.
Definition: i2c.c:335
bool I2C_WaitReceive(i2cBASE_t *pI2cInterface, uint32_t timeout_us)
Waits for the I2C Rx buffer to be full.
Definition: i2c.c:772
uint8_t i2c_rxLastByteInterface2
Definition: i2c.c:77
uint8_t i2c_rxLastByteInterface1
Definition: i2c.c:75
uint8_t I2C_ReadLastRxByte(i2cBASE_t *pI2cInterface)
Used to read last byte received per I2C.
Definition: i2c.c:766
STD_RETURN_TYPE_e I2C_ReadDma(i2cBASE_t *pI2cInterface, uint32_t slaveAddress, uint32_t nrBytes, uint8_t *readData)
reads from an I2C slave, no register address written first, using DMA.
Definition: i2c.c:415
STD_RETURN_TYPE_e I2C_Read(i2cBASE_t *pI2cInterface, uint32_t slaveAddress, uint32_t nrBytes, uint8_t *readData)
reads from an I2C slave, no register address written first, blocking.
Definition: i2c.c:226
STD_RETURN_TYPE_e I2C_WriteReadDma(i2cBASE_t *pI2cInterface, uint32_t slaveAddress, uint32_t nrBytesWrite, uint8_t *writeData, uint32_t nrBytesRead, uint8_t *readData)
reads from an I2C slave, using DMA.
Definition: i2c.c:599
STD_RETURN_TYPE_e I2C_Write(i2cBASE_t *pI2cInterface, uint32_t slaveAddress, uint32_t nrBytes, uint8_t *writeData)
writes to an I2C slave, no register address written first, blocking.
Definition: i2c.c:283
STD_RETURN_TYPE_e I2C_WriteDma(i2cBASE_t *pI2cInterface, uint32_t slaveAddress, uint32_t nrBytes, uint8_t *writeData)
writes to an I2C slave, no register address written first, using DMA.
Definition: i2c.c:510
void I2C_Initialize(void)
Initialize the I2C hardware with dedicated HAL functions. Has to be called before any call to the res...
Definition: i2c.c:222