Trying to use nrfx simple playback command to run RGB LED for specific duration on nrf5340 board

Hello, 

I am trying to use the nrfx driver example to run an RGB led connected externally to the nrf5340 board. 

I dont see the led/pwm cycle running successfully. Can anyone please have a look & suggest corrections?

Device Tree Overlay File:

&sw_pwm {
    status = "okay";
    channel-gpios = <&gpio1 13 PWM_POLARITY_NORMAL>,
                              <&gpio1 14 PWM_POLARITY_NORMAL>,
                             <&gpio1 15 PWM_POLARITY_NORMAL>;
};

/ {
    pwmleds {
        compatible = "pwm-leds";
        status = "okay";

        red_pwm_led: red_pwm_led {
            pwms = <&sw_pwm 2 PWM_MSEC(100) PWM_POLARITY_INVERTED>;
            label = "RED PWM LED";
        };

        green_pwm_led: green_pwm_led {
            pwms = <&sw_pwm 0 PWM_MSEC(100) PWM_POLARITY_INVERTED>;
            label = "GREEN PWM LED";
        };

        blue_pwm_led: blue_pwm_led {
            pwms = <&sw_pwm 1 PWM_MSEC(100) PWM_POLARITY_INVERTED>;
            label = "BLUE PWM LED";
        };
    };

    aliases {
        red-pwm-led = &red_pwm_led;
        green-pwm-led = &green_pwm_led;
        blue-pwm-led = &blue_pwm_led;
        swpwm = &sw_pwm;
        };

};




Code Snippet:
#include "nrfx_pwm.h"

#define GREEN_LED_PIN  (NRF_GPIO_PIN_MAP(1, 13))
#define BLUE_LED_PIN   (NRF_GPIO_PIN_MAP(1, 14))
#define RED_LED_PIN    (NRF_GPIO_PIN_MAP(1, 15))

static nrfx_pwm_t m_pwm0 = NRFX_PWM_INSTANCE(0);

// Sequence: LED ON for 25 ms, OFF for 9975 ms (assuming 10s period, 1 Hz)
static nrf_pwm_values_common_t seq_values[] = {
    25, // ON (duty cycle value for 25 ms)
    0     // OFF (duty cycle value for 9975 ms)
};

static nrf_pwm_sequence_t const seq =
{
    .values.p_common = seq_values,
    .length          = NRF_PWM_VALUES_LENGTH(seq_values),
    .repeats         = 0,
    .end_delay       = 0
};

void pwm_init(void)
{
    nrfx_pwm_config_t const config0 =
    {
        .output_pins  = { GREEN_LED_PIN, BLUE_LED_PIN, RED_LED_PIN },
        .irq_priority = NRFX_PWM_DEFAULT_CONFIG_IRQ_PRIORITY,
        .base_clock   = NRF_PWM_CLK_1MHz,
        .count_mode   = NRF_PWM_MODE_UP,
        .top_value    = 10000, // 10,000 ticks at 1 MHz = 10 ms per period
        .load_mode    = NRF_PWM_LOAD_COMMON,
        .step_mode    = NRF_PWM_STEP_AUTO
    };
    nrfx_pwm_init(&m_pwm0, &config0, NULL, NULL);
}

void start_pwm(void)
{
    nrfx_pwm_simple_playback(&m_pwm0, &seq, 1, NRFX_PWM_FLAG_LOOP);
}
Parents
  • in your device tree you are using software pwm (sw_pwm) but in your C code you are trying to access actual pwm using the NRFX PWM lib. Either use software pwm also in the C code or use actual hardware pwm in your overlay to match your C code.

    Hmm, after closer look, it looks like you are using nrfx pwm lib in a hardcoded way that you are not relying on the overlay/dts information for your PWM. So the fact that you are using sw_pwm should not matter.

    Maybe the ticks you have selected in your pwm_init is too small.It probably isn't visible on the LEDs with such a small values as the ticks are shown in nano seconds. Chosing a 10ms is too small to be visible on LED, try a bigger value.

    in pwm_init, choose config0.top_value = 1000000 for a 1S instead of 10ms

    also make the ON time a little bigger

    static nrf_pwm_values_common_t seq_values[] = {
        500000,  // ON for 500 000 ticks = 0.5 s
        0        // OFF for the rest
    };

  • I see. I will read up on the nrfx pwm library in further detail, understand it and make the changes. 

    Thanks. 

Reply Children
No Data
Related