ostimer.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. /*
  2. * Copyright (C) 2001-2005 by egnite Software GmbH. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. * 3. Neither the name of the copyright holders nor the names of
  14. * contributors may be used to endorse or promote products derived
  15. * from this software without specific prior written permission.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  18. * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  19. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  20. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  21. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  22. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  23. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  24. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  25. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  26. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
  27. * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  28. * SUCH DAMAGE.
  29. *
  30. * For additional information see http://www.ethernut.de/
  31. *
  32. */
  33. /*!
  34. * \file arch/avr/dev/ostimer.c
  35. * \brief AVR system timer support.
  36. *
  37. * \verbatim
  38. * $Id: ostimer.c 4706 2012-10-06 17:42:01Z haraldkipp $
  39. * \endverbatim
  40. */
  41. #include <cfg/os.h>
  42. #include <sys/atom.h>
  43. #include <dev/irqreg.h>
  44. #include <arch/timer.h>
  45. /*!
  46. * \brief Nominal number of system ticks per second.
  47. *
  48. * The actual frequency depends on the timer crystal.
  49. *
  50. * \note Since version 3.9.8, the default frequency had been changed
  51. * from 16 Hz to 1024 Hz, when the timer is running with an
  52. * external 32 kHz clock crystal.
  53. */
  54. #ifndef NUT_TICK_NFREQ
  55. #ifdef NUT_CPU_FREQ
  56. #define NUT_TICK_NFREQ 1000L
  57. #else
  58. #define NUT_TICK_NFREQ 1024L
  59. #endif
  60. #endif
  61. #ifdef NUT_CPU_FREQ /* ----- NUT_CPU_FREQ */
  62. #ifndef NUT_TIMER_CRYSTAL
  63. #define NUT_TIMER_CRYSTAL NUT_CPU_FREQ
  64. #endif
  65. #ifndef NUT_TIMER_PRESCALE
  66. #define NUT_TIMER_PRESCALE 128
  67. #endif
  68. #else /* ----- !NUT_CPU_FREQ */
  69. #ifndef NUT_TIMER_CRYSTAL
  70. #define NUT_TIMER_CRYSTAL 32768L
  71. #endif
  72. #ifndef NUT_TIMER_PRESCALE
  73. #define NUT_TIMER_PRESCALE 1
  74. #endif
  75. #endif /* ----- NUT_CPU_FREQ */
  76. /* Output compare register value. */
  77. #define OCR_VALUE (((2 * NUT_TIMER_CRYSTAL / (NUT_TIMER_PRESCALE * NUT_TICK_NFREQ) + 1) / 2) - 1)
  78. /* Calculated number of timer ticks per second. */
  79. #define NUT_TICK_FREQ ((2 * NUT_TIMER_CRYSTAL / (OCR_VALUE + 1) / NUT_TIMER_PRESCALE + 1) / 2)
  80. #ifdef NUT_CPU_FREQ /* ----- NUT_CPU_FREQ */
  81. /* Setup for timer clocked from main clock with NUT_CPU_FREQ
  82. * and 1000 Hz timer tick
  83. */
  84. #if defined(MCU_AT90CAN128)
  85. #define TCCR_FLAGS (_BV(CS20) | _BV(CS22) | _BV(WGM21))
  86. #elif defined(MCU_ATMEGA2560)
  87. #define TCCR_FLAGS (_BV(WGM21))
  88. #define TCCR2B_FLAGS (_BV(CS20) | _BV(CS22))
  89. #elif defined(MCU_ATMEGA2561)
  90. #define TCCR_FLAGS (_BV(WGM21))
  91. #define TCCR2B_FLAGS (_BV(CS20) | _BV(CS22))
  92. #elif defined(MCU_AT90USB1287)
  93. #define TCCR_FLAGS (_BV(WGM21))
  94. #define TCCR2B_FLAGS (_BV(CS20) | _BV(CS22))
  95. #elif defined(MCU_ATMEGA103) /* MCU_ATMEGA103 */
  96. #define TCCR_FLAGS (_BV(CS00) | _BV(CS02) | _BV(CTC0))
  97. #elif defined(MCU_ATMEGA128)
  98. #define TCCR_FLAGS (_BV(CS00) | _BV(CS02) | _BV(WGM01))
  99. #else
  100. #warning Unknown CPU Type
  101. #endif
  102. #else /* ----- !NUT_CPU_FREQ */
  103. /* Setup for timer clocked from TOSC with 32768 Hz and 1024 Hz timer tick*/
  104. #if defined(MCU_AT90CAN128)
  105. #define TCCR_FLAGS (_BV(CS20) |_BV(WGM21))
  106. #define TCCR_AFLAGS _BV(CS01)
  107. #define ASSR_BIT AS2
  108. #define ASSR_BUSY (_BV(TCN2UB) | _BV(OCR2UB) | _BV(TCR2UB))
  109. #elif defined(MCU_ATMEGA2560)
  110. #define TCCR_FLAGS (_BV(WGM21))
  111. #define TCCR2B_FLAGS (_BV(CS20))
  112. #define TCCR2B_AFLAGS (_BV(CS21))
  113. #define ASSR_BIT AS2
  114. #define ASSR_BUSY (_BV(TCN2UB) | _BV(OCR2UB) | _BV(TCR2UB))
  115. #elif defined(MCU_ATMEGA2561)
  116. #define TCCR_FLAGS (_BV(WGM21))
  117. #define TCCR2B_FLAGS (_BV(CS20))
  118. #define TCCR2B_AFLAGS (_BV(CS21))
  119. #define ASSR_BIT AS2
  120. #define ASSR_BUSY (_BV(TCN2UB) | _BV(OCR2UB) | _BV(TCR2UB))
  121. #define TCCR2B_AFLAGS (_BV(CS21))
  122. #define ASSR_BIT AS2
  123. #define ASSR_BUSY (_BV(TCN2UB) | _BV(OCR2UB) | _BV(TCR2UB))
  124. #elif defined(MCU_AT90USB1287)
  125. #define TCCR_FLAGS (_BV(WGM21))
  126. #define TCCR2B_FLAGS (_BV(CS20) | _BV(CS22))
  127. #elif defined(MCU_ATMEGA103) /* MCU_ATMEGA103 */
  128. #define TCCR_FLAGS (_BV(CS00) | _BV(CTC0))
  129. #define TCCR_AFLAGS _BV(CS01)
  130. #define ASSR_BIT AS0
  131. #define ASSR_BUSY (_BV(TCN0UB) | _BV(OCR0UB) | _BV(TCR0UB))
  132. #elif defined(MCU_ATMEGA128) /* MCU_ATMEGA128 */
  133. #define TCCR_FLAGS (_BV(CS00) | _BV(WGM01))
  134. #define TCCR_AFLAGS _BV(CS01)
  135. #define ASSR_BIT AS0
  136. #define ASSR_BUSY (_BV(TCN0UB) | _BV(OCR0UB) | _BV(TCR0UB))
  137. #else /* Other MCU */
  138. #error Unknown CPU Type
  139. #endif
  140. #endif /* ----- NUT_CPU_FREQ */
  141. #if defined(MCU_AT90CAN128) || defined(MCU_ATMEGA2560) || defined(MCU_ATMEGA2561) || defined(MCU_AT90USB1287)
  142. #define TCCRx TCCR2A
  143. #define TCNTx TCNT2
  144. #define OCRx OCR2A
  145. #define TIFR_OCFx _BV(OCF2A)
  146. #define TIFR_TOVx _BV(TOV2)
  147. #define sig_TIMER sig_OUTPUT_COMPARE2
  148. #else
  149. #define TCCRx TCCR0
  150. #define TCNTx TCNT0
  151. #define OCRx OCR0
  152. #define TIFR_OCFx _BV(OCF0)
  153. #define TIFR_TOVx _BV(TOV0)
  154. #define sig_TIMER sig_OUTPUT_COMPARE0
  155. #endif
  156. static uint32_t cpu_clock;
  157. /*! \fn CountCpuLoops(void)
  158. * \brief Count the number of code loops until timer overflows.
  159. *
  160. * \return The number of loops.
  161. */
  162. #ifndef NUT_CPU_FREQ
  163. static uint32_t CountCpuLoops(void)
  164. {
  165. #ifdef __GNUC__
  166. uint32_t rc = 1;
  167. __asm__ __volatile__("firstovf: \n\t" /* */
  168. "in %D0,%1 \n\t" /* */
  169. "andi %D0,%2 \n\t" /* */
  170. "breq firstovf \n\t" /* */
  171. "out %1,%D0 \n\n" /* */
  172. /* This loop has 8 cycles. */
  173. "nextovf: \n\t" /* */
  174. "sec \n\t" /* */
  175. "adc %A0,__zero_reg__ \n\t" /* */
  176. "adc %B0,__zero_reg__ \n\t" /* */
  177. "adc %C0,__zero_reg__ \n\t" /* */
  178. "in %D0,%1 \n\t" /* */
  179. "andi %D0,%2 \n\t" /* */
  180. "breq nextovf \n\t" /* */
  181. "clr %D0 " /* */
  182. :"=d"(rc) /* Output %0 */
  183. :"I"(_SFR_IO_ADDR(TIFR)) /* Input %1 */
  184. , "I"(TIFR_TOVx) /* Input %2 */
  185. , "0"(rc) /* Input %0 */
  186. );
  187. return rc;
  188. #elif defined(__IMAGECRAFT__)
  189. uint32_t rc;
  190. asm("CLR R0");
  191. asm("CLR R16");
  192. asm("CLR R17");
  193. asm("CLR R18");
  194. asm("firstovf:");
  195. asm("IN R19, 0x36");
  196. asm("ANDI R19,1");
  197. asm("BREQ firstovf");
  198. asm("OUT 0x36, R19");
  199. /* This loop has 8 cycles. */
  200. asm("nextovf:");
  201. asm("SEC");
  202. asm("ADC R16, R0");
  203. asm("ADC R17, R0");
  204. asm("ADC R18, R0");
  205. asm("IN R19, 0x36");
  206. asm("ANDI R19, 1");
  207. asm("BREQ nextovf");
  208. asm("CLR R19");
  209. asm("STD Y+0, R16");
  210. asm("STD Y+1, R17");
  211. asm("STD Y+2, R18");
  212. asm("STD Y+3, R19");
  213. return rc;
  214. #endif
  215. }
  216. #endif
  217. /*!
  218. * \brief Compute CPU clock in Hertz.
  219. *
  220. * This function determines the CPU clock by running
  221. * a counter loop between two timer interrupts.
  222. *
  223. * The setup requires a 32768 Hz crystal on the TOSC pins!
  224. *
  225. * \return CPU clock in Hertz.
  226. *
  227. */
  228. #ifndef NUT_CPU_FREQ
  229. static uint32_t NutComputeCpuClock(void)
  230. {
  231. uint32_t rc;
  232. /* Disable timer interrupts. */
  233. NutDisableTimerIrq();
  234. /* Select asynchronous oscillator. */
  235. sbi(ASSR, ASSR_BIT);
  236. /* Reset counter register. */
  237. outb(TCNTx, 0);
  238. /* Set prescaler to 8. Overflow will occur every 62.5 ms. */
  239. outb(TCCRx, TCCR_AFLAGS);
  240. #ifdef TCCR2B_AFLAGS
  241. outb(TCCR2B, TCCR2B_AFLAGS);
  242. #endif
  243. /* Wait for asynchronous busy clear. */
  244. while ((inb(ASSR) & ASSR_BUSY) != 0);
  245. /* Clear interrupt flag. */
  246. outb(TIFR, TIFR_TOVx);
  247. NutEnterCritical();
  248. rc = CountCpuLoops();
  249. NutExitCritical();
  250. return rc << 7;
  251. }
  252. #endif
  253. /*!
  254. * \brief Initialize system timer hardware.
  255. *
  256. * This function is automatically called by Nut/OS during system
  257. * initialization.
  258. *
  259. * \param handler System timer interrupt handler.
  260. */
  261. void NutRegisterTimer(void (*handler) (void *))
  262. {
  263. #ifdef NUT_CPU_FREQ
  264. cpu_clock = NUT_CPU_FREQ;
  265. #else
  266. cpu_clock = NutComputeCpuClock();
  267. #endif
  268. /* Disable timer interrupts. */
  269. NutDisableTimerIrq();
  270. #ifndef NUT_CPU_FREQ
  271. /* Select asynchronous oscillator. */
  272. sbi(ASSR, ASSR_BIT);
  273. #endif
  274. /* Reset counter register. */
  275. outb(TCNTx, 0);
  276. /* Set CTC mode and prescaler. */
  277. outb(TCCRx, TCCR_FLAGS);
  278. #ifdef TCCR2B_FLAGS
  279. outb(TCCR2B, TCCR2B_FLAGS);
  280. #endif
  281. /* Set output compare register. */
  282. outb(OCRx, OCR_VALUE);
  283. #ifndef NUT_CPU_FREQ
  284. /* Wait for asynchronous busy clear. */
  285. while ((inb(ASSR) & ASSR_BUSY) != 0);
  286. #endif
  287. /* Clear interrupt flags. */
  288. outb(TIFR, TIFR_OCFx);
  289. /* Enable timer compare interrupts. */
  290. NutRegisterIrqHandler(&sig_TIMER, handler, 0);
  291. }
  292. /*!
  293. * \brief Return the CPU clock in Hertz.
  294. *
  295. * \return CPU clock frequency in Hertz.
  296. */
  297. uint32_t NutArchClockGet(int idx)
  298. {
  299. return cpu_clock;
  300. }
  301. /*!
  302. * \brief Return the number of system ticks per second.
  303. *
  304. * \return System tick frequency in Hertz.
  305. */
  306. uint32_t NutGetTickClock(void)
  307. {
  308. return NUT_TICK_FREQ;
  309. }
  310. /*!
  311. * \brief Calculate system ticks for a given number of milliseconds.
  312. */
  313. uint32_t NutTimerMillisToTicks(uint32_t ms)
  314. {
  315. #if (NUT_TICK_FREQ % 1000)
  316. if (ms >= 0x3E8000UL)
  317. return (ms / 1000UL) * NUT_TICK_FREQ;
  318. return (ms * NUT_TICK_FREQ + 999UL) / 1000UL;
  319. #else
  320. return ms * (NUT_TICK_FREQ / 1000UL);
  321. #endif
  322. }