You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

WInterrupts.c 8.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. /* -*- mode: jde; c-basic-offset: 2; indent-tabs-mode: nil -*- */
  2. /*
  3. Part of the Wiring project - http://wiring.uniandes.edu.co
  4. Copyright (c) 2004-05 Hernando Barragan
  5. This library is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU Lesser General Public
  7. License as published by the Free Software Foundation; either
  8. version 2.1 of the License, or (at your option) any later version.
  9. This library is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. Lesser General Public License for more details.
  13. You should have received a copy of the GNU Lesser General
  14. Public License along with this library; if not, write to the
  15. Free Software Foundation, Inc., 59 Temple Place, Suite 330,
  16. Boston, MA 02111-1307 USA
  17. Modified 24 November 2006 by David A. Mellis
  18. Modified 1 August 2010 by Mark Sproul
  19. */
  20. #include <inttypes.h>
  21. #include <avr/io.h>
  22. #include <avr/interrupt.h>
  23. #include <avr/pgmspace.h>
  24. #include <stdio.h>
  25. #include "wiring_private.h"
  26. static volatile voidFuncPtr intFunc[EXTERNAL_NUM_INTERRUPTS];
  27. // volatile static voidFuncPtr twiIntFunc;
  28. void attachInterrupt(uint8_t interruptNum, void (*userFunc)(void), int mode) {
  29. if(interruptNum < EXTERNAL_NUM_INTERRUPTS) {
  30. intFunc[interruptNum] = userFunc;
  31. // Configure the interrupt mode (trigger on low input, any change, rising
  32. // edge, or falling edge). The mode constants were chosen to correspond
  33. // to the configuration bits in the hardware register, so we simply shift
  34. // the mode into place.
  35. // Enable the interrupt.
  36. switch (interruptNum) {
  37. #if defined(__AVR_ATmega32U4__)
  38. // I hate doing this, but the register assignment differs between the 1280/2560
  39. // and the 32U4. Since avrlib defines registers PCMSK1 and PCMSK2 that aren't
  40. // even present on the 32U4 this is the only way to distinguish between them.
  41. case 0:
  42. EICRA = (EICRA & ~((1<<ISC00) | (1<<ISC01))) | (mode << ISC00);
  43. EIMSK |= (1<<INT0);
  44. break;
  45. case 1:
  46. EICRA = (EICRA & ~((1<<ISC10) | (1<<ISC11))) | (mode << ISC10);
  47. EIMSK |= (1<<INT1);
  48. break;
  49. #elif defined(EICRA) && defined(EICRB) && defined(EIMSK)
  50. case 2:
  51. EICRA = (EICRA & ~((1 << ISC00) | (1 << ISC01))) | (mode << ISC00);
  52. EIMSK |= (1 << INT0);
  53. break;
  54. case 3:
  55. EICRA = (EICRA & ~((1 << ISC10) | (1 << ISC11))) | (mode << ISC10);
  56. EIMSK |= (1 << INT1);
  57. break;
  58. case 4:
  59. EICRA = (EICRA & ~((1 << ISC20) | (1 << ISC21))) | (mode << ISC20);
  60. EIMSK |= (1 << INT2);
  61. break;
  62. case 5:
  63. EICRA = (EICRA & ~((1 << ISC30) | (1 << ISC31))) | (mode << ISC30);
  64. EIMSK |= (1 << INT3);
  65. break;
  66. case 0:
  67. EICRB = (EICRB & ~((1 << ISC40) | (1 << ISC41))) | (mode << ISC40);
  68. EIMSK |= (1 << INT4);
  69. break;
  70. case 1:
  71. EICRB = (EICRB & ~((1 << ISC50) | (1 << ISC51))) | (mode << ISC50);
  72. EIMSK |= (1 << INT5);
  73. break;
  74. case 6:
  75. EICRB = (EICRB & ~((1 << ISC60) | (1 << ISC61))) | (mode << ISC60);
  76. EIMSK |= (1 << INT6);
  77. break;
  78. case 7:
  79. EICRB = (EICRB & ~((1 << ISC70) | (1 << ISC71))) | (mode << ISC70);
  80. EIMSK |= (1 << INT7);
  81. break;
  82. #else
  83. case 0:
  84. #if defined(EICRA) && defined(ISC00) && defined(EIMSK)
  85. EICRA = (EICRA & ~((1 << ISC00) | (1 << ISC01))) | (mode << ISC00);
  86. EIMSK |= (1 << INT0);
  87. #elif defined(MCUCR) && defined(ISC00) && defined(GICR)
  88. MCUCR = (MCUCR & ~((1 << ISC00) | (1 << ISC01))) | (mode << ISC00);
  89. GICR |= (1 << INT0);
  90. #elif defined(MCUCR) && defined(ISC00) && defined(GIMSK)
  91. MCUCR = (MCUCR & ~((1 << ISC00) | (1 << ISC01))) | (mode << ISC00);
  92. GIMSK |= (1 << INT0);
  93. #else
  94. #error attachInterrupt not finished for this CPU (case 0)
  95. #endif
  96. break;
  97. case 1:
  98. #if defined(EICRA) && defined(ISC10) && defined(ISC11) && defined(EIMSK)
  99. EICRA = (EICRA & ~((1 << ISC10) | (1 << ISC11))) | (mode << ISC10);
  100. EIMSK |= (1 << INT1);
  101. #elif defined(MCUCR) && defined(ISC10) && defined(ISC11) && defined(GICR)
  102. MCUCR = (MCUCR & ~((1 << ISC10) | (1 << ISC11))) | (mode << ISC10);
  103. GICR |= (1 << INT1);
  104. #elif defined(MCUCR) && defined(ISC10) && defined(GIMSK) && defined(GIMSK)
  105. MCUCR = (MCUCR & ~((1 << ISC10) | (1 << ISC11))) | (mode << ISC10);
  106. GIMSK |= (1 << INT1);
  107. #else
  108. #warning attachInterrupt may need some more work for this cpu (case 1)
  109. #endif
  110. break;
  111. case 2:
  112. #if defined(EICRA) && defined(ISC20) && defined(ISC21) && defined(EIMSK)
  113. EICRA = (EICRA & ~((1 << ISC20) | (1 << ISC21))) | (mode << ISC20);
  114. EIMSK |= (1 << INT2);
  115. #elif defined(MCUCR) && defined(ISC20) && defined(ISC21) && defined(GICR)
  116. MCUCR = (MCUCR & ~((1 << ISC20) | (1 << ISC21))) | (mode << ISC20);
  117. GICR |= (1 << INT2);
  118. #elif defined(MCUCR) && defined(ISC20) && defined(GIMSK) && defined(GIMSK)
  119. MCUCR = (MCUCR & ~((1 << ISC20) | (1 << ISC21))) | (mode << ISC20);
  120. GIMSK |= (1 << INT2);
  121. #endif
  122. break;
  123. #endif
  124. }
  125. }
  126. }
  127. void detachInterrupt(uint8_t interruptNum) {
  128. if(interruptNum < EXTERNAL_NUM_INTERRUPTS) {
  129. // Disable the interrupt. (We can't assume that interruptNum is equal
  130. // to the number of the EIMSK bit to clear, as this isn't true on the
  131. // ATmega8. There, INT0 is 6 and INT1 is 7.)
  132. switch (interruptNum) {
  133. #if defined(__AVR_ATmega32U4__)
  134. case 0:
  135. EIMSK &= ~(1<<INT0);
  136. break;
  137. case 1:
  138. EIMSK &= ~(1<<INT1);
  139. break;
  140. #elif defined(EICRA) && defined(EICRB) && defined(EIMSK)
  141. case 2:
  142. EIMSK &= ~(1 << INT0);
  143. break;
  144. case 3:
  145. EIMSK &= ~(1 << INT1);
  146. break;
  147. case 4:
  148. EIMSK &= ~(1 << INT2);
  149. break;
  150. case 5:
  151. EIMSK &= ~(1 << INT3);
  152. break;
  153. case 0:
  154. EIMSK &= ~(1 << INT4);
  155. break;
  156. case 1:
  157. EIMSK &= ~(1 << INT5);
  158. break;
  159. case 6:
  160. EIMSK &= ~(1 << INT6);
  161. break;
  162. case 7:
  163. EIMSK &= ~(1 << INT7);
  164. break;
  165. #else
  166. case 0:
  167. #if defined(EIMSK) && defined(INT0)
  168. EIMSK &= ~(1 << INT0);
  169. #elif defined(GICR) && defined(ISC00)
  170. GICR &= ~(1 << INT0); // atmega32
  171. #elif defined(GIMSK) && defined(INT0)
  172. GIMSK &= ~(1 << INT0);
  173. #else
  174. #error detachInterrupt not finished for this cpu
  175. #endif
  176. break;
  177. case 1:
  178. #if defined(EIMSK) && defined(INT1)
  179. EIMSK &= ~(1 << INT1);
  180. #elif defined(GICR) && defined(INT1)
  181. GICR &= ~(1 << INT1); // atmega32
  182. #elif defined(GIMSK) && defined(INT1)
  183. GIMSK &= ~(1 << INT1);
  184. #else
  185. #warning detachInterrupt may need some more work for this cpu (case 1)
  186. #endif
  187. break;
  188. #endif
  189. }
  190. intFunc[interruptNum] = 0;
  191. }
  192. }
  193. /*
  194. void attachInterruptTwi(void (*userFunc)(void) ) {
  195. twiIntFunc = userFunc;
  196. }
  197. */
  198. #if defined(__AVR_ATmega32U4__)
  199. SIGNAL(INT0_vect) {
  200. if(intFunc[EXTERNAL_INT_0])
  201. intFunc[EXTERNAL_INT_0]();
  202. }
  203. SIGNAL(INT1_vect) {
  204. if(intFunc[EXTERNAL_INT_1])
  205. intFunc[EXTERNAL_INT_1]();
  206. }
  207. #elif defined(EICRA) && defined(EICRB)
  208. SIGNAL(INT0_vect) {
  209. if(intFunc[EXTERNAL_INT_2])
  210. intFunc[EXTERNAL_INT_2]();
  211. }
  212. SIGNAL(INT1_vect) {
  213. if(intFunc[EXTERNAL_INT_3])
  214. intFunc[EXTERNAL_INT_3]();
  215. }
  216. SIGNAL(INT2_vect) {
  217. if(intFunc[EXTERNAL_INT_4])
  218. intFunc[EXTERNAL_INT_4]();
  219. }
  220. SIGNAL(INT3_vect) {
  221. if(intFunc[EXTERNAL_INT_5])
  222. intFunc[EXTERNAL_INT_5]();
  223. }
  224. SIGNAL(INT4_vect) {
  225. if(intFunc[EXTERNAL_INT_0])
  226. intFunc[EXTERNAL_INT_0]();
  227. }
  228. SIGNAL(INT5_vect) {
  229. if(intFunc[EXTERNAL_INT_1])
  230. intFunc[EXTERNAL_INT_1]();
  231. }
  232. SIGNAL(INT6_vect) {
  233. if(intFunc[EXTERNAL_INT_6])
  234. intFunc[EXTERNAL_INT_6]();
  235. }
  236. SIGNAL(INT7_vect) {
  237. if(intFunc[EXTERNAL_INT_7])
  238. intFunc[EXTERNAL_INT_7]();
  239. }
  240. #else
  241. SIGNAL(INT0_vect) {
  242. if(intFunc[EXTERNAL_INT_0])
  243. intFunc[EXTERNAL_INT_0]();
  244. }
  245. SIGNAL(INT1_vect) {
  246. if(intFunc[EXTERNAL_INT_1])
  247. intFunc[EXTERNAL_INT_1]();
  248. }
  249. #if defined(EICRA) && defined(ISC20)
  250. SIGNAL(INT2_vect) {
  251. if(intFunc[EXTERNAL_INT_2])
  252. intFunc[EXTERNAL_INT_2]();
  253. }
  254. #endif
  255. #endif
  256. /*
  257. SIGNAL(SIG_2WIRE_SERIAL) {
  258. if(twiIntFunc)
  259. twiIntFunc();
  260. }
  261. */