Hardware/LPC/IT87.cs
changeset 1 361e324a0ed4
child 6 56c9d6c8c08b
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/Hardware/LPC/IT87.cs	Tue Jan 26 22:37:48 2010 +0000
     1.3 @@ -0,0 +1,225 @@
     1.4 +/*
     1.5 +  
     1.6 +  Version: MPL 1.1/GPL 2.0/LGPL 2.1
     1.7 +
     1.8 +  The contents of this file are subject to the Mozilla Public License Version
     1.9 +  1.1 (the "License"); you may not use this file except in compliance with
    1.10 +  the License. You may obtain a copy of the License at
    1.11 + 
    1.12 +  http://www.mozilla.org/MPL/
    1.13 +
    1.14 +  Software distributed under the License is distributed on an "AS IS" basis,
    1.15 +  WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
    1.16 +  for the specific language governing rights and limitations under the License.
    1.17 +
    1.18 +  The Original Code is the Open Hardware Monitor code.
    1.19 +
    1.20 +  The Initial Developer of the Original Code is 
    1.21 +  Michael Möller <m.moeller@gmx.ch>.
    1.22 +  Portions created by the Initial Developer are Copyright (C) 2009-2010
    1.23 +  the Initial Developer. All Rights Reserved.
    1.24 +
    1.25 +  Contributor(s):
    1.26 +
    1.27 +  Alternatively, the contents of this file may be used under the terms of
    1.28 +  either the GNU General Public License Version 2 or later (the "GPL"), or
    1.29 +  the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
    1.30 +  in which case the provisions of the GPL or the LGPL are applicable instead
    1.31 +  of those above. If you wish to allow use of your version of this file only
    1.32 +  under the terms of either the GPL or the LGPL, and not to allow others to
    1.33 +  use your version of this file under the terms of the MPL, indicate your
    1.34 +  decision by deleting the provisions above and replace them with the notice
    1.35 +  and other provisions required by the GPL or the LGPL. If you do not delete
    1.36 +  the provisions above, a recipient may use your version of this file under
    1.37 +  the terms of any one of the MPL, the GPL or the LGPL.
    1.38 + 
    1.39 +*/
    1.40 +
    1.41 +using System;
    1.42 +using System.Collections.Generic;
    1.43 +using System.Drawing;
    1.44 +using System.Text;
    1.45 +
    1.46 +namespace OpenHardwareMonitor.Hardware.LPC {
    1.47 +  public class IT87 : IHardware {
    1.48 +
    1.49 +    private string name;
    1.50 +    private Image icon;
    1.51 +        
    1.52 +    private bool available = false;
    1.53 +    private Chip chip;
    1.54 +    private ushort address;
    1.55 +
    1.56 +    private Sensor[] temperatures;
    1.57 +    private Sensor[] fans;
    1.58 +    private Sensor[] voltages;
    1.59 +    private List<ISensor> active = new List<ISensor>();
    1.60 +    private float[] voltageGains;
    1.61 +
    1.62 +   
    1.63 +    // Consts
    1.64 +    private const byte ITE_VENDOR_ID = 0x90;
    1.65 +       
    1.66 +    // Environment Controller
    1.67 +    private const byte ADDRESS_REGISTER_OFFSET = 0x05;
    1.68 +    private const byte DATA_REGISTER_OFFSET = 0x06;
    1.69 +
    1.70 +    // Environment Controller Registers    
    1.71 +    private const byte CONFIGURATION_REGISTER = 0x00;
    1.72 +    private const byte TEMPERATURE_BASE_REG = 0x29;
    1.73 +    private const byte VENDOR_ID_REGISTER = 0x58;
    1.74 +    private const byte FAN_TACHOMETER_16_BIT_ENABLE_REGISTER = 0x0c;
    1.75 +    private byte[] FAN_TACHOMETER_REG = 
    1.76 +      new byte[] { 0x0d, 0x0e, 0x0f, 0x80, 0x82 };
    1.77 +    private byte[] FAN_TACHOMETER_EXT_REG =
    1.78 +      new byte[] { 0x18, 0x19, 0x1a, 0x81, 0x83 };
    1.79 +    private const byte VOLTAGE_BASE_REG = 0x20;  
    1.80 +    
    1.81 +
    1.82 +    private byte ReadByte(byte register) {
    1.83 +      WinRing0.WriteIoPortByte(
    1.84 +        (ushort)(address + ADDRESS_REGISTER_OFFSET), register);
    1.85 +      return WinRing0.ReadIoPortByte((ushort)(address + DATA_REGISTER_OFFSET));
    1.86 +    }
    1.87 +
    1.88 +    public IT87(Chip chip, ushort address) {
    1.89 +      
    1.90 +      this.chip = chip;
    1.91 +      this.address = address;
    1.92 +
    1.93 +      switch (chip) {
    1.94 +        case Chip.IT8716F: name = "ITE IT8716F"; break;
    1.95 +        case Chip.IT8718F: name = "ITE IT8718F"; break;
    1.96 +        case Chip.IT8720F: name = "ITE IT8720F"; break;
    1.97 +        case Chip.IT8726F: name = "ITE IT8726F"; break;
    1.98 +        default: return;
    1.99 +      }
   1.100 +      
   1.101 +      // Check vendor id
   1.102 +      byte vendorId = ReadByte(VENDOR_ID_REGISTER);      
   1.103 +      if (vendorId != ITE_VENDOR_ID)
   1.104 +        return;
   1.105 +
   1.106 +      // Bit 0x10 of the configuration register should always be 1
   1.107 +      if ((ReadByte(CONFIGURATION_REGISTER) & 0x10) == 0)
   1.108 +        return;
   1.109 +
   1.110 +      temperatures = new Sensor[3];
   1.111 +      for (int i = 0; i < temperatures.Length; i++) 
   1.112 +        temperatures[i] = new Sensor("Temperature #" + (i + 1), i,
   1.113 +          SensorType.Temperature, this);
   1.114 +
   1.115 +      fans = new Sensor[4];
   1.116 +      for (int i = 0; i < fans.Length; i++)
   1.117 +        fans[i] = new Sensor("Fan #" + (i + 1), i, SensorType.Fan, this);
   1.118 +
   1.119 +      voltageGains = new float[] { 
   1.120 +        1, 1, 1, (6.8f / 10 + 1), 1, 1, 1, 1, 1 };
   1.121 +
   1.122 +      voltages = new Sensor[2];
   1.123 +      voltages[0] = new Sensor("CPU VCore", 0, SensorType.Voltage, this);
   1.124 +      voltages[1] = new Sensor("Battery", 8, SensorType.Voltage, this);           
   1.125 +
   1.126 +      available = true;
   1.127 +      icon = Utilities.EmbeddedResources.GetImage("chip.png");
   1.128 +    }
   1.129 +
   1.130 +    public bool IsAvailable {
   1.131 +      get { return available; } 
   1.132 +    }
   1.133 +
   1.134 +    public string Name {
   1.135 +      get { return name; }
   1.136 +    }
   1.137 +
   1.138 +    public string Identifier {
   1.139 +      get { return "/lpc/it87"; }
   1.140 +    }
   1.141 +
   1.142 +    public Image Icon {
   1.143 +      get { return icon; }
   1.144 +    }
   1.145 +
   1.146 +    public ISensor[] Sensors {
   1.147 +      get { return active.ToArray(); }
   1.148 +    }
   1.149 +
   1.150 +    public string GetReport() {
   1.151 +      StringBuilder r = new StringBuilder();
   1.152 +
   1.153 +      r.AppendLine("LPC IT87");
   1.154 +      r.AppendLine();
   1.155 +      r.Append("Chip ID: 0x"); r.AppendLine(chip.ToString("X"));
   1.156 +      r.Append("Chip Name: "); r.AppendLine(name);
   1.157 +      r.Append("Base Address: 0x"); r.AppendLine(address.ToString("X"));
   1.158 +      r.AppendLine();
   1.159 +      r.AppendLine("Environment Controller Registers");
   1.160 +      r.AppendLine();
   1.161 +
   1.162 +      r.AppendLine("      00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F");
   1.163 +      r.AppendLine();
   1.164 +      for (int i = 0; i <= 0xA; i++) {
   1.165 +        r.Append(" "); r.Append((i << 4).ToString("X2")); r.Append("  ");
   1.166 +        for (int j = 0; j <= 0xF; j++) {
   1.167 +          r.Append(" ");
   1.168 +          r.Append(ReadByte((byte)((i << 4) | j)).ToString("X2"));
   1.169 +        }
   1.170 +        r.AppendLine();
   1.171 +      }
   1.172 +      r.AppendLine();
   1.173 +
   1.174 +      return r.ToString();
   1.175 +    }
   1.176 +
   1.177 +    public void Update() {
   1.178 +
   1.179 +      foreach (Sensor sensor in voltages) {
   1.180 +        int value = ReadByte((byte)(VOLTAGE_BASE_REG + sensor.Index));
   1.181 +        sensor.Value = voltageGains[sensor.Index] * 0.001f * (value << 4);
   1.182 +        if (sensor.Value > 0)
   1.183 +          ActivateSensor(sensor);
   1.184 +        else
   1.185 +          DeactivateSensor(sensor);
   1.186 +      }
   1.187 +
   1.188 +      foreach (Sensor sensor in temperatures) {
   1.189 +        int value = ReadByte((byte)(TEMPERATURE_BASE_REG + sensor.Index));
   1.190 +        sensor.Value = value;
   1.191 +        if (value < 254)
   1.192 +          ActivateSensor(sensor);
   1.193 +        else
   1.194 +          DeactivateSensor(sensor);
   1.195 +      }
   1.196 +
   1.197 +      foreach (Sensor sensor in fans) {
   1.198 +        int value = ReadByte(FAN_TACHOMETER_REG[sensor.Index]);
   1.199 +        value |= ReadByte(FAN_TACHOMETER_EXT_REG[sensor.Index]) << 8;
   1.200 +
   1.201 +        if (value > 0) {
   1.202 +          sensor.Value = (value < 0xffff) ? 1.35e6f / ((value) * 2) : 0;
   1.203 +          ActivateSensor(sensor);
   1.204 +        } else {
   1.205 +          DeactivateSensor(sensor);
   1.206 +        }
   1.207 +      }      
   1.208 +    }
   1.209 +
   1.210 +    private void ActivateSensor(Sensor sensor) {
   1.211 +      if (!active.Contains(sensor)) {
   1.212 +        active.Add(sensor);
   1.213 +        SensorAdded(sensor);
   1.214 +      }
   1.215 +    }
   1.216 +
   1.217 +    private void DeactivateSensor(Sensor sensor) {
   1.218 +      if (active.Contains(sensor)) {
   1.219 +        active.Remove(sensor);
   1.220 +        SensorRemoved(sensor);
   1.221 +      }
   1.222 +    }
   1.223 +
   1.224 +    public event SensorEventHandler SensorAdded;
   1.225 +    public event SensorEventHandler SensorRemoved;
   1.226 +
   1.227 +  }
   1.228 +}