Hardware/LPC/IT87.cs
author moel.mich
Wed, 27 Jan 2010 18:12:51 +0000
changeset 2 105939e4eb7e
child 6 56c9d6c8c08b
permissions -rw-r--r--
Fixed a NullReferenceException in the HDD SMART code.
moel@1
     1
/*
moel@1
     2
  
moel@1
     3
  Version: MPL 1.1/GPL 2.0/LGPL 2.1
moel@1
     4
moel@1
     5
  The contents of this file are subject to the Mozilla Public License Version
moel@1
     6
  1.1 (the "License"); you may not use this file except in compliance with
moel@1
     7
  the License. You may obtain a copy of the License at
moel@1
     8
 
moel@1
     9
  http://www.mozilla.org/MPL/
moel@1
    10
moel@1
    11
  Software distributed under the License is distributed on an "AS IS" basis,
moel@1
    12
  WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
moel@1
    13
  for the specific language governing rights and limitations under the License.
moel@1
    14
moel@1
    15
  The Original Code is the Open Hardware Monitor code.
moel@1
    16
moel@1
    17
  The Initial Developer of the Original Code is 
moel@1
    18
  Michael Möller <m.moeller@gmx.ch>.
moel@1
    19
  Portions created by the Initial Developer are Copyright (C) 2009-2010
moel@1
    20
  the Initial Developer. All Rights Reserved.
moel@1
    21
moel@1
    22
  Contributor(s):
moel@1
    23
moel@1
    24
  Alternatively, the contents of this file may be used under the terms of
moel@1
    25
  either the GNU General Public License Version 2 or later (the "GPL"), or
moel@1
    26
  the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
moel@1
    27
  in which case the provisions of the GPL or the LGPL are applicable instead
moel@1
    28
  of those above. If you wish to allow use of your version of this file only
moel@1
    29
  under the terms of either the GPL or the LGPL, and not to allow others to
moel@1
    30
  use your version of this file under the terms of the MPL, indicate your
moel@1
    31
  decision by deleting the provisions above and replace them with the notice
moel@1
    32
  and other provisions required by the GPL or the LGPL. If you do not delete
moel@1
    33
  the provisions above, a recipient may use your version of this file under
moel@1
    34
  the terms of any one of the MPL, the GPL or the LGPL.
moel@1
    35
 
moel@1
    36
*/
moel@1
    37
moel@1
    38
using System;
moel@1
    39
using System.Collections.Generic;
moel@1
    40
using System.Drawing;
moel@1
    41
using System.Text;
moel@1
    42
moel@1
    43
namespace OpenHardwareMonitor.Hardware.LPC {
moel@1
    44
  public class IT87 : IHardware {
moel@1
    45
moel@1
    46
    private string name;
moel@1
    47
    private Image icon;
moel@1
    48
        
moel@1
    49
    private bool available = false;
moel@1
    50
    private Chip chip;
moel@1
    51
    private ushort address;
moel@1
    52
moel@1
    53
    private Sensor[] temperatures;
moel@1
    54
    private Sensor[] fans;
moel@1
    55
    private Sensor[] voltages;
moel@1
    56
    private List<ISensor> active = new List<ISensor>();
moel@1
    57
    private float[] voltageGains;
moel@1
    58
moel@1
    59
   
moel@1
    60
    // Consts
moel@1
    61
    private const byte ITE_VENDOR_ID = 0x90;
moel@1
    62
       
moel@1
    63
    // Environment Controller
moel@1
    64
    private const byte ADDRESS_REGISTER_OFFSET = 0x05;
moel@1
    65
    private const byte DATA_REGISTER_OFFSET = 0x06;
moel@1
    66
moel@1
    67
    // Environment Controller Registers    
moel@1
    68
    private const byte CONFIGURATION_REGISTER = 0x00;
moel@1
    69
    private const byte TEMPERATURE_BASE_REG = 0x29;
moel@1
    70
    private const byte VENDOR_ID_REGISTER = 0x58;
moel@1
    71
    private const byte FAN_TACHOMETER_16_BIT_ENABLE_REGISTER = 0x0c;
moel@1
    72
    private byte[] FAN_TACHOMETER_REG = 
moel@1
    73
      new byte[] { 0x0d, 0x0e, 0x0f, 0x80, 0x82 };
moel@1
    74
    private byte[] FAN_TACHOMETER_EXT_REG =
moel@1
    75
      new byte[] { 0x18, 0x19, 0x1a, 0x81, 0x83 };
moel@1
    76
    private const byte VOLTAGE_BASE_REG = 0x20;  
moel@1
    77
    
moel@1
    78
moel@1
    79
    private byte ReadByte(byte register) {
moel@1
    80
      WinRing0.WriteIoPortByte(
moel@1
    81
        (ushort)(address + ADDRESS_REGISTER_OFFSET), register);
moel@1
    82
      return WinRing0.ReadIoPortByte((ushort)(address + DATA_REGISTER_OFFSET));
moel@1
    83
    }
moel@1
    84
moel@1
    85
    public IT87(Chip chip, ushort address) {
moel@1
    86
      
moel@1
    87
      this.chip = chip;
moel@1
    88
      this.address = address;
moel@1
    89
moel@1
    90
      switch (chip) {
moel@1
    91
        case Chip.IT8716F: name = "ITE IT8716F"; break;
moel@1
    92
        case Chip.IT8718F: name = "ITE IT8718F"; break;
moel@1
    93
        case Chip.IT8720F: name = "ITE IT8720F"; break;
moel@1
    94
        case Chip.IT8726F: name = "ITE IT8726F"; break;
moel@1
    95
        default: return;
moel@1
    96
      }
moel@1
    97
      
moel@1
    98
      // Check vendor id
moel@1
    99
      byte vendorId = ReadByte(VENDOR_ID_REGISTER);      
moel@1
   100
      if (vendorId != ITE_VENDOR_ID)
moel@1
   101
        return;
moel@1
   102
moel@1
   103
      // Bit 0x10 of the configuration register should always be 1
moel@1
   104
      if ((ReadByte(CONFIGURATION_REGISTER) & 0x10) == 0)
moel@1
   105
        return;
moel@1
   106
moel@1
   107
      temperatures = new Sensor[3];
moel@1
   108
      for (int i = 0; i < temperatures.Length; i++) 
moel@1
   109
        temperatures[i] = new Sensor("Temperature #" + (i + 1), i,
moel@1
   110
          SensorType.Temperature, this);
moel@1
   111
moel@1
   112
      fans = new Sensor[4];
moel@1
   113
      for (int i = 0; i < fans.Length; i++)
moel@1
   114
        fans[i] = new Sensor("Fan #" + (i + 1), i, SensorType.Fan, this);
moel@1
   115
moel@1
   116
      voltageGains = new float[] { 
moel@1
   117
        1, 1, 1, (6.8f / 10 + 1), 1, 1, 1, 1, 1 };
moel@1
   118
moel@1
   119
      voltages = new Sensor[2];
moel@1
   120
      voltages[0] = new Sensor("CPU VCore", 0, SensorType.Voltage, this);
moel@1
   121
      voltages[1] = new Sensor("Battery", 8, SensorType.Voltage, this);           
moel@1
   122
moel@1
   123
      available = true;
moel@1
   124
      icon = Utilities.EmbeddedResources.GetImage("chip.png");
moel@1
   125
    }
moel@1
   126
moel@1
   127
    public bool IsAvailable {
moel@1
   128
      get { return available; } 
moel@1
   129
    }
moel@1
   130
moel@1
   131
    public string Name {
moel@1
   132
      get { return name; }
moel@1
   133
    }
moel@1
   134
moel@1
   135
    public string Identifier {
moel@1
   136
      get { return "/lpc/it87"; }
moel@1
   137
    }
moel@1
   138
moel@1
   139
    public Image Icon {
moel@1
   140
      get { return icon; }
moel@1
   141
    }
moel@1
   142
moel@1
   143
    public ISensor[] Sensors {
moel@1
   144
      get { return active.ToArray(); }
moel@1
   145
    }
moel@1
   146
moel@1
   147
    public string GetReport() {
moel@1
   148
      StringBuilder r = new StringBuilder();
moel@1
   149
moel@1
   150
      r.AppendLine("LPC IT87");
moel@1
   151
      r.AppendLine();
moel@1
   152
      r.Append("Chip ID: 0x"); r.AppendLine(chip.ToString("X"));
moel@1
   153
      r.Append("Chip Name: "); r.AppendLine(name);
moel@1
   154
      r.Append("Base Address: 0x"); r.AppendLine(address.ToString("X"));
moel@1
   155
      r.AppendLine();
moel@1
   156
      r.AppendLine("Environment Controller Registers");
moel@1
   157
      r.AppendLine();
moel@1
   158
moel@1
   159
      r.AppendLine("      00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F");
moel@1
   160
      r.AppendLine();
moel@1
   161
      for (int i = 0; i <= 0xA; i++) {
moel@1
   162
        r.Append(" "); r.Append((i << 4).ToString("X2")); r.Append("  ");
moel@1
   163
        for (int j = 0; j <= 0xF; j++) {
moel@1
   164
          r.Append(" ");
moel@1
   165
          r.Append(ReadByte((byte)((i << 4) | j)).ToString("X2"));
moel@1
   166
        }
moel@1
   167
        r.AppendLine();
moel@1
   168
      }
moel@1
   169
      r.AppendLine();
moel@1
   170
moel@1
   171
      return r.ToString();
moel@1
   172
    }
moel@1
   173
moel@1
   174
    public void Update() {
moel@1
   175
moel@1
   176
      foreach (Sensor sensor in voltages) {
moel@1
   177
        int value = ReadByte((byte)(VOLTAGE_BASE_REG + sensor.Index));
moel@1
   178
        sensor.Value = voltageGains[sensor.Index] * 0.001f * (value << 4);
moel@1
   179
        if (sensor.Value > 0)
moel@1
   180
          ActivateSensor(sensor);
moel@1
   181
        else
moel@1
   182
          DeactivateSensor(sensor);
moel@1
   183
      }
moel@1
   184
moel@1
   185
      foreach (Sensor sensor in temperatures) {
moel@1
   186
        int value = ReadByte((byte)(TEMPERATURE_BASE_REG + sensor.Index));
moel@1
   187
        sensor.Value = value;
moel@1
   188
        if (value < 254)
moel@1
   189
          ActivateSensor(sensor);
moel@1
   190
        else
moel@1
   191
          DeactivateSensor(sensor);
moel@1
   192
      }
moel@1
   193
moel@1
   194
      foreach (Sensor sensor in fans) {
moel@1
   195
        int value = ReadByte(FAN_TACHOMETER_REG[sensor.Index]);
moel@1
   196
        value |= ReadByte(FAN_TACHOMETER_EXT_REG[sensor.Index]) << 8;
moel@1
   197
moel@1
   198
        if (value > 0) {
moel@1
   199
          sensor.Value = (value < 0xffff) ? 1.35e6f / ((value) * 2) : 0;
moel@1
   200
          ActivateSensor(sensor);
moel@1
   201
        } else {
moel@1
   202
          DeactivateSensor(sensor);
moel@1
   203
        }
moel@1
   204
      }      
moel@1
   205
    }
moel@1
   206
moel@1
   207
    private void ActivateSensor(Sensor sensor) {
moel@1
   208
      if (!active.Contains(sensor)) {
moel@1
   209
        active.Add(sensor);
moel@1
   210
        SensorAdded(sensor);
moel@1
   211
      }
moel@1
   212
    }
moel@1
   213
moel@1
   214
    private void DeactivateSensor(Sensor sensor) {
moel@1
   215
      if (active.Contains(sensor)) {
moel@1
   216
        active.Remove(sensor);
moel@1
   217
        SensorRemoved(sensor);
moel@1
   218
      }
moel@1
   219
    }
moel@1
   220
moel@1
   221
    public event SensorEventHandler SensorAdded;
moel@1
   222
    public event SensorEventHandler SensorRemoved;
moel@1
   223
moel@1
   224
  }
moel@1
   225
}