I'm looking for guidance on enabling all four PWM channels on the nrf5340-DK MCU development kit using SDKv2.3, which includes Zephyr and other complex software abstractions. Can you help me with this?

Hi,

I noticed that the blinky_pwm examples only demonstrate PWM control for the pwm_led0 tied to led 1 on the development board. Can you guide me on how to modify the demo to enable PWM control on any pin, and use all four channels?

Parents Reply Children
  • For others who might find it helpful.
    Open the file where your nrf sdk is installed:
    <path_to_nrf_sdk>\v2.3.0\zephyr\boards\arm\nrf5340dk_nrf5340\nrf5340_cpuapp_common.dts

    Make changes to add the 4 channels:


        pwmleds {
            compatible = "pwm-leds";
            pwm_led0: pwm_led_0 {
                pwms = <&pwm0 0 PWM_MSEC(20) PWM_POLARITY_INVERTED>;
            };
            pwm_led1: pwm_led_1 {
                pwms = <&pwm0 1 PWM_MSEC(20) PWM_POLARITY_INVERTED>;
            };
            pwm_led2: pwm_led_2 {
                pwms = <&pwm0 2 PWM_MSEC(20) PWM_POLARITY_INVERTED>;
            };
            pwm_led3: pwm_led_3 {
                pwms = <&pwm0 3 PWM_MSEC(20) PWM_POLARITY_INVERTED>;
            };
        };

    open the pinctrl file

    <path_to_nrf_sdk>\v2.3.0\zephyr\boards\arm\nrf5340dk_nrf5340\nrf5340_cpuapp_common-pinctrl.dtsi

    and add:

    assign the pins where you want PWM to be configured to. I believe any GPIO pin can be configured as a PWM. I have assigned to pin 23,24,29. Pin 28 is default.


        pwm0_default: pwm0_default {
            group1 {
                psels = <NRF_PSEL(PWM_OUT0, 0, 28)>,
                        <NRF_PSEL(PWM_OUT1, 0, 23)>,
                        <NRF_PSEL(PWM_OUT2, 0, 24)>,
                        <NRF_PSEL(PWM_OUT3, 0, 29)>;
            };
        };

        pwm0_sleep: pwm0_sleep {
            group1 {
                psels = <NRF_PSEL(PWM_OUT0, 0, 23)>,
                        <NRF_PSEL(PWM_OUT1, 0, 23)>,
                        <NRF_PSEL(PWM_OUT2, 0, 24)>,
                        <NRF_PSEL(PWM_OUT3, 0, 29)>;
                low-power-enable;
            };
        };

    If you are using the blinky_pwm examples you can extend it to include all 4 channels by adding:

    static const struct pwm_dt_spec pwm_led0 = PWM_DT_SPEC_GET(DT_ALIAS(pwm_led0));
    static const struct pwm_dt_spec pwm_led1 = PWM_DT_SPEC_GET(DT_ALIAS(pwm_led1));
    static const struct pwm_dt_spec pwm_led2 = PWM_DT_SPEC_GET(DT_ALIAS(pwm_led2));
    static const struct pwm_dt_spec pwm_led3 = PWM_DT_SPEC_GET(DT_ALIAS(pwm_led3));

    Thank you!

  • Hi Anthony,

    I appreciate the additional hint for future readers.

    While directly modifying the board definition like you are doing is a working approach, I also recommend using overlay files. You can read more about DTS and overlay files here:

    https://developer.nordicsemi.com/nRF_Connect_SDK/doc/2.3.0/zephyr/build/dts/index.html

    Eventually, you will want to use a custom board definition tailored for your product.

    Thanks again Smiley

  • Will open another ticket regarding overlays if I run into issues. Thank you!

  • No problem. Feel free to close this ticket by verifying any answer(s) you deem worked, or I can also close it sometime later.

Related