17#define USER_MODE_DISPATCHER 0x00082000
20#define T3_COUNT_W ((vu32 *)0xB0001800)
21#define T3_MODE_W ((vu32 *)0xB0001810)
22#define T3_COMP_W ((vu32 *)0xB0001820)
24static int AlarmCount = 0;
25static u64 AlarmStatus = 0;
37static struct alarm alarms[MAX_ALARMS];
40static u32 CalculateTimeDiff(u32 t1, u32 t2);
41static int InsertAlarm(u32 now, u32 target);
42static s32 SetAlarmInternal(u16 time,
void (*callback)(s32 dispatch_id, u16 time,
void *common),
void *common);
43static void SetupTIM3(u16 ticks);
45static u32 CalculateTimeDiff(u32 t1, u32 t2)
47 return (t2 < t1 ? (0x10000 | t2) : t2);
50static int InsertAlarm(u32 now, u32 target)
55 for (pos = 0; pos < AlarmCount; pos++) {
56 if (target < CalculateTimeDiff(now, alarms[pos].time)) {
57 for (i = AlarmCount - 1; i >= pos; i--)
58 alarms[i + 1] = alarms[i];
67static s32 SetAlarmInternal(u16 time,
void (*callback)(s32 dispatch_id, u16 time,
void *common),
void *common)
77 if (AlarmCount >= MAX_ALARMS)
81 for (i = 0; i < MAX_ALARMS; i++) {
82 if (((AlarmStatus >> i) & 1) == 0) {
83 AlarmStatus |= (1 << i);
93 pos = InsertAlarm(now, target);
96 alarm->target = (u16)target;
99 alarm->callback = callback;
100 alarm->common = common;
102 SetupTIM3(alarms[0].target);
107s32 ReleaseAlarm(s32
id)
114 for (i = 0; i < AlarmCount; i++) {
115 if (alarms[i].
id ==
id) {
116 if (alarms[i].target == *T3_COMP_W) {
121 time = alarms[i].time;
123 for (j = i; j < AlarmCount - 1; j++)
124 alarms[j] = alarms[j + 1];
127 AlarmStatus &= ~(1 << id);
130 SetupTIM3(alarms[0].target);
133 *T3_MODE_W = Tn_MODE(3, 0, 0, 0, 0, 1, 0, 0, 0, 0);
135 result = (CalculateTimeDiff(time, *T3_COUNT_W) - time);
143s32 SetAlarm(u16 time,
void (*callback)(s32 dispatch_id, u16 time,
void *common),
void *common)
147 result = SetAlarmInternal(time, callback, common);
153static void SetupTIM3(u16 ticks)
157 *T3_MODE_W = Tn_MODE(3, 0, 0, 0, 0, 1, 1, 0, 1, 0);
161void Intc12Handler(
void)
166 for (i = 0; i < AlarmCount; i++) {
167 if (alarms[i].target != alarms[0].target) {
168 SetupTIM3(alarms[i].target);
178 for (i = 0; i < AlarmCount; i++)
179 alarms[i] = alarms[i + 1];
181 gp = ChangeGP(
alarm.gp);
182 AlarmStatus &= ~(1 <<
alarm.id);
183 InvokeUserModeCallback((
void *)USER_MODE_DISPATCHER,
alarm.callback,
alarm.id,
alarm.target,
alarm.common);
188 }
while (alarms[0].target ==
alarm.target);
191 *T3_MODE_W = Tn_MODE(3, 0, 0, 0, 0, 1, 0, 0, 1, 0);
193 SetupTIM3(alarms[0].target);