1 /*
2 * Copyright (c) 2016, Freescale Semiconductor, Inc.
3 * Copyright 2016-2023 NXP
4 * All rights reserved.
5 *
6 * SPDX-License-Identifier: BSD-3-Clause
7 */
8 #ifndef _FSL_I2C_H_
9 #define _FSL_I2C_H_
10
11 #include <stddef.h>
12 #include "fsl_device_registers.h"
13 #include "fsl_common.h"
14
15 /*******************************************************************************
16 * Definitions
17 ******************************************************************************/
18
19 #define I2C_CFG_MASK 0x1f
20
21 /*!
22 * @addtogroup i2c_driver
23 * @{
24 */
25
26 /*! @file */
27
28 /*! @name Driver version */
29 /*@{*/
30 /*! @brief I2C driver version. */
31 #define FSL_I2C_DRIVER_VERSION (MAKE_VERSION(2, 3, 3))
32 /*@}*/
33
34 /*! @brief Retry times for waiting flag. */
35 #ifndef I2C_RETRY_TIMES
36 #define I2C_RETRY_TIMES 0U /* Define to zero means keep waiting until the flag is assert/deassert. */
37 #endif
38
39 /*! @brief Whether to ignore the nack signal of the last byte during master transmit. */
40 #ifndef I2C_MASTER_TRANSMIT_IGNORE_LAST_NACK
41 #define I2C_MASTER_TRANSMIT_IGNORE_LAST_NACK \
42 1U /* Define to one means master ignores the last byte's nack and considers the transfer successful. */
43 #endif
44
45 /* definitions for MSTCODE bits in I2C Status register STAT */
46 #define I2C_STAT_MSTCODE_IDLE (0U) /*!< Master Idle State Code */
47 #define I2C_STAT_MSTCODE_RXREADY (1U) /*!< Master Receive Ready State Code */
48 #define I2C_STAT_MSTCODE_TXREADY (2U) /*!< Master Transmit Ready State Code */
49 #define I2C_STAT_MSTCODE_NACKADR (3U) /*!< Master NACK by slave on address State Code */
50 #define I2C_STAT_MSTCODE_NACKDAT (4U) /*!< Master NACK by slave on data State Code */
51
52 /* definitions for SLVSTATE bits in I2C Status register STAT */
53 #define I2C_STAT_SLVST_ADDR (0)
54 #define I2C_STAT_SLVST_RX (1)
55 #define I2C_STAT_SLVST_TX (2)
56
57 /*! @brief I2C status return codes. */
58 enum
59 {
60 kStatus_I2C_Busy = MAKE_STATUS(kStatusGroup_FLEXCOMM_I2C, 0), /*!< The master is already performing a transfer. */
61 kStatus_I2C_Idle = MAKE_STATUS(kStatusGroup_FLEXCOMM_I2C, 1), /*!< The slave driver is idle. */
62 kStatus_I2C_Nak =
63 MAKE_STATUS(kStatusGroup_FLEXCOMM_I2C, 2), /*!< The slave device sent a NAK in response to a byte. */
64 kStatus_I2C_InvalidParameter =
65 MAKE_STATUS(kStatusGroup_FLEXCOMM_I2C, 3), /*!< Unable to proceed due to invalid parameter. */
66 kStatus_I2C_BitError = MAKE_STATUS(kStatusGroup_FLEXCOMM_I2C, 4), /*!< Transferred bit was not seen on the bus. */
67 kStatus_I2C_ArbitrationLost = MAKE_STATUS(kStatusGroup_FLEXCOMM_I2C, 5), /*!< Arbitration lost error. */
68 kStatus_I2C_NoTransferInProgress =
69 MAKE_STATUS(kStatusGroup_FLEXCOMM_I2C, 6), /*!< Attempt to abort a transfer when one is not in progress. */
70 kStatus_I2C_DmaRequestFail = MAKE_STATUS(kStatusGroup_FLEXCOMM_I2C, 7), /*!< DMA request failed. */
71 kStatus_I2C_StartStopError = MAKE_STATUS(kStatusGroup_FLEXCOMM_I2C, 8), /*!< Start and stop error. */
72 kStatus_I2C_UnexpectedState = MAKE_STATUS(kStatusGroup_FLEXCOMM_I2C, 9), /*!< Unexpected state. */
73 kStatus_I2C_Timeout =
74 MAKE_STATUS(kStatusGroup_FLEXCOMM_I2C,
75 10), /*!< Timeout when waiting for I2C master/slave pending status to set to continue transfer. */
76 kStatus_I2C_Addr_Nak = MAKE_STATUS(kStatusGroup_FLEXCOMM_I2C, 11), /*!< NAK received for Address */
77 kStatus_I2C_EventTimeout = MAKE_STATUS(kStatusGroup_FLEXCOMM_I2C, 12), /*!< Timeout waiting for bus event. */
78 kStatus_I2C_SclLowTimeout = MAKE_STATUS(kStatusGroup_FLEXCOMM_I2C, 13), /*!< Timeout SCL signal remains low. */
79 };
80
81 /*! @} */
82
83 /*!
84 * @addtogroup i2c_driver
85 * @{
86 */
87
88 /*!
89 * @brief I2C status flags.
90 *
91 * @note These enums are meant to be OR'd together to form a bit mask.
92 */
93 enum _i2c_status_flags
94 {
95 kI2C_MasterPendingFlag = I2C_STAT_MSTPENDING_MASK, /*!< The I2C module is waiting for software interaction. bit 0 */
96 kI2C_MasterArbitrationLostFlag =
97 I2C_STAT_MSTARBLOSS_MASK, /*!< The arbitration of the bus was lost. There was collision on the bus. bit 4*/
98 kI2C_MasterStartStopErrorFlag =
99 I2C_STAT_MSTSTSTPERR_MASK, /*!< There was an error during start or stop phase of the transaction. bit 6 */
100 kI2C_MasterIdleFlag = 1UL << 5U, /*!< The I2C master idle status. bit 5 */
101 kI2C_MasterRxReadyFlag = 1UL << I2C_STAT_MSTSTATE_SHIFT, /*!< The I2C master rx ready status. bit 1 */
102 kI2C_MasterTxReadyFlag = 1UL << (I2C_STAT_MSTSTATE_SHIFT + 1U), /*!< The I2C master tx ready status. bit 2 */
103 kI2C_MasterAddrNackFlag = 1UL << 7U, /*!< The I2C master address nack status. bit 7 */
104 kI2C_MasterDataNackFlag = 1UL << (I2C_STAT_MSTSTATE_SHIFT + 2U), /*!< The I2C master data nack status. bit 3 */
105 kI2C_SlavePendingFlag = I2C_STAT_SLVPENDING_MASK, /*!< The I2C module is waiting for software interaction. bit 8 */
106 kI2C_SlaveNotStretching = I2C_STAT_SLVNOTSTR_MASK, /*!< Indicates whether the slave is currently stretching clock (0
107 = yes, 1 = no). bit 11 */
108 kI2C_SlaveSelected =
109 I2C_STAT_SLVSEL_MASK, /*!< Indicates whether the slave is selected by an address match. bit 14 */
110 kI2C_SaveDeselected = I2C_STAT_SLVDESEL_MASK, /*!< Indicates that slave was previously deselected (deselect event
111 took place, w1c). bit 15 */
112 kI2C_SlaveAddressedFlag = 1UL << 22U, /*!< One of the I2C slave's 4 addresses is matched. bit 22 */
113 kI2C_SlaveReceiveFlag = 1UL << I2C_STAT_SLVSTATE_SHIFT, /*!< Slave receive data available. bit 9 */
114 kI2C_SlaveTransmitFlag = 1UL << (I2C_STAT_SLVSTATE_SHIFT + 1U), /*!< Slave data can be transmitted. bit 10 */
115 kI2C_SlaveAddress0MatchFlag = 1UL << 20U, /*!< Slave address0 match. bit 20 */
116 kI2C_SlaveAddress1MatchFlag = 1UL << I2C_STAT_SLVIDX_SHIFT, /*!< Slave address1 match. bit 12 */
117 kI2C_SlaveAddress2MatchFlag = 1UL << (I2C_STAT_SLVIDX_SHIFT + 1U), /*!< Slave address2 match. bit 13 */
118 kI2C_SlaveAddress3MatchFlag = 1UL << 21U, /*!< Slave address3 match. bit 21 */
119 kI2C_MonitorReadyFlag = I2C_STAT_MONRDY_MASK, /*!< The I2C monitor ready interrupt. bit 16 */
120 kI2C_MonitorOverflowFlag = I2C_STAT_MONOV_MASK, /*!< The monitor data overrun interrupt. bit 17 */
121 kI2C_MonitorActiveFlag = I2C_STAT_MONACTIVE_MASK, /*!< The monitor is active. bit 18 */
122 kI2C_MonitorIdleFlag = I2C_STAT_MONIDLE_MASK, /*!< The monitor idle interrupt. bit 19 */
123 kI2C_EventTimeoutFlag = I2C_STAT_EVENTTIMEOUT_MASK, /*!< The bus event timeout interrupt. bit 24 */
124 kI2C_SclTimeoutFlag = I2C_STAT_SCLTIMEOUT_MASK, /*!< The SCL timeout interrupt. bit 25 */
125
126 /* All master flags that can be cleared by software */
127 kI2C_MasterAllClearFlags = kI2C_MasterArbitrationLostFlag | kI2C_MasterStartStopErrorFlag,
128 /* All slave flags that can be cleared by software */
129 kI2C_SlaveAllClearFlags = kI2C_SaveDeselected,
130 /* All common flags that can be cleared by software */
131 kI2C_CommonAllClearFlags =
132 kI2C_MonitorOverflowFlag | kI2C_MonitorIdleFlag | kI2C_EventTimeoutFlag | kI2C_SclTimeoutFlag,
133 };
134
135 /*!
136 * @brief I2C interrupt enable.
137 *
138 * @note These enums are meant to be OR'd together to form a bit mask.
139 */
140 enum _i2c_interrupt_enable
141 {
142 kI2C_MasterPendingInterruptEnable =
143 I2C_STAT_MSTPENDING_MASK, /*!< The I2C master communication pending interrupt. */
144 kI2C_MasterArbitrationLostInterruptEnable =
145 I2C_STAT_MSTARBLOSS_MASK, /*!< The I2C master arbitration lost interrupt. */
146 kI2C_MasterStartStopErrorInterruptEnable =
147 I2C_STAT_MSTSTSTPERR_MASK, /*!< The I2C master start/stop timing error interrupt. */
148 kI2C_SlavePendingInterruptEnable = I2C_STAT_SLVPENDING_MASK, /*!< The I2C slave communication pending interrupt. */
149 kI2C_SlaveNotStretchingInterruptEnable =
150 I2C_STAT_SLVNOTSTR_MASK, /*!< The I2C slave not streching interrupt, deep-sleep mode can be entered only when
151 this interrupt occurs. */
152 kI2C_SlaveDeselectedInterruptEnable = I2C_STAT_SLVDESEL_MASK, /*!< The I2C slave deselection interrupt. */
153 kI2C_MonitorReadyInterruptEnable = I2C_STAT_MONRDY_MASK, /*!< The I2C monitor ready interrupt. */
154 kI2C_MonitorOverflowInterruptEnable = I2C_STAT_MONOV_MASK, /*!< The monitor data overrun interrupt. */
155 kI2C_MonitorIdleInterruptEnable = I2C_STAT_MONIDLE_MASK, /*!< The monitor idle interrupt. */
156 kI2C_EventTimeoutInterruptEnable = I2C_STAT_EVENTTIMEOUT_MASK, /*!< The bus event timeout interrupt. */
157 kI2C_SclTimeoutInterruptEnable = I2C_STAT_SCLTIMEOUT_MASK, /*!< The SCL timeout interrupt. */
158
159 /* All master interrupt sources */
160 kI2C_MasterAllInterruptEnable = kI2C_MasterPendingInterruptEnable | kI2C_MasterArbitrationLostInterruptEnable |
161 kI2C_MasterStartStopErrorInterruptEnable,
162 /* All slave interrupt sources */
163 kI2C_SlaveAllInterruptEnable =
164 kI2C_SlavePendingInterruptEnable | kI2C_SlaveNotStretchingInterruptEnable | kI2C_SlaveDeselectedInterruptEnable,
165 /* All common interrupt sources */
166 kI2C_CommonAllInterruptEnable = kI2C_MonitorReadyInterruptEnable | kI2C_MonitorOverflowInterruptEnable |
167 kI2C_MonitorIdleInterruptEnable | kI2C_EventTimeoutInterruptEnable |
168 kI2C_SclTimeoutInterruptEnable,
169 };
170 /*! @} */
171
172 /*!
173 * @addtogroup i2c_master_driver
174 * @{
175 */
176
177 /*! @brief Direction of master and slave transfers. */
178 typedef enum _i2c_direction
179 {
180 kI2C_Write = 0U, /*!< Master transmit. */
181 kI2C_Read = 1U /*!< Master receive. */
182 } i2c_direction_t;
183
184 /*!
185 * @brief Structure with settings to initialize the I2C master module.
186 *
187 * This structure holds configuration settings for the I2C peripheral. To initialize this
188 * structure to reasonable defaults, call the I2C_MasterGetDefaultConfig() function and
189 * pass a pointer to your configuration structure instance.
190 *
191 * The configuration structure can be made constant so it resides in flash.
192 */
193 typedef struct _i2c_master_config
194 {
195 bool enableMaster; /*!< Whether to enable master mode. */
196 uint32_t baudRate_Bps; /*!< Desired baud rate in bits per second. */
197 bool enableTimeout; /*!< Enable internal timeout function. */
198 uint8_t timeout_Ms; /*!< Event timeout and SCL low timeout value. */
199 } i2c_master_config_t;
200
201 /* Forward declaration of the transfer descriptor and handle typedefs. */
202 /*! @brief I2C master transfer typedef */
203 typedef struct _i2c_master_transfer i2c_master_transfer_t;
204
205 /*! @brief I2C master handle typedef */
206 typedef struct _i2c_master_handle i2c_master_handle_t;
207
208 /*!
209 * @brief Master completion callback function pointer type.
210 *
211 * This callback is used only for the non-blocking master transfer API. Specify the callback you wish to use
212 * in the call to I2C_MasterTransferCreateHandle().
213 *
214 * @param base The I2C peripheral base address.
215 * @param completionStatus Either kStatus_Success or an error code describing how the transfer completed.
216 * @param userData Arbitrary pointer-sized value passed from the application.
217 */
218 typedef void (*i2c_master_transfer_callback_t)(I2C_Type *base,
219 i2c_master_handle_t *handle,
220 status_t completionStatus,
221 void *userData);
222
223 /*!
224 * @brief Transfer option flags.
225 *
226 * @note These enumerations are intended to be OR'd together to form a bit mask of options for
227 * the #_i2c_master_transfer::flags field.
228 */
229 enum _i2c_master_transfer_flags
230 {
231 kI2C_TransferDefaultFlag = 0x00U, /*!< Transfer starts with a start signal, stops with a stop signal. */
232 kI2C_TransferNoStartFlag = 0x01U, /*!< Don't send a start condition, address, and sub address */
233 kI2C_TransferRepeatedStartFlag = 0x02U, /*!< Send a repeated start condition */
234 kI2C_TransferNoStopFlag = 0x04U, /*!< Don't send a stop condition. */
235 };
236
237 /*! @brief States for the state machine used by transactional APIs. */
238 enum _i2c_transfer_states
239 {
240 kIdleState = 0,
241 kTransmitSubaddrState,
242 kTransmitDataState,
243 kReceiveDataBeginState,
244 kReceiveDataState,
245 kReceiveLastDataState,
246 kStartState,
247 kStopState,
248 kWaitForCompletionState
249 };
250
251 /*!
252 * @brief Non-blocking transfer descriptor structure.
253 *
254 * This structure is used to pass transaction parameters to the I2C_MasterTransferNonBlocking() API.
255 */
256 struct _i2c_master_transfer
257 {
258 uint32_t flags; /*!< Bit mask of options for the transfer. See enumeration #_i2c_master_transfer_flags for available
259 options. Set to 0 or #kI2C_TransferDefaultFlag for normal transfers. */
260 uint8_t slaveAddress; /*!< The 7-bit slave address. */
261 i2c_direction_t direction; /*!< Either #kI2C_Read or #kI2C_Write. */
262 uint32_t subaddress; /*!< Sub address. Transferred MSB first. */
263 size_t subaddressSize; /*!< Length of sub address to send in bytes. Maximum size is 4 bytes. */
264 void *data; /*!< Pointer to data to transfer. */
265 size_t dataSize; /*!< Number of bytes to transfer. */
266 };
267
268 /*!
269 * @brief Driver handle for master non-blocking APIs.
270 * @note The contents of this structure are private and subject to change.
271 */
272 struct _i2c_master_handle
273 {
274 uint8_t state; /*!< Transfer state machine current state. */
275 uint32_t transferCount; /*!< Indicates progress of the transfer */
276 uint32_t remainingBytes; /*!< Remaining byte count in current state. */
277 uint8_t *buf; /*!< Buffer pointer for current state. */
278 uint32_t remainingSubaddr;
279 uint8_t subaddrBuf[4];
280 bool checkAddrNack; /*!< Whether to check the nack signal is detected during addressing. */
281 i2c_master_transfer_t transfer; /*!< Copy of the current transfer info. */
282 i2c_master_transfer_callback_t completionCallback; /*!< Callback function pointer. */
283 void *userData; /*!< Application data passed to callback. */
284 };
285
286 /*! @} */
287
288 /*!
289 * @addtogroup i2c_slave_driver
290 * @{
291 */
292 /*! @brief I2C slave address register. */
293 typedef enum _i2c_slave_address_register
294 {
295 kI2C_SlaveAddressRegister0 = 0U, /*!< Slave Address 0 register. */
296 kI2C_SlaveAddressRegister1 = 1U, /*!< Slave Address 1 register. */
297 kI2C_SlaveAddressRegister2 = 2U, /*!< Slave Address 2 register. */
298 kI2C_SlaveAddressRegister3 = 3U, /*!< Slave Address 3 register. */
299 } i2c_slave_address_register_t;
300
301 /*! @brief Data structure with 7-bit Slave address and Slave address disable. */
302 typedef struct _i2c_slave_address
303 {
304 uint8_t address; /*!< 7-bit Slave address SLVADR. */
305 bool addressDisable; /*!< Slave address disable SADISABLE. */
306 } i2c_slave_address_t;
307
308 /*! @brief I2C slave address match options. */
309 typedef enum _i2c_slave_address_qual_mode
310 {
311 kI2C_QualModeMask = 0U, /*!< The SLVQUAL0 field (qualAddress) is used as a logical mask for matching address0. */
312 kI2C_QualModeExtend =
313 1U, /*!< The SLVQUAL0 (qualAddress) field is used to extend address 0 matching in a range of addresses. */
314 } i2c_slave_address_qual_mode_t;
315
316 /*! @brief I2C slave bus speed options. */
317 typedef enum _i2c_slave_bus_speed
318 {
319 kI2C_SlaveStandardMode = 0U,
320 kI2C_SlaveFastMode = 1U,
321 kI2C_SlaveFastModePlus = 2U,
322 kI2C_SlaveHsMode = 3U,
323 } i2c_slave_bus_speed_t;
324
325 /*!
326 * @brief Structure with settings to initialize the I2C slave module.
327 *
328 * This structure holds configuration settings for the I2C slave peripheral. To initialize this
329 * structure to reasonable defaults, call the I2C_SlaveGetDefaultConfig() function and
330 * pass a pointer to your configuration structure instance.
331 *
332 * The configuration structure can be made constant so it resides in flash.
333 */
334 typedef struct _i2c_slave_config
335 {
336 i2c_slave_address_t address0; /*!< Slave's 7-bit address and disable. */
337 i2c_slave_address_t address1; /*!< Alternate slave 7-bit address and disable. */
338 i2c_slave_address_t address2; /*!< Alternate slave 7-bit address and disable. */
339 i2c_slave_address_t address3; /*!< Alternate slave 7-bit address and disable. */
340 i2c_slave_address_qual_mode_t qualMode; /*!< Qualify mode for slave address 0. */
341 uint8_t qualAddress; /*!< Slave address qualifier for address 0. */
342 i2c_slave_bus_speed_t
343 busSpeed; /*!< Slave bus speed mode. If the slave function stretches SCL to allow for software response, it must
344 provide sufficient data setup time to the master before releasing the stretched clock.
345 This is accomplished by inserting one clock time of CLKDIV at that point.
346 The #busSpeed value is used to configure CLKDIV
347 such that one clock time is greater than the tSU;DAT value noted
348 in the I2C bus specification for the I2C mode that is being used.
349 If the #busSpeed mode is unknown at compile time, use the longest data setup time
350 kI2C_SlaveStandardMode (250 ns) */
351 bool enableSlave; /*!< Enable slave mode. */
352 } i2c_slave_config_t;
353
354 /*!
355 * @brief Set of events sent to the callback for non blocking slave transfers.
356 *
357 * These event enumerations are used for two related purposes. First, a bit mask created by OR'ing together
358 * events is passed to I2C_SlaveTransferNonBlocking() in order to specify which events to enable.
359 * Then, when the slave callback is invoked, it is passed the current event through its @a transfer
360 * parameter.
361 *
362 * @note These enumerations are meant to be OR'd together to form a bit mask of events.
363 */
364 typedef enum _i2c_slave_transfer_event
365 {
366 kI2C_SlaveAddressMatchEvent = 0x01U, /*!< Received the slave address after a start or repeated start. */
367 kI2C_SlaveTransmitEvent = 0x02U, /*!< Callback is requested to provide data to transmit
368 (slave-transmitter role). */
369 kI2C_SlaveReceiveEvent = 0x04U, /*!< Callback is requested to provide a buffer in which to place received
370 data (slave-receiver role). */
371 kI2C_SlaveCompletionEvent = 0x20U, /*!< All data in the active transfer have been consumed. */
372 kI2C_SlaveDeselectedEvent =
373 0x40U, /*!< The slave function has become deselected (SLVSEL flag changing from 1 to 0. */
374
375 /*! Bit mask of all available events. */
376 kI2C_SlaveAllEvents = kI2C_SlaveAddressMatchEvent | kI2C_SlaveTransmitEvent | kI2C_SlaveReceiveEvent |
377 kI2C_SlaveCompletionEvent | kI2C_SlaveDeselectedEvent,
378 } i2c_slave_transfer_event_t;
379
380 /*! @brief I2C slave handle typedef. */
381 typedef struct _i2c_slave_handle i2c_slave_handle_t;
382
383 /*! @brief I2C slave transfer structure */
384 typedef struct _i2c_slave_transfer
385 {
386 i2c_slave_handle_t *handle; /*!< Pointer to handle that contains this transfer. */
387 i2c_slave_transfer_event_t event; /*!< Reason the callback is being invoked. */
388 uint8_t receivedAddress; /*!< Matching address send by master. 7-bits plus R/nW bit0 */
389 uint32_t eventMask; /*!< Mask of enabled events. */
390 uint8_t *rxData; /*!< Transfer buffer for receive data */
391 const uint8_t *txData; /*!< Transfer buffer for transmit data */
392 size_t txSize; /*!< Transfer size */
393 size_t rxSize; /*!< Transfer size */
394 size_t transferredCount; /*!< Number of bytes transferred during this transfer. */
395 status_t completionStatus; /*!< Success or error code describing how the transfer completed. Only applies for
396 #kI2C_SlaveCompletionEvent. */
397 } i2c_slave_transfer_t;
398
399 /*!
400 * @brief Slave event callback function pointer type.
401 *
402 * This callback is used only for the slave non-blocking transfer API. To install a callback,
403 * use the I2C_SlaveSetCallback() function after you have created a handle.
404 *
405 * @param base Base address for the I2C instance on which the event occurred.
406 * @param transfer Pointer to transfer descriptor containing values passed to and/or from the callback.
407 * @param userData Arbitrary pointer-sized value passed from the application.
408 */
409 typedef void (*i2c_slave_transfer_callback_t)(I2C_Type *base, volatile i2c_slave_transfer_t *transfer, void *userData);
410
411 /*!
412 * @brief I2C slave software finite state machine states.
413 */
414 typedef enum _i2c_slave_fsm
415 {
416 kI2C_SlaveFsmAddressMatch = 0u,
417 kI2C_SlaveFsmReceive = 2u,
418 kI2C_SlaveFsmTransmit = 3u,
419 } i2c_slave_fsm_t;
420
421 /*!
422 * @brief I2C slave handle structure.
423 * @note The contents of this structure are private and subject to change.
424 */
425 struct _i2c_slave_handle
426 {
427 volatile i2c_slave_transfer_t transfer; /*!< I2C slave transfer. */
428 volatile bool isBusy; /*!< Whether transfer is busy. */
429 volatile i2c_slave_fsm_t slaveFsm; /*!< slave transfer state machine. */
430 i2c_slave_transfer_callback_t callback; /*!< Callback function called at transfer event. */
431 void *userData; /*!< Callback parameter passed to callback. */
432 };
433
434 /*! @brief Typedef for master interrupt handler. */
435 typedef void (*flexcomm_i2c_master_irq_handler_t)(I2C_Type *base, i2c_master_handle_t *handle);
436
437 /*! @brief Typedef for slave interrupt handler. */
438 typedef void (*flexcomm_i2c_slave_irq_handler_t)(I2C_Type *base, i2c_slave_handle_t *handle);
439 /*! @} */
440
441 /*******************************************************************************
442 * API
443 ******************************************************************************/
444
445 #if defined(__cplusplus)
446 extern "C" {
447 #endif
448
449 /*!
450 * @addtogroup i2c_master_driver
451 * @{
452 */
453
454 /*! @name Initialization and deinitialization */
455 /*@{*/
456
457 /*!
458 * @brief Provides a default configuration for the I2C master peripheral.
459 *
460 * This function provides the following default configuration for the I2C master peripheral:
461 * @code
462 * masterConfig->enableMaster = true;
463 * masterConfig->baudRate_Bps = 100000U;
464 * masterConfig->enableTimeout = false;
465 * @endcode
466 *
467 * After calling this function, you can override any settings in order to customize the configuration,
468 * prior to initializing the master driver with I2C_MasterInit().
469 *
470 * @param[out] masterConfig User provided configuration structure for default values. Refer to #i2c_master_config_t.
471 */
472 void I2C_MasterGetDefaultConfig(i2c_master_config_t *masterConfig);
473
474 /*!
475 * @brief Initializes the I2C master peripheral.
476 *
477 * This function enables the peripheral clock and initializes the I2C master peripheral as described by the user
478 * provided configuration. A software reset is performed prior to configuration.
479 *
480 * @param base The I2C peripheral base address.
481 * @param masterConfig User provided peripheral configuration. Use I2C_MasterGetDefaultConfig() to get a set of
482 * defaults
483 * that you can override.
484 * @param srcClock_Hz Frequency in Hertz of the I2C functional clock. Used to calculate the baud rate divisors,
485 * filter widths, and timeout periods.
486 */
487 void I2C_MasterInit(I2C_Type *base, const i2c_master_config_t *masterConfig, uint32_t srcClock_Hz);
488
489 /*!
490 * @brief Deinitializes the I2C master peripheral.
491 *
492 * This function disables the I2C master peripheral and gates the clock. It also performs a software
493 * reset to restore the peripheral to reset conditions.
494 *
495 * @param base The I2C peripheral base address.
496 */
497 void I2C_MasterDeinit(I2C_Type *base);
498
499 /*!
500 * @brief Returns an instance number given a base address.
501 *
502 * If an invalid base address is passed, debug builds will assert. Release builds will just return
503 * instance number 0.
504 *
505 * @param base The I2C peripheral base address.
506 * @return I2C instance number starting from 0.
507 */
508 uint32_t I2C_GetInstance(I2C_Type *base);
509
510 /*!
511 * @brief Performs a software reset.
512 *
513 * Restores the I2C master peripheral to reset conditions.
514 *
515 * @param base The I2C peripheral base address.
516 */
I2C_MasterReset(I2C_Type * base)517 static inline void I2C_MasterReset(I2C_Type *base)
518 {
519 }
520
521 /*!
522 * @brief Enables or disables the I2C module as master.
523 *
524 * @param base The I2C peripheral base address.
525 * @param enable Pass true to enable or false to disable the specified I2C as master.
526 */
I2C_MasterEnable(I2C_Type * base,bool enable)527 static inline void I2C_MasterEnable(I2C_Type *base, bool enable)
528 {
529 if (enable)
530 {
531 base->CFG = (base->CFG & (uint32_t)I2C_CFG_MASK) | I2C_CFG_MSTEN_MASK;
532 }
533 else
534 {
535 base->CFG = (base->CFG & (uint32_t)I2C_CFG_MASK) & ~I2C_CFG_MSTEN_MASK;
536 }
537 }
538
539 /*@}*/
540
541 /*! @name Status */
542 /*@{*/
543 /*!
544 * @brief Gets the I2C status flags.
545 *
546 * A bit mask with the state of all I2C status flags is returned. For each flag, the corresponding bit
547 * in the return value is set if the flag is asserted.
548 *
549 * @param base The I2C peripheral base address.
550 * @return State of the status flags:
551 * - 1: related status flag is set.
552 * - 0: related status flag is not set.
553 * @see @ref _i2c_status_flags.
554 */
555 uint32_t I2C_GetStatusFlags(I2C_Type *base);
556
557 /*!
558 * @brief Clears the I2C status flag state.
559 *
560 * Refer to kI2C_CommonAllClearStatusFlags, kI2C_MasterAllClearStatusFlags and kI2C_SlaveAllClearStatusFlags to see
561 * the clearable flags. Attempts to clear other flags has no effect.
562 *
563 * @param base The I2C peripheral base address.
564 * @param statusMask A bitmask of status flags that are to be cleared. The mask is composed of the members in
565 * kI2C_CommonAllClearStatusFlags, kI2C_MasterAllClearStatusFlags and kI2C_SlaveAllClearStatusFlags. You may pass
566 * the result of a previous call to I2C_GetStatusFlags().
567 * @see #_i2c_status_flags, _i2c_master_status_flags and _i2c_slave_status_flags.
568 */
I2C_ClearStatusFlags(I2C_Type * base,uint32_t statusMask)569 static inline void I2C_ClearStatusFlags(I2C_Type *base, uint32_t statusMask)
570 {
571 /* Only deal with the clearable flags */
572 statusMask &=
573 ((uint32_t)kI2C_CommonAllClearFlags | (uint32_t)kI2C_MasterAllClearFlags | (uint32_t)kI2C_SlaveAllClearFlags);
574 base->STAT = statusMask;
575 }
576
577 /*!
578 * @brief Clears the I2C master status flag state.
579 * @deprecated Do not use this function. It has been superceded by @ref I2C_ClearStatusFlags
580 * The following status register flags can be cleared:
581 * - #kI2C_MasterArbitrationLostFlag
582 * - #kI2C_MasterStartStopErrorFlag
583 *
584 * Attempts to clear other flags has no effect.
585 *
586 * @param base The I2C peripheral base address.
587 * @param statusMask A bitmask of status flags that are to be cleared. The mask is composed of
588 * #_i2c_status_flags enumerators OR'd together. You may pass the result of a previous call to
589 * I2C_GetStatusFlags().
590 * @see _i2c_status_flags.
591 */
I2C_MasterClearStatusFlags(I2C_Type * base,uint32_t statusMask)592 static inline void I2C_MasterClearStatusFlags(I2C_Type *base, uint32_t statusMask)
593 {
594 /* Allow clearing just master status flags */
595 base->STAT = statusMask & (I2C_STAT_MSTARBLOSS_MASK | I2C_STAT_MSTSTSTPERR_MASK);
596 }
597
598 /*@}*/
599
600 /*! @name Interrupts */
601 /*@{*/
602
603 /*!
604 * @brief Enables the I2C interrupt requests.
605 *
606 * @param base The I2C peripheral base address.
607 * @param interruptMask Bit mask of interrupts to enable. See #_i2c_interrupt_enable for the set
608 * of constants that should be OR'd together to form the bit mask.
609 */
I2C_EnableInterrupts(I2C_Type * base,uint32_t interruptMask)610 static inline void I2C_EnableInterrupts(I2C_Type *base, uint32_t interruptMask)
611 {
612 base->INTENSET = interruptMask;
613 }
614
615 /*!
616 * @brief Disables the I2C interrupt requests.
617 *
618 * @param base The I2C peripheral base address.
619 * @param interruptMask Bit mask of interrupts to disable. See #_i2c_interrupt_enable for the set
620 * of constants that should be OR'd together to form the bit mask.
621 */
I2C_DisableInterrupts(I2C_Type * base,uint32_t interruptMask)622 static inline void I2C_DisableInterrupts(I2C_Type *base, uint32_t interruptMask)
623 {
624 base->INTENCLR = interruptMask;
625 }
626
627 /*!
628 * @brief Returns the set of currently enabled I2C interrupt requests.
629 *
630 * @param base The I2C peripheral base address.
631 * @return A bitmask composed of #_i2c_interrupt_enable enumerators OR'd together to indicate the
632 * set of enabled interrupts.
633 */
I2C_GetEnabledInterrupts(I2C_Type * base)634 static inline uint32_t I2C_GetEnabledInterrupts(I2C_Type *base)
635 {
636 return base->INTSTAT;
637 }
638
639 /*@}*/
640
641 /*! @name Bus operations */
642 /*@{*/
643
644 /*!
645 * @brief Sets the I2C bus frequency for master transactions.
646 *
647 * The I2C master is automatically disabled and re-enabled as necessary to configure the baud
648 * rate. Do not call this function during a transfer, or the transfer is aborted.
649 *
650 * @param base The I2C peripheral base address.
651 * @param srcClock_Hz I2C functional clock frequency in Hertz.
652 * @param baudRate_Bps Requested bus frequency in bits per second.
653 */
654 void I2C_MasterSetBaudRate(I2C_Type *base, uint32_t baudRate_Bps, uint32_t srcClock_Hz);
655
656 /*!
657 * @brief Sets the I2C bus timeout value.
658 *
659 * If the SCL signal remains low or bus does not have event longer than the timeout value, kI2C_SclTimeoutFlag or
660 * kI2C_EventTimeoutFlag is set. This can indicete the bus is held by slave or any fault occurs to the I2C module.
661 *
662 * @param base The I2C peripheral base address.
663 * @param timeout_Ms Timeout value in millisecond.
664 * @param srcClock_Hz I2C functional clock frequency in Hertz.
665 */
666 void I2C_MasterSetTimeoutValue(I2C_Type *base, uint8_t timeout_Ms, uint32_t srcClock_Hz);
667
668 /*!
669 * @brief Returns whether the bus is idle.
670 *
671 * Requires the master mode to be enabled.
672 *
673 * @param base The I2C peripheral base address.
674 * @retval true Bus is busy.
675 * @retval false Bus is idle.
676 */
I2C_MasterGetBusIdleState(I2C_Type * base)677 static inline bool I2C_MasterGetBusIdleState(I2C_Type *base)
678 {
679 /* True if MSTPENDING flag is set and MSTSTATE is zero == idle */
680 return ((base->STAT & (I2C_STAT_MSTPENDING_MASK | I2C_STAT_MSTSTATE_MASK)) == I2C_STAT_MSTPENDING_MASK);
681 }
682
683 /*!
684 * @brief Sends a START on the I2C bus.
685 *
686 * This function is used to initiate a new master mode transfer by sending the START signal.
687 * The slave address is sent following the I2C START signal.
688 *
689 * @param base I2C peripheral base pointer
690 * @param address 7-bit slave device address.
691 * @param direction Master transfer directions(transmit/receive).
692 * @retval kStatus_Success Successfully send the start signal.
693 * @retval kStatus_I2C_Busy Current bus is busy.
694 */
695 status_t I2C_MasterStart(I2C_Type *base, uint8_t address, i2c_direction_t direction);
696
697 /*!
698 * @brief Sends a STOP signal on the I2C bus.
699 *
700 * @retval kStatus_Success Successfully send the stop signal.
701 * @retval kStatus_I2C_Timeout Send stop signal failed, timeout.
702 */
703 status_t I2C_MasterStop(I2C_Type *base);
704
705 /*!
706 * @brief Sends a REPEATED START on the I2C bus.
707 *
708 * @param base I2C peripheral base pointer
709 * @param address 7-bit slave device address.
710 * @param direction Master transfer directions(transmit/receive).
711 * @retval kStatus_Success Successfully send the start signal.
712 * @retval kStatus_I2C_Busy Current bus is busy but not occupied by current I2C master.
713 */
I2C_MasterRepeatedStart(I2C_Type * base,uint8_t address,i2c_direction_t direction)714 static inline status_t I2C_MasterRepeatedStart(I2C_Type *base, uint8_t address, i2c_direction_t direction)
715 {
716 return I2C_MasterStart(base, address, direction);
717 }
718
719 /*!
720 * @brief Performs a polling send transfer on the I2C bus.
721 *
722 * Sends up to @a txSize number of bytes to the previously addressed slave device. The slave may
723 * reply with a NAK to any byte in order to terminate the transfer early. If this happens, this
724 * function returns #kStatus_I2C_Nak.
725 *
726 * @param base The I2C peripheral base address.
727 * @param txBuff The pointer to the data to be transferred.
728 * @param txSize The length in bytes of the data to be transferred.
729 * @param flags Transfer control flag to control special behavior like suppressing start or stop, for normal transfers
730 * use kI2C_TransferDefaultFlag
731 * @retval kStatus_Success Data was sent successfully.
732 * @retval #kStatus_I2C_Busy Another master is currently utilizing the bus.
733 * @retval #kStatus_I2C_Nak The slave device sent a NAK in response to a byte.
734 * @retval #kStatus_I2C_ArbitrationLost Arbitration lost error.
735 */
736 status_t I2C_MasterWriteBlocking(I2C_Type *base, const void *txBuff, size_t txSize, uint32_t flags);
737
738 /*!
739 * @brief Performs a polling receive transfer on the I2C bus.
740 *
741 * @param base The I2C peripheral base address.
742 * @param rxBuff The pointer to the data to be transferred.
743 * @param rxSize The length in bytes of the data to be transferred.
744 * @param flags Transfer control flag to control special behavior like suppressing start or stop, for normal transfers
745 * use kI2C_TransferDefaultFlag
746 * @retval kStatus_Success Data was received successfully.
747 * @retval #kStatus_I2C_Busy Another master is currently utilizing the bus.
748 * @retval #kStatus_I2C_Nak The slave device sent a NAK in response to a byte.
749 * @retval #kStatus_I2C_ArbitrationLost Arbitration lost error.
750 */
751 status_t I2C_MasterReadBlocking(I2C_Type *base, void *rxBuff, size_t rxSize, uint32_t flags);
752
753 /*!
754 * @brief Performs a master polling transfer on the I2C bus.
755 *
756 * @note The API does not return until the transfer succeeds or fails due
757 * to arbitration lost or receiving a NAK.
758 *
759 * @param base I2C peripheral base address.
760 * @param xfer Pointer to the transfer structure.
761 * @retval kStatus_Success Successfully complete the data transmission.
762 * @retval kStatus_I2C_Busy Previous transmission still not finished.
763 * @retval kStatus_I2C_Timeout Transfer error, wait signal timeout.
764 * @retval kStatus_I2C_ArbitrationLost Transfer error, arbitration lost.
765 * @retval kStataus_I2C_Nak Transfer error, receive NAK during transfer.
766 * @retval kStataus_I2C_Addr_Nak Transfer error, receive NAK during addressing.
767 */
768 status_t I2C_MasterTransferBlocking(I2C_Type *base, i2c_master_transfer_t *xfer);
769
770 /*@}*/
771
772 /*! @name Non-blocking */
773 /*@{*/
774
775 /*!
776 * @brief Creates a new handle for the I2C master non-blocking APIs.
777 *
778 * The creation of a handle is for use with the non-blocking APIs. Once a handle
779 * is created, there is not a corresponding destroy handle. If the user wants to
780 * terminate a transfer, the I2C_MasterTransferAbort() API shall be called.
781 *
782 * @param base The I2C peripheral base address.
783 * @param[out] handle Pointer to the I2C master driver handle.
784 * @param callback User provided pointer to the asynchronous callback function.
785 * @param userData User provided pointer to the application callback data.
786 */
787 void I2C_MasterTransferCreateHandle(I2C_Type *base,
788 i2c_master_handle_t *handle,
789 i2c_master_transfer_callback_t callback,
790 void *userData);
791
792 /*!
793 * @brief Performs a non-blocking transaction on the I2C bus.
794 *
795 * @param base The I2C peripheral base address.
796 * @param handle Pointer to the I2C master driver handle.
797 * @param xfer The pointer to the transfer descriptor.
798 * @retval kStatus_Success The transaction was started successfully.
799 * @retval #kStatus_I2C_Busy Either another master is currently utilizing the bus, or a non-blocking
800 * transaction is already in progress.
801 */
802 status_t I2C_MasterTransferNonBlocking(I2C_Type *base, i2c_master_handle_t *handle, i2c_master_transfer_t *xfer);
803
804 /*!
805 * @brief Returns number of bytes transferred so far.
806 * @param base The I2C peripheral base address.
807 * @param handle Pointer to the I2C master driver handle.
808 * @param[out] count Number of bytes transferred so far by the non-blocking transaction.
809 * @retval kStatus_Success
810 * @retval #kStatus_I2C_Busy
811 */
812 status_t I2C_MasterTransferGetCount(I2C_Type *base, i2c_master_handle_t *handle, size_t *count);
813
814 /*!
815 * @brief Terminates a non-blocking I2C master transmission early.
816 *
817 * @note It is not safe to call this function from an IRQ handler that has a higher priority than the
818 * I2C peripheral's IRQ priority.
819 *
820 * @param base The I2C peripheral base address.
821 * @param handle Pointer to the I2C master driver handle.
822 * @retval kStatus_Success A transaction was successfully aborted.
823 * @retval #kStatus_I2C_Timeout Timeout during polling for flags.
824 */
825 status_t I2C_MasterTransferAbort(I2C_Type *base, i2c_master_handle_t *handle);
826
827 /*@}*/
828
829 /*! @name IRQ handler */
830 /*@{*/
831
832 /*!
833 * @brief Reusable routine to handle master interrupts.
834 * @note This function does not need to be called unless you are reimplementing the
835 * nonblocking API's interrupt handler routines to add special functionality.
836 * @param base The I2C peripheral base address.
837 * @param handle Pointer to the I2C master driver handle.
838 */
839 void I2C_MasterTransferHandleIRQ(I2C_Type *base, i2c_master_handle_t *handle);
840
841 /*@}*/
842
843 /*! @} */ /* end of i2c_master_driver */
844
845 /*!
846 * @addtogroup i2c_slave_driver
847 * @{
848 */
849
850 /*! @name Slave initialization and deinitialization */
851 /*@{*/
852
853 /*!
854 * @brief Provides a default configuration for the I2C slave peripheral.
855 *
856 * This function provides the following default configuration for the I2C slave peripheral:
857 * @code
858 * slaveConfig->enableSlave = true;
859 * slaveConfig->address0.disable = false;
860 * slaveConfig->address0.address = 0u;
861 * slaveConfig->address1.disable = true;
862 * slaveConfig->address2.disable = true;
863 * slaveConfig->address3.disable = true;
864 * slaveConfig->busSpeed = kI2C_SlaveStandardMode;
865 * @endcode
866 *
867 * After calling this function, override any settings to customize the configuration,
868 * prior to initializing the master driver with I2C_SlaveInit(). Be sure to override at least the @a
869 * address0.address member of the configuration structure with the desired slave address.
870 *
871 * @param[out] slaveConfig User provided configuration structure that is set to default values. Refer to
872 * #i2c_slave_config_t.
873 */
874 void I2C_SlaveGetDefaultConfig(i2c_slave_config_t *slaveConfig);
875
876 /*!
877 * @brief Initializes the I2C slave peripheral.
878 *
879 * This function enables the peripheral clock and initializes the I2C slave peripheral as described by the user
880 * provided configuration.
881 *
882 * @param base The I2C peripheral base address.
883 * @param slaveConfig User provided peripheral configuration. Use I2C_SlaveGetDefaultConfig() to get a set of defaults
884 * that you can override.
885 * @param srcClock_Hz Frequency in Hertz of the I2C functional clock. Used to calculate CLKDIV value to provide
886 * enough
887 * data setup time for master when slave stretches the clock.
888 */
889 status_t I2C_SlaveInit(I2C_Type *base, const i2c_slave_config_t *slaveConfig, uint32_t srcClock_Hz);
890
891 /*!
892 * @brief Configures Slave Address n register.
893 *
894 * This function writes new value to Slave Address register.
895 *
896 * @param base The I2C peripheral base address.
897 * @param addressRegister The module supports multiple address registers. The parameter determines which one shall be
898 * changed.
899 * @param address The slave address to be stored to the address register for matching.
900 * @param addressDisable Disable matching of the specified address register.
901 */
902 void I2C_SlaveSetAddress(I2C_Type *base,
903 i2c_slave_address_register_t addressRegister,
904 uint8_t address,
905 bool addressDisable);
906
907 /*!
908 * @brief Deinitializes the I2C slave peripheral.
909 *
910 * This function disables the I2C slave peripheral and gates the clock. It also performs a software
911 * reset to restore the peripheral to reset conditions.
912 *
913 * @param base The I2C peripheral base address.
914 */
915 void I2C_SlaveDeinit(I2C_Type *base);
916
917 /*!
918 * @brief Enables or disables the I2C module as slave.
919 *
920 * @param base The I2C peripheral base address.
921 * @param enable True to enable or flase to disable.
922 */
I2C_SlaveEnable(I2C_Type * base,bool enable)923 static inline void I2C_SlaveEnable(I2C_Type *base, bool enable)
924 {
925 /* Set or clear the SLVEN bit in the CFG register. */
926 if (enable)
927 {
928 base->CFG = (base->CFG & (uint32_t)I2C_CFG_MASK) | I2C_CFG_SLVEN_MASK;
929 }
930 else
931 {
932 base->CFG = (base->CFG & (uint32_t)I2C_CFG_MASK) & ~I2C_CFG_SLVEN_MASK;
933 }
934 }
935
936 /*@}*/ /* end of Slave initialization and deinitialization */
937
938 /*! @name Slave status */
939 /*@{*/
940
941 /*!
942 * @brief Clears the I2C status flag state.
943 *
944 * The following status register flags can be cleared:
945 * - slave deselected flag
946 *
947 * Attempts to clear other flags has no effect.
948 *
949 * @param base The I2C peripheral base address.
950 * @param statusMask A bitmask of status flags that are to be cleared. The mask is composed of
951 * _i2c_slave_flags enumerators OR'd together. You may pass the result of a previous call to
952 * I2C_SlaveGetStatusFlags().
953 * @see _i2c_slave_flags.
954 */
I2C_SlaveClearStatusFlags(I2C_Type * base,uint32_t statusMask)955 static inline void I2C_SlaveClearStatusFlags(I2C_Type *base, uint32_t statusMask)
956 {
957 /* Allow clearing just slave status flags */
958 base->STAT = statusMask & I2C_STAT_SLVDESEL_MASK;
959 }
960
961 /*@}*/ /* end of Slave status */
962
963 /*! @name Slave bus operations */
964 /*@{*/
965
966 /*!
967 * @brief Performs a polling send transfer on the I2C bus.
968 *
969 * The function executes blocking address phase and blocking data phase.
970 *
971 * @param base The I2C peripheral base address.
972 * @param txBuff The pointer to the data to be transferred.
973 * @param txSize The length in bytes of the data to be transferred.
974 * @return kStatus_Success Data has been sent.
975 * @return kStatus_Fail Unexpected slave state (master data write while master read from slave is expected).
976 */
977 status_t I2C_SlaveWriteBlocking(I2C_Type *base, const uint8_t *txBuff, size_t txSize);
978
979 /*!
980 * @brief Performs a polling receive transfer on the I2C bus.
981 *
982 * The function executes blocking address phase and blocking data phase.
983 *
984 * @param base The I2C peripheral base address.
985 * @param rxBuff The pointer to the data to be transferred.
986 * @param rxSize The length in bytes of the data to be transferred.
987 * @return kStatus_Success Data has been received.
988 * @return kStatus_Fail Unexpected slave state (master data read while master write to slave is expected).
989 */
990 status_t I2C_SlaveReadBlocking(I2C_Type *base, uint8_t *rxBuff, size_t rxSize);
991
992 /*@}*/ /* end of Slave bus operations */
993
994 /*! @name Slave non-blocking */
995 /*@{*/
996
997 /*!
998 * @brief Creates a new handle for the I2C slave non-blocking APIs.
999 *
1000 * The creation of a handle is for use with the non-blocking APIs. Once a handle
1001 * is created, there is not a corresponding destroy handle. If the user wants to
1002 * terminate a transfer, the I2C_SlaveTransferAbort() API shall be called.
1003 *
1004 * @param base The I2C peripheral base address.
1005 * @param[out] handle Pointer to the I2C slave driver handle.
1006 * @param callback User provided pointer to the asynchronous callback function.
1007 * @param userData User provided pointer to the application callback data.
1008 */
1009 void I2C_SlaveTransferCreateHandle(I2C_Type *base,
1010 i2c_slave_handle_t *handle,
1011 i2c_slave_transfer_callback_t callback,
1012 void *userData);
1013
1014 /*!
1015 * @brief Starts accepting slave transfers.
1016 *
1017 * Call this API after calling I2C_SlaveInit() and I2C_SlaveTransferCreateHandle() to start processing
1018 * transactions driven by an I2C master. The slave monitors the I2C bus and pass events to the
1019 * callback that was passed into the call to I2C_SlaveTransferCreateHandle(). The callback is always invoked
1020 * from the interrupt context.
1021 *
1022 * If no slave Tx transfer is busy, a master read from slave request invokes #kI2C_SlaveTransmitEvent callback.
1023 * If no slave Rx transfer is busy, a master write to slave request invokes #kI2C_SlaveReceiveEvent callback.
1024 *
1025 * The set of events received by the callback is customizable. To do so, set the @a eventMask parameter to
1026 * the OR'd combination of #i2c_slave_transfer_event_t enumerators for the events you wish to receive.
1027 * The #kI2C_SlaveTransmitEvent and #kI2C_SlaveReceiveEvent events are always enabled and do not need
1028 * to be included in the mask. Alternatively, you can pass 0 to get a default set of only the transmit and
1029 * receive events that are always enabled. In addition, the #kI2C_SlaveAllEvents constant is provided as
1030 * a convenient way to enable all events.
1031 *
1032 * @param base The I2C peripheral base address.
1033 * @param handle Pointer to i2c_slave_handle_t structure which stores the transfer state.
1034 * @param eventMask Bit mask formed by OR'ing together #i2c_slave_transfer_event_t enumerators to specify
1035 * which events to send to the callback. Other accepted values are 0 to get a default set of
1036 * only the transmit and receive events, and #kI2C_SlaveAllEvents to enable all events.
1037 *
1038 * @retval kStatus_Success Slave transfers were successfully started.
1039 * @retval #kStatus_I2C_Busy Slave transfers have already been started on this handle.
1040 */
1041 status_t I2C_SlaveTransferNonBlocking(I2C_Type *base, i2c_slave_handle_t *handle, uint32_t eventMask);
1042
1043 /*!
1044 * @brief Starts accepting master read from slave requests.
1045 *
1046 * The function can be called in response to #kI2C_SlaveTransmitEvent callback to start a new slave Tx transfer
1047 * from within the transfer callback.
1048 *
1049 * The set of events received by the callback is customizable. To do so, set the @a eventMask parameter to
1050 * the OR'd combination of #i2c_slave_transfer_event_t enumerators for the events you wish to receive.
1051 * The #kI2C_SlaveTransmitEvent and #kI2C_SlaveReceiveEvent events are always enabled and do not need
1052 * to be included in the mask. Alternatively, you can pass 0 to get a default set of only the transmit and
1053 * receive events that are always enabled. In addition, the #kI2C_SlaveAllEvents constant is provided as
1054 * a convenient way to enable all events.
1055 *
1056 * @param base The I2C peripheral base address.
1057 * @param transfer Pointer to #i2c_slave_transfer_t structure.
1058 * @param txData Pointer to data to send to master.
1059 * @param txSize Size of txData in bytes.
1060 * @param eventMask Bit mask formed by OR'ing together #i2c_slave_transfer_event_t enumerators to specify
1061 * which events to send to the callback. Other accepted values are 0 to get a default set of
1062 * only the transmit and receive events, and #kI2C_SlaveAllEvents to enable all events.
1063 *
1064 * @retval kStatus_Success Slave transfers were successfully started.
1065 * @retval #kStatus_I2C_Busy Slave transfers have already been started on this handle.
1066 */
1067 status_t I2C_SlaveSetSendBuffer(
1068 I2C_Type *base, volatile i2c_slave_transfer_t *transfer, const void *txData, size_t txSize, uint32_t eventMask);
1069
1070 /*!
1071 * @brief Starts accepting master write to slave requests.
1072 *
1073 * The function can be called in response to #kI2C_SlaveReceiveEvent callback to start a new slave Rx transfer
1074 * from within the transfer callback.
1075 *
1076 * The set of events received by the callback is customizable. To do so, set the @a eventMask parameter to
1077 * the OR'd combination of #i2c_slave_transfer_event_t enumerators for the events you wish to receive.
1078 * The #kI2C_SlaveTransmitEvent and #kI2C_SlaveReceiveEvent events are always enabled and do not need
1079 * to be included in the mask. Alternatively, you can pass 0 to get a default set of only the transmit and
1080 * receive events that are always enabled. In addition, the #kI2C_SlaveAllEvents constant is provided as
1081 * a convenient way to enable all events.
1082 *
1083 * @param base The I2C peripheral base address.
1084 * @param transfer Pointer to #i2c_slave_transfer_t structure.
1085 * @param rxData Pointer to data to store data from master.
1086 * @param rxSize Size of rxData in bytes.
1087 * @param eventMask Bit mask formed by OR'ing together #i2c_slave_transfer_event_t enumerators to specify
1088 * which events to send to the callback. Other accepted values are 0 to get a default set of
1089 * only the transmit and receive events, and #kI2C_SlaveAllEvents to enable all events.
1090 *
1091 * @retval kStatus_Success Slave transfers were successfully started.
1092 * @retval #kStatus_I2C_Busy Slave transfers have already been started on this handle.
1093 */
1094 status_t I2C_SlaveSetReceiveBuffer(
1095 I2C_Type *base, volatile i2c_slave_transfer_t *transfer, void *rxData, size_t rxSize, uint32_t eventMask);
1096
1097 /*!
1098 * @brief Returns the slave address sent by the I2C master.
1099 *
1100 * This function should only be called from the address match event callback #kI2C_SlaveAddressMatchEvent.
1101 *
1102 * @param base The I2C peripheral base address.
1103 * @param transfer The I2C slave transfer.
1104 * @return The 8-bit address matched by the I2C slave. Bit 0 contains the R/w direction bit, and
1105 * the 7-bit slave address is in the upper 7 bits.
1106 */
I2C_SlaveGetReceivedAddress(I2C_Type * base,volatile i2c_slave_transfer_t * transfer)1107 static inline uint32_t I2C_SlaveGetReceivedAddress(I2C_Type *base, volatile i2c_slave_transfer_t *transfer)
1108 {
1109 return transfer->receivedAddress;
1110 }
1111
1112 /*!
1113 * @brief Aborts the slave non-blocking transfers.
1114 * @note This API could be called at any time to stop slave for handling the bus events.
1115 * @param base The I2C peripheral base address.
1116 * @param handle Pointer to i2c_slave_handle_t structure which stores the transfer state.
1117 * @retval kStatus_Success
1118 * @retval #kStatus_I2C_Idle
1119 */
1120 void I2C_SlaveTransferAbort(I2C_Type *base, i2c_slave_handle_t *handle);
1121
1122 /*!
1123 * @brief Gets the slave transfer remaining bytes during a interrupt non-blocking transfer.
1124 *
1125 * @param base I2C base pointer.
1126 * @param handle pointer to i2c_slave_handle_t structure.
1127 * @param count Number of bytes transferred so far by the non-blocking transaction.
1128 * @retval kStatus_InvalidArgument count is Invalid.
1129 * @retval kStatus_Success Successfully return the count.
1130 */
1131 status_t I2C_SlaveTransferGetCount(I2C_Type *base, i2c_slave_handle_t *handle, size_t *count);
1132
1133 /*@}*/ /* end of Slave non-blocking */
1134
1135 /*! @name Slave IRQ handler */
1136 /*@{*/
1137
1138 /*!
1139 * @brief Reusable routine to handle slave interrupts.
1140 * @note This function does not need to be called unless you are reimplementing the
1141 * non blocking API's interrupt handler routines to add special functionality.
1142 * @param base The I2C peripheral base address.
1143 * @param handle Pointer to i2c_slave_handle_t structure which stores the transfer state.
1144 */
1145 void I2C_SlaveTransferHandleIRQ(I2C_Type *base, i2c_slave_handle_t *handle);
1146
1147 /*@}*/ /* end of Slave IRQ handler */
1148
1149 /*! @} */ /* end of i2c_slave_driver */
1150
1151 #if defined(__cplusplus)
1152 }
1153 #endif
1154
1155 #endif /* _FSL_I2C_H_ */
1156