/** * This function writes a buffer of data to the IIC Device. * * @param I2cLibPtr contains a pointer to the instance of the IIC library * @param WrBuffer is the buffer which contains data to be written * @param ByteCount contains the number of bytes in the buffer to be * written. * @param SlaveAddr is the address of the slave we are sending to. * * @return XST_SUCCESS if successful else XST_FAILURE. * ******************************************************************************/ int I2cWriteData(XIIC_LIB *I2cLibPtr, u8 *WrBuffer, u16 ByteCount, u16 SlaveAddr) { XIIC *I2cInstancePtr; I2cInstancePtr = &I2cLibPtr->I2cInstance; I2cLibPtr->TransmitComplete = FALSE; /* * Send the Data. */ XIicPs_MasterSend(I2cInstancePtr, WrBuffer, ByteCount, SlaveAddr); /* * Wait for the entire buffer to be sent, letting the interrupt * processing work in the background, this function may get * locked up in this loop if the interrupts are not working * correctly. */ while (I2cLibPtr->TransmitComplete == FALSE) { if (0 != I2cLibPtr->TotalErrorCount) { xil_printf("I2cWriteData: Failed due to errors\n\r"); return XST_FAILURE; } } /* * Wait until bus is idle to start another transfer. */ while (XIicPs_BusIsBusy(I2cInstancePtr)); return XST_SUCCESS; }
/** * * This function does a minimal test on the Iic device and driver as a * design example. The purpose of this function is to illustrate * how to use the XIicPs driver. * * This function sends data and expects to receive the same data through the IIC * using the Aardvark test hardware. * * This function uses interrupt driver mode of the IIC. * * @param DeviceId is the Device ID of the IicPs Device and is the * XPAR_<IICPS_instance>_DEVICE_ID value from xparameters.h * * @return XST_SUCCESS if successful, otherwise XST_FAILURE. * * @note * * This function contains an infinite loop such that if interrupts are not * working it may never return. * *******************************************************************************/ int IicPsMasterIntrExample(u16 DeviceId) { int Status; XIicPs_Config *Config; int Index; int tmp; int BufferSizes[NUMBER_OF_SIZES] = {1, 2, 19, 31, 32, 33, 62, 63, 64, 65, 66, 94, 95, 96, 97, 98, 99, 250}; /* * Initialize the IIC driver so that it's ready to use * Look up the configuration in the config table, then initialize it. */ Config = XIicPs_LookupConfig(DeviceId); if (NULL == Config) { return XST_FAILURE; } Status = XIicPs_CfgInitialize(&Iic, Config, Config->BaseAddress); if (Status != XST_SUCCESS) { return XST_FAILURE; } /* * Perform a self-test to ensure that the hardware was built correctly. */ Status = XIicPs_SelfTest(&Iic); if (Status != XST_SUCCESS) { return XST_FAILURE; } /* * Connect the IIC to the interrupt subsystem such that interrupts can * occur. This function is application specific. */ Status = SetupInterruptSystem(&Iic); if (Status != XST_SUCCESS) { return XST_FAILURE; } /* * Setup the handlers for the IIC that will be called from the * interrupt context when data has been sent and received, specify a * pointer to the IIC driver instance as the callback reference so * the handlers are able to access the instance data. */ XIicPs_SetStatusHandler(&Iic, (void *) &Iic, Handler); /* * Set the IIC serial clock rate. */ XIicPs_SetSClk(&Iic, IIC_SCLK_RATE); /* * Initialize the send buffer bytes with a pattern to send and the * the receive buffer bytes to zero to allow the receive data to be * verified. */ for (Index = 0; Index < TEST_BUFFER_SIZE; Index++) { SendBuffer[Index] = (Index % TEST_BUFFER_SIZE); RecvBuffer[Index] = 0; } for(Index = 0; Index < NUMBER_OF_SIZES; Index++) { /* Wait for bus to become idle */ while (XIicPs_BusIsBusy(&Iic)) { /* NOP */ } SendComplete = FALSE; /* * Send the buffer, errors are reported by TotalErrorCount. */ XIicPs_MasterSend(&Iic, SendBuffer, BufferSizes[Index], IIC_SLAVE_ADDR); /* * Wait for the entire buffer to be sent, letting the interrupt * processing work in the background, this function may get * locked up in this loop if the interrupts are not working * correctly. */ while (!SendComplete) { if (0 != TotalErrorCount) { return XST_FAILURE; } } /* * Wait bus activities to finish. */ while (XIicPs_BusIsBusy(&Iic)) { /* NOP */ } /* * Receive data from slave, errors are reported through * TotalErrorCount. */ RecvComplete = FALSE; XIicPs_MasterRecv(&Iic, RecvBuffer, BufferSizes[Index], IIC_SLAVE_ADDR); while (!RecvComplete) { if (0 != TotalErrorCount) { return XST_FAILURE; } } /* Check for received data. */ for(tmp = 0; tmp < BufferSizes[Index]; tmp ++) { /* * Aardvark as slave can only set up to 64 bytes for * output. */ if (RecvBuffer[tmp] != tmp % 64) { return XST_FAILURE; } } } return XST_SUCCESS; }