Hardware PWM on BPI-M2 Zero

I am attempting to hook up an analog speaker to my BPI-M2 Zero. For this, I am wanting to use hardware PWM but I cannot get it working. I am currently using Raspbian, but previously tried the latest Ubuntu server and Armbian images which are linked from the Banana Pi wiki.

The gpio command is erroring on any PWM value:

    matt@bpi-iot-ros-ai:~$ gpio mode 7 pwm
    you choose the hardware PWM:1

    matt@bpi-iot-ros-ai:~$ gpio pwm 7 100
    val pwmWrite 0 <= X <= 1024
    Or you can set new range by yourself by pwmSetRange(range

and I have no /sys/class/pwm - which I suspect may be related to gpio not working for pwm (and why my C# code isn’t working either, as the Microsoft library relies on this)

Here’s the results of gpio readall:

matt@bpi-iot-ros-ai:~$ gpio readall
 +-----+-----+---------+------+---+---Pi ?---+---+------+---------+-----+-----+
 | BCM | wPi |   Name  | Mode | V | Physical | V | Mode | Name    | wPi | BCM |
 +-----+-----+---------+------+---+----++----+---+------+---------+-----+-----+
 |     |     |    3.3v |      |   |  1 || 2  |   |      | 5v      |     |     |
 |  12 |   8 |   SDA.1 | ALT5 | 0 |  3 || 4  |   |      | 5v      |     |     |
 |  11 |   9 |   SCL.1 | ALT5 | 0 |  5 || 6  |   |      | 0v      |     |     |
 |   6 |   7 | GPIO. 7 | ALT4 | 0 |  7 || 8  | 0 | ALT4 | TxD     | 15  | 13  |
 |     |     |      0v |      |   |  9 || 10 | 0 | ALT4 | RxD     | 16  | 14  |
 |   1 |   0 | GPIO. 0 | ALT5 | 0 | 11 || 12 | 0 | ALT4 | GPIO. 1 | 1   | 16  |
 |   0 |   2 | GPIO. 2 | ALT5 | 0 | 13 || 14 |   |      | 0v      |     |     |
 |   3 |   3 | GPIO. 3 | ALT5 | 0 | 15 || 16 | 0 | ALT4 | GPIO. 4 | 4   | 15  |
 |     |     |    3.3v |      |   | 17 || 18 | 0 | ALT3 | GPIO. 5 | 5   | 68  |
 |  64 |  12 |    MOSI | ALT4 | 0 | 19 || 20 |   |      | 0v      |     |     |
 |  65 |  13 |    MISO | ALT4 | 0 | 21 || 22 | 0 | ALT5 | GPIO. 6 | 6   | 2   |
 |  66 |  14 |    SCLK | ALT4 | 0 | 23 || 24 | 0 | ALT4 | CE0     | 10  | 67  |
 |     |     |      0v |      |   | 25 || 26 | 0 | ALT3 | CE1     | 11  | 71  |
 |  19 |  30 |   SDA.0 | ALT4 | 0 | 27 || 28 | 0 | ALT4 | SCL.0   | 31  | 18  |
 |   7 |  21 | GPIO.21 | ALT3 | 0 | 29 || 30 |   |      | 0v      |     |     |
 |   8 |  22 | GPIO.22 | ALT3 | 0 | 31 || 32 | 0 | ALT5 | GPIO.26 | 26  | 354 |
 |   9 |  23 | GPIO.23 | ALT3 | 0 | 33 || 34 |   |      | 0v      |     |     |
 |  10 |  24 | GPIO.24 | ALT3 | 0 | 35 || 36 | 0 | ALT3 | GPIO.27 | 27  | 356 |
 |  17 |  25 | GPIO.25 | ALT3 | 0 | 37 || 38 | 0 | ALT3 | GPIO.28 | 28  | 21  |
 |     |     |      0v |      |   | 39 || 40 | 0 | ALT3 | GPIO.29 | 29  | 20  |
 +-----+-----+---------+------+---+----++----+---+------+---------+-----+-----+
 | BCM | wPi |   Name  | Mode | V | Physical | V | Mode | Name    | wPi | BCM |
 +-----+-----+---------+------+---+---Pi ?---+---+------+---------+-----+-----+

I tried enabling the pwm overlay in the Raspbian /boot/config.txt, but I don’t really know what I’m doing.

How is hardware PCM configured? Is there an image where it “just works”?

Looks like hardware pwm on M2 Zero is only supported on PA5, which is connected to jumper CON3 (UART0). You can confgure it with the sysfs interface after you added the pwm overlay to armbianEnv.txt Details can be found here: https://forum.armbian.com/topic/3886-pwm-on-h3-and-h2-with-mainline-kernel/ I tested it and it worked for me. see below output on PA5 after issuing the ‘echos’ grafik

I will try to modify my BPI-WiringPI fork so that it works with the GPIO commands

Thank you so much! I installed a fresh Armbian distribution, added the pwm overlay, rebooted, and it worked first off. I’m really happy.

To assist future confused people, the Banana Pi wiki page for the BPI-M2 states that CON2-P07 has a default function of PWM1 - hence my apparently futile endeavours with the gpio utility. Is this not the case, or is CON2-P07 not actually GPIO pin 7?

For hardware support type questions like this, is it generally more fruitful to identify for what the SoC supports, rather than what the board supports? Or is it a bit of both?

I thought I Googled fairly extensively for Banana Pi Zero and hardware PCM, but got nowhere.

Looks like the support from BPI for the M2Zero variant is not that good, so I would go more for the SoC rather then the board here. From the H3 datasheet I found, there is only a PWM0 on pin ‘H5’, and no PWM1. H5 goes to con3. So for me the PWM1 in the wiki is a ‘cut and paste’ error from another board.

Maybe @sinovoip can better comment on that?

could we revisit this, please? @bontango @sinovoip which pins should I use for hardware PWM? it’s all a bit chaotic for M2 zero board

only the RX pin ‘PA5’ of the UART connector CON3 works for PWM

jumper CON3 of Banana pi BPI-M2 Zero CON3 Pin Name Default Function GPIO CON3 P03 UART0-TXD PA4 CON3 P02 UART0-RXD PA5 CON3 P01 GND

found out already; so confusing