/** * @brief TIM configuration * @param None * @retval None */ static void TIM_Config(void) { TIM_MasterConfigTypeDef sMasterConfig; /* Time Base configuration */ htim.Instance = TIMx; htim.Init.Period = 3600; htim.Init.Prescaler = 0; htim.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1; htim.Init.CounterMode = TIM_COUNTERMODE_UP; htim.Init.RepetitionCounter = 0x0; if (HAL_TIM_Base_Init(&htim) != HAL_OK) { /* Timer Initiliazation Error */ Error_Handler(); } /* Timer TRGO selection */ sMasterConfig.MasterOutputTrigger = TIM_TRGO_UPDATE; sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE; if (HAL_TIMEx_MasterConfigSynchronization(&htim, &sMasterConfig) != HAL_OK) { /* Timer TRGO selection Error */ Error_Handler(); } }
/* TIM3 init function */ void SC_TIM3_Init(SERVO_CONTROLLER_Frequency frequency) { TIM_ClockConfigTypeDef sClockSourceConfig; TIM_MasterConfigTypeDef sMasterConfig; TIM_OC_InitTypeDef sConfigOC; htim3.Instance = TIM3; htim3.Init.Prescaler = CORE_FCLK / TIM_FCLK - 1; htim3.Init.CounterMode = TIM_COUNTERMODE_UP; htim3.Init.Period = (uint16_t)(TIM_FCLK / frequency); //should not exceed 0xFFFF htim3.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1; HAL_TIM_Base_Init(&htim3); sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL; HAL_TIM_ConfigClockSource(&htim3, &sClockSourceConfig); HAL_TIM_PWM_Init(&htim3); sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET; sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE; HAL_TIMEx_MasterConfigSynchronization(&htim3, &sMasterConfig); sConfigOC.OCMode = TIM_OCMODE_PWM1; sConfigOC.Pulse = 0; sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH; sConfigOC.OCFastMode = TIM_OCFAST_DISABLE; HAL_TIM_PWM_ConfigChannel(&htim3, &sConfigOC, TIM_CHANNEL_1); HAL_TIM_PWM_ConfigChannel(&htim3, &sConfigOC, TIM_CHANNEL_2); HAL_TIM_PWM_ConfigChannel(&htim3, &sConfigOC, TIM_CHANNEL_3); }
/* TIM2 init function */ void MX_TIM2_Init(void) { TIM_ClockConfigTypeDef sClockSourceConfig; TIM_MasterConfigTypeDef sMasterConfig; htim2.Instance = TIM2; htim2.Init.Prescaler = 1; htim2.Init.CounterMode = TIM_COUNTERMODE_UP; htim2.Init.Period = 875; htim2.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1; htim2.Init.RepetitionCounter = 0; HAL_TIM_Base_Init(&htim2); // Initialize interrupt triggering // HAL_NVIC_EnableIRQ(TIM2_IRQn); sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL; HAL_TIM_ConfigClockSource(&htim2, &sClockSourceConfig); sMasterConfig.MasterOutputTrigger = TIM_TRGO_UPDATE; sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE; HAL_TIMEx_MasterConfigSynchronization(&htim2, &sMasterConfig); if (HAL_TIM_Base_Start_IT(&htim2) != HAL_OK) { while(1); } }
void time2_init(void) { TIM_ClockConfigTypeDef sClockSourceConfig; // GPIO_InitTypeDef GPIO_InitStruct; htim2.Instance = TIM2; htim2.Init.Prescaler = 84 - 1; //1us htim2.Init.CounterMode = TIM_COUNTERMODE_UP; htim2.Init.Period = 1000-1; //10us htim2.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1; htim2.Init.RepetitionCounter = 0; HAL_TIM_Base_Init(&htim2); sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL; HAL_TIM_ConfigClockSource(&htim2, &sClockSourceConfig); #if 0 /* GPIO Ports Clock Enable */ __GPIOC_CLK_ENABLE(); GPIO_InitStruct.Pin = GPIO_PIN_0; GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; GPIO_InitStruct.Speed = GPIO_SPEED_LOW; GPIO_InitStruct.Pull = GPIO_NOPULL; HAL_GPIO_Init(GPIOC, &GPIO_InitStruct); #endif }
/* --- Load and start periodic event counter --- */ void startPerEvent(uint32_t rateFactor) { htim2.Init.Period = rateFactor; HAL_TIM_Base_Init(&htim2); HAL_TIM_Base_Start_IT(&htim2); }
/* TIM2 init function */ static void MX_TIM2_Init(void) { TIM_ClockConfigTypeDef sClockSourceConfig; TIM_MasterConfigTypeDef sMasterConfig; htim2.Instance = TIM2; htim2.Init.Prescaler = 84; htim2.Init.CounterMode = TIM_COUNTERMODE_UP; htim2.Init.Period = 1000000; htim2.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1; if (HAL_TIM_Base_Init(&htim2) != HAL_OK) { Error_Handler(); } sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL; if (HAL_TIM_ConfigClockSource(&htim2, &sClockSourceConfig) != HAL_OK) { Error_Handler(); } sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET; sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE; if (HAL_TIMEx_MasterConfigSynchronization(&htim2, &sMasterConfig) != HAL_OK) { Error_Handler(); } }
void Timer_For_Main_Loop_Init() { __TIM6_CLK_ENABLE(); /* Compute the prescaler value to have TIMx counter clock equal to 1 KHz */ uwPrescalerValue = (uint32_t) ((SystemCoreClock / 1000) - 1); /*##-1- Configure the TIM peripheral #######################################*/ /* Set TIMx instance */ TimHandle.Instance = TIM6; /* Initialize TIMx peripheral as follow: + Period = 10000 - 1 + Prescaler = SystemCoreClock/10000 Note that APB clock = TIMx clock if APB prescaler = 1. + ClockDivision = 0 + Counter direction = Up */ TimHandle.Init.Period = 10000 - 1; TimHandle.Init.Prescaler = uwPrescalerValue; TimHandle.Init.ClockDivision = 0; TimHandle.Init.CounterMode = TIM_COUNTERMODE_UP; HAL_TIM_Base_Init(&TimHandle); /*##-2- Start the TIM Base generation in interrupt mode ####################*/ /* Start Channel1 */ HAL_TIM_Base_Start_IT(&TimHandle); HAL_NVIC_EnableIRQ(TIM6_IRQn); }
/* TIM4 init function */ void MX_TIM4_Init(void) { TIM_SlaveConfigTypeDef sSlaveConfig; TIM_MasterConfigTypeDef sMasterConfig; TIM_OC_InitTypeDef sConfigOC; htim4.Instance = TIM4; htim4.Init.Prescaler = 0; htim4.Init.CounterMode = TIM_COUNTERMODE_UP; htim4.Init.Period = 0; htim4.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1; HAL_TIM_Base_Init(&htim4); HAL_TIM_PWM_Init(&htim4); HAL_TIM_OnePulse_Init(&htim4, TIM_OPMODE_SINGLE); sSlaveConfig.SlaveMode = TIM_SLAVEMODE_EXTERNAL1; sSlaveConfig.InputTrigger = TIM_TS_ITR0; HAL_TIM_SlaveConfigSynchronization(&htim4, &sSlaveConfig); sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET; sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE; HAL_TIMEx_MasterConfigSynchronization(&htim4, &sMasterConfig); sConfigOC.OCMode = TIM_OCMODE_PWM1; sConfigOC.Pulse = 0; sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH; sConfigOC.OCFastMode = TIM_OCFAST_DISABLE; HAL_TIM_PWM_ConfigChannel(&htim4, &sConfigOC, TIM_CHANNEL_1); HAL_TIM_MspPostInit(&htim4); }
/** * @brief Initialization */ void TIMx_BUZZER_Init() { globalBuzzerTick = 0; /* Configure timer */ BuzzerTimHandle.Instance = TIMx_BUZZER; BuzzerTimHandle.Init.Prescaler = 0; // (84 Mhz) BuzzerTimHandle.Init.CounterMode = TIM_COUNTERMODE_UP; BuzzerTimHandle.Init.Period = TIMx_BUZZER_PERIOD - 1; BuzzerTimHandle.Init.ClockDivision = 0; if (HAL_TIM_Base_Init(&BuzzerTimHandle) != HAL_OK) { //Error } GPIO_InitTypeDef GPIO_Init; // Enable peripheral clock BUZZER_CLK_ENABLE(); GPIO_Init.Pin = BUZZER_PIN; GPIO_Init.Mode = GPIO_MODE_OUTPUT_PP; GPIO_Init.Pull = GPIO_PULLUP; GPIO_Init.Speed = GPIO_SPEED_FAST; HAL_GPIO_Init(BUZZER_PORT, &GPIO_Init); HAL_GPIO_WritePin(BUZZER_PORT, BUZZER_PIN, GPIO_PIN_RESET); }
/* TIM3 init function */ void MX_TIM3_Init(void) { TIM_ClockConfigTypeDef sClockSourceConfig; TIM_MasterConfigTypeDef sMasterConfig; TIM_IC_InitTypeDef sConfigIC; htim3.Instance = TIM3; htim3.Init.Prescaler = 71; htim3.Init.CounterMode = TIM_COUNTERMODE_UP; htim3.Init.Period = 0xffff; htim3.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1; HAL_TIM_Base_Init(&htim3); sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL; HAL_TIM_ConfigClockSource(&htim3, &sClockSourceConfig); HAL_TIM_IC_Init(&htim3); sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET; sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE; HAL_TIMEx_MasterConfigSynchronization(&htim3, &sMasterConfig); sConfigIC.ICPolarity = TIM_INPUTCHANNELPOLARITY_FALLING; sConfigIC.ICSelection = TIM_ICSELECTION_DIRECTTI; sConfigIC.ICPrescaler = TIM_ICPSC_DIV1; sConfigIC.ICFilter = 0; HAL_TIM_IC_ConfigChannel(&htim3, &sConfigIC, TIM_CHANNEL_4); }
/** * @brief TIM configuration * @param None * @retval None */ static void TIM_Config(void) { TIM_MasterConfigTypeDef master_timer_config; RCC_ClkInitTypeDef clk_init_struct = {0}; /* Temporary variable to retrieve RCC clock configuration */ uint32_t latency; /* Temporary variable to retrieve Flash Latency */ uint32_t timer_clock_frequency = 0; /* Timer clock frequency */ // uint32_t timer_prescaler = 0; /* Time base prescaler to have timebase aligned on minimum frequency possible */ uint32_t timer_prescaler = 10; /* Configuration of timer as time base: */ /* Caution: Computation of frequency is done for a timer instance on APB1 */ /* (clocked by PCLK1) */ /* Timer period can be adjusted by modifying the following constants: */ /* - TIMER_FREQUENCY: timer frequency (unit: Hz). */ /* - TIMER_FREQUENCY_RANGE_MIN: timer minimum frequency (unit: Hz). */ /* Retrieve timer clock source frequency */ HAL_RCC_GetClockConfig(&clk_init_struct, &latency); /* If APB1 prescaler is different of 1, timers have a factor x2 on their */ /* clock source. */ if (clk_init_struct.APB1CLKDivider == RCC_HCLK_DIV1) { timer_clock_frequency = HAL_RCC_GetPCLK1Freq(); } else { timer_clock_frequency = HAL_RCC_GetPCLK1Freq() *2; } /* Timer prescaler calculation */ /* (computation for timer 16 bits, additional + 1 to round the prescaler up) */ timer_prescaler = (timer_clock_frequency / (TIMER_PRESCALER_MAX_VALUE * TIMER_FREQUENCY_RANGE_MIN)) +1; /* Set timer instance */ TimHandle.Instance = TIMx; /* Configure timer parameters */ TimHandle.Init.Period = ((timer_clock_frequency / (timer_prescaler * TIMER_FREQUENCY)) - 1); TimHandle.Init.Prescaler = (timer_prescaler - 1); TimHandle.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1; TimHandle.Init.CounterMode = TIM_COUNTERMODE_UP; TimHandle.Init.RepetitionCounter = 0x0; if (HAL_TIM_Base_Init(&TimHandle) != HAL_OK) { /* Timer initialization Error */ Error_Handler(); } /* Timer TRGO selection */ master_timer_config.MasterOutputTrigger = TIM_TRGO_UPDATE; master_timer_config.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE; if (HAL_TIMEx_MasterConfigSynchronization(&TimHandle, &master_timer_config) != HAL_OK) { /* Timer TRGO selection Error */ Error_Handler(); } }
void us_ticker_init(void) { if (us_ticker_inited) return; us_ticker_inited = 1; // Enable timer clock TIM_MST_RCC; // Configure time base TimMasterHandle.Instance = TIM_MST; TimMasterHandle.Init.Period = 0xFFFF; TimMasterHandle.Init.Prescaler = (uint32_t)(SystemCoreClock / 1000000) - 1; // 1 �s tick TimMasterHandle.Init.ClockDivision = 0; TimMasterHandle.Init.CounterMode = TIM_COUNTERMODE_UP; HAL_TIM_Base_Init(&TimMasterHandle); // Configure interrupts __HAL_TIM_ENABLE_IT(&TimMasterHandle, TIM_IT_UPDATE); // Update interrupt used for 32-bit counter NVIC_SetVector(TIM_MST_UP_IRQ, (uint32_t)tim_update_irq_handler); NVIC_EnableIRQ(TIM_MST_UP_IRQ); // Output compare interrupt used for timeout feature NVIC_SetVector(TIM_MST_OC_IRQ, (uint32_t)tim_oc_irq_handler); NVIC_EnableIRQ(TIM_MST_OC_IRQ); // Enable timer HAL_TIM_Base_Start(&TimMasterHandle); }
/* TIM9 init function */ void MX_TIM9_Init(void) { TIM_ClockConfigTypeDef sClockSourceConfig; TIM_OC_InitTypeDef sConfigOC; htim9.Instance = TIM9; htim9.Init.Prescaler = 167; htim9.Init.CounterMode = TIM_COUNTERMODE_UP; htim9.Init.Period = 19999; htim9.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1; HAL_TIM_Base_Init(&htim9); sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL; HAL_TIM_ConfigClockSource(&htim9, &sClockSourceConfig); HAL_TIM_PWM_Init(&htim9); sConfigOC.OCMode = TIM_OCMODE_PWM1; sConfigOC.Pulse = 1500; sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH; sConfigOC.OCFastMode = TIM_OCFAST_DISABLE; HAL_TIM_PWM_ConfigChannel(&htim9, &sConfigOC, TIM_CHANNEL_1); HAL_TIM_PWM_ConfigChannel(&htim9, &sConfigOC, TIM_CHANNEL_2); }
/** * @brief TIM_Config: Configure TIMusb timer * @param None. * @retval None */ static void TIM_Config(void) { /* Set TIMusb instance */ TimHandle.Instance = TIMusb; /* Initialize TIMx peripheral as follow: + Period = 10000 - 1 + Prescaler = ((SystemCoreClock/2)/10000) - 1 + ClockDivision = 0 + Counter direction = Up */ TimHandle.Init.Period = (CDC_POLLING_INTERVAL*1000) - 1; TimHandle.Init.Prescaler = 84-1; TimHandle.Init.ClockDivision = 0; TimHandle.Init.CounterMode = TIM_COUNTERMODE_UP; if(HAL_TIM_Base_Init(&TimHandle) != HAL_OK) { /* Initialization Error */ Error_Handler(); } /*##-6- Enable TIM peripherals Clock #######################################*/ TIMx_CLK_ENABLE(); /*##-7- Configure the NVIC for TIMx ########################################*/ /* Set Interrupt Group Priority */ HAL_NVIC_SetPriority(TIMx_IRQn, 6, 0); /* Enable the TIMx global Interrupt */ HAL_NVIC_EnableIRQ(TIMx_IRQn); }
/* TIM2 init function */ void MX_TIM2_Init(void) { TIM_ClockConfigTypeDef sClockSourceConfig; TIM_MasterConfigTypeDef sMasterConfig; TIM_OC_InitTypeDef sConfigOC; htim2.Instance = TIM2; htim2.Init.Prescaler = 0; htim2.Init.CounterMode = TIM_COUNTERMODE_UP; htim2.Init.Period = 47; htim2.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1; HAL_TIM_Base_Init(&htim2); sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL; HAL_TIM_ConfigClockSource(&htim2, &sClockSourceConfig); HAL_TIM_PWM_Init(&htim2); sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET; sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE; HAL_TIMEx_MasterConfigSynchronization(&htim2, &sMasterConfig); sConfigOC.OCMode = TIM_OCMODE_PWM1; sConfigOC.Pulse = 0; sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH; sConfigOC.OCFastMode = TIM_OCFAST_DISABLE; HAL_TIM_PWM_ConfigChannel(&htim2, &sConfigOC, TIM_CHANNEL_1); HAL_TIM_PWM_ConfigChannel(&htim2, &sConfigOC, TIM_CHANNEL_4); HAL_TIM_MspPostInit(&htim2); }
/** * @brief TIM6 Configuration * @note TIM6 configuration is based on APB1 frequency * @note TIM6 Update event occurs each TIM6CLK/256 * @param None * @retval None */ void TIM6_Config(void) { static TIM_HandleTypeDef htim; TIM_MasterConfigTypeDef sMasterConfig; /*##-1- Configure the TIM peripheral #######################################*/ /* Time base configuration */ htim.Instance = TIM6; htim.Init.Period = 0x7FF; htim.Init.Prescaler = 0; htim.Init.ClockDivision = 0; htim.Init.CounterMode = TIM_COUNTERMODE_UP; htim.Init.RepetitionCounter = 0; HAL_TIM_Base_Init(&htim); /* TIM6 TRGO selection */ sMasterConfig.MasterOutputTrigger = TIM_TRGO_UPDATE; sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE; HAL_TIMEx_MasterConfigSynchronization(&htim, &sMasterConfig); /*##-2- Enable TIM peripheral counter ######################################*/ HAL_TIM_Base_Start(&htim); }
void MX_TIM1_Init(void) { TIM_ClockConfigTypeDef sClockSourceConfig; TIM_MasterConfigTypeDef sMasterConfig; TIM_SlaveConfigTypeDef sSlaveConfig; htim2.Instance = TIM2; htim2.Init.Prescaler = 31999; htim2.Init.CounterMode = TIM_COUNTERMODE_UP; htim2.Init.Period = 249; htim2.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1; HAL_TIM_Base_Init(&htim2); sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL; HAL_TIM_ConfigClockSource(&htim2, &sClockSourceConfig); sSlaveConfig.SlaveMode = TIM_SLAVEMODE_TRIGGER; sSlaveConfig.InputTrigger = TIM_TS_TI1FP1; sSlaveConfig.TriggerPolarity = TIM_TRIGGERPOLARITY_RISING; sSlaveConfig.TriggerFilter = 15; HAL_TIM_SlaveConfigSynchronization(&htim2, &sSlaveConfig); sMasterConfig.MasterOutputTrigger = TIM_TRGO_UPDATE; sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_ENABLE; HAL_TIMEx_MasterConfigSynchronization(&htim2, &sMasterConfig); }
/** * @brief Initialize the hardware timer 4. * @param None * @retval None */ void HardwareTimer4_Init(void) { __TIM4_CLK_ENABLE(); /* Enable TIM4 interface clock */ /* Desired_timer_frequency = Timer_input_frequency / (prescaler * period) Timer_input_frequency = 84 MHz (same as TIM3, TIM4 is also on APB1) Desired_timer_frequency = 8 Hz (Rotate the LEDs at that frequency for the visuals) ---> prescaler * period = 84 MHz / 8 Hz = 10500000 = 2100 * 5000 ---> prescaler = 5000, period = 2100 */ timmy4.Instance = TIM4; /* Use TIM4 */ timmy4.Init.Prescaler = 2100; /* Set prescaler to 2100 */ timmy4.Init.CounterMode = TIM_COUNTERMODE_DOWN; /* Count down */ timmy4.Init.Period = 5000; /* Set period count register to 5000 */ timmy4.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1; /* Set clock division to 1 */ timmy4.Init.RepetitionCounter = 0; /* Not valid for TIM4 */ timmy4.Channel = HAL_TIM_ACTIVE_CHANNEL_CLEARED; /* Not using channels */ /* Initialize the TIM Time Base Unit */ HAL_TIM_Base_Init(&timmy4); /* Start TIM3 in interrupt mode */ HAL_TIM_Base_Start_IT(&timmy4); /* Set priority for TIM3 IRQ */ HAL_NVIC_SetPriority(TIM4_IRQn, 0, 0); /* Enable IRQ for the TIM3 Timer*/ HAL_NVIC_EnableIRQ(TIM4_IRQn); }
/* TIM10 init function */ void MX_TIM10_Init(void) { TIM_OC_InitTypeDef sConfigOC; htim10.Instance = TIM10; htim10.Init.Prescaler = 0; htim10.Init.CounterMode = TIM_COUNTERMODE_UP; htim10.Init.Period = 0; htim10.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1; if (HAL_TIM_Base_Init(&htim10) != HAL_OK) { Error_Handler(); } if (HAL_TIM_PWM_Init(&htim10) != HAL_OK) { Error_Handler(); } sConfigOC.OCMode = TIM_OCMODE_PWM1; sConfigOC.Pulse = 0; sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH; sConfigOC.OCFastMode = TIM_OCFAST_DISABLE; if (HAL_TIM_PWM_ConfigChannel(&htim10, &sConfigOC, TIM_CHANNEL_1) != HAL_OK) { Error_Handler(); } HAL_TIM_MspPostInit(&htim10); }
// Driver initialization code and Status Register settings void DRV_Init(void) { // Initiate receiver DRV.RX.htim = &htim2; DRV_RX_SetStatus(DRV_RX_STATUS_RESET); #ifdef MAC_COORDINATOR DRV.RX.SR.RLL = 0; #else DRV.RX.SR.RLL = 1; #endif // Initiate transmitter DRV.TX.htim = &htim3; DRV.TX.Send = DRV_TX_Buffer; DRV_TX_SetStatus(DRV_TX_STATUS_RESET); #ifdef MAC_COORDINATOR DRV.TX.htim->Instance->PSC = 1; DRV.TX.htim->Instance->EGR |= TIM_EGR_UG; DRV.TX.SR.RLL = 1; DRV.TX.SR.Visibility = 1; DRV_TX_SetStatus(DRV_TX_STATUS_VISIBILITY); #else DRV.TX.htim->Instance->PSC = 19; DRV.TX.htim->Instance->EGR |= TIM_EGR_UG; DRV.TX.SR.RLL = 0; DRV.TX.SR.Visibility = 0; #endif // Initialize IR output TIM4->CCR1 = 2210; HAL_TIM_Base_Init(&htim4); HAL_TIM_PWM_Start(&htim4, TIM_CHANNEL_1); }
/* TIM17 init function */ void MX_TIM17_Init(void) { TIM_BreakDeadTimeConfigTypeDef sBreakDeadTimeConfig; TIM_OC_InitTypeDef sConfigOC; htim17.Instance = TIM17; htim17.Init.Prescaler = 0; htim17.Init.CounterMode = TIM_COUNTERMODE_UP; htim17.Init.Period = 0; htim17.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1; htim17.Init.RepetitionCounter = 0; HAL_TIM_Base_Init(&htim17); HAL_TIM_OC_Init(&htim17); sBreakDeadTimeConfig.OffStateRunMode = TIM_OSSR_DISABLE; sBreakDeadTimeConfig.OffStateIDLEMode = TIM_OSSI_DISABLE; sBreakDeadTimeConfig.LockLevel = TIM_LOCKLEVEL_OFF; sBreakDeadTimeConfig.DeadTime = 0; sBreakDeadTimeConfig.BreakState = TIM_BREAK_DISABLE; sBreakDeadTimeConfig.BreakPolarity = TIM_BREAKPOLARITY_HIGH; sBreakDeadTimeConfig.AutomaticOutput = TIM_AUTOMATICOUTPUT_DISABLE; HAL_TIMEx_ConfigBreakDeadTime(&htim17, &sBreakDeadTimeConfig); sConfigOC.OCMode = TIM_OCMODE_TIMING; sConfigOC.Pulse = 0; sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH; sConfigOC.OCNPolarity = TIM_OCNPOLARITY_HIGH; sConfigOC.OCFastMode = TIM_OCFAST_DISABLE; sConfigOC.OCIdleState = TIM_OCIDLESTATE_RESET; sConfigOC.OCNIdleState = TIM_OCNIDLESTATE_RESET; HAL_TIM_OC_ConfigChannel(&htim17, &sConfigOC, TIM_CHANNEL_1); }
void Cpwm::Timer_PWM_Init(TIM_TypeDef* TIMx, UInt32 Tim_OCMODE_PWMx) { /*define and init TIMx!*/ mPWM_Struct.Instance=TIMx; mPWM_Struct.Init.Prescaler=mPrescaler; mPWM_Struct.Init.CounterMode=TIM_COUNTERMODE_UP; mPWM_Struct.Init.Period=mPeriod; mPWM_Struct.Init.ClockDivision=TIM_CLOCKDIVISION_DIV1; mPWM_Struct.Init.RepetitionCounter=0; HAL_TIM_Base_Init(&mPWM_Struct); HAL_TIM_PWM_Init(&mPWM_Struct); /*define PWM output!*/ mStrOutputChannel.OCMode=Tim_OCMODE_PWMx; mStrOutputChannel.OCIdleState=TIM_OCIDLESTATE_SET; mStrOutputChannel.OCNIdleState=TIM_OCNIDLESTATE_RESET; mStrOutputChannel.OCPolarity=TIM_OCPOLARITY_HIGH; mStrOutputChannel.OCNPolarity=TIM_OCPOLARITY_HIGH; mStrOutputChannel.OCFastMode=TIM_OCFAST_DISABLE; mStrOutputChannel.Pulse = 0; HAL_TIM_PWM_ConfigChannel(&mPWM_Struct,&mStrOutputChannel,mTIM_CHANNEL_x); /*start pwm*/ HAL_TIM_PWM_Start(&mPWM_Struct,mTIM_CHANNEL_x); }
void CAM_TIM_init(CLOCKSPEED speed) { /* * Enable TIM2 clock */ __TIM2_CLK_ENABLE(); /* Variables */ uint32_t prescaler, period; TIM_MasterConfigTypeDef sMasterConfig; /* * Workout TIM parameters based on given clock speed */ if (speed == FAST) { prescaler = 84; period = 8; } else { prescaler = 84; period = 12; } /* * Time Base configuration and initialization */ htim.Instance = TIM2; htim.Init.Period = period - 1; htim.Init.Prescaler = prescaler - 1; htim.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1; htim.Init.CounterMode = TIM_COUNTERMODE_UP; htim.Init.RepetitionCounter = 0x0; HAL_TIM_Base_Init(&htim); /* * Configure TIM2 to trig ADC using the TRG0 internal trigger */ sMasterConfig.MasterOutputTrigger = TIM_TRGO_UPDATE; sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE; HAL_TIMEx_MasterConfigSynchronization(&htim, &sMasterConfig); /* * Initialize PWM */ HAL_TIM_PWM_Init(&htim); /* * Configure PWM channel */ TIM_OC_InitTypeDef sPWMConfig; sPWMConfig.OCMode = TIM_OCMODE_PWM1; sPWMConfig.Pulse = period / 2; sPWMConfig.OCPolarity = TIM_OCPOLARITY_LOW; sPWMConfig.OCFastMode = TIM_OCFAST_DISABLE; HAL_TIM_PWM_ConfigChannel(&htim, &sPWMConfig, TIM_CHANNEL_2); }
/** * @brief This function configures the TIM6 as a time base source. * The time source is configured to have 1ms time base with a dedicated * Tick interrupt priority. * @note This function is called automatically at the beginning of program after * reset by HAL_Init() or at any time when clock is configured, by HAL_RCC_ClockConfig(). * @param TickPriority Tick interrupt priority. * @retval HAL status */ HAL_StatusTypeDef HAL_InitTick (uint32_t TickPriority) { RCC_ClkInitTypeDef clkconfig; uint32_t uwTimclock, uwAPB1Prescaler = 0U; uint32_t uwPrescalerValue = 0U; uint32_t pFLatency; /*Configure the TIM6 IRQ priority */ HAL_NVIC_SetPriority(TIM6_DAC_IRQn, TickPriority ,0U); /* Enable the TIM6 global Interrupt */ HAL_NVIC_EnableIRQ(TIM6_DAC_IRQn); /* Enable TIM6 clock */ __HAL_RCC_TIM6_CLK_ENABLE(); /* Get clock configuration */ HAL_RCC_GetClockConfig(&clkconfig, &pFLatency); /* Get APB1 prescaler */ uwAPB1Prescaler = clkconfig.APB1CLKDivider; /* Compute TIM6 clock */ if (uwAPB1Prescaler == RCC_HCLK_DIV1) { uwTimclock = HAL_RCC_GetPCLK1Freq(); } else { uwTimclock = 2*HAL_RCC_GetPCLK1Freq(); } /* Compute the prescaler value to have TIM6 counter clock equal to 1MHz */ uwPrescalerValue = (uint32_t) ((uwTimclock / 1000000U) - 1U); /* Initialize TIM6 */ TimHandle.Instance = TIM6; /* Initialize TIMx peripheral as follow: + Period = [(TIM6CLK/1000) - 1]. to have a (1/1000) s time base. + Prescaler = (uwTimclock/1000000 - 1) to have a 1MHz counter clock. + ClockDivision = 0 + Counter direction = Up */ TimHandle.Init.Period = (1000000U / 1000U) - 1U; TimHandle.Init.Prescaler = uwPrescalerValue; TimHandle.Init.ClockDivision = 0; TimHandle.Init.CounterMode = TIM_COUNTERMODE_UP; TimHandle.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE; if(HAL_TIM_Base_Init(&TimHandle) == HAL_OK) { /* Start the TIM time Base generation in interrupt mode */ return HAL_TIM_Base_Start_IT(&TimHandle); } /* Return function status */ return HAL_ERROR; }
/** * @brief This function configures the TIM6 as a time base source. * The time source is configured to have 1ms time base with a dedicated * Tick interrupt priority. * @note This function is called automatically at the beginning of program after * reset by HAL_Init() or at any time when clock is configured, by HAL_RCC_ClockConfig(). * @param TickPriority: Tick interrupt priority. * @retval HAL status */ HAL_StatusTypeDef HAL_InitTick (uint32_t TickPriority) { RCC_ClkInitTypeDef sClokConfig; uint32_t uwTimclock, uwAPB1Prescaler = 0; uint32_t uwPrescalerValue = 0; uint32_t pFLatency; /*Configure the TIM6 IRQ priority */ HAL_NVIC_SetPriority(TIM6_DAC_IRQn, TickPriority ,0); /* Get clock configuration */ HAL_RCC_GetClockConfig(&sClokConfig, &pFLatency); /* Get APB1 prescaler */ uwAPB1Prescaler = sClokConfig.APB1CLKDivider; /* Compute TIM6 clock */ if (uwAPB1Prescaler == 0) { uwTimclock = HAL_RCC_GetPCLK1Freq(); } else { uwTimclock = 2*HAL_RCC_GetPCLK1Freq(); } /* Compute the prescaler value to have TIM6 counter clock equal to 1MHz */ uwPrescalerValue = (uint32_t) ((uwTimclock / 1000000) - 1); /* Initialize TIM6 */ TimHandle.Instance = TIM6; /* Initialize TIMx peripheral as follow: + Period = [(TIM6CLK/1000) - 1]. to have a (1/1000) s time base. + Prescaler = (uwTimclock/1000000 - 1) to have a 1MHz counter clock. + ClockDivision = 0 + Counter direction = Up */ TimHandle.Init.Period = (1000000 / 1000) - 1; TimHandle.Init.Prescaler = uwPrescalerValue; TimHandle.Init.ClockDivision = 0; TimHandle.Init.CounterMode = TIM_COUNTERMODE_UP; if(HAL_TIM_Base_Init(&TimHandle) != HAL_OK) { /* Initialization Error */ ErrorHandler(); } /* Start the TIM time Base generation in interrupt mode */ if(HAL_TIM_Base_Start_IT(&TimHandle) != HAL_OK) { /* Starting Error */ ErrorHandler(); } /* Return function status */ return HAL_OK; }
// Reconfigure the HAL tick using a standard timer instead of systick. HAL_StatusTypeDef HAL_InitTick(uint32_t TickPriority) { // Enable timer clock TIM_MST_RCC; // Reset timer TIM_MST_RESET_ON; TIM_MST_RESET_OFF; // Update the SystemCoreClock variable SystemCoreClockUpdate(); // Configure time base TimMasterHandle.Instance = TIM_MST; TimMasterHandle.Init.Period = 0xFFFF; TimMasterHandle.Init.Prescaler = (uint32_t)(SystemCoreClock / 1000000) - 1; // 1 us tick TimMasterHandle.Init.ClockDivision = 0; TimMasterHandle.Init.CounterMode = TIM_COUNTERMODE_UP; HAL_TIM_Base_Init(&TimMasterHandle); // Configure output compare channel 1 for mbed timeout (enabled later when used) HAL_TIM_OC_Start(&TimMasterHandle, TIM_CHANNEL_1); // Configure output compare channel 2 for HAL tick HAL_TIM_OC_Start(&TimMasterHandle, TIM_CHANNEL_2); PreviousVal = __HAL_TIM_GetCounter(&TimMasterHandle); __HAL_TIM_SetCompare(&TimMasterHandle, TIM_CHANNEL_2, PreviousVal + HAL_TICK_DELAY); // Configure interrupts // Update interrupt used for 32-bit counter // Output compare channel 1 interrupt for mbed timeout // Output compare channel 2 interrupt for HAL tick NVIC_SetVector(TIM_MST_UP_IRQ, (uint32_t)timer_update_irq_handler); NVIC_EnableIRQ(TIM_MST_UP_IRQ); NVIC_SetPriority(TIM_MST_UP_IRQ, 0); NVIC_SetVector(TIM_MST_OC_IRQ, (uint32_t)timer_oc_irq_handler); NVIC_EnableIRQ(TIM_MST_OC_IRQ); NVIC_SetPriority(TIM_MST_OC_IRQ, 1); // Enable interrupts __HAL_TIM_ENABLE_IT(&TimMasterHandle, TIM_IT_UPDATE); // For 32-bit counter __HAL_TIM_ENABLE_IT(&TimMasterHandle, TIM_IT_CC2); // For HAL tick // Enable timer HAL_TIM_Base_Start(&TimMasterHandle); #if 0 // For DEBUG only __GPIOB_CLK_ENABLE(); GPIO_InitTypeDef GPIO_InitStruct; GPIO_InitStruct.Pin = GPIO_PIN_6; GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; GPIO_InitStruct.Pull = GPIO_PULLUP; GPIO_InitStruct.Speed = GPIO_SPEED_HIGH; HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); #endif return HAL_OK; }
void usbKB_init(void) { TimHandle.Instance = TIM2; TimHandle.Init.ClockDivision = 0; TimHandle.Init.CounterMode = TIM_COUNTERMODE_UP; TimHandle.Init.Period = 1000 - 1; TimHandle.Init.Prescaler = 72000 - 1; TimHandle.Init.RepetitionCounter = 0; HAL_TIM_Base_Init(&TimHandle); }
/** * @brief 定时器初始化函数 * @param None * @retval None */ BOOL xMBPortTimersInit( USHORT usTim1Timerout50us ) { /*##-1- Configure the TIM peripheral #######################################*/ /* ----------------------------------------------------------------------- In this example TIM3 input clock (TIM3CLK) is set to APB1 clock (PCLK1), since APB1 prescaler is equal to 1. TIM3CLK = PCLK1 PCLK1 = HCLK => TIM3CLK = HCLK = SystemCoreClock To get TIM3 counter clock at 10 KHz, the Prescaler is computed as following: Prescaler = (TIM3CLK / TIM3 counter clock) - 1 Prescaler = (SystemCoreClock /10 KHz) - 1 Note: SystemCoreClock variable holds HCLK frequency and is defined in system_stm32l1xx.c file. Each time the core clock (HCLK) changes, user had to update SystemCoreClock variable value. Otherwise, any configuration based on this variable will be incorrect. This variable is updated in three ways: 1) by calling CMSIS function SystemCoreClockUpdate() 2) by calling HAL API function HAL_RCC_GetSysClockFreq() 3) each time HAL_RCC_ClockConfig() is called to configure the system clock frequency ----------------------------------------------------------------------- */ /* Compute the prescaler value to have TIMx counter clock equal to 10000 Hz */ uwPrescalerValue = (uint32_t)(SystemCoreClock / 20000) - 1; /* Set TIMx instance */ TimHandle.Instance = TIMx; /* Initialize TIMx peripheral as follows: + Period = 10000 - 1 + Prescaler = (SystemCoreClock/10000) - 1 + ClockDivision = 0 + Counter direction = Up */ TimHandle.Init.Period = (usTim1Timerout50us * 20) - 1; TimHandle.Init.Prescaler = uwPrescalerValue; TimHandle.Init.ClockDivision = 0; TimHandle.Init.CounterMode = TIM_COUNTERMODE_UP; if (HAL_TIM_Base_Init(&TimHandle) != HAL_OK) { /* Initialization Error */ Error_Handler(); } /*##-2- Start the TIM Base generation in interrupt mode ####################*/ /* Start Channel1 */ if (HAL_TIM_Base_Start_IT(&TimHandle) != HAL_OK) { /* Starting Error */ Error_Handler(); } return TRUE; }
/* TIM1 init function */ void MX_TIM1_Init(void) { TIM_ClockConfigTypeDef sClockSourceConfig; TIM_MasterConfigTypeDef sMasterConfig; TIM_BreakDeadTimeConfigTypeDef sBreakDeadTimeConfig; TIM_OC_InitTypeDef sConfigOC; htim1.Instance = TIM1; htim1.Init.Prescaler = 0; htim1.Init.CounterMode = TIM_COUNTERMODE_UP; htim1.Init.Period = 0; htim1.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1; htim1.Init.RepetitionCounter = 0; HAL_TIM_Base_Init(&htim1); sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL; HAL_TIM_ConfigClockSource(&htim1, &sClockSourceConfig); HAL_TIM_OC_Init(&htim1); HAL_TIM_PWM_Init(&htim1); sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET; sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE; HAL_TIMEx_MasterConfigSynchronization(&htim1, &sMasterConfig); sBreakDeadTimeConfig.OffStateRunMode = TIM_OSSR_DISABLE; sBreakDeadTimeConfig.OffStateIDLEMode = TIM_OSSI_DISABLE; sBreakDeadTimeConfig.LockLevel = TIM_LOCKLEVEL_OFF; sBreakDeadTimeConfig.DeadTime = 0; sBreakDeadTimeConfig.BreakState = TIM_BREAK_DISABLE; sBreakDeadTimeConfig.BreakPolarity = TIM_BREAKPOLARITY_HIGH; sBreakDeadTimeConfig.AutomaticOutput = TIM_AUTOMATICOUTPUT_DISABLE; HAL_TIMEx_ConfigBreakDeadTime(&htim1, &sBreakDeadTimeConfig); sConfigOC.OCMode = TIM_OCMODE_TIMING; sConfigOC.Pulse = 0; sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH; sConfigOC.OCNPolarity = TIM_OCNPOLARITY_HIGH; sConfigOC.OCFastMode = TIM_OCFAST_DISABLE; sConfigOC.OCIdleState = TIM_OCIDLESTATE_RESET; sConfigOC.OCNIdleState = TIM_OCNIDLESTATE_RESET; HAL_TIM_OC_ConfigChannel(&htim1, &sConfigOC, TIM_CHANNEL_1); HAL_TIM_OC_ConfigChannel(&htim1, &sConfigOC, TIM_CHANNEL_2); HAL_TIM_OC_ConfigChannel(&htim1, &sConfigOC, TIM_CHANNEL_3); sConfigOC.OCMode = TIM_OCMODE_PWM1; HAL_TIM_PWM_ConfigChannel(&htim1, &sConfigOC, TIM_CHANNEL_4); HAL_TIM_MspPostInit(&htim1); }
void Timer_Init(void) { #ifndef _MS_VS __TIM6_CLK_ENABLE(); // Для отсчёта миллисекунд __TIM2_CLK_ENABLE(); // Для тиков __TIM5_CLK_ENABLE(); #endif HAL_TIM_Base_Init((TIM_HandleTypeDef*)&handleTIM6); HAL_TIM_Base_Start_IT((TIM_HandleTypeDef*)&handleTIM6); TIM_HandleTypeDef handleTIM2 = { TIM2, { 0, TIM_COUNTERMODE_UP, 0xffffffff, TIM_CLOCKDIVISION_DIV4 } }; HAL_TIM_Base_Init((TIM_HandleTypeDef*)&handleTIM2); HAL_TIM_Base_Start((TIM_HandleTypeDef*)&handleTIM2); TIM_HandleTypeDef handleTIM5 = { TIM5, { 8999, // WARN Так и не разобрался, как настроить таймер на 1мс. При 8999 период счета - 10мс. TIM_COUNTERMODE_UP, 0xffffffff, TIM_CLOCKDIVISION_DIV1 } }; HAL_TIM_Base_Init((TIM_HandleTypeDef*)&handleTIM5); HAL_TIM_Base_Start((TIM_HandleTypeDef*)&handleTIM5); }