WInterrupts.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  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. case 2:
  50. EICRA = (EICRA & ~((1<<ISC20) | (1<<ISC21))) | (mode << ISC20);
  51. EIMSK |= (1<<INT2);
  52. break;
  53. case 3:
  54. EICRA = (EICRA & ~((1<<ISC30) | (1<<ISC31))) | (mode << ISC30);
  55. EIMSK |= (1<<INT3);
  56. break;
  57. #elif defined(EICRA) && defined(EICRB) && defined(EIMSK)
  58. case 2:
  59. EICRA = (EICRA & ~((1 << ISC00) | (1 << ISC01))) | (mode << ISC00);
  60. EIMSK |= (1 << INT0);
  61. break;
  62. case 3:
  63. EICRA = (EICRA & ~((1 << ISC10) | (1 << ISC11))) | (mode << ISC10);
  64. EIMSK |= (1 << INT1);
  65. break;
  66. case 4:
  67. EICRA = (EICRA & ~((1 << ISC20) | (1 << ISC21))) | (mode << ISC20);
  68. EIMSK |= (1 << INT2);
  69. break;
  70. case 5:
  71. EICRA = (EICRA & ~((1 << ISC30) | (1 << ISC31))) | (mode << ISC30);
  72. EIMSK |= (1 << INT3);
  73. break;
  74. case 0:
  75. EICRB = (EICRB & ~((1 << ISC40) | (1 << ISC41))) | (mode << ISC40);
  76. EIMSK |= (1 << INT4);
  77. break;
  78. case 1:
  79. EICRB = (EICRB & ~((1 << ISC50) | (1 << ISC51))) | (mode << ISC50);
  80. EIMSK |= (1 << INT5);
  81. break;
  82. case 6:
  83. EICRB = (EICRB & ~((1 << ISC60) | (1 << ISC61))) | (mode << ISC60);
  84. EIMSK |= (1 << INT6);
  85. break;
  86. case 7:
  87. EICRB = (EICRB & ~((1 << ISC70) | (1 << ISC71))) | (mode << ISC70);
  88. EIMSK |= (1 << INT7);
  89. break;
  90. #else
  91. case 0:
  92. #if defined(EICRA) && defined(ISC00) && defined(EIMSK)
  93. EICRA = (EICRA & ~((1 << ISC00) | (1 << ISC01))) | (mode << ISC00);
  94. EIMSK |= (1 << INT0);
  95. #elif defined(MCUCR) && defined(ISC00) && defined(GICR)
  96. MCUCR = (MCUCR & ~((1 << ISC00) | (1 << ISC01))) | (mode << ISC00);
  97. GICR |= (1 << INT0);
  98. #elif defined(MCUCR) && defined(ISC00) && defined(GIMSK)
  99. MCUCR = (MCUCR & ~((1 << ISC00) | (1 << ISC01))) | (mode << ISC00);
  100. GIMSK |= (1 << INT0);
  101. #else
  102. #error attachInterrupt not finished for this CPU (case 0)
  103. #endif
  104. break;
  105. case 1:
  106. #if defined(EICRA) && defined(ISC10) && defined(ISC11) && defined(EIMSK)
  107. EICRA = (EICRA & ~((1 << ISC10) | (1 << ISC11))) | (mode << ISC10);
  108. EIMSK |= (1 << INT1);
  109. #elif defined(MCUCR) && defined(ISC10) && defined(ISC11) && defined(GICR)
  110. MCUCR = (MCUCR & ~((1 << ISC10) | (1 << ISC11))) | (mode << ISC10);
  111. GICR |= (1 << INT1);
  112. #elif defined(MCUCR) && defined(ISC10) && defined(GIMSK) && defined(GIMSK)
  113. MCUCR = (MCUCR & ~((1 << ISC10) | (1 << ISC11))) | (mode << ISC10);
  114. GIMSK |= (1 << INT1);
  115. #else
  116. #warning attachInterrupt may need some more work for this cpu (case 1)
  117. #endif
  118. break;
  119. case 2:
  120. #if defined(EICRA) && defined(ISC20) && defined(ISC21) && defined(EIMSK)
  121. EICRA = (EICRA & ~((1 << ISC20) | (1 << ISC21))) | (mode << ISC20);
  122. EIMSK |= (1 << INT2);
  123. #elif defined(MCUCR) && defined(ISC20) && defined(ISC21) && defined(GICR)
  124. MCUCR = (MCUCR & ~((1 << ISC20) | (1 << ISC21))) | (mode << ISC20);
  125. GICR |= (1 << INT2);
  126. #elif defined(MCUCR) && defined(ISC20) && defined(GIMSK) && defined(GIMSK)
  127. MCUCR = (MCUCR & ~((1 << ISC20) | (1 << ISC21))) | (mode << ISC20);
  128. GIMSK |= (1 << INT2);
  129. #endif
  130. break;
  131. #endif
  132. }
  133. }
  134. }
  135. void detachInterrupt(uint8_t interruptNum) {
  136. if(interruptNum < EXTERNAL_NUM_INTERRUPTS) {
  137. // Disable the interrupt. (We can't assume that interruptNum is equal
  138. // to the number of the EIMSK bit to clear, as this isn't true on the
  139. // ATmega8. There, INT0 is 6 and INT1 is 7.)
  140. switch (interruptNum) {
  141. #if defined(__AVR_ATmega32U4__)
  142. case 0:
  143. EIMSK &= ~(1<<INT0);
  144. break;
  145. case 1:
  146. EIMSK &= ~(1<<INT1);
  147. break;
  148. case 2:
  149. EIMSK &= ~(1<<INT2);
  150. break;
  151. case 3:
  152. EIMSK &= ~(1<<INT3);
  153. break;
  154. #elif defined(EICRA) && defined(EICRB) && defined(EIMSK)
  155. case 2:
  156. EIMSK &= ~(1 << INT0);
  157. break;
  158. case 3:
  159. EIMSK &= ~(1 << INT1);
  160. break;
  161. case 4:
  162. EIMSK &= ~(1 << INT2);
  163. break;
  164. case 5:
  165. EIMSK &= ~(1 << INT3);
  166. break;
  167. case 0:
  168. EIMSK &= ~(1 << INT4);
  169. break;
  170. case 1:
  171. EIMSK &= ~(1 << INT5);
  172. break;
  173. case 6:
  174. EIMSK &= ~(1 << INT6);
  175. break;
  176. case 7:
  177. EIMSK &= ~(1 << INT7);
  178. break;
  179. #else
  180. case 0:
  181. #if defined(EIMSK) && defined(INT0)
  182. EIMSK &= ~(1 << INT0);
  183. #elif defined(GICR) && defined(ISC00)
  184. GICR &= ~(1 << INT0); // atmega32
  185. #elif defined(GIMSK) && defined(INT0)
  186. GIMSK &= ~(1 << INT0);
  187. #else
  188. #error detachInterrupt not finished for this cpu
  189. #endif
  190. break;
  191. case 1:
  192. #if defined(EIMSK) && defined(INT1)
  193. EIMSK &= ~(1 << INT1);
  194. #elif defined(GICR) && defined(INT1)
  195. GICR &= ~(1 << INT1); // atmega32
  196. #elif defined(GIMSK) && defined(INT1)
  197. GIMSK &= ~(1 << INT1);
  198. #else
  199. #warning detachInterrupt may need some more work for this cpu (case 1)
  200. #endif
  201. break;
  202. #endif
  203. }
  204. intFunc[interruptNum] = 0;
  205. }
  206. }
  207. /*
  208. void attachInterruptTwi(void (*userFunc)(void) ) {
  209. twiIntFunc = userFunc;
  210. }
  211. */
  212. #if defined(__AVR_ATmega32U4__)
  213. SIGNAL(INT0_vect) {
  214. if(intFunc[EXTERNAL_INT_0])
  215. intFunc[EXTERNAL_INT_0]();
  216. }
  217. SIGNAL(INT1_vect) {
  218. if(intFunc[EXTERNAL_INT_1])
  219. intFunc[EXTERNAL_INT_1]();
  220. }
  221. SIGNAL(INT2_vect) {
  222. if(intFunc[EXTERNAL_INT_2])
  223. intFunc[EXTERNAL_INT_2]();
  224. }
  225. SIGNAL(INT3_vect) {
  226. if(intFunc[EXTERNAL_INT_3])
  227. intFunc[EXTERNAL_INT_3]();
  228. }
  229. #elif defined(EICRA) && defined(EICRB)
  230. SIGNAL(INT0_vect) {
  231. if(intFunc[EXTERNAL_INT_2])
  232. intFunc[EXTERNAL_INT_2]();
  233. }
  234. SIGNAL(INT1_vect) {
  235. if(intFunc[EXTERNAL_INT_3])
  236. intFunc[EXTERNAL_INT_3]();
  237. }
  238. SIGNAL(INT2_vect) {
  239. if(intFunc[EXTERNAL_INT_4])
  240. intFunc[EXTERNAL_INT_4]();
  241. }
  242. SIGNAL(INT3_vect) {
  243. if(intFunc[EXTERNAL_INT_5])
  244. intFunc[EXTERNAL_INT_5]();
  245. }
  246. SIGNAL(INT4_vect) {
  247. if(intFunc[EXTERNAL_INT_0])
  248. intFunc[EXTERNAL_INT_0]();
  249. }
  250. SIGNAL(INT5_vect) {
  251. if(intFunc[EXTERNAL_INT_1])
  252. intFunc[EXTERNAL_INT_1]();
  253. }
  254. SIGNAL(INT6_vect) {
  255. if(intFunc[EXTERNAL_INT_6])
  256. intFunc[EXTERNAL_INT_6]();
  257. }
  258. SIGNAL(INT7_vect) {
  259. if(intFunc[EXTERNAL_INT_7])
  260. intFunc[EXTERNAL_INT_7]();
  261. }
  262. #else
  263. SIGNAL(INT0_vect) {
  264. if(intFunc[EXTERNAL_INT_0])
  265. intFunc[EXTERNAL_INT_0]();
  266. }
  267. SIGNAL(INT1_vect) {
  268. if(intFunc[EXTERNAL_INT_1])
  269. intFunc[EXTERNAL_INT_1]();
  270. }
  271. #if defined(EICRA) && defined(ISC20)
  272. SIGNAL(INT2_vect) {
  273. if(intFunc[EXTERNAL_INT_2])
  274. intFunc[EXTERNAL_INT_2]();
  275. }
  276. #endif
  277. #endif
  278. /*
  279. SIGNAL(SIG_2WIRE_SERIAL) {
  280. if(twiIntFunc)
  281. twiIntFunc();
  282. }
  283. */