1 /* --COPYRIGHT--,BSD
2  * Copyright (c) 2017, Texas Instruments Incorporated
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * *  Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  *
12  * *  Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * *  Neither the name of Texas Instruments Incorporated nor the names of
17  *    its contributors may be used to endorse or promote products derived
18  *    from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
24  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
27  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
28  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  * --/COPYRIGHT--*/
32 #ifndef TIMER32_H_
33 #define TIMER32_H_
34 
35 //*****************************************************************************
36 //
37 //! \addtogroup timer32_api
38 //! @{
39 //
40 //*****************************************************************************
41 
42 //*****************************************************************************
43 //
44 // If building with a C++ compiler, make all of the definitions in this header
45 // have a C binding.
46 //
47 //*****************************************************************************
48 #ifdef __cplusplus
49 extern "C"
50 {
51 #endif
52 #include <stdint.h>
53 #include <stdbool.h>
54 #include <ti/devices/msp432p4xx/inc/msp.h>
55 
56 //*****************************************************************************
57 //
58 // Control specific variables
59 //
60 //*****************************************************************************
61 #define TIMER32_CMSIS(x) ((Timer32_Type *) x)
62 
63 #define TIMER_OFFSET    0x020
64 
65 #define TIMER32_0_BASE   (uint32_t)TIMER32_1
66 #define TIMER32_1_BASE   (uint32_t)TIMER32_2
67 
68 #define TIMER32_0_INTERRUPT         INT_T32_INT1
69 #define TIMER32_1_INTERRUPT         INT_T32_INT2
70 #define TIMER32_COMBINED_INTERRUPT  INT_T32_INTC
71 
72 #define TIMER32_16BIT            0x00
73 #define TIMER32_32BIT            0x01
74 
75 #define TIMER32_PRESCALER_1      0x00
76 #define TIMER32_PRESCALER_16     0x04
77 #define TIMER32_PRESCALER_256    0x08
78 
79 #define TIMER32_FREE_RUN_MODE   0x00
80 #define TIMER32_PERIODIC_MODE   0x01
81 
82 //*****************************************************************************
83 //
84 // API Function prototypes
85 //
86 //*****************************************************************************
87 
88 //*****************************************************************************
89 //
90 //! Initializes the Timer32 module
91 //!
92 //! \param timer is the instance of the Timer32 module.
93 //! Valid parameters must be one of the following values:
94 //!         - \b TIMER32_0_BASE
95 //!         - \b TIMER32_1_BASE
96 //!
97 //! \param preScaler is the prescaler (or divider) to apply to the clock
98 //! source given to the Timer32 module.
99 //!         Valid values are
100 //!         - \b TIMER32_PRESCALER_1 [DEFAULT]
101 //!         - \b TIMER32_PRESCALER_16
102 //!         - \b TIMER32_PRESCALER_256
103 //! \param resolution is the bit resolution of the Timer32 module.
104 //!         Valid values are
105 //!         - \b TIMER32_16BIT [DEFAULT]
106 //!         - \b TIMER32_32BIT
107 //! \param mode selects between free run and periodic mode. In free run
108 //! mode, the value of the timer is reset to UINT16_MAX (for 16-bit mode) or
109 //! UINT32_MAX (for 16-bit mode) when the timer reaches zero. In periodic mode,
110 //! the timer is reset to the value set by the Timer32_setCount function.
111 //!         Valid values are
112 //!         - \b TIMER32_FREE_RUN_MODE [DEFAULT]
113 //!         - \b TIMER32_PERIODIC_MODE
114 //!
115 //!
116 //! \return None.
117 //
118 //*****************************************************************************
119 extern void Timer32_initModule(uint32_t timer, uint32_t preScaler,
120         uint32_t resolution, uint32_t mode);
121 
122 //*****************************************************************************
123 //
124 //! Sets the count of the timer and resets the current value to the value
125 //! passed. This value is set on the next rising edge of the clock provided to
126 //! the timer module
127 //!
128 //! \param timer is the instance of the Timer32 module.
129 //! Valid parameters must be one of the following values:
130 //!         - \b TIMER32_0_BASE
131 //!         - \b TIMER32_1_BASE
132 //! \param count Value of the timer to set. Note that
133 //! if the timer is in 16-bit mode and a value is passed in that exceeds
134 //! UINT16_MAX, the value will be truncated to UINT16_MAX.
135 //!
136 //! Also note that if the timer is operating in periodic mode, the value passed
137 //! into this function will represent the new period of the timer (the value
138 //! which is reloaded into the timer each time it reaches a zero value).
139 //!
140 //! \return None
141 //
142 //*****************************************************************************
143 extern void Timer32_setCount(uint32_t timer, uint32_t count);
144 
145 //*****************************************************************************
146 //
147 //! Sets the count of the timer without resetting the current value. When the
148 //! current value of the timer reaches zero, the value passed into this function
149 //! will be set as the new count value.
150 //!
151 //! \param timer is the instance of the Timer32 module.
152 //! Valid parameters must be one of the following values:
153 //!         - \b TIMER32_0_BASE
154 //!         - \b TIMER32_1_BASE
155 //! \param count Value of the timer to set in the background. Note that
156 //! if the timer is in 16-bit mode and a value is passed in that exceeds
157 //! UINT16_MAX, the value will be truncated to UINT16_MAX.
158 //!
159 //! Also note that if the timer is operating in periodic mode, the value passed
160 //! into this function will represent the new period of the timer (the value
161 //! which is reloaded into the timer each time it reaches a zero value).
162 //!
163 //! \return None
164 //
165 //*****************************************************************************
166 extern void Timer32_setCountInBackground(uint32_t timer, uint32_t count);
167 
168 //*****************************************************************************
169 //
170 //! Returns the current value of the timer.
171 //!
172 //! \param timer is the instance of the Timer32 module.
173 //! Valid parameters must be one of the following values:
174 //!         - \b TIMER32_0_BASE
175 //!         - \b TIMER32_1_BASE
176 //!
177 //! \return The current count of the timer.
178 //
179 //*****************************************************************************
180 extern uint32_t Timer32_getValue(uint32_t timer);
181 
182 //*****************************************************************************
183 //
184 //! Starts the timer. The Timer32_initModule function should be called (in
185 //! conjunction with Timer32_setCount if periodic mode is desired) prior to
186 //  starting the timer.
187 //!
188 //! \param timer is the instance of the Timer32 module.
189 //! Valid parameters must be one of the following values:
190 //!         - \b TIMER32_0_BASE
191 //!         - \b TIMER32_1_BASE
192 //!
193 //! \param oneShot sets whether the Timer32 module operates in one shot
194 //!  or continuous mode. In one shot mode, the timer will halt when a zero is
195 //!  reached and stay halted until either:
196 //!         - The user calls the Timer32PeriodSet function
197 //!         - The Timer32_initModule is called to reinitialize the timer with one-shot
198 //!             mode disabled.
199 //!
200 //! A true value will cause the timer to operate in one shot mode while a false
201 //! value will cause the timer to operate in continuous mode
202 //!
203 //! \return None
204 //
205 //*****************************************************************************
206 extern void Timer32_startTimer(uint32_t timer, bool oneShot);
207 
208 //*****************************************************************************
209 //
210 //! Halts the timer. Current count and setting values are preserved.
211 //!
212 //! \param timer is the instance of the Timer32 module.
213 //! Valid parameters must be one of the following values:
214 //!         - \b TIMER32_0_BASE
215 //!         - \b TIMER32_1_BASE
216 //!
217 //! \return None
218 //
219 //*****************************************************************************
220 extern void Timer32_haltTimer(uint32_t timer);
221 
222 //*****************************************************************************
223 //
224 //! Enables a Timer32 interrupt source.
225 //!
226 //! \param timer is the instance of the Timer32 module.
227 //! Valid parameters must be one of the following values:
228 //!         - \b TIMER32_0_BASE
229 //!         - \b TIMER32_1_BASE
230 //!
231 //! Enables the indicated Timer32 interrupt source.
232 //!
233 //! \return None.
234 //
235 //*****************************************************************************
236 extern void Timer32_enableInterrupt(uint32_t timer);
237 
238 //*****************************************************************************
239 //
240 //! Disables a Timer32 interrupt source.
241 //!
242 //! \param timer is the instance of the Timer32 module.
243 //! Valid parameters must be one of the following values:
244 //!         - \b TIMER32_0_BASE
245 //!         - \b TIMER32_1_BASE
246 //!
247 //! Disables the indicated Timer32 interrupt source.
248 //!
249 //! \return None.
250 //
251 //*****************************************************************************
252 extern void Timer32_disableInterrupt(uint32_t timer);
253 
254 //*****************************************************************************
255 //
256 //! Clears Timer32 interrupt source.
257 //!
258 //! \param timer is the instance of the Timer32 module.
259 //! Valid parameters must be one of the following values:
260 //!         - \b TIMER32_0_BASE
261 //!         - \b TIMER32_1_BASE
262 //!
263 //! The Timer32 interrupt source is cleared, so that it no longer asserts.
264 //!
265 //! \return None.
266 //
267 //*****************************************************************************
268 extern void Timer32_clearInterruptFlag(uint32_t timer);
269 
270 //*****************************************************************************
271 //
272 //! Gets the current Timer32 interrupt status.
273 //!
274 //! \param timer is the instance of the Timer32 module.
275 //! Valid parameters must be one of the following values:
276 //!         - \b TIMER32_0_BASE
277 //!         - \b TIMER32_1_BASE
278 //!
279 //! This returns the interrupt status for the Timer32 module. A positive value
280 //! will indicate that an interrupt is pending while a zero value will indicate
281 //! that no interrupt is pending.
282 //!
283 //! \return The current interrupt status
284 //
285 //*****************************************************************************
286 extern uint32_t Timer32_getInterruptStatus(uint32_t timer);
287 
288 //*****************************************************************************
289 //
290 //! Registers an interrupt handler for Timer32 interrupts.
291 //!
292 //! \param timerInterrupt is the specific interrupt to register. For the
293 //! Timer32 module, there are a total of three different interrupts: one
294 //! interrupt for each two Timer32 modules, and a "combined" interrupt which
295 //! is a logical OR of each individual Timer32 interrupt.
296 //!         - \b TIMER32_0_INTERRUPT
297 //!         - \b TIMER32_1_INTERRUPT
298 //!         - \b TIMER32_COMBINED_INTERRUPT
299 //!
300 //! \param intHandler is a pointer to the function to be called when the
301 //! Timer32 interrupt occurs.
302 //!
303 //! This function registers the handler to be called when an Timer32
304 //! interrupt occurs. This function enables the global interrupt in the
305 //! interrupt controller; specific Timer32 interrupts must be enabled
306 //! via Timer32_enableInterrupt().  It is the interrupt handler's
307 //! responsibility to clear the interrupt source
308 //! via Timer32_clearInterruptFlag().
309 //!
310 //! \return None.
311 //
312 //*****************************************************************************
313 extern void Timer32_registerInterrupt(uint32_t timerInterrupt,
314         void (*intHandler)(void));
315 
316 //*****************************************************************************
317 //
318 //! Unregisters the interrupt handler for the Timer32 interrupt.
319 //!
320 //! \param timerInterrupt is the specific interrupt to register. For the
321 //! Timer32 module, there are a total of three different interrupts: one
322 //! interrupt for each two Timer32 modules, and a "combined" interrupt which
323 //! is a logical OR of each individual Timer32 interrupt.
324 //!         - \b TIMER32_0_INTERRUPT
325 //!         - \b TIMER32_1_INTERRUPT
326 //!         - \b TIMER32_COMBINED_INTERRUPT
327 //!
328 //! This function unregisters the handler to be called when a Timer32
329 //! interrupt occurs.  This function also masks off the interrupt in the
330 //! interrupt controller so that the interrupt handler no longer is called.
331 //!
332 //! \sa Interrupt_registerInterrupt() for important information about
333 //! registering interrupt handlers.
334 //!
335 //! \return None.
336 //
337 //*****************************************************************************
338 extern void Timer32_unregisterInterrupt(uint32_t timerInterrupt);
339 
340 //*****************************************************************************
341 //
342 // Mark the end of the C bindings section for C++ compilers.
343 //
344 //*****************************************************************************
345 #ifdef __cplusplus
346 }
347 #endif
348 
349 //*****************************************************************************
350 //
351 // Close the Doxygen group.
352 //! @}
353 //
354 //*****************************************************************************
355 
356 #endif /* TIMER32_H_ */
357