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