My Marlin configs for Fabrikator Mini and CTC i3 Pro B
Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

WInterrupts.cpp 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /*
  2. Copyright (c) 2011-2012 Arduino. All right reserved.
  3. This library is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU Lesser General Public
  5. License as published by the Free Software Foundation; either
  6. version 2.1 of the License, or (at your option) any later version.
  7. This library is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  10. See the GNU Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public
  12. License along with this library; if not, write to the Free Software
  13. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  14. */
  15. #ifdef TARGET_LPC1768
  16. #include "../../inc/MarlinConfig.h"
  17. #include "arduino.h"
  18. #include "pinmapping.h"
  19. //#include "HAL_timers.h"
  20. #include "fastio.h"
  21. #define GNUM 31
  22. typedef void (*interruptCB)(void);
  23. static interruptCB callbacksP0[GNUM];
  24. static interruptCB callbacksP2[GNUM];
  25. extern "C" void GpioEnableInt(const uint32_t port, const uint32_t pin, const uint32_t mode);
  26. extern "C" void GpioDisableInt(const uint32_t port, const uint32_t pin);
  27. //void deadloop(void) {}
  28. /* Configure PIO interrupt sources */
  29. static void __initialize() {
  30. for (uint8_t i = 0; i < GNUM; i++) {
  31. callbacksP0[i] = 0;
  32. callbacksP2[i] = 0;
  33. }
  34. NVIC_EnableIRQ(EINT3_IRQn);
  35. }
  36. void attachInterrupt(const uint32_t pin, void (*callback)(void), uint32_t mode) {
  37. static int enabled = 0;
  38. if (!INTERRUPT_PIN(pin)) return;
  39. if (!enabled) {
  40. __initialize();
  41. ++enabled;
  42. }
  43. uint8_t myport = LPC1768_PIN_PORT(pin),
  44. mypin = LPC1768_PIN_PIN(pin);
  45. if (myport == 0)
  46. callbacksP0[mypin] = callback;
  47. else
  48. callbacksP2[mypin] = callback;
  49. // Enable interrupt
  50. GpioEnableInt(myport,mypin,mode);
  51. }
  52. void detachInterrupt(const uint32_t pin) {
  53. if (!INTERRUPT_PIN(pin)) return;
  54. const uint8_t myport = LPC1768_PIN_PORT(pin),
  55. mypin = LPC1768_PIN_PIN(pin);
  56. // Disable interrupt
  57. GpioDisableInt(myport, mypin);
  58. // unset callback
  59. if (myport == 0)
  60. callbacksP0[mypin] = 0;
  61. else //if (myport == 2 )
  62. callbacksP2[mypin] = 0;
  63. }
  64. extern "C" void GpioEnableInt(uint32_t port, uint32_t pin, uint32_t mode) {
  65. //pin here is the processor pin, not logical pin
  66. if (port == 0) {
  67. LPC_GPIOINT->IO0IntClr = _BV(pin);
  68. if (mode == RISING) {
  69. SBI(LPC_GPIOINT->IO0IntEnR, pin);
  70. CBI(LPC_GPIOINT->IO0IntEnF, pin);
  71. }
  72. else if (mode == FALLING) {
  73. SBI(LPC_GPIOINT->IO0IntEnF, pin);
  74. CBI(LPC_GPIOINT->IO0IntEnR, pin);
  75. }
  76. else if (mode == CHANGE) {
  77. SBI(LPC_GPIOINT->IO0IntEnR, pin);
  78. SBI(LPC_GPIOINT->IO0IntEnF, pin);
  79. }
  80. }
  81. else {
  82. LPC_GPIOINT->IO2IntClr = _BV(pin);
  83. if (mode == RISING) {
  84. SBI(LPC_GPIOINT->IO2IntEnR, pin);
  85. CBI(LPC_GPIOINT->IO2IntEnF, pin);
  86. }
  87. else if (mode == FALLING) {
  88. SBI(LPC_GPIOINT->IO2IntEnF, pin);
  89. CBI(LPC_GPIOINT->IO2IntEnR, pin);
  90. }
  91. else if (mode == CHANGE) {
  92. SBI(LPC_GPIOINT->IO2IntEnR, pin);
  93. SBI(LPC_GPIOINT->IO2IntEnF, pin);
  94. }
  95. }
  96. }
  97. extern "C" void GpioDisableInt(const uint32_t port, const uint32_t pin) {
  98. if (port == 0) {
  99. CBI(LPC_GPIOINT->IO0IntEnR, pin);
  100. CBI(LPC_GPIOINT->IO0IntEnF, pin);
  101. LPC_GPIOINT->IO0IntClr = _BV(pin);
  102. }
  103. else {
  104. CBI(LPC_GPIOINT->IO2IntEnR, pin);
  105. CBI(LPC_GPIOINT->IO2IntEnF, pin);
  106. LPC_GPIOINT->IO2IntClr = _BV(pin);
  107. }
  108. }
  109. bool isPowerOf2(unsigned int n) {
  110. return n == 1 || (n & (n - 1)) == 0;
  111. }
  112. #if 0
  113. extern "C" void EINT3_IRQHandler () {
  114. LPC_GPIOINT->IO0IntClr = LPC_GPIOINT->IO2IntClr = 0xFFFFFFFF;
  115. TOGGLE(13);
  116. //NVIC_ClearPendingIRQ(EINT3_IRQn);
  117. }
  118. #else
  119. extern "C" void EINT3_IRQHandler(void) {
  120. // Read in all current interrupt registers. We do this once as the
  121. // GPIO interrupt registers are on the APB bus, and this is slow.
  122. uint32_t rise0 = LPC_GPIOINT->IO0IntStatR,
  123. fall0 = LPC_GPIOINT->IO0IntStatF,
  124. rise2 = LPC_GPIOINT->IO2IntStatR,
  125. fall2 = LPC_GPIOINT->IO2IntStatF;
  126. // Clear the interrupts ASAP
  127. LPC_GPIOINT->IO0IntClr = LPC_GPIOINT->IO2IntClr = 0xFFFFFFFF;
  128. NVIC_ClearPendingIRQ(EINT3_IRQn);
  129. /* multiple pins changes happened.*/
  130. if (rise0) while (rise0 > 0) { // Continue as long as there are interrupts pending
  131. const uint8_t bitloc = 31 - __CLZ(rise0); //CLZ returns number of leading zeros, 31 minus that is location of first pending interrupt
  132. if (callbacksP0[bitloc] != NULL) callbacksP0[bitloc]();
  133. rise0 -= _BV(bitloc);
  134. }
  135. if (fall0) while (fall0 > 0) {
  136. const uint8_t bitloc = 31 - __CLZ(fall0);
  137. if (callbacksP0[bitloc] != NULL) callbacksP0[bitloc]();
  138. fall0 -= _BV(bitloc);
  139. }
  140. if (rise2) while(rise2 > 0) {
  141. const uint8_t bitloc = 31 - __CLZ(rise2);
  142. if (callbacksP2[bitloc] != NULL) callbacksP2[bitloc]();
  143. //LPC_GPIOINT->IO2IntClr = 1 << bitloc;
  144. rise2 -= _BV(bitloc);
  145. }
  146. if (fall2) while (fall2 > 0) {
  147. const uint8_t bitloc = 31 - __CLZ(fall2);
  148. if (callbacksP2[bitloc] != NULL) callbacksP2[bitloc]();
  149. //LPC_GPIOINT->IO2IntClr = 1 << bitloc;
  150. fall2 -= _BV(bitloc);
  151. }
  152. //NVIC_ClearPendingIRQ(EINT3_IRQn);
  153. //LPC_GPIOINT->IO0IntClr = LPC_GPIOINT->IO2IntClr = 0xFFFFFFFF;
  154. //NVIC_ClearPendingIRQ(EINT3_IRQn);
  155. }
  156. #endif
  157. #endif // TARGET_LPC1768