summaryrefslogtreecommitdiff
path: root/arm7/lib/src/OS_tick.c
blob: bc42a54f3a801f0856febd959fb9698cc2dad9bb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include "OS_interrupt.h"
#include "OS_system.h"
#include "OS_tick.h"
#include "OS_timer.h"
#include "function_target.h"

static u16 OSi_UseTick;
static OSTick OSi_TickCounter;
static BOOL OSi_NeedResetTimer;

ARM_FUNC void OS_InitTick(void) {
    if (OSi_UseTick == 0) {
        OSi_UseTick = 1;
        OSi_SetTimerReserved(0);
        OSi_TickCounter = 0;
        OS_SetTimerControl(OS_TIMER_0, 0);
        OS_SetTimerCount(OS_TIMER_0, 0);
        OS_SetTimerControl(OS_TIMER_0, 0xc1);
        OS_SetIrqFunction(8, OSi_CountUpTick);
        (void)OS_EnableIrqMask(8);
        OSi_NeedResetTimer = 0;
    }
}

ARM_FUNC u16 OS_IsTickAvailable(void) {
    return OSi_UseTick;
}

ARM_FUNC void OSi_CountUpTick(void) {
    OSi_TickCounter++;
    if (OSi_NeedResetTimer != 0) {
        OS_SetTimerControl(OS_TIMER_0, 0);
        OS_SetTimerCount(OS_TIMER_0, 0);
        OS_SetTimerControl(OS_TIMER_0, 0xc1);
        OSi_NeedResetTimer = 0;
    }
    OSi_EnterTimerCallback(0, (void(*)(void*))OSi_CountUpTick, NULL);
}

ARM_FUNC OSTick OS_GetTick(void) {
    OSIntrMode prev = OS_DisableInterrupts();
    vu16 countL = *(REGType16 *)((u32)&reg_OS_TM0CNT_L + OS_TIMER_0 * 4);
    vu64 countH = OSi_TickCounter & 0xffffffffffffULL;

    if (reg_OS_IF & 8 && !(countL & 0x8000)) {
        countH++;
    }

    (void)OS_RestoreInterrupts(prev);

    return (countH << 16) | countL;
}