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

Parents Reply
  • Hi!

    Sigurd said:
    github.com/.../quote]

    Looks like the sample is not been updated for some time. I suggest e.g. starting with the hello_world zephyr sample, and start adding the relevant PWM functionality to that sample.

    Ken58 said:
    If I use the nrfx driver/API for PWM, can I continue to use the Zephyr GPIO driver/API?

    Yes.

    Ken58 said:
    What is the impact I will see in DeviceTree for PWM when using the nrfx driver/API?

    I suggest disabling PWM in the devicetree when using the nrfx driver directly, to avoid any conflict.

    Ken58 said:
    As I look into how this can be done, is there an specific documentation section on using Zephyr with nrfx at the same time?  Is there a cookbook for this situation?  Is there a particular sample showing simultaneous use of Zephyr drivers for some devices and nrfx drivers/APIs for others?

    There are 2 nrfx sample in the Zephyr repo here: https://github.com/nrfconnect/sdk-zephyr/tree/main/samples/boards/nordic

    [/quote]
Children
  • Thank you Sigurd.  I'll let you know if I run into any problems.

    Ken

  • Hi Sigurd,

    To this point, I have used overlays or custom board definition to allow my application code to be unchanged when driving PWM LEDs on 3 different boards where the GPIOs used have been different.  When disabling PWMs in the devicetree and using nrfx driver directly, how would you recommend getting the correct GPIOs configuration for each of the 3 boards supported without changing the application code in each instance?

    Ken

  • Hi!

    Ken58 said:
    how would you recommend getting the correct GPIOs configuration for each of the 3 boards supported without changing the application code in each instance?

    You could still have the pinctrl pwm devicetree node present, and use the Devicetree API to get the correct pins when using the nrfx API.

    https://docs.nordicsemi.com/bundle/zephyr-apis-2.9.0/page/group_devicetree-pinctrl.html

  • Hi Sigurd,

    Can you give me an example of what the code using the Devicetree API you referenced above would look like?  For for a simple example: using a nRF52840 Dongle, how would I get the pin for the pwm-led0 keeping in mind that pwm0 will have a status = "disabled" in an overlay since the nrfx PWM API will be used?

    Thanks,

    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

Related