Timer.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2016-2020, 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  */
32 /*!*****************************************************************************
33  * @file Timer.h
34  * @brief Timer driver
35  *
36  * @anchor ti_drivers_Timer_Overview
37  * # Overview
38  * The timer driver allows you to measure elapsed time with simple and
39  * portable APIs.This driver does not have PWM or capture functionalities.
40  * These functionalities are addressed in both the capture and PWM driver.
41  *
42  * The timer driver also handles the general purpose timer resource allocation.
43  * For each driver that requires use of a general purpose timer, it calls
44  * Timer_open() to occupy the specified timer, and calls Timer_close() to
45  * release the occupied timer resource.
46  *
47  * @anchor ti_drivers_Timer_Usage
48  * # Usage
49  *
50  * This documentation provides a basic @ref ti_drivers_Timer_Synopsis
51  * "usage summary" and a set of @ref ti_drivers_Timer_Examples "examples"
52  * in the form of commented code fragments. Detailed descriptions of the
53  * APIs are provided in subsequent sections.
54  *
55  * @anchor ti_drivers_Timer_Synopsis
56  * ## Synopsis
57  * @anchor ti_drivers_Timer_Synopsis_Code
58  * @code
59  * // Import Timer Driver definitions
60  * #include <ti/drivers/Timer.h>
61  *
62  * Timer_Handle handle;
63  * Timer_Params params;
64  *
65  * Timer_Params_init(&params);
66  * params.periodUnits = Timer_PERIOD_HZ;
67  * params.period = 1000;
68  * params.timerMode = Timer_CONTINUOUS_CALLBACK;
69  * params.timerCallback = UserCallbackFunction;
70  *
71  * handle = Timer_open(CONFIG_TIMER0, &params);
72  *
73  * @code
74  * // Import Timer Driver definitions
75  * #include <ti/drivers/Timer.h>
76  *
77  * Timer_Handle handle;
78  * Timer_Params params;
79  *
80  * // Initialize Timer parameters
81  * Timer_Params_init(&params);
82  * params.periodUnits = Timer_PERIOD_HZ;
83  * params.period = 1000;
84  * params.timerMode = Timer_CONTINUOUS_CALLBACK;
85  * params.timerCallback = UserCallbackFunction;
86  *
87  * // Open Timer instance
88  * handle = Timer_open(CONFIG_TIMER0, &params);
89  *
90  * sleep(10000);
91  *
92  * Timer_stop(handle);
93  * @endcode
94  *
95  * <hr>
96  * @anchor ti_drivers_Timer_Examples
97  * # Examples
98  *
99  * @li @ref ti_drivers_Timer_Examples_open "Opening a Timer Instance"
100  * @li @ref ti_drivers_Timer_Examples_mode "Configuring Timer mode and period"
101  *
102  * @anchor ti_drivers_Timer_Examples_open
103  * ## Opening a Timer instance
104  *
105  * @code
106  * Timer_Handle handle;
107  * Timer_Params params;
108  *
109  * Timer_Params_init(&params);
110  * handle = Timer_open(CONFIG_TIMER0, &params);
111  *
112  * if (handle == NULL) {
113  * // Timer_open() failed
114  * while (1);
115  * }
116  @endcode
117  *
118  * @anchor ti_drivers_Timer_Examples_mode
119  * ##Configuring Timer mode and period
120  *
121  * The following example code opens a timer in continuous callback mode. The
122  * period is set to 1000 Hz.
123  *
124  * @code
125  * Timer_Handle handle;
126  * Timer_Params params;
127  *
128  * Timer_Params_init(&params);
129  * params.periodUnits = Timer_PERIOD_HZ;
130  * params.period = 1000;
131  * params.timerMode = Timer_CONTINUOUS_CALLBACK;
132  * params.timerCallback = UserCallbackFunction;
133  *
134  * handle = Timer_open(CONFIG_TIMER0, &params);
135  *
136  * if (handle == NULL) {
137  * // Timer_open() failed
138  * while (1);
139  * }
140  *
141  * status = Timer_start(handle);
142  *
143  * if (status == Timer_STATUS_ERROR) {
144  * //Timer_start() failed
145  * while (1);
146  * }
147  *
148  * sleep(10000);
149  *
150  * Timer_stop(handle);
151  * @endcode
152  *
153  * ### Initializing the Timer Driver #
154  *
155  * Timer_init() must be called before any other timer APIs. This function
156  * calls the device implementation's timer initialization function, for each
157  * element of Timer_config[].
158  *
159  * <hr>
160  * @anchor ti_drivers_Timer_Configuration
161  * # Configuration
162  *
163  * Refer to the @ref driver_configuration "Driver's Configuration" section
164  * for driver configuration information.
165  * <hr>
166 *******************************************************************************
167  */
168 
169 #ifndef ti_drivers_Timer__include
170 #define ti_drivers_Timer__include
171 
172 #include <stdint.h>
173 
174 #include <ti/drivers/dpl/HwiP.h>
175 #include <ti/drivers/dpl/SemaphoreP.h>
176 #include <ti/drivers/Power.h>
177 
178 #ifdef __cplusplus
179 extern "C"
180 {
181 #endif
182 
194 #define Timer_CMD_RESERVED (32)
195 
207 #define Timer_STATUS_RESERVED (-32)
208 
212 #define Timer_STATUS_SUCCESS (0)
213 
217 #define Timer_STATUS_ERROR (-1)
218 
226 #define Timer_STATUS_UNDEFINEDCMD (-2)
227 
231 typedef struct Timer_Config_ *Timer_Handle;
232 
243 typedef enum {
278 } Timer_Mode;
279 
286 typedef enum {
293 
303 typedef void (*Timer_CallBackFxn)(Timer_Handle handle, int_fast16_t status);
304 
312 typedef struct {
314  Timer_Mode timerMode;
315 
317  Timer_PeriodUnits periodUnits;
318 
322 
324  uint32_t period;
325 } Timer_Params;
326 
328 #define TIMER_BASE_OBJECT \
329  /* Timer control variables */ \
330  Timer_Mode mode; /* Blocking or Callback mode */ \
331  Timer_CallBackFxn callBack; /* Callback function pointer */ \
332  Power_NotifyObj notifyObj; /* Ptr to current I2C transaction */ \
333  uint32_t timer; \
334  uint32_t period; \
335  uint32_t prescaler; \
336  \
337  /* Timer RTOS objects */ \
338  HwiP_Handle hwiHandle; /* Hwi object handle */ \
339  SemaphoreP_Struct semStruct; /* Grants exclusive access to I2C */ \
340  SemaphoreP_Handle semHandle; /* Signal I2C transfer complete */ \
341  \
342  bool isRunning; /* Flag to show module is open */ \
343 
350 typedef struct {
351  TIMER_BASE_OBJECT
352 } Timer_Object;
356 #define TIMER_BASE_HWATTRS \
357  \
358  uint32_t baseAddress; \
359  \
360  uint32_t intNum; \
361  \
362  uint32_t intPriority;
363 
369 typedef struct {
370  TIMER_BASE_HWATTRS
371 } Timer_HWAttrs;
385 typedef struct Timer_Config_ {
387  void *object;
388 
390  void const *hwAttrs;
391 } Timer_Config;
392 
397 extern const Timer_Config Timer_config[];
410 extern void Timer_close(Timer_Handle handle);
411 
431 extern int_fast16_t Timer_control(Timer_Handle handle, uint_fast16_t cmd,
432  void *arg);
433 
450 extern uint32_t Timer_getCount(Timer_Handle handle);
451 
452 
463 extern void Timer_init(void);
464 
489 extern Timer_Handle Timer_open(uint_least8_t index, Timer_Params *params);
490 
510 extern int32_t Timer_setPeriod(Timer_Handle handle, Timer_PeriodUnits periodUnits, uint32_t period);
511 
525 
538 extern int32_t Timer_start(Timer_Handle handle);
539 
550 extern void Timer_stop(Timer_Handle handle);
551 
552 #ifdef __cplusplus
553 }
554 #endif
555 
556 #endif /* ti_drivers_Timer__include */
ADC_Params params
Definition: Driver_Init.h:11
Timer_CallBackFxn timerCallback
Definition: Timer.h:321
Timer_PeriodUnits
Timer period unit enum.
Definition: Timer.h:286
Timer_PeriodUnits periodUnits
Definition: Timer.h:317
Timer_Handle Timer_open(uint_least8_t index, Timer_Params *params)
Function to initialize a given timer peripheral specified by the index argument. The Timer_Params spe...
Definition: Timer.h:272
void Timer_init(void)
Function to initialize a timer. This function will go through all available hardware resources and ma...
Power Manager.
Definition: Timer.h:288
struct Timer_Config_ Timer_Config
Timer Global configuration.
void const * hwAttrs
Definition: Timer.h:390
void Timer_Params_init(Timer_Params *params)
Function to initialize the Timer_Params struct to its defaults.
Timer Global configuration.
Definition: Timer.h:385
void(* Timer_CallBackFxn)(Timer_Handle handle, int_fast16_t status)
Timer callback function.
Definition: Timer.h:303
Definition: Timer.h:287
int32_t Timer_setPeriod(Timer_Handle handle, Timer_PeriodUnits periodUnits, uint32_t period)
Function to set the period of a timer after it has been opened.
void Timer_close(Timer_Handle handle)
Function to close a timer. The corresponding timer becomes an available timer resource.
Definition: Timer.h:263
void * object
Definition: Timer.h:387
Definition: Timer.h:290
int_fast16_t Timer_control(Timer_Handle handle, uint_fast16_t cmd, void *arg)
Function performs device-specific features on a given timer.
uint32_t Timer_getCount(Timer_Handle handle)
Function to get the current count of a timer. The value returned represents timer counts...
Timer Parameters.
Definition: Timer.h:312
void Timer_stop(Timer_Handle handle)
Function to stop a timer. If the timer is already stopped this function has no effect.
Timer_Mode
Timer mode settings.
Definition: Timer.h:243
uint32_t period
Definition: Timer.h:324
Definition: Timer.h:255
Definition: Timer.h:244
Timer_Mode timerMode
Definition: Timer.h:314
int32_t Timer_start(Timer_Handle handle)
Function to start a timer.
struct Timer_Config_ * Timer_Handle
A handle that is returned from a Timer_open() call.
Definition: Timer.h:231
© Copyright 1995-2022, Texas Instruments Incorporated. All rights reserved.
Trademarks | Privacy policy | Terms of use | Terms of sale