[Debug] GPIO interrupts debounce

Hi everyone, 

I was working on GPIO interrupts, and i was facing problems in interrupts in falling edge using a switcher .In the end, after many tries, i finally found a solution by implemanting a software debounce algorithm in the code . Here's my final code : 

#include <stdio.h>
#include <stdint.h>
#include <common.h>

#include <adi_processor.h>
#include <adi_pwr.h>
#include <adi_gpio.h>
#include <ADUCM3029.h>
#define CHECK_BIT(var,pos) ((var>>pos) & 1)
#define SYSTICK_MAXCOUNT ((1L<<24)-1) /* we use Systick to complete function Delay10uS(). This value only applies to ADICUP3029 board. */
#define SYSTICK_CLKFREQ   26000000L   /* Systick clock frequency in Hz. This only appies to ADICUP3029 board */

ADI_GPIO_RESULT eGpioResult;
uint8_t gpioMemory[ADI_GPIO_MEMORY_SIZE] = {0};
static void pinIntCallbackA(void* pCBParam, uint32_t Port,  void* PinIntData);
static void pinIntCallbackB(void* pCBParam, uint32_t Port,  void* PinIntData);
int UrtCfg(int iBaud);



void AD5940_Delay10us(uint32_t time)
{
  if(time==0)return;
  if(time*10<SYSTICK_MAXCOUNT/(SYSTICK_CLKFREQ/1000000)){
    SysTick->LOAD = time*10*(SYSTICK_CLKFREQ/1000000);
    SysTick->CTRL = (1 << 2) | (1<<0);    /* Enable SysTick Timer, using core clock */
    while(!((SysTick->CTRL)&(1<<16)));    /* Wait until count to zero */
    SysTick->CTRL = 0;                    /* Disable SysTick Timer */
  }
  else {
    AD5940_Delay10us(time/2);
    AD5940_Delay10us(time/2 + (time&1));
  }
}

/* GPIO2_03 configured as set out */
/* GPIO2_04 configured as input */
/* GPIO2_05 configured as output */
/* GPIO1_01 configured function 1 button */ 
/* GPIO1_15 configured function 0 led 1  */
/* GPIO2_00 configured function 0 led 2  */
void initplatform()
{
     eGpioResult= adi_gpio_Init(gpioMemory, ADI_GPIO_MEMORY_SIZE);
     UrtCfg(9600);
     
     /* Step1, enable pull resistors */
     pADI_GPIO0->PE = 0xFFFF;
     pADI_GPIO1->PE = 0xFFFF;
     //pADI_GPIO2->PE = 0xFFFF; //activate all pulls res
     pADI_GPIO2->PE = 0xFFEF; // desactivate pull up pin 2_04
    
     /* configure pins */
     pADI_GPIO1->CFG = 1<<2; /* GPIO1_01 to function 1 */
     /*GPIO1_15 & GPIO2_0 by default are 0*/
    
     /* Pin output_input mode enable*/
     pADI_GPIO1->IEN |= 1<<1;  //configure pin 1_01 as input
     pADI_GPIO1->OEN |= 1<<15; //configure pin 1_15 as output
     pADI_GPIO2->OEN |= 1<<0;  //configure pin 2_00 as output
     pADI_GPIO2->OEN |= 1<<3;  //configure pin 2_03 as output
     pADI_GPIO2->IEN |= 1<<4;  //configure pin 2_04 as input
     pADI_GPIO2->OEN |= 1<<5;  //configure pin 2_05 as output
    
    
     /* activate interruption on pin GPIO1_01 */
     pADI_GPIO1->POL |= 1<<1; //latched passing from rising edge
     pADI_GPIO1->IENA |= 1<<1;
     
     /* activate interruption on pin GPIO2_03 */
     //pADI_GPIO2->POL |= 0<<4; //latched passing from falling edge
     //pADI_GPIO2->IENB |= 1<<4;
     adi_gpio_SetGroupInterruptPolarity(ADI_GPIO_PORT2,ADI_GPIO_PIN_4);
     //adi_gpio_GroupInterruptPolarityEnable(ADI_GPIO_PORT2,ADI_GPIO_PIN_4,false);
     adi_gpio_SetGroupInterruptPins(ADI_GPIO_PORT2, ADI_GPIO_INTB_IRQ , ADI_GPIO_PIN_4);
    
     /* initialisation */
     pADI_GPIO1->CLR = 1<<15;  //Clear pin 1_15 initially
     pADI_GPIO2->CLR = 1<<0;   //Clear pin 2_00 initially
     pADI_GPIO2->SET = 1<<3;   //Set pin 2_03 initially
     printf("GPIO1_15:%02x\n",pADI_GPIO1->OUT);
     printf("GPIO2_05:%02x\n",pADI_GPIO2->OUT);
     printf("GPIO2_04:%02x\n",pADI_GPIO2->IN);
     
     adi_gpio_RegisterCallback(ADI_GPIO_INTB_IRQ, pinIntCallbackB,NULL);
     adi_gpio_RegisterCallback(ADI_GPIO_INTA_IRQ, pinIntCallbackA,NULL);
     
}

int main()
{
     initplatform();
     while(1)
     {
     }
}

static void pinIntCallbackA(void* pCBParam, uint32_t Port,  void* PinIntData)
{
	volatile int32_t i;
    /* button */
    if((Port == (uint32_t)ADI_GPIO_PORT1) && (*(uint32_t*)PinIntData & ADI_GPIO_PIN_1))
    {
    	for(i = 0; i < (int32_t)(1*26000.0); i++); //set up 1 ms time
    	if((*(uint32_t*)PinIntData & ADI_GPIO_PIN_1)) {
			pADI_GPIO1->TGL |= 1<<15;
			pADI_GPIO2->TGL |= 1<<0;
			pADI_GPIO1->INT|=1<<1;
    	}
    }
}

static void pinIntCallbackB(void* pCBParam, uint32_t Port,  void* PinIntData)
{
     volatile int32_t i;
    /* button */
    if((Port == (uint32_t)ADI_GPIO_PORT2) && (*(uint32_t*)PinIntData & ADI_GPIO_PIN_4)) 
    {
    	for(i = 0; i < (int32_t)(4*26000.0); i++); //Set up 4ms time  
    	if((*(uint32_t*)PinIntData & ADI_GPIO_PIN_4)) {
			pADI_GPIO1->TGL |= 1<<15;
			pADI_GPIO2->TGL |= 1<<0;
			pADI_GPIO1->INT|=1<<1;
    	}
    }
}

int UrtCfg(int iBaud)
{
  int iBits = 3;//8bits, 
  int iFormat = 0;//, int iBits, int iFormat
  int i1;
  int iDiv;
  int iRtC;
  int iOSR;
  int iPllMulValue;
  unsigned long long ullRtClk = 16000000;                // The root clock speed


  /*Setup P0[11:10] as UART pins*/
  pADI_GPIO0->CFG = (1<<22)|(1<<20)|(pADI_GPIO0->CFG&(~((3<<22)|(3<<20))));

  iDiv = (pADI_CLKG0_CLK->CTL1& BITM_CLKG_CLK_CTL1_PCLKDIVCNT);                 // Read UART clock as set by CLKCON1[10:8]
  iDiv = iDiv>>8;
  if (iDiv == 0)
    iDiv = 1;
  iRtC = (pADI_CLKG0_CLK->CTL0& BITM_CLKG_CLK_CTL0_CLKMUX); // Check what is the root clock

  switch (iRtC)
  {
  case 0:                                               // HFOSC selected
    ullRtClk = 26000000;
    break;

  case 1:                                               // HFXTAL selected
    if ((pADI_CLKG0_CLK->CTL0 & 0x200)==0x200)           // 26Mhz XTAL used
        ullRtClk = 26000000;
    else
        ullRtClk = 16000000;                              // Assume 16MHz XTAL
    break;

  case 2:                                               // SPLL output
    iPllMulValue = (pADI_CLKG0_CLK->CTL3 &             // Check muliplication factor in PLL settings
                    BITM_CLKG_CLK_CTL3_SPLLNSEL);      // bits[4:0]. Assume div value of 0xD in bits [14:11]
    ullRtClk = (iPllMulValue *1000000);                // Assume straight multiplication by pADI_CLKG0_CLK->CTL3[4:0]
    break;

  case 3:
    ullRtClk = 26000000;                                //External clock is assumed to be 26MhZ, if different
    break;                                             //clock speed is used, this should be changed

  default:
    break;
  }
  //   iOSR = (pADI_UART0->COMLCR2 & 0x3);
  //   iOSR = 2^(2+iOSR);
  pADI_UART0->COMLCR2 = 0x3;
  iOSR = 32;
  //i1 = (ullRtClk/(iOSR*iDiv))/iBaud;	              // UART baud rate clock source is PCLK divided by OSR
  i1 = (ullRtClk/(iOSR*iDiv))/iBaud-1;   //for bigger M and N value
  pADI_UART0->COMDIV = i1;

  pADI_UART0->COMFBR = 0x8800|(((((2048/(iOSR*iDiv))*ullRtClk)/i1)/iBaud)-2048);
  pADI_UART0->COMIEN = 0;
  pADI_UART0->COMLCR = (iFormat&0x3c)|(iBits&3);


  pADI_UART0->COMFCR = (BITM_UART_COMFCR_RFTRIG & 0x40/*RX_FIFO_4BYTE*/ ) |BITM_UART_COMFCR_FIFOEN;
  pADI_UART0->COMFCR |= BITM_UART_COMFCR_RFCLR|BITM_UART_COMFCR_TFCLR;                                   // Clear the UART FIFOs
  pADI_UART0->COMFCR &= ~(BITM_UART_COMFCR_RFCLR|BITM_UART_COMFCR_TFCLR);                                // Disable clearing mechanism

  NVIC_EnableIRQ(UART_EVT_IRQn);              // Enable UART interrupt source in NVIC
  pADI_UART0->COMIEN = BITM_UART_COMIEN_ERBFI|BITM_UART_COMIEN_ELSI; /* Rx Interrupt */
  return pADI_UART0->COMLSR;
}
#include "stdio.h"
#ifdef __ICCARM__
int putchar(int c)
#else
int fputc(int c, FILE *f)
#endif
{
  pADI_UART0->COMTX = c;
  while((pADI_UART0->COMLSR&0x20) == 0);// tx fifo empty
  return c;
}