7#ifndef _HARDWARE_CLOCKS_H
8#define _HARDWARE_CLOCKS_H
11#include "hardware/structs/clocks.h"
167#ifndef PLL_COMMON_REFDIV
169#define PLL_COMMON_REFDIV 1
173#ifndef PLL_SYS_REFDIV
175#ifdef PLL_COMMON_REFDIV
176#define PLL_SYS_REFDIV PLL_COMMON_REFDIV
178#define PLL_SYS_REFDIV 1
182#ifndef PLL_SYS_VCO_FREQ_HZ
184#ifdef PLL_SYS_VCO_FREQ_KHZ
185#define PLL_SYS_VCO_FREQ_HZ (PLL_SYS_VCO_FREQ_KHZ * KHZ)
189#if (SYS_CLK_HZ == 125 * MHZ || SYS_CLK_HZ == 150 * MHZ) && (XOSC_HZ == 12 * MHZ) && (PLL_SYS_REFDIV == 1)
192#ifndef PLL_SYS_VCO_FREQ_HZ
193#define PLL_SYS_VCO_FREQ_HZ (1500 * MHZ)
196#ifndef PLL_SYS_POSTDIV1
197#if SYS_CLK_HZ == 125 * MHZ
198#define PLL_SYS_POSTDIV1 6
200#define PLL_SYS_POSTDIV1 5
204#ifndef PLL_SYS_POSTDIV2
205#define PLL_SYS_POSTDIV2 2
209#if !defined(PLL_SYS_VCO_FREQ_HZ) || !defined(PLL_SYS_POSTDIV1) || !defined(PLL_SYS_POSTDIV2)
210#error PLL_SYS_VCO_FREQ_HZ, PLL_SYS_POSTDIV1 and PLL_SYS_POSTDIV2 must all be specified when using custom clock setup
214#ifndef PLL_USB_REFDIV
216#ifdef PLL_COMMON_REFDIV
217#define PLL_USB_REFDIV PLL_COMMON_REFDIV
219#define PLL_USB_REFDIV 1
223#ifndef PLL_USB_VCO_FREQ_HZ
225#ifdef PLL_USB_VCO_FREQ_KHZ
226#define PLL_USB_VCO_FREQ_HZ (PLL_USB_VCO_FREQ_KHZ * KHZ)
230#if (USB_CLK_HZ == 48 * MHZ) && (XOSC_HZ == 12 * MHZ) && (PLL_USB_REFDIV == 1)
233#ifndef PLL_USB_VCO_FREQ_HZ
234#define PLL_USB_VCO_FREQ_HZ (1200 * MHZ)
237#ifndef PLL_USB_POSTDIV1
238#define PLL_USB_POSTDIV1 5
241#ifndef PLL_USB_POSTDIV2
242#define PLL_USB_POSTDIV2 5
245#if !defined(PLL_USB_VCO_FREQ_HZ) || !defined(PLL_USB_POSTDIV1) || !defined(PLL_USB_POSTDIV2)
246#error PLL_USB_VCO_FREQ_HZ, PLL_USB_POSTDIV1 and PLL_USB_POSTDIV2 must all be specified when using custom clock setup.
250#ifndef PARAM_ASSERTIONS_ENABLED_HARDWARE_CLOCKS
251#ifdef PARAM_ASSERTIONS_ENABLED_CLOCKS
252#define PARAM_ASSERTIONS_ENABLED_HARDWARE_CLOCKS PARAM_ASSERTIONS_ENABLED_CLOCKS
254#define PARAM_ASSERTIONS_ENABLED_HARDWARE_CLOCKS 0
271bool clock_configure(clock_handle_t clock, uint32_t src, uint32_t auxsrc, uint32_t src_freq, uint32_t freq);
329static inline float frequency_count_mhz(uint src) {
370 uint div_int = (uint)div;
371 uint8_t frac = (uint8_t)((div - (
float)div_int) * (1u << CLOCKS_CLK_GPOUT0_DIV_INT_LSB));
412bool check_sys_clock_hz(uint32_t freq_hz, uint *vco_freq_out, uint *post_div1_out, uint *post_div2_out);
423bool check_sys_clock_khz(uint32_t freq_khz, uint *vco_freq_out, uint *post_div1_out, uint *post_div2_out);
437 uint vco, postdiv1, postdiv2;
441 }
else if (required) {
442 panic(
"System clock of %u Hz cannot be exactly achieved", freq_hz);
459 uint vco, postdiv1, postdiv2;
463 }
else if (required) {
464 panic(
"System clock of %u kHz cannot be exactly achieved", freq_khz);
void clock_configure_int_divider(clock_handle_t clock, uint32_t src, uint32_t auxsrc, uint32_t src_freq, uint32_t int_divider)
Configure the specified clock to use the undividded input source.
Definition clocks.c:112
void clock_stop(clock_handle_t clock)
Stop the specified clock.
Definition clocks.c:33
bool clock_configure(clock_handle_t clock, uint32_t src, uint32_t auxsrc, uint32_t src_freq, uint32_t freq)
Configure the specified clock.
Definition clocks.c:98
void(* resus_callback_t)(void)
Resus callback function type.
Definition clocks.h:339
void clocks_enable_resus(resus_callback_t resus_callback)
Enable the resus function. Restarts clk_sys if it is accidentally stopped.
Definition clocks.c:207
void clock_configure_undivided(clock_handle_t clock, uint32_t src, uint32_t auxsrc, uint32_t src_freq)
Configure the specified clock to use the undividded input source.
Definition clocks.c:116
static void clock_gpio_init(uint gpio, uint src, float div)
Output an optionally divided clock to the specified gpio pin.
Definition clocks.h:368
void clock_set_reported_hz(clock_handle_t clock, uint hz)
Set the "current frequency" of the clock as reported by clock_get_hz without actually changing the cl...
Definition clocks.c:128
uint32_t clock_get_hz(clock_handle_t clock)
Get the current frequency of the specified clock.
Definition clocks.c:123
bool clock_configure_gpin(clock_handle_t clock, uint gpio, uint32_t src_freq, uint32_t freq)
Configure a clock to come from a gpio input.
Definition clocks.c:292
enum clock_num_rp2040 clock_num_t
Clock numbers on RP2040 (used as typedef clock_num_t)
uint32_t frequency_count_khz(uint src)
Measure a clocks frequency using the Frequency counter.
Definition clocks.c:133
void clock_gpio_init_int_frac(uint gpio, uint src, uint32_t div_int, uint8_t div_frac)
Output an optionally divided clock to the specified gpio pin.
Definition clocks.c:231
static bool set_sys_clock_hz(uint32_t freq_hz, bool required)
Attempt to set a system clock frequency in hz.
Definition clocks.h:436
void set_sys_clock_pll(uint32_t vco_freq, uint post_div1, uint post_div2)
Initialise the system clock.
Definition clocks.c:354
bool check_sys_clock_khz(uint32_t freq_khz, uint *vco_freq_out, uint *post_div1_out, uint *post_div2_out)
Check if a given system clock frequency is valid/attainable.
Definition clocks.c:412
void set_sys_clock_48mhz(void)
Initialise the system clock to 48MHz.
Definition clocks.c:325
static bool set_sys_clock_khz(uint32_t freq_khz, bool required)
Attempt to set a system clock frequency in khz.
Definition clocks.h:458
bool check_sys_clock_hz(uint32_t freq_hz, uint *vco_freq_out, uint *post_div1_out, uint *post_div2_out)
Check if a given system clock frequency is valid/attainable.
Definition clocks.c:391