Using input capture mode with the CTIMER component

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Using input capture mode with the CTIMER component

622 Views
scottm
Senior Contributor II

I'm posting an answer this time instead of a question because it's been more than four years since someone pointed out that there are no demos or explanation for using input capture mode with the SDK CTIMER driver. I'm assuming it's been forgotten at this point.

First, all of the available CTIMERs can use any of the CTIMER_INPn inputs as their trigger source. If you use the 'pins' tool, it can configure the pin multiplexing for you. Otherwise you'll need to use INPUTMUX. Despite what the SDK manual says, there are no examples provided in <SDK_ROOT>/boards/<BOARD>/driver_examples/inputmux (at least not for any board I have) so you're on your own there.

Next, use CLOCK_AttachClk() to attach a source clock, then optionally CTIMER_GetDefaultConfig() to get the default config and modify it, or otherwise set up the parameters yourself, and call CTIMER_Init() with the parameters.

Use CTIMER_RegisterCallBack() to register a callback function that will be called by the timer ISR. Note that even if you're defining a single callback, only a pointer to your array is stored so make sure the array you're passing the register function will persist in memory. Yes, the single callback function address could fit in that same pointer but that's not how the driver was written. This is one of those fun details that no one ever bothers to document.

Next, call CTIMER_SetupCapture() to select the capture source and the edge type, and finally CTIMER_StartTimer() to set the timer running, unless you want to do that in your callback function to measure the interval between two edges. You'll get an interrupt on the appropriate edge whether the timer is running or not.

The SDK provides no mechanism to access the capture value, so you'll need to access CTIMERn->CR[c] directly to read the value. CTIMER_Reset() will reset the counter value.

Contrary to what the thread from 2018 says, you don't need to clear any status flags in the callback - that's done by the driver. And the 'flags' parameter passed to your callback function comes from CTIMERn->IR, the interrupt register. Again, that's one of those things the SDK manual just neglects to mention at all.

This is a perfect example of how the SDK can end up being "worse than nothing" - without proper documentation, the only way to use it is to read the datasheet thoroughly yourself, figure out what needs to be done, and then read the driver code to figure out what's actually there.

Anyway, that's what I've found so far. Hopefully that saves someone else the trouble of figuring it out from scratch.

Scott

0 Replies