1 /*
2 * Copyright (c) 2015, Freescale Semiconductor, Inc.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without modification,
6 * are permitted provided that the following conditions are met:
7 *
8 * o Redistributions of source code must retain the above copyright notice, this list
9 * of conditions and the following disclaimer.
10 *
11 * o Redistributions in binary form must reproduce the above copyright notice, this
12 * list of conditions and the following disclaimer in the documentation and/or
13 * other materials provided with the distribution.
14 *
15 * o Neither the name of Freescale Semiconductor, Inc. nor the names of its
16 * contributors may be used to endorse or promote products derived from this
17 * software without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
23 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
26 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31 #ifndef __I2C_IMX_H__
32 #define __I2C_IMX_H__
33
34 #include <stdint.h>
35 #include <stdbool.h>
36 #include <assert.h>
37 #include "device_imx.h"
38
39 /*!
40 * @addtogroup i2c_imx_driver
41 * @{
42 */
43
44 /*******************************************************************************
45 * Definitions
46 ******************************************************************************/
47
48 /*! @brief I2C module initialization structure. */
49 typedef struct _i2c_init_config
50 {
51 uint32_t clockRate; /*!< Current I2C module clock freq. */
52 uint32_t baudRate; /*!< Desired I2C baud rate. */
53 uint8_t slaveAddress; /*!< I2C module's own address when addressed as slave device. */
54 } i2c_init_config_t;
55
56 /*! @brief Flag for I2C interrupt status check or polling status. */
57 enum _i2c_status_flag
58 {
59 i2cStatusTransferComplete = I2C_I2SR_ICF_MASK, /*!< Data Transfer complete flag. */
60 i2cStatusAddressedAsSlave = I2C_I2SR_IAAS_MASK, /*!< Addressed as a slave flag. */
61 i2cStatusBusBusy = I2C_I2SR_IBB_MASK, /*!< Bus is busy flag. */
62 i2cStatusArbitrationLost = I2C_I2SR_IAL_MASK, /*!< Arbitration is lost flag. */
63 i2cStatusSlaveReadWrite = I2C_I2SR_SRW_MASK, /*!< Master reading from slave flag(De-assert if master writing to slave). */
64 i2cStatusInterrupt = I2C_I2SR_IIF_MASK, /*!< An interrupt is pending flag. */
65 i2cStatusReceivedAck = I2C_I2SR_RXAK_MASK, /*!< No acknowledge detected flag. */
66 };
67
68 /*! @brief I2C Bus role of this module. */
69 enum _i2c_work_mode
70 {
71 i2cModeSlave = 0x0, /*!< This module works as I2C Slave. */
72 i2cModeMaster = I2C_I2CR_MSTA_MASK, /*!< This module works as I2C Master. */
73 };
74
75 /*! @brief Data transfer direction. */
76 enum _i2c_direction_mode
77 {
78 i2cDirectionReceive = 0x0, /*!< This module works at receive mode. */
79 i2cDirectionTransmit = I2C_I2CR_MTX_MASK, /*!< This module works at transmit mode. */
80 };
81
82 /*******************************************************************************
83 * API
84 ******************************************************************************/
85
86 #if defined(__cplusplus)
87 extern "C" {
88 #endif
89
90 /*!
91 * @name I2C Initialization and Configuration functions
92 * @{
93 */
94
95 /*!
96 * @brief Initialize I2C module with given initialization structure.
97 *
98 * @param base I2C base pointer.
99 * @param initConfig I2C initialization structure (see @ref i2c_init_config_t).
100 */
101 void I2C_Init(I2C_Type* base, const i2c_init_config_t* initConfig);
102
103 /*!
104 * @brief This function reset I2C module register content to its default value.
105 *
106 * @param base I2C base pointer.
107 */
108 void I2C_Deinit(I2C_Type* base);
109
110 /*!
111 * @brief This function is used to Enable the I2C Module.
112 *
113 * @param base I2C base pointer.
114 */
I2C_Enable(I2C_Type * base)115 static inline void I2C_Enable(I2C_Type* base)
116 {
117 I2C_I2CR_REG(base) |= I2C_I2CR_IEN_MASK;
118 }
119
120 /*!
121 * @brief This function is used to Disable the I2C Module.
122 *
123 * @param base I2C base pointer.
124 */
I2C_Disable(I2C_Type * base)125 static inline void I2C_Disable(I2C_Type* base)
126 {
127 I2C_I2CR_REG(base) &= ~I2C_I2CR_IEN_MASK;
128 }
129
130 /*!
131 * @brief This function is used to set the baud rate of I2C Module.
132 *
133 * @param base I2C base pointer.
134 * @param clockRate I2C module clock frequency.
135 * @param baudRate Desired I2C module baud rate.
136 */
137 void I2C_SetBaudRate(I2C_Type* base, uint32_t clockRate, uint32_t baudRate);
138
139 /*!
140 * @brief This function is used to set the own I2C bus address when addressed as a slave.
141 *
142 * @param base I2C base pointer.
143 * @param slaveAddress Own I2C Bus address.
144 */
I2C_SetSlaveAddress(I2C_Type * base,uint8_t slaveAddress)145 static inline void I2C_SetSlaveAddress(I2C_Type* base, uint8_t slaveAddress)
146 {
147 assert(slaveAddress < 0x80);
148
149 I2C_IADR_REG(base) = (I2C_IADR_REG(base) & ~I2C_IADR_ADR_MASK) | I2C_IADR_ADR(slaveAddress);
150 }
151
152 /*!
153 * @name I2C Bus Control functions
154 * @{
155 */
156
157 /*!
158 * @brief This function is used to Generate a Repeat Start Signal on I2C Bus.
159 *
160 * @param base I2C base pointer.
161 */
I2C_SendRepeatStart(I2C_Type * base)162 static inline void I2C_SendRepeatStart(I2C_Type* base)
163 {
164 I2C_I2CR_REG(base) |= I2C_I2CR_RSTA_MASK;
165 }
166
167 /*!
168 * @brief This function is used to select the I2C bus role of this module,
169 * both I2C Bus Master and Slave can be select.
170 *
171 * @param base I2C base pointer.
172 * @param mode I2C Bus role to set (see @ref _i2c_work_mode enumeration).
173 */
I2C_SetWorkMode(I2C_Type * base,uint32_t mode)174 static inline void I2C_SetWorkMode(I2C_Type* base, uint32_t mode)
175 {
176 assert((mode == i2cModeMaster) || (mode == i2cModeSlave));
177
178 I2C_I2CR_REG(base) = (I2C_I2CR_REG(base) & ~I2C_I2CR_MSTA_MASK) | mode;
179 }
180
181 /*!
182 * @brief This function is used to select the data transfer direction of this module,
183 * both Transmit and Receive can be select.
184 *
185 * @param base I2C base pointer.
186 * @param direction I2C Bus data transfer direction (see @ref _i2c_direction_mode enumeration).
187 */
I2C_SetDirMode(I2C_Type * base,uint32_t direction)188 static inline void I2C_SetDirMode(I2C_Type* base, uint32_t direction)
189 {
190 assert((direction == i2cDirectionReceive) || (direction == i2cDirectionTransmit));
191
192 I2C_I2CR_REG(base) = (I2C_I2CR_REG(base) & ~I2C_I2CR_MTX_MASK) | direction;
193 }
194
195 /*!
196 * @brief This function is used to set the Transmit Acknowledge action when receive
197 * data from other device.
198 *
199 * @param base I2C base pointer.
200 * @param ack The ACK value answerback to remote I2C device.
201 * - true: An acknowledge signal is sent to the bus at the ninth clock bit.
202 * - false: No acknowledge signal response is sent.
203 */
204 void I2C_SetAckBit(I2C_Type* base, bool ack);
205
206 /*!
207 * @name Data transfers functions
208 * @{
209 */
210
211 /*!
212 * @brief Writes one byte of data to the I2C bus.
213 *
214 * @param base I2C base pointer.
215 * @param byte The byte of data to transmit.
216 */
I2C_WriteByte(I2C_Type * base,uint8_t byte)217 static inline void I2C_WriteByte(I2C_Type* base, uint8_t byte)
218 {
219 I2C_I2DR_REG(base) = byte;
220 }
221
222 /*!
223 * @brief Returns the last byte of data read from the bus and initiate another read.
224 *
225 * In a master receive mode, calling this function initiates receiving the next byte of data.
226 *
227 * @param base I2C base pointer.
228 * @return This function returns the last byte received while the I2C module is configured in master
229 * receive or slave receive mode.
230 */
I2C_ReadByte(I2C_Type * base)231 static inline uint8_t I2C_ReadByte(I2C_Type* base)
232 {
233 return (uint8_t)(I2C_I2DR_REG(base) & I2C_I2DR_DATA_MASK);
234 }
235
236 /*!
237 * @name Interrupts and flags management functions
238 * @{
239 */
240
241 /*!
242 * @brief Enable or disable I2C interrupt requests.
243 *
244 * @param base I2C base pointer.
245 * @param enable Enable/Disbale I2C interrupt.
246 * - true: Enable I2C interrupt.
247 * - false: Disable I2C interrupt.
248 */
249 void I2C_SetIntCmd(I2C_Type* base, bool enable);
250
251 /*!
252 * @brief Gets the I2C status flag state.
253 *
254 * @param base I2C base pointer.
255 * @param flags I2C status flag mask (see @ref _i2c_status_flag enumeration.)
256 * @return I2C status, each bit represents one status flag
257 */
I2C_GetStatusFlag(I2C_Type * base,uint32_t flags)258 static inline uint32_t I2C_GetStatusFlag(I2C_Type* base, uint32_t flags)
259 {
260 return (I2C_I2SR_REG(base) & flags);
261 }
262
263 /*!
264 * @brief Clear one or more I2C status flag state.
265 *
266 * @param base I2C base pointer.
267 * @param flags I2C status flag mask (see @ref _i2c_status_flag enumeration.)
268 */
I2C_ClearStatusFlag(I2C_Type * base,uint32_t flags)269 static inline void I2C_ClearStatusFlag(I2C_Type* base, uint32_t flags)
270 {
271 /* Write 0 to clear. */
272 I2C_I2SR_REG(base) &= ~flags;
273 }
274
275 #ifdef __cplusplus
276 }
277 #endif
278
279 /*! @}*/
280
281 #endif /* __I2C_IMX_H__ */
282 /*******************************************************************************
283 * EOF
284 ******************************************************************************/
285