1 //*****************************************************************************
2 //
3 //! @file am_hal_systick.c
4 //!
5 //! @brief Functions for interfacing with the SYSTICK
6 //!
7 //! @addtogroup systick4_4p SYSTICK - System Tick Timer
8 //! @ingroup apollo4p_hal
9 //! @{
10 //
11 //*****************************************************************************
12 
13 //*****************************************************************************
14 //
15 // Copyright (c) 2023, 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_4_4_0-3c5977e664 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 // Macro definitions
55 //
56 //*****************************************************************************
57 #define SYSTICK_MAX_TICKS   ((1 << 24)-1)
58 #define MAX_U32             (0xffffffff)
59 
60 //*****************************************************************************
61 //
62 // Start the SYSTICK.
63 //
64 // This function starts the systick timer.
65 //
66 //*****************************************************************************
67 void
am_hal_systick_start(void)68 am_hal_systick_start(void)
69 {
70     //
71     // Start the systick timer.
72     //
73     SysTick->CTRL |= SysTick_CTRL_ENABLE_Msk;
74 }
75 
76 //*****************************************************************************
77 //
78 // Stop the SYSTICK.
79 //
80 // This function stops the systick timer.
81 //
82 //*****************************************************************************
83 void
am_hal_systick_stop(void)84 am_hal_systick_stop(void)
85 {
86     //
87     // Stop the systick timer.
88     //
89     SysTick->CTRL &= ~SysTick_CTRL_ENABLE_Msk;
90 }
91 
92 //*****************************************************************************
93 //
94 // Enable the interrupt in the SYSTICK.
95 //
96 // This function enables the interupt in the systick timer.
97 //
98 //*****************************************************************************
99 void
am_hal_systick_int_enable(void)100 am_hal_systick_int_enable(void)
101 {
102     //
103     // Enable the systick timer interrupt.
104     //
105     SysTick->CTRL |= SysTick_CTRL_TICKINT_Msk;
106 }
107 
108 //*****************************************************************************
109 //
110 // Disable the interrupt in the SYSTICK.
111 //
112 // This function disables the interupt in the systick timer.
113 //
114 //*****************************************************************************
115 void
am_hal_systick_int_disable(void)116 am_hal_systick_int_disable(void)
117 {
118     //
119     // Disable the systick timer interrupt.
120     //
121     SysTick->CTRL &= ~SysTick_CTRL_TICKINT_Msk;
122 }
123 
124 //*****************************************************************************
125 //
126 // Reads the interrupt status.
127 //
128 // This function reads the interrupt status in the systick timer.
129 //
130 //*****************************************************************************
131 uint32_t
am_hal_systick_int_status_get(void)132 am_hal_systick_int_status_get(void)
133 {
134     //
135     // Return the systick timer interrupt status.
136     //
137     return SysTick->CTRL & SysTick_CTRL_COUNTFLAG_Msk;
138 }
139 
140 //*****************************************************************************
141 //
142 // Reset the interrupt in the SYSTICK.
143 //
144 // This function resets the systick timer by clearing out the configuration
145 // register.
146 //
147 //*****************************************************************************
148 void
am_hal_systick_reset(void)149 am_hal_systick_reset(void)
150 {
151     //
152     // Reset the systick timer interrupt.
153     //
154     SysTick->CTRL = 0x0;
155 }
156 
157 //*****************************************************************************
158 //
159 // Load the value into the SYSTICK.
160 //
161 // This function loads the desired value into the systick timer.
162 //
163 //*****************************************************************************
164 void
am_hal_systick_load(uint32_t ui32LoadVal)165 am_hal_systick_load(uint32_t ui32LoadVal)
166 {
167     //
168     //  The proper SysTick initialization sequence is: (p 4-36 of the M4 UG).
169     //      1. Program reload value
170     //      2. Clear current value
171     //      3. Program CSR
172     // Write the given value to the reload register.
173     // Write the Current Value Register to clear it to 0.
174     //
175     SysTick->LOAD = ui32LoadVal;
176     SysTick->VAL = 0;
177 }
178 
179 //*****************************************************************************
180 //
181 // Get the current count value in the SYSTICK.
182 //
183 // This function gets the current count value in the systick timer.
184 //
185 //*****************************************************************************
186 uint32_t
am_hal_systick_count(void)187 am_hal_systick_count(void)
188 {
189     //
190     // Return the current systick timer count value.
191     //
192     return SysTick->VAL;
193 }
194 
195 //*****************************************************************************
196 //
197 // Wait the specified number of ticks.
198 //
199 // This function delays for the given number of SysTick ticks.
200 //
201 //*****************************************************************************
202 uint32_t
am_hal_systick_wait_ticks(uint32_t ui32Ticks)203 am_hal_systick_wait_ticks(uint32_t ui32Ticks)
204 {
205 
206     if ( ui32Ticks == 0 )
207     {
208         ui32Ticks++;                // Make sure we get the COUNTFLAG
209     }
210 
211     //
212     //  The proper SysTick initialization sequence is: (p 4-36 of the M4 UG).
213     //      1. Program reload value
214     //      2. Clear current value
215     //      3. Program CSR
216     //
217     // Set the reload value to the required number of ticks.
218     //
219     SysTick->LOAD = ui32Ticks;
220 
221     //
222     // Clear the current count.
223     //
224     SysTick->VAL = 0x0;
225 
226     //
227     // Set to use the processor clock, but don't cause an exception (we'll poll).
228     //
229     SysTick->CTRL = SysTick_CTRL_ENABLE_Msk;
230 
231     //
232     // Poll till done
233     //
234     while ( !(SysTick->CTRL & SysTick_CTRL_COUNTFLAG_Msk) );
235 
236     //
237     // And disable systick before exiting.
238     //
239     SysTick->CTRL = 0x0;
240 
241     return 0;
242 }
243 
244 //*****************************************************************************
245 //
246 // Delay the specified number of microseconds.
247 //
248 // This function will use the SysTick timer to delay until the specified
249 //  number of microseconds have elapsed.  It uses the processor clocks and
250 //  takes into account the current CORESEL setting.
251 //
252 //*****************************************************************************
253 uint32_t
am_hal_systick_delay_us(uint32_t ui32NumUs)254 am_hal_systick_delay_us(uint32_t ui32NumUs)
255 {
256 #if defined(CLKGEN_DEFINED)
257     uint32_t ui32nLoops, ui32Ticks, uRet;
258     uint32_t ui32ClkFreq, ui32TicksPerMHz;
259 //    uint32_t ui32CoreSel = CLKGEN->CCTRL_b.CORESEL;
260     uint32_t ui32CoreSel = AM_HAL_CLKGEN_CORESEL_MAXDIV;
261 
262     ui32nLoops = 0;
263     if ( (ui32CoreSel <= AM_HAL_CLKGEN_CORESEL_MAXDIV)  &&  (ui32NumUs >= 2) )
264     {
265         //
266         // Determine clock freq, then whether we need more than 1 iteration.
267         //
268         ui32ClkFreq = AM_HAL_CLKGEN_FREQ_MAX_MHZ >> ui32CoreSel;
269 //       ui32ClkFreq <<= (am_hal_burst_mode_status() == AM_HAL_BURST_MODE)? 1 : 0;
270 
271         ui32TicksPerMHz = SYSTICK_MAX_TICKS / ui32ClkFreq;
272         if ( ui32NumUs > ui32TicksPerMHz )
273         {
274             //
275             // Get number of required loops, as well as additional ticks.
276             //
277             ui32nLoops = ui32NumUs / ui32TicksPerMHz;
278             ui32NumUs  = ui32NumUs % ui32TicksPerMHz;
279         }
280 
281         //
282         // Compute the number of ticks required.
283         // Allow for about 2us of call overhead.
284         //
285         ui32Ticks = (ui32NumUs - 2) * ui32ClkFreq;
286     }
287     else
288     {
289         ui32Ticks = 1;
290     }
291 
292     uRet = (ui32nLoops * SYSTICK_MAX_TICKS) + ui32Ticks;
293     while ( ui32nLoops )
294     {
295         am_hal_systick_wait_ticks(SYSTICK_MAX_TICKS);
296         ui32nLoops--;
297     }
298     am_hal_systick_wait_ticks(ui32Ticks);
299     return uRet;
300 #else
301     return AM_HAL_STATUS_SUCCESS;
302 #endif
303 }
304 
305 //*****************************************************************************
306 //
307 // End Doxygen group.
308 //! @}
309 //
310 //*****************************************************************************
311