Compiler error regarding PWM configuration

Hi all,

I'm trying to use the nrf Connect SDK and configure PWM to use in one pin from nordif nrf52DK. The final chip will be the nrf52810. I have configured the builder to use the nrf52dk_nrf52810. I was using the "blinky" example and trying to add a PWM configuration to it. I have this configurations:

prj.conf:

# PWM
CONFIG_PWM=y
CONFIG_PWM_NRFX=y

nrf52dk_nrf52810.overlay:

/ {
    gpiocustom {
        compatible = "gpio-keys";
        gpiocus0: gpiocus_0 {
            gpios = <&gpio0 11 GPIO_ACTIVE_LOW>;
            label = "Custom gpio 11";
        };
        gpiocus1: gpiocus_1 {
            gpios = <&gpio0 12 GPIO_ACTIVE_LOW>;
            label = "Custom gpio 12";
        };
    };

    soc {
        pwm0: [email protected]  {
            compatible = "nordic,nrf-pwm";
            reg = <0x4001c000 0x1000>;
            label = "PWM_0";
            ch0-pin = <22>;
            #pwm-cells = <3>;
        };
    };

    aliases {
        gpiocus0 = &gpiocus0;
        gpiocus1 = &gpiocus1;
        pwm0 = &pwm0;
    };
};

&sw_pwm {
    status = "okay";
    channel-gpios = <&gpio0 23 GPIO_ACTIVE_HIGH>;
};

Anyone can tell me if the sw_pwm needs to be used to generate the pwm? or only with the pwm is enough?

I added this line in the main.c:

#define DT_ALIAS_PWM_0_LABEL DT_ALIAS(pwm0)

And then, when I try to compile I get this error:

In file included from C:\ncs\v2.0.0\zephyr\include\zephyr\devicetree.h:19,
                 from C:\ncs\v2.0.0\zephyr\include\zephyr\arch\arm\aarch32\arch.h:20,
                 from C:\ncs\v2.0.0\zephyr\include\zephyr\arch\cpu.h:19,
                 from C:\ncs\v2.0.0\zephyr\include\zephyr\kernel_includes.h:33,
                 from C:\ncs\v2.0.0\zephyr\include\zephyr\kernel.h:17,
                 from C:\ncs\v2.0.0\zephyr\include\zephyr\zephyr.h:18,
                 from c:\nordics\blinky\src\main.c:7:
../src/main.c: In function 'main':
c:\nordics\blinky\build_2\zephyr\include\generated\devicetree_unfixed.h:4892:36: error: 'DT_N_S_soc_S_pwm_4001c000' undeclared (first use in this function); did you mean 'DT_N_S_soc_S_pwm_4001c000_ORD'?
 4892 | #define DT_N_ALIAS_pwm0            DT_N_S_soc_S_pwm_4001c000
      |                                    ^~~~~~~~~~~~~~~~~~~~~~~~~
C:\ncs\v2.0.0\zephyr\include\zephyr\devicetree.h:2991:24: note: in expansion of macro 'DT_N_ALIAS_pwm0'
 2991 | #define DT_CAT(a1, a2) a1 ## a2
      |                        ^~
C:\ncs\v2.0.0\zephyr\include\zephyr\devicetree.h:213:25: note: in expansion of macro 'DT_CAT'
  213 | #define DT_ALIAS(alias) DT_CAT(DT_N_ALIAS_, alias)
      |                         ^~~~~~
c:\nordics\blinky\src\main.c:20:30: note: in expansion of macro 'DT_ALIAS'
   20 | #define DT_ALIAS_PWM_0_LABEL DT_ALIAS(pwm0)

How can I solve it? Also I have tried the nRF5 SDK and it was really easy to get the PWM working, but I will need to use zephyr to implement a bootloader in the future.

Hope you can help me.

Best regards

  • Hi Einar, sorry for the delay in my response.

    If I use the .overlay file that you sent now it builds, but when I want to use the PWM I can't initiate it (when the binary is flashed in the board, it gives an error)

    Also I have another question. I'm using the following code to use the PWM:

    static int pwm_init(void)
    {
    	static nrfx_pwm_config_t const config0 =
        {
            .output_pins =
            {
                22,						// channel 0
                23,						// channel 1
                NRFX_PWM_PIN_NOT_USED,	// channel 2
                NRFX_PWM_PIN_NOT_USED	// channel 3
            },
            .irq_priority = 5,
            .base_clock   = NRF_PWM_CLK_500kHz,
            .count_mode   = NRF_PWM_MODE_UP,
            .top_value    = PWM_COUNTERTOP,
            .load_mode    = NRF_PWM_LOAD_INDIVIDUAL,
            .step_mode    = NRF_PWM_STEP_AUTO
        };
        return (nrfx_pwm_init(&my_pwm, &config0, NULL, NULL) == NRFX_SUCCESS) ? 0 : -1;
    
    }

    It's ok to use this code in order to use PWM? For me is more clear to use it this way, but there is another option?

    Also, I want to add a long press button function but I can't find a good example to use it.

    Hope you can help me.

    Best regards

  • Hi,

    clake said:
    If I use the .overlay file that you sent now it builds, but when I want to use the PWM I can't initiate it (when the binary is flashed in the board, it gives an error)

    Which error do you get?

    clake said:
    It's ok to use this code in order to use PWM? For me is more clear to use it this way, but there is another option?

    Here you are using nrfx, so you are effectively bypassing the Zephyr PWM API. That is OK, but the required config is a bit different. You may find the unofficial NRFX PWM Simple Example useful for that (it is not entirely up to date, but shows the main principles).

  • Hi Einar, thanks for the reply.

    So the error was a wrong configuration inside the prj.conf and for that I have the error in the PWM. Now I can use it and the final prj.conf was:

    CONFIG_GPIO=y
    
    CONFIG_NRFX_PWM0=y

    Before I have the value CONFIG_PWM_NRFX=y and with that I have to add the sw_pwm to build.

    I have another question but is different, how can I set the nrf52810 to be in deep sleep or power off? there is an example for that? I saw this https://developer.nordicsemi.com/nRF_Connect_SDK/doc/latest/nrf/samples/nfc/system_off/README.html but I can't compile for the nrf52810.

    Best regards

  • Hi,

    That is good to hear.

    Regarding system off, the method demonstrated by the nRF5x System Off demo is valid for any nRF5x device (and it does build for nrf52dk_nrf52810). Please make a new question if you need more assistance on this though, as it is best to keep separate topics in separate threads.

Related