summaryrefslogtreecommitdiff
path: root/bsp/airm2m/air105/libraries/HAL_Driver/Src/core_tick.c
blob: 01f293a134ffa785699e0f324ce33395434879f8 (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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/*
 * Copyright (c) 2022 OpenLuat & AirM2M
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy of
 * this software and associated documentation files (the "Software"), to deal in
 * the Software without restriction, including without limitation the rights to
 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
 * the Software, and to permit persons to whom the Software is furnished to do so,
 * subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

#include "bl_inc.h"
volatile static uint64_t PowerOnTickCnt;
volatile static uint64_t prvPeriod;
//
static void SystemTickIrqHandler( int32_t Line, void *pData)
{
    volatile uint32_t clr;
    clr = TIMM0->TIM[CORE_TICK_TIM].EOI;
    PowerOnTickCnt += prvPeriod;
}

uint64_t GetSysTick(void)
{
    volatile uint32_t clr;
    volatile uint64_t PowerOnTick;
    ISR_OnOff(CORE_TICK_IRQ, 0);
    PowerOnTick = PowerOnTickCnt + prvPeriod - TIMM0->TIM[CORE_TICK_TIM].CurrentValue;
    if (TIMM0->TIM[CORE_TICK_TIM].IntStatus)
    {
        ISR_OnOff(CORE_TICK_IRQ, 1);
        while(TIMM0->TIM[CORE_TICK_TIM].IntStatus){;}
        PowerOnTick = PowerOnTickCnt + prvPeriod - TIMM0->TIM[CORE_TICK_TIM].CurrentValue;
    }
    ISR_OnOff(CORE_TICK_IRQ, 1);
    return PowerOnTick;
}

uint64_t GetSysTickUS(void)
{
    return GetSysTick()/CORE_TICK_1US;
}

uint64_t GetSysTickMS(void)
{
    return GetSysTick()/CORE_TICK_1MS;
}


void SysTickDelay(uint32_t Tick)
{
    uint64_t Tick1 = GetSysTick();
    uint64_t Tick2 = Tick1 + Tick;
    while(Tick2 > GetSysTick()){;}
    return;
}

void SysTickDelayUntil(uint64_t StartTick, uint32_t Tick)
{
    uint64_t Tick1 = StartTick + Tick;
    while(Tick1 > GetSysTick()){;}
    return;
}

uint8_t SysTickCheckTimeout(uint64_t StartTick, uint32_t Tick)
{
    uint64_t Tick1 = StartTick + Tick;
    if (Tick1 > GetSysTick())
    {
        return 0;
    }
    return 1;
}

void CoreTick_Init(void)
{
    prvPeriod = SystemCoreClock;
    TIMM0->TIM[CORE_TICK_TIM].ControlReg = 0;
    TIMM0->TIM[CORE_TICK_TIM].LoadCount = prvPeriod - 1;
    TIMM0->TIM[CORE_TICK_TIM].ControlReg = TIMER_CONTROL_REG_TIMER_ENABLE|TIMER_CONTROL_REG_TIMER_MODE;
    ISR_OnOff(CORE_TICK_IRQ, 0);
    ISR_SetHandler(CORE_TICK_IRQ, SystemTickIrqHandler, NULL);
    ISR_SetPriority(CORE_TICK_IRQ, CORE_TICK_IRQ_LEVEL);
    ISR_OnOff(CORE_TICK_IRQ, 1);
}