Getting prescalar issue when I try to run PWM example with a period of 9975ms and pulse of 25ms

Hello, 

I tried the blinky_pwm example on the nrf5340 board and it has worked successfully. 

I am getting the error when I try to run the pwm code with some modifications

Objective: To run the PWM to control the LED where the period is 9975ms and pulse is 25ms. 
This means that for nearly 9seconds the LED should be off and the remaining time the led should glow. 

Error Snippet:

00> *** Booting nRF Connect SDK v2.7.0-5cb85570ca43 ***
00> *** Using Zephyr OS v3.6.99-100befc70c74 ***
00> Prescaler for period_cycles 22561046 not found


main.c

#include <zephyr/kernel.h>
#include <zephyr/sys/printk.h>
#include <zephyr/device.h>
#include <zephyr/drivers/pwm.h>
#include <zephyr/drivers/led.h>

#define TOTAL_PERIOD PWM_MSEC(10000)
#define MAX_PULSE PWM_MSEC(25)
#define MIN_PULSE 0

static const struct pwm_dt_spec red_pwm_led = PWM_DT_SPEC_GET(DT_ALIAS(red_pwm_led));
static const struct pwm_dt_spec green_pwm_led = PWM_DT_SPEC_GET(DT_ALIAS(green_pwm_led));
static const struct pwm_dt_spec blue_pwm_led = PWM_DT_SPEC_GET(DT_ALIAS(blue_pwm_led));


static int turn_off_led(const struct pwm_dt_spec *led)
{
    return pwm_set_dt(led, PWM_SEC(10), 0);
}


int32_t ConfigureLed(LedState_t, uint16_t delay_t)
{
    int ret;
    if (!pwm_is_ready_dt(&red_pwm_led) || !pwm_is_ready_dt(&green_pwm_led) || !pwm_is_ready_dt(&blue_pwm_led))
    {
        printk("Error: PWM Device %s or %s or %s is not ready \n", red_pwm_led.dev->name, green_pwm_led.dev->name, blue_pwm_led.dev->name);
        return 0;

    }

    turn_off_led(&red_pwm_led);
    turn_off_led(&green_pwm_led);
    turn_off_led(&blue_pwm_led);

    k_sleep(K_SECONDS(10));
    
    while(delay_t)
    {
        ret = pwm_set_dt(&green_pwm_led, TOTAL_PERIOD, MAX_PULSE);
        if ( ret != 0 )
        {
            printk("Failed to set pulse width 1\n");
            return 0;
        }
        k_sleep(K_SECONDS(10));

        ret = pwm_set_dt(&green_pwm_led, TOTAL_PERIOD, MIN_PULSE);
        if ( ret != 0 )
        {
            printk("Failed to set pulse width 2\n");
            return 0;
        }
        k_sleep(K_MSEC(9975));
        delay_t--;
    }
}


prj.conf

CONFIG_PWM=y
CONFIG_LED=y
CONFIG_LED_PWM=y


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

		red_pwm_led: red_pwm_led {
			pwms = <&pwm0 0 PWM_MSEC(100) PWM_POLARITY_INVERTED>; // This is P0.13
			label = "RED PWM LED";
		};

		green_pwm_led: green_pwm_led {
			pwms = <&pwm0 1 PWM_MSEC(100) PWM_POLARITY_INVERTED>; // This is P0.14
			label = "GREEN PWM LED";
		};

		blue_pwm_led: blue_pwm_led {
			pwms = <&pwm0 2 PWM_MSEC(100) PWM_POLARITY_INVERTED>; //This is P0.15
			label = "BLUE PWM LED";
		};
	};

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



Can anyone suggest/help as to where I am going wrong? I assumed the 32khz crystal is being used. 

Parents
  • I have solved this. 

    This Discussion : PWM in nrf5340 - blinky example fails to compile with SDK 2.7.0

    Mentions that the default pwm node is software emulated and has a limitation of 262ms [PERIOD]. Thus we need to use the SW_PWM node. 

    The only changes required are in the overlay file

    / {
    	pwmleds {
    		compatible = "pwm-leds";
    		status = "okay";
    
    		red_pwm_led: red_pwm_led {
    			pwms = <&sw_pwm 0 PWM_MSEC(100) PWM_POLARITY_INVERTED>; // This is P0.13
    			label = "RED PWM LED";
    		};
    
    		green_pwm_led: green_pwm_led {
    			pwms = <&sw_pwm 1 PWM_MSEC(100) PWM_POLARITY_INVERTED>; // This is P0.14
    			label = "GREEN PWM LED";
    		};
    
    		blue_pwm_led: blue_pwm_led {
    			pwms = <&sw_pwm 2 PWM_MSEC(100) PWM_POLARITY_INVERTED>; //This is P0.15
    			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;
    	};
    };
    
    &pwm0 {
    	status = "okay";
    };
    
    &sw_pwm {
    	status = "okay";
    	channel-gpios = <&gpio1 15 PWM_POLARITY_INVERTED>,
    					<&gpio1 14 PWM_POLARITY_INVERTED>,
    					<&gpio1 13 PWM_POLARITY_INVERTED>;
    };


    It works as expected and the prescalar issue is no longer observed. 

Reply
  • I have solved this. 

    This Discussion : PWM in nrf5340 - blinky example fails to compile with SDK 2.7.0

    Mentions that the default pwm node is software emulated and has a limitation of 262ms [PERIOD]. Thus we need to use the SW_PWM node. 

    The only changes required are in the overlay file

    / {
    	pwmleds {
    		compatible = "pwm-leds";
    		status = "okay";
    
    		red_pwm_led: red_pwm_led {
    			pwms = <&sw_pwm 0 PWM_MSEC(100) PWM_POLARITY_INVERTED>; // This is P0.13
    			label = "RED PWM LED";
    		};
    
    		green_pwm_led: green_pwm_led {
    			pwms = <&sw_pwm 1 PWM_MSEC(100) PWM_POLARITY_INVERTED>; // This is P0.14
    			label = "GREEN PWM LED";
    		};
    
    		blue_pwm_led: blue_pwm_led {
    			pwms = <&sw_pwm 2 PWM_MSEC(100) PWM_POLARITY_INVERTED>; //This is P0.15
    			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;
    	};
    };
    
    &pwm0 {
    	status = "okay";
    };
    
    &sw_pwm {
    	status = "okay";
    	channel-gpios = <&gpio1 15 PWM_POLARITY_INVERTED>,
    					<&gpio1 14 PWM_POLARITY_INVERTED>,
    					<&gpio1 13 PWM_POLARITY_INVERTED>;
    };


    It works as expected and the prescalar issue is no longer observed. 

Children
No Data
Related