ih_irq2.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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_irq2.c,v $
  36. *
  37. */
  38. #include <arch/avr32.h>
  39. #include <dev/irqreg.h>
  40. #include <dev/gpio.h>
  41. #include <arch/avr32/gpio.h>
  42. #include <arch/avr32/ihndlr.h>
  43. #include <avr32/io.h>
  44. #include <cfg/arch/avr32pio.h>
  45. #if defined(AVR32_EIC_IRQ_2) && defined(INTERRUPT2_ALT_PIN) && defined(INTERRUPT2_ALT_PINSET)
  46. #ifndef NUT_IRQPRI_IRQ2
  47. #define NUT_IRQPRI_IRQ2 AVR32_INTC_INT3
  48. #endif
  49. static int Interrupt2Ctl(int cmd, void *param);
  50. IRQ_HANDLER sig_INTERRUPT2 = {
  51. #ifdef NUT_PERFMON
  52. 0, /* Interrupt counter, ir_count. */
  53. #endif
  54. NULL, /* Passed argument, ir_arg. */
  55. NULL, /* Handler subroutine, ir_handler. */
  56. Interrupt2Ctl /* Interrupt control, ir_ctl. */
  57. };
  58. /*!
  59. * \brief External interrupt 2 entry.
  60. */
  61. static SIGNAL(Interrupt2Entry)
  62. {
  63. IRQ_ENTRY();
  64. #ifdef NUT_PERFMON
  65. sig_INTERRUPT2.ir_count++;
  66. #endif
  67. if (sig_INTERRUPT2.ir_handler) {
  68. (sig_INTERRUPT2.ir_handler) (sig_INTERRUPT2.ir_arg);
  69. }
  70. /* Clear interrupt */
  71. AVR32_EIC.icr = AVR32_EIC_ICR_INT2_MASK;
  72. AVR32_EIC.isr;
  73. IRQ_EXIT();
  74. }
  75. /*!
  76. * \brief External interrupt 2 control.
  77. *
  78. * \param cmd Control command.
  79. * - NUT_IRQCTL_INIT Initialize and disable interrupt.
  80. * - NUT_IRQCTL_STATUS Query interrupt status.
  81. * - NUT_IRQCTL_ENABLE Enable interrupt.
  82. * - NUT_IRQCTL_DISABLE Disable interrupt.
  83. * - NUT_IRQCTL_GETPRIO Query interrupt priority.
  84. * - NUT_IRQCTL_GETCOUNT Query and clear interrupt counter.
  85. * \param param Pointer to optional parameter.
  86. *
  87. * \return 0 on success, -1 otherwise.
  88. */
  89. static int Interrupt2Ctl(int cmd, void *param)
  90. {
  91. int rc = 0;
  92. unsigned int *ival = (unsigned int *) param;
  93. int_fast8_t enabled = AVR32_EIC.imr & AVR32_EIC_IMR_INT2_MASK;
  94. /* Disable interrupt. */
  95. if (enabled) {
  96. AVR32_EIC.idr = AVR32_EIC_IDR_INT2_MASK;
  97. AVR32_EIC.imr;
  98. }
  99. switch (cmd) {
  100. case NUT_IRQCTL_INIT:
  101. /* Setup Peripheral mux for interrupt line */
  102. gpio_enable_module_pin(INTERRUPT2_ALT_PIN, INTERRUPT2_ALT_PINSET);
  103. /* Set the vector. */
  104. register_interrupt(Interrupt2Entry, AVR32_EIC_IRQ_2, NUT_IRQPRI_IRQ2);
  105. /* Initialize to edge triggered with defined priority. */
  106. AVR32_EIC.mode &= ~AVR32_EIC_MODE_INT2_MASK;
  107. /* Clear interrupt */
  108. AVR32_EIC.icr = AVR32_EIC_ICR_INT2_MASK;
  109. break;
  110. case NUT_IRQCTL_STATUS:
  111. if (enabled) {
  112. *ival |= 1;
  113. } else {
  114. *ival &= ~1;
  115. }
  116. break;
  117. case NUT_IRQCTL_ENABLE:
  118. enabled = 1;
  119. break;
  120. case NUT_IRQCTL_DISABLE:
  121. enabled = 0;
  122. break;
  123. case NUT_IRQCTL_GETMODE:
  124. {
  125. if (AVR32_EIC.mode & AVR32_EIC_MODE_INT2_MASK) {
  126. if (AVR32_EIC.level & AVR32_EIC_LEVEL_INT2_MASK)
  127. *ival = NUT_IRQMODE_HIGHLEVEL;
  128. else
  129. *ival = NUT_IRQMODE_LOWLEVEL;
  130. } else {
  131. if (AVR32_EIC.edge & AVR32_EIC_EDGE_INT2_MASK)
  132. *ival = NUT_IRQMODE_RISINGEDGE;
  133. else
  134. *ival = NUT_IRQMODE_FALLINGEDGE;
  135. }
  136. }
  137. break;
  138. case NUT_IRQCTL_SETMODE:
  139. if (*ival == NUT_IRQMODE_LOWLEVEL) {
  140. AVR32_EIC.mode |= AVR32_EIC_MODE_INT2_MASK;
  141. AVR32_EIC.level &= ~AVR32_EIC_LEVEL_INT2_MASK;
  142. } else if (*ival == NUT_IRQMODE_HIGHLEVEL) {
  143. AVR32_EIC.mode |= AVR32_EIC_MODE_INT2_MASK;
  144. AVR32_EIC.level |= ~AVR32_EIC_LEVEL_INT2_MASK;
  145. } else if (*ival == NUT_IRQMODE_FALLINGEDGE) {
  146. AVR32_EIC.mode &= ~AVR32_EIC_MODE_INT2_MASK;
  147. AVR32_EIC.edge &= ~AVR32_EIC_EDGE_INT2_MASK;
  148. } else if (*ival == NUT_IRQMODE_RISINGEDGE) {
  149. AVR32_EIC.mode &= ~AVR32_EIC_MODE_INT2_MASK;
  150. AVR32_EIC.edge |= ~AVR32_EIC_EDGE_INT2_MASK;
  151. } else {
  152. rc = -1;
  153. }
  154. break;
  155. case NUT_IRQCTL_GETPRIO:
  156. *ival = NUT_IRQPRI_IRQ2;
  157. break;
  158. #ifdef NUT_PERFMON
  159. case NUT_IRQCTL_GETCOUNT:
  160. *ival = (unsigned int) sig_INTERRUPT2.ir_count;
  161. sig_INTERRUPT2.ir_count = 0;
  162. break;
  163. #endif
  164. default:
  165. rc = -1;
  166. break;
  167. }
  168. /* Enable interrupt. */
  169. if (enabled) {
  170. AVR32_EIC.ier = AVR32_EIC_IER_INT2_MASK;
  171. AVR32_EIC.imr;
  172. AVR32_EIC.en |= AVR32_EIC_EN_INT2_MASK;
  173. }
  174. return rc;
  175. }
  176. #endif // AVR32_EIC_IRQ_2