1 //*****************************************************************************
2 //
3 //  am_hal_stimer.h
4 //! @file
5 //!
6 //! @brief Functions for accessing and configuring the STIMER.
7 //!
8 //! @addtogroup stimer2 Counter/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 #ifndef AM_HAL_STIMER_H
49 #define AM_HAL_STIMER_H
50 
51 //
52 // Compute address of a given COMPARE or CAPTURE register.
53 // Note - For Apollo2, the parameter n should be 0 (as only 1 stimer module
54 //        exists) and the parameter r should be 0-7 (compare) or 0-3 (capture).
55 //
56 #define AM_REG_STIMER_COMPARE(n, r) (AM_REG_CTIMERn(n) +                \
57                                      AM_REG_CTIMER_SCMPR0_O + (r * 4))
58 
59 #define AM_REG_STIMER_CAPTURE(n, r) (AM_REG_CTIMERn(n) +                \
60                                      AM_REG_CTIMER_SCAPT0_O + (r * 4))
61 
62 //*****************************************************************************
63 //
64 //! @name Interrupt Status Bits
65 //! @brief Interrupt Status Bits for enable/disble use
66 //!
67 //! These macros may be used to set and clear interrupt bits
68 //! @{
69 //
70 //*****************************************************************************
71 #define AM_HAL_STIMER_INT_COMPAREA         AM_REG_CTIMER_STMINTSTAT_COMPAREA_M
72 #define AM_HAL_STIMER_INT_COMPAREB         AM_REG_CTIMER_STMINTSTAT_COMPAREB_M
73 #define AM_HAL_STIMER_INT_COMPAREC         AM_REG_CTIMER_STMINTSTAT_COMPAREC_M
74 #define AM_HAL_STIMER_INT_COMPARED         AM_REG_CTIMER_STMINTSTAT_COMPARED_M
75 #define AM_HAL_STIMER_INT_COMPAREE         AM_REG_CTIMER_STMINTSTAT_COMPAREE_M
76 #define AM_HAL_STIMER_INT_COMPAREF         AM_REG_CTIMER_STMINTSTAT_COMPAREF_M
77 #define AM_HAL_STIMER_INT_COMPAREG         AM_REG_CTIMER_STMINTSTAT_COMPAREG_M
78 #define AM_HAL_STIMER_INT_COMPAREH         AM_REG_CTIMER_STMINTSTAT_COMPAREH_M
79 
80 #define AM_HAL_STIMER_INT_OVERFLOW         AM_REG_CTIMER_STMINTSTAT_OVERFLOW_M
81 
82 #define AM_HAL_STIMER_INT_CAPTUREA         AM_REG_CTIMER_STMINTSTAT_CAPTUREA_M
83 #define AM_HAL_STIMER_INT_CAPTUREB         AM_REG_CTIMER_STMINTSTAT_CAPTUREB_M
84 #define AM_HAL_STIMER_INT_CAPTUREC         AM_REG_CTIMER_STMINTSTAT_CAPTUREC_M
85 #define AM_HAL_STIMER_INT_CAPTURED         AM_REG_CTIMER_STMINTSTAT_CAPTURED_M
86 
87 //! @}
88 
89 
90 
91 //*****************************************************************************
92 //
93 //! @name STimer Configuration Bits
94 //! @brief Interrupt Status Bits for enable/disble use
95 //!
96 //! These macros may be used to set and clear interrupt bits
97 //! @{
98 //
99 //*****************************************************************************
100 #define AM_HAL_STIMER_CFG_THAW                                                \
101                                                 AM_REG_CTIMER_STCFG_FREEZE_THAW
102 #define AM_HAL_STIMER_CFG_FREEZE                                              \
103                                               AM_REG_CTIMER_STCFG_FREEZE_FREEZE
104 #define AM_HAL_STIMER_CFG_RUN                                                 \
105                                                   AM_REG_CTIMER_STCFG_CLEAR_RUN
106 #define AM_HAL_STIMER_CFG_CLEAR                                               \
107                                                 AM_REG_CTIMER_STCFG_CLEAR_CLEAR
108 #define AM_HAL_STIMER_CFG_COMPARE_A_ENABLE                                    \
109                                         AM_REG_CTIMER_STCFG_COMPARE_A_EN_ENABLE
110 #define AM_HAL_STIMER_CFG_COMPARE_B_ENABLE                                    \
111                                         AM_REG_CTIMER_STCFG_COMPARE_B_EN_ENABLE
112 #define AM_HAL_STIMER_CFG_COMPARE_C_ENABLE                                    \
113                                         AM_REG_CTIMER_STCFG_COMPARE_C_EN_ENABLE
114 #define AM_HAL_STIMER_CFG_COMPARE_D_ENABLE                                    \
115                                         AM_REG_CTIMER_STCFG_COMPARE_D_EN_ENABLE
116 #define AM_HAL_STIMER_CFG_COMPARE_E_ENABLE                                    \
117                                         AM_REG_CTIMER_STCFG_COMPARE_E_EN_ENABLE
118 #define AM_HAL_STIMER_CFG_COMPARE_F_ENABLE                                    \
119                                         AM_REG_CTIMER_STCFG_COMPARE_F_EN_ENABLE
120 #define AM_HAL_STIMER_CFG_COMPARE_G_ENABLE                                    \
121                                         AM_REG_CTIMER_STCFG_COMPARE_G_EN_ENABLE
122 #define AM_HAL_STIMER_CFG_COMPARE_H_ENABLE                                    \
123                                         AM_REG_CTIMER_STCFG_COMPARE_H_EN_ENABLE
124 
125 //! @}
126 
127 //*****************************************************************************
128 //
129 //! @name Clock Configuration options
130 //! @brief STimer Configuration register options.
131 //!
132 //! These options are to be used with the am_hal_stimer_config() function.
133 //!  @{
134 //
135 //*****************************************************************************
136 #define AM_HAL_STIMER_NO_CLK                                                  \
137               AM_REG_CTIMER_STCFG_CLKSEL(AM_REG_CTIMER_STCFG_CLKSEL_NOCLK)
138 #define AM_HAL_STIMER_HFRC_3MHZ                                               \
139               AM_REG_CTIMER_STCFG_CLKSEL(AM_REG_CTIMER_STCFG_CLKSEL_HFRC_DIV16)
140 #define AM_HAL_STIMER_HFRC_187_5KHZ                                           \
141               AM_REG_CTIMER_STCFG_CLKSEL(AM_REG_CTIMER_STCFG_CLKSEL_HFRC_DIV256)
142 #define AM_HAL_STIMER_XTAL_32KHZ                                              \
143               AM_REG_CTIMER_STCFG_CLKSEL(AM_REG_CTIMER_STCFG_CLKSEL_XTAL_DIV1)
144 #define AM_HAL_STIMER_XTAL_16KHZ                                              \
145               AM_REG_CTIMER_STCFG_CLKSEL(AM_REG_CTIMER_STCFG_CLKSEL_XTAL_DIV2)
146 #define AM_HAL_STIMER_XTAL_1KHZ                                               \
147               AM_REG_CTIMER_STCFG_CLKSEL(AM_REG_CTIMER_STCFG_CLKSEL_XTAL_DIV32)
148 #define AM_HAL_STIMER_LFRC_1KHZ                                               \
149               AM_REG_CTIMER_STCFG_CLKSEL(AM_REG_CTIMER_STCFG_CLKSEL_LFRC_DIV1)
150 #define AM_HAL_STIMER_HFRC_CTIMER0A                                           \
151               AM_REG_CTIMER_STCFG_CLKSEL(AM_REG_CTIMER_STCFG_CLKSEL_CTIMER0A)
152 #define AM_HAL_STIMER_HFRC_CTIMER0B                                           \
153               AM_REG_CTIMER_STCFG_CLKSEL(AM_REG_CTIMER_STCFG_CLKSEL_CTIMER0B)
154 //! @}
155 
156 
157 
158 //*****************************************************************************
159 //
160 //! @name Capture Control Register options.
161 //! @brief Configuration options for capture control register.
162 //!
163 //! These options are to be used with the am_hal_stimer_capture_control_set
164 //! function.
165 //!  @{
166 //
167 //*****************************************************************************
168 #define AM_HAL_STIMER_CAPTURE_A_ENABLE                                        \
169                                  AM_REG_CTIMER_CAPTURE_CONTROL_CAPTURE_A_ENABLE
170 #define AM_HAL_STIMER_CAPTURE_B_ENABLE                                        \
171                                  AM_REG_CTIMER_CAPTURE_CONTROL_CAPTURE_B_ENABLE
172 #define AM_HAL_STIMER_CAPTURE_C_ENABLE                                        \
173                                  AM_REG_CTIMER_CAPTURE_CONTROL_CAPTURE_C_ENABLE
174 #define AM_HAL_STIMER_CAPTURE_D_ENABLE                                        \
175                                  AM_REG_CTIMER_CAPTURE_CONTROL_CAPTURE_D_ENABLE
176 
177 //! @}
178 
179 
180 //*****************************************************************************
181 //
182 //
183 //
184 //*****************************************************************************
185 
186 //*****************************************************************************
187 //
188 // Stimer configuration structure
189 //
190 //*****************************************************************************
191 typedef struct
192 {
193     //
194     //! Configuration options for the STIMER
195     //
196     uint32_t ui32STimerConfig;
197 }
198 am_hal_stimer_config_t;
199 
200 
201 
202 #ifdef __cplusplus
203 extern "C"
204 {
205 #endif
206 
207 //*****************************************************************************
208 //
209 // External function definitions
210 //
211 //*****************************************************************************
212 extern uint32_t am_hal_stimer_config(uint32_t ui32STimerConfig);
213 extern uint32_t am_hal_stimer_counter_get(void);
214 extern void     am_hal_stimer_counter_clear(void);
215 extern void     am_hal_stimer_compare_delta_set(uint32_t ui32CmprInstance,
216                                                 uint32_t ui32Delta);
217 extern uint32_t am_hal_stimer_compare_get(uint32_t ui32CmprInstance);
218 extern void     am_hal_stimer_capture_start(uint32_t ui32CaptureNum,
219                                             uint32_t ui32GPIONumber,
220                                             bool bPolarity);
221 extern void     am_hal_stimer_capture_stop(uint32_t ui32CaptureNum);
222 extern uint32_t am_hal_stimer_capture_get(uint32_t ui32CaptureNum);
223 extern void     am_hal_stimer_int_enable(uint32_t ui32Interrupt);
224 extern uint32_t am_hal_stimer_int_enable_get(void);
225 extern void     am_hal_stimer_int_disable(uint32_t ui32Interrupt);
226 extern void     am_hal_stimer_int_set(uint32_t ui32Interrupt);
227 extern void     am_hal_stimer_int_clear(uint32_t ui32Interrupt);
228 extern uint32_t am_hal_stimer_int_status_get(bool bEnabledOnly);
229 
230 
231 #ifdef __cplusplus
232 }
233 #endif
234 
235 #endif // AM_HAL_STIMER_H
236 
237 //*****************************************************************************
238 //
239 // End Doxygen group.
240 //! @}
241 //
242 //*****************************************************************************
243