| 1 | void InterruptInit(void)
 | 
| 2 | {
 | 
| 3 |   GPIO_InitTypeDef GPIO_InitStructure;
 | 
| 4 |   TIM_ICInitTypeDef TIM_ICInitStructure;
 | 
| 5 |   NVIC_InitTypeDef NVIC_InitStructure;
 | 
| 6 |   TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
 | 
| 7 | 
 | 
| 8 |   // Clock Enable
 | 
| 9 |   RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOE, ENABLE);
 | 
| 10 |   RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);
 | 
| 11 | 
 | 
| 12 |   // Clock enable
 | 
| 13 |   RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);
 | 
| 14 | 
 | 
| 15 |   // Config PD13 als Digital-Ausgang
 | 
| 16 |   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12 | GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15;
 | 
| 17 |   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
 | 
| 18 |   GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
 | 
| 19 |   GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
 | 
| 20 |   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
 | 
| 21 |   GPIO_Init(GPIOD, &GPIO_InitStructure);
 | 
| 22 | 
 | 
| 23 |   // Config des Pins PE11 als AF-Input
 | 
| 24 |   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
 | 
| 25 |   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
 | 
| 26 |   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
 | 
| 27 |   GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
 | 
| 28 |   GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
 | 
| 29 |   GPIO_Init(GPIOE, &GPIO_InitStructure);
 | 
| 30 | 
 | 
| 31 |   // Connect PE11 with TIM1 for Input Capture
 | 
| 32 |   GPIO_PinAFConfig(GPIOE, GPIO_PinSource11, GPIO_AF_TIM1);
 | 
| 33 | 
 | 
| 34 |   TIM_TimeBaseStructure.TIM_Period = 0xFFFF;
 | 
| 35 |   TIM_TimeBaseStructure.TIM_Prescaler = 0;            
 | 
| 36 |   TIM_TimeBaseStructure.TIM_ClockDivision = 0;
 | 
| 37 |   TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
 | 
| 38 |   TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;
 | 
| 39 |   TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure);
 | 
| 40 | 
 | 
| 41 |   NVIC_InitStructure.NVIC_IRQChannel = TIM1_CC_IRQn;
 | 
| 42 |   NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
 | 
| 43 |   NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
 | 
| 44 |   NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
 | 
| 45 |   NVIC_Init(&NVIC_InitStructure);
 | 
| 46 | 
 | 
| 47 |   NVIC_InitStructure.NVIC_IRQChannel = TIM1_UP_TIM10_IRQn;
 | 
| 48 |   NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
 | 
| 49 |   NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
 | 
| 50 |   NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
 | 
| 51 |   NVIC_Init(&NVIC_InitStructure);
 | 
| 52 | 
 | 
| 53 |   // Channel 2
 | 
| 54 |   TIM_ICInitStructure.TIM_Channel = TIM_Channel_2;
 | 
| 55 |   TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Falling;
 | 
| 56 |   TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI;
 | 
| 57 |   TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;
 | 
| 58 |   TIM_ICInitStructure.TIM_ICFilter = 0x02;        //0x02 = 4 consecutive events are needed to validate a transition on the output
 | 
| 59 |   TIM_ICInit(TIM1, &TIM_ICInitStructure);
 | 
| 60 | 
 | 
| 61 |   //Interrupt Enable
 | 
| 62 |   TIM_ITConfig(TIM1, TIM_IT_CC2, ENABLE);
 | 
| 63 |   TIM_ITConfig(TIM1, TIM_IT_Update, ENABLE);
 | 
| 64 | 
 | 
| 65 |   // Timer enable
 | 
| 66 |   TIM_Cmd(TIM1, ENABLE);
 | 
| 67 | }
 | 
| 68 | 
 | 
| 69 | //-----------------
 | 
| 70 | // TIM1 OVF Handler
 | 
| 71 | //-----------------
 | 
| 72 | void TIM1_UP_TIM10_IRQHandler(void)
 | 
| 73 | {
 | 
| 74 |   if (TIM_GetITStatus(TIM1, TIM_IT_Update) != RESET)
 | 
| 75 |   {
 | 
| 76 |     // Delete Interrupt Flags
 | 
| 77 |     TIM_ClearITPendingBit(TIM1, TIM_IT_Update);
 | 
| 78 | 
 | 
| 79 |     OVF_cnt++;
 | 
| 80 |   }
 | 
| 81 | }
 | 
| 82 | 
 | 
| 83 | //-------------------------------
 | 
| 84 | // TIM1 Capture Interrupt Handler
 | 
| 85 | //-------------------------------
 | 
| 86 | void TIM1_CC_IRQHandler(void)
 | 
| 87 | {
 | 
| 88 |   if (TIM_GetITStatus(TIM1, TIM_IT_CC2) != RESET)
 | 
| 89 |   {
 | 
| 90 |     __disable_irq();
 | 
| 91 | 
 | 
| 92 |     // Delete Interrupt Flags
 | 
| 93 |     TIM_ClearITPendingBit(TIM1, TIM_IT_CC2);
 | 
| 94 | 
 | 
| 95 |     uint16_t new = TIM_GetCapture2(TIM1);
 | 
| 96 |     uint8_t OVF = OVF_cnt;
 | 
| 97 | 
 | 
| 98 |     OVF_cnt = 0;
 | 
| 99 | 
 | 
| 100 |     if(UB_USB_CDC_GetStatus()==USB_CDC_CONNECTED)
 | 
| 101 |     {
 | 
| 102 |         uint16_t time = 0;
 | 
| 103 | 
 | 
| 104 |         if(OVF)
 | 
| 105 |         {
 | 
| 106 |           if(ICRnew > ICRold)
 | 
| 107 |           {
 | 
| 108 |             GPIO_ToggleBits(GPIOD, GPIO_Pin_14);
 | 
| 109 | 
 | 
| 110 |             time = (new - old);
 | 
| 111 |           }
 | 
| 112 |           else
 | 
| 113 |           {
 | 
| 114 |             time = ~(old - new);
 | 
| 115 |             OVF--;       // Periodextra has to be decreased by one because one OVF is already included in the calculation
 | 
| 116 |           }
 | 
| 117 |         }
 | 
| 118 |         else
 | 
| 119 |         {
 | 
| 120 |           time = (new - old);
 | 
| 121 |         }  
 | 
| 122 |     }  
 | 
| 123 |     old = new;
 | 
| 124 |     __enable_irq();
 | 
| 125 |   }
 | 
| 126 | }
 |