|
| 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 dev the timer to initialize |
| 57 | + * @param ticks_per_us the timers speed in ticks per us |
| 58 | + * @param callback this callback is called in interrupt context, the emitting channel is |
| 59 | + * passed as argument |
| 60 | + * @return returns 0 on success, -1 if speed not applicable of unknown device given |
| 61 | + */ |
| 62 | +int timer_init(tim_t dev, unsigned int ticks_per_us, void (*callback)(int)); |
| 63 | + |
| 64 | +/** |
| 65 | + * @brief Set a given timer channel for the given timer device. The callback given during |
| 66 | + * initialization is called when timeout ticks have passed after calling this function |
| 67 | + * |
| 68 | + * @param dev the timer device to set |
| 69 | + * @param channel the channel to set |
| 70 | + * @param timeout timeout in ticks after that the registered callback is executed |
| 71 | + * @return 1 on success, -1 on error |
| 72 | + */ |
| 73 | +int timer_set(tim_t dev, int channel, unsigned int timeout); |
| 74 | + |
| 75 | +/** |
| 76 | + * @brief Clear the given channel of the given timer device |
| 77 | + * |
| 78 | + * @param dev the timer device to clear |
| 79 | + * @param channel the channel on the given device to clear |
| 80 | + * @return 1 on success, -1 on error |
| 81 | + */ |
| 82 | +int timer_clear(tim_t dev, int channel); |
| 83 | + |
| 84 | +/** |
| 85 | + * @brief Read the current value of the given timer device |
| 86 | + * |
| 87 | + * @param dev the timer to read the current value from |
| 88 | + * @return the timers current value |
| 89 | + */ |
| 90 | +unsigned int timer_read(tim_t dev); |
| 91 | + |
| 92 | +/** |
| 93 | + * @brief Start the given timer. This function is only needed if the timer was stopped manually before |
| 94 | + * |
| 95 | + * @param dev the timer device to stop |
| 96 | + */ |
| 97 | +void timer_start(tim_t dev); |
| 98 | + |
| 99 | +/** |
| 100 | + * @brief Stop the given timer - this will effect all of the timer's channels |
| 101 | + * |
| 102 | + * @param dev the timer to stop |
| 103 | + */ |
| 104 | +void timer_stop(tim_t dev); |
| 105 | + |
| 106 | +/** |
| 107 | + * @brief Enable the interrupts for the given timer |
| 108 | + * |
| 109 | + * @param dev timer to enable interrupts for |
| 110 | + */ |
| 111 | +void timer_irq_enable(tim_t dev); |
| 112 | + |
| 113 | +/** |
| 114 | + * @brief Disable interrupts for the given timer |
| 115 | + * |
| 116 | + * @param dev the timer to disable interrupts for |
| 117 | + */ |
| 118 | +void timer_irq_disable(tim_t dev); |
| 119 | + |
| 120 | +/** |
| 121 | + * @brief Reset the up-counting value to zero for the given timer |
| 122 | + * |
| 123 | + * Note that this function effects all currently set channels and it can lead to non-deterministic timeouts |
| 124 | + * if any channel is active when this function is called. |
| 125 | + * |
| 126 | + * @param dev the timer to reset |
| 127 | + */ |
| 128 | +void timer_reset(tim_t dev); |
| 129 | + |
| 130 | +#endif /* __TIMER_H */ |
| 131 | +/** @} */ |
0 commit comments