Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,8 @@ include
lib
test
build.log
CMakeLists.txt
CMakeListsPrivate.txt
cmake-build-*
.idea
platformio_override.ini
68 changes: 35 additions & 33 deletions platformio.ini
Original file line number Diff line number Diff line change
@@ -1,33 +1,35 @@
; PlatformIO Project Configuration File
;
; Build options: build flags, source filter
; Upload options: custom upload port, speed and extra flags
; Library options: dependencies, extra library storages
; Advanced options: extra scripting
;
; Please visit documentation for the other options and examples
; https://docs.platformio.org/page/projectconf.html

[env:ATtiny85]
platform = atmelavr
board = attiny85
framework = arduino

lib_deps =
PinChangeInterrupt@1.2.6
TinyWireSio

board_build.f_cpu = 8000000L
board_fuses.lfuse = 0xE2
board_fuses.hfuse = 0xDD
board_fuses.efuse = 0xFF

upload_protocol = stk500v1
; each flag in a new line
upload_flags =
-P$UPLOAD_PORT
-b$UPLOAD_SPEED
-e

upload_port = COM5
upload_speed = 19200
; PlatformIO Project Configuration File
;
; Build options: build flags, source filter
; Upload options: custom upload port, speed and extra flags
; Library options: dependencies, extra library storages
; Advanced options: extra scripting
;
; Please visit documentation for the other options and examples
; https://docs.platformio.org/page/projectconf.html

[platformio]
extra_configs = platformio_override.ini

[env:ATtiny85]
platform = atmelavr
board = attiny85
framework = arduino

lib_deps =
PinChangeInterrupt@1.2.6

board_build.f_cpu = 8000000L
board_fuses.lfuse = 0xE2
board_fuses.hfuse = 0xDD
board_fuses.efuse = 0xFF

upload_protocol = stk500v1
; each flag in a new line
upload_flags =
-P$UPLOAD_PORT
-b$UPLOAD_SPEED
-e

upload_port = COM5
upload_speed = 19200
4 changes: 4 additions & 0 deletions platformio_override_sample.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[env:ATtiny85]
upload_port =
upload_flags =
upload_protocol = usbtiny
32 changes: 16 additions & 16 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

#include "config.h"
#include <PinChangeInterrupt.h>
#include <TinyWireS.h>
#include <Wire.h>

/**
* Buffer size of the I2C RX buffer.
Expand All @@ -30,14 +30,14 @@
*/
#define checkTime(a, b) ((long)(a - b) >= 0)

bool ledStatus = false;
//bool ledStatus = false;

volatile uint16_t rps = 0;
uint16_t rpm = 0;

unsigned long now = 0;
unsigned long timeNextRpmCalc = 0;
unsigned long timepwmLevelMinCalNextStep = 0;
//unsigned long timePwmLevelMinCalNextStep = 0;

uint8_t pwmLevel = 0;
uint8_t pwmLevelMin = 0;
Expand Down Expand Up @@ -78,7 +78,7 @@ void setFanSpeed (uint8_t speed) {
* send-buffer when using this callback
*/
void i2cRequestEvent() {
TinyWireS.send(i2c_regs[reg_position]);
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)
Expand All @@ -96,15 +96,15 @@ void i2cRequestEvent() {
* To be quick, set flags for long running tasks to be called from the mainloop
* instead of running them directly.
*/
void i2cReceiveEvent (uint8_t howMany) {
void i2cReceiveEvent (int howMany) {
if (howMany < 1 || howMany > TWI_RX_BUFFER_SIZE) {
// Sanity-check
return;
}

blinkLed = true;

reg_position = TinyWireS.receive();
reg_position = Wire.read();
howMany--;
if (!howMany) {
// This write was only to set the buffer for next read
Expand All @@ -114,23 +114,23 @@ void i2cReceiveEvent (uint8_t howMany) {
while (howMany--) {
if (reg_position == 0x00 && !pwmLevelMinCal && !pwmLevelMinCalStart) {
// status register
i2c_regs[0x00] = TinyWireS.receive() & 0b00000001; // only interested in bit 0
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] = TinyWireS.receive();
i2c_regs[0x01] = Wire.read();
setFanSpeed(i2c_regs[0x01]);
} else if (reg_position == 0x02 && !pwmLevelMinCal && !pwmLevelMinCalStart) {
// pwm min register
i2c_regs[0x02] = TinyWireS.receive();
i2c_regs[0x02] = Wire.read();
pwmLevelMin = i2c_regs[0x02];
setFanSpeed(i2c_regs[0x01]);
} else {
// no writeable register... just read and discard data
TinyWireS.receive();
Wire.read();
}

reg_position++;
Expand All @@ -143,7 +143,7 @@ void i2cReceiveEvent (uint8_t howMany) {
/**
* ISR to handle tacho impulses.
*/
void handlePcintTacho (void) {
void handlePcintTacho () {
rps++;
}

Expand All @@ -156,9 +156,9 @@ void setup() {

attachPCINT(digitalPinToPCINT(PIN_TACHO), handlePcintTacho, FALLING);

TinyWireS.begin(I2C_SLAVE_ADDRESS);
TinyWireS.onReceive(i2cReceiveEvent);
TinyWireS.onRequest(i2cRequestEvent);
Wire.begin(I2C_SLAVE_ADDRESS);
Wire.onReceive(i2cReceiveEvent);
Wire.onRequest(i2cRequestEvent);

now = millis();
timeNextRpmCalc = now + 1000 * RPS_MULTI;
Expand All @@ -171,12 +171,12 @@ void setup() {
* Main loop.
*/
void loop() {
TinyWireS_stop_check();
//TinyWireS_stop_check();

// need to blink the LED?
if (blinkLed) {
digitalWrite(LED, HIGH);
tws_delay(50);
delay(50);
digitalWrite(LED, LOW);
blinkLed = false;
}
Expand Down