Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions src/rp2_common/hardware_clocks/clocks.c
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,26 @@ bool clock_configure(clock_handle_t clock, uint32_t src, uint32_t auxsrc, uint32
return true;
}

bool clock_configure_mhz(clock_handle_t clock, uint32_t src, uint32_t auxsrc, uint16_t src_freq_mhz, uint16_t freq_mhz) {
assert(src_freq_mhz >= freq_mhz);

if (freq_mhz > src_freq_mhz)
return false;

uint32_t div = (uint32_t)((((uint32_t) src_freq_mhz) << CLOCKS_CLK_GPOUT0_DIV_INT_LSB) / freq_mhz);
#if PICO_RP2040
// on RP2040 only clock divider of 1, or >= 2 are supported
if (div < (2u << CLOCKS_CLK_GPOUT0_DIV_INT_LSB)) {
div = (1u << CLOCKS_CLK_GPOUT0_DIV_INT_LSB);
}
#endif
uint32_t actual_freq = (uint32_t) ((((uint32_t) src_freq_mhz) << CLOCKS_CLK_GPOUT0_DIV_INT_LSB) / div) * MHZ;

clock_configure_internal(clock, src, auxsrc, actual_freq, div);
// Store the configured frequency
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What does this comment mean? (and is it referring to the line below or the line above the comment?)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I’m not entirely sure (it was copied from the function above) - it’s related to the line above it as that’s what stores the configured frequency

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps it'd be less confusing if the comment was moved above the line it's referring to, as is done conventionally? 🙂

return true;
}

void clock_configure_int_divider(clock_handle_t clock, uint32_t src, uint32_t auxsrc, uint32_t src_freq, uint32_t int_divider) {
clock_configure_internal(clock, src, auxsrc, src_freq / int_divider, int_divider << CLOCKS_CLK_GPOUT0_DIV_INT_LSB);
}
Expand Down
26 changes: 26 additions & 0 deletions src/rp2_common/hardware_clocks/include/hardware/clocks.h
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,32 @@ typedef clock_num_t clock_handle_t;
*/
bool clock_configure(clock_handle_t clock, uint32_t src, uint32_t auxsrc, uint32_t src_freq, uint32_t freq);

/*! \brief Configure the specified clock with 1MHz accuracy
* \ingroup hardware_clocks
*
* This function differs from clock_configure in that it does not configure the clocks as accurately,
* but therefore doesn't need to bring in 64-bit division functions, reducing the code size if 64-bit
* division is not otherwise used by the application.
*
* \if rp2350_specific
* Note: The RP2350 clock hardware supports divisors from 1.0->65536.0 in steps of 1/65536
*
* \endif
* \if rp2040_specific
* Note: The RP2040 clock hardware only supports divisors of exactly 1.0 or 2.0->16777216.0 in steps of 1/256
* \endif
*
* See the tables in the description for details on the possible values for clock sources.
*
* \param clock The clock to configure
* \param src The main clock source, can be 0.
* \param auxsrc The auxiliary clock source, which depends on which clock is being set. Can be 0
* \param src_freq_mhz Frequency of the input clock source in MHz
* \param freq_mhz Requested frequency in MHz
* \return true if the clock is updated, false if freq > src_freq
*/
bool clock_configure_mhz(clock_handle_t clock, uint32_t src, uint32_t auxsrc, uint16_t src_freq_mhz, uint16_t freq_mhz);

/*! \brief Configure the specified clock to use the undivided input source
* \ingroup hardware_clocks
*
Expand Down
Loading