-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathMAX31790FanControl.cpp
234 lines (205 loc) · 5.45 KB
/
MAX31790FanControl.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
#include "MAX31790FanControl.h"
#include "Wire.h"
MAX31790::MAX31790(const uint8_t i2c_address, const uint8_t fans)
: FanControl(fans)
{
_deviceAddress = i2c_address;
}
/**
* Initialise as i2c master with
* default fanconfig
*/
RESULT MAX31790::initialise()
{
Wire.begin(); // join bus as master
// configure all fans
for (int i = 1; i <= getFanCount(); i++)
{
// 7:0 - PWM,
// 6:5:00 - no spinup
// 4:0- control fan speed;
// 3:1 - TACH input enable
// 2:0 - tach count
// 1:0- locked rotor
// 0:0- PWM control speed
writeByte(FAN_CONFIG(i), 0x08);
}
// mask all fans incl those that are not connected
writeByte(0x12, 0x3F);
writeByte(0x13, 0x3F);
uint8_t b;
readByte(0x11, b);
Log.notice(F("Fan fault status 1: %c"), b);
readByte(0x10, b);
Log.notice(F("Fan fault status 2: %c"), b);
return RES_OK;
}
RESULT MAX31790::getGlobalConfiguration(GlobalConfig &config)
{
uint8_t b;
RESULT res = readByte(GLOBAL_CONFIG_REG, b);
if (res != RES_OK)
return res;
config.standBy_Not_Run = (b & 0x80);
config.normal_Not_Reset = (b & 0x40);
config.not_Bus_Timeout = (b & 0x20);
config.reserved = 0;
config.oscillator = static_cast<OscillatorEnum>(b & 0x08);
config.watchDog = static_cast<I2CWatchDogEnum>(b & 0x06);
config.watchDogStatus = (b & 0x01);
return RES_OK;
}
/**
* Convert %(0-100) dutyCycle into 0-511 range for IC MAX31790.
*/
uint16_t MAX31790::scaleDutyCycle(const uint16_t dutyCycle) const
{
uint16_t scaled = ((float)dutyCycle / 100) * MAX_DUTY_CYCLE_SCALE;
return scaled;
}
/**
* Write bytes to register and return result
* dutyCycle range is MIN_DUTY_CYCLE - MAX_DUTY_CYCLE (100%)
*/
RESULT MAX31790::setPWM(const uint8_t fanid, const uint16_t dutyCycle)
{
ASSERT_RANGE_DUTY_CYCLE(dutyCycle);
ASSERT_RANGE_FAN_ID(fanid, getFanCount());
uint8_t buffer[2];
uint16_t scaled = scaleDutyCycle(dutyCycle);
uint16_t pwm_bit = scaled << 7;
buffer[0] = pwm_bit >> 8;
buffer[1] = pwm_bit;
return writeBytes(PWMOUT_TARGET_DUTY_CYCLE(fanid), &buffer[0], 2);
}
/**
* Set PWM for all fans to duty
*/
RESULT MAX31790::setPWMForAll(const uint16_t dutyCycle)
{
ASSERT_RANGE_DUTY_CYCLE(dutyCycle);
RESULT res = RES_OK;
for (int i = 1; i <= getFanCount(); i++)
{
res = res & setPWM(i, dutyCycle);
}
return res;
}
/**
* GetTachHz per fan
*/
RESULT MAX31790::getTachHz(const uint8_t fanid, uint16_t& tachHz) {
ASSERT_RANGE_FAN_ID(fanid, getFanCount());
uint8_t buffer[2];
RESULT res = readBytes(TACH_COUNT(fanid), 2, &buffer[0]);
if (res != RES_OK)
return res;
tachHz = buffer[0];
tachHz = tachHz << 8;
tachHz |= buffer[1];
tachHz = tachHz >> 5;
return RES_OK;
}
RESULT MAX31790::getRPM(const uint8_t fanid, uint16_t& rpm) {
return ERR_METHOD_NOT_IMPLEMENTED;
}
/**
* Scan all i2c devices between 1 and 127.
*/
void MAX31790::scanForI2C()
{
Serial.println("Scanning...");
for (byte address = 1; address < 127; address++)
{
Wire.beginTransmission(address);
byte error = Wire.endTransmission();
if (error == 0)
{
Serial.print("i2c device found at address 0x");
if (address < 16)
Serial.print("0");
Serial.print(address);
Serial.println(" !");
}
}
Serial.println("Scan complete.");
}
/**
* Read byte from register and return result.
* RES_OK successful response else ERR
*/
RESULT MAX31790::readByte(const uint8_t address, uint8_t &result)
{
Wire.beginTransmission(_deviceAddress);
Wire.write(address);
if (Wire.endTransmission() != 0)
{
Log.error(F("Failed to end transmission"));
return ERR_BAD_TRANSMISSION;
}
uint8_t bytes = 1;
Wire.requestFrom(_deviceAddress, bytes);
if (Wire.available() <= 1)
{
result = Wire.read(); // Reads the data from the register
return RES_OK;
}
else
{
Log.error(F("Failed to read byte for register"));
return ERR_BAD_READ;
}
}
/**
* Read n bytes starting from startAddress into result.
*/
RESULT MAX31790::readBytes(const uint8_t startAddress, const uint8_t n, uint8_t *result)
{
Wire.beginTransmission(_deviceAddress);
for (int i = 0; i < n; i++)
{
Wire.write(startAddress + i);
if (Wire.endTransmission() != 0)
{
Log.error(F("Failed to end transmission"));
return ERR_BAD_TRANSMISSION;
}
uint8_t q = 1;
Wire.requestFrom(_deviceAddress, q);
if (Wire.available() <= 1)
{
result[i] = Wire.read(); // Reads the data from the register
}
else
{
Log.error(F("Failed to read bytes for register"));
return ERR_BAD_READ;
}
}
return RES_OK;
}
/**
* Write n bytes to register reg
*/
RESULT MAX31790::writeBytes(const uint8_t address, const uint8_t *bytes, const uint8_t n)
{
Wire.beginTransmission(_deviceAddress);
Wire.write(address);
for (int i = 0; i < n; i++)
{
Wire.write(bytes[i]);
}
Wire.endTransmission();
return RES_OK;
}
/**
* Write n bytes to register reg
*/
RESULT MAX31790::writeByte(const uint8_t address, const uint8_t byte)
{
Wire.beginTransmission(_deviceAddress);
Wire.write(address);
Wire.write(byte);
Wire.endTransmission();
return RES_OK;
}