S32 SDK
flexio_i2c_driver.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2014 - 2016, Freescale Semiconductor, Inc.
3  * Copyright 2016 NXP
4  * All rights reserved.
5  *
6  * THIS SOFTWARE IS PROVIDED BY NXP "AS IS" AND ANY EXPRESSED OR
7  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
8  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
9  * IN NO EVENT SHALL NXP OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
10  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
11  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
12  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
13  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
14  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
15  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
16  * THE POSSIBILITY OF SUCH DAMAGE.
17  */
18 
19 #ifndef FLEXIO_I2C_DRIVER_H
20 #define FLEXIO_I2C_DRIVER_H
21 
22 #include <stddef.h>
23 #include <stdbool.h>
24 #include "device_registers.h"
25 #include "flexio_hal.h"
26 #include "flexio.h"
27 #include "edma_driver.h"
28 
59 /*******************************************************************************
60  * Enumerations.
61  ******************************************************************************/
62 
63 
68 #define FLEXIO_I2C_MAX_SIZE (((uint32_t)((0xFFU - 1U) / 18U)) - 1U)
69 
71 /* Length of the DMA scatter-gather chain for Tx. Two blocks are needed, but as the fist one is
72  loaded directly in the TCD, only one block needs to be in RAM */
73 #define FLEXIO_I2C_DMA_TX_CHAIN_LENGTH (1U)
74 /* Length of the DMA scatter-gather chain for Rx. Six blocks are needed, but as the fist one is
75  loaded directly in the TCD, only five blocks needs to be in RAM */
76 #define FLEXIO_I2C_DMA_RX_CHAIN_LENGTH (5U)
77 /* Total size of the DMA scatter-gather buffer. Add 1 because STCD_SIZE already compensates for the
78  first stcd not being loaded in RAM */
79 #define FLEXIO_I2C_DMA_BUF_SIZE (STCD_SIZE(FLEXIO_I2C_DMA_TX_CHAIN_LENGTH + FLEXIO_I2C_DMA_RX_CHAIN_LENGTH + 1U))
80 
83 /*******************************************************************************
84 * Definitions
85 ******************************************************************************/
86 
93 typedef struct
94 {
95  uint16_t slaveAddress;
97  uint32_t baudRate;
98  uint8_t sdaPin;
99  uint8_t sclPin;
105  uint8_t rxDMAChannel;
106  uint8_t txDMAChannel;
108 
109 
118 typedef struct
119 {
121  flexio_common_state_t flexioCommon; /* Common flexio drivers structure */
122  uint8_t *data; /* Transmit/Receive buffer. */
123  uint32_t txRemainingBytes; /* Number of remaining bytes to be transmitted. */
124  uint32_t rxRemainingBytes; /* Number of remaining bytes to be received. */
125  uint8_t rxDMAChannel; /* Rx DMA channel number */
126  uint8_t txDMAChannel; /* Tx DMA channel number */
127  flexio_callback_t callback; /* User callback function */
128  void *callbackParam; /* Parameter for the callback function */
129  uint16_t slaveAddress; /* Slave address */
130  flexio_driver_type_t driverType; /* Driver type: interrupts/polling/DMA */
131  status_t status; /* Current status of the driver */
132  bool receive; /* Transfer direction, true = receive, false = transmit */
133  bool addrReceived; /* Indicated start of receive (after address transmission) */
134  bool driverIdle; /* Idle/busy state of the driver */
135  semaphore_t idleSemaphore; /* Semaphore used by blocking functions */
136  bool blocking; /* Specifies if the current transfer is blocking */
137  bool sendStop; /* Specifies if STOP condition must be generated after current transfer */
138  uint8_t sdaPin; /* Flexio pin to use as I2C SDA pin */
139  uint8_t sclPin; /* Flexio pin to use as I2C SCL pin */
140  uint8_t dummyDmaIdle; /* Dummy location for DMA transfers. */
141  uint8_t dummyDmaStop; /* Dummy location for DMA transfers. */
142  uint8_t dummyDmaReceive; /* Dummy location for DMA transfers. */
143  uint8_t dmaReceiveTxStop0; /* Stores setting for setting Tx shifter stop bit to 0 */
144  uint8_t dmaReceiveTxStop1; /* Stores setting for setting Tx shifter stop bit to 1 */
145  uint8_t dmaReceiveRxStop1; /* Stores setting for setting Rx shifter stop bit to 1 */
146  uint8_t stcd[FLEXIO_I2C_DMA_BUF_SIZE]; /* Buffer for DMA scatter-gather operations */
149 
150 
151 /*******************************************************************************
152  * API
153  ******************************************************************************/
159 #if defined(__cplusplus)
160 extern "C" {
161 #endif
162 
179 status_t FLEXIO_I2C_DRV_MasterInit(uint32_t instance,
180  const flexio_i2c_master_user_config_t * userConfigPtr,
181  flexio_i2c_master_state_t * master);
182 
183 
195 
196 
213 
214 
225 
226 
237 
238 
256  const uint8_t * txBuff,
257  uint32_t txSize,
258  bool sendStop);
259 
260 
261 
276  const uint8_t * txBuff,
277  uint32_t txSize,
278  bool sendStop,
279  uint32_t timeout);
280 
281 
299  uint8_t * rxBuff,
300  uint32_t rxSize,
301  bool sendStop);
302 
303 
318  uint8_t * rxBuff,
319  uint32_t rxSize,
320  bool sendStop,
321  uint32_t timeout);
322 
323 
333 
334 
349 status_t FLEXIO_I2C_DRV_MasterGetStatus(flexio_i2c_master_state_t * master, uint32_t *bytesRemaining);
350 
351 
353 #if defined(__cplusplus)
354 }
355 #endif
356 
359 #endif /* FLEXIO_I2C_DRIVER_H */
360 /*******************************************************************************
361  * EOF
362  ******************************************************************************/
status_t FLEXIO_I2C_DRV_MasterInit(uint32_t instance, const flexio_i2c_master_user_config_t *userConfigPtr, flexio_i2c_master_state_t *master)
Initialize the FLEXIO_I2C master mode driver.
void(* flexio_callback_t)(void *driverState, flexio_event_t event, void *userData)
flexio callback function
Definition: flexio.h:74
status_t FLEXIO_I2C_DRV_MasterGetBaudRate(flexio_i2c_master_state_t *master, uint32_t *baudRate)
Get the currently configured baud rate.
status_t FLEXIO_I2C_DRV_MasterSetSlaveAddr(flexio_i2c_master_state_t *master, const uint16_t address)
Set the slave address for any subsequent I2C communication.
status_t
Status return codes. Common error codes will be a unified enumeration (C enum) that will contain all ...
Definition: status.h:31
status_t FLEXIO_I2C_DRV_MasterDeinit(flexio_i2c_master_state_t *master)
De-initialize the FLEXIO_I2C master mode driver.
status_t FLEXIO_I2C_DRV_MasterSetBaudRate(flexio_i2c_master_state_t *master, uint32_t baudRate)
Set the baud rate for any subsequent I2C communication.
flexio_driver_type_t
Driver type: interrupts/polling/DMA Implements : flexio_driver_type_t_Class.
Definition: flexio.h:42
Master internal context structure.
Master configuration structure.
status_t FLEXIO_I2C_DRV_MasterSendDataBlocking(flexio_i2c_master_state_t *master, const uint8_t *txBuff, uint32_t txSize, bool sendStop, uint32_t timeout)
Perform a blocking send transaction on the I2C bus.
status_t FLEXIO_I2C_DRV_MasterReceiveDataBlocking(flexio_i2c_master_state_t *master, uint8_t *rxBuff, uint32_t rxSize, bool sendStop, uint32_t timeout)
Perform a blocking receive transaction on the I2C bus.
status_t FLEXIO_I2C_DRV_MasterTransferAbort(flexio_i2c_master_state_t *master)
Aborts a non-blocking I2C master transaction.
status_t FLEXIO_I2C_DRV_MasterReceiveData(flexio_i2c_master_state_t *master, uint8_t *rxBuff, uint32_t rxSize, bool sendStop)
Perform a non-blocking receive transaction on the I2C bus.
status_t FLEXIO_I2C_DRV_MasterGetStatus(flexio_i2c_master_state_t *master, uint32_t *bytesRemaining)
Get the status of the current non-blocking I2C master transaction.
status_t FLEXIO_I2C_DRV_MasterSendData(flexio_i2c_master_state_t *master, const uint8_t *txBuff, uint32_t txSize, bool sendStop)
Perform a non-blocking send transaction on the I2C bus.