/* * This function reads one byte from the slave device * * Parameters: * ack - Flag to indicate either to send the acknowledge * message to the slave device or not * * Return Value: * One byte data read from the Slave device */ static unsigned char swI2CReadByte(unsigned char ack) { int i; unsigned char data = 0; for(i=7; i>=0; i--) { /* Set the SCL to Low and SDA to High (Input) */ swI2CSCL(0); swI2CSDA(1); swI2CWait(); /* Set the SCL High */ swI2CSCL(1); swI2CWait(); /* Read data bits from SDA */ data |= (swI2CReadSDA() << i); } if (ack) swI2CAck(); /* Set the SCL Low and SDA High */ swI2CSCL(0); swI2CSDA(1); return data; }
/******************************************************************************* swI2CWriteByte This function writes one byte to the slave device Parameters: data - Data to be write to the slave device Return Value: 0 - Fail to write byte 1 - Success *******************************************************************************/ unsigned char swI2CWriteByte(unsigned char data) { unsigned char value = data; int i; /* Sending the data bit by bit */ for (i=0; i<8; i++) { /* Set SCL to low */ swI2CSCL(0); /* Send data bit */ if ((value & 0x80) != 0) swI2CSDA(1); else swI2CSDA(0); swI2CWait(); /* Toggle clk line to one */ swI2CSCL(1); /* Shift byte to be sent */ value = value << 1; } /* Set the SCL Low and SDA High (prepare to get input) */ swI2CSCL(0); swI2CSDA(1); /* Set the SCL High for ack */ swI2CWait(); swI2CSCL(1); /* Read SDA, until SDA==0 */ for(i=0; i<0xff; i++) { swI2CWait(); swI2CWait(); if (!swI2CReadSDA()) break; } /* Set the SCL Low and SDA High */ swI2CSCL(0); swI2CSDA(1); return (i<0xff); }
/******************************************************************************* swI2CSDA This function set/reset the SDA GPIO pin Parameters: value - Bit value to set to the SCL or SDA (0 = low, 1 = high) Return Value: None *******************************************************************************/ static void swI2CSDA(unsigned char value) { uint32_t ulGPIOData; uint32_t ulGPIODirection; ulGPIODirection = peekRegisterDWord(g_i2cDataGPIODataDirReg); if (value) /* High */ { /* Set direction as input. This will automatically pull the signal up. */ ulGPIODirection &= ~(1 << g_i2cDataGPIO); pokeRegisterDWord(g_i2cDataGPIODataDirReg, ulGPIODirection); } else /* Low */ { /* Set the signal down */ ulGPIOData = peekRegisterDWord(g_i2cDataGPIODataReg); ulGPIOData &= ~(1 << g_i2cDataGPIO); pokeRegisterDWord(g_i2cDataGPIODataReg, ulGPIOData); /* Set direction as output */ ulGPIODirection |= (1 << g_i2cDataGPIO); pokeRegisterDWord(g_i2cDataGPIODataDirReg, ulGPIODirection); } swI2CWait(); }
/******************************************************************************* swI2CReadSDA This function read the data from the SDA GPIO pin Parameters: None Return Value: The SDA data bit sent by the Slave *******************************************************************************/ static unsigned char swI2CReadSDA() { unsigned char data; uint32_t ulGPIODirection; uint32_t ulGPIOData; /* Make sure that the direction is input (High) */ ulGPIODirection = peekRegisterDWord(g_i2cDataGPIODataDirReg); if ((ulGPIODirection & (1 << g_i2cDataGPIO)) != (~(1 << g_i2cDataGPIO))) { ulGPIODirection &= ~(1 << g_i2cDataGPIO); pokeRegisterDWord(g_i2cDataGPIODataDirReg, ulGPIODirection); } /* Now read the SDA line */ ulGPIOData = peekRegisterDWord(g_i2cDataGPIODataReg); if (ulGPIOData & (1 << g_i2cDataGPIO)) return 1; else return 0; swI2CWait(); }