/** @defgroup ir_nikon_timer timer peripheral used to measure signal timing for signal decoding
*@{
*/
#define IR_NIKON_TIMER 4 /**< timer peripheral */
#define IR_NIKON_CHANNEL 3 /**< channel used as input capture */
#define IR_NIKON_JITTER 20 /**< signal timing jitter in % tolerated in timing */
#define IR_NIKON_EXTERNAL_PULLUP true /**< if an external pull-up resistor is already present on the infrared demodulator OUT signal */
/** @} */
volatileboolir_nikon_trigger_flag=false;
/** the mark and space durations (in us) corresponding to the Nikon IR sequence (static, measured from a remote clone) */
constuint16_tir_nikon_sequence[]={2000,28000,400,1580,400,3580,400,63200,2000,28000,400,1580,400,3580,400};// actually there is a 2 pulse trailer, but we skip it to have a faster trigger: 70000, 540, 7200, 580
// setup timer to measure signal timing for bit decoding (use timer channel as input capture)
rcc_periph_clock_enable(RCC_TIM_CH(IR_NIKON_TIMER,IR_NIKON_CHANNEL));// enable clock for GPIO peripheral
rcc_periph_clock_enable(RCC_TIM(IR_NIKON_TIMER));// enable clock for timer peripheral
rcc_periph_clock_enable(RCC_AFIO);// enable clock for alternative functions
#if IR_NIKON_EXTERNAL_PULLUP
gpio_set_mode(TIM_CH_PORT(IR_NIKON_TIMER,IR_NIKON_CHANNEL),GPIO_MODE_INPUT,GPIO_CNF_INPUT_FLOAT,TIM_CH_PIN(IR_NIKON_TIMER,IR_NIKON_CHANNEL));// setup GPIO pin as input
#else
gpio_set(TIM_CH_PORT(IR_NIKON_TIMER,IR_NIKON_CHANNEL),TIM_CH_PIN(IR_NIKON_TIMER,IR_NIKON_CHANNEL));// idle is high (using pull-up resistor)
gpio_set_mode(TIM_CH_PORT(IR_NIKON_TIMER,IR_NIKON_CHANNEL),GPIO_MODE_INPUT,GPIO_CNF_INPUT_PULL_UPDOWN,TIM_CH_PIN(IR_NIKON_TIMER,IR_NIKON_CHANNEL));// setup GPIO pin as input
#endif
timer_reset(TIM(IR_NIKON_TIMER));// reset timer state
timer_set_mode(TIM(IR_NIKON_TIMER),TIM_CR1_CKD_CK_INT,TIM_CR1_CMS_EDGE,TIM_CR1_DIR_UP);// set timer mode, use undivided timer clock,edge alignment (simple count), and count up
// codes are repeated every 110 ms, thus we need to measure at least this duration to detect repeats correctly
// the 16-bit timer is by far precise enough to measure the smallest 560 us burst
timer_set_prescaler(TIM(IR_NIKON_TIMER),(70*(rcc_ahb_frequency/1000)/(1<<16))-1);// set the prescaler so this 16 bits timer overflow after 70 ms (to ignore the second sequence)
timer_ic_set_input(TIM(IR_NIKON_TIMER),TIM_IC(IR_NIKON_CHANNEL),TIM_IC_IN_TI(IR_NIKON_CHANNEL));// configure ICx to use TIn
timer_ic_set_filter(TIM(IR_NIKON_TIMER),TIM_IC(IR_NIKON_CHANNEL),TIM_IC_CK_INT_N_8);// use small filter (noise reduction is more important than timing)
timer_ic_set_polarity(TIM(IR_NIKON_TIMER),TIM_IC(IR_NIKON_CHANNEL),TIM_IC_FALLING);// capture on falling edge (IR bursts are active low on IR demodulators)
timer_ic_set_prescaler(TIM(IR_NIKON_TIMER),TIM_IC(IR_NIKON_CHANNEL),TIM_IC_PSC_OFF);// don't use any prescaler since we want to capture every pulse
timer_clear_flag(TIM(IR_NIKON_TIMER),TIM_SR_UIF);// clear flag
timer_update_on_overflow(TIM(IR_NIKON_TIMER));// only use counter overflow as UEV source (use overflow as start time or timeout)
timer_enable_irq(TIM(IR_NIKON_TIMER),TIM_DIER_UIE);// enable update interrupt for timer
timer_clear_flag(TIM(IR_NIKON_TIMER),TIM_SR_CCIF(IR_NIKON_CHANNEL));// clear input compare flag
timer_ic_enable(TIM(IR_NIKON_TIMER),TIM_IC(IR_NIKON_CHANNEL));// enable capture interrupt only when IR burst