My Marlin configs for Fabrikator Mini and CTC i3 Pro B
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

wiring.c 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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: wiring.c 388 2008-03-08 22:05:23Z mellis $
  18. */
  19. #include "wiring_private.h"
  20. volatile unsigned long timer0_overflow_count = 0;
  21. volatile unsigned long timer0_clock_cycles = 0;
  22. volatile unsigned long timer0_millis = 0;
  23. SIGNAL(TIMER0_OVF_vect)
  24. {
  25. timer0_overflow_count++;
  26. // timer 0 prescale factor is 64 and the timer overflows at 256
  27. timer0_clock_cycles += 64UL * 256UL;
  28. while (timer0_clock_cycles > clockCyclesPerMicrosecond() * 1000UL) {
  29. timer0_clock_cycles -= clockCyclesPerMicrosecond() * 1000UL;
  30. timer0_millis++;
  31. }
  32. }
  33. unsigned long millis()
  34. {
  35. unsigned long m;
  36. uint8_t oldSREG = SREG;
  37. // disable interrupts while we read timer0_millis or we might get an
  38. // inconsistent value (e.g. in the middle of the timer0_millis++)
  39. cli();
  40. m = timer0_millis;
  41. SREG = oldSREG;
  42. return m;
  43. }
  44. unsigned long micros() {
  45. unsigned long m, t;
  46. uint8_t oldSREG = SREG;
  47. cli();
  48. t = TCNT0;
  49. #ifdef TIFR0
  50. if ((TIFR0 & _BV(TOV0)) && (t == 0))
  51. t = 256;
  52. #else
  53. if ((TIFR & _BV(TOV0)) && (t == 0))
  54. t = 256;
  55. #endif
  56. m = timer0_overflow_count;
  57. SREG = oldSREG;
  58. return ((m << 8) + t) * (64 / clockCyclesPerMicrosecond());
  59. }
  60. void delay(unsigned long ms)
  61. {
  62. unsigned long start = millis();
  63. while (millis() - start <= ms)
  64. ;
  65. }
  66. /* Delay for the given number of microseconds. Assumes a 8 or 16 MHz clock.
  67. * Disables interrupts, which will disrupt the millis() function if used
  68. * too frequently. */
  69. void delayMicroseconds(unsigned int us)
  70. {
  71. uint8_t oldSREG;
  72. // calling avrlib's delay_us() function with low values (e.g. 1 or
  73. // 2 microseconds) gives delays longer than desired.
  74. //delay_us(us);
  75. #if F_CPU >= 16000000L
  76. // for the 16 MHz clock on most Arduino boards
  77. // for a one-microsecond delay, simply return. the overhead
  78. // of the function call yields a delay of approximately 1 1/8 us.
  79. if (--us == 0)
  80. return;
  81. // the following loop takes a quarter of a microsecond (4 cycles)
  82. // per iteration, so execute it four times for each microsecond of
  83. // delay requested.
  84. us <<= 2;
  85. // account for the time taken in the preceeding commands.
  86. us -= 2;
  87. #else
  88. // for the 8 MHz internal clock on the ATmega168
  89. // for a one- or two-microsecond delay, simply return. the overhead of
  90. // the function calls takes more than two microseconds. can't just
  91. // subtract two, since us is unsigned; we'd overflow.
  92. if (--us == 0)
  93. return;
  94. if (--us == 0)
  95. return;
  96. // the following loop takes half of a microsecond (4 cycles)
  97. // per iteration, so execute it twice for each microsecond of
  98. // delay requested.
  99. us <<= 1;
  100. // partially compensate for the time taken by the preceeding commands.
  101. // we can't subtract any more than this or we'd overflow w/ small delays.
  102. us--;
  103. #endif
  104. // disable interrupts, otherwise the timer 0 overflow interrupt that
  105. // tracks milliseconds will make us delay longer than we want.
  106. oldSREG = SREG;
  107. cli();
  108. // busy wait
  109. __asm__ __volatile__ (
  110. "1: sbiw %0,1" "\n\t" // 2 cycles
  111. "brne 1b" : "=w" (us) : "0" (us) // 2 cycles
  112. );
  113. // reenable interrupts.
  114. SREG = oldSREG;
  115. }
  116. void init()
  117. {
  118. // this needs to be called before setup() or some functions won't
  119. // work there
  120. sei();
  121. // on the ATmega168, timer 0 is also used for fast hardware pwm
  122. // (using phase-correct PWM would mean that timer 0 overflowed half as often
  123. // resulting in different millis() behavior on the ATmega8 and ATmega168)
  124. sbi(TCCR0A, WGM01);
  125. sbi(TCCR0A, WGM00);
  126. // set timer 0 prescale factor to 64
  127. sbi(TCCR0B, CS01);
  128. sbi(TCCR0B, CS00);
  129. // enable timer 0 overflow interrupt
  130. sbi(TIMSK0, TOIE0);
  131. // timers 1 and 2 are used for phase-correct hardware pwm
  132. // this is better for motors as it ensures an even waveform
  133. // note, however, that fast pwm mode can achieve a frequency of up
  134. // 8 MHz (with a 16 MHz clock) at 50% duty cycle
  135. // set timer 1 prescale factor to 64
  136. sbi(TCCR1B, CS11);
  137. sbi(TCCR1B, CS10);
  138. // put timer 1 in 8-bit phase correct pwm mode
  139. sbi(TCCR1A, WGM10);
  140. // set timer 2 prescale factor to 64
  141. sbi(TCCR2B, CS22);
  142. // configure timer 2 for phase correct pwm (8-bit)
  143. sbi(TCCR2A, WGM20);
  144. // set a2d prescale factor to 128
  145. // 16 MHz / 128 = 125 KHz, inside the desired 50-200 KHz range.
  146. // XXX: this will not work properly for other clock speeds, and
  147. // this code should use F_CPU to determine the prescale factor.
  148. sbi(ADCSRA, ADPS2);
  149. sbi(ADCSRA, ADPS1);
  150. sbi(ADCSRA, ADPS0);
  151. // enable a2d conversions
  152. sbi(ADCSRA, ADEN);
  153. // the bootloader connects pins 0 and 1 to the USART; disconnect them
  154. // here so they can be used as normal digital i/o; they will be
  155. // reconnected in Serial.begin()
  156. UCSR0B = 0;
  157. #if defined(__AVR_ATmega644P__)
  158. //TODO: test to see if disabling this helps?
  159. //UCSR1B = 0;
  160. #endif
  161. }