Hardware/LPC/F718XX.cs
author moel.mich
Tue, 17 Jul 2012 16:12:07 +0000
changeset 365 a8a8ff22d959
parent 344 3145aadca3d2
permissions -rw-r--r--
Improved the data compression for storing the recorded sensor values in the config file.
     1 /*
     2  
     3   This Source Code Form is subject to the terms of the Mozilla Public
     4   License, v. 2.0. If a copy of the MPL was not distributed with this
     5   file, You can obtain one at http://mozilla.org/MPL/2.0/.
     6  
     7   Copyright (C) 2009-2011 Michael Möller <mmoeller@openhardwaremonitor.org>
     8 	
     9 */
    10 
    11 using System.Globalization;
    12 using System.Text;
    13 
    14 namespace OpenHardwareMonitor.Hardware.LPC {
    15   internal class F718XX : ISuperIO {
    16 
    17     private readonly ushort address;
    18     private readonly Chip chip;
    19 
    20     private readonly float?[] voltages;
    21     private readonly float?[] temperatures;
    22     private readonly float?[] fans;
    23     private readonly float?[] controls;
    24 
    25     // Hardware Monitor
    26     private const byte ADDRESS_REGISTER_OFFSET = 0x05;
    27     private const byte DATA_REGISTER_OFFSET = 0x06;
    28 
    29     // Hardware Monitor Registers
    30     private const byte VOLTAGE_BASE_REG = 0x20;
    31     private const byte TEMPERATURE_CONFIG_REG = 0x69;
    32     private const byte TEMPERATURE_BASE_REG = 0x70;
    33     private readonly byte[] FAN_TACHOMETER_REG = 
    34       new byte[] { 0xA0, 0xB0, 0xC0, 0xD0 };
    35     
    36     private byte ReadByte(byte register) {
    37       Ring0.WriteIoPort(
    38         (ushort)(address + ADDRESS_REGISTER_OFFSET), register);
    39       return Ring0.ReadIoPort((ushort)(address + DATA_REGISTER_OFFSET));
    40     }
    41 
    42     public byte? ReadGPIO(int index) {
    43       return null;
    44     }
    45 
    46     public void WriteGPIO(int index, byte value) { }
    47 
    48     public void SetControl(int index, byte? value) { }   
    49 
    50     public F718XX(Chip chip, ushort address) {
    51       this.address = address;
    52       this.chip = chip;
    53 
    54       voltages = new float?[chip == Chip.F71858 ? 3 : 9];
    55       temperatures = new float?[chip == Chip.F71808E ? 2 : 3];
    56       fans = new float?[chip == Chip.F71882 || chip == Chip.F71858 ? 4 : 3];
    57       controls = new float?[0];
    58     }
    59 
    60     public Chip Chip { get { return chip; } }
    61     public float?[] Voltages { get { return voltages; } }
    62     public float?[] Temperatures { get { return temperatures; } }
    63     public float?[] Fans { get { return fans; } }
    64     public float?[] Controls { get { return controls; } }
    65 
    66     public string GetReport() {
    67       StringBuilder r = new StringBuilder();
    68 
    69       r.AppendLine("LPC " + this.GetType().Name);
    70       r.AppendLine();
    71       r.Append("Base Adress: 0x"); 
    72       r.AppendLine(address.ToString("X4", CultureInfo.InvariantCulture));
    73       r.AppendLine();
    74 
    75       if (!Ring0.WaitIsaBusMutex(100))
    76         return r.ToString();
    77 
    78       r.AppendLine("Hardware Monitor Registers");
    79       r.AppendLine();
    80       r.AppendLine("      00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F");
    81       r.AppendLine();
    82       for (int i = 0; i <= 0xF; i++) {
    83         r.Append(" "); 
    84         r.Append((i << 4).ToString("X2", CultureInfo.InvariantCulture)); 
    85         r.Append("  ");
    86         for (int j = 0; j <= 0xF; j++) {
    87           r.Append(" ");
    88           r.Append(ReadByte((byte)((i << 4) | j)).ToString("X2", 
    89             CultureInfo.InvariantCulture));
    90         }
    91         r.AppendLine();
    92       }
    93       r.AppendLine();
    94 
    95       Ring0.ReleaseIsaBusMutex();
    96 
    97       return r.ToString();
    98     }
    99 
   100     public void Update() {
   101       if (!Ring0.WaitIsaBusMutex(10))
   102         return;
   103 
   104       for (int i = 0; i < voltages.Length; i++) {
   105         if (chip == Chip.F71808E && i == 6) {
   106           // 0x26 is reserved on F71808E
   107           voltages[i] = 0;
   108         } else {
   109           int value = ReadByte((byte)(VOLTAGE_BASE_REG + i));
   110           voltages[i] = 0.008f * value;
   111         }
   112       }
   113      
   114       for (int i = 0; i < temperatures.Length; i++) {
   115         switch (chip) {
   116           case Chip.F71858: {
   117               int tableMode = 0x3 & ReadByte(TEMPERATURE_CONFIG_REG);
   118               int high = 
   119                 ReadByte((byte)(TEMPERATURE_BASE_REG + 2 * i));
   120               int low =
   121                 ReadByte((byte)(TEMPERATURE_BASE_REG + 2 * i + 1));              
   122               if (high != 0xbb && high != 0xcc) {
   123                 int bits = 0;
   124                 switch (tableMode) {
   125                   case 0: bits = 0; break;
   126                   case 1: bits = 0; break;
   127                   case 2: bits = (high & 0x80) << 8; break;
   128                   case 3: bits = (low & 0x01) << 15; break;
   129                 }
   130                 bits |= high << 7;
   131                 bits |= (low & 0xe0) >> 1;
   132                 short value = (short)(bits & 0xfff0);
   133                 temperatures[i] = value / 128.0f;
   134               } else {
   135                 temperatures[i] = null;
   136               }
   137           } break;
   138           default: {
   139             sbyte value = (sbyte)ReadByte((byte)(
   140               TEMPERATURE_BASE_REG + 2 * (i + 1)));            
   141             if (value < sbyte.MaxValue && value > 0)
   142               temperatures[i] = value;
   143             else
   144               temperatures[i] = null;
   145           } break;
   146         }
   147       }
   148 
   149       for (int i = 0; i < fans.Length; i++) {
   150         int value = ReadByte(FAN_TACHOMETER_REG[i]) << 8;
   151         value |= ReadByte((byte)(FAN_TACHOMETER_REG[i] + 1));
   152 
   153         if (value > 0) 
   154           fans[i] = (value < 0x0fff) ? 1.5e6f / value : 0;
   155         else 
   156           fans[i] = null;        
   157       }
   158 
   159       Ring0.ReleaseIsaBusMutex();
   160     }
   161   }
   162 }