This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

PWM Device Tree

Hello!

I have the following component in my device tree:

	pwmdevs {
		compatible = "pwm-leds";
		pwmTest0: pwm_device_test_0 {
			pwms = < &pwm1 0x14 >;
			label = "PWM Test";
		};
	};
	
	aliases {
		myDev-pwm = &pwmTest0;
	};

I would like to retrieve the PWM device and the associated pin number (pwm1 and 0x14). I tried:

#define NODE DT_LABEL(myDev_pwm)
#define PIN DT_GPIO_PIN(NODE, gpios)

Which looks like the code I am using to retrieve a GPIO. But I get an error during compile time.

Currently, I am using the definition in the devicetree.h but it is not ideal:

#define PWM_NODE DT_N_S_soc_S_peripheral_40000000_S_pwm_21000_P_label
#define PIN   DT_N_S_pwmdevs_S_<alias>_P_pwms_IDX_0_VAL_channel
 This gives me the node name "PWM1" and pin number 0x14 as expected and I can use them in my code in order to retrieve the device structure (device_get_binding) and set the PWM pin (pwm_pin_set_cycles)

How can I use the Device Tree macros to retrieve the PWM and associated pin?

Thanks!

  • Hi,

    I would like to retrieve the PWM device and the associated pin number (pwm1 and 0x14)

    Did you take a look at the blinky_pwm sample in Zephyr ?

    You can get the GPIO / ch0-pin with:

    #define PWM_CHANNEL DT_PWMS_CHANNEL(PWM_LED0_NODE)
    And the PWM label with 
    #define PWM_LABEL   DT_PWMS_LABEL(PWM_LED0_NODE)
    where PWM_LED0_NODE is defined like this:
    #define PWM_LED0_NODE	DT_ALIAS(pwm_led0)

Related