sanguino/cores/arduino/wiring.c

Fri, 17 Nov 2017 10:13:31 +0100

author
mdd
date
Fri, 17 Nov 2017 10:13:31 +0100
changeset 3
770b218a4931
parent 2
b373b0288715
permissions
-rw-r--r--

proper configuration, homing and planner optimization

2
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
1 /*
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
2 wiring.c - Partial implementation of the Wiring API for the ATmega8.
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
3 Part of Arduino - http://www.arduino.cc/
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
4
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
5 Copyright (c) 2005-2006 David A. Mellis
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
6
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
7 This library is free software; you can redistribute it and/or
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
8 modify it under the terms of the GNU Lesser General Public
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
9 License as published by the Free Software Foundation; either
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
10 version 2.1 of the License, or (at your option) any later version.
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
11
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
12 This library is distributed in the hope that it will be useful,
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
15 Lesser General Public License for more details.
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
16
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
17 You should have received a copy of the GNU Lesser General
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
18 Public License along with this library; if not, write to the
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
19 Free Software Foundation, Inc., 59 Temple Place, Suite 330,
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
20 Boston, MA 02111-1307 USA
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
21
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
22 $Id$
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
23 */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
24
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
25 #include "wiring_private.h"
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
26
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
27 // the prescaler is set so that timer0 ticks every 64 clock cycles, and the
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
28 // the overflow handler is called every 256 ticks.
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
29 #define MICROSECONDS_PER_TIMER0_OVERFLOW (clockCyclesToMicroseconds(64 * 256))
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
30
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
31 // the whole number of milliseconds per timer0 overflow
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
32 #define MILLIS_INC (MICROSECONDS_PER_TIMER0_OVERFLOW / 1000)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
33
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
34 // the fractional number of milliseconds per timer0 overflow. we shift right
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
35 // by three to fit these numbers into a byte. (for the clock speeds we care
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
36 // about - 8 and 16 MHz - this doesn't lose precision.)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
37 #define FRACT_INC ((MICROSECONDS_PER_TIMER0_OVERFLOW % 1000) >> 3)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
38 #define FRACT_MAX (1000 >> 3)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
39
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
40 volatile unsigned long timer0_overflow_count = 0;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
41 volatile unsigned long timer0_millis = 0;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
42 static unsigned char timer0_fract = 0;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
43
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
44 #if defined(__AVR_ATtiny24__) || defined(__AVR_ATtiny44__) || defined(__AVR_ATtiny84__)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
45 SIGNAL(TIM0_OVF_vect)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
46 #else
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
47 SIGNAL(TIMER0_OVF_vect)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
48 #endif
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
49 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
50 // copy these to local variables so they can be stored in registers
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
51 // (volatile variables must be read from memory on every access)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
52 unsigned long m = timer0_millis;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
53 unsigned char f = timer0_fract;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
54
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
55 m += MILLIS_INC;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
56 f += FRACT_INC;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
57 if (f >= FRACT_MAX) {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
58 f -= FRACT_MAX;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
59 m += 1;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
60 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
61
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
62 timer0_fract = f;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
63 timer0_millis = m;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
64 timer0_overflow_count++;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
65 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
66
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
67 unsigned long millis()
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
68 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
69 unsigned long m;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
70 uint8_t oldSREG = SREG;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
71
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
72 // disable interrupts while we read timer0_millis or we might get an
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
73 // inconsistent value (e.g. in the middle of a write to timer0_millis)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
74 cli();
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
75 m = timer0_millis;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
76 SREG = oldSREG;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
77
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
78 return m;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
79 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
80
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
81 unsigned long micros() {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
82 unsigned long m;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
83 uint8_t oldSREG = SREG, t;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
84
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
85 cli();
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
86 m = timer0_overflow_count;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
87 #if defined(TCNT0)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
88 t = TCNT0;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
89 #elif defined(TCNT0L)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
90 t = TCNT0L;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
91 #else
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
92 #error TIMER 0 not defined
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
93 #endif
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
94
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
95
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
96 #ifdef TIFR0
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
97 if ((TIFR0 & _BV(TOV0)) && (t < 255))
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
98 m++;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
99 #else
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
100 if ((TIFR & _BV(TOV0)) && (t < 255))
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
101 m++;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
102 #endif
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
103
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
104 SREG = oldSREG;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
105
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
106 return ((m << 8) + t) * (64 / clockCyclesPerMicrosecond());
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
107 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
108
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
109 void delay(unsigned long ms)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
110 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
111 uint16_t start = (uint16_t)micros();
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
112
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
113 while (ms > 0) {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
114 if (((uint16_t)micros() - start) >= 1000) {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
115 ms--;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
116 start += 1000;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
117 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
118 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
119 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
120
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
121 /* Delay for the given number of microseconds. Assumes a 8 or 16 MHz clock. */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
122 void delayMicroseconds(unsigned int us)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
123 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
124 // calling avrlib's delay_us() function with low values (e.g. 1 or
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
125 // 2 microseconds) gives delays longer than desired.
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
126 //delay_us(us);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
127 #if F_CPU >= 20000000L
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
128 // for the 20 MHz clock on rare Arduino boards
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
129
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
130 // for a one-microsecond delay, simply wait 2 cycle and return. The overhead
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
131 // of the function call yields a delay of exactly a one microsecond.
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
132 __asm__ __volatile__ (
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
133 "nop" "\n\t"
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
134 "nop"); //just waiting 2 cycle
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
135 if (--us == 0)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
136 return;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
137
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
138 // the following loop takes a 1/5 of a microsecond (4 cycles)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
139 // per iteration, so execute it five times for each microsecond of
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
140 // delay requested.
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
141 us = (us<<2) + us; // x5 us
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
142
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
143 // account for the time taken in the preceeding commands.
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
144 us -= 2;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
145
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
146 #elif F_CPU >= 16000000L
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
147 // for the 16 MHz clock on most Arduino boards
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
148
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
149 // for a one-microsecond delay, simply return. the overhead
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
150 // of the function call yields a delay of approximately 1 1/8 us.
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
151 if (--us == 0)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
152 return;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
153
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
154 // the following loop takes a quarter of a microsecond (4 cycles)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
155 // per iteration, so execute it four times for each microsecond of
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
156 // delay requested.
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
157 us <<= 2;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
158
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
159 // account for the time taken in the preceeding commands.
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
160 us -= 2;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
161 #else
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
162 // for the 8 MHz internal clock on the ATmega168
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
163
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
164 // for a one- or two-microsecond delay, simply return. the overhead of
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
165 // the function calls takes more than two microseconds. can't just
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
166 // subtract two, since us is unsigned; we'd overflow.
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
167 if (--us == 0)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
168 return;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
169 if (--us == 0)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
170 return;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
171
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
172 // the following loop takes half of a microsecond (4 cycles)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
173 // per iteration, so execute it twice for each microsecond of
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
174 // delay requested.
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
175 us <<= 1;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
176
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
177 // partially compensate for the time taken by the preceeding commands.
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
178 // we can't subtract any more than this or we'd overflow w/ small delays.
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
179 us--;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
180 #endif
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
181
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
182 // busy wait
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
183 __asm__ __volatile__ (
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
184 "1: sbiw %0,1" "\n\t" // 2 cycles
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
185 "brne 1b" : "=w" (us) : "0" (us) // 2 cycles
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
186 );
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
187 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
188
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
189 void init()
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
190 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
191 // this needs to be called before setup() or some functions won't
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
192 // work there
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
193 sei();
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
194
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
195 // on the ATmega168, timer 0 is also used for fast hardware pwm
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
196 // (using phase-correct PWM would mean that timer 0 overflowed half as often
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
197 // resulting in different millis() behavior on the ATmega8 and ATmega168)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
198 #if defined(TCCR0A) && defined(WGM01)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
199 sbi(TCCR0A, WGM01);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
200 sbi(TCCR0A, WGM00);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
201 #endif
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
202
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
203 // set timer 0 prescale factor to 64
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
204 #if defined(__AVR_ATmega128__)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
205 // CPU specific: different values for the ATmega128
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
206 sbi(TCCR0, CS02);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
207 #elif defined(TCCR0) && defined(CS01) && defined(CS00)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
208 // this combination is for the standard atmega8
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
209 sbi(TCCR0, CS01);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
210 sbi(TCCR0, CS00);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
211 #elif defined(TCCR0B) && defined(CS01) && defined(CS00)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
212 // this combination is for the standard 168/328/1280/2560
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
213 sbi(TCCR0B, CS01);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
214 sbi(TCCR0B, CS00);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
215 #elif defined(TCCR0A) && defined(CS01) && defined(CS00)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
216 // this combination is for the __AVR_ATmega645__ series
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
217 sbi(TCCR0A, CS01);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
218 sbi(TCCR0A, CS00);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
219 #else
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
220 #error Timer 0 prescale factor 64 not set correctly
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
221 #endif
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
222
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
223 // enable timer 0 overflow interrupt
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
224 #if defined(TIMSK) && defined(TOIE0)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
225 sbi(TIMSK, TOIE0);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
226 #elif defined(TIMSK0) && defined(TOIE0)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
227 sbi(TIMSK0, TOIE0);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
228 #else
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
229 #error Timer 0 overflow interrupt not set correctly
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
230 #endif
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
231
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
232 // timers 1 and 2 are used for phase-correct hardware pwm
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
233 // this is better for motors as it ensures an even waveform
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
234 // note, however, that fast pwm mode can achieve a frequency of up
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
235 // 8 MHz (with a 16 MHz clock) at 50% duty cycle
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
236
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
237 #if defined(TCCR1B) && defined(CS11) && defined(CS10)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
238 TCCR1B = 0;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
239
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
240 // set timer 1 prescale factor to 64
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
241 sbi(TCCR1B, CS11);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
242 #if F_CPU >= 8000000L
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
243 sbi(TCCR1B, CS10);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
244 #endif
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
245 #elif defined(TCCR1) && defined(CS11) && defined(CS10)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
246 sbi(TCCR1, CS11);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
247 #if F_CPU >= 8000000L
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
248 sbi(TCCR1, CS10);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
249 #endif
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
250 #endif
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
251 // put timer 1 in 8-bit phase correct pwm mode
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
252 #if defined(TCCR1A) && defined(WGM10)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
253 sbi(TCCR1A, WGM10);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
254 #elif defined(TCCR1)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
255 #warning this needs to be finished
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
256 #endif
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
257
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
258 // set timer 2 prescale factor to 64
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
259 #if defined(TCCR2) && defined(CS22)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
260 sbi(TCCR2, CS22);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
261 #elif defined(TCCR2B) && defined(CS22)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
262 sbi(TCCR2B, CS22);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
263 #else
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
264 #warning Timer 2 not finished (may not be present on this CPU)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
265 #endif
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
266
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
267 // configure timer 2 for phase correct pwm (8-bit)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
268 #if defined(TCCR2) && defined(WGM20)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
269 sbi(TCCR2, WGM20);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
270 #elif defined(TCCR2A) && defined(WGM20)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
271 sbi(TCCR2A, WGM20);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
272 #else
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
273 #warning Timer 2 not finished (may not be present on this CPU)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
274 #endif
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
275
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
276 #if defined(TCCR3B) && defined(CS31) && defined(WGM30)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
277 sbi(TCCR3B, CS31); // set timer 3 prescale factor to 64
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
278 sbi(TCCR3B, CS30);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
279 sbi(TCCR3A, WGM30); // put timer 3 in 8-bit phase correct pwm mode
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
280 #endif
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
281
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
282 #if defined(TCCR4A) && defined(TCCR4B) && defined(TCCR4D) /* beginning of timer4 block for 32U4 and similar */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
283 sbi(TCCR4B, CS42); // set timer4 prescale factor to 64
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
284 sbi(TCCR4B, CS41);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
285 sbi(TCCR4B, CS40);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
286 sbi(TCCR4D, WGM40); // put timer 4 in phase- and frequency-correct PWM mode
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
287 sbi(TCCR4A, PWM4A); // enable PWM mode for comparator OCR4A
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
288 sbi(TCCR4C, PWM4D); // enable PWM mode for comparator OCR4D
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
289 #else /* beginning of timer4 block for ATMEGA1280 and ATMEGA2560 */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
290 #if defined(TCCR4B) && defined(CS41) && defined(WGM40)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
291 sbi(TCCR4B, CS41); // set timer 4 prescale factor to 64
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
292 sbi(TCCR4B, CS40);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
293 sbi(TCCR4A, WGM40); // put timer 4 in 8-bit phase correct pwm mode
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
294 #endif
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
295 #endif /* end timer4 block for ATMEGA1280/2560 and similar */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
296
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
297 #if defined(TCCR5B) && defined(CS51) && defined(WGM50)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
298 sbi(TCCR5B, CS51); // set timer 5 prescale factor to 64
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
299 sbi(TCCR5B, CS50);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
300 sbi(TCCR5A, WGM50); // put timer 5 in 8-bit phase correct pwm mode
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
301 #endif
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
302
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
303 #if defined(ADCSRA)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
304 // set a2d prescale factor to 128
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
305 // 16 MHz / 128 = 125 KHz, inside the desired 50-200 KHz range.
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
306 // XXX: this will not work properly for other clock speeds, and
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
307 // this code should use F_CPU to determine the prescale factor.
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
308 sbi(ADCSRA, ADPS2);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
309 sbi(ADCSRA, ADPS1);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
310 sbi(ADCSRA, ADPS0);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
311
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
312 // enable a2d conversions
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
313 sbi(ADCSRA, ADEN);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
314 #endif
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
315
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
316 // the bootloader connects pins 0 and 1 to the USART; disconnect them
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
317 // here so they can be used as normal digital i/o; they will be
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
318 // reconnected in Serial.begin()
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
319 #if defined(UCSRB)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
320 UCSRB = 0;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
321 #elif defined(UCSR0B)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
322 UCSR0B = 0;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
323 #endif
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
324 }

mercurial