Page 1 of 1

ledc: left shift operation on duty in ledc_set_duty( )

Posted: Sun Jun 23, 2019 12:06 am
by user4_esp32
Hello,

Why is the left shift by 4 of duty necessary in the ledc_set_duty( ) function in esp-idf\components\driver\ledc.c? Doesn't the left shift remove the most significant 4 bits, not the least significant four bits?

Code: Select all

esp_err_t ledc_set_duty(ledc_mode_t speed_mode, ledc_channel_t channel, uint32_t duty)
{
    LEDC_ARG_CHECK(speed_mode < LEDC_SPEED_MODE_MAX, "speed_mode");
    LEDC_ARG_CHECK(channel < LEDC_CHANNEL_MAX, "channel");
    /* The channel configuration should not be changed before the fade operation is done. */
    _ledc_fade_hw_acquire(speed_mode, channel);
    ledc_duty_config(speed_mode,
                     channel,         //uint32_t chan_num,
                     LEDC_VAL_NO_CHANGE,
                     duty << 4,       //uint32_t duty_val,the least 4 bits are decimal part
                     1,               //uint32_t increase,
                     1,               //uint32_t duty_num,
                     1,               //uint32_t duty_cycle,
                     0                //uint32_t duty_scale
                     );
    _ledc_fade_hw_release(speed_mode, channel);
    return ESP_OK;
}

Re: ledc: left shift operation on duty in ledc_set_duty( )

Posted: Sun Jun 23, 2019 4:20 am
by WiFive
By shifting left, you set the fractional bits to zero. You drop the upper 4 bits but they are not used.

Re: ledc: left shift operation on duty in ledc_set_duty( )

Posted: Sun Jun 23, 2019 1:30 pm
by user4_esp32
Thanks, WiFive. Since duty is a uint32_t, why are there fractional bits?

Re: ledc: left shift operation on duty in ledc_set_duty( )

Posted: Sun Jun 23, 2019 4:37 pm
by WiFive
Duty is a uint but the hardware register has fractional bits so the driver does the conversion. The driver doesn't support the fractional duty.

Re: ledc: left shift operation on duty in ledc_set_duty( )

Posted: Sun Jun 23, 2019 11:40 pm
by user4_esp32
Thanks very much, WiFive.