/* * 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 */ long swI2CWriteReg( unsigned char deviceAddress, unsigned char registerIndex, unsigned char data ) { long returnValue = 0; /* Send the Start signal */ swI2CStart(); /* Send the device address and read the data. All should return success in order for the writing processed to be successful */ if ((swI2CWriteByte(deviceAddress) != 0) || (swI2CWriteByte(registerIndex) != 0) || (swI2CWriteByte(data) != 0)) { returnValue = -1; } /* Stop i2c and release the bus */ swI2CStop(); return returnValue; }
/* * 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 swI2CReadReg( unsigned char deviceAddress, unsigned char registerIndex ) { unsigned char data; /* Send the Start signal */ swI2CStart(); /* Send the device address */ swI2CWriteByte(deviceAddress); /* Send the register index */ swI2CWriteByte(registerIndex); /* Get the bus again and get the data from the device read address */ swI2CStart(); swI2CWriteByte(deviceAddress + 1); data = swI2CReadByte(1); /* Stop swI2C and release the bus */ swI2CStop(); return data; }
/* * This function initializes GPIO port for SW I2C communication. * * Parameters: * i2cClkGPIO - The GPIO pin to be used as i2c SCL * i2cDataGPIO - The GPIO pin to be used as i2c SDA * * Return Value: * -1 - Fail to initialize the i2c * 0 - Success */ static long swI2CInit_SM750LE(unsigned char i2cClkGPIO, unsigned char i2cDataGPIO) { int i; /* Initialize the GPIO pin for the i2c Clock Register */ g_i2cClkGPIODataReg = GPIO_DATA_SM750LE; g_i2cClkGPIODataDirReg = GPIO_DATA_DIRECTION_SM750LE; /* Initialize the Clock GPIO Offset */ g_i2cClockGPIO = i2cClkGPIO; /* Initialize the GPIO pin for the i2c Data Register */ g_i2cDataGPIODataReg = GPIO_DATA_SM750LE; g_i2cDataGPIODataDirReg = GPIO_DATA_DIRECTION_SM750LE; /* Initialize the Data GPIO Offset */ g_i2cDataGPIO = i2cDataGPIO; /* Note that SM750LE don't have GPIO MUX and power is always on */ /* Clear the i2c lines. */ for(i=0; i<9; i++) swI2CStop(); return 0; }
/******************************************************************************* swI2CInit This function initializes the i2c attributes and bus Parameters: i2cClkGPIO - The GPIO pin to be used as i2c SCL i2cDataGPIO - The GPIO pin to be used as i2c SDA Return Value: -1 - Fail to initialize the i2c 0 - Success *******************************************************************************/ int32_t swI2CInit(unsigned char i2cClkGPIO, unsigned char i2cDataGPIO,swI2C_wait_proc function) { int i; uint32_t value; uint32_t gate; /* Return 0 if the GPIO pins to be used is out of range. The range is only from [0..63] */ if ((i2cClkGPIO > 31) || (i2cDataGPIO > 31)) return (-1); /* initialize the function pointer */ pfn_swI2C_wait = function; if(pfn_swI2C_wait == NULL) pfn_swI2C_wait = swI2CWait; /* Initialize the GPIO pin for the i2c Clock Register */ g_i2cClkGPIOMuxReg = GPIO_MUX; g_i2cClkGPIODataReg = GPIO_DATA; g_i2cClkGPIODataDirReg = GPIO_DATA_DIRECTION; /* Initialize the Clock GPIO Offset */ g_i2cClockGPIO = i2cClkGPIO; /* Initialize the GPIO pin for the i2c Data Register */ g_i2cDataGPIOMuxReg = GPIO_MUX; g_i2cDataGPIODataReg = GPIO_DATA; g_i2cDataGPIODataDirReg = GPIO_DATA_DIRECTION; /* Initialize the Data GPIO Offset */ g_i2cDataGPIO = i2cDataGPIO; /* Enable the GPIO pins for the i2c Clock and Data (GPIO MUX) */ pokeRegisterDWord(g_i2cClkGPIOMuxReg, peekRegisterDWord(g_i2cClkGPIOMuxReg) & ~(1 << g_i2cClockGPIO)); pokeRegisterDWord(g_i2cDataGPIOMuxReg, peekRegisterDWord(g_i2cDataGPIOMuxReg) & ~(1 << g_i2cDataGPIO)); /* Enable GPIO power */ enableGPIO(1); /* Clear the i2c lines. */ for(i=0; i<9; i++) swI2CStop(); return 0; }
/* * This function initializes the i2c attributes and bus * * Parameters: * i2cClkGPIO - The GPIO pin to be used as i2c SCL * i2cDataGPIO - The GPIO pin to be used as i2c SDA * * Return Value: * -1 - Fail to initialize the i2c * 0 - Success */ long swI2CInit( unsigned char i2cClkGPIO, unsigned char i2cDataGPIO ) { int i; /* Return 0 if the GPIO pins to be used is out of range. The range is only from [0..63] */ if ((i2cClkGPIO > 31) || (i2cDataGPIO > 31)) return -1; if (getChipType() == SM750LE) return swI2CInit_SM750LE(i2cClkGPIO, i2cDataGPIO); /* Initialize the GPIO pin for the i2c Clock Register */ g_i2cClkGPIOMuxReg = GPIO_MUX; g_i2cClkGPIODataReg = GPIO_DATA; g_i2cClkGPIODataDirReg = GPIO_DATA_DIRECTION; /* Initialize the Clock GPIO Offset */ g_i2cClockGPIO = i2cClkGPIO; /* Initialize the GPIO pin for the i2c Data Register */ g_i2cDataGPIOMuxReg = GPIO_MUX; g_i2cDataGPIODataReg = GPIO_DATA; g_i2cDataGPIODataDirReg = GPIO_DATA_DIRECTION; /* Initialize the Data GPIO Offset */ g_i2cDataGPIO = i2cDataGPIO; /* Enable the GPIO pins for the i2c Clock and Data (GPIO MUX) */ POKE32(g_i2cClkGPIOMuxReg, PEEK32(g_i2cClkGPIOMuxReg) & ~(1 << g_i2cClockGPIO)); POKE32(g_i2cDataGPIOMuxReg, PEEK32(g_i2cDataGPIOMuxReg) & ~(1 << g_i2cDataGPIO)); /* Enable GPIO power */ enableGPIO(1); /* Clear the i2c lines. */ for(i=0; i<9; i++) swI2CStop(); return 0; }