Skip to content

Commit c691da8

Browse files
authored
update previous fix as 1.0 < clkdiv < 2.0 IS supported on RP2350 (#2255)
* update previous fix as 1.0 < clkdiv < 2.0 IS supported on RP2350
1 parent 04525b8 commit c691da8

File tree

2 files changed

+23
-7
lines changed

2 files changed

+23
-7
lines changed

src/rp2_common/hardware_clocks/clocks.c

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -101,12 +101,22 @@ bool clock_configure(clock_handle_t clock, uint32_t src, uint32_t auxsrc, uint32
101101
if (freq > src_freq)
102102
return false;
103103

104-
uint32_t div = (uint32_t)((((uint64_t) src_freq) << CLOCKS_CLK_GPOUT0_DIV_INT_LSB) / freq);
105-
// only clock divider of 1, or >= 2 are supported
106-
if (div < (2u << CLOCKS_CLK_GPOUT0_DIV_INT_LSB)) {
107-
div = (1u << CLOCKS_CLK_GPOUT0_DIV_INT_LSB);
104+
uint64_t div64 =((((uint64_t) src_freq) << CLOCKS_CLK_GPOUT0_DIV_INT_LSB) / freq);
105+
uint32_t div, actual_freq;
106+
if (div64 >> 32) {
107+
// set div to 0 for maximum clock divider
108+
div = 0;
109+
actual_freq = src_freq >> (32 - CLOCKS_CLK_GPOUT0_DIV_INT_LSB);
110+
} else {
111+
div = div64;
112+
#if PICO_RP2040
113+
// on RP2040 only clock divider of 1, or >= 2 are supported
114+
if (div < (2u << CLOCKS_CLK_GPOUT0_DIV_INT_LSB)) {
115+
div = (1u << CLOCKS_CLK_GPOUT0_DIV_INT_LSB);
116+
}
117+
#endif
118+
actual_freq = (uint32_t) ((((uint64_t) src_freq) << CLOCKS_CLK_GPOUT0_DIV_INT_LSB) / div);
108119
}
109-
uint32_t actual_freq = (uint32_t) ((((uint64_t) src_freq) << CLOCKS_CLK_GPOUT0_DIV_INT_LSB) / div);
110120

111121
clock_configure_internal(clock, src, auxsrc, actual_freq, div);
112122
// Store the configured frequency

src/rp2_common/hardware_clocks/include/hardware/clocks.h

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -269,16 +269,22 @@ typedef clock_num_t clock_handle_t;
269269
* frequency to be specified, and will set the clock divider to achieve the exact or higher frequency
270270
* achievable, with the maximum being the src_freq.
271271
*
272-
* Note: That the clock hardware only support divisors of exactly 1 or 2.0->65535.0
272+
* \if rp2350_specific
273+
* Note: The RP2350 clock hardware supports divisors from 1.0->65536.0 in steps of 1/65536
273274
*
274-
* See the tables in the description for details on the possible values for clock sources.
275+
* \endif
276+
* \if rp2040_specific
277+
* Note: The RP2040 clock hardware only supports divisors of exactly 1.0 or 2.0->16777216.0 in steps of 1/256
278+
* \endif
275279
*
280+
* See the tables in the description for details on the possible values for clock sources.
276281
*
277282
* \param clock The clock to configure
278283
* \param src The main clock source, can be 0.
279284
* \param auxsrc The auxiliary clock source, which depends on which clock is being set. Can be 0
280285
* \param src_freq Frequency of the input clock source
281286
* \param freq Requested frequency
287+
* \return true if the clock is updated, false if freq > src_freq
282288
*/
283289
bool clock_configure(clock_handle_t clock, uint32_t src, uint32_t auxsrc, uint32_t src_freq, uint32_t freq);
284290

0 commit comments

Comments
 (0)