/* * This function reads the slave device's register * * Parameters: * deviceAddress - i2c Slave device address which register * to be read from * registerIndex - Slave device's register to be read * * Return Value: * Register value */ unsigned char hwI2CReadReg( unsigned char deviceAddress, unsigned char registerIndex ) { unsigned char value = (0xFF); if (hwI2CWriteData(deviceAddress, 1, ®isterIndex) == 1) hwI2CReadData(deviceAddress, 1, &value); return value; }
/* * This function writes a value to the i2c device register. * * Input: deviceAddress - I2C Device Address * registerIndex - Register index to be read * * Output: * The value of the register being read. */ long hwI2CWriteRegMultiple( unsigned char deviceAddress, unsigned char registerIndex, unsigned long length, unsigned char *pBuffer ) { unsigned char value[MAX_HWI2C_FIFO], index, count, start = 1; unsigned long totalData = 0; /* The length is limited to 256 since register index is limited to unsigned char. */ if ((registerIndex + length) > 256) length = 256 - registerIndex; #if 1 /* Temporary use this one */ for (index = 0; index < (unsigned char)length; index++) { if (hwI2CWriteReg(deviceAddress, registerIndex + index, *pBuffer++) != 0) return (-1); } #else /* Does not work here. Need to find out how to write multiple data at once. */ while (length > 0) { if (length < MAX_HWI2C_FIFO) count = length + 1; /* Add one for the register Index. */ else count = MAX_HWI2C_FIFO; /* Write the starting register index */ value[0] = registerIndex; /* Write the data */ for (index = 1; index < count; index++) value[index] = *pBuffer++; if (hwI2CWriteData(deviceAddress, count, &value[0]) != count) return (-1); /* Update the length */ length -= (count - 1); /* Update the registerIndex */ registerIndex += (count - 1); } #endif return 0; }
/* * This function writes a value to the slave device's register * * Parameters: * deviceAddress - i2c Slave device address which register * to be written * registerIndex - Slave device's register to be written * data - Data to be written to the register * * Result: * 0 - Success * -1 - Fail */ int hwI2CWriteReg( unsigned char deviceAddress, unsigned char registerIndex, unsigned char data ) { unsigned char value[2]; value[0] = registerIndex; value[1] = data; if (hwI2CWriteData(deviceAddress, 2, value) == 2) return 0; return (-1); }
/* * This function read the i2c device register value * * Input: deviceAddress - I2C Device Address * registerIndex - Register index to be read * * Output: * The value of the register being read. */ unsigned long hwI2CReadRegMultiple( unsigned char deviceAddress, unsigned char registerIndex, unsigned long length, unsigned char *pBuffer ) { unsigned long totalData = 0; /* The length is limited to 256 since register index is limited to unsigned char. */ if ((registerIndex + length) > 256) length = 256 - registerIndex; /* Read from the slave device */ if (hwI2CWriteData(deviceAddress, 1, ®isterIndex) == 1) totalData = hwI2CReadData(deviceAddress, length, pBuffer); /* Return the total number of read data */ return totalData; }