void timerCaptureAttach(const Timer* timer, TimerCallback callback, void* user_data, boolean risingEdge ) { TimerData* data = timerGetData(timer); if(data->capture_callback != null){ setError(TIMER_CAPTURE_CALLBACK_EXISTS); } // Turn off interrupts whilst changing // in case an interrupt happens half way through CRITICAL_SECTION_START; // Set callback data->capture_callback = callback; data->capture_data = user_data; // Set the rising falling edge if(risingEdge){ __portMaskSet(&timer->pgm_captureedge); }else{ __portMaskClear(&timer->pgm_captureedge); } // Clear any pending capture interrupt flag timerCaptureClearInterruptPending(timer); // enable interrupt __portMaskSet(&timer->pgm_captureint); CRITICAL_SECTION_END; }
/* ------------------------------------------------------- // // Attach a callback function to a timer when it overflows // // This function is called during an interrupt so should // be quick. // ------------------------------------------------------- */ void timerOverflowAttach(const Timer* timer, TimerCallback callback, void* user_data ) { // Turn off interrupts whilst changing // in case an interrupt happens half way through CRITICAL_SECTION_START; if(callback!=null && timerGetData(timer)->overflow_callback != null){ setError(TIMER_OVERFLOW_CALLBACK_EXISTS); } // Set callback TimerData* data = timerGetData(timer); data->overflow_callback = callback; data->overflow_data = user_data; // enable interrupt if there is callback if(callback!=null){ // enable overflow interrupts __portMaskSet(&timer->pgm_overflowint); }else{ // Disable overflow interrupts __portMaskClear(&timer->pgm_overflowint); } CRITICAL_SECTION_END; }
void timerCaptureDetach(const Timer* timer){ TimerData* data = timerGetData(timer); // stop interrupt __portMaskClear(&timer->pgm_captureint); // Clear any pending capture interrupt flag timerCaptureClearInterruptPending(timer); // Set callback data->capture_callback = null; }