1 | static GPIO_InitTypeDef port_cfg_a2 = {
|
2 | .Pin = GPIO_PIN_2,
|
3 | .Mode = GPIO_MODE_AF_PP,
|
4 | .Speed = GPIO_SPEED_FREQ_HIGH,
|
5 | .Pull = GPIO_PULLUP,
|
6 | .Alternate = GPIO_AF1_TIM2,
|
7 |
|
8 | static TIM_HandleTypeDef timer_handle = {
|
9 | .Instance = TIM2,
|
10 | .Init.Prescaler = 0,
|
11 | .Init.CounterMode = TIM_COUNTERMODE_UP,
|
12 | .Init.Period = 0xffff,
|
13 | .Init.ClockDivision = TIM_CLOCKDIVISION_DIV1,
|
14 | .Init.RepetitionCounter = 0,
|
15 | };
|
16 |
|
17 | static TIM_IC_InitTypeDef input_capture_init = {
|
18 | .ICPrescaler = TIM_ICPSC_DIV1,
|
19 | .ICFilter = 0,
|
20 | .ICPolarity = TIM_ICPOLARITY_FALLING,
|
21 | .ICSelection = TIM_ICSELECTION_INDIRECTTI,
|
22 | };
|
23 |
|
24 | static TIM_SlaveConfigTypeDef slave_config = {
|
25 | .SlaveMode = TIM_SLAVEMODE_RESET,
|
26 | .InputTrigger = TIM_TS_TI2FP2,
|
27 | };
|
28 |
|
29 | int main(void)
|
30 | {
|
31 | __HAL_RCC_TIM2_CLK_ENABLE();
|
32 | __HAL_RCC_GPIOA_CLK_ENABLE();
|
33 |
|
34 | HAL_GPIO_Init(GPIOA, port_cfg_a2);
|
35 |
|
36 | HAL_NVIC_SetPriority(TIM2_IRQn, 0, 1);
|
37 |
|
38 | HAL_TIM_IC_Init(&timer_handle);
|
39 |
|
40 | HAL_TIM_IC_ConfigChannel(&timer_handle, &input_capture_init, TIM_CHANNEL_1);
|
41 | input_capture_init.ICPolarity = TIM_ICPOLARITY_RISING;
|
42 | input_capture_init.ICSelection = TIM_ICSELECTION_DIRECTTI;
|
43 | HAL_TIM_IC_ConfigChannel(&timer_handle, &input_capture_init, TIM_CHANNEL_2);
|
44 |
|
45 | HAL_TIM_SlaveConfigSynchronization(&timer_handle, &slave_config);
|
46 | HAL_TIM_IC_Start_IT(&timer_handle, TIM_CHANNEL_1);
|
47 | HAL_TIM_IC_Start_IT(&timer_handle, TIM_CHANNEL_2);
|
48 |
|
49 | HAL_NVIC_EnableIRQ(TIM2_IRQn);
|
50 |
|
51 | while(1);
|
52 | }
|
53 |
|
54 | void TIM2_IRQHandler(void)
|
55 | {
|
56 | TIM_HandleTypeDef *htim = &timer_handle;
|
57 | uint32_t tmpreg = 0U;
|
58 |
|
59 | if(__HAL_TIM_GET_FLAG(htim, TIM_FLAG_CC1) != RESET) {
|
60 | if(__HAL_TIM_GET_IT_SOURCE(htim, TIM_IT_CC1) !=RESET) {
|
61 | __HAL_TIM_CLEAR_IT(htim, TIM_IT_CC1);
|
62 | if((htim->Instance->CCMR1 & TIM_CCMR1_CC1S) != 0x00U) {
|
63 | tmpreg = htim->Instance->CCR1;
|
64 | }
|
65 | }
|
66 | }
|
67 | if(__HAL_TIM_GET_FLAG(htim, TIM_FLAG_CC2) != RESET) {
|
68 | if(__HAL_TIM_GET_IT_SOURCE(htim, TIM_IT_CC2) !=RESET) {
|
69 | __HAL_TIM_CLEAR_IT(htim, TIM_IT_CC2);
|
70 | if((htim->Instance->CCMR1 & TIM_CCMR1_CC2S) != 0x00U) {
|
71 | tmpreg = htim->Instance->CCR2;
|
72 | }
|
73 | }
|
74 | }
|
75 | if(__HAL_TIM_GET_FLAG(htim, TIM_FLAG_CC3) != RESET) {
|
76 | if(__HAL_TIM_GET_IT_SOURCE(htim, TIM_IT_CC3) !=RESET) {
|
77 | __HAL_TIM_CLEAR_IT(htim, TIM_IT_CC3);
|
78 | if((htim->Instance->CCMR2 & TIM_CCMR2_CC3S) != 0x00U) {
|
79 | tmpreg = htim->Instance->CCR3;
|
80 | }
|
81 | }
|
82 | }
|
83 | if(__HAL_TIM_GET_FLAG(htim, TIM_FLAG_CC4) != RESET) {
|
84 | if(__HAL_TIM_GET_IT_SOURCE(htim, TIM_IT_CC4) !=RESET) {
|
85 | __HAL_TIM_CLEAR_IT(htim, TIM_IT_CC4);
|
86 | if((htim->Instance->CCMR2 & TIM_CCMR2_CC4S) != 0x00U) {
|
87 | tmpreg = htim->Instance->CCR4;
|
88 | }
|
89 | }
|
90 | }
|
91 | }
|