ih_macb.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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_macb.c,v $
  36. *
  37. */
  38. #include <arch/avr32.h>
  39. #include <dev/irqreg.h>
  40. #include <avr32/io.h>
  41. #include <arch/avr32/ihndlr.h>
  42. #if defined(AVR32_MACB)
  43. #ifndef NUT_IRQPRI_MACB
  44. #define NUT_IRQPRI_MACB AVR32_INTC_INT2
  45. #endif
  46. static int MacbIrqCtl(int cmd, void *param);
  47. IRQ_HANDLER sig_MACB = {
  48. #ifdef NUT_PERFMON
  49. 0, /* Interrupt counter, ir_count. */
  50. #endif
  51. NULL, /* Passed argument, ir_arg. */
  52. NULL, /* Handler subroutine, ir_handler. */
  53. MacbIrqCtl /* Interrupt control, ir_ctl. */
  54. };
  55. /*!
  56. * \brief MACB interrupt entry.
  57. */
  58. static SIGNAL(MacbIrqEntry)
  59. {
  60. IRQ_ENTRY();
  61. #ifdef NUT_PERFMON
  62. sig_MACB.ir_count++;
  63. #endif
  64. if (sig_MACB.ir_handler) {
  65. (sig_MACB.ir_handler) (sig_MACB.ir_arg);
  66. }
  67. IRQ_EXIT();
  68. }
  69. /*!
  70. * \brief MACB interrupt control.
  71. *
  72. * \param cmd Control command.
  73. * - NUT_IRQCTL_INIT Initialize and disable interrupt.
  74. * - NUT_IRQCTL_STATUS Query interrupt status.
  75. * - NUT_IRQCTL_ENABLE Enable interrupt.
  76. * - NUT_IRQCTL_DISABLE Disable interrupt.
  77. * - NUT_IRQCTL_GETMODE Query interrupt mode.
  78. * - NUT_IRQCTL_SETMODE Set interrupt mode (NUT_IRQMODE_LEVEL or NUT_IRQMODE_EDGE).
  79. * - NUT_IRQCTL_GETPRIO Query interrupt priority.
  80. * - NUT_IRQCTL_SETPRIO Set interrupt priority.
  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. int MacbIrqCtl(int cmd, void *param)
  87. {
  88. int rc = 0;
  89. unsigned int *ival = (unsigned int *) param;
  90. ureg_t imr = AVR32_MACB.imr;
  91. static ureg_t enabledIMR = 0;
  92. int_fast8_t enabled = imr;
  93. /* Disable interrupt. */
  94. if (enabled) {
  95. AVR32_MACB.idr = 0xFFFFFFFF;
  96. AVR32_MACB.imr;
  97. enabledIMR = imr;
  98. }
  99. switch (cmd) {
  100. case NUT_IRQCTL_INIT:
  101. /* Set the vector. */
  102. register_interrupt(MacbIrqEntry, AVR32_MACB_IRQ, NUT_IRQPRI_MACB);
  103. break;
  104. case NUT_IRQCTL_STATUS:
  105. if (enabled) {
  106. *ival |= 1;
  107. } else {
  108. *ival &= ~1;
  109. }
  110. break;
  111. case NUT_IRQCTL_ENABLE:
  112. enabled = 1;
  113. break;
  114. case NUT_IRQCTL_DISABLE:
  115. enabled = 0;
  116. break;
  117. case NUT_IRQCTL_GETPRIO:
  118. *ival = NUT_IRQPRI_MACB;
  119. break;
  120. #ifdef NUT_PERFMON
  121. case NUT_IRQCTL_GETCOUNT:
  122. *ival = (unsigned int) sig_MACB.ir_count;
  123. sig_MACB.ir_count = 0;
  124. break;
  125. #endif
  126. default:
  127. rc = -1;
  128. break;
  129. }
  130. /* Enable interrupt. */
  131. if (enabled) {
  132. AVR32_MACB.ier = enabledIMR;
  133. }
  134. return rc;
  135. }
  136. #endif