What is the recommended way to use nRF52840 advanced PWM sequence and looping functions with nRF Connect SDK v2.9.0?

Hi,

I am developing an application for an nRF52840 using nRF Connect SDK v2.9.0.  The application currently uses the Zephyr driver for simple PWM functions on several GPIOs. Now, I need to add PWM sequences and looping as supported by the SOC on other GPIOs. Correct me if I am wrong, but I don't think the Zephyr driver can support the Nordic PWM module in this way.

What is the best way to do this? Some questions that come to mind: Can the Zephyr PWM driver and the nrfx PWM driver live together in the same application? Must I chose one driver or the other? How does DeviceTree manage this? I'm sure I'll have more questions once the discussion is opened.

Thank you,

Ken

  • Hi!

    Let's say you have something like this in pinctrl dts

    	pwm0_default: pwm0_default {
    		group1 {
    			psels = <NRF_PSEL(PWM_OUT0, 0, 13)>;
    			nordic,invert;
    		};
    	}

    then something like this:

    #include <stdio.h>
    
    #include <zephyr/drivers/pwm.h>
    #include <zephyr/drivers/gpio.h>
    #include <zephyr/pm/device.h>
    #include <zephyr/pm/device_runtime.h>
    #include <zephyr/drivers/gpio.h>
    
    #ifdef CONFIG_PINCTRL
    #include <pinctrl_soc.h>
    #endif
    
    #define PWM_PIN(node_id, prop, idx) \
    	NRF_GET_PIN(DT_PROP_BY_IDX(node_id, prop, idx)),
    
    	static const uint8_t pwm_pin_nums[] = {
    		DT_FOREACH_CHILD_VARGS(
    			DT_PINCTRL_BY_NAME(DT_NODELABEL(pwm0), default, 0),
    			DT_FOREACH_PROP_ELEM, psels, PWM_PIN
    		)
    	};
    
    
    int main(void)
    {
    	printf("Hello World! %s\n", CONFIG_BOARD_TARGET);
    
        for (size_t i = 0; (i < ARRAY_SIZE(pwm_pin_nums)); i++) {
            printf("PWM LEDS[%d]: %d\n",i, pwm_pin_nums[i]);
        }
    
    	return 0;
    }

    Will give you this output:

    *** Booting nRF Connect SDK v2.9.0-7787b2649840 ***
    *** Using Zephyr OS v3.7.99-1f8f3dc29142 ***
    Hello World! nrf52840dk/nrf52840
    PWM LEDS[0]: 13

  • Hi Sigurd,

    I have modified my application to use the nrfx PWM driver, disabled PWMs in Devicetree, and use Devicetree macros to get my PWM pin configurations per your recommendations. Thank you on all that. However, I am not able to build my application. I have a failure adding the build configuration.  Screen shot is below. How can I get past this?

    Thanks,

    Ken

  • Instead of 

    CONFIG_NRFX_PWM=y

    Try e.g.

    CONFIG_NRFX_PWM0=y

  • Hi Sigurd,

    That worked.  I'm currently using 2 PWM modules so I needed CONFIG_NRFX_PWM1=y as well.

    Question:  Where in the latest documentation can I find instruction to set these variables if I intend to use the nfrx PWM driver?  (I think it may be missing)

    Thanks,

    Ken

Related