1 /*
2  * Copyright (c) 2015, Freescale Semiconductor, Inc.
3  * Copyright 2016-2020 NXP
4  * All rights reserved.
5  *
6  * SPDX-License-Identifier: BSD-3-Clause
7  */
8 #ifndef _FSL_LPI2C_H_
9 #define _FSL_LPI2C_H_
10 
11 #include <stddef.h>
12 #include "fsl_device_registers.h"
13 #include "fsl_common.h"
14 
15 /*******************************************************************************
16  * Definitions
17  ******************************************************************************/
18 
19 /*!
20  * @addtogroup lpi2c
21  * @{
22  */
23 
24 /*! @name Driver version */
25 /*@{*/
26 /*! @brief LPI2C driver version 2.1.11. */
27 #define FSL_LPI2C_DRIVER_VERSION (MAKE_VERSION(2, 1, 11))
28 /*@}*/
29 
30 /*! @brief Retry times for waiting flag. */
31 #ifndef I2C_RETRY_TIMES
32 #define I2C_RETRY_TIMES 0U /* Define to zero means keep waiting until the flag is assert/deassert. */
33 #endif
34 
35 /*! @brief LPI2C status return codes. */
36 enum
37 {
38     kStatus_LPI2C_Busy = MAKE_STATUS(kStatusGroup_LPI2C, 0), /*!< The master is already performing a transfer. */
39     kStatus_LPI2C_Idle = MAKE_STATUS(kStatusGroup_LPI2C, 1), /*!< The slave driver is idle. */
40     kStatus_LPI2C_Nak  = MAKE_STATUS(kStatusGroup_LPI2C, 2), /*!< The slave device sent a NAK in response to a byte. */
41     kStatus_LPI2C_FifoError       = MAKE_STATUS(kStatusGroup_LPI2C, 3), /*!< FIFO under run or overrun. */
42     kStatus_LPI2C_BitError        = MAKE_STATUS(kStatusGroup_LPI2C, 4), /*!< Transferred bit was not seen on the bus. */
43     kStatus_LPI2C_ArbitrationLost = MAKE_STATUS(kStatusGroup_LPI2C, 5), /*!< Arbitration lost error. */
44     kStatus_LPI2C_PinLowTimeout =
45         MAKE_STATUS(kStatusGroup_LPI2C, 6), /*!< SCL or SDA were held low longer than the timeout. */
46     kStatus_LPI2C_NoTransferInProgress =
47         MAKE_STATUS(kStatusGroup_LPI2C, 7), /*!< Attempt to abort a transfer when one is not in progress. */
48     kStatus_LPI2C_DmaRequestFail = MAKE_STATUS(kStatusGroup_LPI2C, 8), /*!< DMA request failed. */
49     kStatus_LPI2C_Timeout        = MAKE_STATUS(kStatusGroup_LPI2C, 9), /*!< Timeout polling status flags. */
50 };
51 
52 /*! @} */
53 
54 /*!
55  * @addtogroup lpi2c_master_driver
56  * @{
57  */
58 
59 /*!
60  * @brief LPI2C master peripheral flags.
61  *
62  * The following status register flags can be cleared:
63  * - #kLPI2C_MasterEndOfPacketFlag
64  * - #kLPI2C_MasterStopDetectFlag
65  * - #kLPI2C_MasterNackDetectFlag
66  * - #kLPI2C_MasterArbitrationLostFlag
67  * - #kLPI2C_MasterFifoErrFlag
68  * - #kLPI2C_MasterPinLowTimeoutFlag
69  * - #kLPI2C_MasterDataMatchFlag
70  *
71  * All flags except #kLPI2C_MasterBusyFlag and #kLPI2C_MasterBusBusyFlag can be enabled as
72  * interrupts.
73  *
74  * @note These enums are meant to be OR'd together to form a bit mask.
75  */
76 enum
77 {
78     kLPI2C_MasterTxReadyFlag         = LPI2C_MSR_TDF_MASK,  /*!< Transmit data flag */
79     kLPI2C_MasterRxReadyFlag         = LPI2C_MSR_RDF_MASK,  /*!< Receive data flag */
80     kLPI2C_MasterEndOfPacketFlag     = LPI2C_MSR_EPF_MASK,  /*!< End Packet flag */
81     kLPI2C_MasterStopDetectFlag      = LPI2C_MSR_SDF_MASK,  /*!< Stop detect flag */
82     kLPI2C_MasterNackDetectFlag      = LPI2C_MSR_NDF_MASK,  /*!< NACK detect flag */
83     kLPI2C_MasterArbitrationLostFlag = LPI2C_MSR_ALF_MASK,  /*!< Arbitration lost flag */
84     kLPI2C_MasterFifoErrFlag         = LPI2C_MSR_FEF_MASK,  /*!< FIFO error flag */
85     kLPI2C_MasterPinLowTimeoutFlag   = LPI2C_MSR_PLTF_MASK, /*!< Pin low timeout flag */
86     kLPI2C_MasterDataMatchFlag       = LPI2C_MSR_DMF_MASK,  /*!< Data match flag */
87     kLPI2C_MasterBusyFlag            = LPI2C_MSR_MBF_MASK,  /*!< Master busy flag */
88     kLPI2C_MasterBusBusyFlag         = LPI2C_MSR_BBF_MASK   /*!< Bus busy flag */
89 };
90 
91 /*! @brief Direction of master and slave transfers. */
92 typedef enum _lpi2c_direction
93 {
94     kLPI2C_Write = 0U, /*!< Master transmit. */
95     kLPI2C_Read  = 1U  /*!< Master receive. */
96 } lpi2c_direction_t;
97 
98 /*! @brief LPI2C pin configuration. */
99 typedef enum _lpi2c_master_pin_config
100 {
101     kLPI2C_2PinOpenDrain  = 0x0U, /*!< LPI2C Configured for 2-pin open drain mode */
102     kLPI2C_2PinOutputOnly = 0x1U, /*!< LPI2C Configured for 2-pin output only mode (ultra-fast mode) */
103     kLPI2C_2PinPushPull   = 0x2U, /*!< LPI2C Configured for 2-pin push-pull mode */
104     kLPI2C_4PinPushPull   = 0x3U, /*!< LPI2C Configured for 4-pin push-pull mode */
105     kLPI2C_2PinOpenDrainWithSeparateSlave =
106         0x4U, /*!< LPI2C Configured for 2-pin open drain mode with separate LPI2C slave */
107     kLPI2C_2PinOutputOnlyWithSeparateSlave =
108         0x5U, /*!< LPI2C Configured for 2-pin output only mode(ultra-fast mode) with separate LPI2C slave */
109     kLPI2C_2PinPushPullWithSeparateSlave =
110         0x6U, /*!< LPI2C Configured for 2-pin push-pull mode with separate LPI2C slave */
111     kLPI2C_4PinPushPullWithInvertedOutput = 0x7U /*!< LPI2C Configured for 4-pin push-pull mode(inverted outputs) */
112 } lpi2c_master_pin_config_t;
113 
114 /*! @brief LPI2C master host request selection. */
115 typedef enum _lpi2c_host_request_source
116 {
117     kLPI2C_HostRequestExternalPin  = 0x0U, /*!< Select the LPI2C_HREQ pin as the host request input */
118     kLPI2C_HostRequestInputTrigger = 0x1U, /*!< Select the input trigger as the host request input */
119 } lpi2c_host_request_source_t;
120 
121 /*! @brief LPI2C master host request pin polarity configuration. */
122 typedef enum _lpi2c_host_request_polarity
123 {
124     kLPI2C_HostRequestPinActiveLow  = 0x0U, /*!< Configure the LPI2C_HREQ pin active low */
125     kLPI2C_HostRequestPinActiveHigh = 0x1U  /*!< Configure the LPI2C_HREQ pin active high */
126 } lpi2c_host_request_polarity_t;
127 
128 /*!
129  * @brief Structure with settings to initialize the LPI2C master module.
130  *
131  * This structure holds configuration settings for the LPI2C peripheral. To initialize this
132  * structure to reasonable defaults, call the LPI2C_MasterGetDefaultConfig() function and
133  * pass a pointer to your configuration structure instance.
134  *
135  * The configuration structure can be made constant so it resides in flash.
136  */
137 typedef struct _lpi2c_master_config
138 {
139     bool enableMaster;                   /*!< Whether to enable master mode. */
140     bool enableDoze;                     /*!< Whether master is enabled in doze mode. */
141     bool debugEnable;                    /*!< Enable transfers to continue when halted in debug mode. */
142     bool ignoreAck;                      /*!< Whether to ignore ACK/NACK. */
143     lpi2c_master_pin_config_t pinConfig; /*!< The pin configuration option. */
144     uint32_t baudRate_Hz;                /*!< Desired baud rate in Hertz. */
145     uint32_t busIdleTimeout_ns;          /*!< Bus idle timeout in nanoseconds. Set to 0 to disable. */
146     uint32_t pinLowTimeout_ns;           /*!< Pin low timeout in nanoseconds. Set to 0 to disable. */
147     uint8_t sdaGlitchFilterWidth_ns;     /*!< Width in nanoseconds of glitch filter on SDA pin. Set to 0 to disable. */
148     uint8_t sclGlitchFilterWidth_ns;     /*!< Width in nanoseconds of glitch filter on SCL pin. Set to 0 to disable. */
149     struct
150     {
151         bool enable;                            /*!< Enable host request. */
152         lpi2c_host_request_source_t source;     /*!< Host request source. */
153         lpi2c_host_request_polarity_t polarity; /*!< Host request pin polarity. */
154     } hostRequest;                              /*!< Host request options. */
155 } lpi2c_master_config_t;
156 
157 /*! @brief LPI2C master data match configuration modes. */
158 typedef enum _lpi2c_data_match_config_mode
159 {
160     kLPI2C_MatchDisabled       = 0x0U, /*!< LPI2C Match Disabled */
161     kLPI2C_1stWordEqualsM0OrM1 = 0x2U, /*!< LPI2C Match Enabled and 1st data word equals MATCH0 OR MATCH1 */
162     kLPI2C_AnyWordEqualsM0OrM1 = 0x3U, /*!< LPI2C Match Enabled and any data word equals MATCH0 OR MATCH1 */
163     kLPI2C_1stWordEqualsM0And2ndWordEqualsM1 =
164         0x4U, /*!< LPI2C Match Enabled and 1st data word equals MATCH0, 2nd data equals MATCH1 */
165     kLPI2C_AnyWordEqualsM0AndNextWordEqualsM1 =
166         0x5U, /*!< LPI2C Match Enabled and any data word equals MATCH0, next data equals MATCH1 */
167     kLPI2C_1stWordAndM1EqualsM0AndM1 =
168         0x6U, /*!< LPI2C Match Enabled and 1st data word and MATCH0 equals MATCH0 and MATCH1 */
169     kLPI2C_AnyWordAndM1EqualsM0AndM1 =
170         0x7U /*!< LPI2C Match Enabled and any data word and MATCH0 equals MATCH0 and MATCH1 */
171 } lpi2c_data_match_config_mode_t;
172 
173 /*! @brief LPI2C master data match configuration structure. */
174 typedef struct _lpi2c_match_config
175 {
176     lpi2c_data_match_config_mode_t matchMode; /*!< Data match configuration setting. */
177     bool rxDataMatchOnly; /*!< When set to true, received data is ignored until a successful match. */
178     uint32_t match0;      /*!< Match value 0. */
179     uint32_t match1;      /*!< Match value 1. */
180 } lpi2c_data_match_config_t;
181 
182 /* Forward declaration of the transfer descriptor and handle typedefs. */
183 typedef struct _lpi2c_master_transfer lpi2c_master_transfer_t;
184 typedef struct _lpi2c_master_handle lpi2c_master_handle_t;
185 
186 /*!
187  * @brief Master completion callback function pointer type.
188  *
189  * This callback is used only for the non-blocking master transfer API. Specify the callback you wish to use
190  * in the call to LPI2C_MasterTransferCreateHandle().
191  *
192  * @param base The LPI2C peripheral base address.
193  * @param completionStatus Either kStatus_Success or an error code describing how the transfer completed.
194  * @param userData Arbitrary pointer-sized value passed from the application.
195  */
196 typedef void (*lpi2c_master_transfer_callback_t)(LPI2C_Type *base,
197                                                  lpi2c_master_handle_t *handle,
198                                                  status_t completionStatus,
199                                                  void *userData);
200 
201 /*!
202  * @brief Transfer option flags.
203  *
204  * @note These enumerations are intended to be OR'd together to form a bit mask of options for
205  * the #_lpi2c_master_transfer::flags field.
206  */
207 enum _lpi2c_master_transfer_flags
208 {
209     kLPI2C_TransferDefaultFlag       = 0x00U, /*!< Transfer starts with a start signal, stops with a stop signal. */
210     kLPI2C_TransferNoStartFlag       = 0x01U, /*!< Don't send a start condition, address, and sub address */
211     kLPI2C_TransferRepeatedStartFlag = 0x02U, /*!< Send a repeated start condition */
212     kLPI2C_TransferNoStopFlag        = 0x04U, /*!< Don't send a stop condition. */
213 };
214 
215 /*!
216  * @brief Non-blocking transfer descriptor structure.
217  *
218  * This structure is used to pass transaction parameters to the LPI2C_MasterTransferNonBlocking() API.
219  */
220 struct _lpi2c_master_transfer
221 {
222     uint32_t flags;        /*!< Bit mask of options for the transfer. See enumeration #_lpi2c_master_transfer_flags for
223                               available options. Set to 0 or #kLPI2C_TransferDefaultFlag for normal transfers. */
224     uint16_t slaveAddress; /*!< The 7-bit slave address. */
225     lpi2c_direction_t direction; /*!< Either #kLPI2C_Read or #kLPI2C_Write. */
226     uint32_t subaddress;         /*!< Sub address. Transferred MSB first. */
227     size_t subaddressSize;       /*!< Length of sub address to send in bytes. Maximum size is 4 bytes. */
228     void *data;                  /*!< Pointer to data to transfer. */
229     size_t dataSize;             /*!< Number of bytes to transfer. */
230 };
231 
232 /*!
233  * @brief Driver handle for master non-blocking APIs.
234  * @note The contents of this structure are private and subject to change.
235  */
236 struct _lpi2c_master_handle
237 {
238     uint8_t state;                                       /*!< Transfer state machine current state. */
239     uint16_t remainingBytes;                             /*!< Remaining byte count in current state. */
240     uint8_t *buf;                                        /*!< Buffer pointer for current state. */
241     uint16_t commandBuffer[7];                           /*!< LPI2C command sequence. */
242     lpi2c_master_transfer_t transfer;                    /*!< Copy of the current transfer info. */
243     lpi2c_master_transfer_callback_t completionCallback; /*!< Callback function pointer. */
244     void *userData;                                      /*!< Application data passed to callback. */
245 };
246 
247 /*! @} */
248 
249 /*!
250  * @addtogroup lpi2c_slave_driver
251  * @{
252  */
253 
254 /*!
255  * @brief LPI2C slave peripheral flags.
256  *
257  * The following status register flags can be cleared:
258  * - #kLPI2C_SlaveRepeatedStartDetectFlag
259  * - #kLPI2C_SlaveStopDetectFlag
260  * - #kLPI2C_SlaveBitErrFlag
261  * - #kLPI2C_SlaveFifoErrFlag
262  *
263  * All flags except #kLPI2C_SlaveBusyFlag and #kLPI2C_SlaveBusBusyFlag can be enabled as
264  * interrupts.
265  *
266  * @note These enumerations are meant to be OR'd together to form a bit mask.
267  */
268 enum _lpi2c_slave_flags
269 {
270     kLPI2C_SlaveTxReadyFlag             = LPI2C_SSR_TDF_MASK,  /*!< Transmit data flag */
271     kLPI2C_SlaveRxReadyFlag             = LPI2C_SSR_RDF_MASK,  /*!< Receive data flag */
272     kLPI2C_SlaveAddressValidFlag        = LPI2C_SSR_AVF_MASK,  /*!< Address valid flag */
273     kLPI2C_SlaveTransmitAckFlag         = LPI2C_SSR_TAF_MASK,  /*!< Transmit ACK flag */
274     kLPI2C_SlaveRepeatedStartDetectFlag = LPI2C_SSR_RSF_MASK,  /*!< Repeated start detect flag */
275     kLPI2C_SlaveStopDetectFlag          = LPI2C_SSR_SDF_MASK,  /*!< Stop detect flag */
276     kLPI2C_SlaveBitErrFlag              = LPI2C_SSR_BEF_MASK,  /*!< Bit error flag */
277     kLPI2C_SlaveFifoErrFlag             = LPI2C_SSR_FEF_MASK,  /*!< FIFO error flag */
278     kLPI2C_SlaveAddressMatch0Flag       = LPI2C_SSR_AM0F_MASK, /*!< Address match 0 flag */
279     kLPI2C_SlaveAddressMatch1Flag       = LPI2C_SSR_AM1F_MASK, /*!< Address match 1 flag */
280     kLPI2C_SlaveGeneralCallFlag         = LPI2C_SSR_GCF_MASK,  /*!< General call flag */
281     kLPI2C_SlaveBusyFlag                = LPI2C_SSR_SBF_MASK,  /*!< Master busy flag */
282     kLPI2C_SlaveBusBusyFlag             = LPI2C_SSR_BBF_MASK,  /*!< Bus busy flag */
283 };
284 
285 /*! @brief LPI2C slave address match options. */
286 typedef enum _lpi2c_slave_address_match
287 {
288     kLPI2C_MatchAddress0                = 0U, /*!< Match only address 0. */
289     kLPI2C_MatchAddress0OrAddress1      = 2U, /*!< Match either address 0 or address 1. */
290     kLPI2C_MatchAddress0ThroughAddress1 = 6U, /*!< Match a range of slave addresses from address 0 through address 1. */
291 } lpi2c_slave_address_match_t;
292 
293 /*!
294  * @brief Structure with settings to initialize the LPI2C slave module.
295  *
296  * This structure holds configuration settings for the LPI2C slave peripheral. To initialize this
297  * structure to reasonable defaults, call the LPI2C_SlaveGetDefaultConfig() function and
298  * pass a pointer to your configuration structure instance.
299  *
300  * The configuration structure can be made constant so it resides in flash.
301  */
302 typedef struct _lpi2c_slave_config
303 {
304     bool enableSlave;                             /*!< Enable slave mode. */
305     uint8_t address0;                             /*!< Slave's 7-bit address. */
306     uint8_t address1;                             /*!< Alternate slave 7-bit address. */
307     lpi2c_slave_address_match_t addressMatchMode; /*!< Address matching options. */
308     bool filterDozeEnable;                        /*!< Enable digital glitch filter in doze mode. */
309     bool filterEnable;                            /*!< Enable digital glitch filter. */
310     bool enableGeneralCall;                       /*!< Enable general call address matching. */
311     struct
312     {
313         bool enableAck;     /*!< Enables SCL clock stretching during slave-transmit address byte(s)
314                                         and slave-receiver address and data byte(s) to allow software to
315                                         write the Transmit ACK Register before the ACK or NACK is transmitted.
316                                         Clock stretching occurs when transmitting the 9th bit. When
317                                         enableAckSCLStall is enabled, there is no need to set either
318                                         enableRxDataSCLStall or enableAddressSCLStall. */
319         bool enableTx;      /*!< Enables SCL clock stretching when the transmit data flag is set
320                                          during a slave-transmit transfer. */
321         bool enableRx;      /*!< Enables SCL clock stretching when receive data flag is set during
322                                          a slave-receive transfer. */
323         bool enableAddress; /*!< Enables SCL clock stretching when the address valid flag is asserted. */
324     } sclStall;
325     bool ignoreAck;                   /*!< Continue transfers after a NACK is detected. */
326     bool enableReceivedAddressRead;   /*!< Enable reading the address received address as the first byte of data. */
327     uint32_t sdaGlitchFilterWidth_ns; /*!< Width in nanoseconds of the digital filter on the SDA signal. */
328     uint32_t sclGlitchFilterWidth_ns; /*!< Width in nanoseconds of the digital filter on the SCL signal. */
329     uint32_t dataValidDelay_ns;       /*!< Width in nanoseconds of the data valid delay. */
330     uint32_t clockHoldTime_ns;        /*!< Width in nanoseconds of the clock hold time. */
331 } lpi2c_slave_config_t;
332 
333 /*!
334  * @brief Set of events sent to the callback for non blocking slave transfers.
335  *
336  * These event enumerations are used for two related purposes. First, a bit mask created by OR'ing together
337  * events is passed to LPI2C_SlaveTransferNonBlocking() in order to specify which events to enable.
338  * Then, when the slave callback is invoked, it is passed the current event through its @a transfer
339  * parameter.
340  *
341  * @note These enumerations are meant to be OR'd together to form a bit mask of events.
342  */
343 typedef enum _lpi2c_slave_transfer_event
344 {
345     kLPI2C_SlaveAddressMatchEvent = 0x01U,  /*!< Received the slave address after a start or repeated start. */
346     kLPI2C_SlaveTransmitEvent     = 0x02U,  /*!< Callback is requested to provide data to transmit
347                                                  (slave-transmitter role). */
348     kLPI2C_SlaveReceiveEvent = 0x04U,       /*!< Callback is requested to provide a buffer in which to place received
349                                                   data (slave-receiver role). */
350     kLPI2C_SlaveTransmitAckEvent   = 0x08U, /*!< Callback needs to either transmit an ACK or NACK. */
351     kLPI2C_SlaveRepeatedStartEvent = 0x10U, /*!< A repeated start was detected. */
352     kLPI2C_SlaveCompletionEvent    = 0x20U, /*!< A stop was detected, completing the transfer. */
353 
354     /*! Bit mask of all available events. */
355     kLPI2C_SlaveAllEvents = kLPI2C_SlaveAddressMatchEvent | kLPI2C_SlaveTransmitEvent | kLPI2C_SlaveReceiveEvent |
356                             kLPI2C_SlaveTransmitAckEvent | kLPI2C_SlaveRepeatedStartEvent | kLPI2C_SlaveCompletionEvent,
357 } lpi2c_slave_transfer_event_t;
358 
359 /*! @brief LPI2C slave transfer structure */
360 typedef struct _lpi2c_slave_transfer
361 {
362     lpi2c_slave_transfer_event_t event; /*!< Reason the callback is being invoked. */
363     uint8_t receivedAddress;            /*!< Matching address send by master. */
364     uint8_t *data;                      /*!< Transfer buffer */
365     size_t dataSize;                    /*!< Transfer size */
366     status_t completionStatus; /*!< Success or error code describing how the transfer completed. Only applies for
367                                   #kLPI2C_SlaveCompletionEvent. */
368     size_t transferredCount;   /*!< Number of bytes actually transferred since start or last repeated start. */
369 } lpi2c_slave_transfer_t;
370 
371 /* Forward declaration. */
372 typedef struct _lpi2c_slave_handle lpi2c_slave_handle_t;
373 
374 /*!
375  * @brief Slave event callback function pointer type.
376  *
377  * This callback is used only for the slave non-blocking transfer API. To install a callback,
378  * use the LPI2C_SlaveSetCallback() function after you have created a handle.
379  *
380  * @param base Base address for the LPI2C instance on which the event occurred.
381  * @param transfer Pointer to transfer descriptor containing values passed to and/or from the callback.
382  * @param userData Arbitrary pointer-sized value passed from the application.
383  */
384 typedef void (*lpi2c_slave_transfer_callback_t)(LPI2C_Type *base, lpi2c_slave_transfer_t *transfer, void *userData);
385 
386 /*!
387  * @brief LPI2C slave handle structure.
388  * @note The contents of this structure are private and subject to change.
389  */
390 struct _lpi2c_slave_handle
391 {
392     lpi2c_slave_transfer_t transfer;          /*!< LPI2C slave transfer copy. */
393     bool isBusy;                              /*!< Whether transfer is busy. */
394     bool wasTransmit;                         /*!< Whether the last transfer was a transmit. */
395     uint32_t eventMask;                       /*!< Mask of enabled events. */
396     uint32_t transferredCount;                /*!< Count of bytes transferred. */
397     lpi2c_slave_transfer_callback_t callback; /*!< Callback function called at transfer event. */
398     void *userData;                           /*!< Callback parameter passed to callback. */
399 };
400 
401 /*! @} */
402 
403 /*******************************************************************************
404  * API
405  ******************************************************************************/
406 
407 #if defined(__cplusplus)
408 extern "C" {
409 #endif
410 
411 /*!
412  * @addtogroup lpi2c_master_driver
413  * @{
414  */
415 
416 /*! @name Initialization and deinitialization */
417 /*@{*/
418 
419 /*!
420  * @brief Provides a default configuration for the LPI2C master peripheral.
421  *
422  * This function provides the following default configuration for the LPI2C master peripheral:
423  * @code
424  *  masterConfig->enableMaster            = true;
425  *  masterConfig->debugEnable             = false;
426  *  masterConfig->ignoreAck               = false;
427  *  masterConfig->pinConfig               = kLPI2C_2PinOpenDrain;
428  *  masterConfig->baudRate_Hz             = 100000U;
429  *  masterConfig->busIdleTimeout_ns       = 0;
430  *  masterConfig->pinLowTimeout_ns        = 0;
431  *  masterConfig->sdaGlitchFilterWidth_ns = 0;
432  *  masterConfig->sclGlitchFilterWidth_ns = 0;
433  *  masterConfig->hostRequest.enable      = false;
434  *  masterConfig->hostRequest.source      = kLPI2C_HostRequestExternalPin;
435  *  masterConfig->hostRequest.polarity    = kLPI2C_HostRequestPinActiveHigh;
436  * @endcode
437  *
438  * After calling this function, you can override any settings in order to customize the configuration,
439  * prior to initializing the master driver with LPI2C_MasterInit().
440  *
441  * @param[out] masterConfig User provided configuration structure for default values. Refer to #lpi2c_master_config_t.
442  */
443 void LPI2C_MasterGetDefaultConfig(lpi2c_master_config_t *masterConfig);
444 
445 /*!
446  * @brief Initializes the LPI2C master peripheral.
447  *
448  * This function enables the peripheral clock and initializes the LPI2C master peripheral as described by the user
449  * provided configuration. A software reset is performed prior to configuration.
450  *
451  * @param base The LPI2C peripheral base address.
452  * @param masterConfig User provided peripheral configuration. Use LPI2C_MasterGetDefaultConfig() to get a set of
453  * defaults
454  *      that you can override.
455  * @param sourceClock_Hz Frequency in Hertz of the LPI2C functional clock. Used to calculate the baud rate divisors,
456  *      filter widths, and timeout periods.
457  */
458 void LPI2C_MasterInit(LPI2C_Type *base, const lpi2c_master_config_t *masterConfig, uint32_t sourceClock_Hz);
459 
460 /*!
461  * @brief Deinitializes the LPI2C master peripheral.
462  *
463  * This function disables the LPI2C master peripheral and gates the clock. It also performs a software
464  * reset to restore the peripheral to reset conditions.
465  *
466  * @param base The LPI2C peripheral base address.
467  */
468 void LPI2C_MasterDeinit(LPI2C_Type *base);
469 
470 /*!
471  * @brief Configures LPI2C master data match feature.
472  *
473  * @param base The LPI2C peripheral base address.
474  * @param config Settings for the data match feature.
475  */
476 void LPI2C_MasterConfigureDataMatch(LPI2C_Type *base, const lpi2c_data_match_config_t *config);
477 
478 /* Not static so it can be used from fsl_lpi2c_edma.c. */
479 status_t LPI2C_MasterCheckAndClearError(LPI2C_Type *base, uint32_t status);
480 
481 /* Not static so it can be used from fsl_lpi2c_edma.c. */
482 status_t LPI2C_CheckForBusyBus(LPI2C_Type *base);
483 
484 /*!
485  * @brief Performs a software reset.
486  *
487  * Restores the LPI2C master peripheral to reset conditions.
488  *
489  * @param base The LPI2C peripheral base address.
490  */
LPI2C_MasterReset(LPI2C_Type * base)491 static inline void LPI2C_MasterReset(LPI2C_Type *base)
492 {
493     base->MCR = LPI2C_MCR_RST_MASK;
494     base->MCR = 0;
495 }
496 
497 /*!
498  * @brief Enables or disables the LPI2C module as master.
499  *
500  * @param base The LPI2C peripheral base address.
501  * @param enable Pass true to enable or false to disable the specified LPI2C as master.
502  */
LPI2C_MasterEnable(LPI2C_Type * base,bool enable)503 static inline void LPI2C_MasterEnable(LPI2C_Type *base, bool enable)
504 {
505     base->MCR = (base->MCR & ~LPI2C_MCR_MEN_MASK) | LPI2C_MCR_MEN(enable);
506 }
507 
508 /*@}*/
509 
510 /*! @name Status */
511 /*@{*/
512 
513 /*!
514  * @brief Gets the LPI2C master status flags.
515  *
516  * A bit mask with the state of all LPI2C master status flags is returned. For each flag, the corresponding bit
517  * in the return value is set if the flag is asserted.
518  *
519  * @param base The LPI2C peripheral base address.
520  * @return State of the status flags:
521  *         - 1: related status flag is set.
522  *         - 0: related status flag is not set.
523  * @see _lpi2c_master_flags
524  */
LPI2C_MasterGetStatusFlags(LPI2C_Type * base)525 static inline uint32_t LPI2C_MasterGetStatusFlags(LPI2C_Type *base)
526 {
527     return base->MSR;
528 }
529 
530 /*!
531  * @brief Clears the LPI2C master status flag state.
532  *
533  * The following status register flags can be cleared:
534  * - #kLPI2C_MasterEndOfPacketFlag
535  * - #kLPI2C_MasterStopDetectFlag
536  * - #kLPI2C_MasterNackDetectFlag
537  * - #kLPI2C_MasterArbitrationLostFlag
538  * - #kLPI2C_MasterFifoErrFlag
539  * - #kLPI2C_MasterPinLowTimeoutFlag
540  * - #kLPI2C_MasterDataMatchFlag
541  *
542  * Attempts to clear other flags has no effect.
543  *
544  * @param base The LPI2C peripheral base address.
545  * @param statusMask A bitmask of status flags that are to be cleared. The mask is composed of
546  *  _lpi2c_master_flags enumerators OR'd together. You may pass the result of a previous call to
547  *  LPI2C_MasterGetStatusFlags().
548  * @see _lpi2c_master_flags.
549  */
LPI2C_MasterClearStatusFlags(LPI2C_Type * base,uint32_t statusMask)550 static inline void LPI2C_MasterClearStatusFlags(LPI2C_Type *base, uint32_t statusMask)
551 {
552     base->MSR = statusMask;
553 }
554 
555 /*@}*/
556 
557 /*! @name Interrupts */
558 /*@{*/
559 
560 /*!
561  * @brief Enables the LPI2C master interrupt requests.
562  *
563  * All flags except #kLPI2C_MasterBusyFlag and #kLPI2C_MasterBusBusyFlag can be enabled as
564  * interrupts.
565  *
566  * @param base The LPI2C peripheral base address.
567  * @param interruptMask Bit mask of interrupts to enable. See _lpi2c_master_flags for the set
568  *      of constants that should be OR'd together to form the bit mask.
569  */
LPI2C_MasterEnableInterrupts(LPI2C_Type * base,uint32_t interruptMask)570 static inline void LPI2C_MasterEnableInterrupts(LPI2C_Type *base, uint32_t interruptMask)
571 {
572     base->MIER |= interruptMask;
573 }
574 
575 /*!
576  * @brief Disables the LPI2C master interrupt requests.
577  *
578  * All flags except #kLPI2C_MasterBusyFlag and #kLPI2C_MasterBusBusyFlag can be enabled as
579  * interrupts.
580  *
581  * @param base The LPI2C peripheral base address.
582  * @param interruptMask Bit mask of interrupts to disable. See _lpi2c_master_flags for the set
583  *      of constants that should be OR'd together to form the bit mask.
584  */
LPI2C_MasterDisableInterrupts(LPI2C_Type * base,uint32_t interruptMask)585 static inline void LPI2C_MasterDisableInterrupts(LPI2C_Type *base, uint32_t interruptMask)
586 {
587     base->MIER &= ~interruptMask;
588 }
589 
590 /*!
591  * @brief Returns the set of currently enabled LPI2C master interrupt requests.
592  *
593  * @param base The LPI2C peripheral base address.
594  * @return A bitmask composed of _lpi2c_master_flags enumerators OR'd together to indicate the
595  *      set of enabled interrupts.
596  */
LPI2C_MasterGetEnabledInterrupts(LPI2C_Type * base)597 static inline uint32_t LPI2C_MasterGetEnabledInterrupts(LPI2C_Type *base)
598 {
599     return base->MIER;
600 }
601 
602 /*@}*/
603 
604 /*! @name DMA control */
605 /*@{*/
606 
607 /*!
608  * @brief Enables or disables LPI2C master DMA requests.
609  *
610  * @param base The LPI2C peripheral base address.
611  * @param enableTx Enable flag for transmit DMA request. Pass true for enable, false for disable.
612  * @param enableRx Enable flag for receive DMA request. Pass true for enable, false for disable.
613  */
LPI2C_MasterEnableDMA(LPI2C_Type * base,bool enableTx,bool enableRx)614 static inline void LPI2C_MasterEnableDMA(LPI2C_Type *base, bool enableTx, bool enableRx)
615 {
616     base->MDER = LPI2C_MDER_TDDE(enableTx) | LPI2C_MDER_RDDE(enableRx);
617 }
618 
619 /*!
620  * @brief Gets LPI2C master transmit data register address for DMA transfer.
621  *
622  * @param base The LPI2C peripheral base address.
623  * @return The LPI2C Master Transmit Data Register address.
624  */
LPI2C_MasterGetTxFifoAddress(LPI2C_Type * base)625 static inline uint32_t LPI2C_MasterGetTxFifoAddress(LPI2C_Type *base)
626 {
627     return (uint32_t)&base->MTDR;
628 }
629 
630 /*!
631  * @brief Gets LPI2C master receive data register address for DMA transfer.
632  *
633  * @param base The LPI2C peripheral base address.
634  * @return The LPI2C Master Receive Data Register address.
635  */
LPI2C_MasterGetRxFifoAddress(LPI2C_Type * base)636 static inline uint32_t LPI2C_MasterGetRxFifoAddress(LPI2C_Type *base)
637 {
638     return (uint32_t)&base->MRDR;
639 }
640 
641 /*@}*/
642 
643 /*! @name FIFO control */
644 /*@{*/
645 
646 /*!
647  * @brief Sets the watermarks for LPI2C master FIFOs.
648  *
649  * @param base The LPI2C peripheral base address.
650  * @param txWords Transmit FIFO watermark value in words. The #kLPI2C_MasterTxReadyFlag flag is set whenever
651  *      the number of words in the transmit FIFO is equal or less than @a txWords. Writing a value equal or
652  *      greater than the FIFO size is truncated.
653  * @param rxWords Receive FIFO watermark value in words. The #kLPI2C_MasterRxReadyFlag flag is set whenever
654  *      the number of words in the receive FIFO is greater than @a rxWords. Writing a value equal or greater
655  *      than the FIFO size is truncated.
656  */
LPI2C_MasterSetWatermarks(LPI2C_Type * base,size_t txWords,size_t rxWords)657 static inline void LPI2C_MasterSetWatermarks(LPI2C_Type *base, size_t txWords, size_t rxWords)
658 {
659     base->MFCR = LPI2C_MFCR_TXWATER(txWords) | LPI2C_MFCR_RXWATER(rxWords);
660 }
661 
662 /*!
663  * @brief Gets the current number of words in the LPI2C master FIFOs.
664  *
665  * @param base The LPI2C peripheral base address.
666  * @param[out] txCount Pointer through which the current number of words in the transmit FIFO is returned.
667  *      Pass NULL if this value is not required.
668  * @param[out] rxCount Pointer through which the current number of words in the receive FIFO is returned.
669  *      Pass NULL if this value is not required.
670  */
LPI2C_MasterGetFifoCounts(LPI2C_Type * base,size_t * rxCount,size_t * txCount)671 static inline void LPI2C_MasterGetFifoCounts(LPI2C_Type *base, size_t *rxCount, size_t *txCount)
672 {
673     if (NULL != txCount)
674     {
675         *txCount = (base->MFSR & LPI2C_MFSR_TXCOUNT_MASK) >> LPI2C_MFSR_TXCOUNT_SHIFT;
676     }
677     if (NULL != rxCount)
678     {
679         *rxCount = (base->MFSR & LPI2C_MFSR_RXCOUNT_MASK) >> LPI2C_MFSR_RXCOUNT_SHIFT;
680     }
681 }
682 
683 /*@}*/
684 
685 /*! @name Bus operations */
686 /*@{*/
687 
688 /*!
689  * @brief Sets the I2C bus frequency for master transactions.
690  *
691  * The LPI2C master is automatically disabled and re-enabled as necessary to configure the baud
692  * rate. Do not call this function during a transfer, or the transfer is aborted.
693  *
694  * @note Please note that the second parameter is the clock frequency of LPI2C module, the third
695  * parameter means user configured bus baudrate, this implementation is different from other I2C drivers
696  * which use baudrate configuration as second parameter and source clock frequency as third parameter.
697  *
698  * @param base The LPI2C peripheral base address.
699  * @param sourceClock_Hz LPI2C functional clock frequency in Hertz.
700  * @param baudRate_Hz Requested bus frequency in Hertz.
701  */
702 void LPI2C_MasterSetBaudRate(LPI2C_Type *base, uint32_t sourceClock_Hz, uint32_t baudRate_Hz);
703 
704 /*!
705  * @brief Returns whether the bus is idle.
706  *
707  * Requires the master mode to be enabled.
708  *
709  * @param base The LPI2C peripheral base address.
710  * @retval true Bus is busy.
711  * @retval false Bus is idle.
712  */
LPI2C_MasterGetBusIdleState(LPI2C_Type * base)713 static inline bool LPI2C_MasterGetBusIdleState(LPI2C_Type *base)
714 {
715     return ((base->MSR & LPI2C_MSR_BBF_MASK) >> LPI2C_MSR_BBF_SHIFT) == 1U ? true : false;
716 }
717 
718 /*!
719  * @brief Sends a START signal and slave address on the I2C bus.
720  *
721  * This function is used to initiate a new master mode transfer. First, the bus state is checked to ensure
722  * that another master is not occupying the bus. Then a START signal is transmitted, followed by the
723  * 7-bit address specified in the @a address parameter. Note that this function does not actually wait
724  * until the START and address are successfully sent on the bus before returning.
725  *
726  * @param base The LPI2C peripheral base address.
727  * @param address 7-bit slave device address, in bits [6:0].
728  * @param dir Master transfer direction, either #kLPI2C_Read or #kLPI2C_Write. This parameter is used to set
729  *      the R/w bit (bit 0) in the transmitted slave address.
730  * @retval kStatus_Success START signal and address were successfully enqueued in the transmit FIFO.
731  * @retval #kStatus_LPI2C_Busy Another master is currently utilizing the bus.
732  */
733 status_t LPI2C_MasterStart(LPI2C_Type *base, uint8_t address, lpi2c_direction_t dir);
734 
735 /*!
736  * @brief Sends a repeated START signal and slave address on the I2C bus.
737  *
738  * This function is used to send a Repeated START signal when a transfer is already in progress. Like
739  * LPI2C_MasterStart(), it also sends the specified 7-bit address.
740  *
741  * @note This function exists primarily to maintain compatible APIs between LPI2C and I2C drivers,
742  *      as well as to better document the intent of code that uses these APIs.
743  *
744  * @param base The LPI2C peripheral base address.
745  * @param address 7-bit slave device address, in bits [6:0].
746  * @param dir Master transfer direction, either #kLPI2C_Read or #kLPI2C_Write. This parameter is used to set
747  *      the R/w bit (bit 0) in the transmitted slave address.
748  * @retval kStatus_Success Repeated START signal and address were successfully enqueued in the transmit FIFO.
749  * @retval #kStatus_LPI2C_Busy Another master is currently utilizing the bus.
750  */
LPI2C_MasterRepeatedStart(LPI2C_Type * base,uint8_t address,lpi2c_direction_t dir)751 static inline status_t LPI2C_MasterRepeatedStart(LPI2C_Type *base, uint8_t address, lpi2c_direction_t dir)
752 {
753     return LPI2C_MasterStart(base, address, dir);
754 }
755 
756 /*!
757  * @brief Performs a polling send transfer on the I2C bus.
758  *
759  * Sends up to @a txSize number of bytes to the previously addressed slave device. The slave may
760  * reply with a NAK to any byte in order to terminate the transfer early. If this happens, this
761  * function returns #kStatus_LPI2C_Nak.
762  *
763  * @param base  The LPI2C peripheral base address.
764  * @param txBuff The pointer to the data to be transferred.
765  * @param txSize The length in bytes of the data to be transferred.
766  * @retval kStatus_Success Data was sent successfully.
767  * @retval #kStatus_LPI2C_Busy Another master is currently utilizing the bus.
768  * @retval #kStatus_LPI2C_Nak The slave device sent a NAK in response to a byte.
769  * @retval #kStatus_LPI2C_FifoError FIFO under run or over run.
770  * @retval #kStatus_LPI2C_ArbitrationLost Arbitration lost error.
771  * @retval #kStatus_LPI2C_PinLowTimeout SCL or SDA were held low longer than the timeout.
772  */
773 status_t LPI2C_MasterSend(LPI2C_Type *base, void *txBuff, size_t txSize);
774 
775 /*!
776  * @brief Performs a polling receive transfer on the I2C bus.
777  *
778  * @param base  The LPI2C peripheral base address.
779  * @param rxBuff The pointer to the data to be transferred.
780  * @param rxSize The length in bytes of the data to be transferred.
781  * @retval kStatus_Success Data was received successfully.
782  * @retval #kStatus_LPI2C_Busy Another master is currently utilizing the bus.
783  * @retval #kStatus_LPI2C_Nak The slave device sent a NAK in response to a byte.
784  * @retval #kStatus_LPI2C_FifoError FIFO under run or overrun.
785  * @retval #kStatus_LPI2C_ArbitrationLost Arbitration lost error.
786  * @retval #kStatus_LPI2C_PinLowTimeout SCL or SDA were held low longer than the timeout.
787  */
788 status_t LPI2C_MasterReceive(LPI2C_Type *base, void *rxBuff, size_t rxSize);
789 
790 /*!
791  * @brief Sends a STOP signal on the I2C bus.
792  *
793  * This function does not return until the STOP signal is seen on the bus, or an error occurs.
794  *
795  * @param base The LPI2C peripheral base address.
796  * @retval kStatus_Success The STOP signal was successfully sent on the bus and the transaction terminated.
797  * @retval #kStatus_LPI2C_Busy Another master is currently utilizing the bus.
798  * @retval #kStatus_LPI2C_Nak The slave device sent a NAK in response to a byte.
799  * @retval #kStatus_LPI2C_FifoError FIFO under run or overrun.
800  * @retval #kStatus_LPI2C_ArbitrationLost Arbitration lost error.
801  * @retval #kStatus_LPI2C_PinLowTimeout SCL or SDA were held low longer than the timeout.
802  */
803 status_t LPI2C_MasterStop(LPI2C_Type *base);
804 
805 /*!
806  * @brief Performs a master polling transfer on the I2C bus.
807  *
808  * @note The API does not return until the transfer succeeds or fails due
809  * to error happens during transfer.
810  *
811  * @param base The LPI2C peripheral base address.
812  * @param transfer Pointer to the transfer structure.
813  * @retval kStatus_Success Data was received successfully.
814  * @retval #kStatus_LPI2C_Busy Another master is currently utilizing the bus.
815  * @retval #kStatus_LPI2C_Nak The slave device sent a NAK in response to a byte.
816  * @retval #kStatus_LPI2C_FifoError FIFO under run or overrun.
817  * @retval #kStatus_LPI2C_ArbitrationLost Arbitration lost error.
818  * @retval #kStatus_LPI2C_PinLowTimeout SCL or SDA were held low longer than the timeout.
819  */
820 status_t LPI2C_MasterTransferBlocking(LPI2C_Type *base, lpi2c_master_transfer_t *transfer);
821 
822 /*@}*/
823 
824 /*! @name Non-blocking */
825 /*@{*/
826 
827 /*!
828  * @brief Creates a new handle for the LPI2C master non-blocking APIs.
829  *
830  * The creation of a handle is for use with the non-blocking APIs. Once a handle
831  * is created, there is not a corresponding destroy handle. If the user wants to
832  * terminate a transfer, the LPI2C_MasterTransferAbort() API shall be called.
833  *
834  *
835  * @note The function also enables the NVIC IRQ for the input LPI2C. Need to notice
836  * that on some SoCs the LPI2C IRQ is connected to INTMUX, in this case user needs to
837  * enable the associated INTMUX IRQ in application.
838  *
839  * @param base The LPI2C peripheral base address.
840  * @param[out] handle Pointer to the LPI2C master driver handle.
841  * @param callback User provided pointer to the asynchronous callback function.
842  * @param userData User provided pointer to the application callback data.
843  */
844 void LPI2C_MasterTransferCreateHandle(LPI2C_Type *base,
845                                       lpi2c_master_handle_t *handle,
846                                       lpi2c_master_transfer_callback_t callback,
847                                       void *userData);
848 
849 /*!
850  * @brief Performs a non-blocking transaction on the I2C bus.
851  *
852  * @param base The LPI2C peripheral base address.
853  * @param handle Pointer to the LPI2C master driver handle.
854  * @param transfer The pointer to the transfer descriptor.
855  * @retval kStatus_Success The transaction was started successfully.
856  * @retval #kStatus_LPI2C_Busy Either another master is currently utilizing the bus, or a non-blocking
857  *      transaction is already in progress.
858  */
859 status_t LPI2C_MasterTransferNonBlocking(LPI2C_Type *base,
860                                          lpi2c_master_handle_t *handle,
861                                          lpi2c_master_transfer_t *transfer);
862 
863 /*!
864  * @brief Returns number of bytes transferred so far.
865  * @param base The LPI2C peripheral base address.
866  * @param handle Pointer to the LPI2C master driver handle.
867  * @param[out] count Number of bytes transferred so far by the non-blocking transaction.
868  * @retval kStatus_Success
869  * @retval kStatus_NoTransferInProgress There is not a non-blocking transaction currently in progress.
870  */
871 status_t LPI2C_MasterTransferGetCount(LPI2C_Type *base, lpi2c_master_handle_t *handle, size_t *count);
872 
873 /*!
874  * @brief Terminates a non-blocking LPI2C master transmission early.
875  *
876  * @note It is not safe to call this function from an IRQ handler that has a higher priority than the
877  *      LPI2C peripheral's IRQ priority.
878  *
879  * @param base The LPI2C peripheral base address.
880  * @param handle Pointer to the LPI2C master driver handle.
881  * @retval kStatus_Success A transaction was successfully aborted.
882  * @retval #kStatus_LPI2C_Idle There is not a non-blocking transaction currently in progress.
883  */
884 void LPI2C_MasterTransferAbort(LPI2C_Type *base, lpi2c_master_handle_t *handle);
885 
886 /*@}*/
887 
888 /*! @name IRQ handler */
889 /*@{*/
890 
891 /*!
892  * @brief Reusable routine to handle master interrupts.
893  * @note This function does not need to be called unless you are reimplementing the
894  *  nonblocking API's interrupt handler routines to add special functionality.
895  * @param base The LPI2C peripheral base address.
896  * @param handle Pointer to the LPI2C master driver handle.
897  */
898 void LPI2C_MasterTransferHandleIRQ(LPI2C_Type *base, lpi2c_master_handle_t *handle);
899 
900 /*@}*/
901 
902 /*! @} */
903 
904 /*!
905  * @addtogroup lpi2c_slave_driver
906  * @{
907  */
908 
909 /*! @name Slave initialization and deinitialization */
910 /*@{*/
911 
912 /*!
913  * @brief Provides a default configuration for the LPI2C slave peripheral.
914  *
915  * This function provides the following default configuration for the LPI2C slave peripheral:
916  * @code
917  *  slaveConfig->enableSlave               = true;
918  *  slaveConfig->address0                  = 0U;
919  *  slaveConfig->address1                  = 0U;
920  *  slaveConfig->addressMatchMode          = kLPI2C_MatchAddress0;
921  *  slaveConfig->filterDozeEnable          = true;
922  *  slaveConfig->filterEnable              = true;
923  *  slaveConfig->enableGeneralCall         = false;
924  *  slaveConfig->sclStall.enableAck        = false;
925  *  slaveConfig->sclStall.enableTx         = true;
926  *  slaveConfig->sclStall.enableRx         = true;
927  *  slaveConfig->sclStall.enableAddress    = true;
928  *  slaveConfig->ignoreAck                 = false;
929  *  slaveConfig->enableReceivedAddressRead = false;
930  *  slaveConfig->sdaGlitchFilterWidth_ns   = 0;
931  *  slaveConfig->sclGlitchFilterWidth_ns   = 0;
932  *  slaveConfig->dataValidDelay_ns         = 0;
933  *  slaveConfig->clockHoldTime_ns          = 0;
934  * @endcode
935  *
936  * After calling this function, override any settings  to customize the configuration,
937  * prior to initializing the master driver with LPI2C_SlaveInit(). Be sure to override at least the @a
938  * address0 member of the configuration structure with the desired slave address.
939  *
940  * @param[out] slaveConfig User provided configuration structure that is set to default values. Refer to
941  *      #lpi2c_slave_config_t.
942  */
943 void LPI2C_SlaveGetDefaultConfig(lpi2c_slave_config_t *slaveConfig);
944 
945 /*!
946  * @brief Initializes the LPI2C slave peripheral.
947  *
948  * This function enables the peripheral clock and initializes the LPI2C slave peripheral as described by the user
949  * provided configuration.
950  *
951  * @param base The LPI2C peripheral base address.
952  * @param slaveConfig User provided peripheral configuration. Use LPI2C_SlaveGetDefaultConfig() to get a set of defaults
953  *      that you can override.
954  * @param sourceClock_Hz Frequency in Hertz of the LPI2C functional clock. Used to calculate the filter widths,
955  *      data valid delay, and clock hold time.
956  */
957 void LPI2C_SlaveInit(LPI2C_Type *base, const lpi2c_slave_config_t *slaveConfig, uint32_t sourceClock_Hz);
958 
959 /*!
960  * @brief Deinitializes the LPI2C slave peripheral.
961  *
962  * This function disables the LPI2C slave peripheral and gates the clock. It also performs a software
963  * reset to restore the peripheral to reset conditions.
964  *
965  * @param base The LPI2C peripheral base address.
966  */
967 void LPI2C_SlaveDeinit(LPI2C_Type *base);
968 
969 /*!
970  * @brief Performs a software reset of the LPI2C slave peripheral.
971  *
972  * @param base The LPI2C peripheral base address.
973  */
LPI2C_SlaveReset(LPI2C_Type * base)974 static inline void LPI2C_SlaveReset(LPI2C_Type *base)
975 {
976     base->SCR = LPI2C_SCR_RST_MASK;
977     base->SCR = 0;
978 }
979 
980 /*!
981  * @brief Enables or disables the LPI2C module as slave.
982  *
983  * @param base The LPI2C peripheral base address.
984  * @param enable Pass true to enable or false to disable the specified LPI2C as slave.
985  */
LPI2C_SlaveEnable(LPI2C_Type * base,bool enable)986 static inline void LPI2C_SlaveEnable(LPI2C_Type *base, bool enable)
987 {
988     base->SCR = (base->SCR & ~LPI2C_SCR_SEN_MASK) | LPI2C_SCR_SEN(enable);
989 }
990 
991 /*@}*/
992 
993 /*! @name Slave status */
994 /*@{*/
995 
996 /*!
997  * @brief Gets the LPI2C slave status flags.
998  *
999  * A bit mask with the state of all LPI2C slave status flags is returned. For each flag, the corresponding bit
1000  * in the return value is set if the flag is asserted.
1001  *
1002  * @param base The LPI2C peripheral base address.
1003  * @return State of the status flags:
1004  *         - 1: related status flag is set.
1005  *         - 0: related status flag is not set.
1006  * @see _lpi2c_slave_flags
1007  */
LPI2C_SlaveGetStatusFlags(LPI2C_Type * base)1008 static inline uint32_t LPI2C_SlaveGetStatusFlags(LPI2C_Type *base)
1009 {
1010     return base->SSR;
1011 }
1012 
1013 /*!
1014  * @brief Clears the LPI2C status flag state.
1015  *
1016  * The following status register flags can be cleared:
1017  * - #kLPI2C_SlaveRepeatedStartDetectFlag
1018  * - #kLPI2C_SlaveStopDetectFlag
1019  * - #kLPI2C_SlaveBitErrFlag
1020  * - #kLPI2C_SlaveFifoErrFlag
1021  *
1022  * Attempts to clear other flags has no effect.
1023  *
1024  * @param base The LPI2C peripheral base address.
1025  * @param statusMask A bitmask of status flags that are to be cleared. The mask is composed of
1026  *  #_lpi2c_slave_flags enumerators OR'd together. You may pass the result of a previous call to
1027  *  LPI2C_SlaveGetStatusFlags().
1028  * @see _lpi2c_slave_flags.
1029  */
LPI2C_SlaveClearStatusFlags(LPI2C_Type * base,uint32_t statusMask)1030 static inline void LPI2C_SlaveClearStatusFlags(LPI2C_Type *base, uint32_t statusMask)
1031 {
1032     base->SSR = statusMask;
1033 }
1034 
1035 /*@}*/
1036 
1037 /*! @name Slave interrupts */
1038 /*@{*/
1039 
1040 /*!
1041  * @brief Enables the LPI2C slave interrupt requests.
1042  *
1043  * All flags except #kLPI2C_SlaveBusyFlag and #kLPI2C_SlaveBusBusyFlag can be enabled as
1044  * interrupts.
1045  *
1046  * @param base The LPI2C peripheral base address.
1047  * @param interruptMask Bit mask of interrupts to enable. See #_lpi2c_slave_flags for the set
1048  *      of constants that should be OR'd together to form the bit mask.
1049  */
LPI2C_SlaveEnableInterrupts(LPI2C_Type * base,uint32_t interruptMask)1050 static inline void LPI2C_SlaveEnableInterrupts(LPI2C_Type *base, uint32_t interruptMask)
1051 {
1052     base->SIER |= interruptMask;
1053 }
1054 
1055 /*!
1056  * @brief Disables the LPI2C slave interrupt requests.
1057  *
1058  * All flags except #kLPI2C_SlaveBusyFlag and #kLPI2C_SlaveBusBusyFlag can be enabled as
1059  * interrupts.
1060  *
1061  * @param base The LPI2C peripheral base address.
1062  * @param interruptMask Bit mask of interrupts to disable. See #_lpi2c_slave_flags for the set
1063  *      of constants that should be OR'd together to form the bit mask.
1064  */
LPI2C_SlaveDisableInterrupts(LPI2C_Type * base,uint32_t interruptMask)1065 static inline void LPI2C_SlaveDisableInterrupts(LPI2C_Type *base, uint32_t interruptMask)
1066 {
1067     base->SIER &= ~interruptMask;
1068 }
1069 
1070 /*!
1071  * @brief Returns the set of currently enabled LPI2C slave interrupt requests.
1072  * @param base The LPI2C peripheral base address.
1073  * @return A bitmask composed of #_lpi2c_slave_flags enumerators OR'd together to indicate the
1074  *      set of enabled interrupts.
1075  */
LPI2C_SlaveGetEnabledInterrupts(LPI2C_Type * base)1076 static inline uint32_t LPI2C_SlaveGetEnabledInterrupts(LPI2C_Type *base)
1077 {
1078     return base->SIER;
1079 }
1080 
1081 /*@}*/
1082 
1083 /*! @name Slave DMA control */
1084 /*@{*/
1085 
1086 /*!
1087  * @brief Enables or disables the LPI2C slave peripheral DMA requests.
1088  *
1089  * @param base The LPI2C peripheral base address.
1090  * @param enableAddressValid Enable flag for the address valid DMA request. Pass true for enable, false for disable.
1091  *      The address valid DMA request is shared with the receive data DMA request.
1092  * @param enableRx Enable flag for the receive data DMA request. Pass true for enable, false for disable.
1093  * @param enableTx Enable flag for the transmit data DMA request. Pass true for enable, false for disable.
1094  */
LPI2C_SlaveEnableDMA(LPI2C_Type * base,bool enableAddressValid,bool enableRx,bool enableTx)1095 static inline void LPI2C_SlaveEnableDMA(LPI2C_Type *base, bool enableAddressValid, bool enableRx, bool enableTx)
1096 {
1097     base->SDER = (base->SDER & ~(LPI2C_SDER_AVDE_MASK | LPI2C_SDER_RDDE_MASK | LPI2C_SDER_TDDE_MASK)) |
1098                  LPI2C_SDER_AVDE(enableAddressValid) | LPI2C_SDER_RDDE(enableRx) | LPI2C_SDER_TDDE(enableTx);
1099 }
1100 
1101 /*@}*/
1102 
1103 /*! @name Slave bus operations */
1104 /*@{*/
1105 
1106 /*!
1107  * @brief Returns whether the bus is idle.
1108  *
1109  * Requires the slave mode to be enabled.
1110  *
1111  * @param base The LPI2C peripheral base address.
1112  * @retval true Bus is busy.
1113  * @retval false Bus is idle.
1114  */
LPI2C_SlaveGetBusIdleState(LPI2C_Type * base)1115 static inline bool LPI2C_SlaveGetBusIdleState(LPI2C_Type *base)
1116 {
1117     return ((base->SSR & LPI2C_SSR_BBF_MASK) >> LPI2C_SSR_BBF_SHIFT) == 1U ? true : false;
1118 }
1119 
1120 /*!
1121  * @brief Transmits either an ACK or NAK on the I2C bus in response to a byte from the master.
1122  *
1123  * Use this function to send an ACK or NAK when the #kLPI2C_SlaveTransmitAckFlag is asserted. This
1124  * only happens if you enable the sclStall.enableAck field of the ::lpi2c_slave_config_t configuration
1125  * structure used to initialize the slave peripheral.
1126  *
1127  * @param base The LPI2C peripheral base address.
1128  * @param ackOrNack Pass true for an ACK or false for a NAK.
1129  */
LPI2C_SlaveTransmitAck(LPI2C_Type * base,bool ackOrNack)1130 static inline void LPI2C_SlaveTransmitAck(LPI2C_Type *base, bool ackOrNack)
1131 {
1132     base->STAR = LPI2C_STAR_TXNACK(!ackOrNack);
1133 }
1134 
1135 /*!
1136  * @brief Returns the slave address sent by the I2C master.
1137  *
1138  * This function should only be called if the #kLPI2C_SlaveAddressValidFlag is asserted.
1139  *
1140  * @param base The LPI2C peripheral base address.
1141  * @return The 8-bit address matched by the LPI2C slave. Bit 0 contains the R/w direction bit, and
1142  *      the 7-bit slave address is in the upper 7 bits.
1143  */
LPI2C_SlaveGetReceivedAddress(LPI2C_Type * base)1144 static inline uint32_t LPI2C_SlaveGetReceivedAddress(LPI2C_Type *base)
1145 {
1146     return base->SASR & LPI2C_SASR_RADDR_MASK;
1147 }
1148 
1149 /*!
1150  * @brief Performs a polling send transfer on the I2C bus.
1151  *
1152  * @param base  The LPI2C peripheral base address.
1153  * @param txBuff The pointer to the data to be transferred.
1154  * @param txSize The length in bytes of the data to be transferred.
1155  * @param[out] actualTxSize
1156  * @return Error or success status returned by API.
1157  */
1158 status_t LPI2C_SlaveSend(LPI2C_Type *base, void *txBuff, size_t txSize, size_t *actualTxSize);
1159 
1160 /*!
1161  * @brief Performs a polling receive transfer on the I2C bus.
1162  *
1163  * @param base  The LPI2C peripheral base address.
1164  * @param rxBuff The pointer to the data to be transferred.
1165  * @param rxSize The length in bytes of the data to be transferred.
1166  * @param[out] actualRxSize
1167  * @return Error or success status returned by API.
1168  */
1169 status_t LPI2C_SlaveReceive(LPI2C_Type *base, void *rxBuff, size_t rxSize, size_t *actualRxSize);
1170 
1171 /*@}*/
1172 
1173 /*! @name Slave non-blocking */
1174 /*@{*/
1175 
1176 /*!
1177  * @brief Creates a new handle for the LPI2C slave non-blocking APIs.
1178  *
1179  * The creation of a handle is for use with the non-blocking APIs. Once a handle
1180  * is created, there is not a corresponding destroy handle. If the user wants to
1181  * terminate a transfer, the LPI2C_SlaveTransferAbort() API shall be called.
1182  *
1183  * @note The function also enables the NVIC IRQ for the input LPI2C. Need to notice
1184  * that on some SoCs the LPI2C IRQ is connected to INTMUX, in this case user needs to
1185  * enable the associated INTMUX IRQ in application.
1186 
1187  * @param base The LPI2C peripheral base address.
1188  * @param[out] handle Pointer to the LPI2C slave driver handle.
1189  * @param callback User provided pointer to the asynchronous callback function.
1190  * @param userData User provided pointer to the application callback data.
1191  */
1192 void LPI2C_SlaveTransferCreateHandle(LPI2C_Type *base,
1193                                      lpi2c_slave_handle_t *handle,
1194                                      lpi2c_slave_transfer_callback_t callback,
1195                                      void *userData);
1196 
1197 /*!
1198  * @brief Starts accepting slave transfers.
1199  *
1200  * Call this API after calling I2C_SlaveInit() and LPI2C_SlaveTransferCreateHandle() to start processing
1201  * transactions driven by an I2C master. The slave monitors the I2C bus and pass events to the
1202  * callback that was passed into the call to LPI2C_SlaveTransferCreateHandle(). The callback is always invoked
1203  * from the interrupt context.
1204  *
1205  * The set of events received by the callback is customizable. To do so, set the @a eventMask parameter to
1206  * the OR'd combination of #lpi2c_slave_transfer_event_t enumerators for the events you wish to receive.
1207  * The #kLPI2C_SlaveTransmitEvent and #kLPI2C_SlaveReceiveEvent events are always enabled and do not need
1208  * to be included in the mask. Alternatively, you can pass 0 to get a default set of only the transmit and
1209  * receive events that are always enabled. In addition, the #kLPI2C_SlaveAllEvents constant is provided as
1210  * a convenient way to enable all events.
1211  *
1212  * @param base The LPI2C peripheral base address.
1213  * @param handle Pointer to lpi2c_slave_handle_t structure which stores the transfer state.
1214  * @param eventMask Bit mask formed by OR'ing together #lpi2c_slave_transfer_event_t enumerators to specify
1215  *      which events to send to the callback. Other accepted values are 0 to get a default set of
1216  *      only the transmit and receive events, and #kLPI2C_SlaveAllEvents to enable all events.
1217  *
1218  * @retval kStatus_Success Slave transfers were successfully started.
1219  * @retval #kStatus_LPI2C_Busy Slave transfers have already been started on this handle.
1220  */
1221 status_t LPI2C_SlaveTransferNonBlocking(LPI2C_Type *base, lpi2c_slave_handle_t *handle, uint32_t eventMask);
1222 
1223 /*!
1224  * @brief Gets the slave transfer status during a non-blocking transfer.
1225  * @param base The LPI2C peripheral base address.
1226  * @param handle Pointer to i2c_slave_handle_t structure.
1227  * @param[out] count Pointer to a value to hold the number of bytes transferred. May be NULL if the count is not
1228  *      required.
1229  * @retval kStatus_Success
1230  * @retval kStatus_NoTransferInProgress
1231  */
1232 status_t LPI2C_SlaveTransferGetCount(LPI2C_Type *base, lpi2c_slave_handle_t *handle, size_t *count);
1233 
1234 /*!
1235  * @brief Aborts the slave non-blocking transfers.
1236  * @note This API could be called at any time to stop slave for handling the bus events.
1237  * @param base The LPI2C peripheral base address.
1238  * @param handle Pointer to lpi2c_slave_handle_t structure which stores the transfer state.
1239  * @retval kStatus_Success
1240  * @retval #kStatus_LPI2C_Idle
1241  */
1242 void LPI2C_SlaveTransferAbort(LPI2C_Type *base, lpi2c_slave_handle_t *handle);
1243 
1244 /*@}*/
1245 
1246 /*! @name Slave IRQ handler */
1247 /*@{*/
1248 
1249 /*!
1250  * @brief Reusable routine to handle slave interrupts.
1251  * @note This function does not need to be called unless you are reimplementing the
1252  *  non blocking API's interrupt handler routines to add special functionality.
1253  * @param base The LPI2C peripheral base address.
1254  * @param handle Pointer to lpi2c_slave_handle_t structure which stores the transfer state.
1255  */
1256 void LPI2C_SlaveTransferHandleIRQ(LPI2C_Type *base, lpi2c_slave_handle_t *handle);
1257 
1258 /*@}*/
1259 
1260 /*! @} */
1261 
1262 #if defined(__cplusplus)
1263 }
1264 #endif
1265 
1266 #endif /* _FSL_LPI2C_H_ */
1267