1 //*****************************************************************************
2 //
3 //! @file am_hal_systick.c
4 //!
5 //! @brief Functions for accessing and configuring the SYSTICK.
6 //!
7 //! @addtogroup systick3 SYSTICK - System Timer
8 //! @ingroup apollo3_hal
9 //! @{
10 //
11 //*****************************************************************************
12
13 //*****************************************************************************
14 //
15 // Copyright (c) 2024, Ambiq Micro, Inc.
16 // All rights reserved.
17 //
18 // Redistribution and use in source and binary forms, with or without
19 // modification, are permitted provided that the following conditions are met:
20 //
21 // 1. Redistributions of source code must retain the above copyright notice,
22 // this list of conditions and the following disclaimer.
23 //
24 // 2. Redistributions in binary form must reproduce the above copyright
25 // notice, this list of conditions and the following disclaimer in the
26 // documentation and/or other materials provided with the distribution.
27 //
28 // 3. Neither the name of the copyright holder nor the names of its
29 // contributors may be used to endorse or promote products derived from this
30 // software without specific prior written permission.
31 //
32 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
33 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
34 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
35 // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
36 // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
37 // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
38 // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
39 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
40 // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
41 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
42 // POSSIBILITY OF SUCH DAMAGE.
43 //
44 // This is part of revision release_sdk_3_2_0-dd5f40c14b of the AmbiqSuite Development Package.
45 //
46 //*****************************************************************************
47
48 #include <stdint.h>
49 #include <stdbool.h>
50 #include "am_mcu_apollo.h"
51
52
53 //*****************************************************************************
54 //
55 // Macro definitions
56 //
57 //*****************************************************************************
58 #define SYSTICK_MAX_TICKS ((1 << 24)-1)
59 #define MAX_U32 (0xffffffff)
60
61 //*****************************************************************************
62 //
63 // @brief Start the SYSTICK.
64 //
65 // This function starts the systick timer.
66 //
67 // @note This timer does not run in deep-sleep mode as it runs from the core
68 // clock, which is gated in deep-sleep. If a timer is needed in deep-sleep use
69 // one of the ctimers instead. Also to note is this timer will consume higher
70 // power than the ctimers.
71 //
72 //*****************************************************************************
73 void
am_hal_systick_start(void)74 am_hal_systick_start(void)
75 {
76 //
77 // Start the systick timer.
78 //
79 SysTick->CTRL |= SysTick_CTRL_ENABLE_Msk;
80 }
81
82 //*****************************************************************************
83 //
84 // @brief Stop the SYSTICK.
85 //
86 // This function stops the systick timer.
87 //
88 // @note This timer does not run in deep-sleep mode as it runs from the core
89 // clock, which is gated in deep-sleep. If a timer is needed in deep-sleep use
90 // one of the ctimers instead. Also to note is this timer will consume higher
91 // power than the ctimers.
92 //
93 //*****************************************************************************
94 void
am_hal_systick_stop(void)95 am_hal_systick_stop(void)
96 {
97 //
98 // Stop the systick timer.
99 //
100 SysTick->CTRL &= ~SysTick_CTRL_ENABLE_Msk;
101 }
102
103 //*****************************************************************************
104 //
105 // @brief Enable the interrupt in the SYSTICK.
106 //
107 // This function enables the interupt in the systick timer.
108 //
109 //*****************************************************************************
110 void
am_hal_systick_int_enable(void)111 am_hal_systick_int_enable(void)
112 {
113 //
114 // Enable the systick timer interrupt.
115 //
116 SysTick->CTRL |= SysTick_CTRL_TICKINT_Msk;
117 }
118
119 //*****************************************************************************
120 //
121 // @brief Disable the interrupt in the SYSTICK.
122 //
123 // This function disables the interupt in the systick timer.
124 //
125 //*****************************************************************************
126 void
am_hal_systick_int_disable(void)127 am_hal_systick_int_disable(void)
128 {
129 //
130 // Disable the systick timer interrupt.
131 //
132 SysTick->CTRL &= ~SysTick_CTRL_TICKINT_Msk;
133 }
134
135 //*****************************************************************************
136 //
137 // @brief Reads the interrupt status.
138 //
139 // This function reads the interrupt status in the systick timer.
140 //
141 // @return the interrupt status.
142 //
143 //*****************************************************************************
144 uint32_t
am_hal_systick_int_status_get(void)145 am_hal_systick_int_status_get(void)
146 {
147 //
148 // Return the systick timer interrupt status.
149 //
150 return SysTick->CTRL & SysTick_CTRL_COUNTFLAG_Msk;
151 }
152
153 //*****************************************************************************
154 //
155 // @brief Reset the interrupt in the SYSTICK.
156 //
157 // This function resets the systick timer by clearing out the configuration
158 // register.
159 //
160 //*****************************************************************************
161 void
am_hal_systick_reset(void)162 am_hal_systick_reset(void)
163 {
164 //
165 // Reset the systick timer interrupt.
166 //
167 SysTick->CTRL = 0x0;
168 }
169
170 //*****************************************************************************
171 //
172 // @brief Load the value into the SYSTICK.
173 //
174 // @param ui32LoadVal the desired load value for the systick. Maximum value is
175 // 0x00FF.FFFF.
176 //
177 // This function loads the desired value into the systick timer.
178 //
179 //*****************************************************************************
180 void
am_hal_systick_load(uint32_t ui32LoadVal)181 am_hal_systick_load(uint32_t ui32LoadVal)
182 {
183 //
184 // The proper SysTick initialization sequence is: (p 4-36 of the M4 UG).
185 // 1. Program reload value
186 // 2. Clear current value
187 // 3. Program CSR
188 // Write the given value to the reload register.
189 // Write the Current Value Register to clear it to 0.
190 //
191 SysTick->LOAD = ui32LoadVal;
192 SysTick->VAL = 0;
193 }
194
195 //*****************************************************************************
196 //
197 // @brief Get the current count value in the SYSTICK.
198 //
199 // This function gets the current count value in the systick timer.
200 //
201 // @return Current count value.
202 //
203 //*****************************************************************************
204 uint32_t
am_hal_systick_count(void)205 am_hal_systick_count(void)
206 {
207 //
208 // Return the current systick timer count value.
209 //
210 return SysTick->VAL;
211 }
212
213 //*****************************************************************************
214 //
215 // @brief Wait the specified number of ticks.
216 //
217 // This function delays for the given number of SysTick ticks.
218 //
219 // @note If the SysTick timer is being used elsewhere, it will be corrupted
220 // by calling this function.
221 //
222 // @return 0 if successful.
223 //
224 //*****************************************************************************
225 uint32_t
am_hal_systick_wait_ticks(uint32_t ui32Ticks)226 am_hal_systick_wait_ticks(uint32_t ui32Ticks)
227 {
228
229 if ( ui32Ticks == 0 )
230 {
231 ui32Ticks++; // Make sure we get the COUNTFLAG
232 }
233
234 //
235 // The proper SysTick initialization sequence is: (p 4-36 of the M4 UG).
236 // 1. Program reload value
237 // 2. Clear current value
238 // 3. Program CSR
239 //
240 // Set the reload value to the required number of ticks.
241 //
242 SysTick->LOAD = ui32Ticks;
243
244 //
245 // Clear the current count.
246 //
247 SysTick->VAL = 0x0;
248
249 //
250 // Set to use the processor clock, but don't cause an exception (we'll poll).
251 //
252 SysTick->CTRL = SysTick_CTRL_ENABLE_Msk;
253
254 //
255 // Poll till done
256 //
257 while ( !(SysTick->CTRL & SysTick_CTRL_COUNTFLAG_Msk) );
258
259 //
260 // And disable systick before exiting.
261 //
262 SysTick->CTRL = 0x0;
263
264 return 0;
265 }
266
267 //*****************************************************************************
268 //
269 // @brief Delay the specified number of microseconds.
270 //
271 // This function will use the SysTick timer to delay until the specified
272 // number of microseconds have elapsed. It uses the processor clocks and
273 // takes into account the current CORESEL setting.
274 //
275 // @note If the SysTick timer is being used elsewhere, it will be corrupted
276 // by calling this function.
277 //
278 // @return Total number of SysTick ticks delayed.
279 //
280 //*****************************************************************************
281 uint32_t
am_hal_systick_delay_us(uint32_t ui32NumUs)282 am_hal_systick_delay_us(uint32_t ui32NumUs)
283 {
284 uint32_t ui32nLoops, ui32Ticks, uRet;
285 uint32_t ui32ClkFreq, ui32TicksPerMHz;
286 uint32_t ui32CoreSel = CLKGEN->CCTRL_b.CORESEL;
287
288 ui32nLoops = 0;
289 if ( (ui32CoreSel <= AM_HAL_CLKGEN_CORESEL_MAXDIV) && (ui32NumUs >= 2) )
290 {
291 //
292 // Determine clock freq, then whether we need more than 1 iteration.
293 //
294 ui32ClkFreq = AM_HAL_CLKGEN_FREQ_MAX_MHZ >> ui32CoreSel;
295 ui32ClkFreq <<= (am_hal_burst_mode_status() == AM_HAL_BURST_MODE)? 1 : 0;
296
297 ui32TicksPerMHz = SYSTICK_MAX_TICKS / ui32ClkFreq;
298 if ( ui32NumUs > ui32TicksPerMHz )
299 {
300 //
301 // Get number of required loops, as well as additional ticks.
302 //
303 ui32nLoops = ui32NumUs / ui32TicksPerMHz;
304 ui32NumUs = ui32NumUs % ui32TicksPerMHz;
305 }
306
307 //
308 // Compute the number of ticks required.
309 // Allow for about 2us of call overhead.
310 //
311 ui32Ticks = (ui32NumUs - 2) * ui32ClkFreq;
312 }
313 else
314 {
315 ui32Ticks = 1;
316 }
317
318 uRet = (ui32nLoops * SYSTICK_MAX_TICKS) + ui32Ticks;
319 while ( ui32nLoops )
320 {
321 am_hal_systick_wait_ticks(SYSTICK_MAX_TICKS);
322 ui32nLoops--;
323 }
324 am_hal_systick_wait_ticks(ui32Ticks);
325
326 return uRet;
327 }
328
329 //*****************************************************************************
330 //
331 // End Doxygen group.
332 //! @}
333 //
334 //*****************************************************************************
335