summaryrefslogtreecommitdiff
path: root/platform/drivers/inc/i2c_imx.h
blob: 566c70aba1e0080b5acaa89f63768c3e989f4102 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
/*
 * Copyright (c) 2015, Freescale Semiconductor, Inc.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:
 *
 * o Redistributions of source code must retain the above copyright notice, this list
 *   of conditions and the following disclaimer.
 *
 * o Redistributions in binary form must reproduce the above copyright notice, this
 *   list of conditions and the following disclaimer in the documentation and/or
 *   other materials provided with the distribution.
 *
 * o Neither the name of Freescale Semiconductor, Inc. nor the names of its
 *   contributors may be used to endorse or promote products derived from this
 *   software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#ifndef __I2C_IMX_H__
#define __I2C_IMX_H__

#include <stdint.h>
#include <stdbool.h>
#include <assert.h>
#include "device_imx.h"

/*!
 * @addtogroup i2c_imx_driver
 * @{
 */

/*******************************************************************************
 * Definitions
 ******************************************************************************/

/*! @brief I2C module initialize structure. */
typedef struct _i2c_init_config
{
    uint32_t clockRate;    /*!< Current I2C module clock freq. */
    uint32_t baudRate;     /*!< Desired I2C baud rate. */
    uint8_t  slaveAddress; /*!< I2C module's own address when addressed as slave device. */
} i2c_init_config_t;

/*! @brief Flag for I2C interrupt status check or polling status. */
enum _i2c_status_flag
{
    i2cStatusTransferComplete = I2C_I2SR_ICF_MASK,  /*!< Data Transfer complete flag. */
    i2cStatusAddressedAsSlave = I2C_I2SR_IAAS_MASK, /*!< Addressed as a slave flag. */
    i2cStatusBusBusy          = I2C_I2SR_IBB_MASK,  /*!< Bus is busy flag. */
    i2cStatusArbitrationLost  = I2C_I2SR_IAL_MASK,  /*!< Arbitration is lost flag. */
    i2cStatusSlaveReadWrite   = I2C_I2SR_SRW_MASK,  /*!< Master reading from slave flag(De-assert if master writing to slave). */
    i2cStatusInterrupt        = I2C_I2SR_IIF_MASK,  /*!< An interrupt is pending flag. */
    i2cStatusReceivedAck      = I2C_I2SR_RXAK_MASK, /*!< No acknowledge detected flag. */
};

/*! @brief I2C Bus role of this module. */
enum _i2c_work_mode
{
    i2cModeSlave  = 0x0,                /*!< This module works as I2C Slave. */
    i2cModeMaster = I2C_I2CR_MSTA_MASK, /*!< This module works as I2C Master. */
};

/*! @brief Data transfer direction. */
enum _i2c_direction_mode
{
    i2cDirectionReceive  = 0x0,               /*!< This module works at receive mode. */
    i2cDirectionTransmit = I2C_I2CR_MTX_MASK, /*!< This module works at transmit mode. */
};

/*******************************************************************************
 * API
 ******************************************************************************/

#if defined(__cplusplus)
extern "C" {
#endif

/*!
 * @name I2C Initialization and Configuration functions
 * @{
 */

/*!
 * @brief Initialize I2C module with given initialize structure.
 *
 * @param base I2C base pointer.
 * @param initConfig I2C initialize structure (see @ref i2c_init_config_t).
 */
void I2C_Init(I2C_Type* base, const i2c_init_config_t* initConfig);

/*!
 * @brief This function reset I2C module register content to its default value.
 *
 * @param base I2C base pointer.
 */
void I2C_Deinit(I2C_Type* base);

/*!
 * @brief This function is used to Enable the I2C Module.
 *
 * @param base I2C base pointer.
 */
static inline void I2C_Enable(I2C_Type* base)
{
    I2C_I2CR_REG(base) |= I2C_I2CR_IEN_MASK;
}

/*!
 * @brief This function is used to Disable the I2C Module.
 *
 * @param base I2C base pointer.
 */
static inline void I2C_Disable(I2C_Type* base)
{
    I2C_I2CR_REG(base) &= ~I2C_I2CR_IEN_MASK;
}

/*!
 * @brief This function is used to set the baud rate of I2C Module.
 *
 * @param base I2C base pointer.
 * @param clockRate I2C module clock frequency.
 * @param baudRate Desired I2C module baud rate.
 */
void I2C_SetBaudRate(I2C_Type* base, uint32_t clockRate, uint32_t baudRate);

/*!
 * @brief This function is used to set the own I2C bus address when addressed as a slave.
 *
 * @param base I2C base pointer.
 * @param slaveAddress Own I2C Bus address.
 */
static inline void I2C_SetSlaveAddress(I2C_Type* base, uint8_t slaveAddress)
{
    assert(slaveAddress < 0x80);

    I2C_IADR_REG(base) = (I2C_IADR_REG(base) & ~I2C_IADR_ADR_MASK) | I2C_IADR_ADR(slaveAddress);
}

/*!
 * @name I2C Bus Control functions
 * @{
 */

/*!
 * @brief This function is used to Generate a Repeat Start Signal on I2C Bus.
 *
 * @param base I2C base pointer.
 */
static inline void I2C_SendRepeatStart(I2C_Type* base)
{
    I2C_I2CR_REG(base) |= I2C_I2CR_RSTA_MASK;
}

/*!
 * @brief This function is used to select the I2C bus role of this module,
 *        both I2C Bus Master and Slave can be select.
 *
 * @param base I2C base pointer.
 * @param mode I2C Bus role to set (see @ref _i2c_work_mode enumeration).
 */
static inline void I2C_SetWorkMode(I2C_Type* base, uint32_t mode)
{
    assert((mode == i2cModeMaster) || (mode == i2cModeSlave));

    I2C_I2CR_REG(base) = (I2C_I2CR_REG(base) & ~I2C_I2CR_MSTA_MASK) | mode;
}

/*!
 * @brief This function is used to select the data transfer direction of this module,
 *        both Transmit and Receive can be select.
 *
 * @param base I2C base pointer.
 * @param direction I2C Bus data transfer direction (see @ref _i2c_direction_mode enumeration).
 */
static inline void I2C_SetDirMode(I2C_Type* base, uint32_t direction)
{
    assert((direction == i2cDirectionReceive) || (direction == i2cDirectionTransmit));

    I2C_I2CR_REG(base) = (I2C_I2CR_REG(base) & ~I2C_I2CR_MTX_MASK) | direction;
}

/*!
 * @brief This function is used to set the Transmit Acknowledge action when receive
 *        data from other device.
 *
 * @param base I2C base pointer.
 * @param ack The ACK value answerback to remote I2C device.
 *            - true: An acknowledge signal is sent to the bus at the ninth clock bit.
 *            - false: No acknowledge signal response is sent.
 */
void I2C_SetAckBit(I2C_Type* base, bool ack);

/*!
 * @name Data transfers functions
 * @{
 */

/*!
 * @brief Writes one byte of data to the I2C bus.
 *
 * @param base I2C base pointer.
 * @param byte The byte of data to transmit.
 */
static inline void I2C_WriteByte(I2C_Type* base, uint8_t byte)
{
    I2C_I2DR_REG(base) = byte;
}

/*!
 * @brief Returns the last byte of data read from the bus and initiate another read.
 *
 * In a master receive mode, calling this function initiates receiving the next byte of data.
 *
 * @param base I2C base pointer.
 * @return This function returns the last byte received while the I2C module is configured in master
 *         receive or slave receive mode.
 */
static inline uint8_t I2C_ReadByte(I2C_Type* base)
{
    return (uint8_t)(I2C_I2DR_REG(base) & I2C_I2DR_DATA_MASK);
}

/*!
 * @name Interrupts and flags management functions
 * @{
 */

/*!
 * @brief Enable or disable I2C interrupt requests.
 *
 * @param base I2C base pointer.
 * @param enable Enable/Disbale I2C interrupt.
 *               - true: Enable I2C interrupt.
 *               - false: Disable I2C interrupt.
 */
void I2C_SetIntCmd(I2C_Type* base, bool enable);

/*!
 * @brief Gets the I2C status flag state.
 *
 * @param base I2C base pointer.
 * @param flags I2C status flag mask (see @ref _i2c_status_flag enumeration.)
 * @return I2C status, each bit represents one status flag
 */
static inline uint32_t I2C_GetStatusFlag(I2C_Type* base, uint32_t flags)
{
    return (I2C_I2SR_REG(base) & flags);
}

/*!
 * @brief Clear one or more I2C status flag state.
 *
 * @param base I2C base pointer.
 * @param flags I2C status flag mask (see @ref _i2c_status_flag enumeration.)
 */
static inline void I2C_ClearStatusFlag(I2C_Type* base, uint32_t flags)
{
    /* Write 0 to clear. */
    I2C_I2SR_REG(base) &= ~flags;
}

#ifdef __cplusplus
}
#endif

/*! @}*/

#endif /* __I2C_IMX_H__ */
/*******************************************************************************
 * EOF
 ******************************************************************************/