How to configure Input Capture

Q

How to configure Input Capture using the netX Peripheral Driver?

A

Example code

1) Timer

DRV_TIM_HANDLE_T  g_tTimer1 = { 0 };      /** Defines and initializes an empty timer object */



 /** Configure the timer peripheral */
  g_tTimer1.tConfiguration.eDeviceID = DRV_TIM_DEVICE_ID_GPIOCNTR0;             /** DRV_TIM_DEVICE_ID_SYSTICK, DRV_TIM_DEVICE_ID_TIMER0/1/2 or DRV_TIM_DEVICE_ID_GPIOCNTR0/1/2 can be chose */
  g_tTimer1.tConfiguration.eDioIdInputReference = DRV_DIO_ID_GPIO_2;            /** DIO Input Reference must be set for GPIOCNT timer */
  g_tTimer1.tConfiguration.eOperationMode = DRV_OPERATION_MODE_IRQ;             /** interrupt mode */
  g_tTimer1.tConfiguration.tPreloadValue = (DRV_TIM_PRELOAD_VALUE_E) (DRV_TIM_PRELOAD_VALUE_100ms);    /** preload value 100ms */
  g_tTimer1.tConfiguration.eCountingMode = DRV_TIM_COUNTING_MODE_CONTINUOUS;    /** continuous mode */
  g_tTimer1.tConfiguration.eDioMskOutputReference = DRV_DIO_MSK_GPIO_2;


  /** initialize the timer device */
  if(DRV_OK != DRV_TIM_Init(&g_tTimer1))
  {
    /** Initialization Error */
    Error_Handler();
  }

  /** attaches the callback function */
  eRet = DRV_TIM_IRQAttach(&g_tTimer1, timerClbk, &ulUserParam);
  if(DRV_OK != eRet)
  {
    /** Initialization Error */
    Error_Handler();
  }

  /** Starts the timer */
  eRet = DRV_TIM_Start(&g_tTimer1);
  if(DRV_OK != eRet)
  {
    /** Initialization Error */
    Error_Handler();
  }




2) GPIO

/* Configure the capture input (don't forget to configure GPIO2 in the hardware configuration *.hwc)
 * register a callback
 * enable (unmask) the interrupt */
  DRV_DIO_ChannelSetMode(DRV_DIO_ID_GPIO_2,  DRV_DIO_MODE_CAPTURE_CONT_RISING);
  DRV_DIO_ChannelIRQAttach(DRV_DIO_ID_GPIO_2,getResult,NULL);
  DRV_DIO_ChannelIRQUnmask(DRV_DIO_ID_GPIO_2);


3) interrupt service

static uint32_t  ulClbkCalls = 0;

void timerClbk(void* pvDriverHandle, void* pvUserHandle)
{
  ulClbkCalls++;
  DRV_DIO_ChannelOutToggle(DRV_DIO_ID_MMIO_6);

  /** check user parameter */
  if(*(uint32_t*)pvUserHandle != 1234)
    Error_Handler();
}



uint32_t g_eventCnt=0;
uint32_t g_ulCapture=0;
uint32_t g_ulCapture2=0;

void getResult(void* pvDriverHandle, void* pvUserHandle)
{
  DRV_DIO_ChannelOutToggle(DRV_DIO_ID_MMIO_7);

/* 1st option: direct access to the capture register */
 gpio_app_Type* ptGpioPeripheral=(gpio_app_Type*)gpio_app_BASE; //0xFF801400
  g_ulCapture=ptGpioPeripheral->gpio_app_tc[2];

/* 2nd option: using the driver function (timer handle required, which is not passed to the callback function) */
 DRV_TIM_ChannelGetCapture(&g_tTimer1, DRV_DIO_ID_GPIO_2, &g_ulCapture2);

  g_eventCnt++;

}