void HAL_TIM_Base_MspInit(TIM_HandleTypeDef* htim_base) { GPIO_InitTypeDef GPIO_InitStruct; if(htim_base->Instance==TIM3) { /* Peripheral clock enable */ __TIM3_CLK_ENABLE(); /**TIM3 GPIO Configuration PA6 ------> TIM3_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_AF1_TIM3; HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); } else if(htim_base->Instance==TIM14) { /* Peripheral clock enable */ __TIM14_CLK_ENABLE(); /* Peripheral interrupt init*/ HAL_NVIC_SetPriority(TIM14_IRQn, 0, 0); HAL_NVIC_EnableIRQ(TIM14_IRQn); } }
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 }
void HAL_TIM_PWM_MspInit(TIM_HandleTypeDef* htim_pwm) { if(htim_pwm->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_pwm->Instance==TIM4) { /* USER CODE BEGIN TIM4_MspInit 0 */ /* USER CODE END TIM4_MspInit 0 */ /* Peripheral clock enable */ __TIM4_CLK_ENABLE(); /* USER CODE BEGIN TIM4_MspInit 1 */ /* USER CODE END TIM4_MspInit 1 */ } }
/** * @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_IC_MspInit(TIM_HandleTypeDef* htim_ic) { GPIO_InitTypeDef GPIO_InitStruct; if(htim_ic->Instance==TIM3) { /* USER CODE BEGIN TIM3_MspInit 0 */ /* USER CODE END TIM3_MspInit 0 */ /* Peripheral clock enable */ __TIM3_CLK_ENABLE(); /**TIM3 GPIO Configuration PA6 ------> TIM3_CH1 PA7 ------> TIM3_CH2 PB0 ------> TIM3_CH3 */ GPIO_InitStruct.Pin = GPIO_PIN_6|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_AF2_TIM3; HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); GPIO_InitStruct.Pin = GPIO_PIN_0; 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); /* USER CODE BEGIN TIM3_MspInit 1 */ /* USER CODE END TIM3_MspInit 1 */ } else if(htim_ic->Instance==TIM4) { /* USER CODE BEGIN TIM4_MspInit 0 */ /* USER CODE END TIM4_MspInit 0 */ /* Peripheral clock enable */ __TIM4_CLK_ENABLE(); /**TIM4 GPIO Configuration PD12 ------> TIM4_CH1 PD13 ------> TIM4_CH2 PD14 ------> TIM4_CH3 */ GPIO_InitStruct.Pin = GPIO_PIN_12|GPIO_PIN_13|GPIO_PIN_14; GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; GPIO_InitStruct.Pull = GPIO_NOPULL; GPIO_InitStruct.Speed = GPIO_SPEED_LOW; GPIO_InitStruct.Alternate = GPIO_AF2_TIM4; HAL_GPIO_Init(GPIOD, &GPIO_InitStruct); /* USER CODE BEGIN TIM4_MspInit 1 */ /* USER CODE END TIM4_MspInit 1 */ } }
void TIM3_init() { TIM_TimeBaseInitTypeDef TIM3_TimeBase; NVIC_InitTypeDef NVIC_InitStructure; //RCC_AHB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE); __TIM3_CLK_ENABLE(); TIM_TimeBaseStructInit(&TIM3_TimeBase); TIM3_TimeBase.TIM_Period = 65535; TIM3_TimeBase.TIM_Prescaler = 0; TIM3_TimeBase.TIM_ClockDivision = 0; TIM3_TimeBase.TIM_CounterMode = TIM_CounterMode_Up; TIM_TimeBaseInit(TIM3, &TIM3_TimeBase); TIM_ITConfig(TIM3, TIM_IT_Update, ENABLE); TIM_Cmd(TIM3, ENABLE); NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); //HAL_NVIC_SetPriority(TIM3_IRQn, 1, 1); //HAL_NVIC_EnableIRQ(TIM3_IRQn); }
void HAL_TIM_Base_MspInit(TIM_HandleTypeDef* htim_base) { #ifdef USE_SCOPE 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 */ } #endif //USE_SCOPE #ifdef USE_GEN if(htim_base->Instance==TIM6){ __TIM6_CLK_ENABLE(); } if(htim_base->Instance==TIM7){ __TIM7_CLK_ENABLE(); } #endif //USE_GEN }
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); 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 }
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 TIM3_init(void){ __TIM3_CLK_ENABLE(); TIM_Handle.Init.ClockDivision = TIM_CLOCKDIVISION_DIV4; // 168 MHz / 4 = 42 MHz TIM_Handle.Init.Prescaler = 419; // 42 MHz / (419 + 1) = 100 KHz TIM_Handle.Init.Period = 499; // 100 KHz / (499 + 1) = 200 Hz TIM_Handle.Init.CounterMode = TIM_COUNTERMODE_UP; TIM_Handle.Instance = TIM3; //Same timer whose clocks we enabled HAL_TIM_Base_Init(&TIM_Handle); // Init timer HAL_TIM_Base_Start_IT(&TIM_Handle); // start timer interrupts HAL_NVIC_SetPriority(TIM3_IRQn, 0, 1); HAL_NVIC_EnableIRQ(TIM3_IRQn); }
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 HAL_TIM_Base_MspInit(TIM_HandleTypeDef* htim_base) { GPIO_InitTypeDef GPIO_InitStruct; if(htim_base->Instance==TIM3) { /* USER CODE BEGIN TIM3_MspInit 0 */ /* USER CODE END TIM3_MspInit 0 */ /* Peripheral clock enable */ __TIM3_CLK_ENABLE(); /**TIM3 GPIO Configuration PA6 ------> TIM3_CH1 PA7 ------> TIM3_CH2 PB0 ------> TIM3_CH3 PB1 ------> TIM3_CH4 */ GPIO_InitStruct.Pin = GPIO_PIN_6|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_AF2_TIM3; HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); GPIO_InitStruct.Pin = GPIO_PIN_0|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_TIM3; HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); /* USER CODE BEGIN TIM3_MspInit 1 */ /* USER CODE END TIM3_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 * @param htim: TIM handle pointer * @retval None */ void HAL_TIM_Base_MspInit(TIM_HandleTypeDef *htim) { /*##-1- Enable peripherals and GPIO Clocks #################################*/ /* TIMx Peripheral clock enable */ __TIM3_CLK_ENABLE(); /*##-2- Configure the NVIC for TIMx #########################################*/ /* Set the TIMx priority */ HAL_NVIC_SetPriority(TIM3_IRQn, 0, 1); /* Enable the TIMx global Interrupt */ HAL_NVIC_EnableIRQ(TIM3_IRQn); }
void HAL_TIM_Base_MspInit(TIM_HandleTypeDef* htim_base) { GPIO_InitTypeDef GPIO_InitStruct; if(htim_base->Instance==TIM3) { /* USER CODE BEGIN TIM3_MspInit 0 */ /* USER CODE END TIM3_MspInit 0 */ /* Peripheral clock enable */ __TIM3_CLK_ENABLE(); /**TIM3 GPIO Configuration PB0 ------> TIM3_CH3 PB1 ------> TIM3_CH4 */ GPIO_InitStruct.Pin = GPIO_PIN_0|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_TIM3; HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); /* USER CODE BEGIN TIM3_MspInit 1 */ /* USER CODE END TIM3_MspInit 1 */ } else if(htim_base->Instance==TIM4) { /* USER CODE BEGIN TIM4_MspInit 0 */ /* USER CODE END TIM4_MspInit 0 */ /* Peripheral clock enable */ __TIM4_CLK_ENABLE(); /**TIM4 GPIO Configuration PB7 ------> TIM4_CH2 */ 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_AF2_TIM4; HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); /* USER CODE BEGIN TIM4_MspInit 1 */ /* USER CODE END TIM4_MspInit 1 */ } }
void HAL_TIM_PWM_MspInit(TIM_HandleTypeDef* htim_base) { GPIO_InitTypeDef GPIO_InitStruct; if(htim_base->Instance==TIM3) { __TIM3_CLK_ENABLE(); /**TIM3 GPIO Configuration PA6 ------> TIM3_CH1 */ GPIO_InitStruct.Pin = GPIO_PIN_6; GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; GPIO_InitStruct.Pull = GPIO_NOPULL; GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH; GPIO_InitStruct.Alternate = GPIO_AF1_TIM3; HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); } }
void HAL_TIM_PWM_MspInit(TIM_HandleTypeDef* htim_pwm) { GPIO_InitTypeDef GPIO_InitStruct; if(htim_pwm->Instance==TIM3) { /* USER CODE BEGIN TIM3_MspInit 0 */ /* USER CODE END TIM3_MspInit 0 */ /* Peripheral clock enable */ __TIM3_CLK_ENABLE(); /**TIM3 GPIO Configuration PB4 ------> TIM3_CH1 */ GPIO_InitStruct.Pin = PWM_1_Pin; GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; GPIO_InitStruct.Speed = GPIO_SPEED_LOW; HAL_GPIO_Init(PWM_1_GPIO_Port, &GPIO_InitStruct); __HAL_AFIO_REMAP_TIM3_PARTIAL(); /* USER CODE BEGIN TIM3_MspInit 1 */ /* USER CODE END TIM3_MspInit 1 */ } else if(htim_pwm->Instance==TIM4) { /* USER CODE BEGIN TIM4_MspInit 0 */ /* USER CODE END TIM4_MspInit 0 */ /* Peripheral clock enable */ __TIM4_CLK_ENABLE(); /**TIM4 GPIO Configuration PB9 ------> TIM4_CH4 */ GPIO_InitStruct.Pin = PWM_2_Pin; GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; GPIO_InitStruct.Speed = GPIO_SPEED_LOW; HAL_GPIO_Init(PWM_2_GPIO_Port, &GPIO_InitStruct); /* USER CODE BEGIN TIM4_MspInit 1 */ /* USER CODE END TIM4_MspInit 1 */ } }
/** * @brief CDC_Init_FS * Initializes the CDC media low layer over the FS USB IP * @param None * @retval Result of the operation: USBD_OK if all operations are OK else USBD_FAIL */ static int8_t CDC_Init_FS(void) { husb = &hUsbDeviceFS; /* Configure the USART with some sensible defaults TODO: If possible, don't drive TX high while CDC is "closed"? */ huart2.Instance = USART2; huart2.Init.BaudRate = 115200; huart2.Init.WordLength = UART_WORDLENGTH_8B; huart2.Init.StopBits = UART_STOPBITS_1; huart2.Init.Parity = UART_PARITY_NONE; huart2.Init.Mode = UART_MODE_TX_RX; huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE; huart2.Init.OverSampling = UART_OVERSAMPLING_16; huart2.Init.OneBitSampling = UART_ONEBIT_SAMPLING_DISABLED ; huart2.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT; HAL_UART_Init(&huart2); /* Enable USART2 in NVIC, set priority to high */ NVIC_SetPriority(USART2_IRQn, USART_IRQ_PRIORITY); NVIC_EnableIRQ(USART2_IRQn); /* UART2 receives data to the CDC transmit buffer, byte at a time :( */ if(HAL_UART_Receive_IT(&huart2, usart2cdc_rx, 1) != HAL_OK) return USBD_FAIL; /* Configure USB transmit timer */ husbtimer.Instance = TIM3; husbtimer.Init.Period = 10000 - 1; /* approx 10ms, I think... */ husbtimer.Init.Prescaler = 48-1; husbtimer.Init.ClockDivision = 0; husbtimer.Init.CounterMode = TIM_COUNTERMODE_UP; if(HAL_TIM_Base_Init(&husbtimer) != HAL_OK) return USBD_FAIL; __TIM3_CLK_ENABLE(); NVIC_SetPriority(TIM3_IRQn, USB_TIMER_IRQ_PRIORITY); NVIC_EnableIRQ(TIM3_IRQn); /* Set Application USB Buffers */ USBD_CDC_SetTxBuffer(husb, usart2cdc_tx, 0); /* don't send anything now */ USBD_CDC_SetRxBuffer(husb, cdc2usart_buf_a); /* read into here if CDC data comes */ return USBD_OK; }
void HAL_TIM_OnePulse_MspInit(TIM_HandleTypeDef* htim_base) { GPIO_InitTypeDef GPIO_InitStruct; if (htim_base->Instance == TIM3) { __TIM3_CLK_ENABLE(); /**TIM3 GPIO Configuration PA6 ------> TIM3_CH1 PA7 ------> TIM3_CH2 */ GPIO_InitStruct.Pin = GPIO_PIN_6 | GPIO_PIN_7; GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; GPIO_InitStruct.Pull = GPIO_PULLDOWN; GPIO_InitStruct.Speed = GPIO_SPEED_HIGH; GPIO_InitStruct.Alternate = GPIO_AF2_TIM3; HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); } }
void initTimers() { TIM_HandleTypeDef TIM_Handle; // 10 kHz timer. #if defined STM32F1 __TIM8_CLK_ENABLE(); TIM_Handle.Instance = TIM8; TIM_Handle.Init.Prescaler = (uint16_t)(HAL_RCC_GetPCLK2Freq() / 10000) - 1; #elif defined STM32F2 __TIM4_CLK_ENABLE(); TIM_Handle.Instance = TIM4; TIM_Handle.Init.Prescaler = (uint16_t)(HAL_RCC_GetPCLK2Freq() / 100000) - 1; #elif defined STM32F4 __TIM3_CLK_ENABLE(); TIM_Handle.Instance = TIM3; // TIM3 Clocked from SYSCLK = 168 MHz TIM_Handle.Init.Prescaler = (uint16_t)(HAL_RCC_GetSysClockFreq() / 10000) - 1; #endif // 1 Hz blinking TIM_Handle.Init.Period = 10000; TIM_Handle.Init.ClockDivision = 0; TIM_Handle.Init.CounterMode = TIM_COUNTERMODE_UP; HAL_TIM_Base_Init(&TIM_Handle); HAL_TIM_PWM_Init(&TIM_Handle); TIM_OC_InitTypeDef TIM_OCConfig; TIM_OCConfig.OCMode = TIM_OCMODE_PWM1; // 5000 / 10000 = 50% duty cycle. TIM_OCConfig.Pulse = 4999; TIM_OCConfig.OCPolarity = TIM_OCPOLARITY_HIGH; TIM_OCConfig.OCFastMode = TIM_OCFAST_DISABLE; #if defined STM32F1 HAL_TIM_PWM_ConfigChannel(&TIM_Handle, &TIM_OCConfig, TIM_CHANNEL_3); HAL_TIM_PWM_Start(&TIM_Handle, TIM_CHANNEL_3); #elif defined STM32F2 HAL_TIM_PWM_ConfigChannel(&TIM_Handle, &TIM_OCConfig, TIM_CHANNEL_1); HAL_TIM_PWM_Start(&TIM_Handle, TIM_CHANNEL_1); #elif defined STM32F4 HAL_TIM_PWM_ConfigChannel(&TIM_Handle, &TIM_OCConfig, TIM_CHANNEL_1); HAL_TIM_PWM_Start(&TIM_Handle, TIM_CHANNEL_1); #endif }
void HAL_TIM_Base_MspInit(TIM_HandleTypeDef* htim_base) { GPIO_InitTypeDef GPIO_InitStruct; if(htim_base->Instance==TIM3) { /* Peripheral clock enable */ __TIM3_CLK_ENABLE(); __GPIOD_CLK_ENABLE(); /**TIM3 GPIO Configuration PD2 ------> TIM3_ETR */ GPIO_InitStruct.Pin = GPIO_PIN_2; GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; GPIO_InitStruct.Pull = GPIO_NOPULL; GPIO_InitStruct.Speed = GPIO_SPEED_LOW; HAL_GPIO_Init(GPIOD, &GPIO_InitStruct); } }
// TIM3 is set-up for the USB CDC interface void timer_tim3_init(void) { // set up the timer for USBD CDC __TIM3_CLK_ENABLE(); TIM3_Handle.Instance = TIM3; TIM3_Handle.Init.Period = (USBD_CDC_POLLING_INTERVAL*1000) - 1; // TIM3 fires every USBD_CDC_POLLING_INTERVAL ms TIM3_Handle.Init.Prescaler = 84-1; // for System clock at 168MHz, TIM3 runs at 1MHz TIM3_Handle.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1; TIM3_Handle.Init.CounterMode = TIM_COUNTERMODE_UP; HAL_TIM_Base_Init(&TIM3_Handle); HAL_NVIC_SetPriority(TIM3_IRQn, 6, 0); HAL_NVIC_EnableIRQ(TIM3_IRQn); if (HAL_TIM_Base_Start(&TIM3_Handle) != HAL_OK) { /* Starting Error */ } }
void encoder1_init(void) { GPIO_InitTypeDef gpio_init; gpio_init.Pin = ENC_PIN_CHA; gpio_init.Pull = GPIO_PULLUP; gpio_init.Speed = GPIO_SPEED_HIGH; gpio_init.Mode = GPIO_MODE_AF_PP; gpio_init.Alternate = GPIO_AF2_TIM3; __GPIOC_CLK_ENABLE(); HAL_GPIO_Init(ENC_GPIO, &gpio_init); gpio_init.Pin = ENC_PIN_CHB; HAL_GPIO_Init(ENC_GPIO, &gpio_init); __TIM3_CLK_ENABLE(); hTIM3.Instance = TIM3; // hTIM3.Init.Prescaler = (SystemCoreClock/65535) - 1; // 1s // hTIM3.Init.CounterMode = TIM_COUNTERMODE_UP; // hTIM3.Init.ClockDivision = 0; hTIM3.Init.Period = 65535; //hTIM3.Channel = HAL_TIM_ACTIVE_CHANNEL_1 | HAL_TIM_ACTIVE_CHANNEL_2; //HAL_TIM_Base_Init(&hTIM3); TIM_Encoder_InitTypeDef enc_init; enc_init.EncoderMode = TIM_ENCODERMODE_TI12; enc_init.IC1Polarity = TIM_INPUTCHANNELPOLARITY_RISING; enc_init.IC2Polarity = TIM_INPUTCHANNELPOLARITY_RISING; enc_init.IC1Selection = TIM_ICSELECTION_DIRECTTI; enc_init.IC2Selection = TIM_ICSELECTION_DIRECTTI; enc_init.IC1Prescaler = TIM_ICPSC_DIV1; enc_init.IC2Prescaler = TIM_ICPSC_DIV1; enc_init.IC1Filter = 0; enc_init.IC2Filter = 0; HAL_TIM_Encoder_Init(&hTIM3, &enc_init); __HAL_TIM_SetCounter(&hTIM3,0); HAL_TIM_Encoder_Start(&hTIM3, TIM_CHANNEL_1 | TIM_CHANNEL_2); }
void HAL_TIM_Base_MspInit(TIM_HandleTypeDef* htim_base) { GPIO_InitTypeDef GPIO_InitStruct; if(htim_base->Instance==TIM1) { __TIM1_CLK_ENABLE(); /**TIM1 GPIO Configuration PC0 ------> TIM1_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_TIM1; HAL_GPIO_Init(GPIOC, &GPIO_InitStruct); } if(htim_base->Instance==TIM3) __TIM3_CLK_ENABLE(); }
void HAL_TIM_Base_MspInit(TIM_HandleTypeDef* htim_base) { if(htim_base->Instance==TIM3) { /* USER CODE BEGIN TIM3_MspInit 0 */ /* USER CODE END TIM3_MspInit 0 */ /* Peripheral clock enable */ __TIM3_CLK_ENABLE(); /* Peripheral interrupt init*/ HAL_NVIC_SetPriority(TIM3_IRQn, 0, 0); HAL_NVIC_EnableIRQ(TIM3_IRQn); /* USER CODE BEGIN TIM3_MspInit 1 */ /* USER CODE END TIM3_MspInit 1 */ } }
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); }
void IMU::initializeTimerForUpdate(void) { /* TIMx Peripheral clock enable */ __TIM3_CLK_ENABLE(); const uint32_t CounterClk = 10000; //Hz const uint16_t OutputClk = 400; //Hz samplingFrequencyInHz = OutputClk; samplingPeriodInS = 1.0 / OutputClk; //Prescaler = ((SystemCoreClock/2) / TIM3 counter clock) - 1 const uint16_t Prescaler = (((SystemCoreClock / 2) / CounterClk) - 1); //ARR(TIM_Period) = (TIM3 counter clock / TIM3 output clock) - 1 const uint32_t Period = ((CounterClk / OutputClk) - 1); /* Set TIMx instance */ TimHandle.Instance = TIM3; TimHandle.Init.Period = Period; TimHandle.Init.Prescaler = Prescaler; TimHandle.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1; TimHandle.Init.CounterMode = TIM_COUNTERMODE_UP; TimHandle.Init.RepetitionCounter = 0; if (HAL_TIM_Base_Init(&TimHandle) != HAL_OK) { /* Initialization Error */ while (1) { }; } /* Enable the TIMx global Interrupt */ HAL_NVIC_EnableIRQ((IRQn_Type) TIM3_IRQn); /* Set Interrupt Group Priority */ HAL_NVIC_SetPriority((IRQn_Type) TIM3_IRQn, 1, 2); /* Start Channel1 */ if (HAL_TIM_Base_Start_IT(&TimHandle) != HAL_OK) { /* Starting Error */ while (1) { }; } __HAL_TIM_DISABLE(&TimHandle); }
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 }