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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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. SIGNAL(TIMER0_OVF_vect)
  34. {
  35. // copy these to local variables so they can be stored in registers
  36. // (volatile variables must be read from memory on every access)
  37. unsigned long m = timer0_millis;
  38. unsigned char f = timer0_fract;
  39. m += MILLIS_INC;
  40. f += FRACT_INC;
  41. if (f >= FRACT_MAX) {
  42. f -= FRACT_MAX;
  43. m += 1;
  44. }
  45. timer0_fract = f;
  46. timer0_millis = m;
  47. timer0_overflow_count++;
  48. }
  49. unsigned long millis()
  50. {
  51. unsigned long m;
  52. uint8_t oldSREG = SREG;
  53. // disable interrupts while we read timer0_millis or we might get an
  54. // inconsistent value (e.g. in the middle of a write to timer0_millis)
  55. cli();
  56. m = timer0_millis;
  57. SREG = oldSREG;
  58. return m;
  59. }
  60. unsigned long micros() {
  61. unsigned long m;
  62. uint8_t oldSREG = SREG, t;
  63. cli();
  64. m = timer0_overflow_count;
  65. #if defined(TCNT0)
  66. t = TCNT0;
  67. #elif defined(TCNT0L)
  68. t = TCNT0L;
  69. #else
  70. #error TIMER 0 not defined
  71. #endif
  72. #ifdef TIFR0
  73. if ((TIFR0 & _BV(TOV0)) && (t < 255))
  74. m++;
  75. #else
  76. if ((TIFR & _BV(TOV0)) && (t < 255))
  77. m++;
  78. #endif
  79. SREG = oldSREG;
  80. return ((m << 8) + t) * (64 / clockCyclesPerMicrosecond());
  81. }
  82. void delay(unsigned long ms)
  83. {
  84. uint16_t start = (uint16_t)micros();
  85. while (ms > 0) {
  86. if (((uint16_t)micros() - start) >= 1000) {
  87. ms--;
  88. start += 1000;
  89. }
  90. }
  91. }
  92. /* Delay for the given number of microseconds. Assumes a 8 or 16 MHz clock. */
  93. void delayMicroseconds(unsigned int us)
  94. {
  95. // calling avrlib's delay_us() function with low values (e.g. 1 or
  96. // 2 microseconds) gives delays longer than desired.
  97. //delay_us(us);
  98. #if F_CPU >= 16000000L
  99. // for the 16 MHz clock on most Arduino boards
  100. // for a one-microsecond delay, simply return. the overhead
  101. // of the function call yields a delay of approximately 1 1/8 us.
  102. if (--us == 0)
  103. return;
  104. // the following loop takes a quarter of a microsecond (4 cycles)
  105. // per iteration, so execute it four times for each microsecond of
  106. // delay requested.
  107. us <<= 2;
  108. // account for the time taken in the preceeding commands.
  109. us -= 2;
  110. #else
  111. // for the 8 MHz internal clock on the ATmega168
  112. // for a one- or two-microsecond delay, simply return. the overhead of
  113. // the function calls takes more than two microseconds. can't just
  114. // subtract two, since us is unsigned; we'd overflow.
  115. if (--us == 0)
  116. return;
  117. if (--us == 0)
  118. return;
  119. // the following loop takes half of a microsecond (4 cycles)
  120. // per iteration, so execute it twice for each microsecond of
  121. // delay requested.
  122. us <<= 1;
  123. // partially compensate for the time taken by the preceeding commands.
  124. // we can't subtract any more than this or we'd overflow w/ small delays.
  125. us--;
  126. #endif
  127. // busy wait
  128. __asm__ __volatile__ (
  129. "1: sbiw %0,1" "\n\t" // 2 cycles
  130. "brne 1b" : "=w" (us) : "0" (us) // 2 cycles
  131. );
  132. }
  133. void init()
  134. {
  135. // this needs to be called before setup() or some functions won't
  136. // work there
  137. sei();
  138. // on the ATmega168, timer 0 is also used for fast hardware pwm
  139. // (using phase-correct PWM would mean that timer 0 overflowed half as often
  140. // resulting in different millis() behavior on the ATmega8 and ATmega168)
  141. #if defined(TCCR0A) && defined(WGM01)
  142. sbi(TCCR0A, WGM01);
  143. sbi(TCCR0A, WGM00);
  144. #endif
  145. // set timer 0 prescale factor to 64
  146. #if defined(__AVR_ATmega128__)
  147. // CPU specific: different values for the ATmega128
  148. sbi(TCCR0, CS02);
  149. #elif defined(TCCR0) && defined(CS01) && defined(CS00)
  150. // this combination is for the standard atmega8
  151. sbi(TCCR0, CS01);
  152. sbi(TCCR0, CS00);
  153. #elif defined(TCCR0B) && defined(CS01) && defined(CS00)
  154. // this combination is for the standard 168/328/1280/2560
  155. sbi(TCCR0B, CS01);
  156. sbi(TCCR0B, CS00);
  157. #elif defined(TCCR0A) && defined(CS01) && defined(CS00)
  158. // this combination is for the __AVR_ATmega645__ series
  159. sbi(TCCR0A, CS01);
  160. sbi(TCCR0A, CS00);
  161. #else
  162. #error Timer 0 prescale factor 64 not set correctly
  163. #endif
  164. // enable timer 0 overflow interrupt
  165. #if defined(TIMSK) && defined(TOIE0)
  166. sbi(TIMSK, TOIE0);
  167. #elif defined(TIMSK0) && defined(TOIE0)
  168. sbi(TIMSK0, TOIE0);
  169. #else
  170. #error Timer 0 overflow interrupt not set correctly
  171. #endif
  172. // timers 1 and 2 are used for phase-correct hardware pwm
  173. // this is better for motors as it ensures an even waveform
  174. // note, however, that fast pwm mode can achieve a frequency of up
  175. // 8 MHz (with a 16 MHz clock) at 50% duty cycle
  176. TCCR1B = 0;
  177. // set timer 1 prescale factor to 64
  178. #if defined(TCCR1B) && defined(CS11) && defined(CS10)
  179. sbi(TCCR1B, CS11);
  180. sbi(TCCR1B, CS10);
  181. #elif defined(TCCR1) && defined(CS11) && defined(CS10)
  182. sbi(TCCR1, CS11);
  183. sbi(TCCR1, CS10);
  184. #endif
  185. // put timer 1 in 8-bit phase correct pwm mode
  186. #if defined(TCCR1A) && defined(WGM10)
  187. sbi(TCCR1A, WGM10);
  188. #elif defined(TCCR1)
  189. #warning this needs to be finished
  190. #endif
  191. // set timer 2 prescale factor to 64
  192. #if defined(TCCR2) && defined(CS22)
  193. sbi(TCCR2, CS22);
  194. #elif defined(TCCR2B) && defined(CS22)
  195. sbi(TCCR2B, CS22);
  196. #else
  197. #warning Timer 2 not finished (may not be present on this CPU)
  198. #endif
  199. // configure timer 2 for phase correct pwm (8-bit)
  200. #if defined(TCCR2) && defined(WGM20)
  201. sbi(TCCR2, WGM20);
  202. #elif defined(TCCR2A) && defined(WGM20)
  203. sbi(TCCR2A, WGM20);
  204. #else
  205. #warning Timer 2 not finished (may not be present on this CPU)
  206. #endif
  207. #if defined(TCCR3B) && defined(CS31) && defined(WGM30)
  208. sbi(TCCR3B, CS31); // set timer 3 prescale factor to 64
  209. sbi(TCCR3B, CS30);
  210. sbi(TCCR3A, WGM30); // put timer 3 in 8-bit phase correct pwm mode
  211. #endif
  212. #if defined(TCCR4B) && defined(CS41) && defined(WGM40)
  213. sbi(TCCR4B, CS41); // set timer 4 prescale factor to 64
  214. sbi(TCCR4B, CS40);
  215. sbi(TCCR4A, WGM40); // put timer 4 in 8-bit phase correct pwm mode
  216. #endif
  217. #if defined(TCCR5B) && defined(CS51) && defined(WGM50)
  218. sbi(TCCR5B, CS51); // set timer 5 prescale factor to 64
  219. sbi(TCCR5B, CS50);
  220. sbi(TCCR5A, WGM50); // put timer 5 in 8-bit phase correct pwm mode
  221. #endif
  222. #if defined(ADCSRA)
  223. // set a2d prescale factor to 128
  224. // 16 MHz / 128 = 125 KHz, inside the desired 50-200 KHz range.
  225. // XXX: this will not work properly for other clock speeds, and
  226. // this code should use F_CPU to determine the prescale factor.
  227. sbi(ADCSRA, ADPS2);
  228. sbi(ADCSRA, ADPS1);
  229. sbi(ADCSRA, ADPS0);
  230. // enable a2d conversions
  231. sbi(ADCSRA, ADEN);
  232. #endif
  233. // the bootloader connects pins 0 and 1 to the USART; disconnect them
  234. // here so they can be used as normal digital i/o; they will be
  235. // reconnected in Serial.begin()
  236. #if defined(UCSRB)
  237. UCSRB = 0;
  238. #elif defined(UCSR0B)
  239. UCSR0B = 0;
  240. #endif
  241. }