text: /*
   * encoder.c
   *
   *  Created on: 21 Nov 2018
   *      Author: Sylvan Morris
   *      Updated: Stuart Lunt
   */
  
  #include "encoder.h"
  
  void ENCODER_Init(){
  
  	/*
  	 * Init encoder using input capture
  	 * https://gist.github.com/nraynaud/5082298
  	 * https://stm32f4-discovery.net/2014/05/stm32f4-stm32f429-discovery-pwm-tutorial/
  	 * http://micromouseusa.com/?p=392
  	 *
  	 * TIM2_CH1  PA15
  	 * TIM2_CH2  PB3
  	 */
  
  	RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
  	RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
  	RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
  
  	GPIO_InitTypeDef GPIO_InitStructure;
  	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
  	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
  	GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  	GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
  
  	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_15;
  	GPIO_Init(GPIOA, &GPIO_InitStructure);
  
  	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
  	GPIO_Init(GPIOB, &GPIO_InitStructure);
  
  	GPIO_PinAFConfig(GPIOA, GPIO_PinSource15, GPIO_AF_TIM2);
  	GPIO_PinAFConfig(GPIOB, GPIO_PinSource3, GPIO_AF_TIM2);
  
  	TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
  	TIM_TimeBaseStructure.TIM_Period = 0xffffffff;
  	TIM_TimeBaseStructure.TIM_Prescaler = 0;
  	TIM_TimeBaseStructure.TIM_ClockDivision = 0;
  	TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
  
  	TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
  
  	/* Configure the timer */
  	TIM_EncoderInterfaceConfig(TIM2, TIM_EncoderMode_TI12, TIM_ICPolarity_Rising, TIM_ICPolarity_Rising);
  
  	TIM_SetCounter(TIM2, 0x7fffffff); //setting counter to prevent overflow //remember to change in EKF
  
  	/* TIM4 counter enable */
  	TIM_Cmd(TIM2, ENABLE);
  
  }
  