Portenta_H7_PWM Library

Portenta_H7_PWM library GitHub release
How To Install Using Arduino Library Manager


Features

This library enables you to use Hardware Timers on an STM32H7-based Portenta_H7 board to create and output PWM to pins.

The most important feature is they're purely hardware-based PWM channels. Therefore, the frequency can be higher than hybrid or software-based PWM, and their executions are not blocked by bad-behaving functions / tasks. This important feature is absolutely necessary for mission-critical tasks.

This important feature is absolutely necessary for mission-critical tasks. These hardware timers, using interrupt, still work even if other functions are blocking. Moreover, they are much more precise (certainly depending on clock frequency accuracy) than other software timers using millis() or micros(). That's necessary if you need to measure some data requiring better accuracy.

The PWMs_Array_Complex example will demonstrate the nearly perfect accuracy, compared to software-based PWM, by printing the actual period / duty-cycle in microsecs of each of PWM-channels.

The PWM_Multi_Args will demonstrate the usage of multichannel PWM using multiple Hardware Timers. The 4 independent Hardware Timers are used to control 4 different PWM outputs, with totally independent frequencies and dutycycles.


Currently Supported Boards

This Portenta_H7_PWM library currently supports the following boards:

  1. Portenta_H7 boards, using ArduinoCore-mbed mbed_portenta core


Changelog

Initial Releases v1.0.0

  1. Initial coding to support Portenta_H7 boards such as Portenta_H7 Rev2 ABX00042, etc., using ArduinoCore-mbed mbed_portenta core

Examples

  1. PWM_Multi
  2. PWM_Multi_Args
  3. PWMs_Array_Complex

Debug Terminal Output Sample

1. PWMs_Array_Complex on PORTENTA_H7_M7

The following is the sample terminal output when running example PWMs_Array_Complex on Portenta_H7 to demonstrate the accuracy of Hardware-based PWM, especially when system is very busy.

Starting PWMs_Array_Complex on PORTENTA_H7_M7
Portenta_H7_PWM v1.0.0
Index = 0, Instance = 0x40010000, channel = 3, TimerIndex = 0
Index = 1, Instance = 0x40000800, channel = 1, TimerIndex = 3
Index = 2, Instance = 0x40010400, channel = 2, TimerIndex = 7
Index = 3, Instance = 0x40001800, channel = 2, TimerIndex = 8
SimpleTimer (ms): 2000, us : 12659505, Dus : 9933701
PWM Channel : 0, programmed Period (us): 500000, actual : 499994, programmed DutyCycle : 20, actual : 20.00
PWM Channel : 1, programmed Period (us): 200000, actual : 199999, programmed DutyCycle : 30, actual : 30.00
PWM Channel : 2, programmed Period (us): 100000, actual : 100000, programmed DutyCycle : 50, actual : 50.00
PWM Channel : 3, programmed Period (us): 10000, actual : 10000, programmed DutyCycle : 70, actual : 70.00
SimpleTimer (ms): 2000, us : 22594086, Dus : 9934581
PWM Channel : 0, programmed Period (us): 500000, actual : 499994, programmed DutyCycle : 20, actual : 20.00
PWM Channel : 1, programmed Period (us): 200000, actual : 199998, programmed DutyCycle : 30, actual : 30.00
PWM Channel : 2, programmed Period (us): 100000, actual : 99999, programmed DutyCycle : 50, actual : 50.00
PWM Channel : 3, programmed Period (us): 10000, actual : 10000, programmed DutyCycle : 70, actual : 70.00
SimpleTimer (ms): 2000, us : 32528951, Dus : 9934865
PWM Channel : 0, programmed Period (us): 500000, actual : 499994, programmed DutyCycle : 20, actual : 20.00
PWM Channel : 1, programmed Period (us): 200000, actual : 199999, programmed DutyCycle : 30, actual : 30.00
PWM Channel : 2, programmed Period (us): 100000, actual : 100000, programmed DutyCycle : 50, actual : 50.00
PWM Channel : 3, programmed Period (us): 10000, actual : 10001, programmed DutyCycle : 70, actual : 69.99
SimpleTimer (ms): 2000, us : 42463635, Dus : 9934684
PWM Channel : 0, programmed Period (us): 500000, actual : 499994, programmed DutyCycle : 20, actual : 20.00
PWM Channel : 1, programmed Period (us): 200000, actual : 199999, programmed DutyCycle : 30, actual : 30.00
PWM Channel : 2, programmed Period (us): 100000, actual : 99999, programmed DutyCycle : 50, actual : 50.00
PWM Channel : 3, programmed Period (us): 10000, actual : 10000, programmed DutyCycle : 70, actual : 69.99
SimpleTimer (ms): 2000, us : 52398502, Dus : 9934867
PWM Channel : 0, programmed Period (us): 500000, actual : 499994, programmed DutyCycle : 20, actual : 20.00
PWM Channel : 1, programmed Period (us): 200000, actual : 199998, programmed DutyCycle : 30, actual : 30.00
PWM Channel : 2, programmed Period (us): 100000, actual : 99999, programmed DutyCycle : 50, actual : 50.00
PWM Channel : 3, programmed Period (us): 10000, actual : 10000, programmed DutyCycle : 70, actual : 70.00

Hi!

First of all, thanks a lot for that library!
I Tried your examples and unfortuanelty the lib is working on the one hand, on the other hand, the Pins are not toggeling as expected (they stay "0").

Do i really have to set the pin state manual in the isr callbacks? e.g. digitalWrite?

Thanks in advance
Hubert

Portenta_H7 hardware PWM can be used only for some pins. Check the note in PWMs_Array_Complex.ino#L159-L160

  • Pins OK to use
// Only OK for D0, D1, D2, D4 and D5, PA_0C(D15/A0), PA_1C(D16/A1), 
  • Pins not OK to use
// D3, D6, D7, D8, D9, D10, D11, D12, D13, D14, D17(PC_2C/A2), D18(PC_3C/3), PC2(D19/A4) LEDG, LEDB not OK

I Tried your examples and unfortuanelty the lib is working on the one hand, on the other hand, the Pins are not toggeling as expected (they stay "0").

I suggest you try the unmodified examples first to be sure they are working OK on your board, then adapt one of them to your use case.

Do i really have to set the pin state manual in the isr callbacks? e.g. digitalWrite?

Totally optional to demo some features.

Good Luck,

Thanks for your fast answer!
I tried all of your (unmodified) examples and i had no luck. Couldn't see any pin toggeling on the oszi.

We tried two boards (H7 and the H7 lite) on two computers and couldn't get it running. Maybe there is an issue with the bootloader or some other crap not concerning your library?

Please use the new major release Portenta_H7_PWM Releases v2.0.0, with completely new code to fix PWM output issue you mentioned.


Releases v2.0.0

  1. Completely new code to fix no PWM output issue. Check No PWM-Output signals #1
  2. To permit start, stop, change and restore the settings of any PWM channel on-the-fly

Debug Terminal Output Samples

1. PWM_Multi on PORTENTA_H7_M7

The following is the sample terminal output when running example PWM_Multi on Portenta_H7 to demonstrate how to start multiple PWM channels, then stop, change, restore the PWM settings on-the-fly.

Starting PWM_Multi on PORTENTA_H7_M7
Portenta_H7_PWM v2.0.0
[PWM] Freq = 1000.00, 	DutyCycle % = 50.00, 	DutyCycle = 0.50, 	Pin = 0
[PWM] Freq = 2500.00, 	DutyCycle % = 50.00, 	DutyCycle = 0.50, 	Pin = 1
[PWM] Freq = 4000.00, 	DutyCycle % = 50.00, 	DutyCycle = 0.50, 	Pin = 3
[PWM] Freq = 5000.00, 	DutyCycle % = 50.00, 	DutyCycle = 0.50, 	Pin = 5
[PWM] Freq = 50000.00, 	DutyCycle % = 50.00, 	DutyCycle = 0.50, 	Pin = 6

==========================================================================================================
PW (us) 0	PW (us) 1	PW (us) 2	PW (us) 3	PW (us) 4	
==========================================================================================================
500.00		200.00		125.00		100.00		10.00		
Stop all PWM
0.00		0.00		0.00		0.00		0.00		
0.00		0.00		0.00		0.00		0.00		
Change all PWM
125.00		50.00		31.25		25.00		2.50		
125.00		50.00		31.25		25.00		2.50		
Restore all PWM
500.00		200.00		125.00		100.00		10.00		
500.00		200.00		125.00		100.00		10.00		
Stop all PWM
0.00		0.00		0.00		0.00		0.00		
0.00		0.00		0.00		0.00		0.00		
Change all PWM
125.00		50.00		31.25		25.00		2.50		
125.00		50.00		31.25		25.00		2.50		
Restore all PWM
500.00		200.00		125.00		100.00		10.00		
500.00		200.00		125.00		100.00		10.00	

Releases v2.0.1

  1. Fix PWM_Multi example by not using HRTIM pinD3 and pinD6 at the same time
  2. Temporary fix polarity for HRTIM PWM for core v2.6.1-. Check Portenta: PWM on HRTIM has inverted corner case values #352. To be modified when the issue is fixed.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.