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

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