From 3e251a82f6e8fd6fd2b4abe47dda8a5efaebf4ec Mon Sep 17 00:00:00 2001 From: Youen Toupin Date: Wed, 3 May 2017 22:06:09 +0200 Subject: [PATCH] integrated Timer1 library (instead of custom code) ; but for an unknown reason it breaks the 1-wire communication (timer code doesn't work at all, or timings are wrong, or both) ; search algorithm is broken, and probably everything else as well --- LowLevel.h | 31 ------------------------------- OneWireIO.vcxproj | 5 +++++ OneWireIO.vcxproj.filters | 3 +++ OneWireSlave.cpp | 19 ++++++++++++------- 4 files changed, 20 insertions(+), 38 deletions(-) diff --git a/LowLevel.h b/LowLevel.h index d18d387..94add00 100644 --- a/LowLevel.h +++ b/LowLevel.h @@ -27,39 +27,8 @@ #if defined (__AVR_ATtiny85__) #define CLEARINTERRUPT GIFR |= (1 << INTF0) -#include "UserTimer.h" //ATtiny-support based on TinyCore1 Arduino-core for ATtiny at http://github.com/Coding-Badly/TinyCore1.git -__attribute__((always_inline)) static inline void UserTimer_Init( void ) -{ - UserTimer_SetToPowerup(); - UserTimer_SetWaveformGenerationMode(UserTimer_(CTC_OCR)); -} -__attribute__((always_inline)) static inline void UserTimer_Run(short skipTicks) -{ - UserTimer_SetCount(0); - UserTimer_SetOutputCompareMatchAndClear(skipTicks); - UserTimer_ClockSelect(UserTimer_(Prescale_Value_64)); -} -#define UserTimer_Stop() UserTimer_ClockSelect(UserTimer_(Stopped)) - #elif defined (__AVR_ATmega328P__) #define CLEARINTERRUPT EIFR |= (1 << INTF0) -#define USERTIMER_COMPA_vect TIMER1_COMPA_vect - -__attribute__((always_inline)) static inline void UserTimer_Init( void ) -{ - TCCR1A = 0; - TCCR1B = 0; - // enable timer compare interrupt - TIMSK1 |= (1 << OCIE1A); -} -__attribute__((always_inline)) static inline void UserTimer_Run(short skipTicks) -{ - TCNT1 = 0; - OCR1A = skipTicks; - // turn on CTC mode with 64 prescaler - TCCR1B = (1 << WGM12) | (1 << CS11) | (1 << CS10); -} -#define UserTimer_Stop() TCCR1B = 0 #endif #elif defined(__MK20DX128__) || defined(__MK20DX256__) diff --git a/OneWireIO.vcxproj b/OneWireIO.vcxproj index 94ae257..020eccd 100644 --- a/OneWireIO.vcxproj +++ b/OneWireIO.vcxproj @@ -96,10 +96,15 @@ exit /B 1 true + + true + + + diff --git a/OneWireIO.vcxproj.filters b/OneWireIO.vcxproj.filters index 6f73395..885925e 100644 --- a/OneWireIO.vcxproj.filters +++ b/OneWireIO.vcxproj.filters @@ -2,10 +2,13 @@ + + + diff --git a/OneWireSlave.cpp b/OneWireSlave.cpp index a7b752e..a3bf6b3 100644 --- a/OneWireSlave.cpp +++ b/OneWireSlave.cpp @@ -1,4 +1,5 @@ #include "OneWireSlave.h" +#include "TimerOne.h" // uncomment this line to enable sending messages along with errors (but takes more program memory) //#define ERROR_MESSAGES @@ -68,9 +69,9 @@ void(*OneWireSlave::singleBitSentCallback_)(bool error); void(*OneWireSlave::logCallback_)(const char* message); -ISR(USERTIMER_COMPA_vect) // timer1 interrupt +void timer1Interrupt() { - UserTimer_Stop(); // disable clock + Timer1.detachInterrupt(); void(*event)() = timerEvent; timerEvent = 0; event(); @@ -98,7 +99,8 @@ void OneWireSlave::begin(const byte* rom, byte pinNumber) pin_.writeLow(); // make sure the internal pull-up resistor is disabled // prepare hardware timer - UserTimer_Init(); + Timer1.initialize(4); + Timer1.stop(); // start 1-wire activity beginWaitReset_(); @@ -234,16 +236,19 @@ byte OneWireSlave::crc8(const byte* data, short numBytes) void OneWireSlave::setTimerEvent_(short delayMicroSeconds, void(*handler)()) { delayMicroSeconds -= 10; // remove overhead (tuned on Arduino Uno) + if (delayMicroSeconds < 1) + delayMicroSeconds = 1; - short skipTicks = (delayMicroSeconds - 3) / 4; // round the micro seconds delay to a number of ticks to skip (4us per tick, so 4us must skip 0 tick, 8us must skip 1 tick, etc.) - if (skipTicks < 1) skipTicks = 1; timerEvent = handler; - UserTimer_Run(skipTicks); + Timer1.attachInterrupt(timer1Interrupt); + Timer1.setPeriod(delayMicroSeconds); + Timer1.start(); } void OneWireSlave::disableTimer_() { - UserTimer_Stop(); + Timer1.stop(); + Timer1.detachInterrupt(); } void OneWireSlave::onEnterInterrupt_()