
In the 1.17.388 version, OpenBeken introduced a PWM Groups driver. This driver enables the synchronization of two PWMs and facilitates setting a specified dead time between them. Of course, the duty cycles of each PWM groups are independently controlled and the frequency is also adjustable.
Tuya's (Beken?) implementation
The PWM groups driver is present in the used BK7231N SDK, but after few quick tests it's clear that's it not working correctly:
Code: C / C++
There are multiple issues with this code:
- the second duty cycle is not used at all and both PWMs are using duty_cycle1
- the frequency variable is not in fact a frequency, but rather a period
- the following implementation breaks when a 0 duty_cycle is given
My improvements
I've performed some tests with the original version of the code, also by manually setting values for each PWM:
Code: C / C++
Result:

Test code:
Code: C / C++
results:

Test code:
Code: C / C++
Result:

Test code:
Code: C / C++
Result:

It seems that duty_cycle1, duty_cycle2 and duty_cycle3 describes when a PWM toggles self state. Based on that, I proposed the following implementation:
Code: C / C++
The code above still uses the incorrect name for period variable (it's called frequency), but I also resolved it.
The tests have shown that the period and duty cycles should be calculated on the main oscillator frequency basis:
Code: C / C++
The duty values are assumed to be in percent and frequency is in Hz. The period values are calculates from them.
Usage
To use it in OBK, you need a BK7231N device. First, enable driver:
startDriver PWMG
Then, start the group:
PWMG_Set 10 10 10 1000
Full command syntax is the following:
PWMG_Set Duty1Percent Duty2Percent DeadTimePercent Frequency PinA PinB
Please note that the following will only work correctly for PWM pairs, like PWM0+PWM1, PWM2+PWM3, etc.
Futhermore, for PinA/PinB you need to give a P-index, so PWM0 is not 0, it's 6.
Please see screenshot below for explanation:

Of course, when typing command, type just "6", not "P6", like:
PWMG_Set 10 10 10 1000 6 7
Examples
Command (1000Hz, 10% first, second 10%, dead 10%):
PWMG_Set 10 10 10 1000
Result:

Command (1000Hz, 50% first, second 10%, dead 10%):
PWMG_Set 50 10 10 1000
Result:

Command (5000Hz, 10% first, second 10%, dead 10%):
PWMG_Set 10 10 10 5000
Result:

Summary
PWM groups driver is now working, altough it still could be improved a bit. For example, the code is not currently checking for out of range values, and using them may yield unexpected results. Futhermore I have not tested how PWM behaves when user specified two PWMs that are not from the same group.
The PWM groups driver was added per user request here, but I hope it can be useful for more people as well. Do you have any ideas how could you use such a driver? Let us know.
Cool? Ranking DIY Helpful post? Buy me a coffee.