2 * Copyright (c) 2016, Texas Instruments Incorporated
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
9 * * Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
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.
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.
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.
32 /** ============================================================================
35 * @brief Timer driver interface
37 * The Timer header file should be included in an application as follows:
39 * #include <ti/drivers/Timer.h>
43 * The Timer driver operates as a generic timer interface for timing interval
44 * handling. It can be configured to run in one-shot blocking mode,
45 * one-shot callback mode, continuous callback mode, or free-run mode. This
46 * driver does not have PWM or capture functionalities. These functionalities
47 * are addressed in both the Capture and PWM driver modules.
49 * The Timer driver also handles the general purpose timer resource allocation.
50 * For the driver that requires to use the general purpose timer, it calls
51 * Timer_open() to occupy the specified timer, and calls Timer_close() to release
52 * the occupied timer resource.
54 * ## Opening the Driver ##
57 * Timer_Handle handle;
58 * Timer_Params params;
60 * Timer_Params_init(¶ms);
61 * params.mode = TIMER_MODE_CONTINUOUS_CALLBACK;
62 * params.callbackFxn = someTimerCallbackFunction;
63 * params.periodUnit = TIMER_PERIOD_US;
64 * params.period = 5000000
65 * handle = Timer_open(someTimer_configIndexValue, ¶ms);
68 * System_printf("Timer did not open");
71 * ## Starting the Driver ##
74 * status = Timer_start(handle);
75 * if (status == TIMER_STATUS_ERROR)
77 * System_printf("Timer cannot start at specified period");
81 * ## Stopping the driver ##
87 * ## closing the driver ##
90 * Timer_close(handle);
95 * This module serves as the main interface for TI Drivers
96 * applications. Its purpose is to redirect the module's APIs to specific
97 * peripheral implementations which are specified using a pointer to a
100 * The Timer driver interface module is joined (at link time) to a
101 * NULL-terminated array of Timer_Config data structures named *Timer_config*.
102 * *Timer_config* is implemented in the application with each entry being an
103 * instance of a Timer peripheral. Each entry in *Timer_config* contains a:
104 * - (Timer_FxnTable *) to a set of functions that implement a Timer peripheral
105 * - (void *) data object that is associated with the Timer_FxnTable
106 * - (void *) hardware attributes that are associated to the Timer_FxnTable
108 * # Instrumentation #
109 * The Timer driver interface produces log statements if instrumentation is
112 * ============================================================================
114 #ifndef ti_drivers_Timer__include
115 #define ti_drivers_Timer__include
125 * @brief A handle that is returned from a Timer_open() call.
127 typedef struct Timer_Config_ *Timer_Handle;
130 * Common Timer_control command code reservation offset.
131 * Timer driver implementations should offset command codes with TIMER_CMD_RESERVED
134 * Example implementation specific command codes:
136 * #define TIMERXYZ_CMD_COMMAND0 TIMER_CMD_RESERVED + 0
137 * #define TIMERXYZ_CMD_COMMAND1 TIMER_CMD_RESERVED + 1
140 #define TIMER_CMD_RESERVED (32)
143 * Common Timer_control status code reservation offset.
144 * Timer driver implementations should offset status codes with
145 * TIMER_STATUS_RESERVED growing negatively.
147 * Example implementation specific status codes:
149 * #define TIMERXYZ_STATUS_ERROR0 TIMER_STATUS_RESERVED - 0
150 * #define TIMERXYZ_STATUS_ERROR1 TIMER_STATUS_RESERVED - 1
151 * #define TIMERXYZ_STATUS_ERROR2 TIMER_STATUS_RESERVED - 2
154 #define TIMER_STATUS_RESERVED (-32)
157 * @brief Successful status code returned by Timer_control().
159 * Timer_control() returns TIMER_STATUS_SUCCESS if the control code was executed
162 #define TIMER_STATUS_SUCCESS (0)
165 * @brief Generic error status code returned by Timer_control().
167 * Timer_control() returns TIMER_STATUS_ERROR if the control code was not executed
170 #define TIMER_STATUS_ERROR (-1)
173 * @brief An error status code returned by Timer_control() for undefined
176 * Timer_control() returns TIMER_STATUS_UNDEFINEDCMD if the control code is not
177 * recognized by the driver implementation.
179 #define TIMER_STATUS_UNDEFINEDCMD (-2)
182 * @brief Timer mode enum
184 * The Timer mode needs to be passed in Timer_open() to specify the timer running
185 * mode which handles the interrupt differently.
188 typedef enum Timer_Mode_
190 TIMER_ONESHOT_CB, /*!< User routine doesn't get blocked and user-specified
191 callback function is invoked once the timer interrupt happens for only one time */
192 TIMER_ONESHOT_BLOCK, /*!< User routine gets blocked until timer interrupt
193 happens for only one time */
194 TIMER_CONTINUOUS_CB, /*!< User routine doesn't get blocked and user-specified
195 callback function is invoked every time the timer interrupt happens */
196 TIMER_MODE_FREE_RUNNING
200 * @brief Timer period unit enum
202 * The Timer period unit needs to be passed in Timer_open() to
203 * specify the unit of timing interval.
206 typedef enum Timer_Period_Units_
208 TIMER_PERIOD_US, /* Period in microseconds */
209 TIMER_PERIOD_HZ, /* Period in frequency */
210 TIMER_PERIOD_COUNTS /* Period in counts */
211 } Timer_Period_Units;
214 * @brief Timer callback function
216 * User definable callback function prototype. The Timer driver will call the
217 * defined function and pass in the Timer driver's handle and the pointer to the
218 * user-specified the argument.
220 * @param handle Timer_Handle
222 typedef void (*Timer_CallBackFxn)(Timer_Handle handle);
225 * @brief Timer Parameters
227 * Timer parameters are used to with the Timer_open() call. Default values for
228 * these parameters are set using Timer_Params_init().
231 typedef struct Timer_Params_
233 Timer_Mode timerMode;
234 Timer_Period_Units periodUnits;
235 Timer_CallBackFxn timerCallback;
240 * @brief A function pointer to a driver specific implementation of
243 typedef int_fast16_t (*Timer_ControlFxn)(Timer_Handle handle, uint_fast16_t cmd,
247 * @brief A function pointer to a driver specific implementation of
250 typedef void (*Timer_CloseFxn)(Timer_Handle handle);
253 * @brief A function pointer to a driver specific implementation of
256 typedef uint32_t (*Timer_GetCountFxn)(Timer_Handle handle);
259 * @brief A function pointer to a driver specific implementation of
262 typedef void (*Timer_InitFxn)(Timer_Handle handle);
265 * @brief A function pointer to a driver specific implementation of
268 typedef Timer_Handle (*Timer_OpenFxn)(Timer_Handle handle,
269 Timer_Params *params);
272 * @brief A function pointer to a driver specific implementation of
275 typedef void (*Timer_StartFxn)(Timer_Handle handle);
278 * @brief A function pointer to a driver specific implementation of
281 typedef void (*Timer_StopFxn)(Timer_Handle handle);
284 * @brief The definition of a Timer function table that contains the
285 * required set of functions to control a specific Timer driver
288 typedef struct Timer_FxnTable_
290 /*! Function to close the specified instance */
291 Timer_CloseFxn closeFxn;
292 /*! Function to send contorl commands to the counter for a specific instance */
293 Timer_ControlFxn controlFxn;
294 /*! Function to get the count of the timer for a specific instance */
295 Timer_GetCountFxn getCountFxn;
296 /*! Function to open the specified instance */
297 Timer_InitFxn initFxn;
298 /*! Function to open the specified instance */
299 Timer_OpenFxn openFxn;
300 /*! Function to start the timer for a specific instance */
301 Timer_StartFxn startFxn;
302 /*! Function to stop the timer for a specific instance */
303 Timer_StopFxn stopFxn;
306 typedef struct Timer_Config_
308 Timer_FxnTable const *fxnTablePtr;
314 * @brief Function performs implementation specific features on a given
317 * @pre Timer_open() must have been called first.
319 * @param handle A Timer_Handle returned from Timer_open().
321 * @param cmd A command value defined by the driver specific
324 * @param arg A pointer to an optional R/W (read/write) argument that
325 * is accompanied with cmd.
327 * @return A Timer_Status describing an error or success state. Negative values
328 * indicate an error occurred.
332 extern int_fast16_t Timer_control(Timer_Handle handle, uint_fast16_t cmd,
337 * @brief Function to close a Timer peripheral specified by the Timer handle
339 * The function takes care of timer resource allocation. The corresponding timer
340 * resource to the Timer_Handle is released to be an available timer resource.
342 * @pre Timer_open() had to be called first.
344 * @param handle A Timer_Handle returned from Timer_open
348 extern void Timer_close(Timer_Handle handle);
351 * @brief Function to get the current count of a started timer
353 * @pre Timer_open() had to be called first.
355 * @param handle A Timer_Handle returned from Timer_open
359 * @return The current count of the specified Timer
362 extern uint32_t Timer_getCount(Timer_Handle handle);
366 * @brief Function to initialize a timer module. This function will go through
367 * all available hardware resources and mark them as "available"
371 extern void Timer_init(void);
374 * @brief Function to initialize a given Timer peripheral specified by the
375 * particular index value. The parameter specifies which mode the Timer
378 * The function takes care of timer resource allocation. If the particular timer
379 * passed by user has already been used by other modules, the return value is NULL.
380 * If the particular timer is available to use, Timer module owns it and returns
383 * @param index Logical peripheral number for the Timer indexed into
384 * the Timer_config table
386 * @param params Pointer to an parameter block, if NULL it will use
387 * default values. All the fields in this structure are
390 * @return A Timer_Handle on success or a NULL on an error if it has been
391 * opened already or used by other modules.
396 extern Timer_Handle Timer_open(uint_least8_t index, Timer_Params *params);
399 * @brief Function to initialize the Timer_Params struct to its defaults
401 * @param params An pointer to Timer_Params structure for
404 * Defaults values are:
405 * mode = TIMER_MODE_ONESHOT_BLOCKING
407 * periodUnit = TIMER_PERIOD_US
410 extern void Timer_Params_init(Timer_Params *params);
413 * @brief Function to start Timer with the given period. The timer running mode
414 * and interval period unit are specified in the Timer_Params when calling
417 * @param handle Timer_Handle
419 * @return TIMER_STATUS_SUCCESS if timer starts successfully.
420 * TIMER_STATUS_ERROR if timer fails to start.
423 extern void Timer_start(Timer_Handle handle);
426 * @brief Function to stop timer after Timer_start() is called with success.
428 * @param handle Timer_Handle
431 extern void Timer_stop(Timer_Handle handle);
437 #endif /* ti_driver_Timer__include */