Open
Description
Hi,
A topic has been on my mind last few days, thought I would socialize it to see if it's worth exploring it.
A few months back I measured it takes 48us on STM32F1 to run the setpwm function for 6PWM.
Which is more than the inverse clarke/park and svpwm combined in my case.
I think we are converting to too many different value domains:
- we calculate Ua Ub and Uc voltages (float)
- Dc_a Dc_b and Dc_c are calculated here as percentages of the power supply [0,1] (float)
- duty cycles are multiplied here by pwm_range to scale it to the timer resolution [0,4095] (uint32_t)
- duty cycles are scaled to the Auto reload register value here which is for example 72Mhz/16Khz/2 so [0,2250] in my case (uint32_t)
Few ideas:
- a non breaking change could be to scale from [0,1] to [0,ARR+1] directly, skipping [0,4095] in point (3). This math could be adapted
- a more complicated change would be to directly scale to [0,ARR+1] in _setPwm but we would need a way to get ARR+1 in a consistent way for all drivers (check getOverflow )
- the arduino bloat could be skipped by directly using the Low level code in _setPwm (e.g. this is calculated each loop but is not used in our case, this also seems unnecessary).
[UPDATE]
- commenting the arduino bloat reduced by 4us only
- SetPhaseState seems to take 8us for 3 executions, this could be used to check if channel is running before stopping/resuming
- converting from [0,4095] to [0,2250] is integer math so it might not be taking a lot of time