ihndlr.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. /*
  2. * Copyright (C) 2001-2005 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.8 2009/03/05 22:16:57 freckle
  36. * use __NUT_EMULATION instead of __APPLE__, __linux__, or __CYGWIN__
  37. *
  38. * Revision 1.7 2008/07/26 09:38:02 haraldkipp
  39. * Added support for NUT_IRQMODE_NONE and NUT_IRQMODE_LEVEL.
  40. *
  41. * Revision 1.6 2006/10/08 16:48:09 haraldkipp
  42. * Documentation fixed
  43. *
  44. * Revision 1.5 2006/01/05 16:51:54 haraldkipp
  45. * NutIrqSetPriority() didn't correctly return the previous priority. This
  46. * bug has been fixed.
  47. * New function NutIrqSetMode() allows to modify the interrupt modes.
  48. *
  49. * Revision 1.4 2005/10/24 10:17:24 haraldkipp
  50. * New API functions added to create platform independant drivers.
  51. * Interrupt counting requires NUT_PERFMON to be defined.
  52. *
  53. * Revision 1.3 2005/08/02 17:46:47 haraldkipp
  54. * Major API documentation update.
  55. *
  56. * Revision 1.2 2005/07/26 16:30:58 haraldkipp
  57. * Copyright added.
  58. * Temporarily exclude NutRegisterIrqHandler() from Linux builds.
  59. *
  60. */
  61. #include <sys/atom.h>
  62. #include <dev/irqreg.h>
  63. /*!
  64. * \addtogroup xgInterrupt
  65. */
  66. /*@{*/
  67. /*!
  68. * \brief Call a registered interrupt handler.
  69. */
  70. void CallHandler(IRQ_HANDLER * irh)
  71. {
  72. #ifdef NUT_PERFMON
  73. irh->ir_count++;
  74. #endif
  75. if (irh->ir_handler)
  76. (irh->ir_handler) (irh->ir_arg);
  77. }
  78. /*!
  79. * \brief Register an interrupt handler.
  80. *
  81. * This function is typically called by device drivers, but
  82. * applications may also implement their local interrupt
  83. * handlers.
  84. *
  85. * \param irq Interrupt to be associated with this handler.
  86. * \param handler This routine will be called by Nut/OS, when the
  87. * specified interrupt occurs.
  88. * \param arg Argument to be passed to the interrupt handler.
  89. *
  90. * \return 0 on success, -1 otherwise.
  91. */
  92. #ifndef __NUT_EMULATION__
  93. int NutRegisterIrqHandler(IRQ_HANDLER * irq, void (*handler) (void *), void *arg)
  94. {
  95. int rc = 0;
  96. /* Initialize this interrupt. */
  97. if (irq->ir_ctl) {
  98. rc = (irq->ir_ctl) (NUT_IRQCTL_INIT, NULL);
  99. }
  100. /* Set the interrupt handler. */
  101. irq->ir_arg = arg;
  102. irq->ir_handler = handler;
  103. return rc;
  104. }
  105. /*!
  106. * \brief Check if the corresponding interrupt is enabled or disabled.
  107. *
  108. * \param irq Interrupt to query.
  109. *
  110. * \return 0 if disabled, 1 if enabled.
  111. */
  112. int NutIrqStatus(IRQ_HANDLER * irq)
  113. {
  114. int rc = 0;
  115. int status = 0;
  116. if (irq->ir_ctl) {
  117. rc = (irq->ir_ctl) (NUT_IRQCTL_STATUS, &status);
  118. }
  119. if (rc != 0) {
  120. status = 0;
  121. }
  122. return status;
  123. }
  124. /*!
  125. * \brief Enable a specified interrupt.
  126. *
  127. * \param irq Interrupt to enable.
  128. *
  129. * \return 0 on success, -1 otherwise.
  130. */
  131. int NutIrqEnable(IRQ_HANDLER * irq)
  132. {
  133. int rc = -1;
  134. if (irq->ir_ctl) {
  135. rc = (irq->ir_ctl) (NUT_IRQCTL_ENABLE, NULL);
  136. }
  137. return rc;
  138. }
  139. /*!
  140. * \brief Disable a specified interrupt.
  141. *
  142. * \param irq Interrupt to disable.
  143. *
  144. * \return 0 on success, -1 otherwise.
  145. */
  146. int NutIrqDisable(IRQ_HANDLER * irq)
  147. {
  148. int rc = -1;
  149. if (irq->ir_ctl) {
  150. rc = (irq->ir_ctl) (NUT_IRQCTL_DISABLE, NULL);
  151. }
  152. return rc;
  153. }
  154. /*!
  155. * \brief Modify the priority level of an interrupt.
  156. *
  157. * The function returns the old priority, which makes it easy to
  158. * temporarily switch to another priority and later set back the
  159. * old one.
  160. *
  161. * \note Not all targets support dynamic interrupt prioritization.
  162. * Check the hardware data sheet for valid levels.
  163. *
  164. * \param irq Interrupt to modify.
  165. * \param level New priority level.
  166. *
  167. * \return Old priority level or -1 in case of an error.
  168. */
  169. int NutIrqSetPriority(IRQ_HANDLER * irq, int level)
  170. {
  171. int rc = -1;
  172. if (irq->ir_ctl) {
  173. int prev;
  174. rc = (irq->ir_ctl) (NUT_IRQCTL_GETPRIO, &prev);
  175. if (rc == 0 && (rc = (irq->ir_ctl) (NUT_IRQCTL_SETPRIO, &level)) == 0) {
  176. rc = prev;
  177. }
  178. }
  179. return rc;
  180. }
  181. /*!
  182. * \brief Query the priority level of an interrupt.
  183. *
  184. * The function returns the priority
  185. *
  186. * \note Not all targets support dynamic interrupt prioritization.
  187. * Check the hardware data sheet for valid levels.
  188. *
  189. * \param irq Interrupt to query.
  190. *
  191. * \return Priority level or -1 in case of an error.
  192. */
  193. int NutIrqGetPriority(IRQ_HANDLER * irq)
  194. {
  195. int rc = -1;
  196. if (irq->ir_ctl) {
  197. int level;
  198. rc = (irq->ir_ctl) (NUT_IRQCTL_GETPRIO, &level);
  199. if (rc == 0) {
  200. rc = level;
  201. }
  202. }
  203. return rc;
  204. }
  205. /*!
  206. * \brief Modify the interrupt mode.
  207. *
  208. * The function returns the old mode, which makes it easy to
  209. * temporarily switch to another mode and later set back the
  210. * old one.
  211. *
  212. * \note Not all targets support all modes. Check the hardware
  213. * data sheet for valid levels.
  214. *
  215. * \param irq Interrupt to modify.
  216. * \param mode New interrupt mode:
  217. * - NUT_IRQMODE_NONE No change. Used to query current mode.
  218. * - NUT_IRQMODE_LOWLEVEL Low level sensitive.
  219. * - NUT_IRQMODE_HIGHLEVEL High level sensitive.
  220. * - NUT_IRQMODE_FALLINGEDGE Negative edge triggered.
  221. * - NUT_IRQMODE_RISINGEDGE Positive edge triggered.
  222. * - NUT_IRQMODE_EDGE Triggers on any edge or active internal edge.
  223. * - NUT_IRQMODE_LEVEL Triggers on level change or active internal level.
  224. *
  225. *
  226. * \return Old mode or -1 in case of an error.
  227. */
  228. int NutIrqSetMode(IRQ_HANDLER * irq, int mode)
  229. {
  230. int rc = -1;
  231. if (irq->ir_ctl) {
  232. int prev;
  233. rc = (irq->ir_ctl) (NUT_IRQCTL_GETMODE, &prev);
  234. if (rc == 0 && (mode == NUT_IRQMODE_NONE || (rc = (irq->ir_ctl) (NUT_IRQCTL_SETMODE, &mode)) == 0)) {
  235. rc = prev;
  236. }
  237. }
  238. return rc;
  239. }
  240. #endif
  241. /*@}*/