1 //*****************************************************************************
2 //
3 // am_hal_stimer.c
4 //! @file
5 //!
6 //! @brief Functions for interfacing with the system timer (STIMER).
7 //!
8 //! @addtogroup stimer2 System Timer (STIMER)
9 //! @ingroup apollo2hal
10 //! @{
11 //
12 //*****************************************************************************
13
14 //*****************************************************************************
15 //
16 // Copyright (c) 2017, Ambiq Micro
17 // All rights reserved.
18 //
19 // Redistribution and use in source and binary forms, with or without
20 // modification, are permitted provided that the following conditions are met:
21 //
22 // 1. Redistributions of source code must retain the above copyright notice,
23 // this list of conditions and the following disclaimer.
24 //
25 // 2. Redistributions in binary form must reproduce the above copyright
26 // notice, this list of conditions and the following disclaimer in the
27 // documentation and/or other materials provided with the distribution.
28 //
29 // 3. Neither the name of the copyright holder nor the names of its
30 // contributors may be used to endorse or promote products derived from this
31 // software without specific prior written permission.
32 //
33 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
34 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
35 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
36 // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
37 // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
38 // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
39 // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
40 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
41 // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
42 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
43 // POSSIBILITY OF SUCH DAMAGE.
44 //
45 // This is part of revision 1.2.11 of the AmbiqSuite Development Package.
46 //
47 //*****************************************************************************
48
49 #include <stdint.h>
50 #include <stdbool.h>
51 #include "am_mcu_apollo.h"
52
53 //*****************************************************************************
54 //
55 //! @brief Set up the stimer.
56 //!
57 //! @param ui32STimerConfig is the value to load into the configuration reg.
58 //!
59 //! This function should be used to perform the initial set-up of the
60 //! stimer.
61 //!
62 //! @return The 32-bit current config of the STimer Config register
63 //
64 //*****************************************************************************
65 uint32_t
am_hal_stimer_config(uint32_t ui32STimerConfig)66 am_hal_stimer_config(uint32_t ui32STimerConfig)
67 {
68 uint32_t ui32CurrVal;
69
70 //
71 // Read the current config
72 //
73 ui32CurrVal = AM_REG(CTIMER, STCFG);
74
75 //
76 // Write our configuration value.
77 //
78 AM_REG(CTIMER, STCFG) = ui32STimerConfig;
79
80 //
81 // If all of the clock sources are not HFRC, disable LDO when sleeping if timers are enabled.
82 //
83 if ( (AM_BFR(CTIMER, STCFG, CLKSEL) == AM_REG_CTIMER_STCFG_CLKSEL_HFRC_DIV16) ||
84 (AM_BFR(CTIMER, STCFG, CLKSEL) == AM_REG_CTIMER_STCFG_CLKSEL_HFRC_DIV256) )
85 {
86 AM_BFW(PWRCTRL, MISCOPT, DIS_LDOLPMODE_TIMERS, 0);
87 }
88 else
89 {
90 AM_BFW(PWRCTRL, MISCOPT, DIS_LDOLPMODE_TIMERS, 1);
91 }
92 return ui32CurrVal;
93 }
94
95 //*****************************************************************************
96 //
97 //! @brief Get the current stimer value.
98 //!
99 //! This function can be used to read, uninvasively, the value in the stimer.
100 //!
101 //! @return The 32-bit value from the STimer counter register.
102 //
103 //*****************************************************************************
104 uint32_t
am_hal_stimer_counter_get(void)105 am_hal_stimer_counter_get(void)
106 {
107 return AM_REG(CTIMER, STTMR);
108 }
109
110 //*****************************************************************************
111 //
112 //! @brief Clear the stimer counter.
113 //!
114 //! This function clears the STimer Counter and leaves the stimer running.
115 //!
116 //! @return None.
117 //
118 //*****************************************************************************
119 void
am_hal_stimer_counter_clear(void)120 am_hal_stimer_counter_clear(void)
121 {
122 //
123 // Set the clear bit
124 //
125 AM_REG(CTIMER, STCFG) |= AM_REG_CTIMER_STCFG_CLEAR_M;
126
127 //
128 // Reset the clear bit
129 //
130 AM_REG(CTIMER, STCFG) &= ~AM_REG_CTIMER_STCFG_CLEAR_M;
131 }
132
133 //*****************************************************************************
134 //
135 //! @brief Set the compare value.
136 //!
137 //! @param ui32CmprInstance is the compare register instance number (0-7).
138 //! @param ui32Delta is the value to add to the STimer counter and load into
139 //! the comparator register. It should be > 1
140 //!
141 //! NOTE: There is no way to set an absolute value into a comparator register.
142 //! Only deltas added to the STimer counter can be written to the compare
143 //! registers.
144 //! CAUTION: The HAL implementation requires temporarily disabling the
145 //! comparison. To avoid the remote possibility of losing an interrupt
146 //! during that time, delta should always be set to a value greater than 1
147 //!
148 //! @return None.
149 //
150 //*****************************************************************************
151 void
am_hal_stimer_compare_delta_set(uint32_t ui32CmprInstance,uint32_t ui32Delta)152 am_hal_stimer_compare_delta_set(uint32_t ui32CmprInstance, uint32_t ui32Delta)
153 {
154 uint32_t cfgVal;
155 uint32_t ui32Critical = 0;
156 if ( ui32CmprInstance > 7 )
157 {
158 return;
159 }
160
161 cfgVal = AM_REG(CTIMER, STCFG);
162 // We need to disable the compare temporarily while setting the delta value
163 // That leaves a corner case where we could miss the trigger if setting a very
164 // small delta. To avoid this, we take critical section, and we should ensure
165 // that delta value is at least > 1
166
167 // Disable the compare if already enabled, when setting the new value
168 AM_REG(CTIMER, STCFG) &= ~((AM_HAL_STIMER_CFG_COMPARE_A_ENABLE << ui32CmprInstance));
169 //
170 // Start a critical section.
171 //
172 ui32Critical = am_hal_interrupt_master_disable();
173 AM_REGVAL(AM_REG_STIMER_COMPARE(0, ui32CmprInstance)) = ui32Delta;
174 // Restore Compare Enable bit
175 AM_REG(CTIMER, STCFG) |= cfgVal & (AM_HAL_STIMER_CFG_COMPARE_A_ENABLE << ui32CmprInstance);
176 //
177 // End the critical section.
178 //
179 am_hal_interrupt_master_set(ui32Critical);
180 }
181
182 //*****************************************************************************
183 //
184 //! @brief Get the current stimer compare register value.
185 //!
186 //! @param ui32CmprInstance is the compare register instance number (0-7).
187 //!
188 //! This function can be used to read the value in an stimer compare register.
189 //!
190 //!
191 //! @return None.
192 //
193 //*****************************************************************************
194 uint32_t
am_hal_stimer_compare_get(uint32_t ui32CmprInstance)195 am_hal_stimer_compare_get(uint32_t ui32CmprInstance)
196 {
197 if ( ui32CmprInstance > 7 )
198 {
199 return 0;
200 }
201
202 return AM_REGVAL(AM_REG_STIMER_COMPARE(0, ui32CmprInstance));
203 }
204
205 //*****************************************************************************
206 //
207 //! @brief Start capturing data with the specified capture register.
208 //!
209 //! @param ui32CaptureNum is the Capture Register Number to read (0-3).
210 //! ui32GPIONumber is the pin number.
211 //! bPolarity: false (0) = Capture on low to high transition.
212 //! true (1) = Capture on high to low transition.
213 //!
214 //! Use this function to start capturing.
215 //!
216 //! @return None.
217 //
218 //*****************************************************************************
219 void
am_hal_stimer_capture_start(uint32_t ui32CaptureNum,uint32_t ui32GPIONumber,bool bPolarity)220 am_hal_stimer_capture_start(uint32_t ui32CaptureNum,
221 uint32_t ui32GPIONumber,
222 bool bPolarity)
223 {
224 uint32_t ui32CapCtrl;
225
226 if ( ui32GPIONumber > (AM_HAL_GPIO_MAX_PADS-1) )
227 {
228 return;
229 }
230
231 //
232 // Set the polarity and pin selection in the GPIO block.
233 //
234 switch (ui32CaptureNum)
235 {
236 case 0:
237 AM_BFW(GPIO, STMRCAP, STPOL0, bPolarity);
238 AM_BFW(GPIO, STMRCAP, STSEL0, ui32GPIONumber);
239 ui32CapCtrl = AM_REG_CTIMER_CAPTURE_CONTROL_CAPTURE_A_M;
240 break;
241 case 1:
242 AM_BFW(GPIO, STMRCAP, STPOL1, bPolarity);
243 AM_BFW(GPIO, STMRCAP, STSEL1, ui32GPIONumber);
244 ui32CapCtrl = AM_REG_CTIMER_CAPTURE_CONTROL_CAPTURE_B_M;
245 break;
246 case 2:
247 AM_BFW(GPIO, STMRCAP, STPOL2, bPolarity);
248 AM_BFW(GPIO, STMRCAP, STSEL2, ui32GPIONumber);
249 ui32CapCtrl = AM_REG_CTIMER_CAPTURE_CONTROL_CAPTURE_C_M;
250 break;
251 case 3:
252 AM_BFW(GPIO, STMRCAP, STPOL3, bPolarity);
253 AM_BFW(GPIO, STMRCAP, STSEL3, ui32GPIONumber);
254 ui32CapCtrl = AM_REG_CTIMER_CAPTURE_CONTROL_CAPTURE_D_M;
255 break;
256 default:
257 return; // error concealment.
258 }
259
260 //
261 // Enable it in the CTIMER Block
262 //
263 AM_REG(CTIMER, CAPTURE_CONTROL) |= ui32CapCtrl;
264 }
265
266 //*****************************************************************************
267 //
268 //! @brief Start capturing data with the specified capture register.
269 //!
270 //! @param ui32CaptureNum is the Capture Register Number to read.
271 //!
272 //! Use this function to start capturing.
273 //!
274 //! @return None.
275 //
276 //*****************************************************************************
am_hal_stimer_capture_stop(uint32_t ui32CaptureNum)277 void am_hal_stimer_capture_stop(uint32_t ui32CaptureNum)
278 {
279 //
280 // Disable it in the CTIMER block.
281 //
282 AM_REG(CTIMER, CAPTURE_CONTROL) &=
283 ~(AM_REG_CTIMER_CAPTURE_CONTROL_CAPTURE_A_M <<
284 ((AM_REG_CTIMER_CAPTURE_CONTROL_CAPTURE_B_S -
285 AM_REG_CTIMER_CAPTURE_CONTROL_CAPTURE_A_S) * ui32CaptureNum));
286 }
287
288 //*****************************************************************************
289 //
290 //! @brief Get the current stimer capture register value.
291 //!
292 //! @param ui32CaptureNum is the Capture Register Number to read.
293 //!
294 //! This function can be used to read the value in an stimer capture register.
295 //!
296 //!
297 //! @return None.
298 //
299 //*****************************************************************************
am_hal_stimer_capture_get(uint32_t ui32CaptureNum)300 uint32_t am_hal_stimer_capture_get(uint32_t ui32CaptureNum)
301 {
302 if ( ui32CaptureNum > 7 )
303 {
304 return 0;
305 }
306
307 return AM_REGVAL(AM_REG_STIMER_CAPTURE(0, ui32CaptureNum));
308 }
309
310 //*****************************************************************************
311 //
312 //! @brief Enables the selected system timer interrupt.
313 //!
314 //! @param ui32Interrupt is the interrupt to be used.
315 //!
316 //! This function will enable the selected interrupts in the STIMER interrupt
317 //! enable register. In order to receive an interrupt from an stimer component,
318 //! you will need to enable the interrupt for that component in this main
319 //! register, as well as in the stimer configuration register (accessible though
320 //! am_hal_stimer_config()), and in the NVIC.
321 //!
322 //! ui32Interrupt should be the logical OR of one or more of the following
323 //! values:
324 //!
325 //! AM_HAL_STIMER_INT_COMPAREA
326 //! AM_HAL_STIMER_INT_COMPAREB
327 //! AM_HAL_STIMER_INT_COMPAREC
328 //! AM_HAL_STIMER_INT_COMPARED
329 //! AM_HAL_STIMER_INT_COMPAREE
330 //! AM_HAL_STIMER_INT_COMPAREF
331 //! AM_HAL_STIMER_INT_COMPAREG
332 //! AM_HAL_STIMER_INT_COMPAREH
333 //!
334 //! AM_HAL_STIMER_INT_OVERFLOW
335 //!
336 //! AM_HAL_STIMER_INT_CAPTUREA
337 //! AM_HAL_STIMER_INT_CAPTUREB
338 //! AM_HAL_STIMER_INT_CAPTUREC
339 //! AM_HAL_STIMER_INT_CAPTURED
340 //!
341 //! @return None.
342 //
343 //*****************************************************************************
344 void
am_hal_stimer_int_enable(uint32_t ui32Interrupt)345 am_hal_stimer_int_enable(uint32_t ui32Interrupt)
346 {
347 //
348 // Enable the interrupt at the module level.
349 //
350 AM_REGn(CTIMER, 0, STMINTEN) |= ui32Interrupt;
351 }
352
353 //*****************************************************************************
354 //
355 //! @brief Return the enabled stimer interrupts.
356 //!
357 //! This function will return all enabled interrupts in the STIMER
358 //! interrupt enable register.
359 //!
360 //! @return return enabled interrupts. This will be a logical or of:
361 //!
362 //! AM_HAL_STIMER_INT_COMPAREA
363 //! AM_HAL_STIMER_INT_COMPAREB
364 //! AM_HAL_STIMER_INT_COMPAREC
365 //! AM_HAL_STIMER_INT_COMPARED
366 //! AM_HAL_STIMER_INT_COMPAREE
367 //! AM_HAL_STIMER_INT_COMPAREF
368 //! AM_HAL_STIMER_INT_COMPAREG
369 //! AM_HAL_STIMER_INT_COMPAREH
370 //!
371 //! AM_HAL_STIMER_INT_OVERFLOW
372 //!
373 //! AM_HAL_STIMER_INT_CAPTUREA
374 //! AM_HAL_STIMER_INT_CAPTUREB
375 //! AM_HAL_STIMER_INT_CAPTUREC
376 //! AM_HAL_STIMER_INT_CAPTURED
377 //!
378 //! @return Return the enabled timer interrupts.
379 //
380 //*****************************************************************************
381 uint32_t
am_hal_stimer_int_enable_get(void)382 am_hal_stimer_int_enable_get(void)
383 {
384 //
385 // Return enabled interrupts.
386 //
387 return AM_REGn(CTIMER, 0, STMINTEN);
388 }
389
390 //*****************************************************************************
391 //
392 //! @brief Disables the selected stimer interrupt.
393 //!
394 //! @param ui32Interrupt is the interrupt to be used.
395 //!
396 //! This function will disable the selected interrupts in the STIMER
397 //! interrupt register.
398 //!
399 //! ui32Interrupt should be the logical OR of one or more of the following
400 //! values:
401 //!
402 //! AM_HAL_STIMER_INT_COMPAREA
403 //! AM_HAL_STIMER_INT_COMPAREB
404 //! AM_HAL_STIMER_INT_COMPAREC
405 //! AM_HAL_STIMER_INT_COMPARED
406 //! AM_HAL_STIMER_INT_COMPAREE
407 //! AM_HAL_STIMER_INT_COMPAREF
408 //! AM_HAL_STIMER_INT_COMPAREG
409 //! AM_HAL_STIMER_INT_COMPAREH
410 //!
411 //! AM_HAL_STIMER_INT_OVERFLOW
412 //!
413 //! AM_HAL_STIMER_INT_CAPTUREA
414 //! AM_HAL_STIMER_INT_CAPTUREB
415 //! AM_HAL_STIMER_INT_CAPTUREC
416 //! AM_HAL_STIMER_INT_CAPTURED
417 //!
418 //! @return None.
419 //
420 //*****************************************************************************
421 void
am_hal_stimer_int_disable(uint32_t ui32Interrupt)422 am_hal_stimer_int_disable(uint32_t ui32Interrupt)
423 {
424 //
425 // Disable the interrupt at the module level.
426 //
427 AM_REGn(CTIMER, 0, STMINTEN) &= ~ui32Interrupt;
428 }
429
430 //*****************************************************************************
431 //
432 //! @brief Sets the selected stimer interrupt.
433 //!
434 //! @param ui32Interrupt is the interrupt to be used.
435 //!
436 //! This function will set the selected interrupts in the STIMER
437 //! interrupt register.
438 //!
439 //! ui32Interrupt should be the logical OR of one or more of the following
440 //! values:
441 //!
442 //! AM_HAL_STIMER_INT_COMPAREA
443 //! AM_HAL_STIMER_INT_COMPAREB
444 //! AM_HAL_STIMER_INT_COMPAREC
445 //! AM_HAL_STIMER_INT_COMPARED
446 //! AM_HAL_STIMER_INT_COMPAREE
447 //! AM_HAL_STIMER_INT_COMPAREF
448 //! AM_HAL_STIMER_INT_COMPAREG
449 //! AM_HAL_STIMER_INT_COMPAREH
450 //!
451 //! AM_HAL_STIMER_INT_OVERFLOW
452 //!
453 //! AM_HAL_STIMER_INT_CAPTUREA
454 //! AM_HAL_STIMER_INT_CAPTUREB
455 //! AM_HAL_STIMER_INT_CAPTUREC
456 //! AM_HAL_STIMER_INT_CAPTURED
457 //!
458 //! @return None.
459 //
460 //*****************************************************************************
461 void
am_hal_stimer_int_set(uint32_t ui32Interrupt)462 am_hal_stimer_int_set(uint32_t ui32Interrupt)
463 {
464 //
465 // Set the interrupts.
466 //
467 AM_REGn(CTIMER, 0, STMINTSET) = ui32Interrupt;
468 }
469
470 //*****************************************************************************
471 //
472 //! @brief Clears the selected stimer interrupt.
473 //!
474 //! @param ui32Interrupt is the interrupt to be used.
475 //!
476 //! This function will clear the selected interrupts in the STIMER
477 //! interrupt register.
478 //!
479 //! ui32Interrupt should be the logical OR of one or more of the following
480 //! values:
481 //!
482 //! AM_HAL_STIMER_INT_COMPAREA
483 //! AM_HAL_STIMER_INT_COMPAREB
484 //! AM_HAL_STIMER_INT_COMPAREC
485 //! AM_HAL_STIMER_INT_COMPARED
486 //! AM_HAL_STIMER_INT_COMPAREE
487 //! AM_HAL_STIMER_INT_COMPAREF
488 //! AM_HAL_STIMER_INT_COMPAREG
489 //! AM_HAL_STIMER_INT_COMPAREH
490 //!
491 //! AM_HAL_STIMER_INT_OVERFLOW
492 //!
493 //! AM_HAL_STIMER_INT_CAPTUREA
494 //! AM_HAL_STIMER_INT_CAPTUREB
495 //! AM_HAL_STIMER_INT_CAPTUREC
496 //! AM_HAL_STIMER_INT_CAPTURED
497 //!
498 //! @return None.
499 //
500 //*****************************************************************************
501 void
am_hal_stimer_int_clear(uint32_t ui32Interrupt)502 am_hal_stimer_int_clear(uint32_t ui32Interrupt)
503 {
504 //
505 // Disable the interrupt at the module level.
506 //
507 AM_REGn(CTIMER, 0, STMINTCLR) = ui32Interrupt;
508 }
509
510
511 //*****************************************************************************
512 //
513 //! @brief Returns either the enabled or raw stimer interrupt status.
514 //!
515 //! This function will return the stimer interrupt status.
516 //!
517 //! @bEnabledOnly if true returns the status of the enabled interrupts
518 //! only.
519 //!
520 //! The return value will be the logical OR of one or more of the following
521 //! values:
522 //!
523 //!
524 //! @return Returns the stimer interrupt status.
525 //
526 //*****************************************************************************
527 uint32_t
am_hal_stimer_int_status_get(bool bEnabledOnly)528 am_hal_stimer_int_status_get(bool bEnabledOnly)
529 {
530 //
531 // Return the desired status.
532 //
533 uint32_t ui32RetVal = AM_REGn(CTIMER, 0, STMINTSTAT);
534
535 if ( bEnabledOnly )
536 {
537 ui32RetVal &= AM_REGn(CTIMER, 0, STMINTEN);
538 }
539
540 return ui32RetVal;
541 }
542
543 //*****************************************************************************
544 //
545 // End Doxygen group.
546 //! @}
547 //
548 //*****************************************************************************
549