How to change pwm period on runtime in ncs?

Hi, 

I'm trying to play some sounds on a piezo with the help of the pwm. I tried to set the period with pwm_set_dt but only the first call is successful and every subsequent call raised an error. 

Here is my test code:

    for(uint16_t i = 400; i < 800; i++)
    {
        int ret = pwm_set_dt(&piezo_pwm_pos, PWM_HZ(i),
						   PWM_HZ(i) / 2);
        LOG_DBG("pwm_set_dt = %d", ret);

        ret = pwm_set_dt(&piezo_pwm_neg, PWM_HZ(i),
						   PWM_HZ(i) / 2);
        LOG_DBG("pwm_set_dt = %d", ret);
        k_msleep(10);
    }

The results looked like this:

00> D: pwm_set_dt = 0
00> D: pwm_set_dt = 0
00> E: Incompatible period.
00> D: pwm_set_dt = -22
00> E: Incompatible period.
00> D: pwm_set_dt = -22
00> E: Incompatible period.
00> D: pwm_set_dt = -22
00> E: Incompatible period.
00> D: pwm_set_dt = -22
00> E: Incompatible period.
00> D: pwm_set_dt = -22

I didn't find anything in the documentation of pwm_set for the error 22.

How can I change period and pulse while the pwm is running?

Best regards,

Christian

Parents Reply
  • It's defined like this:

    pwmpiezo0 {
    		compatible = "pwm-leds";
    		status = "okay";
    	
    		piezo_pwm_pos: piezo_pwm_pos {
    				status = "okay";
    				pwms = <&pwm1 0 PWM_HZ(7500) PWM_POLARITY_NORMAL>;
    				label = "Piezo positive";
    		};
    	
    		piezo_pwm_neg: piezo_pwm_neg {
    				status = "okay";
    				pwms = <&pwm1 1 PWM_HZ(7500) PWM_POLARITY_INVERTED>;
    				label = "Piezo negative";
    		};
    	};

    One of the pins is already inverted. I then call 

    ret = pwm_set_dt(&piezo_pwm_pos, PWM_HZ(m_piezo_params.frequency),
    					PWM_HZ(m_piezo_params.frequency) / 2);
    
    ret = pwm_set_dt(&piezo_pwm_neg, PWM_HZ(m_piezo_params.frequency),
    				   PWM_HZ(m_piezo_params.frequency) / 2);

    within a timeout handler every x ms. If m_piezo_params.frequency stays the same, the function calls return 0. But if I change the frequency it breaks after the first timeout. 

Children
No Data
Related