Issue about PWM of NCS

Dear Nordic Engineers

I developed a PWM function to drive the RGB LED. Then I found a problem, which is when I used a PWM signal of one channel to control the RED light of the RGB lamp, causing it to go from bright to off and then back to bright, the other two channels would have a level reversal when the lamp was turned off. This is the data I captured using a logic analyzer.

But in fact, my program doesn't control these two channels. Here is my program and the overlay. This issue causes one of the RGB lights to go out, and the other two channels will flicker for a moment when that happens. Could you please tell me how to turn off the level inversion of the other two channels?

/*
 * Copyright (c) 2016 Intel Corporation
 *
 * SPDX-License-Identifier: Apache-2.0
 */

/**
 * @file Sample app to demonstrate PWM-based RGB LED control
 */

#include <zephyr/device.h>
#include <zephyr/devicetree.h>
#include <errno.h>
#include <zephyr/drivers/led.h>
#include <zephyr/sys/util.h>
#include <zephyr/kernel.h>

#include <zephyr/logging/log.h>
LOG_MODULE_REGISTER(main, CONFIG_LOG_DEFAULT_LEVEL);

#define LED_PWM_NODE_ID	 DT_COMPAT_GET_ANY_STATUS_OKAY(pwm_leds)

const int num_leds = 3;

#define MAX_BRIGHTNESS	100

#define FADE_DELAY_MS	10
#define FADE_DELAY	K_MSEC(FADE_DELAY_MS)

/**
 * @brief Run tests on a single LED using the LED API syscalls.
 *
 * @param led_pwm LED PWM device.
 * @param led Number of the LED to test.
 */
static void run_led_test(const struct device *led_pwm, uint8_t led)
{
	int err;
	uint16_t level;

	/* Increase LED brightness gradually up to the maximum level. */
	LOG_INF("  Increasing brightness gradually");
	for (level = 0; level <= MAX_BRIGHTNESS; level++) {
		err = led_set_brightness(led_pwm, led, level);
		if (err < 0) {
			LOG_ERR("err=%d brightness=%d\n", err, level);
			return;
		}
		k_sleep(FADE_DELAY);
	}
	k_sleep(K_MSEC(1000));

	err = led_set_brightness(led_pwm, led, 100);
		if (err < 0) {
			LOG_ERR("err=%d brightness=%d\n", err, level);
			return;
		}
	k_sleep(K_MSEC(1000));

	for (level = MAX_BRIGHTNESS; level > 0; level--) {
		err = led_set_brightness(led_pwm, led, level);
		if (err < 0) {
			LOG_ERR("err=%d brightness=%d\n", err, level);
			return;
		}
		k_sleep(FADE_DELAY);
	}
	k_sleep(K_MSEC(1000));

	err = led_set_brightness(led_pwm, led, 0);
	if (err < 0) {
		LOG_ERR("err=%d brightness=%d\n", err, level);
		return;
	}
	k_sleep(K_MSEC(1000));

}

static void run_led_test_red_and_green(const struct device *led_pwm, uint8_t led)
{
	int err;
	uint16_t level;

	err = led_set_brightness(led_pwm, led, 100);
		if (err < 0) {
			LOG_ERR("err=%d brightness=%d\n", err, level);
			return;
		}
		k_sleep(FADE_DELAY);

	for (level = MAX_BRIGHTNESS; level > led; level--) {
		err = led_set_brightness(led_pwm, led, level);
		if (err < 0) {
			LOG_ERR("err=%d brightness=%d\n", err, level);
			return;
		}
		k_sleep(FADE_DELAY);
	}
	k_sleep(K_MSEC(1000));

	err = led_set_brightness(led_pwm, led, 0);
	if (err < 0) {
		LOG_ERR("err=%d brightness=%d\n", err, level);
		return;
	}
	k_sleep(FADE_DELAY);
}

int main(void)
{
	const struct device *led_pwm;
	uint8_t led;

	led_pwm = DEVICE_DT_GET(LED_PWM_NODE_ID);
	if (!device_is_ready(led_pwm)) {
		LOG_ERR("Device %s is not ready", led_pwm->name);
		return 0;
	}

	if (!num_leds) {
		LOG_ERR("No LEDs found for %s", led_pwm->name);
		return 0;
	}

	// for (led = 1; led <= 2; led++) {
	// 		run_led_test_red_and_green(led_pwm,led);
	// 	}

	while (1)
	{
		// for (led = 1; led <= num_leds; led++) {
			run_led_test(led_pwm, 3);
		// }
	}
}

6087.nrf54l15dk_nrf54l15_cpuapp.overlay

Best regards,

Hannibal

Parents
  • Hi Wang, 

    I think the whole issue here is that you are executing

    	while (1)
    	{
    		// for (led = 1; led <= num_leds; led++) {
    			run_led_test(led_pwm, 3);
    		// }
    	}

    LED here is not just a number but it is the PWM channel index as specified in your overlay file like below

        pwms = <&pwm20 0 …>,   /* led index 0: RED  */
               <&pwm20 1 …>,   /* led index 1: GREEN*/
               <&pwm20 2 …>;   /* led index 2: BLUE */
    

    So in the configuration config->num_leds == 3 which you have used right but the indices are 

    led_set_brightness(led_pwm, 0, brightness);  // RED
    led_set_brightness(led_pwm, 1, brightness);  // GREEN
    led_set_brightness(led_pwm, 2, brightness);  // BLUE
    

    So your for loop should not start at led = 1 but should start at led =0

    for (uint8_t led = 0; led < num_leds; led++) {
        run_led_test(led_pwm, led);
    }
    

Reply
  • Hi Wang, 

    I think the whole issue here is that you are executing

    	while (1)
    	{
    		// for (led = 1; led <= num_leds; led++) {
    			run_led_test(led_pwm, 3);
    		// }
    	}

    LED here is not just a number but it is the PWM channel index as specified in your overlay file like below

        pwms = <&pwm20 0 …>,   /* led index 0: RED  */
               <&pwm20 1 …>,   /* led index 1: GREEN*/
               <&pwm20 2 …>;   /* led index 2: BLUE */
    

    So in the configuration config->num_leds == 3 which you have used right but the indices are 

    led_set_brightness(led_pwm, 0, brightness);  // RED
    led_set_brightness(led_pwm, 1, brightness);  // GREEN
    led_set_brightness(led_pwm, 2, brightness);  // BLUE
    

    So your for loop should not start at led = 1 but should start at led =0

    for (uint8_t led = 0; led < num_leds; led++) {
        run_led_test(led_pwm, led);
    }
    

Children
No Data
Related