high level IF to hardware PWM?

Posted on
  • Reading through the "PWM how to do it yourself" page, what is described is a software approach.

    Is analogWrite(pin, level, {freq:}) the high-level interface to STM32 hardware PWM? I.e., control of frequency via the options parameter, and duty cycle via the level/value parameter?

    So, a 1000Hz, 25% duty-cycle waveform is produced, entirely without any code running but simply via the hardware (after Espruino sets it up from the analogWrite call) with analogWrite(pin, 0.25, {freq:1000})? Once done on a board that supports hardware PWM, like the Pico, the MCU is generating that waveform all on its own, the Espruino main loop just idling, right?

  • This covers experiments with the espruino board.

    Pins that can connect to the timer circuits can generate PWM without running code other that the setup.
    On the Espruino board up to 4 pins are on each timer. One frequency but 4 different duty cycles.
    Software is used on the pins that can't connect to timers. A timer circuit is used but the pin toggle is handled by software.

  • Pins marked PWM capable on the pinout chart have hardware pwm Using analogWrite will use the appropriate timer and do hardware pwm if you use it on a pin that has one. You can also have it do software pwm on any pin, but obviously you want to stick with hardware pwm pins if possible, for the reasons you described in op

  • Just to add - yes, analogWrite uses the hardware by default, and the CPU isn't involved at all. Having said that, it uses the high speed oscillator, so it'll still end up using a bit of power (even if the CPU is totally free to do something else).

    You can use {soft:true} to force software PWM though - it's occasionally useful if you want more PWM pins (for example on the LEDs), or if you can't get the combination of PWM frequencies you want.

  • Post a reply
    • Bold
    • Italics
    • Link
    • Image
    • List
    • Quote
    • code
    • Preview

high level IF to hardware PWM?

Posted by Avatar for dwallersv @dwallersv