ledc: left shift operation on duty in ledc_set_duty( )

user4_esp32
Posts: 21
Joined: Sun Nov 26, 2017 4:48 pm

ledc: left shift operation on duty in ledc_set_duty( )

Postby user4_esp32 » Sun Jun 23, 2019 12:06 am

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;
}

WiFive
Posts: 3529
Joined: Tue Dec 01, 2015 7:35 am

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

Postby WiFive » Sun Jun 23, 2019 4:20 am

By shifting left, you set the fractional bits to zero. You drop the upper 4 bits but they are not used.

user4_esp32
Posts: 21
Joined: Sun Nov 26, 2017 4:48 pm

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

Postby user4_esp32 » Sun Jun 23, 2019 1:30 pm

Thanks, WiFive. Since duty is a uint32_t, why are there fractional bits?

WiFive
Posts: 3529
Joined: Tue Dec 01, 2015 7:35 am

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

Postby WiFive » Sun Jun 23, 2019 4:37 pm

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.

user4_esp32
Posts: 21
Joined: Sun Nov 26, 2017 4:48 pm

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

Postby user4_esp32 » Sun Jun 23, 2019 11:40 pm

Thanks very much, WiFive.

Who is online

Users browsing this forum: AppdigEng and 80 guests