Hardware/LPC/F718XX.cs
author moel.mich
Sun, 08 Aug 2010 13:57:26 +0000
changeset 165 813d8bc3192f
parent 163 67be1c62f950
child 166 fa9dfbfc4145
permissions -rw-r--r--
Refactored the hardware monitoring code into a library (Issue 101).
     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.Text;
    41 
    42 namespace OpenHardwareMonitor.Hardware.LPC {
    43   internal class F718XX : ISuperIO {
    44 
    45     private ushort address;
    46     private Chip chip;
    47 
    48     private float?[] voltages;
    49     private float?[] temperatures;
    50     private float?[] fans;
    51 
    52     // Hardware Monitor
    53     private const byte ADDRESS_REGISTER_OFFSET = 0x05;
    54     private const byte DATA_REGISTER_OFFSET = 0x06;
    55 
    56     // Hardware Monitor Registers
    57     private const byte VOLTAGE_BASE_REG = 0x20;
    58     private const byte TEMPERATURE_CONFIG_REG = 0x69;
    59     private const byte TEMPERATURE_BASE_REG = 0x70;
    60     private byte[] FAN_TACHOMETER_REG = new byte[] { 0xA0, 0xB0, 0xC0, 0xD0 };
    61     
    62     private byte ReadByte(byte register) {
    63       WinRing0.WriteIoPortByte(
    64         (ushort)(address + ADDRESS_REGISTER_OFFSET), register);
    65       return WinRing0.ReadIoPortByte((ushort)(address + DATA_REGISTER_OFFSET));
    66     }
    67 
    68     public F718XX(Chip chip, ushort address) {
    69       this.address = address;
    70       this.chip = chip;
    71 
    72       voltages = new float?[chip == Chip.F71858 ? 3 : 9];
    73       temperatures = new float?[3];
    74       fans = new float?[chip == Chip.F71882 || chip == Chip.F71858? 4 : 3];
    75     }
    76 
    77     public Chip Chip { get { return chip; } }
    78     public float?[] Voltages { get { return voltages; } }
    79     public float?[] Temperatures { get { return temperatures; } }
    80     public float?[] Fans { get { return fans; } }
    81 
    82     public string GetReport() {
    83       StringBuilder r = new StringBuilder();
    84 
    85       r.AppendLine("LPC " + this.GetType().Name);
    86       r.AppendLine();
    87       r.Append("Base Adress: 0x"); r.AppendLine(address.ToString("X4"));
    88       r.AppendLine();
    89 
    90       if (!WinRing0.WaitIsaBusMutex(100))
    91         return r.ToString();
    92 
    93       r.AppendLine("Hardware Monitor Registers");
    94       r.AppendLine();
    95       r.AppendLine("      00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F");
    96       r.AppendLine();
    97       for (int i = 0; i <= 0xF; i++) {
    98         r.Append(" "); r.Append((i << 4).ToString("X2")); r.Append("  ");
    99         for (int j = 0; j <= 0xF; j++) {
   100           r.Append(" ");
   101           r.Append(ReadByte((byte)((i << 4) | j)).ToString("X2"));
   102         }
   103         r.AppendLine();
   104       }
   105       r.AppendLine();
   106 
   107       WinRing0.ReleaseIsaBusMutex();
   108 
   109       return r.ToString();
   110     }
   111 
   112     public void Update() {
   113       if (!WinRing0.WaitIsaBusMutex(10))
   114         return;
   115 
   116       for (int i = 0; i < voltages.Length; i++) {
   117         int value = ReadByte((byte)(VOLTAGE_BASE_REG + i));
   118         voltages[i] = 0.008f * value;
   119       }
   120      
   121       for (int i = 0; i < temperatures.Length; i++) {
   122         switch (chip) {
   123           case Chip.F71858: {
   124               int tableMode = 0x3 & ReadByte((byte)(TEMPERATURE_CONFIG_REG));
   125               int high = 
   126                 ReadByte((byte)(TEMPERATURE_BASE_REG + 2 * i));
   127               int low =
   128                 ReadByte((byte)(TEMPERATURE_BASE_REG + 2 * i + 1));              
   129               if (high != 0xbb && high != 0xcc) {
   130                 int bits = 0;
   131                 switch (tableMode) {
   132                   case 0: bits = 0; break;
   133                   case 1: bits = 0; break;
   134                   case 2: bits = (high & 0x80) << 8; break;
   135                   case 3: bits = (low & 0x01) << 15; break;
   136                 }
   137                 bits |= high << 7;
   138                 bits |= (low & 0xe0) >> 1;
   139                 short value = (short)(bits & 0xfff0);
   140                 temperatures[i] = value / 128.0f;
   141               } else {
   142                 temperatures[i] = null;
   143               }
   144           } break;
   145           default: {
   146             sbyte value = (sbyte)ReadByte((byte)(
   147               TEMPERATURE_BASE_REG + 2 * (i + 1)));            
   148             if (value < sbyte.MaxValue && value > 0)
   149               temperatures[i] = value;
   150             else
   151               temperatures[i] = null;
   152           } break;
   153         }
   154       }
   155 
   156       for (int i = 0; i < fans.Length; i++) {
   157         int value = ReadByte(FAN_TACHOMETER_REG[i]) << 8;
   158         value |= ReadByte((byte)(FAN_TACHOMETER_REG[i] + 1));
   159 
   160         if (value > 0) 
   161           fans[i] = (value < 0x0fff) ? 1.5e6f / value : 0;
   162         else 
   163           fans[i] = null;        
   164       }
   165 
   166       WinRing0.ReleaseIsaBusMutex();
   167     }
   168   }
   169 }