1 /*
2 * Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
3 * Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without modification,
6 * are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice, this list of
9 * conditions and the following disclaimer.
10 *
11 * 2. Redistributions in binary form must reproduce the above copyright notice, this list
12 * of conditions and the following disclaimer in the documentation and/or other materials
13 * provided with the distribution.
14 *
15 * 3. Neither the name of the copyright holder nor the names of its contributors may be used
16 * to endorse or promote products derived from this software without specific prior written
17 * permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
26 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
27 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
28 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
29 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 #include "los_timer.h"
33 #include "los_config.h"
34 #include "ARMCM3.h"
35 #include "los_tick.h"
36 #include "los_arch_interrupt.h"
37 #include "los_debug.h"
38
39 STATIC UINT32 SysTickStart(HWI_PROC_FUNC handler);
40 STATIC UINT64 SysTickReload(UINT64 nextResponseTime);
41 STATIC UINT64 SysTickCycleGet(UINT32 *period);
42 STATIC VOID SysTickLock(VOID);
43 STATIC VOID SysTickUnlock(VOID);
44
45 STATIC ArchTickTimer g_archTickTimer = {
46 .freq = 0,
47 .irqNum = SysTick_IRQn,
48 .periodMax = LOSCFG_BASE_CORE_TICK_RESPONSE_MAX,
49 .init = SysTickStart,
50 .getCycle = SysTickCycleGet,
51 .reload = SysTickReload,
52 .lock = SysTickLock,
53 .unlock = SysTickUnlock,
54 .tickHandler = NULL,
55 };
56
SysTickStart(HWI_PROC_FUNC handler)57 STATIC UINT32 SysTickStart(HWI_PROC_FUNC handler)
58 {
59 UINT32 ret;
60 ArchTickTimer *tick = &g_archTickTimer;
61
62 tick->freq = OS_SYS_CLOCK;
63
64 #if (LOSCFG_USE_SYSTEM_DEFINED_INTERRUPT == 1)
65 #if (LOSCFG_PLATFORM_HWI_WITH_ARG == 1)
66 OsSetVector(tick->irqNum, handler, NULL);
67 #else
68 OsSetVector(tick->irqNum, handler);
69 #endif
70 #endif
71
72 ret = SysTick_Config(LOSCFG_BASE_CORE_TICK_RESPONSE_MAX);
73 if (ret == 1) {
74 return LOS_ERRNO_TICK_PER_SEC_TOO_SMALL;
75 }
76
77 return LOS_OK;
78 }
79
SysTickReload(UINT64 nextResponseTime)80 STATIC UINT64 SysTickReload(UINT64 nextResponseTime)
81 {
82 if (nextResponseTime > g_archTickTimer.periodMax) {
83 nextResponseTime = g_archTickTimer.periodMax;
84 }
85
86 SysTick->CTRL &= ~SysTick_CTRL_ENABLE_Msk;
87 SysTick->LOAD = (UINT32)(nextResponseTime - 1UL); /* set reload register */
88 SysTick->VAL = 0UL; /* Load the SysTick Counter Value */
89 NVIC_ClearPendingIRQ(SysTick_IRQn);
90 SysTick->CTRL |= SysTick_CTRL_ENABLE_Msk;
91
92 return nextResponseTime;
93 }
94
SysTickCycleGet(UINT32 * period)95 STATIC UINT64 SysTickCycleGet(UINT32 *period)
96 {
97 UINT32 hwCycle = 0;
98 UINT32 intSave = LOS_IntLock();
99 UINT32 val = SysTick->VAL;
100 *period = SysTick->LOAD;
101 if (val != 0) {
102 hwCycle = *period - val;
103 }
104 LOS_IntRestore(intSave);
105 return (UINT64)hwCycle;
106 }
107
SysTickLock(VOID)108 STATIC VOID SysTickLock(VOID)
109 {
110 SysTick->CTRL &= ~SysTick_CTRL_ENABLE_Msk;
111 }
112
SysTickUnlock(VOID)113 STATIC VOID SysTickUnlock(VOID)
114 {
115 SysTick->CTRL |= SysTick_CTRL_ENABLE_Msk;
116 }
117
ArchSysTickTimerGet(VOID)118 ArchTickTimer *ArchSysTickTimerGet(VOID)
119 {
120 return &g_archTickTimer;
121 }
122
ArchEnterSleep(VOID)123 UINT32 ArchEnterSleep(VOID)
124 {
125 __DSB();
126 __WFI();
127 __ISB();
128
129 return LOS_OK;
130 }
131