1 //
2 // Copyright (c) 2010-2018 Antmicro
3 //
4 // This file is licensed under the MIT License.
5 // Full license text is available in 'licenses/MIT.txt'.
6 //
7 using Antmicro.Renode.Exceptions;
8 using Antmicro.Renode.Peripherals.Sensor;
9 using Antmicro.Renode.Peripherals.SPI;
10 using Antmicro.Renode.Utilities;
11 
12 namespace Antmicro.Renode.Peripherals.Sensors
13 {
14     public class TI_LM74 : ISPIPeripheral, ITemperatureSensor
15     {
TI_LM74()16         public TI_LM74()
17         {
18             Reset();
19         }
20 
FinishTransmission()21         public void FinishTransmission()
22         {
23             Reset();
24         }
25 
Reset()26         public void Reset()
27         {
28             isFirstByte = true;
29             currentReadOut = 0;
30         }
31 
Transmit(byte data)32         public byte Transmit(byte data)
33         {
34             byte value = 0;
35             if(isFirstByte)
36             {
37                 //The 3 LSB are set to 1. 0x1000 = 0.0625C. Decimal->Int->UInt conversion to handle negative values.
38                 currentReadOut = (((uint)(int)(Temperature * 10000 / 625) << 3) | 0x7);
39                 value = (byte)(currentReadOut >> 8);
40             }
41             else
42             {
43                 value = (byte)(currentReadOut & 0xFF);
44             }
45             isFirstByte = !isFirstByte;
46             return value;
47         }
48 
49         public decimal Temperature
50         {
51             get
52             {
53                 return temperature;
54             }
55             set
56             {
57                 if(MinTemperature > value || value > MaxTemperature)
58                 {
59                     throw new RecoverableException("The temperature value must be between {0} and {1}.".FormatWith(MinTemperature, MaxTemperature));
60                 }
61                 temperature = value;
62             }
63         }
64 
65         private decimal temperature;
66         private uint currentReadOut;
67         private bool isFirstByte;
68 
69         private const decimal MaxTemperature = 150;
70         private const decimal MinTemperature = -55;
71     }
72 }
73