STM32 PWM Shutdown (Break Input) – Stop PWM Safely

In this tutorial, we’ll discuss The STM32 PWM Break Input (Shutdown), how to configure and use the PWM Shutdown (Break Input) Feature in STM32 microcontrollers to safely stop the PWM outputs and put them into a predefined safe state (High or Low). We’ll test the STM32 PWM Stop Feature with a code example and a full test project. Without further ado, let’s get right into it!

Table of Contents

  1. STM32 PWM Shutdown (Break Input)
  2. STM32 PWM Break Input (Shutdown)
  3. STM32 PWM Break Input (Shutdown) Example
  4. Wrap Up

STM32 PWM Shutdown (Break Input)

Advanced Timers (TIM1 & TIM8) in STM32 microcontrollers have a “Break” safety feature. The purpose of the break function is to protect power switches (Transistors) driven by PWM signals generated by the timer peripherals. The two break inputs are usually connected to fault outputs of power stages, 3-phase inverters, smart gate drivers, etc.

When activated, the Break circuitry shuts down the PWM outputs and forces them to a predefined safe state. Several internal MCU events can also be selected to trigger an output shutdown.

The break features two channels:

  • Break channel that gathers both system-level faults (clock failure, parity error, etc) and application faults (from input pins and built-in comparator), and can force the outputs to a predefined level (either active or inactive) after a deadtime duration.
  • Break2 channel only includes application faults and is able to force the outputs to an inactive state.
❕ Note

External break events can also generate software interrupts so the CPU can handle such fault conditions (if needed).


STM32 PWM Break Input (Shutdown)

There are many sources for both of the PWM break channels (BRK & BRK2) either internal or external sources. In this section, we’ll list down all possible sources that could be used to generate a PWM break event.

Sources for the Break (BRK) channel are:

  • An external source connected to one of the BKIN pins (as per selection done in the AFIO controller), with polarity selection and optional digital filtering.
  • An internal source:
    • The Cortex-M4 LOCKUP output
    • The PVD output
    • The SRAM parity error signal
    • A flash ECC error
    • A clock failure event generated by the CSS detector
    • The output from a comparator, with polarity selection and optional digital filtering

Sources for Break2 (BRK2) are:

  • An external source connected to one of the BKIN pins (as per selection done in the AFIO controller), with polarity selection and optional digital filtering
  • An internal source coming from a comparator output

The PWM Break events can also be generated by software using BG and B2G bits in the TIMx_EGR register. The software break generation using BG and B2G is active whatever the BKE and BK2E enable bits values.


STM32 PWM Break Input (Shutdown) Example

In this example project, we’ll configure our STM32 microcontroller’s Timer1 to enable the 3-Phase Center-Aligned PWM outputs on channels CH1, CH2, and CH3. We’ll set the PWM output signal’s duty cycles to (25%, 50%, and 75% respectively), and the PWM frequency to 20kHz.

We’ll also enable the PWM Break input pin and connect it to an external push button to disable all PWM outputs upon activation. Clearing the break input detection flag and re-enabling/restarting the PWM output generation should resume the original operation. However, we’ll enable the AOE (Automatic Output Enable) within the break input configurations.

#1

Open STM32CubeMX, create a new project, and select the target microcontroller. For me, it’s (STM32F103C8T6 / BluePill)

#2

Configure the advanced-control timer (TIM1) as follows to enable the center-aligned mode 1 PWM outputs on channels (1, 2, and 3).

The PWM Configurations & Calculations For 3-Phase Center-Aligned Mode were explained in more detail in this tutorial, so you can refer back to it (if needed). It’ll be as follows:

  • PSC = 0, ARR = 1800 (20kHz)
  • PWM1 Pulse = 450 (25%)
  • PWM2 Pulse = 900 (50%)
  • PWM3 Pulse = 1350 (75%)
STM32 PWM Shutdown (Break Input) Example Configurations 1

We’ll also enable the PWM break input interrupt signal.

STM32 PWM Break Input Interrupt

I’ll connect this pin to a push button that shorts this pin to Vdd (HIGH) when pressed. Therefore, I’ll enable the internal Pull-Down resistor in the GPIO configurations.

STM32 PWM Break Input Pull Up-Down Configuration

#3

Go to the Clock configuration page and select a clock source to give you the maximum SysClk of 72MHz (in my case).

STM32 Clock Tree Configuration CubeMX

#4

Name & Generate The Project Initialization Code For CubeIDE or The IDE You’re Using.

STM32 3-Phase PWM Break Input Example Code

Here is The Application Code For This LAB (main.c)

❕ Note

Note that I’ve enabled the break event interrupt to capture the external break fault and handle it inside the ISR callback handler function (if needed). However, the HAL_TIMEx_BreakCallback() function is left blank in my code example in case you need to put some logic operations inside.

In the auto-generated MX_TIM1_Init() function, we need to manually enable the AOE feature (if needed) by editing the following line in that function:

STM32 3-Phase PWM Outputs + Break Input Example Testing

Here are the output signals on the Timer1 PWM Channels (1,2, and 3) pins as shown on my DSO. The 3-phase PWM signals at 20kHz (25%, 50%, and 75%) have duty cycles that are center-aligned and perfectly synchronized.

The Green trace shows the break input pin (connected to a push button). As you can see below, when the break input pin (green trace) goes HIGH, all the PWM outputs are immediately turned OFF (the IDLE state is configured as LOW).

STM32 PWM Break Input (Shutdown) Example

When the break pin goes LOW, the AOE feature allows the 3x PWM output channels to resume operation as normal. However, in most cases, you will need to do some safety checks inside the break input detection interrupt handler before re-enabling the PWM outputs.


Required Parts For STM32 Examples

All the example Code/LABs/Projects in this STM32 Series of Tutorials are done using the Dev boards & Electronic Parts Below:

QTY.Component NameAmazon.comAliExpresseBay
1STM32-F103 BluePill Board (ARM Cortex-M3 @ 72MHz)AmazonAliExpresseBay
1Nucleo-L432KC (ARM Cortex-M4 @ 80MHz)AmazonAliExpresseBay
1ST-Link V2 DebuggerAmazonAliExpresseBay
2BreadBoardAmazonAliExpresseBay
1LEDs KitAmazonAmazonAliExpresseBay
1Resistors KitAmazonAmazonAliExpresseBay
1Capacitors KitAmazonAmazonAliExpress & AliExpresseBay & eBay
1Jumper Wires PackAmazonAmazonAliExpress & AliExpresseBay & eBay
1Push ButtonsAmazonAmazonAliExpresseBay
1PotentiometersAmazonAliExpresseBay
1Micro USB CableAmazonAliExpresseBay

★ Check The Links Below For The Full Course Kit List & LAB Test Equipment Required For Debugging ★

Download Attachments

You can download all attachment files for this Article/Tutorial (project files, schematics, code, etc..) using the link below. Please consider supporting our work through the various support options listed in the link down below. Every small donation helps to keep this website up and running and ultimately supports the whole community.


Wrap Up

In conclusion, we’ve explored the STM32 PWM Break Function, how it works, and how to configure the STM32 advanced control timer to generate 3-Phase PWM signals with break input enabled to shut down all PWM outputs on break event and resume operations once the break condition is cleared (using the AOE).

You can build on top of the provided example code project and integrate it into your system. You can also check the rest of the tutorials in this series to learn more about other features of the STM32 PWM.

Share This Page With Your Network!
Join Our +25,000 Newsletter Subscribers!

Stay Updated With All New Content Releases. You Also Get Occasional FREE Coupon Codes For Courses & Other Stuff!

Photo of author
Author
Khaled Magdy
Embedded systems engineer with several years of experience in embedded software and hardware design. I work as an embedded SW engineer in the Automotive & e-Mobility industry. However, I still do Hardware design and SW development for DSP, Control Systems, Robotics, AI/ML, and other fields I'm passionate about.
I love reading, writing, creating projects, and teaching. A reader by day and a writer by night, it's my lifestyle. I believe that the combination of brilliant minds, bold ideas, and a complete disregard for what is possible, can and will change the world! I will be there when it happens, will you?

Leave a Comment