ostimer_cortex.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /*
  2. * Copyright (C) 2001-2007 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. * $Log: ostimer_at91.c,v $
  35. * Revision 1.0 2008/03/26 19:34:10 psilva
  36. * Firts revision to rum on Stellaris LM3S6965
  37. *
  38. */
  39. #include <cfg/os.h>
  40. #include <cfg/clock.h>
  41. #include <arch/cm3.h>
  42. #include <dev/irqreg.h>
  43. #include <sys/timer.h>
  44. #if defined(MCU_STM32)
  45. #include <arch/cm3/stm/stm32xxxx.h>
  46. #include <arch/cm3/stm/stm32_clk.h>
  47. #elif defined(MCU_LPC176x)
  48. #include <arch/cm3/nxp/lpc176x.h>
  49. #include <arch/cm3/nxp/lpc176x_clk.h>
  50. #elif defined(MCU_LPC177x_8x)
  51. #include <arch/cm3/nxp/lpc177x_8x.h>
  52. #include <arch/cm3/nxp/lpc177x_8x_clk.h>
  53. #elif defined(MCU_LPC407x_8x)
  54. #include <arch/cm3/nxp/lpc407x_8x.h>
  55. #include <arch/cm3/nxp/lpc407x_8x_clk.h>
  56. #else
  57. #warning "Unknown CM3 family"
  58. #endif
  59. /*!
  60. * \addtogroup xgNutArchArmOsTimerCm3
  61. */
  62. /*@{*/
  63. #ifndef NUT_TICK_FREQ
  64. #define NUT_TICK_FREQ 1000UL
  65. #endif
  66. /*!
  67. * \brief Initialize system timer.
  68. *
  69. * This function is automatically called by Nut/OS
  70. * during system initialization.
  71. *
  72. * Nut/OS uses CortexM SysTick timer via CMSIS for its
  73. * timer services.
  74. * Applications should not modify any registers of this
  75. * timer, but make use of the Nut/OS timer API.
  76. */
  77. void NutRegisterTimer(void (*handler)(void*))
  78. {
  79. /* With CortexM SysTick timer is a core timer.
  80. * It is installed by providing a function void SysTick_Handler(void).
  81. * This function is called because it moves the interrupt
  82. * vectors from flash to ram.
  83. * Then it programs the timer and starts it.
  84. */
  85. Cortex_RegisterInt(SysTick_IRQn, handler);
  86. /* Program frequency and enable is done by CMSIS function */
  87. SysTick_Config(SysCtlClockGet()/NUT_TICK_FREQ);
  88. }
  89. /*!
  90. * \brief Return the CPU clock in Hertz. Or peripheral clock in hertz
  91. * On cortex this the same.
  92. *
  93. * \return CPU clock frequency in Hertz.
  94. */
  95. uint32_t NutArchClockGet(int idx)
  96. {
  97. uint32_t clock = 0;
  98. #if defined(MCU_STM32)
  99. clock = STM_ClockGet(idx);
  100. #elif defined(MCU_LPC17xx)
  101. clock = Lpc17xx_ClockGet(idx);
  102. #else
  103. #warning "Unknown CM3 family"
  104. #endif
  105. return clock;
  106. }
  107. /*!
  108. * \brief Return the number of system ticks per second.
  109. *
  110. * \return System tick frequency in Hertz.
  111. */
  112. uint32_t NutGetTickClock(void)
  113. {
  114. return NUT_TICK_FREQ;
  115. }
  116. /*!
  117. * \brief Calculate system ticks for a given number of milliseconds.
  118. */
  119. uint32_t NutTimerMillisToTicks(uint32_t ms)
  120. {
  121. return (ms * NutGetTickClock()) / 1000;
  122. }
  123. /*@}*/