changing circuitry to disable RTC, update initialization to match
[fw/openalt] / i2c / lm75.c
1 #include "FreeRTOS.h"
2
3 #include "i2c.h"
4 #include "lm75.h"
5
6 //
7 //
8 //
9 static U8 lm75Address = LM75_ADDRESS;
10 static U8 lm75LastRegister = LM75_REGISTER_TEMPERATURE;
11 static U8 lm75Mode = 0;
12
13 //
14 //
15 //
16 static int lm75Read8 (U8 reg, int *value)
17 {
18   lm75LastRegister = reg;
19   *value = 0;
20
21   if (i2cWriteBuffer (lm75Address, (U8 *) &reg, sizeof (U8)))
22     return -1;
23
24   return i2cReadBuffer (lm75Address, (U8 *) value, sizeof (U8));
25 }
26
27 static int lm75Write8 (U8 reg, int value)
28 {
29   U8 buffer [2];
30
31   buffer [0] = lm75LastRegister = reg;
32   buffer [1] = value;
33
34   return i2cWriteBuffer (lm75Address, buffer, sizeof (buffer));
35 }
36
37 //
38 //  DANGER, WILL ROBINSON!  'value' is the desired temperature * 2, where
39 //  the low bit indicates 0.5C or not.  So for -50C degrees, we see -100.
40 //  For -50.5C, we see -101.
41 //
42 static int lm75Read16 (U8 reg, int *value)
43 {
44   U8 buffer [2];
45
46   buffer [0] = lm75LastRegister = reg;
47
48   if (!lm75Mode)
49   {
50     if (i2cWriteReadBuffer (lm75Address, buffer, sizeof (U8), sizeof (buffer)))
51       return -1;
52   }
53   else
54   {
55     if (i2cWriteBuffer (lm75Address, (U8 *) &reg, sizeof (U8)))
56       return -1;
57
58     if (i2cReadBuffer (lm75Address, buffer, sizeof (buffer)))
59       return -1;
60   }
61
62   //
63   //  Sign extend negative numbers
64   //
65   *value = ((buffer [0] << 8) | buffer [1]) >> 7;
66
67   if (buffer [0] & 0x80)
68     *value |= 0xfffffe00;
69
70   return I2CERR_NONE;
71 }
72
73 static int lm75Write16 (U8 reg, int value)
74 {
75   U8 buffer [3];
76
77   value <<= 7;
78
79   buffer [0] = lm75LastRegister = reg;
80   buffer [1] = value >> 8;
81   buffer [2] = value;
82
83   return i2cWriteBuffer (lm75Address, buffer, sizeof (buffer));
84 }
85
86 //
87 //  Don't call lm75Init() before tasker is started.  The I2C routines use the
88 //  newlib/syscalls.c _times function, which calls xTaskGetTickCount(), which
89 //  hasn't been initialized yet.
90 //
91 int lm75Init (void)
92 {
93   return lm75ConfigWrite (0);
94 }
95
96 void lm75SetMode (int mode)
97 {
98   lm75Mode = mode;
99 }
100
101 void lm75SetAddress (U8 address)
102 {
103   lm75Address = address;
104 }
105
106 int lm75ReRead (int *value)
107 {
108   *value = 0;
109
110   if (lm75LastRegister == LM75_REGISTER_CONFIGURATION)
111     return i2cReadBuffer (lm75Address, (U8 *) value, sizeof (U8));
112   else
113   {
114     U8 buffer [2];
115
116     if (i2cReadBuffer (lm75Address, buffer, sizeof (buffer)))
117       return -1;
118
119     *value = ((buffer [0] << 8) | buffer [1]) >> 7;
120   }
121
122   return I2CERR_NONE;
123 }
124
125 int lm75TemperatureRead (int *temp)
126 {
127   return lm75Read16 (LM75_REGISTER_TEMPERATURE, temp);
128 }
129
130 int lm75ConfigRead (int *configValue)
131 {
132   return lm75Read8 (LM75_REGISTER_CONFIGURATION, configValue);
133 }
134
135 int lm75ConfigWrite (int configValue)
136 {
137   return lm75Write8 (LM75_REGISTER_CONFIGURATION, configValue);
138 }
139
140 int lm75THYSTRead (int *thystValue)
141 {
142   return lm75Read16 (LM75_REGISTER_THYST, thystValue);
143 }
144
145 int lm75THYSTWrite (int thystValue)
146 {
147   return lm75Write16 (LM75_REGISTER_THYST, thystValue);
148 }
149
150 int lm75TOSTRead (int *thystValue)
151 {
152   return lm75Read16 (LM75_REGISTER_TOS, thystValue);
153 }
154
155 int lm75TOSWrite (int thystValue)
156 {
157   return lm75Write16 (LM75_REGISTER_TOS, thystValue);
158 }