wiring.c 9.2 KB

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