AnsweredAssumed Answered

STM32L053 timer modul for waveform generation

Question asked by koch.fabian on Aug 12, 2015
Good morning everbody,

I am currently using a STM32L053C8 and I want to generate a square waveform. My APBx timer clock is 8MHz and can't be rised. Is it possible to generate a 8 MHz squrae waveform at this APBx timer clock or what is the maximum waveform frequency at this timer clock? I am currently only getting a 2 MHz square waveform with the clock and timer config below an I hope that I missed a prescaler or something.
void SystemClock_Config(void)
{
 
      RCC_OscInitTypeDef RCC_OscInitStruct;
      RCC_ClkInitTypeDef RCC_ClkInitStruct;
 
      __PWR_CLK_ENABLE();
 
      __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
 
      RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
      RCC_OscInitStruct.HSIState = RCC_HSI_ON;
      RCC_OscInitStruct.HSICalibrationValue = 16;
      RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
      HAL_RCC_OscConfig(&RCC_OscInitStruct);
 
      RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
                                  |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
      RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI;
      RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV2;
      RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
      RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;
      HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0);
 
      HAL_RCC_MCOConfig(RCC_MCO1, RCC_MCO1SOURCE_HSI, RCC_MCODIV_2);
 
      HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/8000);
 
      HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK_DIV8);
 
}
 
void ConfigTimerOut(void)
{
    GPIO_InitTypeDef GPIO_InitStruct;
    TIM_Base_InitTypeDef TIM_BaseInitStruct;
    TIM_OC_InitTypeDef TIM_OCInitStruct;
    TIM_HandleTypeDef  TIM_HandleStruct;
    TIM_ClockConfigTypeDef TIM_ClockConfigStruct;
    TIM_MasterConfigTypeDef TIM_MasterConfigStruct;
 
    __TIM21_CLK_ENABLE();
    __GPIOB_CLK_ENABLE();
 
    GPIO_InitStruct.Pin = GPIO_PIN_13;
    GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
    GPIO_InitStruct.Pull = GPIO_NOPULL;
    GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
    GPIO_InitStruct.Alternate = GPIO_AF6_TIM21;
    HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
 
    TIM_BaseInitStruct.ClockDivision = TIM_CLOCKDIVISION_DIV1;
    TIM_BaseInitStruct.CounterMode = TIM_COUNTERMODE_UP;
    TIM_BaseInitStruct.Period = 1;
    TIM_BaseInitStruct.Prescaler = 0;
 
    TIM_OCInitStruct.OCMode = TIM_OCMODE_TOGGLE;
    TIM_OCInitStruct.OCPolarity = TIM_OCPOLARITY_HIGH;
    TIM_OCInitStruct.OCFastMode = TIM_OCFAST_DISABLE;
    TIM_OCInitStruct.Pulse = 0;
 
    TIM_HandleStruct.Init = TIM_BaseInitStruct;
    TIM_HandleStruct.Instance = TIM21;
    TIM_HandleStruct.Channel = HAL_TIM_ACTIVE_CHANNEL_1;
 
 
    TIM_ClockConfigStruct.ClockPolarity = TIM_CLOCKPOLARITY_BOTHEDGE;
    TIM_ClockConfigStruct.ClockPrescaler = TIM_CLOCKPRESCALER_DIV1;
    TIM_ClockConfigStruct.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
 
 
    TIM_MasterConfigStruct.MasterOutputTrigger = TIM_TRGO_ENABLE;
    TIM_MasterConfigStruct.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
 
    HAL_TIM_Base_Init(&TIM_HandleStruct);
    HAL_TIM_OC_ConfigChannel(&TIM_HandleStruct,&TIM_OCInitStruct,TIM_CHANNEL_1);
    HAL_TIM_ConfigClockSource(&TIM_HandleStruct,&TIM_ClockConfigStruct);
    HAL_TIMEx_MasterConfigSynchronization(&TIM_HandleStruct,&TIM_MasterConfigStruct);
 
    TIM21->CCER |= TIM_CCER_CC1E; /* (5)*/
    TIM21->BDTR |= TIM_BDTR_MOE; /* (6) */
    TIM21->CR1 |= TIM_CR1_CEN; /* (7) */
}

Outcomes