STM32 NUCLEO F767ZI PWM

Discussion and questions about boards that can run MicroPython but don't have a dedicated forum.
Target audience: Everyone interested in running MicroPython on other hardware.
Post Reply
GizmoGear
Posts: 2
Joined: Thu Feb 03, 2022 6:33 pm

STM32 NUCLEO F767ZI PWM

Post by GizmoGear » Thu Feb 03, 2022 7:15 pm

I'm currently attempting to drive a servo motor using a STM32 NUCLEO F767ZI. According to the Micropython docs, there should be a PWM class within the machine module. I've even seen mention of a Servo class under the pyb module. By running dir(machine) and dir(pyb), I don't seem to have either on my build of Micropython.

I've looked through the docs and found where some ports should have the PWM class, whereas other ports use the Timer class to accomplish PWM.

Is the PWM class the universal solution for PWM use, or would Timer be used to generate PWM for STM32?

I'm running Version: 0221 Build: Jun 23 2017 17:43:45.

Asensio12
Posts: 6
Joined: Thu Jan 13, 2022 3:33 pm

Re: STM32 NUCLEO F767ZI PWM

Post by Asensio12 » Fri Feb 11, 2022 4:59 pm

This is what has worked for me in the past on STM32 L4s and F0s:

Code: Select all

import pyb

timer2 = pyb.Timer(2, freq=1000)
ch1 = timer2.channel(1, pyb.Timer.PWM, pin=pyb.Pin.board.PA0, pulse_width_percent=50)
Note that, the information about which pins are connected to each combination of Timer+Channel is available on the MCU data sheet (see the alternate function table), and the Alternate function CSV on github (ports / stm32 / boards / [your MCU])
Example:

Code: Select all

# PA0  TIM2_CH1
# PA1  TIM2_CH2
# PA2  TIM2_CH3
# PA3  TIM2_CH4

GizmoGear
Posts: 2
Joined: Thu Feb 03, 2022 6:33 pm

Re: STM32 NUCLEO F767ZI PWM

Post by GizmoGear » Fri Mar 04, 2022 10:10 pm

Thank you very much, Asensio12! With your help, I was able to get PWM working! I've noticed that any pin that is tied to a particular timer/channel pair initialize when that command is executed.

For example:

Code: Select all

timer2 = pyb.Timer(2, freq=1000)
ch1 = timer2.channel(1, pyb.Timer.PWM, pin=pyb.Pin.board.D13, pulse_width_percent=50)
This will generate PWM output from pin D13/PA_5 at a 50% duty cycle, but I observed the same output from pins PA_15 and PA_0 as well. I was able to run 'pyb.Pin.board.[PIN NAME].init(0)' to disable output from any number of the pins.

This raises the question: Is there a way to ensure that only the desired pin ouputs PWM?

Post Reply