PageRenderTime 42ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/arduino-1.8.8/hardware/arduino/avr/cores/standard/wiring.c

https://gitlab.com/vapkse/arduino
C | 297 lines | 173 code | 43 blank | 81 comment | 39 complexity | 6351b7ed121442bff7fa8acda5c08bea MD5 | raw file
  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 >= 16000000L
  103. // for the 16 MHz clock on most Arduino boards
  104. // for a one-microsecond delay, simply return. the overhead
  105. // of the function call yields a delay of approximately 1 1/8 us.
  106. if (--us == 0)
  107. return;
  108. // the following loop takes a quarter of a microsecond (4 cycles)
  109. // per iteration, so execute it four times for each microsecond of
  110. // delay requested.
  111. us <<= 2;
  112. // account for the time taken in the preceeding commands.
  113. us -= 2;
  114. #else
  115. // for the 8 MHz internal clock on the ATmega168
  116. // for a one- or two-microsecond delay, simply return. the overhead of
  117. // the function calls takes more than two microseconds. can't just
  118. // subtract two, since us is unsigned; we'd overflow.
  119. if (--us == 0)
  120. return;
  121. if (--us == 0)
  122. return;
  123. // the following loop takes half of a microsecond (4 cycles)
  124. // per iteration, so execute it twice for each microsecond of
  125. // delay requested.
  126. us <<= 1;
  127. // partially compensate for the time taken by the preceeding commands.
  128. // we can't subtract any more than this or we'd overflow w/ small delays.
  129. us--;
  130. #endif
  131. // busy wait
  132. __asm__ __volatile__ (
  133. "1: sbiw %0,1" "\n\t" // 2 cycles
  134. "brne 1b" : "=w" (us) : "0" (us) // 2 cycles
  135. );
  136. }
  137. void init()
  138. {
  139. // this needs to be called before setup() or some functions won't
  140. // work there
  141. sei();
  142. // on the ATmega168, timer 0 is also used for fast hardware pwm
  143. // (using phase-correct PWM would mean that timer 0 overflowed half as often
  144. // resulting in different millis() behavior on the ATmega8 and ATmega168)
  145. #if defined(TCCR0A) && defined(WGM01)
  146. sbi(TCCR0A, WGM01);
  147. sbi(TCCR0A, WGM00);
  148. #endif
  149. // set timer 0 prescale factor to 64
  150. #if defined(__AVR_ATmega128__)
  151. // CPU specific: different values for the ATmega128
  152. sbi(TCCR0, CS02);
  153. #elif defined(TCCR0) && defined(CS01) && defined(CS00)
  154. // this combination is for the standard atmega8
  155. sbi(TCCR0, CS01);
  156. sbi(TCCR0, CS00);
  157. #elif defined(TCCR0B) && defined(CS01) && defined(CS00)
  158. // this combination is for the standard 168/328/1280/2560
  159. sbi(TCCR0B, CS01);
  160. sbi(TCCR0B, CS00);
  161. #elif defined(TCCR0A) && defined(CS01) && defined(CS00)
  162. // this combination is for the __AVR_ATmega645__ series
  163. sbi(TCCR0A, CS01);
  164. sbi(TCCR0A, CS00);
  165. #else
  166. #error Timer 0 prescale factor 64 not set correctly
  167. #endif
  168. // enable timer 0 overflow interrupt
  169. #if defined(TIMSK) && defined(TOIE0)
  170. sbi(TIMSK, TOIE0);
  171. #elif defined(TIMSK0) && defined(TOIE0)
  172. sbi(TIMSK0, TOIE0);
  173. #else
  174. #error Timer 0 overflow interrupt not set correctly
  175. #endif
  176. // timers 1 and 2 are used for phase-correct hardware pwm
  177. // this is better for motors as it ensures an even waveform
  178. // note, however, that fast pwm mode can achieve a frequency of up
  179. // 8 MHz (with a 16 MHz clock) at 50% duty cycle
  180. #if defined(TCCR1B) && defined(CS11) && defined(CS10)
  181. TCCR1B = 0;
  182. // set timer 1 prescale factor to 64
  183. sbi(TCCR1B, CS11);
  184. #if F_CPU >= 8000000L
  185. sbi(TCCR1B, CS10);
  186. #endif
  187. #elif defined(TCCR1) && defined(CS11) && defined(CS10)
  188. sbi(TCCR1, CS11);
  189. #if F_CPU >= 8000000L
  190. sbi(TCCR1, CS10);
  191. #endif
  192. #endif
  193. // put timer 1 in 8-bit phase correct pwm mode
  194. #if defined(TCCR1A) && defined(WGM10)
  195. sbi(TCCR1A, WGM10);
  196. #elif defined(TCCR1)
  197. #warning this needs to be finished
  198. #endif
  199. // set timer 2 prescale factor to 64
  200. #if defined(TCCR2) && defined(CS22)
  201. sbi(TCCR2, CS22);
  202. #elif defined(TCCR2B) && defined(CS22)
  203. sbi(TCCR2B, CS22);
  204. #else
  205. #warning Timer 2 not finished (may not be present on this CPU)
  206. #endif
  207. // configure timer 2 for phase correct pwm (8-bit)
  208. #if defined(TCCR2) && defined(WGM20)
  209. sbi(TCCR2, WGM20);
  210. #elif defined(TCCR2A) && defined(WGM20)
  211. sbi(TCCR2A, WGM20);
  212. #else
  213. #warning Timer 2 not finished (may not be present on this CPU)
  214. #endif
  215. #if defined(TCCR3B) && defined(CS31) && defined(WGM30)
  216. sbi(TCCR3B, CS31); // set timer 3 prescale factor to 64
  217. sbi(TCCR3B, CS30);
  218. sbi(TCCR3A, WGM30); // put timer 3 in 8-bit phase correct pwm mode
  219. #endif
  220. #if defined(TCCR4B) && defined(CS41) && defined(WGM40)
  221. sbi(TCCR4B, CS41); // set timer 4 prescale factor to 64
  222. sbi(TCCR4B, CS40);
  223. sbi(TCCR4A, WGM40); // put timer 4 in 8-bit phase correct pwm mode
  224. #endif
  225. #if defined(TCCR5B) && defined(CS51) && defined(WGM50)
  226. sbi(TCCR5B, CS51); // set timer 5 prescale factor to 64
  227. sbi(TCCR5B, CS50);
  228. sbi(TCCR5A, WGM50); // put timer 5 in 8-bit phase correct pwm mode
  229. #endif
  230. #if defined(ADCSRA)
  231. // set a2d prescale factor to 128
  232. // 16 MHz / 128 = 125 KHz, inside the desired 50-200 KHz range.
  233. // XXX: this will not work properly for other clock speeds, and
  234. // this code should use F_CPU to determine the prescale factor.
  235. sbi(ADCSRA, ADPS2);
  236. sbi(ADCSRA, ADPS1);
  237. sbi(ADCSRA, ADPS0);
  238. // enable a2d conversions
  239. sbi(ADCSRA, ADEN);
  240. #endif
  241. // the bootloader connects pins 0 and 1 to the USART; disconnect them
  242. // here so they can be used as normal digital i/o; they will be
  243. // reconnected in Serial.begin()
  244. #if defined(UCSRB)
  245. UCSRB = 0;
  246. #elif defined(UCSR0B)
  247. UCSR0B = 0;
  248. #endif
  249. }