1.30.18.66 SERCOMx_I2C_LastByteAckStatusGet Function

C

/* x = SERCOM instance number */

/* I2C slave mode */

SERCOM_I2C_SLAVE_ACK_STATUS SERCOMx_I2C_LastByteAckStatusGet(void)	

Summary

Returns the ACK status of the last byte written to the I2C master

Description

This function returns the ACK status of the last byte written to the I2C master

Precondition

SERCOMx_I2C_Initialize must have been called for the associated SERCOM I2C instance

Parameters

None.

Returns

SERCOM_I2C_SLAVE_ACK_STATUS_RECEIVED_ACK - I2C master acknowledged the last byte

SERCOM_I2C_SLAVE_ACK_STATUS_RECEIVED_NAK - I2C master sent NAK

Example

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

SERCOM_I2C_SLAVE_TRANSFER_DIR transferDir;
bool isFirstByteSent = false;

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 = SERCOM5_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_READ)
    {
        // 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

Since this API indicates the status of the last byte sent to the I2C master; for sending the first byte to the I2C master, the application must not call this API. Instead, the application should always send the first byte to I2C master's read request. The application would use this API when the SERCOM I2C slave PLIB is used in polled mode (interrupt is disabled).