STM32 Timer DMA Explained

In the previous article, STM32 Timer Synchronization and Trigger Modes Explained, one timer started, stopped, and reset another timer in hardware, and the same trigger signals were also used to start ADC conversions. A timer-triggered ADC produces a result at every trigger, and something still has to collect each result before the next one arrives. A timer can also do more than send a trigger signal, because it can send a request to the DMA controller, and the DMA controller then moves one piece of data without any work from the CPU.

In this article, we will first look at the reason for combining a timer with DMA and at the roles that the timer and the DMA controller have in this combination. After that, we will follow the path of a request from the timer to the destination and look at the most common applications. We will then configure timer DMA in STM32CubeMX and build a small example on the NUCLEO-F446RE board, where TIM2 changes its own PWM duty cycle from a buffer. The article focuses on the timer side of the connection, so it does not explain the general features of the DMA controller. This article is part of the STM32 timer tutorial series.

Why Use DMA with a Timer

Many timer applications repeat the same small action at a fixed rate. A PWM output may need a new duty cycle in every period, a capture channel may need its result stored after every edge, and a waveform generator may need the next output value at every update event. The usual way to handle this work is a timer interrupt, where the CPU stops what it is doing, runs a short handler, copies one value, and returns. This approach is simple and works well at low rates, which is the reason why the STM32 Timer Interrupts Explained article uses it for most examples.

The cost of an interrupt becomes visible when the rate grows. Every event needs the interrupt entry and exit, the register saves, and the handler code. This adds up to a noticeable part of the CPU time when the events arrive every few microseconds. The timing of the data also depends on the interrupt latency, which changes whenever another interrupt of the same or higher priority is active. DMA removes both problems, because the transfer is carried out by a separate hardware unit that runs next to the CPU and has a fixed and short reaction time.

It is important to separate the two roles in this combination. The timer only generates the event and turns it into a DMA request, and it does not move any data by itself. The DMA controller receives the request and performs the transfer that was configured in advance. It means that it reads from a source address, writes to a destination address, and updates its own counters. The CPU is involved only at the beginning, when it configures both units, and at the end, when it processes the finished buffer or starts a new transfer.


How Timer-Triggered DMA Works

A timer can produce a DMA request from several of its events. The two groups that are used most often are the update event and the capture/compare events. The update event happens when the counter overflows or underflows, or when the update is generated by software, so it occurs once per timer period. A capture/compare event happens when the counter matches the value of a channel register in output mode, or when an edge is captured in input mode, and every channel has its own event. Some timers also offer a request for the trigger input and for the commutation event, but these are less common.

Each request source has an enable bit in the DMA/interrupt enable register (DIER) of the timer. The update request uses the UDE bit, and the capture/compare requests use the CC1DE to CC4DE bits. These bits are separate from the interrupt enable bits UIE and CC1IE to CC4IE, so a timer can raise an interrupt, a DMA request, both, or neither for the same event. In HAL, the functions with the DMA suffix, such as HAL_TIM_PWM_Start_DMA(), set the correct enable bit for you, and the macro __HAL_TIM_ENABLE_DMA() does the same job when a project uses a register-level or low-level approach.

The request does not go directly to the memory. It travels to the DMA controller, which has to know that this specific timer event belongs to one of its transfer channels. This assignment is fixed in hardware on many families, so a given timer event can only be served by one or two specific DMA channels or streams. Other families add a request multiplexer called DMAMUX, which lets the firmware select the request source for each channel freely. The same event is therefore handled in different ways on different devices, and the mapping always has to be checked in the reference manual of the chosen MCU.

Request path from a timer event through a DMA request and the DMA controller to a timer register, GPIO, or memory

Timer DMA Applications

The applications in this section differ in the direction of the data and in the timer event that is used. In every case, the timer sets the pace and the DMA controller moves the data, and the examples are kept close to the timers and the DMA controllers of the STM32F446 that is used in the rest of the article. On this device, the timers TIM2 to TIM7 are served by DMA1, while the advanced timers TIM1 and TIM8 are served by DMA2.

Three panels showing a buffer feeding a timer register, a capture register filling a buffer, and a repeating PWM waveform

Update Timer Registers or Output Data from a Buffer

The most common use is to write a sequence of values from memory into a register of the timer, so that the timer output changes in every period without a single interrupt. When the destination is a capture/compare register (CCR1 in the picture above), the buffer holds a list of duty cycles, and each value is written during one PWM period. This is how a smooth LED fade, a sine-weighted motor drive, or a servo motion profile can run entirely in hardware. The example later in this article uses exactly this method.

A second variation writes to a register that does not belong to the timer. The timer update event can request a transfer that copies a value from a buffer to the output data register of a GPIO port, which creates a parallel pattern on several pins at a fixed rate. Timers that serve as a sample clock for a DAC use the same idea, because the update event requests the next sample for the converter. Some timers also have a DMA burst feature, which writes several consecutive timer registers in a single request, so a complete set of values such as the auto-reload value and the compare values can change at the same update event.


Transfer Captured Measurements

In input capture mode, the timer copies the counter value into a capture register at every selected edge, and the capture event can request DMA. The DMA controller then moves each captured value into a buffer before the next edge overwrites it. This is important when the input pulse’s frequency is very high, because an interrupt handler may be too slow and a missed capture shows up as a wrong period. After the buffer is full, the CPU subtracts neighbouring values to get the periods or the pulse widths. The settings of the capture channel are the same as in STM32 Input Capture Mode Explained, and only the transfer of the result changes.


Support Repeated Waveform or Signal Generation

A circular buffer in combination with a timer gives a waveform that repeats without any attention from the CPU. After the last element has been sent, the DMA controller returns to the first element automatically, and the waveform continues for as long as the timer runs. The buffer can hold the duty cycles of a sine wave, the compare values of a stepper motor pattern, or the pulse widths that encode the bits of a serial protocol such as the one used by addressable RGB LEDs. A new waveform is created by writing different values into the buffer, which does not need any change to the timer or to the DMA settings.


Configure Timer DMA in CubeMX

The configuration of timer DMA in STM32CubeMX has two parts. The timer itself is configured as usual for its job, and the DMA request is then added in a separate tab of the timer settings. If you need a reminder of the tool, the article STM32CubeMX Explained describes the main areas of the interface.

Enable the Timer DMA Request

Select the timer, open the Configuration view, and switch to the DMA Settings tab. Click Add, and open the list of requests, which contains the events that this timer can send to DMA on the selected device. Choose the event that fits the application, such as TIM2_CH1 for a compare event of channel 1 or TIM2_UP for the update event. CubeMX then assigns a stream and a channel automatically on devices that use fixed mapping, and it assigns a DMAMUX request on devices that have a multiplexer.

STM32CubeMX TIM2 DMA Settings tab with the TIM2_CH1 request added on DMA1 Stream 5

The table shows the assignments that are used in this article, as listed in the reference manual of the STM32F446 (RM0390). Please confirm the values in your own document, because this mapping is the first thing to check when a transfer does not start.

Timer eventDMA controllerStreamChannel
TIM2_CH1DMA1Stream 5Channel 3
TIM2_UPDMA1Stream 1 or Stream 7Channel 3
TIM3_CH1DMA1Stream 4Channel 5
TIM3_UPDMA1Stream 2Channel 5

Set Direction, Width, Increment, and Priority

After the request is added, the same tab shows the DMA parameters. The Direction defines which side is the source. It is set to Memory To Peripheral when a buffer feeds a timer register, and to Peripheral To Memory when captures are stored in a buffer. The Increment Address option decides which address moves after each transfer. The memory side is normally incremented, so every transfer takes the next element, and the peripheral side is not incremented, because the destination or source is a single fixed register.

STM32CubeMX DMA request settings showing direction, increment address, data width, mode, and priority

The Data Width is the setting that causes most of the errors in practice, and it has to match the size of the buffer elements as well as the size of the register that is accessed. A buffer of uint32_t values uses the word width on both sides, and a buffer of uint16_t values uses the half-word width. The Priority only matters when several DMA channels of the same controller are active at the same time, so the default value of Low is correct for a single timer transfer. A project with several streams should give the stream with the tightest timing a higher priority.


Circular and Normal Mode

The Mode setting decides what happens after the last element has been transferred. In Normal mode, the DMA controller stops, and the transfer has to be started again by the firmware, which is suitable for a single sequence such as one burst of captured values. In Circular mode, the controller reloads its counter and continues from the first element, which is the right choice for continuous waveform output and for a capture stream that is processed in halves. In circular mode, the half-transfer and the transfer-complete callbacks are the tool for working with the buffer while the DMA controller keeps running.


STM32 Timer DMA Example

In this example, we will use the TIM2 on the NUCLEO-F446RE to drive the green LED LD2 on pin PA5 with a PWM signal. We will also add a DMA transfer to write a new duty cycle into the compare register of channel 1 in every PWM period. The buffer holds 1000 values that follow a smooth curve, and the PWM frequency is 1 kHz, so one pass through the buffer takes exactly one second. The LED therefore fades up and down once per second while the CPU runs an empty main loop. The timer clock is 90 MHz in this example, as in the earlier articles, and the prescaler values change if your clock tree differs.

Configure the Timer

STM32CubeMX TIM2 parameter settings with prescaler 89, counter period 999, and auto-reload preload enabled
  • Select TIM2 and keep the Clock Source on Internal Clock. Click the pin PA5 and set its function to TIM2_CH1, and then set Channel1 to PWM Generation CH1.
  • In the Parameter Settings tab, set the Prescaler to 89 and the Counter Period to 999, which gives a 1 MHz counter clock and a 1 kHz PWM frequency, as explained in STM32 PWM Mode Explained.
  • Enable auto-reload preload and set the Pulse to 0. The channel preload setting is enabled by default in the PWM mode, and it should stay enabled, because it makes every new compare value take effect at the next update event and not in the middle of a period.

STM32CubeMX TIM2_CH1 DMA configuration with Memory To Peripheral, circular mode, memory increment, and word data width

Add the request TIM2_CH1 in the DMA Settings tab with the options from the previous section, which are Memory To Peripheral, Circular mode, memory increment enabled, peripheral increment disabled, and Word width on both sides. The Word width is correct here because TIM2 is a 32-bit timer on this device, and the buffer elements are 32-bit as well. CubeMX also enables the interrupt of DMA1 Stream 5 automatically, which is needed for the callbacks.

The generated function MX_TIM2_Init() contains the lines below, and the call to HAL_TIM_PWM_ConfigChannel() at the end sets up channel 1 in PWM mode with the pulse value of 0.


Prepare the Buffer

The buffer is declared as a static global array, so it exists for the whole run of the program. It is filled before the transfer starts, and the values are calculated with a raised cosine, which starts at 0, rises to the auto-reload value of 999, and returns to 0 at the end of the buffer. The array type is uint32_t, because the HAL function for the PWM transfer takes a pointer to 32-bit data on the STM32F4 family.

In the user code section of main(), the buffer is filled in a loop.


Start the Transfer

The function HAL_TIM_PWM_Start_DMA() enables the PWM channel, enables the DMA request of channel 1, starts the DMA controller, and starts the counter. The last parameter is the number of elements to transfer, so the value is LUT_SIZE.

The compare event of channel 1 happens once in every PWM period. At each event, the timer requests one transfer, and the DMA controller copies the next element of duty_lut into the compare register CCR1. The new value is stored in the preload register and becomes active at the next update event, so the duty cycle changes in steps of one PWM period without any CPU involvement.


Use the Completion and Error Callbacks

HAL reports the state of the DMA transfer through callbacks, which are implemented in the user code section at the end of main.c. The half-transfer callback is called when the first 500 elements have been moved, and the transfer-complete callback is called when the last element has been moved. The error callback is called when the DMA controller reports a transfer error.

When the project runs, the LED fades up and down once per second, and the two counters increase by one every second in the debugger. If you change the DMA mode to Normal, the transfer stops after one pass, and the callbacks are called only once. In that case, the firmware can call HAL_TIM_PWM_Stop_DMA() in the transfer-complete callback or start the next transfer from there.


Buffer Lifetime and the Meaning of Completion

The buffer must stay valid for as long as the DMA transfer is active, because the DMA controller reads it directly and does not keep a copy. A global or static array satisfies this condition. A local array inside a function does not, because its memory is released when the function returns, and the DMA controller would then read stack data that other code has already overwritten. The same rule applies when the buffer is changed while the transfer runs, since a value that is being written by the CPU can be read by the DMA controller at the same moment, and this is the reason to modify only the half of the buffer that the controller has just finished.

The second point is the difference between the DMA completion and the timer event. The transfer-complete callback means that the last element has been written to the compare register, and it does not mean that the output has already shown the last duty cycle. Because of the preload register, the last value becomes active one update event later. In the same way, a timer event does not mean that a transfer has finished, because the timer only requests the transfer, and the DMA controller needs a short time to carry it out. At the rates used in this example, the delay is negligible, but it can be visible when the timer period is only a few clock cycles long.

Circular DMA buffer of 1000 elements with the half-transfer callback after element 500 and the transfer-complete callback at the end

Timer DMA Problems

Wrong DMA Request, Channel, Stream, or DMAMUX Request

The most frequent problem is a DMA configuration that does not match the timer event. On devices with fixed mapping, a timer event only works with the stream and channel that the reference manual assigns to it, and a wrong selection means that the request never reaches the transfer. A conflict is also possible when two timer events share a stream, because on the STM32F446 the update request of TIM2 and its channel 3 request use the same stream. The firmware cannot use both at the same time, and CubeMX shows a conflict that has to be solved by moving one of them to another available stream. On devices with DMAMUX, the equivalent mistake is the selection of the wrong request ID for the channel.


Incorrect Data Width or Memory Increment

A width that does not match the buffer type produces values that look random or shifted. If a buffer of uint32_t values is transferred with a half-word width, the controller reads only half of every element and moves through the array at the wrong speed. A missing memory increment is easier to recognize, because the output then repeats the first value of the buffer forever, since the controller reads the same address in every transfer. The opposite error, which is an enabled peripheral increment, moves the destination address through the timer registers and corrupts neighboring registers, so the peripheral increment has to stay disabled for a single register.


Buffer Scope and Lifetime Errors

A buffer that is declared as a local variable, or one that is modified while the transfer is active, gives results that change from one run to the next. This kind of error is difficult to find, because the first values often look correct and the damage appears later when the stack memory is reused. The buffer should be global or static, and the firmware should only change the half that the controller is not currently using. On devices with a data cache, such as the STM32F7 and the STM32H7, the buffer also has to be handled with cache maintenance or placed in a non-cacheable memory region.


Confusing a Timer Interrupt with a DMA Request

The update interrupt and the update DMA request are two separate paths with separate enable bits, and enabling one of them does not enable the other. A project that calls HAL_TIM_Base_Start_IT() and expects data to move has only enabled the interrupt, and a project that calls the DMA start function has not enabled the interrupt, so the period-elapsed callback is not called for every event. The callbacks of a DMA transfer report the progress of the transfer and not the individual timer events, because they are called after half of the buffer and after the whole buffer, and not after every single element. Another detail applies to compare requests, because a value in the buffer that is larger than the auto-reload value never produces a compare match, and the next request does not happen in that period.


Assuming the DMA Mapping Is Identical Across STM32 Families

A mapping that works on one family cannot be copied to another family without checking. The STM32F4 devices use fixed stream and channel pairs, many newer families use DMAMUX, and the controller that serves a timer can change between devices of the same family. The HAL function names are the same, but the CubeMX settings, the generated DMA structure, and the available options are different, so a project should be recreated with the settings of the actual device. The reference manual of the selected MCU is the final source for the request table, and the safest approach is to start from the table before the DMA settings are chosen. The article STM32 Timer Types Explained lists which timers exist on each kind of device and helps to find the right group.


Conclusion

Timer DMA connects a timer event to a data transfer that runs without the CPU. The timer generates a request from its update event or from a capture/compare event, and the DMA controller moves one element for every request, either from memory to a timer register or from a capture register to memory. This combination is the basis of buffered PWM, of captured measurements that are stored without interrupts, and of repeating waveforms.

For a reliable setup, look up the request in the reference manual first, then choose the direction, data width, increment, and mode so that they match the buffer and the register, and keep the buffer valid for the whole transfer. The completion callbacks report the progress of the DMA transfer and not the individual timer events, and the preload registers add one more period before a transferred value reaches the output. In the next articles of the series, we will build on these ideas with further timer applications.


Download the Project Here

Download the CubeMX project files and HAL source code โ€” all tested on real hardware.

If you find this useful, you can support us with a Donation as well.

Article by Yashmiti

Yashmiti is a technical writer at STM32Guide, where she creates tutorials and guides that help embedded-systems learners and developers work confidently with STM32 microcontrollers. Her writing covers tools like STM32CubeIDE and STM32CubeMX, along with peripherals and communication protocols โ€” always with an emphasis on clear explanations and hands-on examples that take readers from concept to working project.

She's also part of the effort to build STM32Guide into an organized, easy-to-navigate resource for the STM32 community.

Leave a Comment