/* Copyright (c) 2011-2012 Arduino. All right reserved. This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ #ifdef TARGET_LPC1768 #include "../../../macros.h" #include "../HAL.h" #include "arduino.h" #include "pinmapping.h" //#include "HAL_timers.h" #include "fastio.h" #define GNUM 31 typedef void (*interruptCB)(void); static interruptCB callbacksP0[GNUM]; static interruptCB callbacksP2[GNUM]; extern "C" void GpioEnableInt(uint32_t port, uint32_t pin, uint32_t mode); extern "C" void GpioDisableInt(uint32_t port, uint32_t pin); //void deadloop(void) {} /* Configure PIO interrupt sources */ static void __initialize() { int i; for (i=0; iIO0IntClr = (1 << pin); if (mode ==RISING) { LPC_GPIOINT->IO0IntEnR |= (1<IO0IntEnF &= ~(1<IO0IntEnF |= (1<IO0IntEnR &= ~(1<IO0IntEnR |= (1<IO0IntEnF |= (1<IO2IntClr = (1 << pin); if (mode ==RISING) { LPC_GPIOINT->IO2IntEnR |= (1<IO2IntEnF &= ~(1<IO2IntEnF |= (1<IO2IntEnR &= ~(1<IO2IntEnR |= (1<IO2IntEnF |= (1<IO0IntEnR &= ~(1<IO0IntEnF &= ~(1<IO0IntClr = 1 << pin; } else { LPC_GPIOINT->IO2IntEnR &= ~(1<IO2IntEnF &= ~(1<IO2IntClr = 1 << pin; } } bool isPowerOf2(unsigned int n) { return n == 1 || (n & (n-1)) == 0; } #if 0 extern "C" void EINT3_IRQHandler () { LPC_GPIOINT->IO0IntClr = LPC_GPIOINT->IO2IntClr = 0xFFFFFFFF; TOGGLE(13); //NVIC_ClearPendingIRQ(EINT3_IRQn); } #else extern "C" void EINT3_IRQHandler(void) { // Read in all current interrupt registers. We do this once as the // GPIO interrupt registers are on the APB bus, and this is slow. uint32_t rise0 = LPC_GPIOINT->IO0IntStatR; uint32_t fall0 = LPC_GPIOINT->IO0IntStatF; uint32_t rise2 = LPC_GPIOINT->IO2IntStatR; uint32_t fall2 = LPC_GPIOINT->IO2IntStatF; //Clear teh interrupts ASAP LPC_GPIOINT->IO0IntClr = LPC_GPIOINT->IO2IntClr = 0xFFFFFFFF; NVIC_ClearPendingIRQ(EINT3_IRQn); uint8_t bitloc; if (rise0 == 0) goto fall0; /* multiple pins changes happened.*/ while(rise0 > 0) { //Continue as long as there are interrupts pending bitloc = 31 - __CLZ(rise0); //CLZ returns number of leading zeros, 31 minus that is location of first pending interrupt if (callbacksP0[bitloc]!=0) callbacksP0[bitloc](); rise0 -= 1<IO0IntClr = fall0;*/ else { while(fall0 > 0) { bitloc = 31 - __CLZ(fall0); if (callbacksP0[bitloc]!=0) callbacksP0[bitloc](); fall0 -= 1<IO2IntClr = rise2; }*/ else { while(rise2 > 0) { bitloc = 31 - __CLZ(rise2); if (callbacksP2[bitloc]!=0) callbacksP2[bitloc](); //LPC_GPIOINT->IO2IntClr = 1 << bitloc; rise2 -= 1<IO2IntClr = fall2; }*/ else { while(fall2 > 0) { bitloc = 31 - __CLZ(fall2); if (callbacksP2[bitloc]!=0) callbacksP2[bitloc](); //LPC_GPIOINT->IO2IntClr = 1 << bitloc; fall2 -= 1<IO0IntClr = LPC_GPIOINT->IO2IntClr = 0xFFFFFFFF; //NVIC_ClearPendingIRQ(EINT3_IRQn); return; //silences warning } } #endif #endif // TARGET_LPC1768