Use PWMs in real-time capable applications

Important

This is the Azure Sphere (Legacy) documentation. Azure Sphere (Legacy) is retiring on 27 September 2027, and users must migrate to Azure Sphere (Integrated) by this time. Use the Version selector located above the TOC to view the Azure Sphere (Integrated) documentation.

Azure Sphere supports Pulse Width Modulation. Pulse-width modulation is achieved by varying the duty cycle (ratio of on-time to off-time) of a pulsed digital signal. Pulse-width modulators (PWMs) are used in a wide range of applications such as digital motor control, communication, and digital-to-analog conversion.

This topic describes how to use PWMs in real-time capable applications (RTApp). For information about using PWMs in high-level applications, see Using PWM in high-level applications.

Note

The Azure Sphere OS does not reset peripherals on start-up. Your applications should ensure that peripherals are in a known-good state on start-up or after restart.

MT3620 support for PWM on the real-time cores

This section describes the PWM options that only apply when running Azure Sphere on the MT3620.

The PWM features that the MT3620 supports are listed in MT3620 Support Status. The MT3620 development board user guide describes the pin layout and pin functions.

The MT3620 has 12 PWM channels, identified by PWM0-PWM11. They are organized into 3 groups of 4 channels. Each group is associated with a PWM controller (PWM-CONTROLLER-0, PWM-CONTROLLER-1, PWM-CONTROLLER-2). The PWM channels and GPIO pins GPIO0 through GPIO11 map to the same pins on the MT3620. If your application uses a PWM controller all of the pins associated with that controller are allocated for use as PWM outputs. None of them can be used for GPIO.

For register base addresses, interrupt numbers, clock rate, sampling frequency, and other details about the MT3620, see the MT3620 Datasheet and the MT3620 M4 User Manual; if questions remain, you can request details from Avnet by emailing Azure.Sphere@avnet.com.

The PWM hardware can be configured to use one of three clock frequencies: 32 KHz, 2 MHz (XTAL/13), or 26 MHz (XTAL). On the real-time (RT) cores, a real-time application (RTApp) can select which clock to use as the base. User-defined base frequencies are not supported. However, your application can generate signal frequencies that are derived from the selected base frequency. For an example, see the PWMAudio sample appication in the Azure Sphere Gallery.

Application manifest settings

To use a PWM in an RTApp you add the Pwm capability to the application manifest and specify the PWM controllers that will be used in the application. For example, the following line specifies PWM controller 0 for use in an RTApp that targets the MT3620 reference development board (RDB):

"Capabilities": {
   "Pwm": [ "PWM-CONTROLLER-0" ] }

Azure Sphere application manifest has more details about the application manifest.

The PWM sample applications

The CodethinkLabs repository on GitHub contains a number of sample applications that demonstrate how to use MT3620 supported peripherals in Azure Sphere real-time capable applications. Specifically, the PWM_RTApp_MT3620_BareMetal sample demonstrates how to incorporate a PWM peripheral in a real-time capable application.

The PWMAudio project demonstrates how to use the real-time cores to generate PWM (Pulse Width Modulation) audio. This project is part of the Azure Sphere Gallery, a collection of unmaintained scripts, utilities, and functions.