The next system of the STM32G4 I'm taking on is the Timer system. And no, it's not just because I get to see my name all over the place...
This chip has a whole host of different types of timers: 3x advanced-control timers TIM1/8/20, 4x general-purpose timers TIM2/3/4/5, 3x general-purpose timers TIM15/16/17 (huh? must be cut-down versions), 2x basic timers TIM6/7, a low-power timer LPTIM, and a high-resolution timer HRTIM that is actually seven timers in one: master, and 6x slaves. Not sure exactly what master/slave relationship is just yet.
I've still got to figure out things like duty cycle, frequency, and syncing the ADC to the dead-time, but so far so good.
|Dead simple amirite?|
I got a pretty good head start understanding timers from this video by Eddie Amaya. In short, they're 16-bit counters that count up or down based on the peripheral clock divided by some prescalar. When the counter reaches the value set in the TIMx_ARR register it does... something; fires an interrupt, toggles a GPIO, etc. Then when the timer overflows, it resets back to 0 (in the case of the GPIO it toggles it back on again). Apparently they can be tied to DMA reqeusts too, according to p1101 of the datasheet.
I'm particularly interested in the center-aligned timing modes that allow for up-down counting, which will be super useful for current sampling during BLDC commutation (more on that project in a later post).
|H gates for the top 3 traces, and a spike indicating current sampling from Ben Katz's blog.|
But since I'm still new to timers I figure I'll just try and get a PWM signal out first.
STM's timer's are ... weird? After reading about them for the better part of 3 days, I started trying to get a simple PWM signal up and running. Took me the better part of another day to figure out how to enable them (grrr... CubeMX's generated code is actually flat out wrong). Aaaand then about 10 minutes and as many lines of code to get three center-aligned PWM pulses synchronized?!
|That was alarmingly easy...|