Skip to content

Commit

Permalink
implement tws_delay, a delayloop that will still call TinyWireS_stop_…
Browse files Browse the repository at this point in the history
…check() often enough. UNTESTED, "if it complies, ship it"
  • Loading branch information
rambo committed Nov 1, 2012
1 parent b3906ef commit 71accad
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 4 deletions.
16 changes: 16 additions & 0 deletions TinyWireS/TinyWireS.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ extern "C" {
}

#include "TinyWireS.h"
#include "Arduino.h"

// Constructors ////////////////////////////////////////////////////////////////

Expand Down Expand Up @@ -79,6 +80,21 @@ void TinyWireS_stop_check()
usi_onReceiverPtr(amount);
}

// Implement a delay loop that checks for the stop bit (basically direct copy of the stock arduino implementation from wiring.c)
void tws_delay(unsigned long ms)
{
uint16_t start = (uint16_t)micros();
while (ms > 0)
{
TinyWireS_stop_check();
if (((uint16_t)micros() - start) >= 1000)
{
ms--;
start += 1000;
}
}
}

// Preinstantiate Objects //////////////////////////////////////////////////////

USI_TWI_S TinyWireS = USI_TWI_S();
Expand Down
2 changes: 2 additions & 0 deletions TinyWireS/TinyWireS.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ class USI_TWI_S
};

void TinyWireS_stop_check();
// Implement a delay loop that checks for the stop bit (basically direct copy of the stock arduino implementation from wiring.c)
void tws_delay(unsigned long);

extern USI_TWI_S TinyWireS;

Expand Down
10 changes: 6 additions & 4 deletions TinyWireS/examples/attiny85_i2c_slave/attiny85_i2c_slave.ino
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
/**
* Example sketch for writing to and reading from a slave in transactional manner
*
* NOTE: You must not use delay() or I2C communications will fail, use tws_delay() instead (or preferably some smarter timing system)
*
* On write the first byte received is considered the register addres to modify/read
* On each byte sent or read the register address is incremented (and it will loop back to 0)
*
Expand Down Expand Up @@ -57,16 +59,16 @@ void requestEvent()
reg_position = (reg_position+1) % sizeof(i2c_regs);
}


// TODO: Either update this to use something smarter for timing or remove it alltogether
void blinkn(uint8_t blinks)
{
digitalWrite(3, HIGH);
while(blinks--)
{
digitalWrite(3, LOW);
delay(50);
tws_delay(50);
digitalWrite(3, HIGH);
delay(100);
tws_delay(100);
}
}

Expand Down Expand Up @@ -129,7 +131,7 @@ void loop()
{
/**
* This is the only way we can detect stop condition (http://www.avrfreaks.net/index.php?name=PNphpBB2&file=viewtopic&p=984716&sid=82e9dc7299a8243b86cf7969dd41b5b5#984716)
* it needs to be called in a very tight loop in order not to miss any.
* it needs to be called in a very tight loop in order not to miss any (REMINDER: Do *not* use delay() anywhere, use tws_delay() instead).
* It will call the function registered via TinyWireS.onReceive(); if there is data in the buffer on stop.
*/
TinyWireS_stop_check();
Expand Down

2 comments on commit 71accad

@stellasis
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hi, I just want to ask if I can use this library without using arduino? I mean, using attiny as master instead of arduino? thanks

@rambo
Copy link
Owner Author

@rambo rambo commented on 71accad Aug 4, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can use any master that can deal with slave stretching the clock, I have not verified that TinyWireM(aster) can (and most of the simple bit-banged masters can't). See also https://github.com/rambo/TinyWire#note-about-reliable-communication

Please sign in to comment.