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