cancel
Showing results for 
Search instead for 
Did you mean: 

Configure a one pulse timer in cube MX

XBouc.1
Associate

Hi,

I am trying for a while now to configure a one pulse timer with cubeMX without success. I am using a nucleo-L433RC for my tests.

I was able to make the one pulse work by manually configure it. I used the example of the L432KC in the package to do so. The function HAL_TIM_OnePulse_ConfigChannel seems to never be called with the cubeMX configuration.

I followed several examples I found on the forum without success:

https://community.st.com/s/question/0D50X00009XkXgN/retriggerable-one-pulse-mode-in-cubemx

https://community.st.com/s/question/0D50X00009XkY3D/cubemx-and-one-pulse-mode

https://community.st.com/s/question/0D50X00009XkeN1/cubemx-support-of-one-pulse-modes-for-timers

https://community.st.com/s/question/0D50X00009XkXXV/tim15-pwm-ch1ch1n-one-pulse-mode-how-to-avoid-glitch-

https://community.st.com/s/question/0D50X0000BFDrGpSQL/one-pulse-mode-input-can-not-be-driven-high-enough

There is also an example with cubeMX on a F0 but the generated code from cubeMX in the example does not match the source code in the example. It seems to have been manually changed.

https://st--c.eu10.content.force.com/sfc/dist/version/download/?oid=00Db0000000YtG6&ids=0680X000006Hy4U&d=%2Fa%2F0X0000000b5K%2FY3RzMa9IjlnaAomMdkM5_hH.H4.fQUTzJCd0nOfjy9A&asPdf=false

I am lost at this point. Could you provide me information on how to properly configure cubeMX to have a one pulse timer working please?

Thank you!

1 REPLY 1
olavi
Associate III

I have spent a couple of days to solve a similar problem and was disappointed that the answers were not easily available in internet. The official ST documentation was quite cryptic and difficult to use. What I wanted was to learn

  • What are the setup steps in STM32CudeIDE Device Configuration Tool
  • What is the code I need to write in my application
  • How to avoid the initial delay in one pulse mode
  • How to have all 4 timer channels available
  • How to implement this without using GPIO to trigger the pulses

I did my investigations using a STM32H750 board running at 400 Mhz. Here are the answers for the last three questions:

  1. The initial delay can be avoided by using the retriggerable one pulse mode with down-counting
  2. In past I have been using TIM2, but the external trigger is conflicting with channel 1, leaving only 3 channels available for the pulses. Luckily the TIM5 shares the channel outputs with TIM2 and has a dedicated external trigger input.
  3. I didn't find a simple solution with internal trigger (to avoid the GPIO usage). I had to accept that a GPIO output is required to start the pulses. This is connected to the GPIO input that is used for the TIM5_ETR signal.

At the end, I was surprised how simple the solution was. In STM32CudeIDE Device Configuration Tool for TIM5, I entered the following parameters for a 20 MHz counter clock.

Slave Mode: Trigger Mode

Trigger Source: ETR1

Clock Source: Internal Clock

Channel1: Output Compare CH1

Channel2: Output Compare CH2

Channel3: Output Compare CH3

Channel4: Output Compare CH3

One Pulse Mode, checked

Counter Settings

Prescaler: 9

Counter Mode: Down

Counter Period: 0xffff

Internal Clock Division: No Division

auto-reload preload: Disable

Slave Mode Control: Trigger Mode

Trigger Output Parameters

Master/Slave Mode: Disable

Trigger Event Selection: Enable

Clear Input

Clear Input Source: Disable

Trigger

Trigger Polarity: non inverted

Trigger Prescaler: Prescaler not used

Trigger Filter: 0

Output Compare Channels (1..4)

Mode: Retriggerable OPM1

Pulse: 0

Output compare preload: Disable

CH Polarity: High

In the application code, I had to enable the channels before the main loop

TIM5->CCER |= 0x1111;

For the main loop, I had a function to update the CCRx registers and generate the trigger pulse with GPIO macros.

uint32_t ccr1 = pwm1 * T5_FREQ_MHZ;

uint32_t ccr2 = pwm2 * T5_FREQ_MHZ;

uint32_t ccr3 = pwm3 * T5_FREQ_MHZ;

uint32_t ccr4 = pwm4 * T5_FREQ_MHZ;

TIM5->CCR1 = ccr1; // Set PWM targets

TIM5->CCR2 = ccr2;

TIM5->CCR3 = ccr3;

TIM5->CCR4 = ccr4;

GPIO_set(TIM5_Start); // Generate trigger pulse

GPIO_clr(TIM5_Start);

The H750 supports the floating point values. The pwm1 .. pwm4 variable are floats as numbers in microseconds, such as 1000.0 and 2000.0 used for servo control. The T5_FREQ_MHZ is a constant (20).

In my application, I use PID controls with the following goals

  • The delay between process value reading and PID control execution is minimized
  • The delay between PID control execution and driving the control value is minimized

The timer is a good solution to minimize the CPU loading. If a faster or more precise control is required, then a software based fast protocol is used to drive the control value in few microseconds. The H750 has enough computing power for that unless the application uses large displays with fast update requirements.