1 /*
2 * Copyright (c) 2015-2016, Freescale Semiconductor, Inc.
3 * Copyright 2016-2021 NXP
4 * All rights reserved.
5 *
6 * SPDX-License-Identifier: BSD-3-Clause
7 */
8
9 #include "fsl_common.h"
10
11 /* Component ID definition, used by tools. */
12 #ifndef FSL_COMPONENT_ID
13 #define FSL_COMPONENT_ID "platform.drivers.common_arm"
14 #endif
15
16 #ifndef __GIC_PRIO_BITS
17 #if defined(ENABLE_RAM_VECTOR_TABLE)
InstallIRQHandler(IRQn_Type irq,uint32_t irqHandler)18 uint32_t InstallIRQHandler(IRQn_Type irq, uint32_t irqHandler)
19 {
20 #ifdef __VECTOR_TABLE
21 #undef __VECTOR_TABLE
22 #endif
23
24 /* Addresses for VECTOR_TABLE and VECTOR_RAM come from the linker file */
25 #if defined(__CC_ARM) || defined(__ARMCC_VERSION)
26 extern uint32_t Image$$VECTOR_ROM$$Base[];
27 extern uint32_t Image$$VECTOR_RAM$$Base[];
28 extern uint32_t Image$$RW_m_data$$Base[];
29
30 #define __VECTOR_TABLE Image$$VECTOR_ROM$$Base
31 #define __VECTOR_RAM Image$$VECTOR_RAM$$Base
32 #define __RAM_VECTOR_TABLE_SIZE (((uint32_t)Image$$RW_m_data$$Base - (uint32_t)Image$$VECTOR_RAM$$Base))
33 #elif defined(__ICCARM__)
34 extern uint32_t __RAM_VECTOR_TABLE_SIZE[];
35 extern uint32_t __VECTOR_TABLE[];
36 extern uint32_t __VECTOR_RAM[];
37 #elif defined(__GNUC__)
38 extern uint32_t __VECTOR_TABLE[];
39 extern uint32_t __VECTOR_RAM[];
40 extern uint32_t __RAM_VECTOR_TABLE_SIZE_BYTES[];
41 uint32_t __RAM_VECTOR_TABLE_SIZE = (uint32_t)(__RAM_VECTOR_TABLE_SIZE_BYTES);
42 #endif /* defined(__CC_ARM) || defined(__ARMCC_VERSION) */
43 uint32_t n;
44 uint32_t ret;
45 uint32_t irqMaskValue;
46
47 irqMaskValue = DisableGlobalIRQ();
48 if (SCB->VTOR != (uint32_t)__VECTOR_RAM)
49 {
50 /* Copy the vector table from ROM to RAM */
51 for (n = 0; n < ((uint32_t)__RAM_VECTOR_TABLE_SIZE) / sizeof(uint32_t); n++)
52 {
53 __VECTOR_RAM[n] = __VECTOR_TABLE[n];
54 }
55 /* Point the VTOR to the position of vector table */
56 SCB->VTOR = (uint32_t)__VECTOR_RAM;
57 }
58
59 ret = __VECTOR_RAM[(int32_t)irq + 16];
60 /* make sure the __VECTOR_RAM is noncachable */
61 __VECTOR_RAM[(int32_t)irq + 16] = irqHandler;
62
63 EnableGlobalIRQ(irqMaskValue);
64
65 return ret;
66 }
67 #endif /* ENABLE_RAM_VECTOR_TABLE. */
68 #endif /* __GIC_PRIO_BITS. */
69
70 #if (defined(FSL_FEATURE_SOC_SYSCON_COUNT) && (FSL_FEATURE_SOC_SYSCON_COUNT > 0))
71
72 /*
73 * When FSL_FEATURE_POWERLIB_EXTEND is defined to non-zero value,
74 * powerlib should be used instead of these functions.
75 */
76 #if !(defined(FSL_FEATURE_POWERLIB_EXTEND) && (FSL_FEATURE_POWERLIB_EXTEND != 0))
77
78 /*
79 * When the SYSCON STARTER registers are discontinuous, these functions are
80 * implemented in fsl_power.c.
81 */
82 #if !(defined(FSL_FEATURE_SYSCON_STARTER_DISCONTINUOUS) && FSL_FEATURE_SYSCON_STARTER_DISCONTINUOUS)
83
EnableDeepSleepIRQ(IRQn_Type interrupt)84 void EnableDeepSleepIRQ(IRQn_Type interrupt)
85 {
86 uint32_t intNumber = (uint32_t)interrupt;
87
88 uint32_t index = 0;
89
90 while (intNumber >= 32u)
91 {
92 index++;
93 intNumber -= 32u;
94 }
95
96 SYSCON->STARTERSET[index] = 1UL << intNumber;
97 (void)EnableIRQ(interrupt); /* also enable interrupt at NVIC */
98 }
99
DisableDeepSleepIRQ(IRQn_Type interrupt)100 void DisableDeepSleepIRQ(IRQn_Type interrupt)
101 {
102 uint32_t intNumber = (uint32_t)interrupt;
103
104 (void)DisableIRQ(interrupt); /* also disable interrupt at NVIC */
105 uint32_t index = 0;
106
107 while (intNumber >= 32u)
108 {
109 index++;
110 intNumber -= 32u;
111 }
112
113 SYSCON->STARTERCLR[index] = 1UL << intNumber;
114 }
115 #endif /* FSL_FEATURE_SYSCON_STARTER_DISCONTINUOUS */
116 #endif /* FSL_FEATURE_POWERLIB_EXTEND */
117 #endif /* FSL_FEATURE_SOC_SYSCON_COUNT */
118
119 #if defined(DWT)
120 /* Use WDT. */
MSDK_EnableCpuCycleCounter(void)121 void MSDK_EnableCpuCycleCounter(void)
122 {
123 /* Make sure the DWT trace fucntion is enabled. */
124 if (CoreDebug_DEMCR_TRCENA_Msk != (CoreDebug_DEMCR_TRCENA_Msk & CoreDebug->DEMCR))
125 {
126 CoreDebug->DEMCR |= CoreDebug_DEMCR_TRCENA_Msk;
127 }
128
129 /* CYCCNT not supported on this device. */
130 assert(DWT_CTRL_NOCYCCNT_Msk != (DWT->CTRL & DWT_CTRL_NOCYCCNT_Msk));
131
132 /* Read CYCCNT directly if CYCCENT has already been enabled, otherwise enable CYCCENT first. */
133 if (DWT_CTRL_CYCCNTENA_Msk != (DWT_CTRL_CYCCNTENA_Msk & DWT->CTRL))
134 {
135 DWT->CTRL |= DWT_CTRL_CYCCNTENA_Msk;
136 }
137 }
138
MSDK_GetCpuCycleCount(void)139 uint32_t MSDK_GetCpuCycleCount(void)
140 {
141 return DWT->CYCCNT;
142 }
143 #endif /* defined(DWT) */
144
145 #if !(defined(SDK_DELAY_USE_DWT) && defined(DWT))
146 /* Use software loop. */
147 #if defined(__CC_ARM) /* This macro is arm v5 specific */
148 /* clang-format off */
DelayLoop(uint32_t count)149 __ASM static void DelayLoop(uint32_t count)
150 {
151 loop
152 SUBS R0, R0, #1
153 CMP R0, #0
154 BNE loop
155 BX LR
156 }
157 #elif defined(__ARM_ARCH_8A__) /* This macro is ARMv8-A specific */
DelayLoop(uint32_t count)158 static void DelayLoop(uint32_t count)
159 {
160 __ASM volatile(" MOV X0, %0" : : "r"(count));
161 __ASM volatile(
162 "loop: \n"
163 " SUB X0, X0, #1 \n"
164 " CMP X0, #0 \n"
165
166 " BNE loop \n"
167 :
168 :
169 : "r0");
170 }
171 /* clang-format on */
172 #elif defined(__ARMCC_VERSION) || defined(__ICCARM__) || defined(__GNUC__)
173 /* Cortex-M0 has a smaller instruction set, SUBS isn't supported in thumb-16 mode reported from __GNUC__ compiler,
174 * use SUB and CMP here for compatibility */
DelayLoop(uint32_t count)175 static void DelayLoop(uint32_t count)
176 {
177 __ASM volatile(" MOV R0, %0" : : "r"(count));
178 __ASM volatile(
179 "loop: \n"
180 #if defined(__GNUC__) && !defined(__ARMCC_VERSION)
181 " SUB R0, R0, #1 \n"
182 #else
183 " SUBS R0, R0, #1 \n"
184 #endif
185 " CMP R0, #0 \n"
186
187 " BNE loop \n"
188 :
189 :
190 : "r0");
191 }
192 #endif /* defined(__CC_ARM) */
193 #endif /* defined(SDK_DELAY_USE_DWT) && defined(DWT) */
194
195 /*!
196 * @brief Delay at least for some time.
197 * Please note that, if not uses DWT, this API will use while loop for delay, different run-time environments have
198 * effect on the delay time. If precise delay is needed, please enable DWT delay. The two parmeters delayTime_us and
199 * coreClock_Hz have limitation. For example, in the platform with 1GHz coreClock_Hz, the delayTime_us only supports
200 * up to 4294967 in current code. If long time delay is needed, please implement a new delay function.
201 *
202 * @param delayTime_us Delay time in unit of microsecond.
203 * @param coreClock_Hz Core clock frequency with Hz.
204 */
SDK_DelayAtLeastUs(uint32_t delayTime_us,uint32_t coreClock_Hz)205 void SDK_DelayAtLeastUs(uint32_t delayTime_us, uint32_t coreClock_Hz)
206 {
207 uint64_t count;
208
209 if (delayTime_us > 0U)
210 {
211 count = USEC_TO_COUNT(delayTime_us, coreClock_Hz);
212
213 assert(count <= UINT32_MAX);
214
215 #if defined(SDK_DELAY_USE_DWT) && defined(DWT) /* Use DWT for better accuracy */
216
217 MSDK_EnableCpuCycleCounter();
218 /* Calculate the count ticks. */
219 count += MSDK_GetCpuCycleCount();
220
221 if (count > UINT32_MAX)
222 {
223 count -= UINT32_MAX;
224 /* Wait for cyccnt overflow. */
225 while (count < MSDK_GetCpuCycleCount())
226 {
227 }
228 }
229
230 /* Wait for cyccnt reach count value. */
231 while (count > MSDK_GetCpuCycleCount())
232 {
233 }
234 #else
235 /* Divide value may be different in various environment to ensure delay is precise.
236 * Every loop count includes three instructions, due to Cortex-M7 sometimes executes
237 * two instructions in one period, through test here set divide 1.5. Other M cores use
238 * divide 4. By the way, divide 1.5 or 4 could let the count lose precision, but it does
239 * not matter because other instructions outside while loop is enough to fill the time.
240 */
241 #if (__CORTEX_M == 7)
242 count = count / 3U * 2U;
243 #else
244 count = count / 4U;
245 #endif
246 DelayLoop((uint32_t)count);
247 #endif /* defined(SDK_DELAY_USE_DWT) && defined(DWT) */
248 }
249 }
250