| 44 | } |
| 45 | |
| 46 | uint32_t add_timer(struct TimerState* timers, int64_t reload_val, timer_cb trigger_callback, void *trigger_cb_user_data, uint32_t isr_num) { |
| 47 | if(timers->num_inuse == MAX_TIMERS) { |
| 48 | perror("[TIMER ERROR] add_timer: Maximum number of timers is already used\n"); |
| 49 | exit(-1); |
| 50 | } |
| 51 | if(trigger_callback == NULL && isr_num == TIMER_IRQ_NOT_USED) { |
| 52 | perror("[TIMER ERROR] add_timer: No callback or irq passed to newly created timer\n"); |
| 53 | exit(-1); |
| 54 | } |
| 55 | |
| 56 | uint32_t ind; |
| 57 | if (timers->num_inuse == timers->end_ind) |
| 58 | { |
| 59 | // Insert new at the end |
| 60 | ind = timers->end_ind++; |
| 61 | } |
| 62 | else |
| 63 | { |
| 64 | // Find a gap |
| 65 | for (ind = 0; ind < timers->end_ind; ++ind) { |
| 66 | if(timers->timers[ind].in_use) { |
| 67 | break; |
| 68 | } |
| 69 | } |
| 70 | } |
| 71 | ++timers->num_inuse; |
| 72 | |
| 73 | if(reload_val == 0) { |
| 74 | reload_val = MAX_RELOAD_VAL; |
| 75 | } |
| 76 | |
| 77 | timers->timers[ind].in_use = 1; |
| 78 | timers->timers[ind].irq_num = isr_num; |
| 79 | timers->timers[ind].ticker_val = reload_val; |
| 80 | timers->timers[ind].reload_val = reload_val; |
| 81 | timers->timers[ind].trigger_callback = trigger_callback; |
| 82 | timers->timers[ind].trigger_cb_user_data = trigger_cb_user_data; |
| 83 | timers->timers[ind].is_active = 0; |
| 84 | |
| 85 | #ifdef DEBUG_TIMER |
| 86 | printf("[TIMER] Added timer with id %d, Now: num_inuse=%u\n", ind, (int) timers->num_inuse); |
| 87 | print_timer(ind); |
| 88 | #endif |
| 89 | |
| 90 | return ind; |
| 91 | } |
| 92 | |
| 93 | static inline void sync_timers(uc_engine *uc) { |
| 94 | uc->get_timer_countdown(uc->ctx, &uc->fw->timers.cur_countdown); |
no test coverage detected