it's a pulse back from an ultrasonic sensor from seeedstudio (http://www.seeedstudio.com/depot/datasheet/Seeed%20Ultrasonic%20Sensor%20datasheet.pdf)
basicly i need to emulate the pulseIn() from Wiring, but in a better way. later i might use the interrupt function but i'd settle for just getting it working for now
to use the sensor, first i set the pin to output mode, send a 10us pulse and then switch back to input to read the distance as the pulse width
my code looks like this so far, it's not very friendly but it makes for easy(er) tweaking:
rcc_enable_clk_timer2();
Timer* timer2= (Timer*)TIMER2_BASE;
timer2->CR2 = BIT(7) ;
// set CCMR1 CC1S to 01 (set active input TI1)
/// 01: CC1 channel is configured as input, IC1 is mapped on TI1.
timer2->CCMR1 &= ~BIT(1);
timer2->CCMR1 |= BIT(0);
// set CCMR1 CC2S to 10 (set active input TI1)
/// 10: CC2 channel is configured as input, IC2 is mapped on TI1
timer2->CCMR1 |= BIT(9);
timer2->CCMR1 &= ~BIT(8);
// select the active polarity on TI1FP1. write CC1P to 0 (active on rising edge)
///0: non-inverted: capture is done on a rising edge of IC1.
timer2->CCER &= ~BIT(5);
// select the active polarity on TI1FP2. write CC2P to 1 (active on falling edge)
///1: inverted: capture is done on a falling edge of IC1
timer2->CCER |= BIT(1) ;
//select valid trigger input: write TS to 101 in SMCR (Slave Mode Config Register). meaning we should reset when TI1 triggers
/// 101: Filtered Timer Input 1 (TI1FP1)
timer2->SMCR |= BIT(6) | BIT(4) ;
timer2->SMCR &= ~BIT(5);
// write SMS to 100 in SMCR: slave controller in reset mode
///100: Reset Mode - Rising edge of the selected trigger input (TRGI) reinitializes the counter and generates an update of the registers
timer2->SMCR &= ~BIT(1) ;
//timer2->SMCR &= ~BIT(0);
timer2->SMCR |= BIT(0);
timer2->SMCR |= BIT(2);
timer2->CR1|=BIT(9)&BIT(8);
// enable the CC channels
timer2->CCER |= (BIT0 | BIT4);
// send a pulse
pinMode(pingPin, OUTPUT);
digitalWrite(pingPin, LOW); // ensure it is pulled low
delayMicroseconds(2);
digitalWrite(pingPin, HIGH);
delayMicroseconds(10);
digitalWrite(pingPin, LOW);
delayMicroseconds(2);
pinMode(pingPin, INPUT);
// wait 40 ms, spec say the pulse will never be wider than 35ms if there are no obstacle within 4meters
delay(40);
////
after that we should in theory have the period and duty cycle in timer2->CCR1 and timer2->CCR2. i tried various things over the weekend: enabling the counter and tweaking registers but as bnewbold said, i'm probably missing an initialisation somewhere