OK, I finally got the functionality for which I was trying. Here is the code:
/* simple capture program */
#include "common.h"
#include "ics.h"
#include "ftm.h"
#include "sysinit.h"
static uint16_t counterVal;
void FTM2_Task(void);
void FTM0_Task(void);
int main(void)
{
sysinit();
/* FTM2 output compare mode, channel 1 clears when match */
FTM_OutputCompareInit(FTM2, FTM_CHANNEL_CHANNEL1, FTM_OUTPUT_CLEAR);
/* set C1V value, 800 usec with divide by 16 clock */
FTM_SetChannelValue(FTM2, FTM_CHANNEL_CHANNEL1, 1000);
/* Set up FTM2 CH0 as rising edge capture input */
FTM_InputCaptureInit(FTM2,FTM_CHANNEL_CHANNEL0,FTM_INPUTCAPTURE_RISINGEDGE);
/* set clock source and start the counter */
FTM_ClockSet(FTM2, FTM_CLOCK_SYSTEMCLOCK, FTM_CLOCK_PS_DIV16);
/* set MOD value */
FTM_SetModValue(FTM2,0);
/* set callback function for ISR */
FTM_SetCallback(FTM2, FTM2_Task);
/* clear any pending channel 0 interrupt flags */
FTM_ClrChannelFlag(FTM2, FTM_CHANNEL_CHANNEL0);
/* Emnable CH0 interrupt */
FTM_EnableChannelInt(FTM2, FTM_CHANNEL_CHANNEL0);
/* Clear any pending CH1 inteerupt */
FTM_ClrChannelFlag(FTM2, FTM_CHANNEL_CHANNEL1);
/* Allow FTM2 interrupts now */
NVIC_EnableIRQ(FTM2_IRQn);
while (1)
{
/* dummy read */
counterVal = FTM2->CNT;
}
}
void FTM2_Task(void)
{
static uint8_t channel;
channel = FTM_GetChannelFlag(FTM2, 0);
if (channel == 128)
{
/* Reset FTM2 counter */
FTM2->CNT = 0;
/* Force CH1 output high */
FTM_SWOutputControlSet(FTM2,FTM_CHANNEL_CHANNEL1,FTM_SWOCTRL_HIGH);
/* clear any pending CH1 interrupt flag */
FTM_ClrChannelFlag(FTM2, FTM_CHANNEL_CHANNEL1);
/* Enabvle the CH1 interrupt */
FTM_EnableChannelInt(FTM2, FTM_CHANNEL_CHANNEL1);
/* Claer CH0 capture interrupt flag */
FTM_ClrChannelFlag(FTM2, FTM_CHANNEL_CHANNEL0);
}
else // interrupt is CH1 compare
{
/* Force CH1 output low. WHY DO I NEED TO DO THIS??? */
FTM_SWOutputControlSet(FTM2,FTM_CHANNEL_CHANNEL1,FTM_SWOCTRL_LOW);
/* Clear both interrupt flags */
FTM_ClrChannelFlag(FTM2, FTM_CHANNEL_CHANNEL1);
FTM_ClrChannelFlag(FTM2, FTM_CHANNEL_CHANNEL0);
/* Disable further CH1 interrupts */
FTM_DisableChannelInt(FTM2, FTM_CHANNEL_CHANNEL1);
}
}
The big question now is, "Why do I need to use the force the CH1 output low at the compare interrupt, if the channel is already configured to clear the output bit at compare? I SHOULD just need to force it (CH1 output on PTC1) high at the capture interrupt, then the compare interrupt should bring it low, but that is not happening. I have to explicitly force the output low. What am I missing?: