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 "los_tick.h"
35 #include "los_arch_interrupt.h"
36 #include "los_debug.h"
37
38 STATIC UINT32 SysTickStart(HWI_PROC_FUNC handler);
39 STATIC UINT64 SysTickReload(UINT64 nextResponseTime);
40 STATIC UINT64 SysTickCycleGet(UINT32 *period);
41 STATIC VOID SysTickLock(VOID);
42 STATIC VOID SysTickUnlock(VOID);
43
44 STATIC ArchTickTimer g_archTickTimer = {
45 .freq = 0,
46 .irqNum = SysTick_IRQn,
47 .periodMax = LOSCFG_BASE_CORE_TICK_RESPONSE_MAX,
48 .init = SysTickStart,
49 .getCycle = SysTickCycleGet,
50 .reload = SysTickReload,
51 .lock = SysTickLock,
52 .unlock = SysTickUnlock,
53 .tickHandler = NULL,
54 };
55
SysTickStart(HWI_PROC_FUNC handler)56 STATIC UINT32 SysTickStart(HWI_PROC_FUNC handler)
57 {
58 UINT32 ret;
59 ArchTickTimer *tick = &g_archTickTimer;
60
61 tick->freq = OS_SYS_CLOCK;
62
63 #if (LOSCFG_USE_SYSTEM_DEFINED_INTERRUPT == 1)
64 #if (LOSCFG_PLATFORM_HWI_WITH_ARG == 1)
65 OsSetVector(tick->irqNum, handler, NULL);
66 #else
67 OsSetVector(tick->irqNum, handler);
68 #endif
69 #endif
70
71 ret = SysTick_Config(LOSCFG_BASE_CORE_TICK_RESPONSE_MAX);
72 if (ret == 1) {
73 return LOS_ERRNO_TICK_PER_SEC_TOO_SMALL;
74 }
75
76 return LOS_OK;
77 }
78
SysTickReload(UINT64 nextResponseTime)79 STATIC UINT64 SysTickReload(UINT64 nextResponseTime)
80 {
81 if (nextResponseTime > g_archTickTimer.periodMax) {
82 nextResponseTime = g_archTickTimer.periodMax;
83 }
84 SysTick->CTRL &= ~SysTick_CTRL_ENABLE_Msk;
85 SysTick->LOAD = (UINT32)(nextResponseTime - 1UL); /* set reload register */
86 SysTick->VAL = 0UL; /* Load the SysTick Counter Value */
87 NVIC_ClearPendingIRQ(SysTick_IRQn);
88 SysTick->CTRL |= SysTick_CTRL_ENABLE_Msk;
89 return nextResponseTime;
90 }
91
SysTickCycleGet(UINT32 * period)92 STATIC UINT64 SysTickCycleGet(UINT32 *period)
93 {
94 UINT32 hwCycle = 0;
95 UINT32 intSave = LOS_IntLock();
96 UINT32 val = SysTick->VAL;
97 *period = SysTick->LOAD;
98 if (val != 0) {
99 hwCycle = *period - val;
100 }
101 LOS_IntRestore(intSave);
102 return (UINT64)hwCycle;
103 }
104
SysTickLock(VOID)105 STATIC VOID SysTickLock(VOID)
106 {
107 SysTick->CTRL &= ~SysTick_CTRL_ENABLE_Msk;
108 }
109
SysTickUnlock(VOID)110 STATIC VOID SysTickUnlock(VOID)
111 {
112 SysTick->CTRL |= SysTick_CTRL_ENABLE_Msk;
113 }
114
ArchSysTickTimerGet(VOID)115 ArchTickTimer *ArchSysTickTimerGet(VOID)
116 {
117 return &g_archTickTimer;
118 }
119
ArchEnterSleep(VOID)120 UINT32 ArchEnterSleep(VOID)
121 {
122 __DSB();
123 __WFI();
124 __ISB();
125
126 return LOS_OK;
127 }
128