Hardware/LPC/W83627.cs
author moel.mich
Sun, 07 Feb 2010 19:53:51 +0000
changeset 31 c4d1fb76a9e1
parent 30 420adf504011
permissions -rw-r--r--
Added initial support for W83627HF. Some refactoring for IHardware classes.
     1 /*
     2   
     3   Version: MPL 1.1/GPL 2.0/LGPL 2.1
     4 
     5   The contents of this file are subject to the Mozilla Public License Version
     6   1.1 (the "License"); you may not use this file except in compliance with
     7   the License. You may obtain a copy of the License at
     8  
     9   http://www.mozilla.org/MPL/
    10 
    11   Software distributed under the License is distributed on an "AS IS" basis,
    12   WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
    13   for the specific language governing rights and limitations under the License.
    14 
    15   The Original Code is the Open Hardware Monitor code.
    16 
    17   The Initial Developer of the Original Code is 
    18   Michael Möller <m.moeller@gmx.ch>.
    19   Portions created by the Initial Developer are Copyright (C) 2009-2010
    20   the Initial Developer. All Rights Reserved.
    21 
    22   Contributor(s):
    23 
    24   Alternatively, the contents of this file may be used under the terms of
    25   either the GNU General Public License Version 2 or later (the "GPL"), or
    26   the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
    27   in which case the provisions of the GPL or the LGPL are applicable instead
    28   of those above. If you wish to allow use of your version of this file only
    29   under the terms of either the GPL or the LGPL, and not to allow others to
    30   use your version of this file under the terms of the MPL, indicate your
    31   decision by deleting the provisions above and replace them with the notice
    32   and other provisions required by the GPL or the LGPL. If you do not delete
    33   the provisions above, a recipient may use your version of this file under
    34   the terms of any one of the MPL, the GPL or the LGPL.
    35  
    36 */
    37 
    38 using System;
    39 using System.Collections.Generic;
    40 using System.Drawing;
    41 using System.Text;
    42 
    43 namespace OpenHardwareMonitor.Hardware.LPC {
    44   public class W83627 : Winbond, IHardware {
    45    
    46     private Sensor[] temperatures;
    47     private Sensor[] fans;
    48     private Sensor[] voltages;
    49 
    50     private float[] voltageGains;
    51     private string[] fanNames;
    52 
    53     // Hardware Monitor Registers
    54     private const byte VOLTAGE_BASE_REG = 0x20;   
    55     private const byte TEMPERATURE_BASE_REG = 0x50;
    56     private const byte TEMPERATURE_SYS_REG = 0x27;
    57 
    58     private byte[] FAN_TACHO_REG = new byte[] { 0x28, 0x29, 0x2A, 0x3F, 0x53 };
    59     private byte[] FAN_TACHO_BANK = new byte[] { 0, 0, 0, 0, 5 };       
    60     private byte[] FAN_BIT_REG = new byte[] { 0x47, 0x4B, 0x4C, 0x59, 0x5D };
    61     private byte[] FAN_DIV_BIT0 = new byte[] { 36, 38, 30, 8, 10 };
    62     private byte[] FAN_DIV_BIT1 = new byte[] { 37, 39, 31, 9, 11 };
    63     private byte[] FAN_DIV_BIT2 = new byte[] { 5, 6, 7, 23, 15 };
    64 
    65     public W83627(Chip chip, byte revision, ushort address) 
    66       : base(chip, revision, address)
    67     {
    68      
    69       temperatures = new Sensor[3];
    70       temperatures[0] = new Sensor("CPU", 0, SensorType.Temperature, this);
    71       temperatures[1] = new Sensor("Auxiliary", 1, SensorType.Temperature, this);
    72       temperatures[2] = new Sensor("System", 2, SensorType.Temperature, this);
    73 
    74       switch (chip) {
    75         case Chip.W83627DHG:
    76         case Chip.W83627DHGP: 
    77           fanNames = new string[] { "System", "CPU #1", "Auxiliary #1", 
    78             "CPU #2", "Auxiliary #2" };
    79           voltageGains = new float[] { 0.008f, 1, 1, 0.016f, 1, 1, 1, 0.016f };
    80           voltages = new Sensor[3];
    81           voltages[0] = new Sensor("CPU VCore", 0, SensorType.Voltage, this);
    82           voltages[1] = new Sensor("+3.3V", 3, SensorType.Voltage, this);
    83           voltages[2] = new Sensor("Battery", 7, SensorType.Voltage, this);
    84           break;
    85         case Chip.W83627HF: 
    86           fanNames = new string[] { "Fan #1", "Fan #2", "Fan #3" };
    87           voltageGains = new float[] { 0.016f, 1, 0.016f, 1, 1, 1, 1, 0.016f };
    88           voltages = new Sensor[3];
    89           voltages[0] = new Sensor("CPU VCore", 0, SensorType.Voltage, this);
    90           voltages[1] = new Sensor("+3.3V", 2, SensorType.Voltage, this);
    91           voltages[2] = new Sensor("Battery", 7, SensorType.Voltage, this);
    92           break;
    93         default: fanNames = new string[0];
    94           break;
    95       }
    96       
    97       fans = new Sensor[fanNames.Length];
    98       for (int i = 0; i < fanNames.Length; i++)
    99         fans[i] = new Sensor(fanNames[i], i, SensorType.Fan, this);
   100     }    
   101 
   102     public void Update() {
   103       foreach (Sensor sensor in voltages) {
   104         if (sensor.Index < 7) {
   105           int value = ReadByte(0, (byte)(VOLTAGE_BASE_REG + sensor.Index));
   106           sensor.Value = voltageGains[sensor.Index] * value;
   107           if (sensor.Value > 0)
   108             ActivateSensor(sensor);
   109           else
   110             DeactivateSensor(sensor);
   111         } else {
   112           // Battery voltage
   113           bool valid = (ReadByte(0, 0x5D) & 0x01) > 0;
   114           if (valid) {
   115             sensor.Value = voltageGains[sensor.Index] * 
   116               ReadByte(5, 0x51);
   117             ActivateSensor(sensor);
   118           } else
   119             DeactivateSensor(sensor);
   120         }
   121       }
   122 
   123       foreach (Sensor sensor in temperatures) {
   124         int value;
   125         if (sensor.Index < 2) {
   126           value = (sbyte)ReadByte((byte)(sensor.Index + 1), TEMPERATURE_BASE_REG);
   127           value = (value << 1) | ReadByte((byte)(sensor.Index + 1),
   128             (byte)(TEMPERATURE_BASE_REG + 1)) >> 7;
   129         } else {
   130           value = (sbyte)ReadByte(0, TEMPERATURE_SYS_REG) << 1;
   131         }
   132         sensor.Value = value / 2.0f;
   133         if (value < 0xFE)
   134           ActivateSensor(sensor);
   135         else
   136           DeactivateSensor(sensor);
   137       }
   138 
   139       long bits = 0;
   140       for (int i = 0; i < FAN_BIT_REG.Length; i++)
   141         bits = (bits << 8) | ReadByte(0, FAN_BIT_REG[i]);
   142       foreach (Sensor sensor in fans) {
   143         int count = ReadByte(FAN_TACHO_BANK[sensor.Index], 
   144           FAN_TACHO_REG[sensor.Index]);
   145         int divisorBits = (int)(
   146           (((bits >> FAN_DIV_BIT2[sensor.Index]) & 1) << 2) |
   147           (((bits >> FAN_DIV_BIT1[sensor.Index]) & 1) << 1) |
   148            ((bits >> FAN_DIV_BIT0[sensor.Index]) & 1));
   149         int divisor = 1 << divisorBits;
   150         sensor.Value = (count < 0xff) ? 1.35e6f / (count * divisor) : 0;
   151         ActivateSensor(sensor);        
   152       }     
   153     }
   154   }
   155 }