Skip to content

Commit 0d90992

Browse files
authored
clarify clock_configure and make it return the correct value achieved for bad clock inputs (earlephilhower#2225)
1 parent 5288585 commit 0d90992

File tree

2 files changed

+14
-3
lines changed

2 files changed

+14
-3
lines changed

src/rp2_common/hardware_clocks/clocks.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,10 @@ bool clock_configure(clock_handle_t clock, uint32_t src, uint32_t auxsrc, uint32
102102
return false;
103103

104104
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);
108+
}
105109
uint32_t actual_freq = (uint32_t) ((((uint64_t) src_freq) << CLOCKS_CLK_GPOUT0_DIV_INT_LSB) / div);
106110

107111
clock_configure_internal(clock, src, auxsrc, actual_freq, div);

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

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -262,11 +262,18 @@ extern "C" {
262262

263263
typedef clock_num_t clock_handle_t;
264264

265-
/*! \brief Configure the specified clock
265+
/*! \brief Configure the specified clock with automatic clock divisor setup
266266
* \ingroup hardware_clocks
267267
*
268+
* This method allows both the src_frequency of the input clock source AND the desired
269+
* frequency to be specified, and will set the clock divider to achieve the exact or higher frequency
270+
* achievable, with the maximum being the src_freq.
271+
*
272+
* Note: That the clock hardware only support divisors of exactly 1 or 2.0->65535.0
273+
*
268274
* See the tables in the description for details on the possible values for clock sources.
269275
*
276+
*
270277
* \param clock The clock to configure
271278
* \param src The main clock source, can be 0.
272279
* \param auxsrc The auxiliary clock source, which depends on which clock is being set. Can be 0
@@ -275,7 +282,7 @@ typedef clock_num_t clock_handle_t;
275282
*/
276283
bool clock_configure(clock_handle_t clock, uint32_t src, uint32_t auxsrc, uint32_t src_freq, uint32_t freq);
277284

278-
/*! \brief Configure the specified clock to use the undividded input source
285+
/*! \brief Configure the specified clock to use the undivided input source
279286
* \ingroup hardware_clocks
280287
*
281288
* See the tables in the description for details on the possible values for clock sources.
@@ -287,7 +294,7 @@ bool clock_configure(clock_handle_t clock, uint32_t src, uint32_t auxsrc, uint32
287294
*/
288295
void clock_configure_undivided(clock_handle_t clock, uint32_t src, uint32_t auxsrc, uint32_t src_freq);
289296

290-
/*! \brief Configure the specified clock to use the undividded input source
297+
/*! \brief Configure the specified clock to use the undivided input source
291298
* \ingroup hardware_clocks
292299
*
293300
* See the tables in the description for details on the possible values for clock sources.

0 commit comments

Comments
 (0)