Skip to content

Commit c4c6256

Browse files
committed
Calculate size of FRAC field using its own MSB and LSB, rather than hoping that INT_LSB is in the right place
1 parent 3a693bc commit c4c6256

File tree

3 files changed

+3
-3
lines changed
  • src/rp2_common
    • hardware_adc/include/hardware
    • hardware_pio/include/hardware
    • hardware_pwm/include/hardware

3 files changed

+3
-3
lines changed

src/rp2_common/hardware_adc/include/hardware/adc.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ static inline void adc_run(bool run) {
204204
static inline void adc_set_clkdiv(float clkdiv) {
205205
invalid_params_if(HARDWARE_ADC, clkdiv >= 1 << (ADC_DIV_INT_MSB - ADC_DIV_INT_LSB + 1));
206206
#if PICO_ADC_CLKDIV_ROUND_NEAREST
207-
clkdiv += 0.5f / (1 << ADC_DIV_INT_LSB); // round to the nearest fraction
207+
clkdiv += 0.5f / (1 << (ADC_DIV_FRAC_MSB + 1 - ADC_DIV_FRAC_LSB)); // round to the nearest fraction
208208
#endif
209209
adc_hw->div = (uint32_t)(clkdiv * (float) (1 << ADC_DIV_INT_LSB));
210210
}

src/rp2_common/hardware_pio/include/hardware/pio.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -495,7 +495,7 @@ static inline void sm_config_set_clkdiv_int_frac(pio_sm_config *c, uint16_t div_
495495
static inline void pio_calculate_clkdiv8_from_float(float div, uint32_t *div_int, uint8_t *div_frac8) {
496496
valid_params_if(HARDWARE_PIO, div >= 1 && div <= 65536);
497497
#if PICO_PIO_CLKDIV_ROUND_NEAREST
498-
div += 0.5f / 256; // round to the nearest 1/256
498+
div += 0.5f / (1 << (PIO_SM0_CLKDIV_FRAC_MSB + 1 - PIO_SM0_CLKDIV_FRAC_LSB)); // round to the nearest 1/256
499499
#endif
500500
*div_int = (uint16_t)div;
501501
// not a strictly necessary check, but if this changes, then this method should

src/rp2_common/hardware_pwm/include/hardware/pwm.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ static inline void pwm_config_set_phase_correct(pwm_config *c, bool phase_correc
161161
static inline void pwm_config_set_clkdiv(pwm_config *c, float div) {
162162
valid_params_if(HARDWARE_PWM, div >= 1.f && div < 256.f);
163163
#if PICO_PWM_CLKDIV_ROUND_NEAREST
164-
div += 0.5f / (1 << PWM_CH0_DIV_INT_LSB); // round to the nearest fraction
164+
div += 0.5f / (1 << (PWM_CH0_DIV_FRAC_MSB + 1 - PWM_CH0_DIV_FRAC_LSB)); // round to the nearest fraction
165165
#endif
166166
c->div = (uint32_t)(div * (float)(1u << PWM_CH0_DIV_INT_LSB));
167167
}

0 commit comments

Comments
 (0)