How to enable PWM on PIN Zephyr

Hello All,

I am trying to enable a PWM output on a Seeed Studio Xiao BLE board using Zephyr.

I am aware that this is a Zephyr issue but hopefully I can get some help here:

I am running the PWM blinky Example and I am unable to get it to work:

So far I understand the following:

In the dts I think this is the relevant code:

pwmleds {
		compatible = "pwm-leds";
		pwm_led0: pwm_led_0 {
			pwms = <&pwm0 2 PWM_MSEC(200) PWM_POLARITY_INVERTED>;

in the above: the pwm output is being set on in port 2 which is pin D0 on the xiao board, which will have the period of 200 ms and the polarity is inverted.

I am assuming the comparability line sets what class of interfaces can be run by the PWM led device, but this is not defined any where so how can I find what pwm-leds is?

Furthermore, how would I set the pwm device such that I can output the PWM on a pin in terms of teh configuration I need to do in the device tree?

Any resources that you can link would be awesome. I have found that the zephyr documentation is not obvious what is going on and I think the YouTube videos just gloss over the underlying operational mechanisms.

Thanks in advance.

Enabling PWM on a pin in Zephyr involves two main steps: device tree configuration and code in your application. Here’s a breakdown:

1. Device Tree Configuration (DTS):

The device tree (DTS) defines the hardware components and their configuration. Here’s how to set up PWM in the DTS:

  • Include necessary overlays: Your project might have overlay files defining specific functionalities. Include the overlay enabling PWM for your desired controller (e.g., timer). Refer to your board’s documentation for details.
  • Define PWM channel: Within the overlay file, define a node for the specific PWM channel you want to use. This node typically includes properties like:
    • status : Set to "okay" to enable the PWM channel.
    • ch0-pin : This property specifies the actual GPIO pin number you want to use for PWM output. Refer to your board’s pinout diagram for valid pin numbers.

2. Application Code:

Once the DTS configures the PWM channel, you can use Zephyr APIs in your application code to control it:

  • Include necessary headers: Include the pwm.h header file to access the PWM functionalities.
  • Device binding: Use the device binding API (DEVICE_DT_GET(dev, ... ) ) to get a reference to the PWM device from the device tree node you defined earlier.
  • PWM configuration: Use functions like pwm_pin_set_cycles or pwm_pin_set_usec to configure the PWM period (cycle time) and pulse width (duty cycle) for your desired output.

Here are some resources that provide more details and code examples:

Remember, the specific steps and function calls might vary depending on your Zephyr version and board configuration. Consult the official documentation and explore examples for the most accurate details.

1 Like