Keyboard firmwares for Atmel AVR and Cortex-M
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 9.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  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. SIGNAL(TIM0_OVF_vect)
  35. #else
  36. SIGNAL(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. if (((uint16_t)micros() - start) >= 1000) {
  91. ms--;
  92. start += 1000;
  93. }
  94. }
  95. }
  96. /* Delay for the given number of microseconds. Assumes a 8 or 16 MHz clock. */
  97. void delayMicroseconds(unsigned int us)
  98. {
  99. // calling avrlib's delay_us() function with low values (e.g. 1 or
  100. // 2 microseconds) gives delays longer than desired.
  101. //delay_us(us);
  102. #if F_CPU >= 20000000L
  103. // for the 20 MHz clock on rare Arduino boards
  104. // for a one-microsecond delay, simply wait 2 cycle and return. The overhead
  105. // of the function call yields a delay of exactly a one microsecond.
  106. __asm__ __volatile__ (
  107. "nop" "\n\t"
  108. "nop"); //just waiting 2 cycle
  109. if (--us == 0)
  110. return;
  111. // the following loop takes a 1/5 of a microsecond (4 cycles)
  112. // per iteration, so execute it five times for each microsecond of
  113. // delay requested.
  114. us = (us<<2) + us; // x5 us
  115. // account for the time taken in the preceeding commands.
  116. us -= 2;
  117. #elif F_CPU >= 16000000L
  118. // for the 16 MHz clock on most Arduino boards
  119. // for a one-microsecond delay, simply return. the overhead
  120. // of the function call yields a delay of approximately 1 1/8 us.
  121. if (--us == 0)
  122. return;
  123. // the following loop takes a quarter of a microsecond (4 cycles)
  124. // per iteration, so execute it four times for each microsecond of
  125. // delay requested.
  126. us <<= 2;
  127. // account for the time taken in the preceeding commands.
  128. us -= 2;
  129. #else
  130. // for the 8 MHz internal clock on the ATmega168
  131. // for a one- or two-microsecond delay, simply return. the overhead of
  132. // the function calls takes more than two microseconds. can't just
  133. // subtract two, since us is unsigned; we'd overflow.
  134. if (--us == 0)
  135. return;
  136. if (--us == 0)
  137. return;
  138. // the following loop takes half of a microsecond (4 cycles)
  139. // per iteration, so execute it twice for each microsecond of
  140. // delay requested.
  141. us <<= 1;
  142. // partially compensate for the time taken by the preceeding commands.
  143. // we can't subtract any more than this or we'd overflow w/ small delays.
  144. us--;
  145. #endif
  146. // busy wait
  147. __asm__ __volatile__ (
  148. "1: sbiw %0,1" "\n\t" // 2 cycles
  149. "brne 1b" : "=w" (us) : "0" (us) // 2 cycles
  150. );
  151. }
  152. void init()
  153. {
  154. // this needs to be called before setup() or some functions won't
  155. // work there
  156. sei();
  157. // on the ATmega168, timer 0 is also used for fast hardware pwm
  158. // (using phase-correct PWM would mean that timer 0 overflowed half as often
  159. // resulting in different millis() behavior on the ATmega8 and ATmega168)
  160. #if defined(TCCR0A) && defined(WGM01)
  161. sbi(TCCR0A, WGM01);
  162. sbi(TCCR0A, WGM00);
  163. #endif
  164. // set timer 0 prescale factor to 64
  165. #if defined(__AVR_ATmega128__)
  166. // CPU specific: different values for the ATmega128
  167. sbi(TCCR0, CS02);
  168. #elif defined(TCCR0) && defined(CS01) && defined(CS00)
  169. // this combination is for the standard atmega8
  170. sbi(TCCR0, CS01);
  171. sbi(TCCR0, CS00);
  172. #elif defined(TCCR0B) && defined(CS01) && defined(CS00)
  173. // this combination is for the standard 168/328/1280/2560
  174. sbi(TCCR0B, CS01);
  175. sbi(TCCR0B, CS00);
  176. #elif defined(TCCR0A) && defined(CS01) && defined(CS00)
  177. // this combination is for the __AVR_ATmega645__ series
  178. sbi(TCCR0A, CS01);
  179. sbi(TCCR0A, CS00);
  180. #else
  181. #error Timer 0 prescale factor 64 not set correctly
  182. #endif
  183. // enable timer 0 overflow interrupt
  184. #if defined(TIMSK) && defined(TOIE0)
  185. sbi(TIMSK, TOIE0);
  186. #elif defined(TIMSK0) && defined(TOIE0)
  187. sbi(TIMSK0, TOIE0);
  188. #else
  189. #error Timer 0 overflow interrupt not set correctly
  190. #endif
  191. // timers 1 and 2 are used for phase-correct hardware pwm
  192. // this is better for motors as it ensures an even waveform
  193. // note, however, that fast pwm mode can achieve a frequency of up
  194. // 8 MHz (with a 16 MHz clock) at 50% duty cycle
  195. #if defined(TCCR1B) && defined(CS11) && defined(CS10)
  196. TCCR1B = 0;
  197. // set timer 1 prescale factor to 64
  198. sbi(TCCR1B, CS11);
  199. #if F_CPU >= 8000000L
  200. sbi(TCCR1B, CS10);
  201. #endif
  202. #elif defined(TCCR1) && defined(CS11) && defined(CS10)
  203. sbi(TCCR1, CS11);
  204. #if F_CPU >= 8000000L
  205. sbi(TCCR1, CS10);
  206. #endif
  207. #endif
  208. // put timer 1 in 8-bit phase correct pwm mode
  209. #if defined(TCCR1A) && defined(WGM10)
  210. sbi(TCCR1A, WGM10);
  211. #elif defined(TCCR1)
  212. #warning this needs to be finished
  213. #endif
  214. // set timer 2 prescale factor to 64
  215. #if defined(TCCR2) && defined(CS22)
  216. sbi(TCCR2, CS22);
  217. #elif defined(TCCR2B) && defined(CS22)
  218. sbi(TCCR2B, CS22);
  219. #else
  220. #warning Timer 2 not finished (may not be present on this CPU)
  221. #endif
  222. // configure timer 2 for phase correct pwm (8-bit)
  223. #if defined(TCCR2) && defined(WGM20)
  224. sbi(TCCR2, WGM20);
  225. #elif defined(TCCR2A) && defined(WGM20)
  226. sbi(TCCR2A, WGM20);
  227. #else
  228. #warning Timer 2 not finished (may not be present on this CPU)
  229. #endif
  230. #if defined(TCCR3B) && defined(CS31) && defined(WGM30)
  231. sbi(TCCR3B, CS31); // set timer 3 prescale factor to 64
  232. sbi(TCCR3B, CS30);
  233. sbi(TCCR3A, WGM30); // put timer 3 in 8-bit phase correct pwm mode
  234. #endif
  235. #if defined(TCCR4A) && defined(TCCR4B) && defined(TCCR4D) /* beginning of timer4 block for 32U4 and similar */
  236. sbi(TCCR4B, CS42); // set timer4 prescale factor to 64
  237. sbi(TCCR4B, CS41);
  238. sbi(TCCR4B, CS40);
  239. sbi(TCCR4D, WGM40); // put timer 4 in phase- and frequency-correct PWM mode
  240. sbi(TCCR4A, PWM4A); // enable PWM mode for comparator OCR4A
  241. sbi(TCCR4C, PWM4D); // enable PWM mode for comparator OCR4D
  242. #else /* beginning of timer4 block for ATMEGA1280 and ATMEGA2560 */
  243. #if defined(TCCR4B) && defined(CS41) && defined(WGM40)
  244. sbi(TCCR4B, CS41); // set timer 4 prescale factor to 64
  245. sbi(TCCR4B, CS40);
  246. sbi(TCCR4A, WGM40); // put timer 4 in 8-bit phase correct pwm mode
  247. #endif
  248. #endif /* end timer4 block for ATMEGA1280/2560 and similar */
  249. #if defined(TCCR5B) && defined(CS51) && defined(WGM50)
  250. sbi(TCCR5B, CS51); // set timer 5 prescale factor to 64
  251. sbi(TCCR5B, CS50);
  252. sbi(TCCR5A, WGM50); // put timer 5 in 8-bit phase correct pwm mode
  253. #endif
  254. #if defined(ADCSRA)
  255. // set a2d prescale factor to 128
  256. // 16 MHz / 128 = 125 KHz, inside the desired 50-200 KHz range.
  257. // XXX: this will not work properly for other clock speeds, and
  258. // this code should use F_CPU to determine the prescale factor.
  259. sbi(ADCSRA, ADPS2);
  260. sbi(ADCSRA, ADPS1);
  261. sbi(ADCSRA, ADPS0);
  262. // enable a2d conversions
  263. sbi(ADCSRA, ADEN);
  264. #endif
  265. // the bootloader connects pins 0 and 1 to the USART; disconnect them
  266. // here so they can be used as normal digital i/o; they will be
  267. // reconnected in Serial.begin()
  268. #if defined(UCSRB)
  269. UCSRB = 0;
  270. #elif defined(UCSR0B)
  271. UCSR0B = 0;
  272. #endif
  273. }