Move also cpu/ to bertos/ :-).
[bertos.git] / bertos / cpu / arm / io / at91_tc.h
1 /**
2  * \file
3  * <!--
4  * This file is part of BeRTOS.
5  *
6  * Bertos is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19  *
20  * As a special exception, you may use this file as part of a free software
21  * library without restriction.  Specifically, if other files instantiate
22  * templates or use macros or inline functions from this file, or you compile
23  * this file and link it with other files to produce an executable, this
24  * file does not by itself cause the resulting executable to be covered by
25  * the GNU General Public License.  This exception does not however
26  * invalidate any other reasons why the executable file might be covered by
27  * the GNU General Public License.
28  *
29  * Copyright 2007 Develer S.r.l. (http://www.develer.com/)
30  *
31  * -->
32  *
33  * \version $Id$
34  *
35  * \author Daniele Basile <asterix@develer.com>
36  *
37  * AT91SAM7 Conunter timer definition.
38  * This file is based on NUT/OS implementation. See license below.
39  */
40
41 /*
42  * Copyright (C) 2005-2006 by egnite Software GmbH. All rights reserved.
43  *
44  * Redistribution and use in source and binary forms, with or without
45  * modification, are permitted provided that the following conditions
46  * are met:
47  *
48  * 1. Redistributions of source code must retain the above copyright
49  *    notice, this list of conditions and the following disclaimer.
50  * 2. Redistributions in binary form must reproduce the above copyright
51  *    notice, this list of conditions and the following disclaimer in the
52  *    documentation and/or other materials provided with the distribution.
53  * 3. Neither the name of the copyright holders nor the names of
54  *    contributors may be used to endorse or promote products derived
55  *    from this software without specific prior written permission.
56  *
57  * THIS SOFTWARE IS PROVIDED BY EGNITE SOFTWARE GMBH AND CONTRIBUTORS
58  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
59  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
60  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL EGNITE
61  * SOFTWARE GMBH OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
62  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
63  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
64  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
65  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
66  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
67  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
68  * SUCH DAMAGE.
69  *
70  * For additional information see http://www.ethernut.de/
71  */
72
73 #ifndef AT91_TC_H
74 #define AT91_TC_H
75
76
77 /**
78  * Timer Counter Control Register
79  */
80 #define TC_TC0_OFF              0x00000000     ///< Channel 0 control register offset.
81 #define TC_TC1_OFF              0x00000040     ///< Channel 1 control register offset.
82 #define TC_TC2_OFF              0x00000080     ///< Channel 2 control register offset.
83 #define TC0_CCR         (*((reg32_t *)(TC_BASE + TC_TC0_OFF))) ///< Channel 0 control register address.
84 #define TC1_CCR         (*((reg32_t *)(TC_BASE + TC_TC1_OFF))) ///< Channel 1 control register address.
85 #define TC2_CCR         (*((reg32_t *)(TC_BASE + TC_TC2_OFF))) ///< Channel 2 control register address.
86 #define TC_CLKEN                         0      ///< Clock enable command.
87 #define TC_CLKDIS                        1      ///< Clock disable command.
88 #define TC_SWTRG                         2      ///< Software trigger command.
89
90 /**
91  * Timer Counter Channel Mode Register
92  */
93 #define TC_CMR_OFF              0x00000004      ///< Mode register offset.
94 #define TC0_CMR         (*((reg32_t *)(TC_BASE + TC_TC0_OFF + TC_CMR_OFF))) ///< Channel 0 mode register address.
95 #define TC1_CMR         (*((reg32_t *)(TC_BASE + TC_TC1_OFF + TC_CMR_OFF))) ///< Channel 1 mode register address.
96 #define TC2_CMR         (*((reg32_t *)(TC_BASE + TC_TC2_OFF + TC_CMR_OFF))) ///< Channel 2 mode register address.
97
98 #define TC_CLKS_MASK            0x00000007      ///< Clock selection mask.
99 #define TC_CLKS_MCK2            0x00000000      ///< Selects MCK / 2.
100 #define TC_CLKS_MCK8            0x00000001      ///< Selects MCK / 8.
101 #define TC_CLKS_MCK32           0x00000002      ///< Selects MCK / 32.
102 #define TC_CLKS_MCK128          0x00000003      ///< Selects MCK / 128.
103 #define TC_CLKS_MCK1024         0x00000004      ///< Selects MCK / 1024.
104 #define TC_CLKS_XC0             0x00000005      ///< Selects external clock 0.
105 #define TC_CLKS_XC1             0x00000006      ///< Selects external clock 1.
106 #define TC_CLKS_XC2             0x00000007      ///< Selects external clock 2.
107
108 #define TC_CLKI                          3      ///< Increments on falling edge.
109
110 #define TC_BURST_MASK           0x00000030      ///< Burst signal selection mask.
111 #define TC_BURST_NONE           0x00000000      ///< Clock is not gated by an external signal.
112 #define TC_BUSRT_XC0            0x00000010      ///< ANDed with external clock 0.
113 #define TC_BURST_XC1            0x00000020      ///< ANDed with external clock 1.
114 #define TC_BURST_XC2            0x00000030      ///< ANDed with external clock 2.
115
116
117
118 #define TC_WAVE                         15      ///< Selects waveform mode.
119 //To select capture mode you must set TC_WAVE bit to 0.
120 //#define TC_CAPT                         15      ///< Selects capture mode.
121
122 /**
123  * Capture Mode
124  */
125 #define TC_CPCTRG                       14      ///< RC Compare Enable Trigger Enable.
126 #define TC_LDBSTOP                       6      ///< Counter clock stopped on RB loading.
127 #define TC_LDBDIS                        7      ///< Counter clock disabled on RB loading.
128
129 #define TC_ETRGEDG_MASK         0x00000300      ///< External trigger edge selection mask.
130 #define TC_ETRGEDG_RISING_EDGE  0x00000100      ///< Trigger on external rising edge.
131 #define TC_ETRGEDG_FALLING_EDGE 0x00000200      ///< Trigger on external falling edge.
132 #define TC_ETRGEDG_BOTH_EDGE    0x00000300      ///< Trigger on both external edges.
133
134 #define TC_ABETRG_MASK          0x00000400      ///< TIOA or TIOB external trigger selection mask.
135 #define TC_ABETRG_TIOA                  10      ///< TIOA used as an external trigger.
136 //To use external trigger TIOB you must set TC_ABETRG_TIOA bit to 0.
137 //#define TC_ABETRG_TIOB                  10      ///< TIOB used as an external trigger.
138
139
140 #define TC_LDRA_MASK            0x00030000      ///< RA loading selection mask.
141 #define TC_LDRA_RISING_EDGE     0x00010000      ///< Load RA on rising edge of TIOA.
142 #define TC_LDRA_FALLING_EDGE    0x00020000      ///< Load RA on falling edge of TIOA.
143 #define TC_LDRA_BOTH_EDGE       0x00030000      ///< Load RA on any edge of TIOA.
144
145 #define TC_LDRB_MASK            0x000C0000      ///< RB loading selection mask.
146 #define TC_LDRB_RISING_EDGE     0x00040000      ///< Load RB on rising edge of TIOA.
147 #define TC_LDRB_FALLING_EDGE    0x00080000      ///< Load RB on falling edge of TIOA.
148 #define TC_LDRB_BOTH_EDGE       0x000C0000      ///< Load RB on any edge of TIOA.
149
150
151 /**
152  * Waveform Mode
153  */
154 #define TC_CPCSTOP                       6      ///< Counter clock stopped on RC compare.
155 #define TC_CPCDIS                        7      ///< Counter clock disabled on RC compare.
156
157 #define TC_EEVTEDG_MASK         0x00000300      ///< External event edge selection mask.
158 #define TC_EEVTEDG_RISING_EDGE  0x00000100      ///< External event on rising edge..
159 #define TC_EEVTEDG_FALLING_EDGE 0x00000200      ///< External event on falling edge..
160 #define TC_EEVTEDG_BOTH_EDGE    0x00000300      ///< External event on any edge..
161
162 #define TC_EEVT_MASK            0x00000C00      ///< External event selection mask.
163 #define TC_EEVT_TIOB            0x00000000      ///< TIOB selected as external event.
164 #define TC_EEVT_XC0             0x00000400      ///< XC0 selected as external event.
165 #define TC_EEVT_XC1             0x00000800      ///< XC1 selected as external event.
166 #define TC_EEVT_XC2             0x00000C00      ///< XC2 selected as external event.
167
168 #define TC_ENETRG                       12      ///< External event trigger enable.
169
170 #define TC_WAVSEL_MASK          0x00006000      ///< Waveform selection mask.
171 #define TC_WAVSEL_UP_RC         0x00000000      ///< UP mode whitout automatic trigger on RC compare.
172 #define TC_WAVSEL_UP_RC_TRG     0x00004000      ///< UP mode whit automatic trigger on RC compare.
173 #define TC_WAVSEL_UPDOWN_RC     0x00002000      ///< UPDOWN mode whitout automatic trigger on RC compare.
174 #define TC_WAVSEL_UPDOWN_RC_TRG 0x00003000      ///< UPDOWN mode whit automatic trigger on RC compare.
175
176
177 #define TC_ACPA_MASK            0x00030000      ///< Masks RA compare effect on TIOA.
178 #define TC_ACPA_SET_OUTPUT      0x00010000      ///< RA compare sets TIOA.
179 #define TC_ACPA_CLEAR_OUTPUT    0x00020000      ///< RA compare clears TIOA.
180 #define TC_ACPA_TOGGLE_OUTPUT   0x00030000      ///< RA compare toggles TIOA.
181
182 #define TC_ACPC_MASK            0x000C0000      ///< Masks RC compare effect on TIOA.
183 #define TC_ACPC_SET_OUTPUT      0x00040000      ///< RC compare sets TIOA.
184 #define TC_ACPC_CLEAR_OUTPUT    0x00080000      ///< RC compare clears TIOA.
185 #define TC_ACPC_TOGGLE_OUTPUT   0x000C0000      ///< RC compare toggles TIOA.
186
187 #define TC_AEEVT_MASK           0x00300000      ///< Masks external event effect on TIOA.
188 #define TC_AEEVT_SET_OUTPUT     0x00100000      ///< External event sets TIOA.
189 #define TC_AEEVT_CLEAR_OUTPUT   0x00200000      ///< External event clears TIOA.
190 #define TC_AEEVT_TOGGLE_OUTPUT  0x00300000      ///< External event toggles TIOA.
191
192 #define TC_ASWTRG_MASK          0x00C00000      ///< Masks software trigger effect on TIOA.
193 #define TC_ASWTRG_SET_OUTPUT    0x00400000      ///< Software trigger sets TIOA.
194 #define TC_ASWTRG_CLEAR_OUTPUT  0x00800000      ///< Software trigger clears TIOA.
195 #define TC_ASWTRG_TOGGLE_OUTPUT 0x00C00000      ///< Software trigger toggles TIOA.
196
197 #define TC_BCPB_MASK            0x03000000      ///< Masks RB compare effect on TIOB.
198 #define TC_BCPB_SET_OUTPUT      0x01000000      ///< RB compare sets TIOB.
199 #define TC_BCPB_CLEAR_OUTPUT    0x02000000      ///< RB compare clears TIOB.
200 #define TC_BCPB_TOGGLE_OUTPUT   0x03000000      ///< RB compare toggles TIOB.
201
202 #define TC_BCPC_MASK            0x0C000000      ///< Masks RC compare effect on TIOB.
203 #define TC_BCPC_SET_OUTPUT      0x04000000      ///< RC compare sets TIOB.
204 #define TC_BCPC_CLEAR_OUTPUT    0x08000000      ///< RC compare clears TIOB.
205 #define TC_BCPC_TOGGLE_OUTPUT   0x0C000000      ///< RC compare toggles TIOB.
206
207 #define TC_BEEVT_MASK           0x30000000      ///< Masks external event effect on TIOB.
208 #define TC_BEEVT_SET_OUTPUT     0x10000000      ///< External event sets TIOB.
209 #define TC_BEEVT_CLEAR_OUTPUT   0x20000000      ///< External event clears TIOB.
210 #define TC_BEEVT_TOGGLE_OUTPUT  0x30000000      ///< External event toggles TIOB.
211
212 #define TC_BSWTRG_MASK          0xC0000000      ///< Masks software trigger effect on TIOB.
213 #define TC_BSWTRG_SET_OUTPUT    0x40000000      ///< Software trigger sets TIOB.
214 #define TC_BSWTRG_CLEAR_OUTPUT  0x80000000      ///< Software trigger clears TIOB.
215 #define TC_BSWTRG_TOGGLE_OUTPUT 0xC0000000      ///< Software trigger toggles TIOB.
216
217 /**
218  * Counter Value Register
219  */
220 #define TC_CV_OFF               0x00000010      ///< Counter register value offset.
221 #define TC0_CV          (*((reg32_t *)(TC_BASE + TC_TC0_OFF + TC_CV_OFF))) ///< Counter 0 value.
222 #define TC1_CV          (*((reg32_t *)(TC_BASE + TC_TC1_OFF + TC_CV_OFF))) ///< Counter 1 value.
223 #define TC2_CV          (*((reg32_t *)(TC_BASE + TC_TC2_OFF + TC_CV_OFF))) ///< Counter 2 value.
224
225 /**
226  * Timer Counter Register A
227  */
228 #define TC_RA_OFF               0x00000014      ///< Register A offset.
229 #define TC0_RA          (*((reg32_t *)(TC_BASE + TC_TC0_OFF + TC_RA_OFF))) ///< Channel 0 register A.
230 #define TC1_RA          (*((reg32_t *)(TC_BASE + TC_TC1_OFF + TC_RA_OFF))) ///< Channel 1 register A.
231 #define TC2_RA          (*((reg32_t *)(TC_BASE + TC_TC2_OFF + TC_RA_OFF))) ///< Channel 2 register A.
232
233
234 /**
235  * Timer Counter Register B
236  */
237 #define TC_RB_OFF               0x00000018      ///< Register B offset.
238 #define TC0_RB           (*((reg32_t *)(TC_BASE + TC_TC0_OFF + TC_RB_OFF))) ///< Channel 0 register B.
239 #define TC1_RB           (*((reg32_t *)(TC_BASE + TC_TC1_OFF + TC_RB_OFF))) ///< Channel 1 register B.
240 #define TC2_RB           (*((reg32_t *)(TC_BASE + TC_TC2_OFF + TC_RB_OFF))) ///< Channel 2 register B.
241
242
243 /**
244  * Timer Counter Register C
245  */
246 #define TC_RC_OFF               0x0000001C      ///< Register C offset.
247 #define TC0_RC          (*((reg32_t *)(TC_BASE + TC_TC0_OFF + TC_RC_OFF))) ///< Channel 0 register C.
248 #define TC1_RC          (*((reg32_t *)(TC_BASE + TC_TC1_OFF + TC_RC_OFF))) ///< Channel 1 register C.
249 #define TC2_RC          (*((reg32_t *)(TC_BASE + TC_TC2_OFF + TC_RC_OFF))) ///< Channel 2 register C.
250
251
252
253 /**
254  * Timer Counter Status and Interrupt Registers
255  */
256 #define TC_SR_OFF               0x00000020      ///< Status Register offset.
257 #define TC0_SR          (*((reg32_t *)(TC_BASE + TC_TC0_OFF + TC_SR_OFF))) ///< Status register address.
258 #define TC1_SR          (*((reg32_t *)(TC_BASE + TC_TC1_OFF + TC_SR_OFF))) ///< Status register address.
259 #define TC2_SR          (*((reg32_t *)(TC_BASE + TC_TC2_OFF + TC_SR_OFF))) ///< Status register address.
260
261 #define TC_IER_OFF              0x00000024      ///< Interrupt Enable Register offset.
262 #define TC0_IER         (*((reg32_t *)(TC_BASE + TC_TC0_OFF + TC_IER_OFF))) ///< Channel 0 interrupt enable register address.
263 #define TC1_IER         (*((reg32_t *)(TC_BASE + TC_TC1_OFF + TC_IER_OFF))) ///< Channel 1 interrupt enable register address.
264 #define TC2_IER         (*((reg32_t *)(TC_BASE + TC_TC2_OFF + TC_IER_OFF))) ///< Channel 2 interrupt enable register address.
265
266 #define TC_IDR_OFF              0x00000028      ///< Interrupt Disable Register offset.
267 #define TC0_IDR        (*((reg32_t *)(TC_BASE + TC_TC0_OFF + TC_IDR_OFF))) ///< Channel 0 interrupt disable register address.
268 #define TC1_IDR        (*((reg32_t *)(TC_BASE + TC_TC1_OFF + TC_IDR_OFF))) ///< Channel 1 interrupt disable register address.
269 #define TC2_IDR        (*((reg32_t *)(TC_BASE + TC_TC2_OFF + TC_IDR_OFF))) ///< Channel 2 interrupt disable register address.
270
271 #define TC_IMR_OFF              0x0000002C      ///< Interrupt Mask Register offset.
272 #define TC0_IMR        (*((reg32_t *)(TC_BASE + TC_TC0_OFF + TC_IMR_OFF))) ///< Channel 0 interrupt mask register address.
273 #define TC1_IMR        (*((reg32_t *)(TC_BASE + TC_TC1_OFF + TC_IMR_OFF))) ///< Channel 1 interrupt mask register address.
274 #define TC2_IMR        (*((reg32_t *)(TC_BASE + TC_TC2_OFF + TC_IMR_OFF))) ///< Channel 2 interrupt mask register address.
275
276 #define TC_COVFS                         0      ///< Counter overflow flag.
277 #define TC_LOVRS                         1      ///< Load overrun flag.
278 #define TC_CPAS                          2      ///< RA compare flag.
279 #define TC_CPBS                          3      ///< RB compare flag.
280 #define TC_CPCS                          4      ///< RC compare flag.
281 #define TC_LDRAS                         5      ///< RA loading flag.
282 #define TC_LDRBS                         6      ///< RB loading flag.
283 #define TC_ETRGS                         7      ///< External trigger flag.
284 #define TC_CLKSTA                       16      ///< Clock enable flag.
285 #define TC_MTIOA                        17      ///< TIOA flag.
286 #define TC_MTIOB                        18      ///< TIOB flag.
287
288
289 /**
290  * Timer Counter Block Control Register
291  */
292 #define TC_BCR_OFF              0x000000C0      ///< Block control register offset.
293 #define TC_BCR         (*((reg32_t *)(TC_BASE + TC_BCR_OFF))) ///< Block control register address.
294 #define TC_SYNC                          0      ///< Synchronisation trigger
295
296
297 /**
298  * Timer Counter Block Mode Register
299  */
300 #define TC_BMR_OFF              0x000000C4      ///< Block mode register offset.
301 #define TC_BMR         (*((reg32_t *)(TC_BASE + TC_BMR_OFF))) ///< Block mode register address.
302 #define TC_TC0XC0S              0x00000003      ///< External clock signal 0 selection mask.
303 #define TC_TCLK0XC0             0x00000000      ///< Selects TCLK0.
304 #define TC_NONEXC0              0x00000001      ///< None selected.
305 #define TC_TIOA1XC0             0x00000002      ///< Selects TIOA1.
306 #define TC_TIOA2XC0             0x00000003      ///< Selects TIOA2.
307
308 #define TC_TC1XC1S              0x0000000C      ///< External clock signal 1 selection mask.
309 #define TC_TCLK1XC1             0x00000000      ///< Selects TCLK1.
310 #define TC_NONEXC1              0x00000004      ///< None selected.
311 #define TC_TIOA0XC1             0x00000008      ///< Selects TIOA0.
312 #define TC_TIOA2XC1             0x0000000C      ///< Selects TIOA2.
313
314 #define TC_TC2XC2S              0x00000030      ///< External clock signal 2 selection mask.
315 #define TC_TCLK2XC2             0x00000000      ///< Selects TCLK2.
316 #define TC_NONEXC2              0x00000010      ///< None selected.
317 #define TC_TIOA0XC2             0x00000020      ///< Selects TIOA0.
318 #define TC_TIOA1XC2             0x00000030      ///< Selects TIOA1.
319
320
321 #endif /* AT91_TC_H */