problem using two input capture

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

problem using two input capture

1,360 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by eplant_lab on Mon Jan 18 04:34:50 MST 2016
Hello, we have an application in which we have to monitor two signals ( square wave ), using a LPC1765
If the signal is present we have to detect the frequency. 
we use input capture pin CAP1.0 and CAP1.1. We use TIMER1 for CAP1.0 and TIMER2 for CAP1.1.

these are the TIMER init functions

void TIMER1_Init(void) {

NVIC_DisableIRQ(TIMER1_IRQn);
NVIC_ClearPendingIRQ(TIMER1_IRQn);

Chip_TIMER_Init(LPC_TIMER1);
Chip_TIMER_Reset(LPC_TIMER1);

Chip_TIMER_PrescaleSet(LPC_TIMER1, (uint32_t) TIMER1_PRESCALE);

Chip_TIMER_CaptureRisingEdgeEnable(LPC_TIMER1, CAP_0);
Input1CaptureEdge = RISING_EDGE;

Chip_TIMER_CaptureEnableInt(LPC_TIMER1, CAP_0);

Chip_TIMER_MatchEnableInt(LPC_TIMER1, 1);
Chip_TIMER_SetMatch(LPC_TIMER1, 1, DETECT_TIME);
Chip_TIMER_ResetOnMatchEnable(LPC_TIMER1, 1);

Chip_TIMER_Enable(LPC_TIMER1);
NVIC_SetPriority(TIMER1_IRQn, 5);
NVIC_EnableIRQ(TIMER1_IRQn);

}


void TIMER2_Init(void) {

NVIC_DisableIRQ(TIMER2_IRQn);
NVIC_ClearPendingIRQ(TIMER2_IRQn);

Chip_Clock_EnablePeriphClock(SYSCTL_CLOCK_TIMER2);

Chip_TIMER_Init(LPC_TIMER2);
Chip_TIMER_Reset(LPC_TIMER2);

Chip_TIMER_PrescaleSet(LPC_TIMER2, (uint32_t) TIMER2_PRESCALE);

Chip_TIMER_CaptureRisingEdgeEnable(LPC_TIMER2, CAP_1);
Chip_TIMER_ClearCapture(LPC_TIMER2, CAP_1);

Chip_TIMER_CaptureEnableInt(LPC_TIMER2, CAP_1);

Chip_TIMER_MatchEnableInt(LPC_TIMER2, 2);
Chip_TIMER_SetMatch(LPC_TIMER2, 2, DETECT_TIME);
Chip_TIMER_ResetOnMatchEnable(LPC_TIMER2, 2);

Chip_TIMER_Enable(LPC_TIMER2);
NVIC_SetPriority(TIMER2_IRQn, 5);
NVIC_EnableIRQ(TIMER2_IRQn);
}





and these the interrupt routines
void TIMER1_IRQHandler(void) {

if (Chip_TIMER_CapturePending(LPC_TIMER1, CAP_0))   // if a rising edge on CAP1.0 is detected
{
//do something
Chip_TIMER_ClearCapture(LPC_TIMER1, CAP_0);
}

else if (Chip_TIMER_MatchPending(LPC_TIMER1, 1)) {  // if no rising edge is detected for DETECT_TIME
//do something
Chip_TIMER_ClearMatch(LPC_TIMER1, 1);
}
}

void TIMER2_IRQHandler(void)
{
if (Chip_TIMER_CapturePending(LPC_TIMER1, CAP_1))   // if a rising edge on CAP1.1 is detected
{
//do something
Chip_TIMER_ClearCapture(LPC_TIMER1, CAP_1;
}

else if (Chip_TIMER_MatchPending(LPC_TIMER2, 2)) {  // if no rising edge is detected for DETECT_TIME
//do something
Chip_TIMER_ClearMatch(LPC_TIMER2, 2);
}
}


For TIMER1 interrupt we have no problems, in TIMER2 it seems that the input capture doesn't work as the handler is entered only when the macth condition occurs.
We tried to switch the cap pin, but the problem is still present on the TIMER2 Interrupt handler, so we think the problem is in the init, but we can't find where is the error.
Any suggestions?
Labels (1)
0 Kudos
6 Replies

1,191 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by R2D2 on Mon Jan 18 08:02:06 MST 2016

Quote: serge
Does the LPC17xx has Chip_IOCON_PinMux?



LPCOpen has a Chip_IOCON_PinMux function 

/* Setup pin modes and function */
void Chip_IOCON_PinMux(LPC_IOCON_T *pIOCON, uint8_t port, uint8_t pin, uint32_t mode, uint8_t func)
{
uint8_t reg, bitPos;
uint32_t temp;

/* Adjust the mode selection bits from 3:2 to bits 1:0 to write them to PINMODE. */
mode >>= 2;

bitPos =  IOCON_BIT_INDEX(pin);
reg = IOCON_REG_INDEX(port,pin);

temp = pIOCON->PINSEL[reg] & ~(0x03UL << bitPos);
pIOCON->PINSEL[reg] = temp | (func << bitPos);

temp = pIOCON->PINMODE[reg] & ~(0x03UL << bitPos);
pIOCON->PINMODE[reg] = temp | (mode << bitPos);
}
0 Kudos

1,191 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by serge on Mon Jan 18 07:58:48 MST 2016
Does the LPC17xx has Chip_IOCON_PinMux?
0 Kudos

1,191 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by R2D2 on Mon Jan 18 07:57:43 MST 2016

Quote: eplant_lab
we use input capture pin CAP1.0 and CAP1.1.[color=#f00] We use TIMER1 for CAP1.0 and TIMER2 for CAP1.1.[/color]



That's complete nonsense and here your problems are probably starting...
0 Kudos

1,191 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by eplant_lab on Mon Jan 18 06:18:54 MST 2016
thanks for reply!


Quote: MikeSimmonds


1) Timer 1 Init does not have an "Chip_Clock_EnablePeriphClock(SYSCTL_CLOCK_TIMER1);" maybe this is enabled out of reset,
but why rely on that?




1) Right. I will add this line to the project. (But the problem is not on TIMER1)

void TIMER1_Init(void) {

NVIC_DisableIRQ(TIMER1_IRQn);
NVIC_ClearPendingIRQ(TIMER1_IRQn);

Chip_Clock_EnablePeriphClock(SYSCTL_CLOCK_TIMER1);

Chip_TIMER_Init(LPC_TIMER1);
Chip_TIMER_Reset(LPC_TIMER1);

Chip_TIMER_PrescaleSet(LPC_TIMER1, (uint32_t) TIMER1_PRESCALE);

Chip_TIMER_CaptureRisingEdgeEnable(LPC_TIMER1, CAP_0);

Chip_TIMER_CaptureEnableInt(LPC_TIMER1, CAP_0);

Chip_TIMER_MatchEnableInt(LPC_TIMER1, 1);
Chip_TIMER_SetMatch(LPC_TIMER1, 1, ENGINE_OFF_DETECT_TIME_MS);
Chip_TIMER_ResetOnMatchEnable(LPC_TIMER1, 1);

Chip_TIMER_Enable(LPC_TIMER1);
NVIC_SetPriority(TIMER1_IRQn, 5);
NVIC_EnableIRQ(TIMER1_IRQn);

}



Quote: MikeSimmonds


2) In Timer 2 IRQ, the first two calls are using the TIMER ONE equate!




Ops... this is a error I made in " cut and paste " from code editor  :~ , the problem is present with correct equate


void TIMER2_IRQHandler(void) {
if (Chip_TIMER_CapturePending(LPC_TIMER2, 1)) {
//do something
Chip_TIMER_ClearCapture(LPC_TIMER2, CAP_1);
}

else if (Chip_TIMER_MatchPending(LPC_TIMER2, 2))
{
//do something
Chip_TIMER_ClearMatch(LPC_TIMER2, 2);
}
}



The TIMER1 and TIMER2 init are called in this SetupHardware func

/* Sets up system hardware */
static void prvSetupHardware(void)
{
// other init

Chip_GPIO_Init(LPC_GPIO);
Chip_IOCON_Init(LPC_IOCON);

Chip_GPIO_WriteDirBit(LPC_GPIO, 1, 18, false); 
Chip_GPIO_WriteDirBit(LPC_GPIO, 1, 19, false); 
Chip_IOCON_PinMux(LPC_IOCON, 1, 18, IOCON_MODE_INACT , IOCON_FUNC3);
Chip_IOCON_PinMux(LPC_IOCON, 1, 19, IOCON_MODE_INACT , IOCON_FUNC3);


TIMER1_Init();
TIMER2_Init();

}



and this is the main  ( it's a FreeRTOS tipical main )

int main(void)
{
SystemCoreClockUpdate();
prvSetupHardware();
VariableInit();

xTaskCreate(tMain, (signed char *) "Main",StackMain, NULL, 4, (xTaskHandle *) NULL);
xTaskCreate(tTask1, (signed char *) "Task1",StackTask1, NULL, 2, (xTaskHandle *) NULL);
xTaskCreate(tTask2, (signed char *) "Task2",StackTask2, NULL, 2, (xTaskHandle *) NULL);

/* Start the scheduler */
vTaskStartScheduler();

return 1;
}


0 Kudos

1,191 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by R2D2 on Mon Jan 18 05:26:36 MST 2016

Quote: eplant_lab

Any suggestions?



Yes, post a complete project...
0 Kudos

1,191 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by MikeSimmonds on Mon Jan 18 04:54:06 MST 2016
I did not go deep, but ...

1) Timer 1 Init does not have an "Chip_Clock_EnablePeriphClock(SYSCTL_CLOCK_TIMER1);" maybe this is enabled out of reset,
but why rely on that?

2) In Timer 2 IRQ, the first two calls are using the TIMER ONE equate!

HTH, Mike.
0 Kudos