PlatformIO package of the Teensy core framework compatible with GCC 10 & C++20
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.

WInterrupts.c 3.6KB

3 jaren geleden
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /* Interrupt functions for the Teensy and Teensy++
  2. * http://www.pjrc.com/teensy/
  3. * Copyright (c) 2008-2010 PJRC.COM, LLC
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining a copy
  6. * of this software and associated documentation files (the "Software"), to deal
  7. * in the Software without restriction, including without limitation the rights
  8. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. * copies of the Software, and to permit persons to whom the Software is
  10. * furnished to do so, subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be included in
  13. * all copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. * THE SOFTWARE.
  22. */
  23. #include <avr/io.h>
  24. #include <avr/interrupt.h>
  25. #include <avr/pgmspace.h>
  26. #include <stdint.h>
  27. #include "wiring.h"
  28. #include "wiring_private.h"
  29. #if defined(__AVR_ATmega32U4__)
  30. #define NUM_INTERRUPT 4
  31. #else
  32. #define NUM_INTERRUPT 8
  33. #endif
  34. volatile static voidFuncPtr intFunc[NUM_INTERRUPT];
  35. static const uint8_t PROGMEM interrupt_mode_mask[] = {0xFC, 0xF3, 0xCF, 0x3F};
  36. static uint8_t pin2int(uint8_t pin)
  37. {
  38. switch (pin) {
  39. case CORE_INT0_PIN: return 0;
  40. case CORE_INT1_PIN: return 1;
  41. case CORE_INT2_PIN: return 2;
  42. case CORE_INT3_PIN: return 3;
  43. #if !defined(__AVR_ATmega32U4__)
  44. case CORE_INT4_PIN: return 4;
  45. case CORE_INT5_PIN: return 5;
  46. case CORE_INT6_PIN: return 6;
  47. case CORE_INT7_PIN: return 7;
  48. #endif
  49. default: return 255;
  50. }
  51. }
  52. #if defined(__AVR_ATmega32U4__)
  53. void attachInterrupt(uint8_t inum, void (*userFunc)(void), uint8_t mode)
  54. {
  55. uint8_t mask;
  56. if (inum >= NUM_INTERRUPT) {
  57. inum = pin2int(inum);
  58. if (inum >= NUM_INTERRUPT) return;
  59. }
  60. intFunc[inum] = userFunc;
  61. mask = pgm_read_byte(interrupt_mode_mask + inum);
  62. mode &= 0x03;
  63. EICRA = (EICRA & mask) | (mode << (inum * 2));
  64. EIMSK |= (1 << inum);
  65. }
  66. #else
  67. void attachInterrupt(uint8_t inum, void (*userFunc)(void), uint8_t mode)
  68. {
  69. uint8_t mask, index;
  70. if (inum >= NUM_INTERRUPT) {
  71. inum = pin2int(inum);
  72. if (inum >= NUM_INTERRUPT) return;
  73. }
  74. intFunc[inum] = userFunc;
  75. index = inum & 3;
  76. mask = pgm_read_byte(interrupt_mode_mask + index);
  77. mode &= 0x03;
  78. if (inum & 4) {
  79. EICRB = (EICRB & mask) | (mode << (index * 2));
  80. } else {
  81. EICRA = (EICRA & mask) | (mode << (index * 2));
  82. }
  83. EIMSK |= (1 << inum);
  84. }
  85. #endif
  86. void detachInterrupt(uint8_t inum)
  87. {
  88. if (inum >= NUM_INTERRUPT) {
  89. inum = pin2int(inum);
  90. if (inum >= NUM_INTERRUPT) return;
  91. }
  92. EIMSK &= ~(1 << inum);
  93. intFunc[inum] = 0;
  94. }
  95. SIGNAL(INT0_vect) {
  96. if (intFunc[0]) intFunc[0](); // INT0 is pin 0 (PD0)
  97. }
  98. SIGNAL(INT1_vect) {
  99. if (intFunc[1]) intFunc[1](); // INT1 is pin 1 (PD1)
  100. }
  101. SIGNAL(INT2_vect) {
  102. if (intFunc[2]) intFunc[2](); // INT2 is pin 2 (PD2) (also Serial RX)
  103. }
  104. SIGNAL(INT3_vect) {
  105. if (intFunc[3]) intFunc[3](); // INT3 is pin 3 (PD3) (also Serial TX)
  106. }
  107. #if !defined(__AVR_ATmega32U4__)
  108. SIGNAL(INT4_vect) {
  109. if (intFunc[4]) intFunc[4](); // INT4 is pin 20 (PC7)
  110. }
  111. SIGNAL(INT5_vect) {
  112. if (intFunc[5]) intFunc[5](); // INT5 is pin 4 (PD4)
  113. }
  114. SIGNAL(INT6_vect) {
  115. if (intFunc[6]) intFunc[6](); // INT6 is pin 6 (PD6)
  116. }
  117. SIGNAL(INT7_vect) {
  118. if (intFunc[7]) intFunc[7](); // INT7 is pin 7 (PD7)
  119. }
  120. #endif