puuu / USIWire

USI based TWI/I2C library for Arduino
GNU Lesser General Public License v3.0
58 stars 18 forks source link

Enhancement: Provide Callback on entering Interrupt Routine #13

Open jbaumann opened 4 years ago

jbaumann commented 4 years ago

When working with the Watchdog, code has to be executed directly after waking up, otherwise the watchdog resets the Microcontroller.

When USI_Start_Condition_ISR() is called, then the processor might have been woken from any sleep mode, which means that the Watchdog has to be turned off very early during its execution.

I propose a callback for this. I have tested an implementation of this with my current application ATTinyDaemon with different communication partners (RPi3, RPI Zero...) and it works without adverse effects. Here is what my implementation looks like in USI_TWI_Slave.c, line 161ff:

__interrupt void USI_Start_Condition_ISR(void)
#endif
{

    unsigned char tmpPin; // Temporary variable for pin state
    unsigned char tmpRxHead; // Temporary variable to store volatile

    if (USI_TWI_On_Slave_Interrupt) {
            USI_TWI_On_Slave_Interrupt();
    }

    // call slave receive callback on repeated start
...

together with adding the attribute in TWI_Slave.h, line 41,

void (*USI_TWI_On_Slave_Interrupt)(void);

a method declaration in USIWire.h, line 66

    void onInterrupt( void (*)(void) );

and the following in USIWire.c, line 282ff:

// sets function called on slave interrupt
void USIWire::onInterrupt( void (*function)(void) ) {
  USI_TWI_On_Slave_Interrupt = function;
}
jbaumann commented 4 years ago

Added a pull request for this.