X-Git-Url: https://git.rohieb.name/hackover2013-badge-firmware.git/blobdiff_plain/9d18e10afb2439a6a9ba6978a799259746a837b7..4bb0f0854a5aa7467512e0bd1e5bf4c724437d4e:/core/timer32/timer32.c?ds=sidebyside diff --git a/core/timer32/timer32.c b/core/timer32/timer32.c index b768905..d25638f 100644 --- a/core/timer32/timer32.c +++ b/core/timer32/timer32.c @@ -69,6 +69,7 @@ volatile uint32_t timer32_0_counter = 0; volatile uint32_t timer32_1_counter = 0; +void (*interruptHandler0)(void) = NULL; /**************************************************************************/ /*! @@ -130,14 +131,20 @@ void timer32Delay(uint8_t timerNum, uint32_t delay) /**************************************************************************/ void TIMER32_0_IRQHandler(void) { - /* Clear the interrupt flag */ - TMR_TMR32B0IR = TMR_TMR32B0IR_MR0; + /* Call the callback function if required */ + if(NULL != interruptHandler0) + { + (*interruptHandler0)(); + } - /* If you wish to perform some action after each timer 'tick' (such as + /* Clear the interrupt flag */ + TMR_TMR32B0IR = TMR_TMR32B0IR_MR0; + + /* If you wish to perform some action after each timer 'tick' (such as incrementing a counter variable) you can do so here */ - timer32_0_counter++; + timer32_0_counter++; - return; + return; } /**************************************************************************/ @@ -341,4 +348,79 @@ void timer32Init(uint8_t timerNum, uint32_t timerInterval) return; } +/**************************************************************************/ +/*! + @brief Sets the optional callback function for 32-bit timer 0 + + @section EXAMPLE + @code + #include "core/timer32/timer32.h" + + static volatile int32_t timerCounter; + + // Callback function for 32-bit timer 0 + void ct32b0Callback(void) + { + timerCounter++; + } + + int main(void) + { + // Configure cpu and mandatory peripherals + systemInit(); + + // Init timer 0 with 1ms delay + timer32Init(0, TIMER32_CCLK_1MS); + + // Setup the interrupt callback + timer32SetIntHandler(ct32b0Callback); + + // Enable the timer + timer32Enable(0); + + while (1) + { + } + } + @endcode +*/ +/**************************************************************************/ +void timer32SetIntHandler(void (*handler)(void)) +{ + interruptHandler0 = handler; +} + +/**************************************************************************/ +/*! + @brief Returns the value of the auto-incrementing timer counter(s) +*/ +/**************************************************************************/ +uint32_t timer32GetCount(uint8_t timerNum) +{ + if (0 == timerNum) + { + return timer32_0_counter; + } + else + { + return timer32_1_counter; + } +} + +/**************************************************************************/ +/*! + @brief Resets the auto-incrementing timer counter(s) +*/ +/**************************************************************************/ +void timer32ResetCounter(uint8_t timerNum) +{ + if (0 == timerNum) + { + timer32_0_counter = 0; + } + else if (1 == timerNum) + { + timer32_1_counter = 0; + } +}