libopencm3
A free/libre/open-source firmware library for various ARM Cortex-M3 microcontrollers.
timer.c
Go to the documentation of this file.
1/** @addtogroup timer_file TIMER peripheral API
2 *
3 * @brief <b>Access functions for the Timer/Counter </b>
4 *
5 * @ingroup peripheral_apis
6 * LGPL License Terms @ref lgpl_license
7 * @author @htmlonly &copy; @endhtmlonly 2016
8 * Maxim Sloyko <maxims@google.com>
9 *
10 */
11
12/*
13 * This file is part of the libopencm3 project.
14 *
15 * Copyright (C) 2017-2018 Unicore MX project<dev(at)lists(dot)unicore-mx(dot)org>
16 * Copyright (C) 2021 Eduard Drusa <ventyl86(at)netkosice(dot)sk>
17 *
18 * This library is free software: you can redistribute it and/or modify
19 * it under the terms of the GNU Lesser General Public License as published by
20 * the Free Software Foundation, either version 3 of the License, or
21 * (at your option) any later version.
22 *
23 * This library is distributed in the hope that it will be useful,
24 * but WITHOUT ANY WARRANTY; without even the implied warranty of
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 * GNU Lesser General Public License for more details.
27 *
28 * You should have received a copy of the GNU Lesser General Public License
29 * along with this library. If not, see <http://www.gnu.org/licenses/>.
30 */
31
34/**@{*/
35
36/** @brief Get timer ticks
37 *
38 * @param[in] timer uint32_t timer base
39 * @returns current ticks value
40 */
41uint32_t timer_get_ticks(uint32_t timer)
42{
43 uint32_t ticks;
44 uint32_t cc;
45
46 /* TODO: Check WTF is this doing? */
47 cc = TIMER_CC(0, 0);
48 TIMER_TASK_CAPTURE(timer, 0) = 1;
49 ticks = TIMER_CC(timer, 0);
50 TIMER_CC(timer, 0) = cc;
51 return ticks;
52}
53
54/** @brief Set timer mode (counter/timer)
55 *
56 * @param[in] timer uint32_t timer base
57 * @param[in] mode enum timer_mode
58 */
59void timer_set_mode(uint32_t timer, enum timer_mode mode)
60{
61 TIMER_MODE(timer) = mode;
62}
63
64/** @brief Set timer bit mode (width)
65 *
66 * @param[in] timer uint32_t timer base
67 * @param[in] bitmode enum timer_bitmode
68 */
69void timer_set_bitmode(uint32_t timer, enum timer_bitmode bitmode)
70{
71 TIMER_BITMODE(timer) = bitmode;
72}
73
74/** @brief Start the timer
75 *
76 * @param[in] timer uint32_t timer base
77 */
78void timer_start(uint32_t timer)
79{
81}
82
83/** @brief Stop the timer
84 *
85 * @param[in] timer uint32_t timer base
86 */
87void timer_stop(uint32_t timer)
88{
90}
91
92/** @brief Clear the timer
93 *
94 * @param[in] timer uint32_t timer base
95 */
96void timer_clear(uint32_t timer)
97{
99}
100
101/** @brief Set prescaler value
102 *
103 * @param[in] timer uint32_t timer base
104 * @param[in] presc uint8_t prescaler value
105 */
106void timer_set_prescaler(uint32_t timer, uint8_t presc)
107{
108 TIMER_PRESCALER(timer) = presc & TIMER_PRESCALER_MASK;
109}
110
111/** @brief Set compare register
112 *
113 * @param[in] timer uint32_t timer base
114 * @param[in] compare_num uint8_t compare number (0-3)
115 * @param[in] compare_val uint32_t compare value
116 */
117void timer_set_compare(uint32_t timer, uint8_t compare_num, uint32_t compare_val)
118{
119 if (compare_num > 3) {
120 return;
121 }
122
123 TIMER_CC(timer, compare_num) = compare_val;
124}
125
126/** @brief Get the timer tick frequency
127 *
128 * @param[in] timer uint32_t timer base
129 * @returns frequency of ticking
130 */
131uint32_t timer_get_freq(uint32_t timer)
132{
133 return CLOCK_PCLK/(1<<TIMER_PRESCALER(timer));
134}
135
136/** @brief Get compare register
137 *
138 * @param[in] timer uint32_t timer base
139 * @param[in] compare_num uint8_t compare number (0-3)
140 * @returns compare register value
141 */
142uint32_t timer_get_cc(uint32_t timer, uint8_t compare_num)
143{
144 return TIMER_CC(timer, compare_num);
145}
146/**@}*/
147
#define CLOCK_PCLK
Definition: 51/clock.h:44
#define PERIPH_TRIGGER_TASK(task)
timer_mode
Definition: common/timer.h:91
#define TIMER_PRESCALER_MASK
Definition: common/timer.h:70
#define TIMER_TASK_CLEAR(T)
Definition: common/timer.h:51
#define TIMER_PRESCALER(T)
Definition: common/timer.h:69
#define TIMER_TASK_START(T)
Definition: common/timer.h:48
#define TIMER_MODE(T)
Definition: common/timer.h:67
#define TIMER_TASK_CAPTURE(T, C)
Definition: common/timer.h:53
#define TIMER_BITMODE(T)
Definition: common/timer.h:68
#define TIMER_CC(T, C)
Definition: common/timer.h:72
timer_bitmode
Definition: common/timer.h:96
#define TIMER_TASK_STOP(T)
Definition: common/timer.h:49
uint32_t timer_get_ticks(uint32_t timer)
Get timer ticks.
Definition: timer.c:41
void timer_set_mode(uint32_t timer, enum timer_mode mode)
Set timer mode (counter/timer)
Definition: timer.c:59
void timer_set_bitmode(uint32_t timer, enum timer_bitmode bitmode)
Set timer bit mode (width)
Definition: timer.c:69
uint32_t timer_get_freq(uint32_t timer)
Get the timer tick frequency.
Definition: timer.c:131
void timer_set_prescaler(uint32_t timer, uint8_t presc)
Set prescaler value.
Definition: timer.c:106
void timer_clear(uint32_t timer)
Clear the timer.
Definition: timer.c:96
void timer_set_compare(uint32_t timer, uint8_t compare_num, uint32_t compare_val)
Set compare register.
Definition: timer.c:117
uint32_t timer_get_cc(uint32_t timer, uint8_t compare_num)
Get compare register.
Definition: timer.c:142
void timer_start(uint32_t timer)
Start the timer.
Definition: timer.c:78
void timer_stop(uint32_t timer)
Stop the timer.
Definition: timer.c:87