1 //*****************************************************************************
2 //
3 //! @file am_util_delay.c
4 //!
5 //! @brief A few useful delay functions.
6 //!
7 //! Functions for fixed delays.
8 //!
9 //! @addtogroup delay Delay Functionality
10 //! @ingroup utils
11 //! @{
12 //
13 //*****************************************************************************
14 
15 //*****************************************************************************
16 //
17 // Copyright (c) 2024, Ambiq Micro, Inc.
18 // All rights reserved.
19 //
20 // Redistribution and use in source and binary forms, with or without
21 // modification, are permitted provided that the following conditions are met:
22 //
23 // 1. Redistributions of source code must retain the above copyright notice,
24 // this list of conditions and the following disclaimer.
25 //
26 // 2. Redistributions in binary form must reproduce the above copyright
27 // notice, this list of conditions and the following disclaimer in the
28 // documentation and/or other materials provided with the distribution.
29 //
30 // 3. Neither the name of the copyright holder nor the names of its
31 // contributors may be used to endorse or promote products derived from this
32 // software without specific prior written permission.
33 //
34 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
35 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
36 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
37 // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
38 // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
39 // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
40 // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
41 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
42 // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
43 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
44 // POSSIBILITY OF SUCH DAMAGE.
45 //
46 // This is part of revision stable-c1f95ddf60 of the AmbiqSuite Development Package.
47 //
48 //*****************************************************************************
49 
50 #include  <stdint.h>
51 #include <stdbool.h>
52 #include "am_mcu_apollo.h"
53 #include "am_util_delay.h"
54 
55 //*****************************************************************************
56 //
57 // Delays for a desired amount of loops.
58 //
59 //*****************************************************************************
60 void
am_util_delay_cycles(uint32_t ui32Iterations)61 am_util_delay_cycles(uint32_t ui32Iterations)
62 {
63     //
64     // Call the BOOTROM cycle delay function
65     //
66 #if defined(AM_PART_APOLLO4_API) || defined(AM_PART_APOLLO5_API)
67     am_hal_delay_us( ui32Iterations);
68 #else
69     am_hal_flash_delay(ui32Iterations);
70 #endif // AM_PART_APOLLO4_API || AM_PART_APOLLO5_API
71 }
72 
73 //*****************************************************************************
74 //
75 // Delays for a desired amount of milliseconds.
76 //
77 //*****************************************************************************
78 void
am_util_delay_ms(uint32_t ui32MilliSeconds)79 am_util_delay_ms(uint32_t ui32MilliSeconds)
80 {
81 #if defined(AM_PART_APOLLO4_API) || defined(AM_PART_APOLLO5_API)
82     am_hal_delay_us( ui32MilliSeconds * 1000);
83 #else // AM_PART_APOLLO4_API || AM_PART_APOLLO5_API
84     uint32_t ui32Loops, ui32HFRC;
85 #if AM_APOLLO3_CLKGEN
86     am_hal_clkgen_status_t sClkgenStatus;
87     am_hal_clkgen_status_get(&sClkgenStatus);
88     ui32HFRC = sClkgenStatus.ui32SysclkFreq;
89 #else // AM_APOLLO3_CLKGEN
90     ui32HFRC = am_hal_clkgen_sysclk_get();
91 #endif // AM_APOLLO3_CLKGEN
92     ui32Loops = ui32MilliSeconds * (ui32HFRC / 3000);
93 
94     //
95     // Call the BOOTROM cycle delay function
96     //
97     am_hal_flash_delay(ui32Loops);
98 #endif // AM_PART_APOLLO4_API || AM_PART_APOLLO5_API
99 }
100 
101 //*****************************************************************************
102 //
103 // Delays for a desired amount of microseconds.
104 //
105 //*****************************************************************************
106 void
am_util_delay_us(uint32_t ui32MicroSeconds)107 am_util_delay_us(uint32_t ui32MicroSeconds)
108 {
109 #if defined(AM_PART_APOLLO4_API) || defined(AM_PART_APOLLO5_API)
110     am_hal_delay_us( ui32MicroSeconds );
111 #else // AM_PART_APOLLO4_API || AM_PART_APOLLO5_API
112     uint32_t ui32Loops, ui32HFRC;
113 
114 #if AM_APOLLO3_CLKGEN
115     am_hal_clkgen_status_t sClkgenStatus;
116     am_hal_clkgen_status_get(&sClkgenStatus);
117     ui32HFRC = sClkgenStatus.ui32SysclkFreq;
118 #else // AM_APOLLO3_CLKGEN
119     ui32HFRC = am_hal_clkgen_sysclk_get();
120 #endif // AM_APOLLO3_CLKGEN
121     ui32Loops = ui32MicroSeconds * (ui32HFRC / 3000000);
122 
123     //
124     // Call the BOOTROM cycle delay function
125     //
126     am_hal_flash_delay(ui32Loops);
127 #endif // AM_PART_APOLLO4_API || AM_PART_APOLLO5_API
128 }
129 
130 //*****************************************************************************
131 //
132 // End Doxygen group.
133 //! @}
134 //
135 //*****************************************************************************
136 
137