I'd need one channel at 30kHz, able to reach 85/15 duty cycle.
My goal is to drive a 12V relay without the need for a dual power supply, using fewest possible parts.
I've managed to come up with a bare-bones buck-boost converter that only needs two caps and one inductor on top of the typical transistor+diode relay driver. (See pic).
The controller would need to provide for the PWM signal, though. On the plus side, this circuit can reduce the power consumption of the relay after it has engaged. (Almost all relays need far less power to stay on, once they have "clicked")
So I tried various hacks...
1. Abuse the second UART
Set it to 300kbit/s. You get 10bits = 10 time slots per character. By chosing a proper bit pattern, you can tune the duty cycle from 10% to 90% in steps of 10%.
With a 128byte FIFO, the CPU load is negligible.
Almost perfect... but the Chinese, in their wisdom, make both UARTS sputter some nonsense at startup.
This always makes the relay click on briefly at startup. Do not want!
2. Brute force with timer interrupts
Set the hardware timer to interrupt at 60kHz. The ISR can adjust the reload value so you get the required duty cycle.
Tried it. Works.
The CPU load is very high. This hack just feels wrong all the way.
3. Abuse the SPI
Did not try that yet. The SPI documentation is almost non-existent. Strategy would be the same as with the UART.
4. Hardware PWM?
Does it have one? That's what I came here for to find out...
Some more background on the circuit itself.
It's buck-boost, because a normal boost keeps the input power supply connected to the load at all times. No good, the relay would never turn off after turning on the first time.
Choice of transistor is sub-optimal. A 3.3V logic level FET would be better. The shown transistor works and it's the one I had around.
This converter is totally unregulated. Not a big deal. Input 3.3V is stabilized. Output load is constant and doesn't care about the 12V being exactly matched.
The transistor is a PFET, even though NFETs are better. Uninitialized pins are pulled up and I want the transistor to be off in that case. The transistor must never be on constantly. A power inductor at DC is almost 0 Ohms...
I plan to run this from a 1-2W 3.3V power supply (RAC-01 or RAC-02). If the transistor is stuck on, worst case, the PSU shuts down briefly, so no smoke...