summaryrefslogtreecommitdiffstats
path: root/libopencm3/include/libopencm3/lpc43xx/timer.h
blob: 2c691e7b70731ec42032b92e560029b44d914348 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
/** @defgroup timer_defines Timer

@brief <b>Defined Constants and Types for the LPC43xx timer</b>

@ingroup LPC43xx_defines

@version 1.0.0

@author @htmlonly &copy; @endhtmlonly 2012 Michael Ossmann <mike@ossmann.com>

@date 10 March 2013

LGPL License Terms @ref lgpl_license
 */
/*
 * This file is part of the libopencm3 project.
 *
 * Copyright (C) 2012 Michael Ossmann <mike@ossmann.com>
 *
 * This library is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this library.  If not, see <http://www.gnu.org/licenses/>.
 */

#ifndef LPC43XX_TIMER_H
#define LPC43XX_TIMER_H

/**@{*/

#include <libopencm3/cm3/common.h>
#include <libopencm3/lpc43xx/memorymap.h>

/* --- Convenience macros -------------------------------------------------- */

/* Timer base addresses */
#define TIMER0                          TIMER0_BASE
#define TIMER1                          TIMER1_BASE
#define TIMER2                          TIMER2_BASE
#define TIMER3                          TIMER3_BASE


/* --- Timer registers ----------------------------------------------------- */

/* Interrupt Register */
#define TIMER_IR(timer)                 MMIO32(timer + 0x000)
#define TIMER0_IR                       TIMER_IR(TIMER0)
#define TIMER1_IR                       TIMER_IR(TIMER1)
#define TIMER2_IR                       TIMER_IR(TIMER2)
#define TIMER3_IR                       TIMER_IR(TIMER3)

/* Timer Control Register */
#define TIMER_TCR(timer)                MMIO32(timer + 0x004)
#define TIMER0_TCR                      TIMER_TCR(TIMER0)
#define TIMER1_TCR                      TIMER_TCR(TIMER1)
#define TIMER2_TCR                      TIMER_TCR(TIMER2)
#define TIMER3_TCR                      TIMER_TCR(TIMER3)

/* Timer Counter */
#define TIMER_TC(timer)                 MMIO32(timer + 0x008)
#define TIMER0_TC                       TIMER_TC(TIMER0)
#define TIMER1_TC                       TIMER_TC(TIMER1)
#define TIMER2_TC                       TIMER_TC(TIMER2)
#define TIMER3_TC                       TIMER_TC(TIMER3)

/* Prescale Register */
#define TIMER_PR(timer)                 MMIO32(timer + 0x00C)
#define TIMER0_PR                       TIMER_PR(TIMER0)
#define TIMER1_PR                       TIMER_PR(TIMER1)
#define TIMER2_PR                       TIMER_PR(TIMER2)
#define TIMER3_PR                       TIMER_PR(TIMER3)

/* Prescale Counter */
#define TIMER_PC(timer)                 MMIO32(timer + 0x010)
#define TIMER0_PC                       TIMER_PC(TIMER0)
#define TIMER1_PC                       TIMER_PC(TIMER1)
#define TIMER2_PC                       TIMER_PC(TIMER2)
#define TIMER3_PC                       TIMER_PC(TIMER3)

/* Match Control Register */
#define TIMER_MCR(timer)                MMIO32(timer + 0x014)
#define TIMER0_MCR                      TIMER_MCR(TIMER0)
#define TIMER1_MCR                      TIMER_MCR(TIMER1)
#define TIMER2_MCR                      TIMER_MCR(TIMER2)
#define TIMER3_MCR                      TIMER_MCR(TIMER3)

/* Match Register 0 */
#define TIMER_MR0(timer)                MMIO32(timer + 0x018)
#define TIMER0_MR0                      TIMER_MR0(TIMER0)
#define TIMER1_MR0                      TIMER_MR0(TIMER1)
#define TIMER2_MR0                      TIMER_MR0(TIMER2)
#define TIMER3_MR0                      TIMER_MR0(TIMER3)

/* Match Register 1 */
#define TIMER_MR1(timer)                MMIO32(timer + 0x01C)
#define TIMER0_MR1                      TIMER_MR1(TIMER0)
#define TIMER1_MR1                      TIMER_MR1(TIMER1)
#define TIMER2_MR1                      TIMER_MR1(TIMER2)
#define TIMER3_MR1                      TIMER_MR1(TIMER3)

/* Match Register 2 */
#define TIMER_MR2(timer)                MMIO32(timer + 0x020)
#define TIMER0_MR2                      TIMER_MR2(TIMER0)
#define TIMER1_MR2                      TIMER_MR2(TIMER1)
#define TIMER2_MR2                      TIMER_MR2(TIMER2)
#define TIMER3_MR2                      TIMER_MR2(TIMER3)

/* Match Register 3 */
#define TIMER_MR3(timer)                MMIO32(timer + 0x024)
#define TIMER0_MR3                      TIMER_MR3(TIMER0)
#define TIMER1_MR3                      TIMER_MR3(TIMER1)
#define TIMER2_MR3                      TIMER_MR3(TIMER2)
#define TIMER3_MR3                      TIMER_MR3(TIMER3)

/* Capture Control Register */
#define TIMER_CCR(timer)                MMIO32(timer + 0x028)
#define TIMER0_CCR                      TIMER_CCR(TIMER0)
#define TIMER1_CCR                      TIMER_CCR(TIMER1)
#define TIMER2_CCR                      TIMER_CCR(TIMER2)
#define TIMER3_CCR                      TIMER_CCR(TIMER3)

/* Capture Register 0 */
#define TIMER_CR0(timer)                MMIO32(timer + 0x02C)
#define TIMER0_CR0                      TIMER_CR0(TIMER0)
#define TIMER1_CR0                      TIMER_CR0(TIMER1)
#define TIMER2_CR0                      TIMER_CR0(TIMER2)
#define TIMER3_CR0                      TIMER_CR0(TIMER3)

/* Capture Register 1 */
#define TIMER_CR1(timer)                MMIO32(timer + 0x030)
#define TIMER0_CR1                      TIMER_CR1(TIMER0)
#define TIMER1_CR1                      TIMER_CR1(TIMER1)
#define TIMER2_CR1                      TIMER_CR1(TIMER2)
#define TIMER3_CR1                      TIMER_CR1(TIMER3)

/* Capture Register 2 */
#define TIMER_CR2(timer)                MMIO32(timer + 0x034)
#define TIMER0_CR2                      TIMER_CR2(TIMER0)
#define TIMER1_CR2                      TIMER_CR2(TIMER1)
#define TIMER2_CR2                      TIMER_CR2(TIMER2)
#define TIMER3_CR2                      TIMER_CR2(TIMER3)

/* Capture Register 3 */
#define TIMER_CR3(timer)                MMIO32(timer + 0x038)
#define TIMER0_CR3                      TIMER_CR3(TIMER0)
#define TIMER1_CR3                      TIMER_CR3(TIMER1)
#define TIMER2_CR3                      TIMER_CR3(TIMER2)
#define TIMER3_CR3                      TIMER_CR3(TIMER3)

/* External Match Register */
#define TIMER_EMR(timer)                MMIO32(timer + 0x03C)
#define TIMER0_EMR                      TIMER_EMR(TIMER0)
#define TIMER1_EMR                      TIMER_EMR(TIMER1)
#define TIMER2_EMR                      TIMER_EMR(TIMER2)
#define TIMER3_EMR                      TIMER_EMR(TIMER3)

/* Count Control Register */
#define TIMER_CTCR(timer)               MMIO32(timer + 0x070)
#define TIMER0_CTCR                     TIMER_CTCR(TIMER0)
#define TIMER1_CTCR                     TIMER_CTCR(TIMER1)
#define TIMER2_CTCR                     TIMER_CTCR(TIMER2)
#define TIMER3_CTCR                     TIMER_CTCR(TIMER3)

/* --- TIMERx_IR values ----------------------------------------------------- */

#define TIMER_IR_MR0INT                 (1 << 0)
#define TIMER_IR_MR1INT                 (1 << 1)
#define TIMER_IR_MR2INT                 (1 << 2)
#define TIMER_IR_MR3INT                 (1 << 3)
#define TIMER_IR_CR0INT                 (1 << 4)
#define TIMER_IR_CR1INT                 (1 << 5)
#define TIMER_IR_CR2INT                 (1 << 6)
#define TIMER_IR_CR3INT                 (1 << 7)

/* --- TIMERx_TCR values --------------------------------------------------- */

#define TIMER_TCR_CEN                   (1 << 0)
#define TIMER_TCR_CRST                  (1 << 1)

/* --- TIMERx_MCR values --------------------------------------------------- */

#define TIMER_MCR_MR0I                  (1 << 0)
#define TIMER_MCR_MR0R                  (1 << 1)
#define TIMER_MCR_MR0S                  (1 << 2)
#define TIMER_MCR_MR1I                  (1 << 3)
#define TIMER_MCR_MR1R                  (1 << 4)
#define TIMER_MCR_MR1S                  (1 << 5)
#define TIMER_MCR_MR2I                  (1 << 6)
#define TIMER_MCR_MR2R                  (1 << 7)
#define TIMER_MCR_MR2S                  (1 << 8)
#define TIMER_MCR_MR3I                  (1 << 9)
#define TIMER_MCR_MR3R                  (1 << 10)
#define TIMER_MCR_MR3S                  (1 << 11)

/* --- TIMERx_MCR values --------------------------------------------------- */

#define TIMER_CCR_CAP0RE                (1 << 0)
#define TIMER_CCR_CAP0FE                (1 << 1)
#define TIMER_CCR_CAP0I                 (1 << 2)
#define TIMER_CCR_CAP1RE                (1 << 3)
#define TIMER_CCR_CAP1FE                (1 << 4)
#define TIMER_CCR_CAP1I                 (1 << 5)
#define TIMER_CCR_CAP2RE                (1 << 6)
#define TIMER_CCR_CAP2FE                (1 << 7)
#define TIMER_CCR_CAP2I                 (1 << 8)
#define TIMER_CCR_CAP3RE                (1 << 9)
#define TIMER_CCR_CAP3FE                (1 << 10)
#define TIMER_CCR_CAP3I                 (1 << 11)

/* --- TIMERx_EMR values --------------------------------------------------- */

#define TIMER_EMR_EM0                   (1 << 0)
#define TIMER_EMR_EM1                   (1 << 1)
#define TIMER_EMR_EM2                   (1 << 2)
#define TIMER_EMR_EM3                   (1 << 3)
#define TIMER_EMR_EMC0_SHIFT            4
#define TIMER_EMR_EMC0_MASK             (0x3 << TIMER_EMR_EMC0_SHIFT)
#define TIMER_EMR_EMC1_SHIFT            6
#define TIMER_EMR_EMC1_MASK             (0x3 << TIMER_EMR_EMC1_SHIFT)
#define TIMER_EMR_EMC2_SHIFT            8
#define TIMER_EMR_EMC2_MASK             (0x3 << TIMER_EMR_EMC2_SHIFT)
#define TIMER_EMR_EMC3_SHIFT            10
#define TIMER_EMR_EMC3_MASK             (0x3 << TIMER_EMR_EMC3_SHIFT)

#define TIMER_EMR_EMC_NOTHING           0x0
#define TIMER_EMR_EMC_CLEAR             0x1
#define TIMER_EMR_EMC_SET               0x2
#define TIMER_EMR_EMC_TOGGLE            0x3

/* --- TIMERx_CTCR values -------------------------------------------------- */

#define TIMER_CTCR_MODE_TIMER           (0x0 << 0)
#define TIMER_CTCR_MODE_COUNTER_RISING  (0x1 << 0)
#define TIMER_CTCR_MODE_COUNTER_FALLING (0x2 << 0)
#define TIMER_CTCR_MODE_COUNTER_BOTH    (0x3 << 0)
#define TIMER_CTCR_MODE_MASK            (0x3 << 0)

#define TIMER_CTCR_CINSEL_CAPN_0        (0x0 << 2)
#define TIMER_CTCR_CINSEL_CAPN_1        (0x1 << 2)
#define TIMER_CTCR_CINSEL_CAPN_2        (0x2 << 2)
#define TIMER_CTCR_CINSEL_CAPN_3        (0x3 << 2)
#define TIMER_CTCR_CINSEL_MASK          (0x3 << 2)

/* --- TIMER function prototypes ------------------------------------------- */

BEGIN_DECLS

void timer_reset(uint32_t timer_peripheral);
void timer_enable_counter(uint32_t timer_peripheral);
void timer_disable_counter(uint32_t timer_peripheral);
uint32_t timer_get_counter(uint32_t timer_peripheral);
void timer_set_counter(uint32_t timer_peripheral, uint32_t count);
uint32_t timer_get_prescaler(uint32_t timer_peripheral);
void timer_set_prescaler(uint32_t timer_peripheral, uint32_t prescaler);
void timer_set_mode(uint32_t timer_peripheral, uint32_t mode);
void timer_set_count_input(uint32_t timer_peripheral, uint32_t input);

END_DECLS

/**@}*/

#endif