Browse Source

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

timer1
Youen Toupin 5 years ago
parent
commit
3e251a82f6
  1. 31
      LowLevel.h
  2. 5
      OneWireIO.vcxproj
  3. 3
      OneWireIO.vcxproj.filters
  4. 19
      OneWireSlave.cpp

31
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__)

5
OneWireIO.vcxproj

@ -96,10 +96,15 @@ exit /B 1
<ClCompile Include="OneWireSlave.cpp">
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">true</ExcludedFromBuild>
</ClCompile>
<ClCompile Include="TimerOne.cpp">
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">true</ExcludedFromBuild>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="config\known_16bit_timers.h" />
<ClInclude Include="LowLevel.h" />
<ClInclude Include="OneWireSlave.h" />
<ClInclude Include="TimerOne.h" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">

3
OneWireIO.vcxproj.filters

@ -2,10 +2,13 @@
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<ClCompile Include="OneWireSlave.cpp" />
<ClCompile Include="TimerOne.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="LowLevel.h" />
<ClInclude Include="OneWireSlave.h" />
<ClInclude Include="TimerOne.h" />
<ClInclude Include="config\known_16bit_timers.h" />
</ItemGroup>
<ItemGroup>
<CustomBuild Include="OneWireIO.ino" />

19
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_()

Loading…
Cancel
Save