This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

controlling leds with pwm with zephyr gives errors and not functioning well

hello Nordic

i work with nrf52840, with zephyr (2.6.9)

i have 4 leds on my board that i want to control seperatly with pwm

i put each one of them on a different pwm channel .. following my .dts:

/{...    
    leds {
		compatible = "gpio-leds";
		led0_red: led_0 {
			gpios = <&gpio1 8 GPIO_ACTIVE_HIGH>;
			label = "RED_LED_0";
		};
		led1_orange: led_1 {
			gpios = <&gpio0 8 GPIO_ACTIVE_HIGH>;
			label = "ORANGE_LED_1";
		};
		led2_green: led_2 {
			gpios = <&gpio0 6 GPIO_ACTIVE_HIGH>;
			label = "GREEN_LED_2";
		};		
		led3_green2: led_3 {
			gpios = <&gpio0 30 GPIO_ACTIVE_HIGH>;
			label = "GREEN_LED_3";
		};					
	};
	
	pwmleds {
		compatible = "pwm-leds";
		pwm_led0_red: pwm_led_0 {
			pwms = <&pwm0 40>;
			label = "RED_PWM_LED_0";
		};
		pwm_led1_orange: pwm_led_1 {
			pwms = <&pwm0 8>;
			label = "ORANGE_PWM_LED_1";
		};
		pwm_led2_green1: pwm_led_2 {
			pwms = <&pwm0 6>;
			label = "GREEN_1_PWM_LED_2";
		};
		pwm_led3_green2: pwm_led_3 {
			pwms = <&pwm0 30>;
			label = "GREEN_2_PWM_LED_3";
		};
	};
	aliases {
		led0-red = &led0_red;
		led1-orange = &led1_orange;
		led2-green = &led2_green;
		led3-green2 = &led3_green2;
		pwm-led0-red = &pwm_led0_red;
		pwm-led1-orange = &pwm_led1_orange;
		pwm-led2-green2 = &pwm_led2_green1;
		pwm-led3-green1 = &pwm_led3_green2;
		sw0 = &button0;
	};
	
};

    &pwm0 {
		status = "okay";
		ch0-pin = <40>;
		ch1-pin = <8>;
		ch2-pin = <6>;
		ch3-pin = <30>;
	};

added the following to my prj.conf  

# PWM Related configs
CONFIG_PWM=y
CONFIG_LED_PWM=y
CONFIG_LED=y

i am following the led_pwm example in the ncs but in the RTT i get the following error :

[00000009] <err> pwm_nrfx: Prescaler for period_cycles 32000000 not found.
[00000009] <err> LEDs: err=-22

or this error when i try to blink with 1000 off and 50 on or vice versa

[00000362] <err> pwm_nrfx: Prescaler for period_cycles 16800000 not found.
[00000362] <err> LEDs: err=-22

and i see some comment regarding the issue (if i got it right) in pwm_nrfx.c line 125

static int pwm_nrfx_pin_set(const struct device *dev, uint32_t pwm,
			    uint32_t period_cycles, uint32_t pulse_cycles,
			    pwm_flags_t flags)
{
	/* We assume here that period_cycles will always be 16MHz
	 * peripheral clock. Since pwm_nrfx_get_cycles_per_sec() function might
	 * be removed, see ISSUE #6958.
	 * TODO: Remove this comment when issue has been resolved.
	 */...

so what should i predefine or do to avoid that error ?

hope to read from you soon

best regards

Ziv

Parents
  • Hi 

    It seems you are trying to use the PWM module with very large values?

    The PWM module in the nRF52 devices can not have a period longer than about 250ms, when using the maximum prescaler value. 

    If you keep the period below 250000us it should work fine. 

    Best regards
    Torbjørn 

Reply
  • Hi 

    It seems you are trying to use the PWM module with very large values?

    The PWM module in the nRF52 devices can not have a period longer than about 250ms, when using the maximum prescaler value. 

    If you keep the period below 250000us it should work fine. 

    Best regards
    Torbjørn 

Children
  • hi ovrebekk

    thanks for relpaly though i don't understand this limitation and i just opened a thread trying to question that 

    https://devzone.nordicsemi.com/f/nordic-q-a/85623/pwm_nrfx-in-ncs-issue

    if you will plane to answer the new thread then i will close this one with verify answer, if you prefer to answer here its fine as well

    best regards

    Ziv

  • Hi Ziv

    The PWM module is designed to handle quick pulses (for fading LED's, controlling motors, playing sound etc), where the PWM period is normally some tens of milliseconds or less.  

    You don't really need the PWM module to turn on and off a pin every second, since in microcontroller terms this is very slow. 

    The limitation is set by the COUNTERTOP register which goes to 32767 only, and the PRESCALER register which only goes to 7 (125kHz frequency). 

    This gives you a maximum PWM period of 32767 / 125000 = 262ms

    I assigned the other case to me as well, and I will provide a response to that later today. 

    Best regards
    Torbjørn

Related