cancel
Showing results for 
Search instead for 
Did you mean: 

Timer2 Configuration

DOSS FOF
Associate II
Posted on May 15, 2017 at 10:47

I'm working on STM32 nucleo432lkc. I'm trying to divide the MSI frequency for the timer by 50 but when I try this , i get nothing on the ouput of the pin. Below is the timer 2 code :

void TIM2_init(void)

{

      TIM_ClockConfigTypeDef sClockSourceConfig;

      TIM_MasterConfigTypeDef sMasterConfig;

    __TIM2_CLK_ENABLE();

    TIM_init_struct.Instance = TIM2;

    TIM_init_struct.Init.Prescaler = 0 ;//40000;

    TIM_init_struct.Init.CounterMode = TIM_COUNTERMODE_UP;

    TIM_init_struct.Init.Period = 0 ;//491;

    TIM_init_struct.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;

    TIM_init_struct.Init.RepetitionCounter = 0;

    //HAL_TIM_Base_Init(&TIM_init_struct);

    if(HAL_TIM_Base_Init(&TIM_init_struct)== HAL_OK)

      {

        /* Start the TIM time Base generation in interrupt mode */

        HAL_TIM_Base_Start_IT(&TIM_init_struct);

      }

    HAL_NVIC_SetPriority(TIM2_IRQn, 0, 1);

    HAL_NVIC_EnableIRQ(TIM2_IRQn);

     sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;

      if (HAL_TIM_ConfigClockSource(&TIM_init_struct, &sClockSourceConfig) != HAL_OK)

      {

          TIM_Error_Handler();

      }

      sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;

      sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;

      if (HAL_TIMEx_MasterConfigSynchronization(&TIM_init_struct, &sMasterConfig) != HAL_OK)

      {

          TIM_Error_Handler();

      }

}

void TIM2_IRQHandler()

{

    //}

     HAL_TIM_IRQHandler(&TIM_init_struct);

}

#include 'stdbool.h'

bool test = false;

void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)

{

    int timerValue =  HAL_RCC_GetSysClockFreq();

    if (test) {

        HAL_GPIO_WritePin(GPIOB, GPIO_PIN_0, GPIO_PIN_RESET);

        test = false;

    } else {

        HAL_GPIO_WritePin(GPIOB, GPIO_PIN_0, GPIO_PIN_SET);

        test = true;

    }

//    HAL_GPIO_TogglePin(GPIOB, GPIO_PIN_0);

    HAL_GPIO_TogglePin(GPIOB, GPIO_PIN_3);

}

10 REPLIES 10
RomainR.
ST Employee
Posted on May 15, 2017 at 17:08

Hello,

It would seem in your code that you do not configure the value of your timer period?

TIM_init_struct.Init.Period = 0; // 491;�?

What is your requirement in your application for the interruptions period?

Could you verify ?

Best regards

To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.

Posted on May 16, 2017 at 09:17

Hello Romain,

In fact I forgot and pasted previous version. But the real values were this:

    TIM_init_struct.Init.Prescaler = 1 ;

    TIM_init_struct.Init.CounterMode = TIM_COUNTERMODE_UP;

    TIM_init_struct.Init.Period = 50 ;

    TIM_init_struct.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;

    TIM_init_struct.Init.RepetitionCounter = 0;

The clock frequency is 38.4 MHz and tried to divided by  2x51 in that way. But when I run the programme 

, I trully get the call back function 'void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)',

but the GPIO didn't work. In contrast I realized that another function inside the callback works.

That is the GPIO intialization :

         GPIO_InitTypeDef GPIO_init_struct;

        GPIO_init_struct.Pin = GPIO_PIN_3 |GPIO_PIN_7;

        GPIO_init_struct.Mode = GPIO_MODE_OUTPUT_PP;

        GPIO_init_struct.Pull = GPIO_NOPULL;

        GPIO_init_struct.Speed =  GPIO_SPEED_FREQ_HIGH 

        HAL_GPIO_Init(GPIOB, &GPIO_init_struct);

Notice: the values greater than 100 (TIM_init_struct.Init.Period = 50) work.

Posted on May 17, 2017 at 16:53

Hello,

Assuming your Timer 2 input clock is 4MHz, this means that with the following settings:

TIM_init_struct.Init.Prescaler = 1;
TIM_init_struct.Init.Period = 50;�?�?

The counting resolution of your timer is about 26ns.

With a value of period = 50, you will get an overflow interrupt every 1.3μs (26ns x 50) For a value of period = 100, you will get an interrupt every 2.6μs.

period = (Timer Clock frequency / Timer prescaler x period frequency) - 1

How do you visualize the GPIO that is supposed to make the toggle in the Timer Callback function with such a fast period of 1.3μs? If you use an oscilloscope you should be able to see the change of state of this output?

Again, I ask you the same question, what are your specificity in your application for Timer 2 interrupt point of view ?

You should fix this point and then configure your timer according.

Regards

To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.

Posted on May 17, 2017 at 21:21

Hello,

I'm beginner in programming stm32. In fact I have an external device which contains 4 Digital to analog converter, it communic via SPI. And I'm trying to genarate signals simultanously on the 4 DAC output at 48 KHz.

As I Charge the 4 DACs register via SPI , I need to call HAL_SPI_Transmit () each (T = 1/48 KHz ) mean while the SPI must be 4 times faster. So my goal is to use SPI with timers or other mean in order to generate interruption at (T = 1/48 KHz ) and transmit data and so on.

regards

Posted on May 18, 2017 at 09:08

Seydouba,

Ok better understand your application scope now.

In general, the periph SPI can operate at the maximum speed of PCLK2 / 2. For the L4 family it means 40MHz (if you have a SYSCLK = 80MHz)

In your case, you need to transmit by SPI 4 values for I imagine the generation of analog signals at fs = 48kHz. So your SPI should not exceed the 2Mhz band.

However, I do not know which DAC device you are using but you need to consult its datasheet in order to respect its maximum SPI rate.

So you have the choice of your maximum clock speed for the SYSCLK - HCLK and therefore PCLKx.

You can configure your project with: MSI 8 Mhz AHB Prescaler = 1 APB2 Prescaler = 2 HCLK = 8Mhz and PCLK2 = 4Mhz (there is another prescaler / 2 internal to the SPI which will bring this clock back to 2MHz)

Finally here is your Timer configuration that will give you the 48kHz time base this way:

TIM_init_struct.Init.Prescaler = 1; // PCLK1 = 8 MHz no need to divise it
TIM_init_struct.Init.Period = (8000000 / 48000) -1;// Timer2 resolution is 125ns 

Next, I can advise you to use the SPI in DMA (memory to peripheral) mode with buffers that contain your data to be transmitted to your external DAC by the SPI.

You will find examples in the Cube as: \ Projects \ STM32L432KC-Nucleo \ Templates \ SPI \ SPI_FullDuplex_ComDMA.

Regrads

To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.

Posted on May 18, 2017 at 16:41

I did as you advised me but I have got fs = 2.46 Khz for DACS. But With GPIO the pin speed is 23.8 Khz. Below you have clock config

{RCC_ClkInitTypeDef Clk_init_struct;

    Clk_init_struct.ClockType =  RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK

                                |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;

    Clk_init_struct.SYSCLKSource =  RCC_SYSCLKSOURCE_MSI ; //RCC_SYSCLKSOURCE_PLLCLK;

    Clk_init_struct.AHBCLKDivider = RCC_SYSCLK_DIV1  ;

    Clk_init_struct.APB1CLKDivider =  RCC_HCLK_DIV1  ;

    Clk_init_struct.APB2CLKDivider = RCC_HCLK_DIV2  ;

    if (HAL_OK != HAL_RCC_ClockConfig(&Clk_init_struct, FLASH_LATENCY_2)) {

                    RCC_Error_Handler();

                }

};

void OscConfig(void)

{RCC_OscInitTypeDef Osc_init_struct;

//RCC_PLLInitTypeDef PLL_init_struct;

    Osc_init_struct.OscillatorType = RCC_OSCILLATORTYPE_MSI ;

        Osc_init_struct.HSEState = RCC_HSE_OFF ;

        Osc_init_struct.HSI48State = RCC_HSI48_OFF;

        Osc_init_struct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT ;

        Osc_init_struct.HSIState = RCC_HSI_OFF;

        Osc_init_struct.LSEState = RCC_LSE_OFF  ;

        Osc_init_struct.LSIState = RCC_LSI_OFF ;

        Osc_init_struct.MSICalibrationValue = 0;

        Osc_init_struct.MSIClockRange =RCC_MSIRANGE_7;

        Osc_init_struct.MSIState = RCC_MSI_ON ;

        Osc_init_struct.PLL.PLLState = RCC_PLL_ON;

        Osc_init_struct.PLL.PLLM =3;

        Osc_init_struct.PLL.PLLN = 10;

        Osc_init_struct.PLL.PLLP = RCC_PLLP_DIV4;

        Osc_init_struct.PLL.PLLQ = RCC_PLLQ_DIV4;

        Osc_init_struct.PLL.PLLSource =RCC_PLLSOURCE_MSI;

        Osc_init_struct.PLL.PLLR = RCC_PLLR_DIV2;
DOSS FOF
Associate II
Posted on May 19, 2017 at 10:29

Hello Romain,

Another notice , I have managed to generate 48KHz for GPIO and 4.8 KHz for the DACs. Whatever the SPI configuration (Baudrate ) I always have this differences : Dacs signal 10 times lower than Timer interruption. I don't understand this problem. 

Dac maximum speed is 50MHz in the datasheet. Concerning SPI_DMA I have tried it but no success, may be the configuaration is not well done. I will bring the code next time

Thanks

Posted on May 19, 2017 at 11:04

The original post was too long to process during our migration. Please click on the provided URL to read the original post. https://st--c.eu10.content.force.com/sfc/dist/version/download/?oid=00Db0000000YtG6&ids=0680X000006I6sE&d=%2Fa%2F0X0000000bx3%2FV1JY9.rDQ0.kkr5U_kYYanpWThlyVIRwNvNpK_VG_vg&asPdf=false
Posted on May 22, 2017 at 15:09

Here is your clock configuration. Strange !!!!

RCC_MSIRANGE_7 = 8MHz and PLLM dividor = 3. Which mean PLL input frequency is equal to 2.667MHz ?

It's not good to have PLL input frequency < 4. Range should be 4 to 16MHz (look into Datasheet > Electrical Charac > PLL)

With PLL config, SYSCLK = 13.333MHz

Not easy to have correct Timer interrupt with this settings !

I suggest you study the datasheet and reference manual before.

0690X000006071zQAA.png

To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.