Hi,I am trying to get input capture working but fails. Pin 5 is connected to the pps output of a gps. I have verified that I can read the pulse using digitalRead() so I know the pulse is there and the Due can measure it. I just cant get the input capture to work.I have followed the "Timer interrupts on Due" thread and read the data sheet and some but not all relevant source code in the arduino 1.5 package.

This is the code I am running, based on the code examples in the other thread. I tried to comment it to explain what it does.

Quote

/* trying to get input capture working for Arduion Due with GPS PPS signal connected to pin 5. Based on the information in this discussion thread: http://arduino.cc/forum/index.php?topic=130423.0 It does not work, for some reason! by Paul Dreik http://www.pauldreik.se/ */volatile boolean l;

//we need to configure the pin to be controlled by the right peripheral.//pin 5 is port C. PIOC_PDR is defined in hardware/arduino/sam/system/CMSIS/Device/ATMEL/sam3xa/include/instance/instance_pioc.h//and PIO_PDR_P25 is defined in hardware/arduino/sam/system/CMSIS/Device/ATMEL/sam3xa/include/component/component_pio.h //this disables the pio from controlling the pin. see 32.7.2

REG_PIOC_PDR |= PIO_PDR_P25;

//next thing is to assign the io line to the peripheral. See 32.7.24.//we need to know which peripheral we should use. Read table 37-4 in section 37.5.1.//TIOA6 is peripheral B, so we want to set that bit to 1.//REG_PIOC_ABSR is defined in hardware/arduino/sam/system/CMSIS/Device/ATMEL/sam3xa/include/instance/instance_pioc.h//PIO_ABSR_P25 is defined in hardware/arduino/sam/system/CMSIS/Device/ATMEL/sam3xa/include/component/component_pio.h REG_PIOC_ABSR |= PIO_ABSR_P25;

//allow configuring the clock. pmc_set_writeprotect(false);

/* Every peripheral in the SAM3X is off by default (to save power) and should be turned on. */ pmc_enable_periph_clk(ID_TC6);

/* configure the timer. All this is about setting TC_CMRx, see 37.7.10 in atmel pdf. CLOCK1 is 42 MHz. We want input capture. Nothing else should be necessary. */ TC_Configure(tc, channel, TC_CMR_TCCLKS_TIMER_CLOCK1 | TC_CMR_LDRA_RISING);

As you see, there is an LDRA interrupt just at start and the overflow interrupt works as it should, but I would expect to get the LDRA interrupt (PPS) once per second.Does anyone know what is going on?

RA is loaded only if it has not been loaded since the last trigger or if RB has been loaded sincethe last loading of RA.

So I updated the code to also capture TIOA6 falling in RB. The problem is now solved.

The updated code, with more comments on the relevant parts:

Quote

/* Input capture working for Arduino Due with GPS PPS signal connected to pin 5. Based on the information in this discussion thread: http://arduino.cc/forum/index.php?topic=130423.0 by Paul Dreik http://www.pauldreik.se/ */volatile boolean l;

//read LDRA. If we dont, we will get overflow (TC_SR_LOVRS)if(inputcaptureA) {const uint32_t ra= TC2->TC_CHANNEL[0].TC_RA;Serial.print(" ra=");Serial.print(ra); }//read LDRB. If we dont, we will get overflow (TC_SR_LOVRS)if(inputcaptureB) {const uint32_t rb= TC2->TC_CHANNEL[0].TC_RB;Serial.print(" rb=");Serial.print(rb); }

//we need to configure the pin to be controlled by the right peripheral.//pin 5 is port C. PIOC_PDR is defined in hardware/arduino/sam/system/CMSIS/Device/ATMEL/sam3xa/include/instance/instance_pioc.h//and PIO_PDR_P25 is defined in hardware/arduino/sam/system/CMSIS/Device/ATMEL/sam3xa/include/component/component_pio.h //this disables the pio from controlling the pin. see 32.7.2

REG_PIOC_PDR |= PIO_PDR_P25;

//next thing is to assign the io line to the peripheral. See 32.7.24.//we need to know which peripheral we should use. Read table 37-4 in section 37.5.1.//TIOA6 is peripheral B, so we want to set that bit to 1.//REG_PIOC_ABSR is defined in hardware/arduino/sam/system/CMSIS/Device/ATMEL/sam3xa/include/instance/instance_pioc.h//PIO_ABSR_P25 is defined in hardware/arduino/sam/system/CMSIS/Device/ATMEL/sam3xa/include/component/component_pio.h REG_PIOC_ABSR |= PIO_ABSR_P25;

//allow configuring the clock. pmc_set_writeprotect(false);

/* Every peripheral in the SAM3X is off by default (to save power) and should be turned on. */ pmc_enable_periph_clk(ID_TC6);

/* configure the timer. All this is about setting TC_CMRx, see 37.7.10 in atmel pdf. We use CLOCK1 at 42 MHz to get the best possible resolution. We want input capture on TIOA6 (pin 5). Nothing else should be necessary, BUT there is a caveat: As mentioned in 37.6.8, we only get the value loaded in RA if not loaded since the last trigger, or RB has been loaded. Since I do not want to trigger as that sets the timer value to 0, I instead let register B be loaded when the pulse is going low. */ TC_Configure(tc, channel, TC_CMR_TCCLKS_TIMER_CLOCK1 | TC_CMR_LDRA_RISING | TC_CMR_LDRB_FALLING);