I am currently working on a small project on an attiny85 that requires me to use one of the external pin change interrupts. I had that set up all working. I also need the tiny to talk to another device using i2c, so I wanted to implement that using the TinyWire library. However, I sadly had to find out that this library already uses the ISR for the PCINT vector and there's no mean for me to somehow edit that. So figured I could edit the library a bit and add a handler function to sneak in my code. I've never done something like that but here's my effort based heavily on this:
The library itself is structured as follows:
- TinyWire.cpp
- TinyWire.h
- twi.cpp - containing lots of smart stuff which i mostly don't understand as well as my desired ISR
- twi.h
First thing I did was adding a static function pointer to the twi.h file
//twi.h
static void (*__PCINT0Hook)(void);
The ISR (included in twi.cpp) was by one line at the bottom to look like this:
//twi.cpp
ISR( PCINT0_vect )
{
if((PIN_USI & (1 << PIN_USI_SDA)) && (PIN_USI & ( 1 << PIN_USI_SCL ))){
// stop condition occured
_delay_us(7);
if((PIN_USI & (1 << PIN_USI_SDA)) && (PIN_USI & ( 1 << PIN_USI_SCL ))){
twi_bus_busy = false;
if(currently_receiving){ // If we are receiving bytes from a master, call user callback
if(rxByteNum>0) twi_onSlaveReceive(rxByteNum); // check, if anything is in the rx buffer, because it is possible,
// that the communication was interrupted by a stop condition
currently_receiving = false;
}
USISR |= 1<<USIPF; // resetting stop condition flag
}
}
// --------- NEW STUFF -----------
__PCINT0Hook();
}
Now I just added a public function to the TinyTwi class that sets the function pointer to my desire.
//TinyWire.h
void PCINT0Hook(void (*function)(void));
--------------------------------------
//TinyWire.cpp
void TinyTwi::PCINT0Hook(void (*function)(void))
{
__PCINT0Hook = function;
}
Now all I need to do in order to get my code executed in the ISR, is to wrap it in a function in my main program and pass it to the handler.
//main.ino
void MySneakyISR()
{
//do something
}
TinyWire.PCINT0Hook(demoISRaction);
Or so I thought... Turns out I'm not the genius I thought I was and I can't seem to get this to work. It's also really hard trying to figure out what's wrong, as my way of debugging was primarily flashing leds and sending information over i2c (which is not working atm).
I was wondering if any of you could help me get on track again? Thanks for all the ideas in advance.