Skip to content

Commit

Permalink
support timing on Arduino 101
Browse files Browse the repository at this point in the history
The MsTimer2 library (using timer2 with hard-coded 1ms resolution)
supports ATmega1280, ATmega328, ATmega48/88/168 and ATmega128/8
(http://playground.arduino.cc/Main/MsTimer2). For the Arduino/Genuino
101 we need to use the CurieTimerOne library
(https://www.arduino.cc/en/Reference/CurieTimerOne).
  • Loading branch information
seth10 committed Oct 6, 2016
1 parent a4510aa commit ff3b86b
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 39 deletions.
30 changes: 25 additions & 5 deletions BaseI2CDevice.cpp
Expand Up @@ -21,7 +21,11 @@
*/

#include "BaseI2CDevice.h"
#include "MsTimer2.h"
#if defined(ARDUINO_ARC32_TOOLS)
#include "CurieTimerOne.h"
#else
#include "MsTimer2.h"
#endif
#include <Wire.h>

extern "C" {
Expand Down Expand Up @@ -77,7 +81,11 @@ uint8_t* BaseI2CDevice::readRegisters(
uint8_t buffer_length, // (optional) length of user-supplied buffer
bool clear_buffer) // should we zero out the buffer first? (optional)
{
MsTimer2::reset();
#if defined(ARDUINO_ARC32_TOOLS)
CurieTimerOne.rdRstTickCount();
#else
MsTimer2::reset();
#endif
if (!buffer)
{
buffer = _buffer;
Expand Down Expand Up @@ -119,7 +127,11 @@ uint8_t* BaseI2CDevice::readRegisters(

_write_error_code = Wire.endTransmission();

MsTimer2::reset();
#if defined(ARDUINO_ARC32_TOOLS)
CurieTimerOne.rdRstTickCount();
#else
MsTimer2::reset();
#endif
return buffer;
}

Expand Down Expand Up @@ -167,7 +179,11 @@ bool BaseI2CDevice::writeRegisters(
uint8_t bytes_to_write, // number of bytes to write
uint8_t* buffer) // optional user-supplied buffer
{
MsTimer2::reset();
#if defined(ARDUINO_ARC32_TOOLS)
CurieTimerOne.rdRstTickCount();
#else
MsTimer2::reset();
#endif
if (!buffer)
{
buffer = _buffer;
Expand All @@ -193,7 +209,11 @@ bool BaseI2CDevice::writeRegisters(

_write_error_code = Wire.endTransmission();

MsTimer2::reset();
#if defined(ARDUINO_ARC32_TOOLS)
CurieTimerOne.rdRstTickCount();
#else
MsTimer2::reset();
#endif
return _write_error_code == 0; // 0 indicates success
}

Expand Down
59 changes: 36 additions & 23 deletions EVShield.cpp
Expand Up @@ -20,7 +20,11 @@

#include "EVShield.h"
#include "Wire.h"
#include "MsTimer2.h"
#if defined(ARDUINO_ARC32_TOOLS)
#include "CurieTimerOne.h"
#else
#include "MsTimer2.h"
#endif
static void pingEV();

#if defined(__AVR__)
Expand Down Expand Up @@ -128,9 +132,13 @@ void EVShield::initProtocols(SH_Protocols protocol)

void EVShield::I2CTimer()
{
//TCNT2 = 0;
MsTimer2::set(300, pingEV); // 300ms period
MsTimer2::start();
#if defined(ARDUINO_ARC32_TOOLS)
CurieTimerOne.start(300000, pingEV); // in microseconds
#else
//TCNT2 = 0;
MsTimer2::set(300, pingEV); // 300ms period
MsTimer2::start();
#endif
}

void EVShield::initLEDTimers()
Expand Down Expand Up @@ -751,25 +759,30 @@ int EVShieldBankB::sensorReadRaw(uint8_t which_sensor)

void pingEV()
{
TWCR = (1<<TWINT)|(1<<TWSTA)|(1<<TWEN);
while ((TWCR & (1<<TWINT)) == 0);
TWDR = 0x34;
TWCR = (1<<TWINT)|(1<<TWEN);
while ((TWCR & (1<<TWINT)) == 0);
TWCR = (1<<TWINT)|(1<<TWSTO)|(1<<TWEN);
TCNT2 = 0;//initialize counter value to 0
/*
if (toggle2)
{
digitalWrite(13, HIGH);
toggle2 = 0;
}
else
{
digitalWrite(13, LOW);
toggle2 = 1;
}
*/
#if defined(ARDUINO_ARC32_TOOLS)
Wire.beginTransmission(0x34);
Wire.endTransmission();
#else
TWCR = (1<<TWINT)|(1<<TWSTA)|(1<<TWEN);
while ((TWCR & (1<<TWINT)) == 0);
TWDR = 0x34;
TWCR = (1<<TWINT)|(1<<TWEN);
while ((TWCR & (1<<TWINT)) == 0);
TWCR = (1<<TWINT)|(1<<TWSTO)|(1<<TWEN);
TCNT2 = 0;//initialize counter value to 0
/*
if (toggle2)
{
digitalWrite(13, HIGH);
toggle2 = 0;
}
else
{
digitalWrite(13, LOW);
toggle2 = 1;
}
*/
#endif
}

#if defined(__AVR__)
Expand Down
22 changes: 12 additions & 10 deletions MsTimer2.cpp
Expand Up @@ -148,14 +148,16 @@ void MsTimer2::_overflow() {
}
}

ISR(TIMER2_OVF_vect) {
#if defined (__AVR_ATmega168__) || defined (__AVR_ATmega48__) || defined (__AVR_ATmega88__) || defined (__AVR_ATmega328P__) || (__AVR_ATmega1280__)
TCNT2 = MsTimer2::tcnt2;
#elif defined (__AVR_ATmega128__)
TCNT2 = MsTimer2::tcnt2;
#elif defined (__AVR_ATmega8__)
TCNT2 = MsTimer2::tcnt2;
#endif
MsTimer2::_overflow();
}
#if defined(__AVR__)
ISR(TIMER2_OVF_vect) {
#if defined (__AVR_ATmega168__) || defined (__AVR_ATmega48__) || defined (__AVR_ATmega88__) || defined (__AVR_ATmega328P__) || (__AVR_ATmega1280__)
TCNT2 = MsTimer2::tcnt2;
#elif defined (__AVR_ATmega128__)
TCNT2 = MsTimer2::tcnt2;
#elif defined (__AVR_ATmega8__)
TCNT2 = MsTimer2::tcnt2;
#endif
MsTimer2::_overflow();
}

#endif
4 changes: 3 additions & 1 deletion MsTimer2.h
@@ -1,7 +1,9 @@
#ifndef MsTimer2_h
#define MsTimer2_h

#include <avr/interrupt.h>
#if defined(__AVR__)
#include <avr/interrupt.h>
#endif

namespace MsTimer2 {
extern unsigned long msecs;
Expand Down
3 changes: 3 additions & 0 deletions SHDefines.h
Expand Up @@ -41,6 +41,9 @@
#endif

#endif
#if defined(ARDUINO_ARC32_TOOLS)
#define MODEL_EVSHIELD_D
#endif


/**
Expand Down

0 comments on commit ff3b86b

Please sign in to comment.