ih_rtc.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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: ih_rtc.c,v $
  36. *
  37. */
  38. #include <arch/avr32.h>
  39. #include <arch/avr32/pm.h>
  40. #include <dev/irqreg.h>
  41. #include <avr32/io.h>
  42. #include <arch/avr32/ihndlr.h>
  43. #ifndef NUT_IRQPRI_RTC
  44. #define NUT_IRQPRI_RTC AVR32_INTC_INT0
  45. #endif
  46. // Work around differences between ES and production chips.
  47. #ifndef AVR32_RTC_CTRL_CLKEN_MASK
  48. #define AVR32_RTC_CTRL_CLKEN_MASK AVR32_RTC_CTRL_EN_MASK
  49. #endif
  50. static int RealTimeCounterIrqCtl(int cmd, void *param);
  51. IRQ_HANDLER sig_RTC = {
  52. #ifdef NUT_PERFMON
  53. 0, /* Interrupt counter, ir_count. */
  54. #endif
  55. NULL, /* Passed argument, ir_arg. */
  56. NULL, /* Handler subroutine, ir_handler. */
  57. RealTimeCounterIrqCtl /* Interrupt control, ir_ctl. */
  58. };
  59. /*!
  60. * \brief Real Time Counter interrupt entry.
  61. */
  62. static SIGNAL(RealTimeCounterIrqEntry)
  63. {
  64. IRQ_ENTRY();
  65. #ifdef NUT_PERFMON
  66. sig_RTC.ir_count++;
  67. #endif
  68. if (sig_RTC.ir_handler) {
  69. (sig_RTC.ir_handler) (sig_RTC.ir_arg);
  70. }
  71. IRQ_EXIT();
  72. }
  73. /*!
  74. * \brief Real Time Counter interrupt control.
  75. *
  76. * \param cmd Control command.
  77. * - NUT_IRQCTL_INIT Initialize and disable interrupt.
  78. * - NUT_IRQCTL_STATUS Query interrupt status.
  79. * - NUT_IRQCTL_ENABLE Enable interrupt.
  80. * - NUT_IRQCTL_DISABLE Disable interrupt.
  81. * - NUT_IRQCTL_GETCOUNT Query and clear interrupt counter.
  82. * \param param Pointer to optional parameter.
  83. *
  84. * \return 0 on success, -1 otherwise.
  85. */
  86. static int RealTimeCounterIrqCtl(int cmd, void *param)
  87. {
  88. int rc = 0;
  89. unsigned int *ival = (unsigned int *) param;
  90. int_fast8_t enabled = AVR32_RTC.imr & AVR32_RTC_IMR_TOPI_MASK;
  91. /* Disable interrupt. */
  92. if (enabled) {
  93. AVR32_RTC.idr = AVR32_RTC_IDR_TOPI_MASK;
  94. AVR32_RTC.imr;
  95. }
  96. switch (cmd) {
  97. case NUT_IRQCTL_INIT:
  98. /* Enable 32k oscillator crystal */
  99. pm_enable_osc32_crystal(&AVR32_PM);
  100. pm_enable_clk32_no_wait(&AVR32_PM, AVR32_PM_OSCCTRL32_STARTUP_0_RCOSC);
  101. /* Wait until the rtc CTRL register is up-to-date */
  102. while ((AVR32_RTC.ctrl & AVR32_RTC_CTRL_BUSY_MASK));
  103. /* Set the new RTC configuration to 1ms */
  104. AVR32_RTC.ctrl = 1 << AVR32_RTC_CTRL_CLK32_OFFSET | 4 << AVR32_RTC_CTRL_PSEL_OFFSET | AVR32_RTC_CTRL_CLKEN_MASK;
  105. /* Wait until the rtc CTRL register is up-to-date */
  106. while ((AVR32_RTC.ctrl & AVR32_RTC_CTRL_BUSY_MASK));
  107. register_interrupt(RealTimeCounterIrqEntry, AVR32_RTC_IRQ, NUT_IRQPRI_RTC);
  108. break;
  109. case NUT_IRQCTL_STATUS:
  110. if (enabled) {
  111. *ival |= 1;
  112. } else {
  113. *ival &= ~1;
  114. }
  115. break;
  116. case NUT_IRQCTL_ENABLE:
  117. enabled = 1;
  118. break;
  119. case NUT_IRQCTL_DISABLE:
  120. enabled = 0;
  121. break;
  122. #ifdef NUT_PERFMON
  123. case NUT_IRQCTL_GETCOUNT:
  124. *ival = (unsigned int) sig_TC0.ir_count;
  125. sig_RTC.ir_count = 0;
  126. break;
  127. #endif
  128. default:
  129. rc = -1;
  130. break;
  131. }
  132. /* Enable interrupt. */
  133. if (enabled) {
  134. AVR32_RTC.ier = AVR32_RTC_IER_TOPI_MASK;
  135. }
  136. return rc;
  137. }