-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain.cpp
More file actions
230 lines (196 loc) · 5.72 KB
/
main.cpp
File metadata and controls
230 lines (196 loc) · 5.72 KB
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
/**
* ATtiny I²C Fan Control
*
* Control a PWM fan over the I²C bus.
*
* Copyright (C) 2020-2021 Peter Müller <peter@crycode.de> (https://crycode.de)
* License: CC BY-NC-SA 4.0
*/
#include "config.h"
#include <PinChangeInterrupt.h>
#include <Wire.h>
/**
* Buffer size of the I2C RX buffer.
*/
#define TWI_RX_BUFFER_SIZE ( 16 )
/**
* Multiplicator for rounds per second (RPS) counting.
* This is the number of seconds to count the tacho impulses before re-calculate
* the RPS/RPM of the fan.
*/
#define RPS_MULTI 3
/**
* Macro to check the time for time-based events.
* If a is greater than or equal to b this returns true, otherwise false.
* This resprects a possible rollover of a and b.
*/
#define checkTime(a, b) ((long)(a - b) >= 0)
//bool ledStatus = false;
volatile uint16_t rps = 0;
uint16_t rpm = 0;
unsigned long now = 0;
unsigned long timeNextRpmCalc = 0;
//unsigned long timePwmLevelMinCalNextStep = 0;
uint8_t pwmLevel = 0;
uint8_t pwmLevelMin = 0;
bool pwmLevelMinCal = false;
bool pwmLevelMinCalStart = false;
bool blinkLed = false;
// The "registers" we expose to I2C
volatile uint8_t i2c_regs[] = {
0x00, // 0x00 - Status register
0x00, // 0x01 - set speed command
0x00, // 0x02 - min pwm
0x00, // 0x03 - tacho rps
0x00, // 0x04 - tacho low byte
0x00, // 0x05 - tacho high byte
};
const uint8_t reg_size = sizeof(i2c_regs);
// Tracks the current register pointer position
volatile uint8_t reg_position;
/**
* Set the fan speed.
* @param speed The speed from 0 (off) to 255 (full speed).
*/
void setFanSpeed (uint8_t speed) {
if (speed > 0) {
pwmLevel = map(speed, 0, 255, pwmLevelMin, 255);
} else {
pwmLevel = 0;
}
analogWrite(PIN_PWM, pwmLevel);
}
/**
* This is called for each read request we receive, never put more than one byte of data (with TinyWireS.send) to the
* send-buffer when using this callback
*/
void i2cRequestEvent() {
Wire.write(i2c_regs[reg_position]);
// Increment the reg position on each read, and loop back to zero
reg_position++;
if (reg_position >= reg_size)
{
reg_position = 0;
}
}
/**
* The I2C data received-handler
*
* This needs to complete before the next incoming transaction (start, data,
* restart/stop) on the bus occurs.
*
* To be quick, set flags for long running tasks to be called from the mainloop
* instead of running them directly.
*/
void i2cReceiveEvent (int howMany) {
if (howMany < 1 || howMany > TWI_RX_BUFFER_SIZE) {
// Sanity-check
return;
}
blinkLed = true;
reg_position = Wire.read();
howMany--;
if (!howMany) {
// This write was only to set the buffer for next read
return;
}
while (howMany--) {
if (reg_position == 0x00 && !pwmLevelMinCal && !pwmLevelMinCalStart) {
// status register
i2c_regs[0x00] = Wire.read() & 0b00000001; // only interested in bit 0
if (bitRead(i2c_regs[0x00], 0)) {
// if bit 0 is set start calibration
pwmLevelMinCalStart = true;
}
} else if (reg_position == 0x01 && !pwmLevelMinCal && !pwmLevelMinCalStart) {
// fan speed register
i2c_regs[0x01] = Wire.read();
setFanSpeed(i2c_regs[0x01]);
} else if (reg_position == 0x02 && !pwmLevelMinCal && !pwmLevelMinCalStart) {
// pwm min register
i2c_regs[0x02] = Wire.read();
pwmLevelMin = i2c_regs[0x02];
setFanSpeed(i2c_regs[0x01]);
} else {
// no writeable register... just read and discard data
Wire.read();
}
reg_position++;
if (reg_position >= reg_size) {
reg_position = 0;
}
}
}
/**
* ISR to handle tacho impulses.
*/
void handlePcintTacho () {
rps++;
}
/**
* Setup function.
*/
void setup() {
pinMode(LED, OUTPUT);
pinMode(PIN_PWM, OUTPUT);
attachPCINT(digitalPinToPCINT(PIN_TACHO), handlePcintTacho, FALLING);
Wire.begin(I2C_SLAVE_ADDRESS);
Wire.onReceive(i2cReceiveEvent);
Wire.onRequest(i2cRequestEvent);
now = millis();
timeNextRpmCalc = now + 1000 * RPS_MULTI;
setFanSpeed(0);
pwmLevelMinCalStart = true;
}
/**
* Main loop.
*/
void loop() {
//TinyWireS_stop_check();
// need to blink the LED?
if (blinkLed) {
digitalWrite(LED, HIGH);
delay(50);
digitalWrite(LED, LOW);
blinkLed = false;
}
now = millis();
if (checkTime(now, timeNextRpmCalc)) {
rpm = rps * 60 / RPS_MULTI;
i2c_regs[3] = (rps / RPS_MULTI) & 0xff;
i2c_regs[4] = rpm & 0xff;
i2c_regs[5] = (rpm >> 8) & 0xff;
rps = 0;
timeNextRpmCalc = now + 1000 * RPS_MULTI;
if (pwmLevelMinCalStart) {
// should start calibration... need to stop the fan first in 2 loop runs... third run will start calibration
if (!pwmLevelMinCal) {
// first loop run stop fan and set flag to run calibration
setFanSpeed(0);
bitSet(i2c_regs[0x00], 0);
digitalWrite(LED, HIGH);
pwmLevelMinCal = true;
} else {
// second loop run unset flag for calibration start
pwmLevelMinCalStart = false;
}
} else if (pwmLevelMinCal) {
if (rpm > 200) {
// fan is rotating with at least 200rpm ... calibration done! :-)
pwmLevelMin = pwmLevel + 5; // add 5 to be sure that the fan will rotate
pwmLevelMinCal = false;
i2c_regs[0x02] = pwmLevelMin;
bitClear(i2c_regs[0x00], 0);
setFanSpeed(0);
digitalWrite(LED, LOW);
} else {
// fan not rotating
// increase pwm
pwmLevel += 5;
analogWrite(PIN_PWM, pwmLevel);
bitSet(i2c_regs[0x00], 0);
digitalWrite(LED, HIGH);
}
}
}
}