Search This Blog

Thursday, March 17, 2011

How to Access Byte Data on SMBus (Simple)

#define SMBusStatus         0x00
#define SMBusSlaveStatus 0x01
#define SMBusControl       0x02
#define SMBusHostCmd    0x03
#define SMBusAddress      0x04
#define SMBusData0          0x05
#define DeviceAddress       0xA0  // It depends on H/W design.
#define RWDataByte          0x48
#define SMBusInterrupt      1 << 1
#define BIT0                      1

UINT8
SMBusReadByte (
  IN UINT8 RegisterIndex
)
{
  UINT8 Data8;
 
  do {
     IoWrite8 (SMBusBaseAddress + SMBusStatus, 0xFF);
     IoRead8 (SMBusBaseAddress + SMBusStatus, Data8);
  } while (Data8 != 0);                                                                  // Waiting for all status be cleared

  IoWrite8 (SMBusBaseAddress + SMBusAddress, (DeviceAddress << 1) | BIT0);  //Read
  IoWrite8 (SMBusBaseAddress + SMBusHostCmd, RegisterIndex);
  IoWrite8 (SMBusBaseAddress + SMBusControl, RWDataByte);
  do {
     IoRead8 (SMBusBaseAddress + SMBusStatus, Data8);
  } while (Data8 != SMBusInterrupt);                                                     // Check SMBus read ready
  IoRead8 (SMBusBaseAddress + SMBusData0, Data8);
  Return Data8;
}

VOID
SMBusWriteByte (
  IN UINT8 RegisterIndex,
  IN UINT8 WriteData
)
{
  UINT8 Data8;
 
  do {
     IoWrite8 (SMBusBaseAddress + SMBusStatus, 0xFF);
     IoRead8 (SMBusBaseAddress + SMBusStatus, Data8);
  } while (Data8 != 0);                                                                     // Waiting for all status be cleared

  IoWrite8 (SMBusBaseAddress + SMBusAddress, (DeviceAddress << 1));  // Write
  IoWrite8 (SMBusBaseAddress + SMBusData0, WriteData);
  IoWrite8 (SMBusBaseAddress + SMBusHostCmd, RegisterIndex);
  IoWrite8 (SMBusBaseAddress + SMBusControl, RWDataByte);
  do {
     IoRead8 (SMBusBaseAddress + SMBusStatus, Data8);
  } while (Data8 != SMBusInterrupt);                                            // Check SMBus write ready
}

No comments:

Post a Comment