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.h"
26 #include "edma_driver.h"
27 
58 /*******************************************************************************
59  * Enumerations.
60  ******************************************************************************/
61 
62 
67 #define FLEXIO_I2C_MAX_SIZE (((uint32_t)((0xFFU - 1U) / 18U)) - 1U)
68 
70 /* Length of the DMA scatter-gather chain for Tx. Two blocks are needed, but as the fist one is
71  loaded directly in the TCD, only one block needs to be in RAM */
72 #define FLEXIO_I2C_DMA_TX_CHAIN_LENGTH (1U)
73 /* Length of the DMA scatter-gather chain for Rx. Six blocks are needed, but as the fist one is
74  loaded directly in the TCD, only five blocks needs to be in RAM */
75 #define FLEXIO_I2C_DMA_RX_CHAIN_LENGTH (5U)
76 /* Total size of the DMA scatter-gather buffer. Add 1 because STCD_SIZE already compensates for the
77  first stcd not being loaded in RAM */
78 #define FLEXIO_I2C_DMA_BUF_SIZE (STCD_SIZE(FLEXIO_I2C_DMA_TX_CHAIN_LENGTH + FLEXIO_I2C_DMA_RX_CHAIN_LENGTH + 1U))
79 
82 /*******************************************************************************
83 * Definitions
84 ******************************************************************************/
85 
92 typedef struct
93 {
94  uint16_t slaveAddress;
96  uint32_t baudRate;
97  uint8_t sdaPin;
98  uint8_t sclPin;
104  uint8_t rxDMAChannel;
105  uint8_t txDMAChannel;
107 
108 
117 typedef struct
118 {
120  flexio_common_state_t flexioCommon; /* Common flexio drivers structure */
121  uint8_t *data; /* Transmit/Receive buffer. */
122  uint32_t txRemainingBytes; /* Number of remaining bytes to be transmitted. */
123  uint32_t rxRemainingBytes; /* Number of remaining bytes to be received. */
124  uint8_t rxDMAChannel; /* Rx DMA channel number */
125  uint8_t txDMAChannel; /* Tx DMA channel number */
126  flexio_callback_t callback; /* User callback function */
127  void *callbackParam; /* Parameter for the callback function */
128  uint16_t slaveAddress; /* Slave address */
129  flexio_driver_type_t driverType; /* Driver type: interrupts/polling/DMA */
130  status_t status; /* Current status of the driver */
131  bool receive; /* Transfer direction, true = receive, false = transmit */
132  bool addrReceived; /* Indicated start of receive (after address transmission) */
133  bool driverIdle; /* Idle/busy state of the driver */
134  semaphore_t idleSemaphore; /* Semaphore used by blocking functions */
135  bool blocking; /* Specifies if the current transfer is blocking */
136  bool sendStop; /* Specifies if STOP condition must be generated after current transfer */
137  uint8_t sdaPin; /* Flexio pin to use as I2C SDA pin */
138  uint8_t sclPin; /* Flexio pin to use as I2C SCL pin */
139  uint8_t dummyDmaIdle; /* Dummy location for DMA transfers. */
140  uint8_t dummyDmaStop; /* Dummy location for DMA transfers. */
141  uint8_t dummyDmaReceive; /* Dummy location for DMA transfers. */
142  uint8_t dmaReceiveTxStop0; /* Stores setting for setting Tx shifter stop bit to 0 */
143  uint8_t dmaReceiveTxStop1; /* Stores setting for setting Tx shifter stop bit to 1 */
144  uint8_t dmaReceiveRxStop1; /* Stores setting for setting Rx shifter stop bit to 1 */
145  uint8_t stcd[FLEXIO_I2C_DMA_BUF_SIZE]; /* Buffer for DMA scatter-gather operations */
148 
149 
150 /*******************************************************************************
151  * API
152  ******************************************************************************/
158 #if defined(__cplusplus)
159 extern "C" {
160 #endif
161 
178 status_t FLEXIO_I2C_DRV_MasterInit(uint32_t instance,
179  const flexio_i2c_master_user_config_t * userConfigPtr,
180  flexio_i2c_master_state_t * master);
181 
182 
194 
195 
212 
213 
224 
225 
236 
237 
255  const uint8_t * txBuff,
256  uint32_t txSize,
257  bool sendStop);
258 
259 
260 
275  const uint8_t * txBuff,
276  uint32_t txSize,
277  bool sendStop,
278  uint32_t timeout);
279 
280 
298  uint8_t * rxBuff,
299  uint32_t rxSize,
300  bool sendStop);
301 
302 
317  uint8_t * rxBuff,
318  uint32_t rxSize,
319  bool sendStop,
320  uint32_t timeout);
321 
322 
332 
333 
348 status_t FLEXIO_I2C_DRV_MasterGetStatus(flexio_i2c_master_state_t * master, uint32_t *bytesRemaining);
349 
350 
352 #if defined(__cplusplus)
353 }
354 #endif
355 
358 #endif /* FLEXIO_I2C_DRIVER_H */
359 /*******************************************************************************
360  * EOF
361  ******************************************************************************/
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:81
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:44
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:49
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.