1.2.21.62 1.3.25.62 1.4.21.62 1.5.25.62 1.6.22.62 1.7.23.62 1.9.19.62 1.29.23.62 1.30.18.62 1.31.18.62 1.32.26.62 1.33.16.62 1.37.18.62 1.38.21.62 1.39.18.62 1.40.20.62 SERCOMx_I2C_TransferDirGet Function

C

/* x = SERCOM instance number */

/* I2C slave mode */

SERCOM_I2C_SLAVE_TRANSFER_DIR SERCOMx_I2C_TransferDirGet(void)	

Summary

Returns the I2C transfer direction

Description

This function returns the I2C transfer direction

Precondition

SERCOMx_I2C_Initialize must have been called for the associated SERCOM I2C instance

Parameters

None.

Returns

SERCOM_I2C_SLAVE_TRANSFER_DIR_WRITE - I2C master is writing data to I2C slave

SERCOM_I2C_SLAVE_TRANSFER_DIR_READ - I2C master is reading data from I2C slave

Example

The below code snippet shows the use of the SERCOMx_I2C_TransferDirGet() API when SERCOM I2C slave PLIB is used in polled mode (interrupt is disabled).

static bool isFirstByteSent;
static bool transferDir;
SERCOM_I2C_SLAVE_INTFLAG intFlags = SERCOM0_I2C_InterruptFlagsGet();

if (intFlags & SERCOM_I2C_SLAVE_INTFLAG_AMATCH)
{
    isFirstByteSent = false;
    
    // Read and save the transfer direction in a global variable.
    transferDir = SERCOM0_I2C_TransferDirGet();
    
    // Send ACK succeeded by reception of next byte
    SERCOM0_I2C_SendCommand(SERCOM_I2C_SLAVE_COMMAND_SEND_ACK);
}
else if (intFlags & SERCOM_I2C_SLAVE_INTFLAG_DRDY)
{
    if (transferDir == SERCOM_I2C_SLAVE_TRANSFER_DIR_WRITE)
    {
        // I2C master is writing data to I2C slave. Read the received byte.
        rxByte = SERCOM0_I2C_ReadByte();
        
        // Execute acknowledge action succeeded by reception of next byte
        SERCOM0_I2C_SendCommand(SERCOM_I2C_SLAVE_COMMAND_SEND_ACK);
    }
    else
    {
        // I2C master is reading data from I2C slave. Check if the previous data
        // is acknowledged by the I2C master.
        
        if (isFirstByteSent == false)
        {
            SERCOM0_I2C_WriteByte(txData);
            isFirstByteSent = true;
        }
        else
        {
            if (SERCOM0_I2C_LastByteAckStatusGet() == SERCOM_I2C_SLAVE_ACK_STATUS_RECEIVED_ACK)
            {
                // Last byte was acknowledged by the I2C master; send another byte.
                
                SERCOM0_I2C_WriteByte(txData);
                
                //Execute a byte read operation followed by ACK/NACK reception
                SERCOM0_I2C_SendCommand(SERCOM_I2C_SLAVE_COMMAND_RECEIVE_ACK_NAK);
            }
            else
            {
                // Last byte was NAK'd by the I2C master; wait for start condition.
                
                //Wait for any start (S/Sr) condition
                SERCOM0_I2C_SendCommand(SERCOM_I2C_SLAVE_COMMAND_WAIT_FOR_START);
            }
        }
    }
}

Remarks

None.