| #include <device.h> 
 #define SLAVE_ADDR 0x4
 #define DATAI2C_byteCount 0x6
 
 uint8 writeSwitch_flag ,readSwitch_flag;
 
 
 void main()
 {
 uint8 index,position,I2C_byteCount;
 uint8 I2C_masterBuffer[10];
 uint8 I2C_slaveBuffer[10];
 
 /* Message to be written*/
 uint8 message[] = {0x11,0x22,0x33,0x44,0x55,0x66};
 
 
 
 /* Enable I2C master and slave interrupts*/
 I2C_S_EnableInt();
 I2C_M_EnableInt();
 
 /* Initialize I2C slave read and write buffers as I2C_slaveBuffer */
 I2C_S_SlaveInitReadBuf(I2C_slaveBuffer,DATAI2C_byteCount);
 I2C_S_SlaveInitWriteBuf(I2C_slaveBuffer,DATAI2C_byteCount);
 
 /* Start all components */
 LCD_Start();
 isr_WriteSwitch_Start();
 isr_ReadSwitch_Start();
 I2C_S_Start();
 I2C_M_Start();
 
 /* Enable global interrupt */
 CYGlobalIntEnable;
 
 for(;;)
 {
 /* When Write switch is pressed write data to slave */
 if (writeSwitch_flag)
 {
 /* Clear the writeSwitch_flag flag :writeSwitch_flag is set to 1 by Isr_WriteSlave */
 writeSwitch_flag=0;
 
 /* Clear any previous status */
 I2C_M_MasterClearStatus();
 
 /* Write data to the slave*/
 I2C_M_MasterWriteBuf(SLAVE_ADDR,message,DATAI2C_byteCount,I2C_M_MODE_COMPLETE_XFER);
 
 /* Wait till write operation is complete*/
 while (!(I2C_M_MasterStatus() & I2C_M_MSTAT_WR_CMPLT))
 {
 ;
 }
 
 }/* If statement ends here */
 
 /* When Read switch is pressed read back the data written into the slave */
 if (readSwitch_flag)
 {
 /* Clear the writeSwitch_flag flag:writeSwitch_flag is set to 2 by Isr_ReadSlave */
 readSwitch_flag=0;
 
 /* Clear any previous status */
 I2C_M_MasterClearStatus();
 
 /* Get the size of data previously written by master */
 I2C_byteCount = I2C_M_MasterGetWriteBufSize();
 
 /* Read back the data */
 I2C_M_MasterReadBuf(SLAVE_ADDR,I2C_masterBuffer,I2C_byteCount,I2C_M_MODE_COMPLETE_XFER);
 
 /* Wait till read operation is complete */
 while (!(I2C_M_MasterStatus()&I2C_M_MSTAT_RD_CMPLT))
 {
 ;
 }
 
 /* Get the no. of bytes read by the master */
 I2C_byteCount = I2C_M_MasterGetReadBufSize();
 
 /* Display the read back data */
 for(index=0,position=0;index<I2C_byteCount;index++,position+=2)
 {
 LCD_Position(0,position);
 LCD_PrintInt8(I2C_masterBuffer[index]);
 }/* for loop ends here */
 
 }/* if statement ends here */
 
 /* Once write to slave is complete display the data and re-intialize the write buffer */
 if (I2C_S_SlaveStatus()& I2C_S_SSTAT_WR_CMPT)
 {
 /* Get the no. of bytes in the slave write buffer (written by master)*/
 I2C_byteCount = I2C_S_SlaveGetWriteBufSize();
 
 /* Re-initialize the slave write buffer index to starting of the buffer(0) */
 I2C_S_SlaveClearWriteBuf();
 
 /*clear the status*/
 I2C_S_SlaveClearWriteStatus();
 
 /* Display the data written into the slave */
 for(index=0,position=0;index<I2C_byteCount;index++,position+=2)
 {
 LCD_Position(1,position);
 LCD_PrintInt8(I2C_slaveBuffer[index]);
 }/* for loop ends here */
 
 }/* If statement ends here */
 
 /* If the master has completed the read transaction, re-initialize the read buffer*/
 if(I2C_S_SlaveStatus()& I2C_S_SSTAT_RD_CMPT)
 {
 /* Re-initialize the read buffer index to  to starting of the buffer(0) */
 I2C_S_SlaveClearReadBuf();
 
 /* Clear the status */
 I2C_S_SlaveClearReadStatus();
 
 }/* If statement ends here */
 }/* for loop ends here */
 
 }
 
 
 /* [] END OF FILE */
 
 |