/** * @brief Attach a RTC interrupt. * @param interrupt Interrupt number to attach to; this may be any rtc_interrupt_id. * @param handler Handler to attach to the given interrupt. * @see rtc_interrupt_id */ void rtc_attach_interrupt( uint8 interrupt, voidFuncPtr handler) { RTC->handlers[interrupt] = handler; rtc_enable_irq(interrupt); switch (interrupt) { case RTC_SECONDS_INTERRUPT: nvic_irq_enable(NVIC_RTC); break; case RTC_OVERFLOW_INTERRUPT: nvic_irq_enable(NVIC_RTC); break; case RTC_ALARM_GLOBAL_INTERRUPT: nvic_irq_enable(NVIC_RTC); break; case RTC_ALARM_SPECIFIC_INTERRUPT: nvic_irq_enable(NVIC_RTCALARM); break; // The alarm specific interrupt can wake us from deep sleep. } }
static void timer_handler(unsigned long data) { static int first; if (!first) { first = 1; rtai_cli(); } CHECK_FLAGS(); while ((ovr = rt_irq_wait_if(TIMER_IRQ)) > 0) { if (ovr == RT_IRQ_TASK_ERR) return; if (ovr > 0) { /* overrun processing, if any, goes here */ rt_sem_signal(dspsem); } } /* normal processing goes here */ intcnt++; rtc_enable_irq(TIMER_IRQ, TIMER_FRQ); rt_sem_signal(dspsem); }
int main(void) { RT_TASK *maint; int maxcnt; printf("GIVE THE NUMBER OF INTERRUPTS YOU WANT TO COUNT: "); scanf("%d", &maxcnt); start_rt_timer(0); if (!(maint = rt_task_init(nam2num("MAIN"), 1, 0, 0))) { printf("CANNOT INIT MAIN TASK > MAIN <\n"); exit(1); } if (!(dspsem = rt_sem_init(nam2num("DSPSEM"), 0))) { printf("CANNOT INIT SEMAPHORE > DSPSEM <\n"); exit(1); } tasklet = rt_init_tasklet(); mlockall(MCL_CURRENT | MCL_FUTURE); rt_insert_tasklet(tasklet, 0, timer_handler, 111, nam2num("TSKLET"), 1); rt_request_irq_task(TIMER_IRQ, tasklet, RT_IRQ_TASKLET, 1); rtc_start(TIMER_FRQ); rtc_enable_irq(TIMER_IRQ, TIMER_FRQ); while (intcnt < maxcnt) { rt_sem_wait(dspsem); printf("OVERRUNS %d, INTERRUPT COUNT %d\n", ovr, intcnt); } rtc_stop(); rt_release_irq_task(TIMER_IRQ); printf("TEST ENDS\n"); rt_remove_tasklet(tasklet); rt_delete_tasklet(tasklet); rt_sem_delete(dspsem); rt_task_delete(maint); return 0; }