Skip to content

Commit 5969a10

Browse files
d3zd3zdkalowsk
authored andcommitted
drivers: pwm: rpi_pico: Convert clk calculations to fixed-point
Remove the use of floating point to calculate in `pwm_rpi_get_cycles_per_sec` to fixed point calculations that result in the same value. There is still a division here, which is somewhat slow still, but the end result about twice fast for the whole path through the led pwm code. Even better would be to cache these values, since there is no interface in this driver to change the configuration past init time. Signed-off-by: David Brown <[email protected]>
1 parent c1670f9 commit 5969a10

File tree

1 file changed

+5
-12
lines changed

1 file changed

+5
-12
lines changed

drivers/pwm/pwm_rpi_pico.c

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -38,15 +38,6 @@ struct pwm_rpi_config {
3838
const clock_control_subsys_t clk_id;
3939
};
4040

41-
static float pwm_rpi_get_clkdiv(const struct device *dev, int slice)
42-
{
43-
const struct pwm_rpi_config *cfg = dev->config;
44-
45-
/* the divider is a fixed point 8.4 convert to float for use in pico-sdk */
46-
return (float)cfg->slice_configs[slice].integral +
47-
(float)cfg->slice_configs[slice].frac / 16.0f;
48-
}
49-
5041
static inline uint32_t pwm_rpi_channel_to_slice(uint32_t channel)
5142
{
5243
return channel / 2;
@@ -68,20 +59,22 @@ static int pwm_rpi_get_cycles_per_sec(const struct device *dev, uint32_t ch, uin
6859
return -EINVAL;
6960
}
7061

62+
const struct pwm_rpi_slice_config *slice_config = &cfg->slice_configs[slice];
63+
7164
ret = clock_control_get_rate(cfg->clk_dev, cfg->clk_id, &pclk);
7265
if (ret < 0 || pclk == 0) {
7366
return -EINVAL;
7467
}
7568

76-
if (cfg->slice_configs[slice].integral == 0) {
69+
if (slice_config->integral == 0) {
7770
*cycles = pclk;
7871
} else {
7972
/* No need to check for divide by 0 since the minimum value of
8073
* pwm_rpi_get_clkdiv is 1
8174
*/
82-
*cycles = (uint64_t)((float)pclk / pwm_rpi_get_clkdiv(dev, slice));
75+
*cycles = (uint64_t)pclk * 16 /
76+
((uint64_t)slice_config->integral * 16 + slice_config->frac);
8377
}
84-
8578
return 0;
8679
}
8780

0 commit comments

Comments
 (0)