ostimer.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /*!
  2. * Copyright (C) 2001-2010 by egnite Software GmbH
  3. *
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. *
  10. * 1. Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * 2. Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. * 3. Neither the name of the copyright holders nor the names of
  16. * contributors may be used to endorse or promote products derived
  17. * from this software without specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20. * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  22. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  23. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  24. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  25. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  26. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  27. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  28. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
  29. * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  30. * SUCH DAMAGE.
  31. *
  32. * For additional information see http://www.ethernut.de/
  33. */
  34. /*
  35. * $Log: ostimer.c,v $
  36. *
  37. */
  38. #include <cfg/os.h>
  39. #include <cfg/clock.h>
  40. #include <arch/avr32.h>
  41. #include <dev/irqreg.h>
  42. #include <sys/timer.h>
  43. #include <arch/avr32/ihndlr.h>
  44. #include <avr32/io.h>
  45. /*!
  46. * \addtogroup xgNutArchAvr32OsTimer
  47. */
  48. /*@{*/
  49. #ifndef NUT_TICK_FREQ
  50. #define NUT_TICK_FREQ 1000UL
  51. #endif
  52. IRQ_HANDLER sig_sysCompare = {
  53. #ifdef NUT_PERFMON
  54. 0, /* Interrupt counter, ir_count. */
  55. #endif
  56. NULL, /* Passed argument, ir_arg. */
  57. NULL, /* Handler subroutine, ir_handler. */
  58. NULL /* Interrupt control, ir_ctl. */
  59. };
  60. /*!
  61. * \brief System Compare interrupt entry.
  62. */
  63. static SIGNAL(SystemCompareIrqEntry)
  64. {
  65. IRQ_ENTRY();
  66. uint32_t compare;
  67. compare = Get_system_register(AVR32_COMPARE);
  68. Set_system_register(AVR32_COMPARE, 0);
  69. if (sig_sysCompare.ir_handler) {
  70. (sig_sysCompare.ir_handler) (sig_sysCompare.ir_arg);
  71. }
  72. #if __AVR32_AP7000__ || __AT32AP7000__ || __AVR32_UC3A0512ES__ || __AVR32_UC3A1512ES__
  73. /* AP7000 and UC3 prior to rev H doesn't clear COUNT on compare match, so we need to
  74. offset COMPARE */
  75. compare += NutGetCpuClock() / NUT_TICK_FREQ;
  76. if (!compare) // Avoid disabling compare.
  77. ++compare;
  78. #endif
  79. Set_system_register(AVR32_COMPARE, compare);
  80. IRQ_EXIT();
  81. }
  82. /*!
  83. * \brief Initialize system timer.
  84. *
  85. * This function is automatically called by Nut/OS
  86. * during system initialization.
  87. *
  88. * Nut/OS uses on-chip tick counter for its timer services.
  89. * Applications should not modify any registers of this
  90. * timer, but make use of the Nut/OS timer API. Timer 1
  91. * and timer 2 are available to applications.
  92. */
  93. void NutRegisterTimer(void (*handler) (void *))
  94. {
  95. /* Set compare value for the specified tick frequency. */
  96. Set_system_register(AVR32_COMPARE, NutGetCpuClock() / NUT_TICK_FREQ + Get_system_register(AVR32_COUNT));
  97. sig_sysCompare.ir_handler = handler;
  98. register_interrupt(SystemCompareIrqEntry, AVR32_CORE_COMPARE_IRQ, AVR32_INTC_INT0);
  99. }
  100. /*!
  101. * \brief Return the CPU clock in Hertz.
  102. *
  103. * On AVR32 CPUs the processor clock may differ from the clock
  104. * driving the peripherals. In this case NutArchClockGet() will
  105. * provide the correct clock depending on it's argument.
  106. *
  107. * \return CPU clock frequency in Hertz.
  108. */
  109. uint32_t NutArchClockGet(int idx);
  110. /*!
  111. * \brief Return the number of system ticks per second.
  112. *
  113. * \return System tick frequency in Hertz.
  114. */
  115. uint32_t NutGetTickClock(void)
  116. {
  117. return NUT_TICK_FREQ;
  118. }
  119. /*!
  120. * \brief Calculate system ticks for a given number of milliseconds.
  121. */
  122. uint32_t NutTimerMillisToTicks(uint32_t ms)
  123. {
  124. #if (NUT_TICK_FREQ % 1000)
  125. if (ms >= 0x3E8000UL)
  126. return (ms / 1000UL) * NUT_TICK_FREQ;
  127. return (ms * NUT_TICK_FREQ + 999UL) / 1000UL;
  128. #else
  129. return ms * (NUT_TICK_FREQ / 1000UL);
  130. #endif
  131. }
  132. /*@}*/
  133. #if defined ( MCU_AVR32UC3L064 )
  134. #include "ostimer_uc3l.c"
  135. #else
  136. #include "ostimer_uc3a.c"
  137. #endif