[Documentation] Hardware PWM

With the recent introduction of hardware PWM, I would like to ask for some clarification in the documentation.
My understanding so far:

Software PWM

  • Host process supplies the target duty cycle
  • Timer is created in the host process
  • Pin toggling to achieve the target duty cycle is done in the host process

Hardware PWM

  • Host process supplies the target duty cycle
  • Timer is created in the MCU hardware
  • Pin toggling to achieve the target duty cycle is done in the MCU

Questions:

  • What are the benefits? → I would guess higher timing precision. So worthwhile for servos and LED brightness control?
  • Does it make sense for temperature or fans?
  • Does it have drawbacks?

Both Software PWM and Hardware PWM are implemented on the MCU. In both cases, the host just configures the actions the MCU should perform.

The difference is, hardware PWM uses a “hardware block” on the MCU dedicated to performing PWM, while software PWM is implemented by toggling the output pin using Klipper’s regular software timer mechanism.

The main advantage to Software PWM:

  • Any pin can be used.
  • The cycle and duty times are very precise.

The disadvantages of Software PWM:

  • The toggling of the output is done in the MCU software, which can result in some scheduling jitter. I’d guess this jitter would max out at ~20us for AVR and ~5us for ARM.
  • Again, since the toggling is done in the mcu software, very high toggling rates can increase the mcu cpu load.

The main advantages of hardware PWM:

  • It can toggle at very high rates without jitter and without addition mcu cpu load.

The disadvantage of hardware PWM is:

  • Not all pins can be used for hardware PWM (the pins that can be used depend on the type of each micro-controller).
  • The actual cycle and duty times that are implemented may only be a coarse representation of the requested values. Available cycle times vary between micro-controller types, and there are also limitations should multiple pins on the same MCU be used for hardware PWM.

In summary, on Klipper, use hardware PWM if you need to approximate a DAC (that is, approximating a voltage varying output, typically via an RC circuit). In this case, hardware PWM can obtain the necessary high-speed update rates. In other cases, software PWM is preferable.

-Kevin

1 Like

Thanks for this clarification. Would be an excellent addition to the FAQ IMHO.

Already have a post Raspberry as primary mcu - General Discussion - Klipper, as I thought it might have been a different issue, but this topic seems more specific for the actual problem.

Hardware pwm for host (raspberry) is behaving “funny”.
(Preamble: I have a fan “[controller_fan octopus_fan]” configured with “shutdown_speed: 1.0”)

Result is always the same “MCU ‘mcu’ shutdown: Unable to config pwm device” which goes away after a firmware_restart.
BUT
If I boot the raspberry from “off” state I simply get the error but the fan STAYS OFF
If I reboot the raspberry, on top of the error the fan GOES ON (expected behaviour)
Below I paste the log after:
Raspberry off → on → firmware_restart → raspberry reboot → firmware_restart
Ps @koconnor , man, I do miss a simple ‘bug’ report on github, I didn’t know I could drop a file here to upload it as an attachment. I tried to paste it and… windows green screen :sweat_smile:

klippy.log (995.6 KB)