Raspberry Pi CM5 IoT In C - - PWM Using GPIO5
Written by Harry Fairhead   
Monday, 12 May 2025
Article Index
Raspberry Pi CM5 IoT In C - - PWM Using GPIO5
Pulse Density Mode
The PWM Registers
Working with PWM
What Else Can You Use PWM For?

The CM5 supports PWM and you can direct access to its hardware using the GPIO5 library. This is an extract from the newly-published Raspberry Pi Compute Module 5 IoT In C

Raspberry Pi Compute Module 5
IoT In C
Using Linux
Drivers and Gpio5

By Harry Fairhead

CIoTCM5360
Buy from Amazon.

Contents

  1. The CM5 For The IoT
  2. Setting Up the CM5 ***NEW!!
  3. C and Visual Studio Code
  4. Drivers: A First Program
  5. The GPIO Character Drive
  6. GPIO Using I/O Control
  7. GPIO Events
  8. GPIO Hardware With Gpio5 
  9. Some Electronics
  10. The Device Tree
  11. Pulse Width Modulation
  12. SPI Devices
  13. I2C Driver and Gpio5
  14. Sensor Drivers – Linux IIO & hwmon
  15. 1-Wire Bus
  16. The PIO
  17. Going Further With Drivers
  18. Almost Real-Time Linux
  19. Appendix I Gpio5

 

 

In Chapter but not in this extract

  • Pulse Width Modulation
  • Some Basic Pi PWM Facts
  • Software PWM
  • The PWM Driver
  • Simple PWM Functions

Using PWM Hardware – Extending Gpio5

The Linux drivers are easy enough to use, but they don’t allow you to use all four channels, to set the clock frequency or to use any of the additional modes that the hardware supports. An attractive alternative is to access the PWM hardware directly via an extension to the Gpio5 library which takes it beyond just controlling the GPIO lines directly.

The CM5 has four PWM channels which correspond to GPIO lines:

      function 0  function 3
PWM 0  GPIO12   
PWM 1  GPIO13 
PWM 2  GPIO14      GPIO18
PWM 3  GPIO15      GPIO19

It is easier to use GPIO12 to GPIO15 as they can be enabled using the same function code.

All GPIO lines set to PWM expect the PAD to be correctly configured by additional code.

PWM Modes

The CM5’s PWM hardware is more sophisticated than other hardware you might have encountered before. The first big difference is that it can work in a number of modes that generate different types of signal.


The basic PWM signal is generated using a 32-bit counter that rolls over at Range, the range boundary, or at zero depending on whether it is counting up or down. The value of Duty determines when the signal is high.

 PWM1

trailing-edge mode the counter starts at zero and counts up to range before rolling over. The output is a one when the count is less than the value of Duty and a zero when it is greater.

pwm2


In leading-edge mode the count starts at Range and counts down to zero before rolling over. The output is a one when count is less than Duty. This produces a signal 180 degrees out of phase with trailing-edge mode.

 pwm2


In double-edge mode the count starts at Range and counts down to zero and then starts to count up to Range before repeating. The output is high when the count is less than Duty. This produces a “phase-corrected” signal with the pulse at the center of the time slot:

 pwm3

 



Last Updated ( Tuesday, 13 May 2025 )