The question is old but I was wondering the same and started digging into the topic.
In an STM32 micro, not all timers implement every function. My post is based on the STM32F030 Timer 1, which is I believe the most feature packed 4 channel implementation (or at least it was a few years ago).
The timer basically acts as a counter, with various options to where the counter clock comes from, what resets it, what the period is and the counter direction. This is the base for all extra features it implements, the HAL driver refers to it as Time Base. By itself, the counter function is not related to the timer channels.
There are 1-6 channels implemented in STM32 timers. These channels can be configured either independently, or in some case, in pairs (for functions like quadrature encoder mode). A channel can be configured as either input capture, or output compare. The input capture "listens to" some event and saves the counter of the time base in the CCRx registers. The output compare compares the counter register to a set value, given in the CCRx registers.
All the simple IC/OC modes and the extra functions, like PWM input, output, Hall effect sensor interfacing, are built on top of these two modes, their respective option bits act on the various input/output multiplexers and, regarding to OC mode basically tell the hardware what action to take based on the comparator outputs (CNT = CCRx, CNT > CCRx). In this case, PWM mode lets the output mode controller return to the CNT <= CCRx state when the counter register resets (more specifically, the update event), where as in the other OC modes the output mode controller ignores this signal and can be reset by hand, or by an external signal. The output signal is the OCxREF signal which then passes through some more hardware before it gets to the output pins. If it gets output at all, because you are allowed to not connect the timer to the output pins.
The STM32 timers are complicated. They have many logical blocks and a ton of configuration registers/bits so I may have missed something or completely misread something. Please feel free to correct me.
Answer from Buga Dániel on Stack OverflowThe question is old but I was wondering the same and started digging into the topic.
In an STM32 micro, not all timers implement every function. My post is based on the STM32F030 Timer 1, which is I believe the most feature packed 4 channel implementation (or at least it was a few years ago).
The timer basically acts as a counter, with various options to where the counter clock comes from, what resets it, what the period is and the counter direction. This is the base for all extra features it implements, the HAL driver refers to it as Time Base. By itself, the counter function is not related to the timer channels.
There are 1-6 channels implemented in STM32 timers. These channels can be configured either independently, or in some case, in pairs (for functions like quadrature encoder mode). A channel can be configured as either input capture, or output compare. The input capture "listens to" some event and saves the counter of the time base in the CCRx registers. The output compare compares the counter register to a set value, given in the CCRx registers.
All the simple IC/OC modes and the extra functions, like PWM input, output, Hall effect sensor interfacing, are built on top of these two modes, their respective option bits act on the various input/output multiplexers and, regarding to OC mode basically tell the hardware what action to take based on the comparator outputs (CNT = CCRx, CNT > CCRx). In this case, PWM mode lets the output mode controller return to the CNT <= CCRx state when the counter register resets (more specifically, the update event), where as in the other OC modes the output mode controller ignores this signal and can be reset by hand, or by an external signal. The output signal is the OCxREF signal which then passes through some more hardware before it gets to the output pins. If it gets output at all, because you are allowed to not connect the timer to the output pins.
The STM32 timers are complicated. They have many logical blocks and a ton of configuration registers/bits so I may have missed something or completely misread something. Please feel free to correct me.
PWM modes: Output changes continually based on comparison of counter value and compare register.
-PWM mode 1: (110) if TIMx_CNT < TIMx_CCR1 output is LOW else HIGH
-PWM mode 2: (111) if TIMx_CNT < TIMx_CCR1 output is HIGH else LOW
Output pin state changes two times for one update event, first when CNT == CCR and second when ARR overflows (update event) and is set to back zero (in upcounting). Setting the ARR controls period and setting CCR controls the duty cycle.
OC modes: Output is only changed when counter is equal to compare register
-Toggle on match: (011)
-Set active (high) on match: (001)
-Set inactive (low) on match: (010)
Output pin state changes only when CNT == CCR. The overflow event does not change the pin state.
You can technically generate PWM by using OC mode, but it very impractical. OC mode is more suitable for generating various shape pulses other than PWM.
There is only one "output" block of the timer which can be configured to both modes. There is a lot of other settings (polarity, deadtime, output disconnect) you can find in Reference Manual.
Hint: Let Cube MX and HAL do the dirty work of configuring the timer clock tree and pin assignments (function like MX_TIM8_Init()). Than control the timer and do what you want by directly setting the bits in the timer control registers. It is easier and faster to understand how to work with timer by reading few reference manual pages than trying to understand multiple layers of HAL functions and Cube MX settings. Moreover, you may need to change setting of the timer during the program run, like reconfigure from PWM to OC mode, or generate only one pulse some times.