My Marlin configs for Fabrikator Mini and CTC i3 Pro B
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.cpp 5.4KB

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