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 #endif
181 
193 #define Timer_CMD_RESERVED (32)
194 
206 #define Timer_STATUS_RESERVED (-32)
207 
211 #define Timer_STATUS_SUCCESS (0)
212 
216 #define Timer_STATUS_ERROR (-1)
217 
225 #define Timer_STATUS_UNDEFINEDCMD (-2)
226 
230 typedef struct Timer_Config_ *Timer_Handle;
231 
242 typedef enum
243 {
278 } Timer_Mode;
279 
286 typedef enum
287 {
294 
304 typedef void (*Timer_CallBackFxn)(Timer_Handle handle, int_fast16_t status);
305 
313 typedef struct
314 {
316  Timer_Mode timerMode;
317 
319  Timer_PeriodUnits periodUnits;
320 
324 
326  uint32_t period;
327 } Timer_Params;
328 
330 #define TIMER_BASE_OBJECT \
331  /* Timer control variables */ \
332  Timer_Mode mode; /* Blocking or Callback mode */ \
333  Timer_CallBackFxn callBack; /* Callback function pointer */ \
334  Power_NotifyObj notifyObj; /* Ptr to current I2C transaction */ \
335  uint32_t timer; \
336  uint32_t period; \
337  uint32_t prescaler; \
338  \
339  /* Timer RTOS objects */ \
340  HwiP_Handle hwiHandle; /* Hwi object handle */ \
341  SemaphoreP_Struct semStruct; /* Grants exclusive access to I2C */ \
342  SemaphoreP_Handle semHandle; /* Signal I2C transfer complete */ \
343  \
344  bool isRunning; /* Flag to show module is open */ \
345 
352 typedef struct
353 {
354  TIMER_BASE_OBJECT
355 } Timer_Object;
359 #define TIMER_BASE_HWATTRS \
360  \
361  uint32_t baseAddress; \
362  \
363  uint32_t intNum; \
364  \
365  uint32_t intPriority;
366 
372 typedef struct
373 {
374  TIMER_BASE_HWATTRS
375 } Timer_HWAttrs;
389 typedef struct Timer_Config_
390 {
392  void *object;
393 
395  void const *hwAttrs;
396 } Timer_Config;
397 
402 extern const Timer_Config Timer_config[];
415 extern void Timer_close(Timer_Handle handle);
416 
436 extern int_fast16_t Timer_control(Timer_Handle handle, uint_fast16_t cmd, void *arg);
437 
454 extern uint32_t Timer_getCount(Timer_Handle handle);
455 
466 extern void Timer_init(void);
467 
492 extern Timer_Handle Timer_open(uint_least8_t index, Timer_Params *params);
493 
513 extern int32_t Timer_setPeriod(Timer_Handle handle, Timer_PeriodUnits periodUnits, uint32_t period);
514 
528 
541 extern int32_t Timer_start(Timer_Handle handle);
542 
553 extern void Timer_stop(Timer_Handle handle);
554 
555 #ifdef __cplusplus
556 }
557 #endif
558 
559 #endif /* ti_drivers_Timer__include */
ADC_Params params
Definition: Driver_Init.h:11
Timer_CallBackFxn timerCallback
Definition: Timer.h:323
Timer_PeriodUnits
Timer period unit enum.
Definition: Timer.h:286
Timer_PeriodUnits periodUnits
Definition: Timer.h:319
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:289
struct Timer_Config_ Timer_Config
Timer Global configuration.
void const * hwAttrs
Definition: Timer.h:395
void Timer_Params_init(Timer_Params *params)
Function to initialize the Timer_Params struct to its defaults.
Timer Global configuration.
Definition: Timer.h:389
void(* Timer_CallBackFxn)(Timer_Handle handle, int_fast16_t status)
Timer callback function.
Definition: Timer.h:304
Definition: Timer.h:288
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:392
Definition: Timer.h:291
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:313
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:242
uint32_t period
Definition: Timer.h:326
Definition: Timer.h:255
Definition: Timer.h:244
Timer_Mode timerMode
Definition: Timer.h:316
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:230
© Copyright 1995-2022, Texas Instruments Incorporated. All rights reserved.
Trademarks | Privacy policy | Terms of use | Terms of sale