1 /*
2 * Copyright (c) 2022 OpenLuat & AirM2M
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a copy of
5 * this software and associated documentation files (the "Software"), to deal in
6 * the Software without restriction, including without limitation the rights to
7 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
8 * the Software, and to permit persons to whom the Software is furnished to do so,
9 * subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in all
12 * copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
16 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
17 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
18 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
19 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20 */
21
22 #include "bl_inc.h"
23 volatile static uint64_t PowerOnTickCnt;
24 volatile static uint64_t prvPeriod;
25 //
SystemTickIrqHandler(int32_t Line,void * pData)26 static void SystemTickIrqHandler( int32_t Line, void *pData)
27 {
28 volatile uint32_t clr;
29 clr = TIMM0->TIM[CORE_TICK_TIM].EOI;
30 PowerOnTickCnt += prvPeriod;
31 }
32
GetSysTick(void)33 uint64_t GetSysTick(void)
34 {
35 volatile uint32_t clr;
36 volatile uint64_t PowerOnTick;
37 ISR_OnOff(CORE_TICK_IRQ, 0);
38 PowerOnTick = PowerOnTickCnt + prvPeriod - TIMM0->TIM[CORE_TICK_TIM].CurrentValue;
39 if (TIMM0->TIM[CORE_TICK_TIM].IntStatus)
40 {
41 ISR_OnOff(CORE_TICK_IRQ, 1);
42 while(TIMM0->TIM[CORE_TICK_TIM].IntStatus){;}
43 PowerOnTick = PowerOnTickCnt + prvPeriod - TIMM0->TIM[CORE_TICK_TIM].CurrentValue;
44 }
45 ISR_OnOff(CORE_TICK_IRQ, 1);
46 return PowerOnTick;
47 }
48
GetSysTickUS(void)49 uint64_t GetSysTickUS(void)
50 {
51 return GetSysTick()/CORE_TICK_1US;
52 }
53
GetSysTickMS(void)54 uint64_t GetSysTickMS(void)
55 {
56 return GetSysTick()/CORE_TICK_1MS;
57 }
58
59
SysTickDelay(uint32_t Tick)60 void SysTickDelay(uint32_t Tick)
61 {
62 uint64_t Tick1 = GetSysTick();
63 uint64_t Tick2 = Tick1 + Tick;
64 while(Tick2 > GetSysTick()){;}
65 return;
66 }
67
SysTickDelayUntil(uint64_t StartTick,uint32_t Tick)68 void SysTickDelayUntil(uint64_t StartTick, uint32_t Tick)
69 {
70 uint64_t Tick1 = StartTick + Tick;
71 while(Tick1 > GetSysTick()){;}
72 return;
73 }
74
SysTickCheckTimeout(uint64_t StartTick,uint32_t Tick)75 uint8_t SysTickCheckTimeout(uint64_t StartTick, uint32_t Tick)
76 {
77 uint64_t Tick1 = StartTick + Tick;
78 if (Tick1 > GetSysTick())
79 {
80 return 0;
81 }
82 return 1;
83 }
84
CoreTick_Init(void)85 void CoreTick_Init(void)
86 {
87 prvPeriod = SystemCoreClock;
88 TIMM0->TIM[CORE_TICK_TIM].ControlReg = 0;
89 TIMM0->TIM[CORE_TICK_TIM].LoadCount = prvPeriod - 1;
90 TIMM0->TIM[CORE_TICK_TIM].ControlReg = TIMER_CONTROL_REG_TIMER_ENABLE|TIMER_CONTROL_REG_TIMER_MODE;
91 ISR_OnOff(CORE_TICK_IRQ, 0);
92 ISR_SetHandler(CORE_TICK_IRQ, SystemTickIrqHandler, NULL);
93 ISR_SetPriority(CORE_TICK_IRQ, CORE_TICK_IRQ_LEVEL);
94 ISR_OnOff(CORE_TICK_IRQ, 1);
95 }
96