23 vs32 timer_counter_id;
24 timer_alarm_handler_t callback_handler;
25 void *callback_handler_arg;
28#define PTR_TO_ALARM_ID(ptr_, cid_) ((s32)((((uiptr)(ptr_)) << 4) | ((cid_) & 0xFE) | 1))
29#define ALARM_ID_TO_PTR(id_) ((alarm_struct_t *)((((uiptr)(id_)) >> 8) << 4))
30#define ALARM_ID_IS_VALID(id_) ((ALARM_ID_TO_PTR(id_) != NULL) && ((siptr)(id_) >= 0) && ((((((uiptr)id_) & 0xFF) == (((ALARM_ID_TO_PTR(id_))->timer_counter_id) & 0xFF)))))
34alarm_struct_t g_AlarmBuf[ALARM_COUNT]
__attribute__((aligned(16)));
35alarm_struct_t *g_pFreeAlarm = NULL;
39 g_pFreeAlarm = &g_AlarmBuf[0];
40 for (u32 i = 0; i < (ALARM_COUNT - 1); i += 1)
42 g_AlarmBuf[i].alarm_next = &g_AlarmBuf[i + 1];
44 g_AlarmBuf[ALARM_COUNT - 1].alarm_next = NULL;
47static inline alarm_struct_t *ForTimer_AllocAlarm(
void)
49 alarm_struct_t *alarm_current;
50 alarm_current = g_pFreeAlarm;
51 if (alarm_current != NULL)
53 g_pFreeAlarm = alarm_current->alarm_next;
58static inline void ForTimer_FreeAlarm(alarm_struct_t *alarm_current)
60 alarm_current->alarm_next = g_pFreeAlarm;
61 alarm_current->timer_counter_id = 0;
62 g_pFreeAlarm = alarm_current;
65u64 AlarmHandler(s32 alarm_id, u64 scheduled_time, u64 actual_time,
void *arg,
void *last_pc)
68 alarm_struct_t *alarm_current;
70 alarm_current = (alarm_struct_t *)arg;
71 result = alarm_current->callback_handler(
72 PTR_TO_ALARM_ID(alarm_current, alarm_id),
75 alarm_current->callback_handler_arg, last_pc);
78 ForTimer_FreeAlarm(alarm_current);
84s32 iSetTimerAlarm(u64 clock_cycles, timer_alarm_handler_t callback_handler,
void *arg)
87 alarm_struct_t *alarm_current;
89 if (callback_handler == NULL)
93 alarm_current = ForTimer_AllocAlarm();
94 if (alarm_current == NULL)
98 timer_counter_id = iAllocTimerCounter();
99 if (timer_counter_id < 0)
101 ForTimer_FreeAlarm(alarm_current);
102 return timer_counter_id;
104 alarm_current->timer_counter_id = timer_counter_id;
105 alarm_current->callback_handler = callback_handler;
106 alarm_current->callback_handler_arg = arg;
107 iSetTimerHandler(timer_counter_id, clock_cycles, AlarmHandler, alarm_current);
108 iStartTimerCounter(timer_counter_id);
109 return PTR_TO_ALARM_ID(alarm_current, timer_counter_id);
120s32
SetTimerAlarm(u64 clock_cycles, timer_alarm_handler_t callback_handler,
void *arg)
126 ret = iSetTimerAlarm(clock_cycles, callback_handler, arg);
134s32 iReleaseTimerAlarm(s32
id)
136 alarm_struct_t *alarm_current;
139 alarm_current = ALARM_ID_TO_PTR(
id);
140 if (!ALARM_ID_IS_VALID(
id))
144 ret = iFreeTimerCounter(alarm_current->timer_counter_id);
147 ForTimer_FreeAlarm(alarm_current);
152s32 ReleaseTimerAlarm(s32
id)
154 alarm_struct_t *alarm_current;
157 alarm_current = ALARM_ID_TO_PTR(
id);
159 if (!ALARM_ID_IS_VALID(
id))
167 FreeTimerCounter(alarm_current->timer_counter_id);
169 ForTimer_FreeAlarm(alarm_current);
s32 SetTimerAlarm(u64 clock_cycles, timer_alarm_handler_t callback_handler, void *arg)