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