libopencm3
A free/libre/open-source firmware library for various ARM Cortex-M3 microcontrollers.
common/timer.h
Go to the documentation of this file.
1/** @addtogroup timer_defines
2 *
3 * @author @htmlonly &copy; @endhtmlonly 2016 Maxim Sloyko <maxims@google.com>
4 * @author @htmlonly &copy; @endhtmlonly 2021 Eduard Drusa <ventyl86 at netkosice dot sk>
5 *
6 **/
7
8/*
9 * This file is part of the libopencm3 project.
10 *
11 * Copyright (C) 2017-2018 Unicore MX project<dev(at)lists(dot)unicore-mx(dot)org>
12 * Copyright (C) 2021 Eduard Drusa <ventyl86(at)netkosice(dot)sk>
13 *
14 * This library is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License as published by
16 * the Free Software Foundation, either version 3 of the License, or
17 * (at your option) any later version.
18 *
19 * This library is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU Lesser General Public License for more details.
23 *
24 * You should have received a copy of the GNU Lesser General Public License
25 * along with this library. If not, see <http://www.gnu.org/licenses/>.
26 */
27
28#pragma once
29
33
34/**@{*/
35/* Timer/Counter */
36/** @addtogroup timer_block
37 * @{
38 */
39
40#define TIMER0 TIMER0_BASE
41#define TIMER1 TIMER1_BASE
42#define TIMER2 TIMER2_BASE
43
44/**@}*/
45
46/* Tasks */
47
48#define TIMER_TASK_START(T) MMIO32((T) + 0x000)
49#define TIMER_TASK_STOP(T) MMIO32((T) + 0x004)
50#define TIMER_TASK_COUNT(T) MMIO32((T) + 0x008)
51#define TIMER_TASK_CLEAR(T) MMIO32((T) + 0x00C)
52#define TIMER_TASK_SHUTDOWN(T) MMIO32((T) + 0x010)
53#define TIMER_TASK_CAPTURE(T, C) MMIO32((T) + 0x040 + (uint32_t)(0x4 * (C)))
54
55
56
57
58/* Events */
59
60#define TIMER_EVENT_COMPARE(T, C) MMIO32((T) + 0x140 + (uint32_t)(0x4 * (C)))
61
62/* Registers */
63
64#define TIMER_SHORTS(T) _PERIPH_SHORTS(T)
65#define TIMER_INTENSET(T) _PERIPH_INTENSET(T)
66#define TIMER_INTENCLR(T) _PERIPH_INTENCLR(T)
67#define TIMER_MODE(T) MMIO32((T) + 0x504)
68#define TIMER_BITMODE(T) MMIO32((T) + 0x508)
69#define TIMER_PRESCALER(T) MMIO32((T) + 0x510)
70#define TIMER_PRESCALER_MASK (0xf)
71
72#define TIMER_CC(T, C) MMIO32((T) + 0x540 + 0x4 * (C))
73
74
75
76
77/* Register Contents */
78
79/** @addtogroup timer_shorts Timer event -> task shortcuts
80 * @{
81 */
82
83#define TIMER_SHORTS_COMPARE_CLEAR(C) (1 << (C))
84#define TIMER_SHORTS_COMPARE_STOP(C) (1 << (8 + (C)))
85/**@}*/
86
87#define TIMER_INTEN_COMPARE(C) (1 << (16 + (C)))
88
89#define TIMER_MODE_SELECT (1 << 0)
90
94};
95
101};
102
103/**@}*/
104
106
107uint32_t timer_get_ticks(uint32_t timer);
108void timer_set_mode(uint32_t timer, enum timer_mode mode);
109void timer_set_bitmode(uint32_t timer, enum timer_bitmode bitmode);
110void timer_start(uint32_t timer);
111void timer_stop(uint32_t timer);
112void timer_clear(uint32_t timer);
113void timer_set_prescaler(uint32_t timer, uint8_t presc);
114void timer_set_compare(uint32_t timer, uint8_t compare_num, uint32_t compare_val);
115uint32_t timer_get_cc(uint32_t timer, uint8_t compare_num);
116uint32_t timer_get_freq(uint32_t timer);
117
119
120
#define END_DECLS
Definition: common.h:34
#define BEGIN_DECLS
Definition: common.h:33
timer_mode
Definition: common/timer.h:91
timer_bitmode
Definition: common/timer.h:96
@ TIMER_MODE_COUNTER
Definition: common/timer.h:93
@ TIMER_MODE_TIMER
Definition: common/timer.h:92
@ TIMER_BITMODE_32BIT
Definition: common/timer.h:100
@ TIMER_BITMODE_24BIT
Definition: common/timer.h:99
@ TIMER_BITMODE_16BIT
Definition: common/timer.h:97
@ TIMER_BITMODE_08BIT
Definition: common/timer.h:98
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