Skip to content

Commit f17efea

Browse files
committed
Merge pull request #614 from haukepetersen/periph_driver_timer
Initial import of low-level timer driver interface
2 parents 9f24ae2 + 324481f commit f17efea

File tree

1 file changed

+135
-0
lines changed

1 file changed

+135
-0
lines changed

drivers/include/periph/timer.h

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
/*
2+
* Copyright (C) 2014 Freie Universität Berlin
3+
*
4+
* This file is subject to the terms and conditions of the LGPLv2 License.
5+
* See the file LICENSE in the top level directory for more details.
6+
*/
7+
8+
/**
9+
* @ingroup driver_periph
10+
* @brief Low-level timer peripheral driver
11+
* @{
12+
*
13+
* @file timer.h
14+
* @brief Low-level timer peripheral driver interface definitions
15+
*
16+
* @author Hauke Petersen <[email protected]>
17+
*/
18+
19+
#ifndef __TIMER_H
20+
#define __TIMER_H
21+
22+
#include "periph_conf.h"
23+
24+
25+
/**
26+
* @brief Definition of available timers
27+
*
28+
* Each timer is based on a hardware timer, which can further have 1 or more channels.
29+
* To this point 4 timers are possible, might need to be expanded for some cases.
30+
*/
31+
typedef enum {
32+
#if TIMER_0_EN
33+
TIMER_0 = 0, /**< 1st timer */
34+
#endif
35+
#if TIMER_1_EN
36+
TIMER_1, /**< 2nd timer */
37+
#endif
38+
#if TIMER_2_EN
39+
TIMER_2, /**< 3rd timer */
40+
#endif
41+
#if TIMER_3_EN
42+
TIMER_3, /**< 4th timer */
43+
#endif
44+
TIMER_UNDEFINED /**< fall-back if no timer is defined */
45+
} tim_t; /* named tim instead of timer to avoid conflicts with vendor libraries */
46+
47+
/**
48+
* @brief Initialize the given timer
49+
*
50+
* Each timer device is running with the given speed. Each can contain one or more channels
51+
* as defined in periph_conf.h. The timer is configured in up-counting mode and will count
52+
* until TIMER_x_MAX_VALUE as defined in used board's periph_conf.h until overflowing.
53+
*
54+
* The timer will be started automatically after initialization with interrupts enabled.
55+
*
56+
* @param[in] dev the timer to initialize
57+
* @param[in] ticks_per_us the timers speed in ticks per us
58+
* @param[in] callback this callback is called in interrupt context, the emitting channel is
59+
* passed as argument
60+
*
61+
* @return returns 0 on success, -1 if speed not applicable of unknown device given
62+
*/
63+
int timer_init(tim_t dev, unsigned int ticks_per_us, void (*callback)(int));
64+
65+
/**
66+
* @brief Set a given timer channel for the given timer device. The callback given during
67+
* initialization is called when timeout ticks have passed after calling this function
68+
*
69+
* @param[in] dev the timer device to set
70+
* @param[in] channel the channel to set
71+
* @param[in] timeout timeout in ticks after that the registered callback is executed
72+
*
73+
* @return 1 on success, -1 on error
74+
*/
75+
int timer_set(tim_t dev, int channel, unsigned int timeout);
76+
77+
/**
78+
* @brief Clear the given channel of the given timer device
79+
*
80+
* @param[in] dev the timer device to clear
81+
* @param[in] channel the channel on the given device to clear
82+
*
83+
* @return 1 on success, -1 on error
84+
*/
85+
int timer_clear(tim_t dev, int channel);
86+
87+
/**
88+
* @brief Read the current value of the given timer device
89+
*
90+
* @param[in] dev the timer to read the current value from
91+
*
92+
* @return the timers current value
93+
*/
94+
unsigned int timer_read(tim_t dev);
95+
96+
/**
97+
* @brief Start the given timer. This function is only needed if the timer was stopped manually before
98+
*
99+
* @param[in] dev the timer device to stop
100+
*/
101+
void timer_start(tim_t dev);
102+
103+
/**
104+
* @brief Stop the given timer - this will effect all of the timer's channels
105+
*
106+
* @param[in] dev the timer to stop
107+
*/
108+
void timer_stop(tim_t dev);
109+
110+
/**
111+
* @brief Enable the interrupts for the given timer
112+
*
113+
* @param[in] dev timer to enable interrupts for
114+
*/
115+
void timer_irq_enable(tim_t dev);
116+
117+
/**
118+
* @brief Disable interrupts for the given timer
119+
*
120+
* @param[in] dev the timer to disable interrupts for
121+
*/
122+
void timer_irq_disable(tim_t dev);
123+
124+
/**
125+
* @brief Reset the up-counting value to zero for the given timer
126+
*
127+
* Note that this function effects all currently set channels and it can lead to non-deterministic timeouts
128+
* if any channel is active when this function is called.
129+
*
130+
* @param[in] dev the timer to reset
131+
*/
132+
void timer_reset(tim_t dev);
133+
134+
#endif /* __TIMER_H */
135+
/** @} */

0 commit comments

Comments
 (0)