4.5. PWM

4.5.1. Overview

Pulse width modulation (PWM) is a scheme where a digital signal provides a smoothly varying average voltage. This is achieved with positive pulses of some controlled width, at regular intervals. The fraction of time spent high is known as the duty cycle. This may be used to approximate an analog output, or control switchmode power electronics.

The RP2040 PWM block has 8 identical slices. Each slice can drive two PWM output signals, or measure the frequency or duty cycle of an input signal. This gives a total of up to 16 controllable PWM outputs. All 30 GPIO pins can be driven by the PWM block.

Figure 103. A single PWM slice. A 16-bit counter counts from 0 up to some programmed value, and then wraps to zero, or counts back down again, depending on PWM mode. The A and B outputs transition high and low based on the current count value and the preprogrammed A and B thresholds. The counter advances based on a number of events: it may be free-running, or gated by level or edge of an input signal on the B pin. A fractional divider slows the overall count rate for finer control of output frequency.

Block diagram of a single PWM slice. It shows an input pin B connected to an event select block and a rising/falling edge detector. The event select block also receives a '1' input and an 'Event select' signal. Its output goes to the 'EN' input of a 'Fractional Clock Divider (8.4)'. The divider also receives 'Phase Advance' and 'Phase Retard' signals. Its output goes to the 'EN' input of a '16-bit up/down Counter'. The counter's output goes to two 'Output compare units (level A and level B)' and an 'IRQ Latch'. The counter also has a 'Wrap' output that feeds back into the 'Event select' block. The output compare units produce 'Output (pin A)' and 'Output (pin B)' signals.
Block diagram of a single PWM slice. It shows an input pin B connected to an event select block and a rising/falling edge detector. The event select block also receives a '1' input and an 'Event select' signal. Its output goes to the 'EN' input of a 'Fractional Clock Divider (8.4)'. The divider also receives 'Phase Advance' and 'Phase Retard' signals. Its output goes to the 'EN' input of a '16-bit up/down Counter'. The counter's output goes to two 'Output compare units (level A and level B)' and an 'IRQ Latch'. The counter also has a 'Wrap' output that feeds back into the 'Event select' block. The output compare units produce 'Output (pin A)' and 'Output (pin B)' signals.

Each PWM slice is equipped with the following:

Slices can be enabled or disabled simultaneously via a single, global control register. The slices then run in perfect lockstep, so that more complex power circuitry can be switched by the outputs of multiple slices.

4.5.2. Programmer’s Model

All 30 GPIO pins on RP2040 can be used for PWM:

Table 515. Mapping of PWM channels to GPIO pins on RP2040. This is also shown in the main GPIO function table, Table 279

GPIO0123456789101112131415
PWM Channel0A0B1A1B2A2B3A3B4A4B5A5B6A6B7A7B
GPIO1617181920212223242526272829
PWM Channel0A0B1A1B2A2B3A3B4A4B5A5B6A6B

4.5.2.1. Pulse Width Modulation

The PWM hardware functions by continuously comparing the input value to a free-running counter. This produces a toggling output where the amount of time spent at the high output level is proportional to the input value. The fraction of time spent at the high signal level is known as the duty cycle of the signal.

The counting period is controlled by the TOP register, with a maximum possible period of 65536 cycles, as the counter and TOP are 16 bits in size. The input values are configured via the CC register.

Figure 104. The counter repeatedly counts from 0 to TOP , forming a sawtooth shape. The counter is continuously compared with some input value. When the input value is higher than the counter, the output is driven high. Otherwise, the output is low. The output period \( T \) is defined by the TOP value of the counter, and how fast the counter is configured to count. The average output voltage, as a fraction of the IO power supply, is the input value divided by the counter period ( \( \text{TOP} + 1 \) )

Figure 104: Timing diagram for PWM operation. The top graph, 'Input (Count)', shows a sawtooth wave for the counter (red line) increasing from 0 to TOP over time t. A horizontal blue line represents the 'Counter compare level' at TOP/3. The bottom graph, 'Output (Pulse)', shows the resulting GPIO pulse output (blue line) which is high (at IOVDD) when the counter is below the compare level and low (at 0) otherwise. The period of the output is T, and the pulse width is T/3. The output is high for the first T/3 of each cycle and low for the remaining 2T/3.
Figure 104: Timing diagram for PWM operation. The top graph, 'Input (Count)', shows a sawtooth wave for the counter (red line) increasing from 0 to TOP over time t. A horizontal blue line represents the 'Counter compare level' at TOP/3. The bottom graph, 'Output (Pulse)', shows the resulting GPIO pulse output (blue line) which is high (at IOVDD) when the counter is below the compare level and low (at 0) otherwise. The period of the output is T, and the pulse width is T/3. The output is high for the first T/3 of each cycle and low for the remaining 2T/3.

This example shows the counting period and the A and B counter compare levels being configured on one of RP2040's PWM slices.

Pico Examples: https://github.com/raspberrypi/pico-examples/blob/master/pwm/hello_pwm/hello_pwm.c Lines 14 - 29

14 // Tell GPIO 0 and 1 they are allocated to the PWM
15 gpio_set_function(0, GPIO_FUNC_PWM);
16 gpio_set_function(1, GPIO_FUNC_PWM);
17
18 // Find out which PWM slice is connected to GPIO 0 (it's slice 0)
19 uint slice_num = pwm_gpio_to_slice_num(0);
20
21 // Set period of 4 cycles (0 to 3 inclusive)
22 pwm_set_wrap(slice_num, 3);
23 // Set channel A output high for one cycle before dropping
24 pwm_set_chan_level(slice_num, PWM_CHAN_A, 1);
25 // Set initial B output high for three cycles before dropping
26 pwm_set_chan_level(slice_num, PWM_CHAN_B, 3);
27 // Set the PWM running
28 pwm_set_enabled(slice_num, true);

Figure 105 shows how the PWM hardware operates once it has been configured in this way.

Figure 105. The slice counts repeatedly from 0 to 3, which is configured as the TOP value. The output waves therefore have a period of 4. Output A is high for 1 cycle in 4, so the average output voltage is 1/4 of the IO supply voltage. Output B is high for 3 cycles in every 4. Note the rising edges of A and B are always aligned.

Timing diagram for a PWM slice with TOP=3. The counter cycles through 0, 1, 2, 3 repeatedly. Output A is high for 1 cycle (count 0) and low for 3 cycles (counts 1, 2, 3). Output B is high for 3 cycles (counts 0, 1, 2) and low for 1 cycle (count 3). The rising edges of A and B are aligned at count 0.
Timing diagram for a PWM slice with TOP=3. The counter cycles through 0, 1, 2, 3 repeatedly. Output A is high for 1 cycle (count 0) and low for 3 cycles (counts 1, 2, 3). Output B is high for 3 cycles (counts 0, 1, 2) and low for 1 cycle (count 3). The rising edges of A and B are aligned at count 0.

The default behaviour of a PWM slice is to count upward until the value of the TOP register is reached, and then immediately wrap to 0. PWM slices also offer a phase-correct mode, enabled by setting CSR_PH_CORRECT to 1, where the counter starts to count downward after reaching TOP, until it reaches 0 again.

It is called phase-correct mode because the pulse is always centred on the same point, no matter the duty cycle. In other words, its phase is not a function of duty cycle. The output frequency is halved when phase-correct mode is enabled.

Figure 106. In phase-correct mode, the counter counts back down from TOP to 0 once it reaches TOP.

Timing diagram for a PWM slice in phase-correct mode. The counter (red line) counts up from 0 to TOP and then counts back down to 0. The counter compare level (blue line) is set to TOP/3. The output pulse (blue line) is high for the first 1/3 of the cycle and low for the remaining 2/3. The period is 2T.
Timing diagram for a PWM slice in phase-correct mode. The counter (red line) counts up from 0 to TOP and then counts back down to 0. The counter compare level (blue line) is set to TOP/3. The output pulse (blue line) is high for the first 1/3 of the cycle and low for the remaining 2/3. The period is 2T.

4.5.2.2. 0% and 100% Duty Cycle

The RP2040 PWM can produce toggle-free 0% and 100% duty cycle output.

Figure 107. Glitch-free 0% duty cycle output for CC = 0, and glitch-free 100% duty cycle output for CC = TOP + 1

Timing diagram showing glitch-free 0% and 100% duty cycle outputs. The counter (red line) counts up from 0 to TOP. The counter compare level (blue line) is set to 0 for 0% duty cycle and to TOP+1 for 100% duty cycle. The output pulse (blue line) is low for 0% and high for 100% duty cycle. The period is 2T.
Timing diagram showing glitch-free 0% and 100% duty cycle outputs. The counter (red line) counts up from 0 to TOP. The counter compare level (blue line) is set to 0 for 0% duty cycle and to TOP+1 for 100% duty cycle. The output pulse (blue line) is low for 0% and high for 100% duty cycle. The period is 2T.

A CC value of 0 will produce a 0% output, i.e. the output signal is always low. A CC value of TOP + 1 (i.e. equal to the period, in non-phase-correct mode) will produce a 100% output. For example, if TOP is programmed to 254, the counter will have a period of 255 cycles, and CC values in the range of 0 to 255 inclusive will produce duty cycles in the range 0% to 100% inclusive.

Glitch-free output at 0% and 100% is important e.g. to avoid switching losses when a MOSFET is controlled at its minimum and maximum current levels.

4.5.2.3. Double Buffering

Figure 108 shows how a change in input value will produce a change in output duty cycle. This can be used to approximate some analog waveform such as a sine wave.

Figure 108. The input value varies with each counter period: first \( TOP/3 \) , then \( 2 \times TOP/3 \) , and finally \( TOP + 1 \) for 100% duty cycle. Each increase in the input value causes a corresponding increase in the output duty cycle.

Figure 108: Timing diagram showing double buffering. The top graph, 'Input (Count)', shows a sawtooth counter (red) and a step-wise compare level (blue) that increases at each counter wrap (t, 2T, 3T). The bottom graph, 'Output (Pulse)', shows the resulting GPIO pulse output (blue) which is high for the duration of the compare level and low otherwise. The output duty cycle increases as the compare level increases.

The figure consists of two vertically aligned graphs sharing a common time axis \( t \) .
The top graph, titled 'Input (Count)', has a vertical axis labeled 'Count'. It shows a red line representing the counter, which increases linearly from 0 to \( TOP \) and then resets to 0 at times \( t \) , \( 2T \) , and \( 3T \) . A blue step function represents the 'Counter compare level'. It starts at \( TOP/3 \) from \( t=0 \) to \( t=T \) , jumps to \( 2 \times TOP/3 \) at \( t=T \) , and jumps to \( TOP \) at \( t=2T \) .
The bottom graph, titled 'Output (Pulse)', has a vertical axis labeled \( V \) . It shows the 'GPIO pulse output' as a blue square wave. The output is high (at level \( IOVDD \) ) when the counter value is less than the current compare level, and low (at 0) otherwise. The duty cycle of the output increases as the compare level increases.

Figure 108: Timing diagram showing double buffering. The top graph, 'Input (Count)', shows a sawtooth counter (red) and a step-wise compare level (blue) that increases at each counter wrap (t, 2T, 3T). The bottom graph, 'Output (Pulse)', shows the resulting GPIO pulse output (blue) which is high for the duration of the compare level and low otherwise. The output duty cycle increases as the compare level increases.

In Figure 108, the input value only changes at the instant where the counter wraps through 0. Figure 109 shows what happens if the input value is allowed to change at any other time: an unwanted glitch is produced at the output.

Figure 109. The input value changes whilst the counter is mid-ramp. This produces additional toggling at the output.

Figure 109: Timing diagram showing an unwanted glitch. Similar to Figure 108, but the compare level (blue) changes at a mid-ramp point (5T/3) instead of at a wrap. This causes an additional transition (glitch) in the output pulse (blue) at that time.

This figure is similar to Figure 108 but illustrates a glitch scenario. The counter (red) and its wraps at \( t \) , \( 2T \) , and \( 3T \) are the same. However, the compare level (blue) starts at \( TOP/3 \) until \( t=T \) , then jumps to \( 2 \times TOP/3 \) at \( t=T \) . At a later time, \( 5T/3 \) , the compare level jumps again to \( TOP \) while the counter is still in its mid-ramp. This second jump causes an additional transition in the output pulse (blue) at \( 5T/3 \) , which is an unwanted glitch.

Figure 109: Timing diagram showing an unwanted glitch. Similar to Figure 108, but the compare level (blue) changes at a mid-ramp point (5T/3) instead of at a wrap. This causes an additional transition (glitch) in the output pulse (blue) at that time.

The behaviour becomes even more perplexing if the \( TOP \) register is also modified. It would be difficult for software to write to \( CC \) or \( TOP \) with the correct timing. To solve this, each slice has two copies of the \( CC \) and \( TOP \) registers: one copy which software can modify, and another, internal copy which is updated from the first register at the instant the counter wraps. Software can modify its copy of the register at will, but the changes are not captured by the PWM output until the next wrap.

Figure 110 shows the sequence of events where a software interrupt handler changes the value of \( CC\_A \) each time the counter wraps.

Figure 110. Each counter wrap causes the interrupt request signal to assert. The processor enters its interrupt handler, writes to its copy of the CC register, and clears the interrupt. When the counter wraps again, the latched version of the CC register is instantaneously updated with the most recent value written by software, and this value controls the duty cycle for the next period. The IRQ is reasserted so that software can write another fresh value to its copy of the CC register.

Timing diagram for Figure 110 showing Counter at top, IRQ, CC_A, and CC_A latched signals.

The diagram shows four digital signals over time. The 'Counter at top' signal is a periodic square wave that wraps from a high value to 0. The 'IRQ' signal is a narrow pulse that asserts (goes high) at each counter wrap. The 'CC_A' signal is a periodic square wave that changes value at each counter wrap, with values 0, 1, 2, and 3 shown. The 'CC_A latched' signal is a square wave that is updated to the current value of CC_A at each counter wrap, indicated by a shaded region during the wrap event.

Timing diagram for Figure 110 showing Counter at top, IRQ, CC_A, and CC_A latched signals.

There is no limitation on what values can be written to CC or TOP , or when they are written. In normal PWM mode ( CSR_PH_CORRECT is 0) the latched copies are updated when the counter wraps to 0, which occurs once every TOP + 1 cycles. In phase-correct mode ( CSR_PH_CORRECT is 1), the latched copies are updated on the 0 to 0 count transition, i.e. the point where the counter stops counting downward and begins to count upward again.

4.5.2.4. Clock Divider

Each slice has a fractional clock divider, configured by the DIV register. This is an 8 integer bit, 4 fractional bit clock divider, which allows the count rate to be slowed by up to a factor of 256. The clock divider allows much lower output frequencies to be achieved – approximately 7.5Hz from a 125MHz system clock. Lower frequencies than this will require a system timer interrupt (Section 4.6)

It does this by generating an enable signal which gates the operation of the counter.

Figure 111. The clock divider generates an enable signal. The counter only counts on cycles where this signal is high. A clock divisor of 1 causes the enable to be asserted on every cycle, so the counter counts by one on every system clock cycle. Higher divisors cause the count enable to be asserted less frequently. Fractional division achieves an average fractional counting rate by spacing some enable pulses further apart than others.

Timing diagram for Figure 111 showing DIV_INT, DIV_FRAC, and Counter enable signals for different divisors.

The diagram shows three sets of signals for different clock divisors. Each set includes DIV_INT , DIV_FRAC , and Counter enable signals.
- For divisor 1: DIV_INT is 1, DIV_FRAC is .0, and Counter enable is a continuous high signal.
- For divisor 3: DIV_INT is 3, DIV_FRAC is .0, and Counter enable is a square wave that is high for 1 out of every 3 clock cycles.
- For divisor 2 with a fractional part: DIV_INT is 2, DIV_FRAC is .5, and Counter enable is a square wave that is high for 1.5 out of every 2 clock cycles, achieved by having some high pulses be wider than others.

Timing diagram for Figure 111 showing DIV_INT, DIV_FRAC, and Counter enable signals for different divisors.

The fractional divider is a first-order delta-sigma type.

The clock divider also allows the effective count range to be extended, when using level-sensitive or edge-sensitive modes to take duty cycle or frequency measurements.

4.5.2.5. Level-sensitive and Edge-sensitive Triggering

Figure 112. PWM slice event selection. The counter advances when its enable input is high, and this enable is generated in two sequential stages. First, any one of four event types (always on, pin B high, pin B rise, pin B fall) can generate enable pulses for the fractional clock divider. The divider can reduce the rate of the enable pulses, before passing them on to the counter.

Figure 112: PWM slice event selection block diagram. An 'Event select' block has four inputs: '1' (always on), 'Input (pin B)', 'Rising edge', and 'Falling edge'. Its output 'EN' goes to a 'Fractional Clock Divider (8.4)'. The divider also receives 'Phase Advance' and 'Phase Retard' inputs. The divider's output is 'Count enable'.
graph LR
    EventSelect[Event select]
    InputB[Input (pin B)]
    RisingEdge[Rising edge]
    FallingEdge[Falling edge]
    EN[EN]
    Divider[Fractional Clock Divider (8.4)]
    PhaseAdvance[Phase Advance]
    PhaseRetard[Phase Retard]
    CountEnable[Count enable]

    EventSelect --> EN
    InputB --> EventSelect
    RisingEdge --> EventSelect
    FallingEdge --> EventSelect
    EN --> Divider
    PhaseAdvance --> Divider
    PhaseRetard --> Divider
    Divider --> CountEnable
  
Figure 112: PWM slice event selection block diagram. An 'Event select' block has four inputs: '1' (always on), 'Input (pin B)', 'Rising edge', and 'Falling edge'. Its output 'EN' goes to a 'Fractional Clock Divider (8.4)'. The divider also receives 'Phase Advance' and 'Phase Retard' inputs. The divider's output is 'Count enable'.

By default, each slice's counter is free-running, and will count continuously whenever the slice is enabled. There are three other options available:

These modes are selected by the DIVMODE field in each slice's CSR. In free-running mode, the A and B pins are both outputs. In any other mode, the B pin becomes an input, and controls the operation of the counter. CC_B is ignored when not in free-running mode.

By allowing the slice to run for a fixed amount of time in level-sensitive or edge-sensitive mode, it's possible to measure the duty cycle or frequency of an input signal. Due to the type of edge-detect circuit used, the low period and high period of the measured signal must both be strictly greater than the system clock period when taking frequency measurements.

The clock divider is still operational in level-sensitive and edge-sensitive mode. At maximum division (writing 0 to DIV_INT), the counter will only advance once per 256 high input cycles in level-sensitive modes, or once per 256 edges in edge-sensitive mode. This allows longer-running measurements to be taken, although the resolution is still just 16 bits.

Pico Examples: https://github.com/raspberrypi/pico-examples/blob/master/pwm/measure_duty_cycle/measure_duty_cycle.c Lines 19 - 37

19 float measure_duty_cycle(uint gpio) {
20     // Only the PWM B pins can be used as inputs.
21     assert(pwm_gpio_to_channel(gpio) == PWM_CHAN_B);
22     uint slice_num = pwm_gpio_to_slice_num(gpio);
23
24     // Count once for every 100 cycles the PWM B input is high
25     pwm_config cfg = pwm_get_default_config();
26     pwm_config_set_clkdiv_mode(&cfg, PWM_DIV_B_HIGH);
27     pwm_config_set_clkdiv(&cfg, 100);
28     pwm_init(slice_num, &cfg, false);
29     gpio_set_function(gpio, GPIO_FUNC_PWM);
30
31     pwm_set_enabled(slice_num, true);
32     sleep_ms(10);
33     pwm_set_enabled(slice_num, false);
34     float counting_rate = clock_get_hz(clk_sys) / 100;
35     float max_possible_count = counting_rate * 0.01;
36     return pwm_get_counter(slice_num) / max_possible_count;
37 }
  

4.5.2.6. Configuring PWM Period

When free-running, the period of a PWM slice's output (measured in system clock cycles) is controlled by three parameters:

The slice counts from 0 to TOP , and then either wraps, or begins counting backward, depending on the setting of CSR_PH_CORRECT . The rate of counting is slowed by the clock divider, with a maximum speed of one count per cycle, and a

minimum speed of one count per \( 255\frac{15}{16} \) cycles. The period in clock cycles can be calculated as:

\[ \text{period} = (\text{TOP} + 1) \times (\text{CSR\_PH\_CORRECT} + 1) \times \left( \text{DIV\_INT} + \frac{\text{DIV\_FRAC}}{16} \right) \]

The output frequency can then be determined based on the system clock frequency:

\[ f_{PWM} = \frac{f_{sys}}{\text{period}} = \frac{f_{sys}}{(\text{TOP} + 1) \times (\text{CSR\_PH\_CORRECT} + 1) \times \left( \text{DIV\_INT} + \frac{\text{DIV\_FRAC}}{16} \right)} \]

4.5.2.7. Interrupt Request (IRQ) and DMA Data Request (DREQ)

The PWM block has a single IRQ output. The interrupt status registers INTR , INTS and INTE allow software to control which slices will assert this IRQ output, to check which slices are the cause of the IRQ's assertion, and to clear and acknowledge the interrupt.

A slice generates an interrupt request each time its counter wraps (or, if CSR_PH_CORRECT is enabled, each time the counter returns to 0). This sets the flag corresponding to this slice in the raw interrupt status register, INTR . If this slice's interrupt is enabled in INTE , then this flag will cause the PWM block's IRQ to be asserted, and the flag will also appear in the masked interrupt status register INTS .

Flags are cleared by writing a mask back to INTR . This is demonstrated in the "LED fade" SDK example.

This scheme allows multiple slices to generate interrupts concurrently, and a system interrupt handler to determine which slices caused the most recent interruption, and handle appropriately. Normally this would mean reloading those slices' TOP or CC registers, but the PWM block can also be used as a source of regular interrupt requests for non-PWM-related purposes.

The same pulse which sets the interrupt flag in INTR is also available as a one-cycle data request to the RP2040 system DMA. For each cycle the DMA sees a DREQ asserted, it will make one data transfer to its programmed location, in as timely a manner as possible. In combination with the double-buffered behaviour of CC and TOP , this allows the DMA to efficiently stream data to a PWM slice at a rate of one transfer per counter period. Alternatively, a PWM slice could serve as a pacing timer for DMA transfers to some other memory-mapped hardware.

4.5.2.8. On-the-fly Phase Adjustment

For some applications it is necessary to control the phase relationship between two PWM outputs on different slices.

The global enable register EN contains an alias of the CSR_EN flag for each slice, and allows multiple slices to be started and stopped simultaneously. If two slices with the same output frequency are started at the same time, they will run in perfect lockstep, and have a fixed phase relationship, determined by the initial counter values.

The CSR_PH_ADV and CSR_PH_RET fields will advance or retard a slice's output phase by one count, whilst it is running. They do so by inserting or deleting pulses from the clock enable (the output of the clock divider), as shown in Figure 113 .

Figure 113. The clock enable signal, output by the clock divider, controls the rate of counting. Phase advance forces the clock enable high on cycles where it is low, causing the counter to jump forward by one count. Phase retard forces the clock enable low when it would be high, holding the counter back by one count.

Timing diagram showing clock, clock enable, and counter behavior for phase advance and phase retard.

The figure consists of three vertically stacked timing diagrams. Each diagram shows a 'Clock' signal (a regular square wave), a 'DIV_INT' signal (a constant high level), and a 'Count' signal (a sequence of numbers 0 through 5 or 6). The 'Clock enable' signal is shown as a series of pulses that occur at specific clock cycles. In the first diagram, the clock enable is high for cycles 0, 1, 2, 3, 4, and 5. In the second diagram, the clock enable is high for cycles 0, 1, 2, 3, 4, 5, and 6. In the third diagram, the clock enable is high for cycles 0, 1, 2, 3, and 4. The 'Count' signal is shown as a sequence of numbers 0 through 5 or 6, with the count increasing by one for each clock cycle where the clock enable is high. The 'Clock enable' signal is controlled by the 'CSR_PH_ADV' and 'CSR_PH_RET' signals. In the first diagram, 'CSR_PH_ADV' is high for cycles 1 and 2, causing the clock enable to be high for those cycles. In the second diagram, 'CSR_PH_RET' is high for cycles 1 and 2, causing the clock enable to be low for those cycles. In the third diagram, 'CSR_PH_RET' is high for cycles 1 and 2, causing the clock enable to be low for those cycles.

Timing diagram showing clock, clock enable, and counter behavior for phase advance and phase retard.

The counter can not count faster than once per cycle, so PH_ADV requires DIV_INT > 1 or DIV_FRAC > 0. Likewise, the counter will not start to count backward if PH_RET is asserted when the clock enable is permanently low.

To advance or retard the phase by one count, software writes 1 to PH_ADV or PH_RET . Once an enable pulse has been inserted or deleted, the PH_ADV or PH_RET register bit will return to 0, and software can poll the CSR until this happens. PH_ADV will always insert a pulse into the next available gap, and PH_RET will always delete the next available pulse.

4.5.3. List of Registers

The PWM registers start at a base address of 0x40050000 (defined as PWM_BASE in SDK).

Table 516. List of PWM registers

OffsetNameInfo
0x00CH0_CSRControl and status register
0x04CH0_DIVINT and FRAC form a fixed-point fractional number. Counting rate is system clock frequency divided by this number. Fractional division uses simple 1st-order sigma-delta.
0x08CH0_CTRDirect access to the PWM counter
0x0cCH0_CCCounter compare values
0x10CH0_TOPCounter wrap value
0x14CH1_CSRControl and status register
0x18CH1_DIVINT and FRAC form a fixed-point fractional number. Counting rate is system clock frequency divided by this number. Fractional division uses simple 1st-order sigma-delta.
0x1cCH1_CTRDirect access to the PWM counter
0x20CH1_CCCounter compare values
0x24CH1_TOPCounter wrap value
0x28CH2_CSRControl and status register
0x2cCH2_DIVINT and FRAC form a fixed-point fractional number. Counting rate is system clock frequency divided by this number. Fractional division uses simple 1st-order sigma-delta.
OffsetNameInfo
0x30CH2_CTRDirect access to the PWM counter
0x34CH2_CCCounter compare values
0x38CH2_TOPCounter wrap value
0x3cCH3_CSRControl and status register
0x40CH3_DIVINT and FRAC form a fixed-point fractional number.
Counting rate is system clock frequency divided by this number.
Fractional division uses simple 1st-order sigma-delta.
0x44CH3_CTRDirect access to the PWM counter
0x48CH3_CCCounter compare values
0x4cCH3_TOPCounter wrap value
0x50CH4_CSRControl and status register
0x54CH4_DIVINT and FRAC form a fixed-point fractional number.
Counting rate is system clock frequency divided by this number.
Fractional division uses simple 1st-order sigma-delta.
0x58CH4_CTRDirect access to the PWM counter
0x5cCH4_CCCounter compare values
0x60CH4_TOPCounter wrap value
0x64CH5_CSRControl and status register
0x68CH5_DIVINT and FRAC form a fixed-point fractional number.
Counting rate is system clock frequency divided by this number.
Fractional division uses simple 1st-order sigma-delta.
0x6cCH5_CTRDirect access to the PWM counter
0x70CH5_CCCounter compare values
0x74CH5_TOPCounter wrap value
0x78CH6_CSRControl and status register
0x7cCH6_DIVINT and FRAC form a fixed-point fractional number.
Counting rate is system clock frequency divided by this number.
Fractional division uses simple 1st-order sigma-delta.
0x80CH6_CTRDirect access to the PWM counter
0x84CH6_CCCounter compare values
0x88CH6_TOPCounter wrap value
0x8cCH7_CSRControl and status register
0x90CH7_DIVINT and FRAC form a fixed-point fractional number.
Counting rate is system clock frequency divided by this number.
Fractional division uses simple 1st-order sigma-delta.
0x94CH7_CTRDirect access to the PWM counter
0x98CH7_CCCounter compare values
0x9cCH7_TOPCounter wrap value
OffsetNameInfo
0xa0ENThis register aliases the CSR_EN bits for all channels. Writing to this register allows multiple channels to be enabled or disabled simultaneously, so they can run in perfect sync. For each channel, there is only one physical EN register bit, which can be accessed through here or CHx_CSR.
0xa4INTRRaw Interrupts
0xa8INTEInterrupt Enable
0xacINTFInterrupt Force
0xb0INTSInterrupt status after masking & forcing

PWM: CH0_CSR, CH1_CSR, ..., CH6_CSR, CH7_CSR Registers

Offsets: 0x00, 0x14, ..., 0x78, 0x8c

Description

Control and status register

Table 517. CH0_CSR, CH1_CSR, ..., CH6_CSR, CH7_CSR Registers

BitsDescriptionTypeReset
31:8Reserved.--
7PH_ADV : Advance the phase of the counter by 1 count, while it is running. Self-clearing. Write a 1, and poll until low. Counter must be running at less than full speed ( \( \text{div\_int} + \text{div\_frac} / 16 > 1 \) )SC0x0
6PH_RET : Retard the phase of the counter by 1 count, while it is running. Self-clearing. Write a 1, and poll until low. Counter must be running.SC0x0
5:4DIVMODERW0x0
Enumerated values:
0x0 → DIV: Free-running counting at rate dictated by fractional divider
0x1 → LEVEL: Fractional divider operation is gated by the PWM B pin.
0x2 → RISE: Counter advances with each rising edge of the PWM B pin.
0x3 → FALL: Counter advances with each falling edge of the PWM B pin.
3B_INV : Invert output BRW0x0
2A_INV : Invert output ARW0x0
1PH_CORRECT : 1: Enable phase-correct modulation. 0: Trailing-edgeRW0x0
0EN : Enable the PWM channel.RW0x0

PWM: CH0_DIV, CH1_DIV, ..., CH6_DIV, CH7_DIV Registers

Offsets: 0x04, 0x18, ..., 0x7c, 0x90

Description

INT and FRAC form a fixed-point fractional number.

Counting rate is system clock frequency divided by this number.

Fractional division uses simple 1st-order sigma-delta.

Table 518. CH0_DIV, CH1_DIV, ..., CH6_DIV, CH7_DIV Registers

BitsDescriptionTypeReset
31:12Reserved.--
11:4INTRW0x01
3:0FRACRW0x0

PWM: CH0_CTR, CH1_CTR, ..., CH6_CTR, CH7_CTR Registers

Offsets: 0x08, 0x1c, ..., 0x80, 0x94

Table 519. CH0_CTR, CH1_CTR, ..., CH6_CTR, CH7_CTR Registers

BitsDescriptionTypeReset
31:16Reserved.--
15:0Direct access to the PWM counterRW0x0000

PWM: CH0_CC, CH1_CC, ..., CH6_CC, CH7_CC Registers

Offsets: 0x0c, 0x20, ..., 0x84, 0x98

Description

Counter compare values

Table 520. CH0_CC, CH1_CC, ..., CH6_CC, CH7_CC Registers

BitsDescriptionTypeReset
31:16BRW0x0000
15:0ARW0x0000

PWM: CH0_TOP, CH1_TOP, ..., CH6_TOP, CH7_TOP Registers

Offsets: 0x10, 0x24, ..., 0x88, 0x9c

Table 521. CH0_TOP, CH1_TOP, ..., CH6_TOP, CH7_TOP Registers

BitsDescriptionTypeReset
31:16Reserved.--
15:0Counter wrap valueRW0xffff

PWM: EN Register

Offset: 0xa0

Description

This register aliases the CSR_EN bits for all channels.
Writing to this register allows multiple channels to be enabled or disabled simultaneously, so they can run in perfect sync.
For each channel, there is only one physical EN register bit, which can be accessed through here or CHx_CSR.

Table 522. EN Register

BitsDescriptionTypeReset
31:8Reserved.--
7CH7RW0x0
6CH6RW0x0
5CH5RW0x0
4CH4RW0x0
3CH3RW0x0
BitsDescriptionTypeReset
2CH2RW0x0
1CH1RW0x0
0CH0RW0x0

PWM: INTR Register

Offset: 0xa4

Description

Raw Interrupts

Table 523. INTR Register

BitsDescriptionTypeReset
31:8Reserved.--
7CH7WC0x0
6CH6WC0x0
5CH5WC0x0
4CH4WC0x0
3CH3WC0x0
2CH2WC0x0
1CH1WC0x0
0CH0WC0x0

PWM: INTE Register

Offset: 0xa8

Description

Interrupt Enable

Table 524. INTE Register

BitsDescriptionTypeReset
31:8Reserved.--
7CH7RW0x0
6CH6RW0x0
5CH5RW0x0
4CH4RW0x0
3CH3RW0x0
2CH2RW0x0
1CH1RW0x0
0CH0RW0x0

PWM: INTF Register

Offset: 0xac

Description

Interrupt Force

Table 525. INTF Register

BitsDescriptionTypeReset
31:8Reserved.--
7CH7RW0x0
6CH6RW0x0
5CH5RW0x0
4CH4RW0x0
3CH3RW0x0
2CH2RW0x0
1CH1RW0x0
0CH0RW0x0

PWM: INTS Register

Offset: 0xb0

Description

Interrupt status after masking & forcing

Table 526. INTS Register

BitsDescriptionTypeReset
31:8Reserved.--
7CH7RO0x0
6CH6RO0x0
5CH5RO0x0
4CH4RO0x0
3CH3RO0x0
2CH2RO0x0
1CH1RO0x0
0CH0RO0x0