ih_at91irq2.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. /*
  2. * Copyright (C) 2005-2006 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$
  35. * Revision 1.5 2008/08/11 06:59:10 haraldkipp
  36. * BSD types replaced by stdint types (feature request #1282721).
  37. *
  38. * Revision 1.4 2006/07/05 07:56:34 haraldkipp
  39. * Interrupt handler will be included only, if the related interrupt
  40. * ID is defined in the platform specific header file.
  41. *
  42. * Revision 1.3 2006/06/28 17:10:15 haraldkipp
  43. * Include more general header file for ARM.
  44. *
  45. * Revision 1.2 2006/04/07 12:22:27 haraldkipp
  46. * *** empty log message ***
  47. *
  48. * Revision 1.1 2005/10/24 08:56:09 haraldkipp
  49. * First check in.
  50. *
  51. */
  52. #include <arch/arm.h>
  53. #include <dev/irqreg.h>
  54. #if defined (IRQ2_ID)
  55. #ifndef NUT_IRQPRI_IRQ2
  56. #define NUT_IRQPRI_IRQ2 4
  57. #endif
  58. static int Interrupt2Ctl(int cmd, void *param);
  59. IRQ_HANDLER sig_INTERRUPT2 = {
  60. #ifdef NUT_PERFMON
  61. 0, /* Interrupt counter, ir_count. */
  62. #endif
  63. NULL, /* Passed argument, ir_arg. */
  64. NULL, /* Handler subroutine, ir_handler. */
  65. Interrupt2Ctl /* Interrupt control, ir_ctl. */
  66. };
  67. /*!
  68. * \brief External interrupt 2 entry.
  69. */
  70. static void Interrupt2Entry(void) NUT_NAKED_FUNC;
  71. void Interrupt2Entry(void)
  72. {
  73. IRQ_ENTRY();
  74. #ifdef NUT_PERFMON
  75. sig_INTERRUPT2.ir_count++;
  76. #endif
  77. if (sig_INTERRUPT2.ir_handler) {
  78. (sig_INTERRUPT2.ir_handler) (sig_INTERRUPT2.ir_arg);
  79. }
  80. IRQ_EXIT();
  81. }
  82. /*!
  83. * \brief External interrupt 2 control.
  84. *
  85. * \param cmd Control command.
  86. * - NUT_IRQCTL_INIT Initialize and disable interrupt.
  87. * - NUT_IRQCTL_STATUS Query interrupt status.
  88. * - NUT_IRQCTL_ENABLE Enable interrupt.
  89. * - NUT_IRQCTL_DISABLE Disable interrupt.
  90. * - NUT_IRQCTL_GETPRIO Query interrupt priority.
  91. * - NUT_IRQCTL_SETPRIO Set interrupt priority.
  92. * - NUT_IRQCTL_GETCOUNT Query and clear interrupt counter.
  93. * \param param Pointer to optional parameter.
  94. *
  95. * \return 0 on success, -1 otherwise.
  96. */
  97. static int Interrupt2Ctl(int cmd, void *param)
  98. {
  99. int rc = 0;
  100. unsigned int *ival = (unsigned int *)param;
  101. int_fast8_t enabled = inr(AIC_IMR) & _BV(IRQ2_ID);
  102. /* Disable interrupt. */
  103. if (enabled) {
  104. outr(AIC_IDCR, _BV(IRQ2_ID));
  105. }
  106. switch(cmd) {
  107. case NUT_IRQCTL_INIT:
  108. /* Set the vector. */
  109. outr(AIC_SVR(IRQ2_ID), (unsigned int)Interrupt2Entry);
  110. /* Initialize to edge triggered with defined priority. */
  111. outr(AIC_SMR(IRQ2_ID), AIC_SRCTYPE_EXT_NEGATIVE_EDGE | NUT_IRQPRI_IRQ2);
  112. /* Clear interrupt */
  113. outr(AIC_ICCR, _BV(IRQ2_ID));
  114. break;
  115. case NUT_IRQCTL_STATUS:
  116. if (enabled) {
  117. *ival |= 1;
  118. }
  119. else {
  120. *ival &= ~1;
  121. }
  122. break;
  123. case NUT_IRQCTL_ENABLE:
  124. enabled = 1;
  125. break;
  126. case NUT_IRQCTL_DISABLE:
  127. enabled = 0;
  128. break;
  129. case NUT_IRQCTL_GETMODE:
  130. {
  131. unsigned int val = inr(AIC_SMR(IRQ2_ID)) & AIC_SRCTYPE;
  132. if (val == AIC_SRCTYPE_EXT_LOW_LEVEL) {
  133. *ival = NUT_IRQMODE_LOWLEVEL;
  134. } else if (val == AIC_SRCTYPE_EXT_HIGH_LEVEL) {
  135. *ival = NUT_IRQMODE_HIGHLEVEL;
  136. } else if (val == AIC_SRCTYPE_EXT_POSITIVE_EDGE) {
  137. *ival = NUT_IRQMODE_RISINGEDGE;
  138. } else {
  139. *ival = NUT_IRQMODE_FALLINGEDGE;
  140. }
  141. }
  142. break;
  143. case NUT_IRQCTL_SETMODE:
  144. if (*ival == NUT_IRQMODE_LOWLEVEL) {
  145. outr(AIC_SMR(IRQ2_ID), (inr(AIC_SMR(IRQ2_ID)) & ~AIC_SRCTYPE) | AIC_SRCTYPE_EXT_LOW_LEVEL);
  146. } else if (*ival == NUT_IRQMODE_HIGHLEVEL) {
  147. outr(AIC_SMR(IRQ2_ID), (inr(AIC_SMR(IRQ2_ID)) & ~AIC_SRCTYPE) | AIC_SRCTYPE_EXT_HIGH_LEVEL);
  148. } else if (*ival == NUT_IRQMODE_FALLINGEDGE) {
  149. outr(AIC_SMR(IRQ2_ID), (inr(AIC_SMR(IRQ2_ID)) & ~AIC_SRCTYPE) | AIC_SRCTYPE_EXT_NEGATIVE_EDGE);
  150. } else if (*ival == NUT_IRQMODE_RISINGEDGE) {
  151. outr(AIC_SMR(IRQ2_ID), (inr(AIC_SMR(IRQ2_ID)) & ~AIC_SRCTYPE) | AIC_SRCTYPE_EXT_POSITIVE_EDGE);
  152. } else {
  153. rc = -1;
  154. }
  155. break;
  156. case NUT_IRQCTL_GETPRIO:
  157. *ival = inr(AIC_SMR(IRQ2_ID)) & AIC_PRIOR;
  158. break;
  159. case NUT_IRQCTL_SETPRIO:
  160. outr(AIC_SMR(IRQ2_ID), (inr(AIC_SMR(IRQ2_ID)) & ~AIC_PRIOR) | *ival);
  161. break;
  162. #ifdef NUT_PERFMON
  163. case NUT_IRQCTL_GETCOUNT:
  164. *ival = (unsigned int)sig_INTERRUPT2.ir_count;
  165. sig_INTERRUPT2.ir_count = 0;
  166. break;
  167. #endif
  168. default:
  169. rc = -1;
  170. break;
  171. }
  172. /* Enable interrupt. */
  173. if (enabled) {
  174. outr(AIC_IECR, _BV(IRQ2_ID));
  175. }
  176. return rc;
  177. }
  178. #endif /* IRQ2_ID */