Hardware/LPC/F71882.cs
author moel.mich
Fri, 29 Jan 2010 20:05:32 +0000
changeset 9 e5adb0fd4917
child 15 0e812fe67bac
permissions -rw-r--r--
Added additional checks to SMBIOS parsing. New class HexStringArray is for debugging with report data.
moel@9
     1
/*
moel@9
     2
  
moel@9
     3
  Version: MPL 1.1/GPL 2.0/LGPL 2.1
moel@9
     4
moel@9
     5
  The contents of this file are subject to the Mozilla Public License Version
moel@9
     6
  1.1 (the "License"); you may not use this file except in compliance with
moel@9
     7
  the License. You may obtain a copy of the License at
moel@9
     8
 
moel@9
     9
  http://www.mozilla.org/MPL/
moel@9
    10
moel@9
    11
  Software distributed under the License is distributed on an "AS IS" basis,
moel@9
    12
  WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
moel@9
    13
  for the specific language governing rights and limitations under the License.
moel@9
    14
moel@9
    15
  The Original Code is the Open Hardware Monitor code.
moel@9
    16
moel@9
    17
  The Initial Developer of the Original Code is 
moel@9
    18
  Michael Möller <m.moeller@gmx.ch>.
moel@9
    19
  Portions created by the Initial Developer are Copyright (C) 2009-2010
moel@9
    20
  the Initial Developer. All Rights Reserved.
moel@9
    21
moel@9
    22
  Contributor(s):
moel@9
    23
moel@9
    24
  Alternatively, the contents of this file may be used under the terms of
moel@9
    25
  either the GNU General Public License Version 2 or later (the "GPL"), or
moel@9
    26
  the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
moel@9
    27
  in which case the provisions of the GPL or the LGPL are applicable instead
moel@9
    28
  of those above. If you wish to allow use of your version of this file only
moel@9
    29
  under the terms of either the GPL or the LGPL, and not to allow others to
moel@9
    30
  use your version of this file under the terms of the MPL, indicate your
moel@9
    31
  decision by deleting the provisions above and replace them with the notice
moel@9
    32
  and other provisions required by the GPL or the LGPL. If you do not delete
moel@9
    33
  the provisions above, a recipient may use your version of this file under
moel@9
    34
  the terms of any one of the MPL, the GPL or the LGPL.
moel@9
    35
 
moel@9
    36
*/
moel@9
    37
moel@9
    38
using System;
moel@9
    39
using System.Collections.Generic;
moel@9
    40
using System.Drawing;
moel@9
    41
using System.Text;
moel@9
    42
moel@9
    43
namespace OpenHardwareMonitor.Hardware.LPC {
moel@9
    44
  public class F71882  : IHardware {
moel@9
    45
moel@9
    46
    private string name;
moel@9
    47
    private Image icon;
moel@9
    48
moel@9
    49
    private ushort address;
moel@9
    50
moel@9
    51
    private List<ISensor> active = new List<ISensor>();
moel@9
    52
moel@9
    53
    private Sensor[] temperatures;
moel@9
    54
    private Sensor[] fans;
moel@9
    55
    private Sensor[] voltages;
moel@9
    56
    private float[] voltageGains;
moel@9
    57
moel@9
    58
    // Hardware Monitor
moel@9
    59
    private const byte ADDRESS_REGISTER_OFFSET = 0x05;
moel@9
    60
    private const byte DATA_REGISTER_OFFSET = 0x06;
moel@9
    61
moel@9
    62
    // Hardware Monitor Registers
moel@9
    63
    private const byte VOLTAGE_BASE_REG = 0x20;
moel@9
    64
    private const byte TEMPERATURE_BASE_REG = 0x72;
moel@9
    65
    private byte[] FAN_TACHOMETER_REG = new byte[] { 0xA0, 0xB0, 0xC0, 0xD0 };
moel@9
    66
    
moel@9
    67
    private byte ReadByte(byte register) {
moel@9
    68
      WinRing0.WriteIoPortByte(
moel@9
    69
        (ushort)(address + ADDRESS_REGISTER_OFFSET), register);
moel@9
    70
      return WinRing0.ReadIoPortByte((ushort)(address + DATA_REGISTER_OFFSET));
moel@9
    71
    }
moel@9
    72
moel@9
    73
    public F71882(ushort address) {
moel@9
    74
      this.address = address;
moel@9
    75
moel@9
    76
      this.name = "Fintek F71882";
moel@9
    77
moel@9
    78
      temperatures = new Sensor[3];
moel@9
    79
      for (int i = 0; i < temperatures.Length; i++)
moel@9
    80
        temperatures[i] = new Sensor("Temperature #" + (i + 1), i,
moel@9
    81
          SensorType.Temperature, this);
moel@9
    82
moel@9
    83
      fans = new Sensor[4];
moel@9
    84
      for (int i = 0; i < fans.Length; i++)
moel@9
    85
        fans[i] = new Sensor("Fan #" + (i + 1), i, SensorType.Fan, this);
moel@9
    86
moel@9
    87
      voltageGains = new float[] { 1, 0.5f, 1, 1, 1, 1, 1, 1, 1 };
moel@9
    88
      voltages = new Sensor[4];
moel@9
    89
      voltages[0] = new Sensor("VCC3V", 0, SensorType.Voltage, this);
moel@9
    90
      voltages[1] = new Sensor("CPU VCore", 1, SensorType.Voltage, this);      
moel@9
    91
      voltages[2] = new Sensor("VSB3V", 7, SensorType.Voltage, this);
moel@9
    92
      voltages[3] = new Sensor("Battery", 8, SensorType.Voltage, this);
moel@9
    93
moel@9
    94
      this.icon = Utilities.EmbeddedResources.GetImage("chip.png");
moel@9
    95
    }
moel@9
    96
moel@9
    97
    public string Name {
moel@9
    98
      get { return name; }
moel@9
    99
    }
moel@9
   100
moel@9
   101
    public string Identifier {
moel@9
   102
      get { return "/lpc/f71882fg"; }
moel@9
   103
    }
moel@9
   104
moel@9
   105
    public Image Icon {
moel@9
   106
      get { return icon; }
moel@9
   107
    }
moel@9
   108
moel@9
   109
    public ISensor[] Sensors {
moel@9
   110
      get { return active.ToArray(); }
moel@9
   111
    }
moel@9
   112
moel@9
   113
    public string GetReport() {
moel@9
   114
      StringBuilder r = new StringBuilder();
moel@9
   115
moel@9
   116
      r.AppendLine("LPC F71882FG");
moel@9
   117
      r.AppendLine();
moel@9
   118
      r.Append("Base Adress: 0x"); r.AppendLine(address.ToString("X4"));
moel@9
   119
      r.AppendLine();
moel@9
   120
      r.AppendLine("Hardware Monitor Registers");
moel@9
   121
      r.AppendLine();
moel@9
   122
moel@9
   123
      r.AppendLine("      00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F");
moel@9
   124
      r.AppendLine();
moel@9
   125
      for (int i = 0; i <= 0xF; i++) {
moel@9
   126
        r.Append(" "); r.Append((i << 4).ToString("X2")); r.Append("  ");
moel@9
   127
        for (int j = 0; j <= 0xF; j++) {
moel@9
   128
          r.Append(" ");
moel@9
   129
          r.Append(ReadByte((byte)((i << 4) | j)).ToString("X2"));
moel@9
   130
        }
moel@9
   131
        r.AppendLine();
moel@9
   132
      }
moel@9
   133
      r.AppendLine();
moel@9
   134
      return r.ToString();
moel@9
   135
    }
moel@9
   136
moel@9
   137
    public void Update() {
moel@9
   138
moel@9
   139
      foreach (Sensor sensor in voltages) {
moel@9
   140
        int value = ReadByte((byte)(VOLTAGE_BASE_REG + sensor.Index));
moel@9
   141
        sensor.Value = voltageGains[sensor.Index] * 0.001f * (value << 4);
moel@9
   142
        if (sensor.Value > 0)
moel@9
   143
          ActivateSensor(sensor);
moel@9
   144
        else
moel@9
   145
          DeactivateSensor(sensor);
moel@9
   146
      }
moel@9
   147
moel@9
   148
      foreach (Sensor sensor in temperatures) {
moel@9
   149
        int value = ReadByte((byte)(TEMPERATURE_BASE_REG + 2 * sensor.Index));
moel@9
   150
        sensor.Value = value;
moel@9
   151
        if (value < 254)
moel@9
   152
          ActivateSensor(sensor);
moel@9
   153
        else
moel@9
   154
          DeactivateSensor(sensor);
moel@9
   155
      }
moel@9
   156
moel@9
   157
      foreach (Sensor sensor in fans) {
moel@9
   158
        int value = ReadByte(FAN_TACHOMETER_REG[sensor.Index]) << 8;
moel@9
   159
        value |= ReadByte((byte)(FAN_TACHOMETER_REG[sensor.Index] + 1));
moel@9
   160
moel@9
   161
        if (value > 0) {
moel@9
   162
          sensor.Value = (value < 0x0fff) ? 1.5e6f / value : 0;
moel@9
   163
          ActivateSensor(sensor);
moel@9
   164
        } else {
moel@9
   165
          DeactivateSensor(sensor);
moel@9
   166
        }
moel@9
   167
      }      
moel@9
   168
    }
moel@9
   169
moel@9
   170
    private void ActivateSensor(Sensor sensor) {
moel@9
   171
      if (!active.Contains(sensor)) {
moel@9
   172
        active.Add(sensor);
moel@9
   173
        SensorAdded(sensor);
moel@9
   174
      }
moel@9
   175
    }
moel@9
   176
moel@9
   177
    private void DeactivateSensor(Sensor sensor) {
moel@9
   178
      if (active.Contains(sensor)) {
moel@9
   179
        active.Remove(sensor);
moel@9
   180
        SensorRemoved(sensor);
moel@9
   181
      }
moel@9
   182
    }
moel@9
   183
moel@9
   184
    public event SensorEventHandler SensorAdded;
moel@9
   185
    public event SensorEventHandler SensorRemoved;
moel@9
   186
moel@9
   187
  }
moel@9
   188
}