void lp_ticker_init(void) { if (!lp_ticker_inited) { lp_ticker_inited = 1; __TIM2_CLK_ENABLE(); __TIM2_FORCE_RESET(); __TIM2_RELEASE_RESET(); // Update the SystemCoreClock variable SystemCoreClockUpdate(); // Configure time base TimMasterHandle.Instance = TIM2; TimMasterHandle.Init.Period = 0xFFFFFFFF; TimMasterHandle.Init.Prescaler = (uint32_t)(SystemCoreClock / 1000000000) - 1; // 1 ms tick TimMasterHandle.Init.ClockDivision = 0; TimMasterHandle.Init.CounterMode = TIM_COUNTERMODE_UP; TimMasterHandle.Init.RepetitionCounter = 0; HAL_TIM_OC_Init(&TimMasterHandle); vIRQ_SetVector(TIM2_IRQn, (uint32_t)lp_handler); vIRQ_EnableIRQ(TIM2_IRQn); HAL_TIM_OC_Start(&TimMasterHandle, TIM_CHANNEL_1); } }
void HAL_TIM_Base_MspInit(TIM_HandleTypeDef* htim_base) { GPIO_InitTypeDef GPIO_InitStruct; if(htim_base->Instance==TIM2) { /* USER CODE BEGIN TIM2_MspInit 0 */ /* USER CODE END TIM2_MspInit 0 */ /* Peripheral clock enable */ __TIM2_CLK_ENABLE(); /**TIM2 GPIO Configuration PA0 ------> TIM2_CH1 PA1 ------> TIM2_CH2 PA2 ------> TIM2_CH3 PA3 ------> TIM2_CH4 */ GPIO_InitStruct.Pin = GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3; GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; GPIO_InitStruct.Pull = GPIO_NOPULL; GPIO_InitStruct.Speed = GPIO_SPEED_LOW; GPIO_InitStruct.Alternate = GPIO_AF1_TIM2; HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); /* USER CODE BEGIN TIM2_MspInit 1 */ /* USER CODE END TIM2_MspInit 1 */ } }
void pwmout_init(pwmout_t* obj, PinName pin) { // Get the peripheral name from the pin and assign it to the object obj->pwm = (PWMName)pinmap_peripheral(pin, PinMap_PWM); if (obj->pwm == (PWMName)NC) { error("PWM error: pinout mapping failed."); } // Enable TIM clock if (obj->pwm == PWM_1) __TIM1_CLK_ENABLE(); if (obj->pwm == PWM_2) __TIM2_CLK_ENABLE(); if (obj->pwm == PWM_3) __TIM3_CLK_ENABLE(); if (obj->pwm == PWM_4) __TIM4_CLK_ENABLE(); if (obj->pwm == PWM_9) __TIM9_CLK_ENABLE(); if (obj->pwm == PWM_10) __TIM10_CLK_ENABLE(); if (obj->pwm == PWM_11) __TIM11_CLK_ENABLE(); // Configure GPIO pinmap_pinout(pin, PinMap_PWM); obj->pin = pin; obj->period = 0; obj->pulse = 0; pwmout_period_us(obj, 20000); // 20 ms per default }
void pwmout_init(pwmout_t* obj, PinName pin) { // Get the peripheral name from the pin and assign it to the object obj->pwm = (PWMName)pinmap_peripheral(pin, PinMap_PWM); MBED_ASSERT(obj->pwm != (PWMName)NC); // Get the functions (timer channel, (non)inverted) from the pin and assign it to the object uint32_t function = pinmap_function(pin, PinMap_PWM); MBED_ASSERT(function != (uint32_t)NC); obj->channel = STM_PIN_CHANNEL(function); obj->inverted = STM_PIN_INVERTED(function); // Enable TIM clock if (obj->pwm == PWM_1) __TIM1_CLK_ENABLE(); if (obj->pwm == PWM_2) __TIM2_CLK_ENABLE(); if (obj->pwm == PWM_3) __TIM3_CLK_ENABLE(); // Configure GPIO pinmap_pinout(pin, PinMap_PWM); obj->pin = pin; obj->period = 0; obj->pulse = 0; obj->prescaler = 1; pwmout_period_us(obj, 20000); // 20 ms per default }
/** * @brief TIM MSP Initialization * This function configures the hardware resources used in this example: * - Peripheral's clock enable * - Peripheral's GPIO Configuration * @param htim: TIM handle pointer * @retval None */ void HAL_TIM_PWM_MspInit(TIM_HandleTypeDef *htim) { GPIO_InitTypeDef GPIO_InitStruct; /* Enable TIM1, TIM3 & TIM4 clocks */ __TIM2_CLK_ENABLE(); __TIM3_CLK_ENABLE(); __TIM4_CLK_ENABLE(); /* Enable GPIOA, GPIOB & GPIOC Clocks */ __GPIOA_CLK_ENABLE(); __GPIOB_CLK_ENABLE(); /* Configure PA.8 (TIM1_Channel1), PC.6 (TIM3_Channel1) and PB.6 (TIM4_Channel1), in push-pull, alternate function mode */ GPIO_InitStruct.Pin = GPIO_PIN_0; GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; GPIO_InitStruct.Pull = GPIO_PULLUP; GPIO_InitStruct.Speed = GPIO_SPEED_HIGH; GPIO_InitStruct.Alternate = GPIO_AF1_TIM2; HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); GPIO_InitStruct.Alternate = GPIO_AF2_TIM3; GPIO_InitStruct.Pin = GPIO_PIN_4; HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); GPIO_InitStruct.Alternate = GPIO_AF2_TIM4; GPIO_InitStruct.Pin = GPIO_PIN_6; HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); }
void pwmout_init(pwmout_t* obj, PinName pin) { // Get the peripheral name from the pin and assign it to the object obj->pwm = (PWMName)pinmap_peripheral(pin, PinMap_PWM); MBED_ASSERT(obj->pwm != (PWMName)NC); // Get the pin function and assign the used channel to the object uint32_t function = pinmap_function(pin, PinMap_PWM); MBED_ASSERT(function != (uint32_t)NC); obj->channel = STM_PIN_CHANNEL(function); obj->inverted = STM_PIN_INVERTED(function); // Enable TIM clock if (obj->pwm == PWM_2) __TIM2_CLK_ENABLE(); #if defined(TIM3_BASE) if (obj->pwm == PWM_3) __TIM3_CLK_ENABLE(); #endif if (obj->pwm == PWM_21) __TIM21_CLK_ENABLE(); #if defined(TIM22_BASE) if (obj->pwm == PWM_22) __TIM22_CLK_ENABLE(); #endif // Configure GPIO pinmap_pinout(pin, PinMap_PWM); obj->pin = pin; obj->period = 0; obj->pulse = 0; pwmout_period_us(obj, 20000); // 20 ms per default }
void HAL_TIM_Encoder_MspInit(TIM_HandleTypeDef* htim_encoder) { GPIO_InitTypeDef GPIO_InitStruct; if(htim_encoder->Instance==TIM2) { /* USER CODE BEGIN TIM2_MspInit 0 */ /* USER CODE END TIM2_MspInit 0 */ /* Peripheral clock enable */ __TIM2_CLK_ENABLE(); /**TIM2 GPIO Configuration PA0-WKUP ------> TIM2_CH1 PA1 ------> TIM2_CH2 */ GPIO_InitStruct.Pin = GPIO_PIN_0|GPIO_PIN_1; GPIO_InitStruct.Mode = GPIO_MODE_INPUT; GPIO_InitStruct.Pull = GPIO_NOPULL; HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); /* USER CODE BEGIN TIM2_MspInit 1 */ /* USER CODE END TIM2_MspInit 1 */ } }
void HAL_TIMEx_HallSensor_MspInit(TIM_HandleTypeDef* htimex_hallsensor) { GPIO_InitTypeDef GPIO_InitStruct; if(htimex_hallsensor->Instance==TIM2) { /* USER CODE BEGIN TIM2_MspInit 0 */ /* USER CODE END TIM2_MspInit 0 */ /* Peripheral clock enable */ __TIM2_CLK_ENABLE(); /**TIM2 GPIO Configuration PA0-WKUP ------> TIM2_CH1 PA1 ------> TIM2_CH2 PA2 ------> TIM2_CH3 PA3 ------> TIM2_CH4 */ GPIO_InitStruct.Pin = GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_2; GPIO_InitStruct.Mode = GPIO_MODE_INPUT; GPIO_InitStruct.Pull = GPIO_NOPULL; HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); GPIO_InitStruct.Pin = GPIO_PIN_3; GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; GPIO_InitStruct.Speed = GPIO_SPEED_LOW; HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); /* USER CODE BEGIN TIM2_MspInit 1 */ /* USER CODE END TIM2_MspInit 1 */ } }
void pwmout_init(pwmout_t* obj, PinName pin) { // Get the peripheral name from the pin and assign it to the object obj->pwm = (PWMName)pinmap_peripheral(pin, PinMap_PWM); if (obj->pwm == (PWMName)NC) { error("PWM error: pinout mapping failed."); } // Enable TIM clock #if defined(TIM1_BASE) if (obj->pwm == PWM_1) __TIM1_CLK_ENABLE(); #endif #if defined(TIM2_BASE) if (obj->pwm == PWM_2) __TIM2_CLK_ENABLE(); #endif if (obj->pwm == PWM_3) __TIM3_CLK_ENABLE(); if (obj->pwm == PWM_14) __TIM14_CLK_ENABLE(); if (obj->pwm == PWM_15) __TIM15_CLK_ENABLE(); if (obj->pwm == PWM_16) __TIM16_CLK_ENABLE(); if (obj->pwm == PWM_17) __TIM17_CLK_ENABLE(); // Configure GPIO pinmap_pinout(pin, PinMap_PWM); obj->pin = pin; obj->period = 0; obj->pulse = 0; pwmout_period_us(obj, 20000); // 20 ms per default }
/** * @brief TIM MSP Initialization * This function configures the hardware resources used in this example: * - Peripheral's clock enable * - Peripheral's GPIO Configuration * @param htim: TIM handle pointer * @retval None */ void HAL_TIM_PWM_MspInit(TIM_HandleTypeDef *htim) { GPIO_InitTypeDef GPIO_InitStruct; /*##-1- Enable peripherals and GPIO Clocks #################################*/ /* TIM2 Peripheral clock enable */ __TIM2_CLK_ENABLE(); /* Enable GPIO Channels Clock */ __GPIOA_CLK_ENABLE(); __GPIOB_CLK_ENABLE(); /*##-2- Configure I/Os #####################################################*/ /* Configure PA.0 (TIM2_Channel1), PA.1 (TIM2_Channel2), PB.10 (TIM2_Channel3), PB.11 (TIM2_Channel4) in output, push-pull, alternate function mode */ /* Common configuration for all channels */ GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; GPIO_InitStruct.Pull = GPIO_PULLUP; GPIO_InitStruct.Speed = GPIO_SPEED_HIGH; GPIO_InitStruct.Alternate = GPIO_AF2_TIM2; GPIO_InitStruct.Pin = GPIO_PIN_0; HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); GPIO_InitStruct.Pin = GPIO_PIN_1; HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); GPIO_InitStruct.Pin = GPIO_PIN_10; HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); GPIO_InitStruct.Pin = GPIO_PIN_11; HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); }
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); }
void HAL_TIM_Base_MspInit(TIM_HandleTypeDef* htim_base) { GPIO_InitTypeDef GPIO_InitStruct; if(htim_base->Instance==TIM2) { /* USER CODE BEGIN TIM2_MspInit 0 */ /* USER CODE END TIM2_MspInit 0 */ /* Peripheral clock enable */ __TIM2_CLK_ENABLE(); /**TIM2 GPIO Configuration PA0-WKUP ------> TIM2_CH1 */ GPIO_InitStruct.Pin = GPIO_PIN_0; GPIO_InitStruct.Mode = GPIO_MODE_INPUT; GPIO_InitStruct.Pull = GPIO_NOPULL; HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); /* USER CODE BEGIN TIM2_MspInit 1 */ /* USER CODE END TIM2_MspInit 1 */ } else if(htim_base->Instance==TIM3) { /* USER CODE BEGIN TIM3_MspInit 0 */ /* USER CODE END TIM3_MspInit 0 */ /* Peripheral clock enable */ __TIM3_CLK_ENABLE(); /* USER CODE BEGIN TIM3_MspInit 1 */ /* USER CODE END TIM3_MspInit 1 */ } else if(htim_base->Instance==TIM8) { /* USER CODE BEGIN TIM8_MspInit 0 */ /* USER CODE END TIM8_MspInit 0 */ /* Peripheral clock enable */ __TIM8_CLK_ENABLE(); /**TIM8 GPIO Configuration PC6 ------> TIM8_CH1 */ GPIO_InitStruct.Pin = GPIO_PIN_6; GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; GPIO_InitStruct.Speed = GPIO_SPEED_LOW; HAL_GPIO_Init(GPIOC, &GPIO_InitStruct); /* USER CODE BEGIN TIM8_MspInit 1 */ /* USER CODE END TIM8_MspInit 1 */ } }
void HAL_TIM_PWM_MspInit(TIM_HandleTypeDef* htim_pwm) { GPIO_InitTypeDef GPIO_InitStruct; if(htim_pwm->Instance==TIM2) { /* Peripheral clock enable */ __TIM2_CLK_ENABLE(); /**TIM2 GPIO Configuration PB10 ------> TIM2_CH3 PA15 ------> TIM2_CH1 */ GPIO_InitStruct.Pin = GPIO_PIN_10; GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; GPIO_InitStruct.Pull = GPIO_NOPULL; GPIO_InitStruct.Speed = GPIO_SPEED_LOW; GPIO_InitStruct.Alternate = GPIO_AF1_TIM2; HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); GPIO_InitStruct.Pin = GPIO_PIN_15; GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; GPIO_InitStruct.Pull = GPIO_NOPULL; GPIO_InitStruct.Speed = GPIO_SPEED_LOW; GPIO_InitStruct.Alternate = GPIO_AF1_TIM2; HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); } else if(htim_pwm->Instance==TIM3) { /* Peripheral clock enable */ __TIM3_CLK_ENABLE(); /**TIM3 GPIO Configuration PC6 ------> TIM3_CH1 PB5 ------> TIM3_CH2 */ GPIO_InitStruct.Pin = GPIO_PIN_6; GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; GPIO_InitStruct.Pull = GPIO_NOPULL; GPIO_InitStruct.Speed = GPIO_SPEED_LOW; GPIO_InitStruct.Alternate = GPIO_AF2_TIM3; HAL_GPIO_Init(GPIOC, &GPIO_InitStruct); GPIO_InitStruct.Pin = GPIO_PIN_5; GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; GPIO_InitStruct.Pull = GPIO_NOPULL; GPIO_InitStruct.Speed = GPIO_SPEED_LOW; GPIO_InitStruct.Alternate = GPIO_AF2_TIM3; HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); } }
//------------------------------------------------------------------------------ void timer_init(void) { __TIM2_CLK_ENABLE(); timer2Handle.Instance = TIM2; timer2Handle.Init.Prescaler = 271; timer2Handle.Init.CounterMode = TIM_COUNTERMODE_UP; timer2Handle.Init.Period = 62499; timer2Handle.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1; HAL_TIM_Base_Init(&timer2Handle); // Init timer HAL_TIM_Base_Start_IT(&timer2Handle); // start timer interrupts HAL_NVIC_SetPriority(TIM2_IRQn, 0, 1); HAL_NVIC_EnableIRQ(TIM2_IRQn); }
void HAL_TIM_Base_MspInit(TIM_HandleTypeDef* htim_base) { if(htim_base->Instance==TIM2) { /* USER CODE BEGIN TIM2_MspInit 0 */ /* USER CODE END TIM2_MspInit 0 */ /* Peripheral clock enable */ __TIM2_CLK_ENABLE(); /* Peripheral interrupt init*/ HAL_NVIC_SetPriority(TIM2_IRQn, 7, 0); HAL_NVIC_EnableIRQ(TIM2_IRQn); /* USER CODE BEGIN TIM2_MspInit 1 */ /* USER CODE END TIM2_MspInit 1 */ } else if(htim_base->Instance==TIM6) { /* USER CODE BEGIN TIM6_MspInit 0 */ /* USER CODE END TIM6_MspInit 0 */ /* Peripheral clock enable */ __TIM6_CLK_ENABLE(); /* Peripheral interrupt init*/ HAL_NVIC_SetPriority(TIM6_DAC_IRQn, 10, 0); HAL_NVIC_EnableIRQ(TIM6_DAC_IRQn); /* USER CODE BEGIN TIM6_MspInit 1 */ /* USER CODE END TIM6_MspInit 1 */ } else if(htim_base->Instance==TIM7) { /* USER CODE BEGIN TIM7_MspInit 0 */ /* USER CODE END TIM7_MspInit 0 */ /* Peripheral clock enable */ __TIM7_CLK_ENABLE(); /* Peripheral interrupt init*/ HAL_NVIC_SetPriority(TIM7_IRQn, 5, 0); HAL_NVIC_EnableIRQ(TIM7_IRQn); /* USER CODE BEGIN TIM7_MspInit 1 */ /* USER CODE END TIM7_MspInit 1 */ } }
/** * @brief TIM MSP Initialization * This function configures the hardware resources used in this example: * - Peripheral's clock enable * - Peripheral's GPIO Configuration * - DMA configuration for transmission request by peripheral * @param htim: TIM handle pointer * @retval None */ void HAL_TIM_PWM_MspInit(TIM_HandleTypeDef *htim) { GPIO_InitTypeDef GPIO_InitStruct; static DMA_HandleTypeDef hdma_tim; /*##-1- Enable peripherals and GPIO Clocks #################################*/ /* TIM2 clock enable */ __TIM2_CLK_ENABLE(); /* Enable GPIO TIM2_Channel3 (PORTB) Clock */ __GPIOA_CLK_ENABLE(); /* Enable DMA1 clock */ __DMA1_CLK_ENABLE(); /*##-2- Configure peripheral GPIO ##########################################*/ /* Configure TIM2_Channel3 in output, push-pull & alternate function mode */ GPIO_InitStruct.Pin = GPIO_PIN_0; GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; GPIO_InitStruct.Pull = GPIO_PULLUP; GPIO_InitStruct.Speed = GPIO_SPEED_HIGH; GPIO_InitStruct.Alternate = GPIO_AF2_TIM2; HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); /*##-3- Configure the DMA stream ###########################################*/ /* Set the parameters to be configured */ hdma_tim.Init.Request = DMA_REQUEST_8; hdma_tim.Instance = DMA1_Channel2; hdma_tim.Init.Direction = DMA_MEMORY_TO_PERIPH; hdma_tim.Init.PeriphInc = DMA_PINC_DISABLE; hdma_tim.Init.MemInc = DMA_MINC_ENABLE; hdma_tim.Init.PeriphDataAlignment = DMA_PDATAALIGN_WORD; hdma_tim.Init.MemDataAlignment = DMA_MDATAALIGN_WORD; hdma_tim.Init.Mode = DMA_NORMAL; hdma_tim.Init.Priority = DMA_PRIORITY_HIGH; /* Link hdma_tim to hdma[TIM_DMA_ID_CC3] (channel3) */ __HAL_LINKDMA(htim, hdma[TIM_DMA_ID_UPDATE], hdma_tim); /* Initialize TIMx DMA handle */ HAL_DMA_Init(htim->hdma[TIM_DMA_ID_UPDATE]); /*##-4- Configure the NVIC for DMA #########################################*/ /* NVIC configuration for DMA transfer complete interrupt */ HAL_NVIC_SetPriority(DMA1_Channel2_3_IRQn, 0, 0); HAL_NVIC_EnableIRQ(DMA1_Channel2_3_IRQn); }
void HAL_TIM_Base_MspInit(TIM_HandleTypeDef* htim_base) { if(htim_base->Instance==TIM2) { /* Peripheral clock enable */ __TIM2_CLK_ENABLE(); /*##-2- Configure the NVIC for TIMx ########################################*/ /* Set Interrupt Group Priority */ HAL_NVIC_SetPriority(TIMx_IRQn, 4, 0); /* Enable the TIMx global Interrupt */ HAL_NVIC_EnableIRQ(TIMx_IRQn); } }
void HAL_TIM_Base_MspInit(TIM_HandleTypeDef* htim_base) { if(htim_base->Instance==TIM2) { /* USER CODE BEGIN TIM2_MspInit 0 */ /* USER CODE END TIM2_MspInit 0 */ /* Peripheral clock enable */ __TIM2_CLK_ENABLE(); /* USER CODE BEGIN TIM2_MspInit 1 */ /* USER CODE END TIM2_MspInit 1 */ } }
void HAL_TIM_PWM_MspInit(TIM_HandleTypeDef* htim_base) { GPIO_InitTypeDef GPIO_InitStruct; if (htim_base->Instance == TIM2) { __TIM2_CLK_ENABLE(); /**TIM2 GPIO Configuration PA5 ------> TIM3_CH1 */ GPIO_InitStruct.Pin = GPIO_PIN_5; GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; GPIO_InitStruct.Pull = GPIO_NOPULL; GPIO_InitStruct.Speed = GPIO_SPEED_HIGH; GPIO_InitStruct.Alternate = GPIO_AF1_TIM2; HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); } }
void HAL_TIM_Base_MspInit(TIM_HandleTypeDef* htim_base) { GPIO_InitTypeDef GPIO_InitStruct; if(htim_base->Instance==TIM2) { /* USER CODE BEGIN TIM2_MspInit 0 */ /* USER CODE END TIM2_MspInit 0 */ /* Peripheral clock enable */ __TIM2_CLK_ENABLE(); /**TIM2 GPIO Configuration PA5 ------> TIM2_CH1 */ GPIO_InitStruct.Pin = GPIO_PIN_5; GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; GPIO_InitStruct.Pull = GPIO_NOPULL; GPIO_InitStruct.Speed = GPIO_SPEED_LOW; GPIO_InitStruct.Alternate = GPIO_AF1_TIM2; HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); /* Peripheral DMA init*/ hdma_tim2_ch1.Instance = DMA1_Stream5; hdma_tim2_ch1.Init.Channel = DMA_CHANNEL_3; hdma_tim2_ch1.Init.Direction = DMA_PERIPH_TO_MEMORY; hdma_tim2_ch1.Init.PeriphInc = DMA_PINC_DISABLE; hdma_tim2_ch1.Init.MemInc = DMA_MINC_ENABLE; hdma_tim2_ch1.Init.PeriphDataAlignment = DMA_PDATAALIGN_WORD; hdma_tim2_ch1.Init.MemDataAlignment = DMA_MDATAALIGN_WORD; hdma_tim2_ch1.Init.Mode = DMA_NORMAL; hdma_tim2_ch1.Init.Priority = DMA_PRIORITY_LOW; hdma_tim2_ch1.Init.FIFOMode = DMA_FIFOMODE_DISABLE; HAL_DMA_Init(&hdma_tim2_ch1); __HAL_LINKDMA(htim_base,hdma[TIM_DMA_ID_CC1],hdma_tim2_ch1); /* Peripheral interrupt init*/ HAL_NVIC_SetPriority(TIM2_IRQn, 3, 0); HAL_NVIC_EnableIRQ(TIM2_IRQn); /* USER CODE BEGIN TIM2_MspInit 1 */ /* USER CODE END TIM2_MspInit 1 */ } }
void HAL_TIM_Base_MspInit(TIM_HandleTypeDef* htim_base) { GPIO_InitTypeDef GPIO_InitStruct; if(htim_base->Instance==TIM2) { /* Peripheral clock enable */ __TIM2_CLK_ENABLE(); /**TIM2 GPIO Configuration PA1 ------> TIM2_CH2 */ GPIO_InitStruct.Pin = GPIO_PIN_1; GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; GPIO_InitStruct.Pull = GPIO_NOPULL; GPIO_InitStruct.Speed = GPIO_SPEED_LOW; GPIO_InitStruct.Alternate = GPIO_AF1_TIM2; HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); } }
void ClockEnable (void) { __GPIOA_CLK_ENABLE(); __GPIOB_CLK_ENABLE(); __GPIOC_CLK_ENABLE(); __GPIOD_CLK_ENABLE(); __GPIOE_CLK_ENABLE(); __GPIOF_CLK_ENABLE(); __GPIOG_CLK_ENABLE(); __GPIOH_CLK_ENABLE(); __ADC1_CLK_ENABLE(); __ADC2_CLK_ENABLE(); __DAC_CLK_ENABLE(); __FSMC_CLK_ENABLE(); __SPI2_CLK_ENABLE(); __DMA2_CLK_ENABLE(); __TIM1_CLK_ENABLE(); __TIM2_CLK_ENABLE(); }
void HAL_TIM_Base_MspInit(TIM_HandleTypeDef* htim_base) { GPIO_InitTypeDef GPIO_InitStruct; if(htim_base->Instance==TIM2) { __TIM2_CLK_ENABLE(); /**TIM1 GPIO Configuration PA0 ------> TIM2_CH1 */ GPIO_InitStruct.Pin = GPIO_PIN_0; GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; GPIO_InitStruct.Pull = GPIO_PULLDOWN; GPIO_InitStruct.Speed = GPIO_SPEED_LOW; GPIO_InitStruct.Alternate = GPIO_AF2_TIM2; HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); } if(htim_base->Instance==TIM21) __TIM21_CLK_ENABLE(); }
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); }
extern void claw_init(){ uint16_t PrescalerValue = 0; GPIO_InitTypeDef GPIO_InitStructure; TIM_OC_InitTypeDef PWMConfig; __TIM2_CLK_ENABLE(); __BRD_D3_GPIO_CLK(); GPIO_InitStructure.Pin = BRD_D3_PIN; //Pin GPIO_InitStructure.Mode = GPIO_MODE_AF_PP; //Set mode to be output alternate GPIO_InitStructure.Pull = GPIO_NOPULL; //Enable Pull up, down or no pull resister GPIO_InitStructure.Speed = GPIO_SPEED_MEDIUM; //Pin latency GPIO_InitStructure.Alternate = GPIO_AF1_TIM2; //Set alternate function to be timer 3 HAL_GPIO_Init(BRD_D3_GPIO_PORT, &GPIO_InitStructure); //Initialise Pin /* Compute the prescaler value. SystemCoreClock = 168000000 - set for 500Khz clock */ PrescalerValue = (uint16_t) ((SystemCoreClock /2) / 500000) - 1; /* Configure Timer settings */ TIM_Init.Instance = TIM2; //Enable Timer 3 TIM_Init.Init.Period = 20000; //Set for 200ms (5Hz) period TIM_Init.Init.Prescaler = PrescalerValue; //Set presale value TIM_Init.Init.ClockDivision = 0; //Set clock division TIM_Init.Init.RepetitionCounter = 0; // Set Reload Value TIM_Init.Init.CounterMode = TIM_COUNTERMODE_UP; //Set timer to count up. /* PWM Mode configuration for Channel 2 - set pulse width*/ PWMConfig.OCMode = TIM_OCMODE_PWM1; //Set PWM MODE (1 or 2 - NOT CHANNEL) PWMConfig.Pulse = 100; //1ms pulse width to 10ms PWMConfig.OCPolarity = TIM_OCPOLARITY_HIGH; PWMConfig.OCNPolarity = TIM_OCNPOLARITY_HIGH; PWMConfig.OCFastMode = TIM_OCFAST_DISABLE; PWMConfig.OCIdleState = TIM_OCIDLESTATE_RESET; PWMConfig.OCNIdleState = TIM_OCNIDLESTATE_RESET; /* Enable PWM for Timer 2, channel 2 */ HAL_TIM_PWM_Init(&TIM_Init); HAL_TIM_PWM_ConfigChannel(&TIM_Init, &PWMConfig, TIM_CHANNEL_2); /* Start PWM */ HAL_TIM_PWM_Start(&TIM_Init, TIM_CHANNEL_2); }
void vInitialiseTimerForIntQueueTest( void ) { TIM_HandleTypeDef xTimHandle; const uint32_t ulPrescale = 0; /* No prescale. */ /* Clock the utilised timers. */ __TIM2_CLK_ENABLE(); __TIM3_CLK_ENABLE(); __TIM4_CLK_ENABLE(); /* Configure TIM2 to generate an interrupt at the required frequency. */ xTimHandle.Instance = TIM2; xTimHandle.Init.Period = ( SystemCoreClock / 2UL ) / ( tmrTIMER_2_FREQUENCY - 1 ); xTimHandle.Init.Prescaler = ulPrescale; xTimHandle.Init.ClockDivision = 0; xTimHandle.Init.CounterMode = TIM_COUNTERMODE_UP; HAL_TIM_Base_Init( &xTimHandle ); HAL_TIM_Base_Start_IT( &xTimHandle ); /* Configure and enable TIM2 interrupt. */ NVIC_SetPriority( TIM2_IRQn, tmrLOWER_PRIORITY ); NVIC_ClearPendingIRQ( TIM2_IRQn ); NVIC_EnableIRQ( TIM2_IRQn ); /* Repeat for TIM3 and TIM4. */ xTimHandle.Instance = TIM3; xTimHandle.Init.Period = ( SystemCoreClock / 2UL ) / ( tmrTIMER_3_FREQUENCY - 1 ); HAL_TIM_Base_Init( &xTimHandle ); HAL_TIM_Base_Start_IT( &xTimHandle ); NVIC_SetPriority( TIM3_IRQn, tmrMEDIUM_PRIORITY ); NVIC_ClearPendingIRQ( TIM3_IRQn ); NVIC_EnableIRQ( TIM3_IRQn ); xTimHandle.Instance = TIM4; xTimHandle.Init.Period = ( SystemCoreClock / 2UL ) / ( tmrTIMER_4_FREQUENCY - 1 ); HAL_TIM_Base_Init( &xTimHandle ); HAL_TIM_Base_Start_IT( &xTimHandle ); NVIC_SetPriority( TIM4_IRQn, tmrHIGHER_PRIORITY ); NVIC_ClearPendingIRQ( TIM4_IRQn ); NVIC_EnableIRQ( TIM4_IRQn ); }
Cpwm::Cpwm(TIM_TypeDef* TIMx, UInt32 TIM_CHANNEL_x, GPIO_TypeDef* GPIOx, UInt16 GPIO_PIN_x, UInt8 GPIO_AFx_TIMx):mPrescaler(205), mPeriod(8191), mTIM_CHANNEL_x(0) //mFrequenz(0) { mTIM_CHANNEL_x = TIM_CHANNEL_x; switch (reinterpret_cast<UInt32>(TIMx)) { case reinterpret_cast<UInt32>(TIM1): __TIM1_CLK_ENABLE(); break; case reinterpret_cast<UInt32>(TIM2): __TIM2_CLK_ENABLE(); break; case reinterpret_cast<UInt32>(TIM3): __TIM3_CLK_ENABLE(); break; default: break; } switch(reinterpret_cast<UInt32>(GPIOx)) { case reinterpret_cast<UInt32>(GPIOA): __GPIOA_CLK_ENABLE(); break; case reinterpret_cast<UInt32>(GPIOB): __GPIOB_CLK_ENABLE(); break; case reinterpret_cast<UInt32>(GPIOC): __GPIOC_CLK_ENABLE(); break; case reinterpret_cast<UInt32>(GPIOD): __GPIOD_CLK_ENABLE(); break; default: break; } Timer_PWM_Init(TIMx, TIM_OCMODE_PWM1); GPIO_Init(GPIOx, GPIO_PIN_x, GPIO_AFx_TIMx); }
/* TIM2 init function - periodic event timebase 32-bit */ void MX_TIM2_Init(void) { TIM_MasterConfigTypeDef sMasterConfig; /* Peripheral clock enable */ __TIM2_CLK_ENABLE(); htim2.Instance = TIM2; htim2.Init.Prescaler = 0; htim2.Init.CounterMode = TIM_COUNTERMODE_UP; htim2.Init.Period = 48; HAL_TIM_Base_Init(&htim2); sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET; sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE; HAL_TIMEx_MasterConfigSynchronization(&htim2, &sMasterConfig); /* Peripheral interrupt init*/ HAL_NVIC_SetPriority(TIM2_IRQn, 0, 0); HAL_NVIC_EnableIRQ(TIM2_IRQn); }
void pwmout_init_ex(pwmout_t* obj, PinName pin, uint32_t freq) { HAL_RCC_DeInit(); if (!HSE_SystemClock_Config_72Mhz()) { error("PWM error: switch to 72Mhz failed.");; } // Update the SystemCoreClock variable SystemCoreClockUpdate(); if (freq > SystemCoreClock) { error("PWM error: clock is too high."); } if ( (SystemCoreClock / 2) % freq) { error("PWM error: unsupported clock."); } // Get the peripheral name from the pin and assign it to the object obj->pwm = (PWMName)pinmap_peripheral(pin, PinMap_PWM); if (obj->pwm == (PWMName)NC) { error("PWM error: pinout mapping failed."); } // Enable TIM clock if (obj->pwm == PWM_1) __TIM1_CLK_ENABLE(); if (obj->pwm == PWM_2) __TIM2_CLK_ENABLE(); if (obj->pwm == PWM_3) __TIM3_CLK_ENABLE(); // Configure GPIO pinmap_pinout(pin, PinMap_PWM); obj->pin = pin; obj->period = 0; obj->pulse = 0; pwmout_period_ns(obj, (SystemCoreClock / 2) / freq); // 20 ms per default }
void HAL_TIM_Base_MspInit(TIM_HandleTypeDef* htim_base) { GPIO_InitTypeDef GPIO_InitStruct; if(htim_base->Instance==TIM1) { /* USER CODE BEGIN TIM1_MspInit 0 */ /* USER CODE END TIM1_MspInit 0 */ /* Peripheral clock enable */ __TIM1_CLK_ENABLE(); /**TIM1 GPIO Configuration PE9 ------> TIM1_CH1 PE11 ------> TIM1_CH2 PE13 ------> TIM1_CH3 */ GPIO_InitStruct.Pin = GPIO_PIN_9|GPIO_PIN_11; GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; GPIO_InitStruct.Pull = GPIO_NOPULL; GPIO_InitStruct.Speed = GPIO_SPEED_LOW; GPIO_InitStruct.Alternate = GPIO_AF1_TIM1; HAL_GPIO_Init(GPIOE, &GPIO_InitStruct); GPIO_InitStruct.Pin = GPIO_PIN_13; GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; GPIO_InitStruct.Pull = GPIO_PULLDOWN; GPIO_InitStruct.Speed = GPIO_SPEED_LOW; GPIO_InitStruct.Alternate = GPIO_AF1_TIM1; HAL_GPIO_Init(GPIOE, &GPIO_InitStruct); /* Peripheral interrupt init*/ HAL_NVIC_SetPriority(TIM1_CC_IRQn, 0, 9); HAL_NVIC_EnableIRQ(TIM1_CC_IRQn); /* USER CODE BEGIN TIM1_MspInit 1 */ /* USER CODE END TIM1_MspInit 1 */ } else if(htim_base->Instance==TIM2) { /* USER CODE BEGIN TIM2_MspInit 0 */ /* USER CODE END TIM2_MspInit 0 */ /* Peripheral clock enable */ __TIM2_CLK_ENABLE(); /* Peripheral interrupt init*/ HAL_NVIC_SetPriority(TIM2_IRQn, 0, 5); HAL_NVIC_EnableIRQ(TIM2_IRQn); /* USER CODE BEGIN TIM2_MspInit 1 */ /* USER CODE END TIM2_MspInit 1 */ } else if(htim_base->Instance==TIM5) { /* USER CODE BEGIN TIM5_MspInit 0 */ /* USER CODE END TIM5_MspInit 0 */ /* Peripheral clock enable */ __TIM5_CLK_ENABLE(); /**TIM5 GPIO Configuration PA1 ------> TIM5_CH2 */ GPIO_InitStruct.Pin = GPIO_PIN_1; GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; GPIO_InitStruct.Pull = GPIO_NOPULL; GPIO_InitStruct.Speed = GPIO_SPEED_LOW; GPIO_InitStruct.Alternate = GPIO_AF2_TIM5; HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); /* Peripheral interrupt init*/ HAL_NVIC_SetPriority(TIM5_IRQn, 0, 13); HAL_NVIC_EnableIRQ(TIM5_IRQn); /* USER CODE BEGIN TIM5_MspInit 1 */ /* USER CODE END TIM5_MspInit 1 */ } else if(htim_base->Instance==TIM9) { /* USER CODE BEGIN TIM9_MspInit 0 */ /* USER CODE END TIM9_MspInit 0 */ /* Peripheral clock enable */ __TIM9_CLK_ENABLE(); /**TIM9 GPIO Configuration PE5 ------> TIM9_CH1 PE6 ------> TIM9_CH2 */ GPIO_InitStruct.Pin = GPIO_PIN_5|GPIO_PIN_6; GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; GPIO_InitStruct.Pull = GPIO_NOPULL; GPIO_InitStruct.Speed = GPIO_SPEED_LOW; GPIO_InitStruct.Alternate = GPIO_AF3_TIM9; HAL_GPIO_Init(GPIOE, &GPIO_InitStruct); /* USER CODE BEGIN TIM9_MspInit 1 */ /* USER CODE END TIM9_MspInit 1 */ } else if(htim_base->Instance==TIM10) { /* USER CODE BEGIN TIM10_MspInit 0 */ /* USER CODE END TIM10_MspInit 0 */ /* Peripheral clock enable */ __TIM10_CLK_ENABLE(); /**TIM10 GPIO Configuration PF6 ------> TIM10_CH1 */ GPIO_InitStruct.Pin = GPIO_PIN_6; GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; GPIO_InitStruct.Pull = GPIO_NOPULL; GPIO_InitStruct.Speed = GPIO_SPEED_LOW; GPIO_InitStruct.Alternate = GPIO_AF3_TIM10; HAL_GPIO_Init(GPIOF, &GPIO_InitStruct); /* USER CODE BEGIN TIM10_MspInit 1 */ /* USER CODE END TIM10_MspInit 1 */ } else if(htim_base->Instance==TIM11) { /* USER CODE BEGIN TIM11_MspInit 0 */ /* USER CODE END TIM11_MspInit 0 */ /* Peripheral clock enable */ __TIM11_CLK_ENABLE(); /**TIM11 GPIO Configuration PF7 ------> TIM11_CH1 */ GPIO_InitStruct.Pin = GPIO_PIN_7; GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; GPIO_InitStruct.Pull = GPIO_NOPULL; GPIO_InitStruct.Speed = GPIO_SPEED_LOW; GPIO_InitStruct.Alternate = GPIO_AF3_TIM11; HAL_GPIO_Init(GPIOF, &GPIO_InitStruct); /* USER CODE BEGIN TIM11_MspInit 1 */ /* USER CODE END TIM11_MspInit 1 */ } else if(htim_base->Instance==TIM13) { /* USER CODE BEGIN TIM13_MspInit 0 */ /* USER CODE END TIM13_MspInit 0 */ /* Peripheral clock enable */ __TIM13_CLK_ENABLE(); /**TIM13 GPIO Configuration PF8 ------> TIM13_CH1 */ GPIO_InitStruct.Pin = GPIO_PIN_8; GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; GPIO_InitStruct.Pull = GPIO_NOPULL; GPIO_InitStruct.Speed = GPIO_SPEED_LOW; GPIO_InitStruct.Alternate = GPIO_AF9_TIM13; HAL_GPIO_Init(GPIOF, &GPIO_InitStruct); /* USER CODE BEGIN TIM13_MspInit 1 */ /* USER CODE END TIM13_MspInit 1 */ } }