ostimer.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /*
  2. * Copyright (C) 2000-2004 by ETH Zurich
  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 ETH ZURICH 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 ETH ZURICH
  21. * 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. * unix_timer.c - unix emulation of a real-time clock that is used as a timer
  35. *
  36. * 2004.04.01 Matthias Ringwald <matthias.ringwald@inf.ethz.ch>
  37. *
  38. */
  39. #ifdef __CYGWIN__
  40. #include <sys/features.h>
  41. #include <sys/signal.h>
  42. #endif
  43. #include <cfg/os.h>
  44. #include <dev/irqreg.h>
  45. #include <sys/atom.h>
  46. #include <unistd.h>
  47. #ifndef NUT_CPU_FREQ
  48. #define NUT_CPU_FREQ 14000000UL
  49. #endif
  50. extern int timer_count;
  51. /* timer thread, generating ms ticks */
  52. static pthread_t timer_thread;
  53. /*!
  54. * \brief Timer emulation
  55. *
  56. * send interrupt signal to NUT thread on every 10ms tick
  57. */
  58. #define SCALE 1
  59. void *NutTimerEmulation(void *arg)
  60. {
  61. uint8_t trigger_irq = (uint8_t) (uintptr_t) arg;
  62. // non-nut thread => not interested in SIGUSR1 (IRQ signals)
  63. pthread_sigmask(SIG_BLOCK, &irq_signal, 0);
  64. for( ;; ) {
  65. usleep( 1000 * SCALE );
  66. NutUnixRaiseInterrupt(trigger_irq);
  67. }
  68. return NULL;
  69. }
  70. /*!
  71. * \brief Initialize system timer.
  72. *
  73. * This function is automatically called by Nut/OS
  74. * during system initialization.
  75. */
  76. void NutRegisterTimer(void (*handler) (void *))
  77. {
  78. uint8_t timerIrqNr = IRQ_TIMER0;
  79. // register irq handler
  80. NutRegisterIrqHandler(timerIrqNr, handler, (void *) 0);
  81. // create rtc timer simulation
  82. pthread_create(&timer_thread, NULL, NutTimerEmulation, (void *) (uintptr_t) timerIrqNr);
  83. }
  84. /*!
  85. * \brief Return the CPU clock in Hertz.
  86. *
  87. * \return CPU clock frequency in Hertz.
  88. */
  89. uint32_t NutArchClockGet(int idx)
  90. {
  91. return NUT_CPU_FREQ;
  92. }
  93. /*!
  94. * \brief Return the number of system ticks per second.
  95. *
  96. * \return System tick frequency in Hertz.
  97. */
  98. uint32_t NutGetTickClock(void)
  99. {
  100. return 1000UL;
  101. }
  102. /*!
  103. * \brief Calculate system ticks for a given number of milliseconds.
  104. */
  105. uint32_t NutTimerMillisToTicks(uint32_t ms)
  106. {
  107. return ms;
  108. }