iic
Vitis Drivers API Documentation
 All Data Structures Files Functions Variables Typedefs Macros Groups Pages
xiic_slave_example.c File Reference

Overview

Shows IIC slave-mode operation.

This file consists of a Interrupt mode design example which uses the Xilinx IIC device and XIic driver to exercise the slave functionality of the IIC device.

The XIic_SlaveSend() API is used to transmit the data and XIic_SlaveRecv() API is used to receive the data.

The example is tested on ML300/ML310/ML403/ML501 Xilinx boards.

The IIC devices that are present on the Xilinx boards do not support the Master functionality. This example has been tested with an off board external IIC Master device and the IIC device configured as a Slave.

This code assumes that no Operating System is being used.

MODIFICATION HISTORY:
Ver   Who  Date  Changes


1.00a mta 03/01/06 Created. 2.00a ktn 11/17/09 Updated to use the HAL APIs and replaced call to XIic_Initialize API with XIic_LookupConfig and XIic_CfgInitialize. Some of the macros have been renamed in the IIC driver and some renamed macros are used in this example. 3.4 ms 01/23/17 Added xil_printf statement in main function to ensure that "Successfully ran" and "Failed" strings are available in all examples. This is a fix for CR-965028. 3.10 gm 07/09/23 Added SDT support. 3.15 vlt 01/27/26 Fixed codespell issues. 3.15 vlt 02/17/26 Added timeout handling to polled wait loops. 3.16 tb 09/22/26 Align Doxygen comments with AMD documentation standards

 
#include "xparameters.h"
#include "xiic.h"
#include "xil_sutil.h"
#include "xil_exception.h"
#include "xil_printf.h"
#include "xinterrupt_wrap.h"

Macros

#define XIIC_BASEADDRESS   XPAR_XIIC_0_BASEADDR
 IIC base address. More...
 
#define SLAVE_ADDRESS   0x70
 7-bit slave address, 0xE0 as an 8-bit number. More...
 
#define RECEIVE_COUNT   25
 IIC receive byte count. More...
 
#define SEND_COUNT   25
 IIC transmit byte count. More...
 
#define MAX_DELAY_CNT   10000
 Maximum delay count for timeout handling. More...
 

Functions

int IicSlaveExample (void)
 Writes and reads the data as a slave. More...
 
int SlaveWriteData (u16 ByteCount)
 Writes a buffer of bytes when the IIC master initiates a read. More...
 
int SlaveReadData (u8 *BufferPtr, u16 ByteCount)
 Reads a buffer of bytes when the IIC master writes to the slave. More...
 
int main (void)
 Runs the IIC Slave example. More...
 

Variables

XIic IicInstance
 The instance of the IIC device. More...
 
u8 WriteBuffer [SEND_COUNT]
 Write buffer for writing a page. More...
 
u8 ReadBuffer [RECEIVE_COUNT]
 Read buffer for reading a page. More...
 
volatile u8 TransmitComplete
 Transmit done flag. More...
 
volatile u8 ReceiveComplete
 Receive done flag. More...
 
volatile u8 SlaveRead
 Slave read flag. More...
 
volatile u8 SlaveWrite
 Slave write flag. More...
 

Macro Definition Documentation

#define MAX_DELAY_CNT   10000

Maximum delay count for timeout handling.

Referenced by SlaveReadData(), and SlaveWriteData().

#define RECEIVE_COUNT   25

IIC receive byte count.

Referenced by IicSlaveExample(), and SlaveReadData().

#define SEND_COUNT   25

IIC transmit byte count.

Referenced by IicSlaveExample(), and SlaveWriteData().

#define SLAVE_ADDRESS   0x70

7-bit slave address, 0xE0 as an 8-bit number.

Referenced by IicSlaveExample().

#define XIIC_BASEADDRESS   XPAR_XIIC_0_BASEADDR

IIC base address.

Referenced by IicSlaveExample().

Function Documentation

int IicSlaveExample ( void  )

Writes and reads the data as a slave.

This function writes and reads the data as a slave. The IIC master on the bus initiates the transfers.

Returns
  • XST_SUCCESS if successful.
  • XST_FAILURE otherwise.

References XIic_Config::BaseAddress, XIic_Config::IntrId, XIic_Config::IntrParent, ReadBuffer, RECEIVE_COUNT, SEND_COUNT, SLAVE_ADDRESS, SlaveReadData(), SlaveWriteData(), WriteBuffer, XII_ADDR_TO_RESPOND_TYPE, XIIC_BASEADDRESS, XIic_CfgInitialize(), XIic_InterruptHandler(), XIic_LookupConfig(), XIic_SetAddress(), XIic_SetRecvHandler(), XIic_SetSendHandler(), XIic_SetStatusHandler(), and XIic_SlaveInclude().

Referenced by main().

int main ( void  )

Runs the IIC Slave example.

Main function to call the IIC Slave example.

Returns
  • XST_SUCCESS if successful.
  • XST_FAILURE otherwise.

References IicSlaveExample().

int SlaveReadData ( u8 *  BufferPtr,
u16  ByteCount 
)

Reads a buffer of bytes when the IIC master writes to the slave.

This function reads a buffer of bytes when the IIC Master on the bus writes data to the slave device.

Parameters
[out]BufferPtrThe address of the data buffer to be filled.
[in]ByteCountThe number of bytes in the buffer to be read.
Returns
  • XST_SUCCESS if successful.
  • XST_FAILURE otherwise.

References XIic::BaseAddress, MAX_DELAY_CNT, ReadBuffer, RECEIVE_COUNT, ReceiveComplete, SlaveRead, XIic_IntrGlobalDisable, XIic_IntrGlobalEnable, XIic_SlaveRecv(), XIic_Start(), and XIic_Stop().

Referenced by IicSlaveExample().

int SlaveWriteData ( u16  ByteCount)

Writes a buffer of bytes when the IIC master initiates a read.

This function writes a buffer of bytes to the IIC bus when the IIC master initiates a read operation.

Parameters
[in]ByteCountThe number of bytes in the buffer to be written.
Returns
  • XST_SUCCESS if successful.
  • XST_FAILURE otherwise.

References XIic::BaseAddress, MAX_DELAY_CNT, SEND_COUNT, SlaveWrite, TransmitComplete, WriteBuffer, XIic_IntrGlobalDisable, XIic_IntrGlobalEnable, XIic_SlaveSend(), XIic_Start(), and XIic_Stop().

Referenced by IicSlaveExample().

Variable Documentation

XIic IicInstance

The instance of the IIC device.

u8 ReadBuffer[RECEIVE_COUNT]

Read buffer for reading a page.

volatile u8 ReceiveComplete

Receive done flag.

volatile u8 SlaveRead

Slave read flag.

Referenced by SlaveReadData(), and TenBitAddrReadData().

volatile u8 SlaveWrite

Slave write flag.

Referenced by SlaveWriteData(), and TenBitAddrWriteData().

volatile u8 TransmitComplete

Transmit done flag.

u8 WriteBuffer[SEND_COUNT]

Write buffer for writing a page.