Pulse width modulation (PWM)

Pulse width modulation allows you to create a block wave with a certain duty cycle. It is useful for use cases like dimming LEDs.

             <--> pulse-width
___          ____          ____
   |        |    |        |    |           duty-cycle = pulse-width / period
   |________|    |________|    |______
             <--period--->

There are 12 PWM channels.

Each channel consists of a free-running 8-bit counter with two configurable comparators. The period comparator resets the counter value back to zero once it reaches a user-configured top value. This sets the period (number of cycles between rising-edges of pulses) of the output waveform. The pulse width comparator changes the channel output from high to low when the counter value surpasses a user-configured pulse width value. This sets the pulse width (number of cycles the output spends high) of the output waveform.

Each channel has a 64-bit section in the address space for configuration.

OffsetPWM channel
0x00Channel 0
0x08Channel 1
0x10Channel 2
0x18Channel 3
0x20Channel 4
0x28Channel 5
0x30Channel 6

To see which pins can be connected to which channel please consult the pin multiplexer.

Config

For each channel, there is a 32-bit register defining the pulse width and another 32-bit register above it defining how long the complete wave is (counter top). These registers are write-only, and will return a value of zero if read. The counters are only 8-bit wide, any values written that are larger than 8-bits are invalid.

OffsetDescriptionRead/Write
0x0Pulse widthWrite-only
0x4Counter topWrite-only

A channel is enabled by setting an non-zero counter top value.

To generate an always-low signal, set a counter top value of zero. To generate an always-high signal, set a pulse-width value greater than the (non-zero) counter top value.