-
Notifications
You must be signed in to change notification settings - Fork 972
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make all float->int/frac conversions round to nearest by default (#2065)
* making pio_calculate_clkdiv8_from_float round to the neareset 1/256 (not lower 1/256) * chage rounding of all float clkdivs to round to neareset; default to nearest (which is a backwards incompatible change, but I think OK), and add ability to turn it off * fix copy/paste errors in PICO_CONFIG * Calculate size of FRAC field using its own MSB and LSB, rather than hoping that INT_LSB is in the right place * Add a new REG_FIELD_WIDTH macro, and make the calculation of the clock-dividers more consistent --------- Co-authored-by: Andrew Scheller <[email protected]>
- Loading branch information
1 parent
5a98889
commit 6bb44dd
Showing
9 changed files
with
71 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -253,6 +253,11 @@ extern "C" { | |
#else | ||
#define PARAM_ASSERTIONS_ENABLED_HARDWARE_CLOCKS 0 | ||
#endif | ||
#endif | ||
|
||
// PICO_CONFIG: PICO_CLOCK_GPIO_CLKDIV_ROUND_NEAREST, True if floating point GPIO clock divisors should be rounded to the nearest possible clock divisor rather than rounding down, type=bool, default=PICO_CLKDIV_ROUND_NEAREST, group=hardware_clocks | ||
#ifndef PICO_CLOCK_GPIO_CLKDIV_ROUND_NEAREST | ||
#define PICO_CLOCK_GPIO_CLKDIV_ROUND_NEAREST PICO_CLKDIV_ROUND_NEAREST | ||
#endif | ||
|
||
typedef clock_num_t clock_handle_t; | ||
|
@@ -387,11 +392,15 @@ static inline void clock_gpio_init_int_frac(uint gpio, uint src, uint32_t div_in | |
static inline void clock_gpio_init(uint gpio, uint src, float div) | ||
{ | ||
uint div_int = (uint)div; | ||
#if CLOCKS_CLK_GPOUT0_DIV_FRAC_MSB - CLOCKS_CLK_GPOUT0_DIV_FRAC_LSB == 15 | ||
uint16_t frac = (uint16_t)((div - (float)div_int) * (1u << 16)); | ||
const int frac_bit_count = REG_FIELD_WIDTH(CLOCKS_CLK_GPOUT0_DIV_FRAC); | ||
#if PICO_CLOCK_GPIO_CLKDIV_ROUND_NEAREST | ||
div += 0.5f / (1 << frac_bit_count); // round to the nearest fraction | ||
#endif | ||
#if REG_FIELD_WIDTH(CLOCKS_CLK_GPOUT0_DIV_FRAC) == 16 | ||
uint16_t frac = (uint16_t)((div - (float)div_int) * (1u << frac_bit_count)); | ||
clock_gpio_init_int_frac16(gpio, src, div_int, frac); | ||
#elif CLOCKS_CLK_GPOUT0_DIV_FRAC_MSB - CLOCKS_CLK_GPOUT0_DIV_FRAC_LSB == 7 | ||
uint8_t frac = (uint8_t)((div - (float)div_int) * (1u << 8)); | ||
#elif REG_FIELD_WIDTH(CLOCKS_CLK_GPOUT0_DIV_FRAC) == 8 | ||
uint8_t frac = (uint8_t)((div - (float)div_int) * (1u << frac_bit_count)); | ||
clock_gpio_init_int_frac8(gpio, src, div_int, frac); | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
kilograham
Author
Contributor
|
||
#else | ||
#error unsupported number of fractional bits | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Maybe missing something here but had a compile error here since CLOCKS_CLK_GPOUT0_DIV_FRAC is undefined. Im assuming that was just a tab completion issue with the omission of _MSB or _LSB?