Initial Release
This commit is contained in:
11
src/rp2_common/hardware_clocks/CMakeLists.txt
Normal file
11
src/rp2_common/hardware_clocks/CMakeLists.txt
Normal file
@ -0,0 +1,11 @@
|
||||
pico_simple_hardware_target(clocks)
|
||||
|
||||
target_link_libraries(hardware_clocks INTERFACE
|
||||
hardware_resets
|
||||
hardware_watchdog
|
||||
hardware_xosc
|
||||
hardware_pll
|
||||
# not currently used by clocks.c, but sensibly bundled here
|
||||
# as changing frequencies may require upping voltage
|
||||
hardware_vreg
|
||||
)
|
389
src/rp2_common/hardware_clocks/clocks.c
Normal file
389
src/rp2_common/hardware_clocks/clocks.c
Normal file
@ -0,0 +1,389 @@
|
||||
/*
|
||||
* Copyright (c) 2020 Raspberry Pi (Trading) Ltd.
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-3-Clause
|
||||
*/
|
||||
|
||||
#include "pico.h"
|
||||
#include "hardware/regs/clocks.h"
|
||||
#include "hardware/platform_defs.h"
|
||||
#include "hardware/resets.h"
|
||||
#include "hardware/clocks.h"
|
||||
#include "hardware/watchdog.h"
|
||||
#include "hardware/pll.h"
|
||||
#include "hardware/xosc.h"
|
||||
#include "hardware/irq.h"
|
||||
#include "hardware/gpio.h"
|
||||
|
||||
check_hw_layout(clocks_hw_t, clk[clk_adc].selected, CLOCKS_CLK_ADC_SELECTED_OFFSET);
|
||||
check_hw_layout(clocks_hw_t, fc0.result, CLOCKS_FC0_RESULT_OFFSET);
|
||||
check_hw_layout(clocks_hw_t, ints, CLOCKS_INTS_OFFSET);
|
||||
|
||||
static uint32_t configured_freq[CLK_COUNT];
|
||||
|
||||
static resus_callback_t _resus_callback;
|
||||
|
||||
// Clock muxing consists of two components:
|
||||
// - A glitchless mux, which can be switched freely, but whose inputs must be
|
||||
// free-running
|
||||
// - An auxiliary (glitchy) mux, whose output glitches when switched, but has
|
||||
// no constraints on its inputs
|
||||
// Not all clocks have both types of mux.
|
||||
static inline bool has_glitchless_mux(enum clock_index clk_index) {
|
||||
return clk_index == clk_sys || clk_index == clk_ref;
|
||||
}
|
||||
|
||||
void clock_stop(enum clock_index clk_index) {
|
||||
clock_hw_t *clock = &clocks_hw->clk[clk_index];
|
||||
hw_clear_bits(&clock->ctrl, CLOCKS_CLK_USB_CTRL_ENABLE_BITS);
|
||||
configured_freq[clk_index] = 0;
|
||||
}
|
||||
|
||||
/// \tag::clock_configure[]
|
||||
bool clock_configure(enum clock_index clk_index, uint32_t src, uint32_t auxsrc, uint32_t src_freq, uint32_t freq) {
|
||||
uint32_t div;
|
||||
|
||||
assert(src_freq >= freq);
|
||||
|
||||
if (freq > src_freq)
|
||||
return false;
|
||||
|
||||
// Div register is 24.8 int.frac divider so multiply by 2^8 (left shift by 8)
|
||||
div = (uint32_t) (((uint64_t) src_freq << 8) / freq);
|
||||
|
||||
clock_hw_t *clock = &clocks_hw->clk[clk_index];
|
||||
|
||||
// If increasing divisor, set divisor before source. Otherwise set source
|
||||
// before divisor. This avoids a momentary overspeed when e.g. switching
|
||||
// to a faster source and increasing divisor to compensate.
|
||||
if (div > clock->div)
|
||||
clock->div = div;
|
||||
|
||||
// If switching a glitchless slice (ref or sys) to an aux source, switch
|
||||
// away from aux *first* to avoid passing glitches when changing aux mux.
|
||||
// Assume (!!!) glitchless source 0 is no faster than the aux source.
|
||||
if (has_glitchless_mux(clk_index) && src == CLOCKS_CLK_SYS_CTRL_SRC_VALUE_CLKSRC_CLK_SYS_AUX) {
|
||||
hw_clear_bits(&clock->ctrl, CLOCKS_CLK_REF_CTRL_SRC_BITS);
|
||||
while (!(clock->selected & 1u))
|
||||
tight_loop_contents();
|
||||
}
|
||||
// If no glitchless mux, cleanly stop the clock to avoid glitches
|
||||
// propagating when changing aux mux. Note it would be a really bad idea
|
||||
// to do this on one of the glitchless clocks (clk_sys, clk_ref).
|
||||
else {
|
||||
hw_clear_bits(&clock->ctrl, CLOCKS_CLK_GPOUT0_CTRL_ENABLE_BITS);
|
||||
if (configured_freq[clk_index] > 0) {
|
||||
// Delay for 3 cycles of the target clock, for ENABLE propagation.
|
||||
// Note XOSC_COUNT is not helpful here because XOSC is not
|
||||
// necessarily running, nor is timer... so, 3 cycles per loop:
|
||||
uint delay_cyc = configured_freq[clk_sys] / configured_freq[clk_index] + 1;
|
||||
asm volatile (
|
||||
"1: \n\t"
|
||||
"sub %0, #1 \n\t"
|
||||
"bne 1b"
|
||||
: "+r" (delay_cyc)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// Set aux mux first, and then glitchless mux if this clock has one
|
||||
hw_write_masked(&clock->ctrl,
|
||||
(auxsrc << CLOCKS_CLK_SYS_CTRL_AUXSRC_LSB),
|
||||
CLOCKS_CLK_SYS_CTRL_AUXSRC_BITS
|
||||
);
|
||||
|
||||
if (has_glitchless_mux(clk_index)) {
|
||||
hw_write_masked(&clock->ctrl,
|
||||
src << CLOCKS_CLK_REF_CTRL_SRC_LSB,
|
||||
CLOCKS_CLK_REF_CTRL_SRC_BITS
|
||||
);
|
||||
while (!(clock->selected & (1u << src)))
|
||||
tight_loop_contents();
|
||||
}
|
||||
|
||||
hw_set_bits(&clock->ctrl, CLOCKS_CLK_GPOUT0_CTRL_ENABLE_BITS);
|
||||
|
||||
// Now that the source is configured, we can trust that the user-supplied
|
||||
// divisor is a safe value.
|
||||
clock->div = div;
|
||||
|
||||
// Store the configured frequency
|
||||
configured_freq[clk_index] = freq;
|
||||
|
||||
return true;
|
||||
}
|
||||
/// \end::clock_configure[]
|
||||
|
||||
void clocks_init(void) {
|
||||
// Start tick in watchdog
|
||||
watchdog_start_tick(XOSC_MHZ);
|
||||
|
||||
// Everything is 48MHz on FPGA apart from RTC. Otherwise set to 0 and will be set in clock configure
|
||||
if (running_on_fpga()) {
|
||||
for (uint i = 0; i < CLK_COUNT; i++) {
|
||||
configured_freq[i] = 48 * MHZ;
|
||||
}
|
||||
configured_freq[clk_rtc] = 46875;
|
||||
return;
|
||||
}
|
||||
|
||||
// Disable resus that may be enabled from previous software
|
||||
clocks_hw->resus.ctrl = 0;
|
||||
|
||||
// Enable the xosc
|
||||
xosc_init();
|
||||
|
||||
// Before we touch PLLs, switch sys and ref cleanly away from their aux sources.
|
||||
hw_clear_bits(&clocks_hw->clk[clk_sys].ctrl, CLOCKS_CLK_SYS_CTRL_SRC_BITS);
|
||||
while (clocks_hw->clk[clk_sys].selected != 0x1)
|
||||
tight_loop_contents();
|
||||
hw_clear_bits(&clocks_hw->clk[clk_ref].ctrl, CLOCKS_CLK_REF_CTRL_SRC_BITS);
|
||||
while (clocks_hw->clk[clk_ref].selected != 0x1)
|
||||
tight_loop_contents();
|
||||
|
||||
/// \tag::pll_settings[]
|
||||
// Configure PLLs
|
||||
// REF FBDIV VCO POSTDIV
|
||||
// PLL SYS: 12 / 1 = 12MHz * 125 = 1500MHZ / 6 / 2 = 125MHz
|
||||
// PLL USB: 12 / 1 = 12MHz * 40 = 480 MHz / 5 / 2 = 48MHz
|
||||
/// \end::pll_settings[]
|
||||
|
||||
reset_block(RESETS_RESET_PLL_SYS_BITS | RESETS_RESET_PLL_USB_BITS);
|
||||
unreset_block_wait(RESETS_RESET_PLL_SYS_BITS | RESETS_RESET_PLL_USB_BITS);
|
||||
|
||||
/// \tag::pll_init[]
|
||||
pll_init(pll_sys, 1, 1500 * MHZ, 6, 2);
|
||||
pll_init(pll_usb, 1, 480 * MHZ, 5, 2);
|
||||
/// \end::pll_init[]
|
||||
|
||||
// Configure clocks
|
||||
// CLK_REF = XOSC (12MHz) / 1 = 12MHz
|
||||
clock_configure(clk_ref,
|
||||
CLOCKS_CLK_REF_CTRL_SRC_VALUE_XOSC_CLKSRC,
|
||||
0, // No aux mux
|
||||
12 * MHZ,
|
||||
12 * MHZ);
|
||||
|
||||
/// \tag::configure_clk_sys[]
|
||||
// CLK SYS = PLL SYS (125MHz) / 1 = 125MHz
|
||||
clock_configure(clk_sys,
|
||||
CLOCKS_CLK_SYS_CTRL_SRC_VALUE_CLKSRC_CLK_SYS_AUX,
|
||||
CLOCKS_CLK_SYS_CTRL_AUXSRC_VALUE_CLKSRC_PLL_SYS,
|
||||
125 * MHZ,
|
||||
125 * MHZ);
|
||||
/// \end::configure_clk_sys[]
|
||||
|
||||
// CLK USB = PLL USB (48MHz) / 1 = 48MHz
|
||||
clock_configure(clk_usb,
|
||||
0, // No GLMUX
|
||||
CLOCKS_CLK_USB_CTRL_AUXSRC_VALUE_CLKSRC_PLL_USB,
|
||||
48 * MHZ,
|
||||
48 * MHZ);
|
||||
|
||||
// CLK ADC = PLL USB (48MHZ) / 1 = 48MHz
|
||||
clock_configure(clk_adc,
|
||||
0, // No GLMUX
|
||||
CLOCKS_CLK_ADC_CTRL_AUXSRC_VALUE_CLKSRC_PLL_USB,
|
||||
48 * MHZ,
|
||||
48 * MHZ);
|
||||
|
||||
// CLK RTC = PLL USB (48MHz) / 1024 = 46875Hz
|
||||
clock_configure(clk_rtc,
|
||||
0, // No GLMUX
|
||||
CLOCKS_CLK_RTC_CTRL_AUXSRC_VALUE_CLKSRC_PLL_USB,
|
||||
48 * MHZ,
|
||||
46875);
|
||||
|
||||
// CLK PERI = clk_sys. Used as reference clock for Peripherals. No dividers so just select and enable
|
||||
// Normally choose clk_sys or clk_usb
|
||||
clock_configure(clk_peri,
|
||||
0,
|
||||
CLOCKS_CLK_PERI_CTRL_AUXSRC_VALUE_CLK_SYS,
|
||||
125 * MHZ,
|
||||
125 * MHZ);
|
||||
}
|
||||
|
||||
/// \tag::clock_get_hz[]
|
||||
uint32_t clock_get_hz(enum clock_index clk_index) {
|
||||
return configured_freq[clk_index];
|
||||
}
|
||||
/// \end::clock_get_hz[]
|
||||
|
||||
void clock_set_reported_hz(enum clock_index clk_index, uint hz) {
|
||||
configured_freq[clk_index] = hz;
|
||||
}
|
||||
|
||||
/// \tag::frequency_count_khz[]
|
||||
uint32_t frequency_count_khz(uint src) {
|
||||
fc_hw_t *fc = &clocks_hw->fc0;
|
||||
|
||||
// If frequency counter is running need to wait for it. It runs even if the source is NULL
|
||||
while(fc->status & CLOCKS_FC0_STATUS_RUNNING_BITS) {
|
||||
tight_loop_contents();
|
||||
}
|
||||
|
||||
// Set reference freq
|
||||
fc->ref_khz = clock_get_hz(clk_ref) / 1000;
|
||||
|
||||
// FIXME: Don't pick random interval. Use best interval
|
||||
fc->interval = 10;
|
||||
|
||||
// No min or max
|
||||
fc->min_khz = 0;
|
||||
fc->max_khz = 0xffffffff;
|
||||
|
||||
// Set SRC which automatically starts the measurement
|
||||
fc->src = src;
|
||||
|
||||
while(!(fc->status & CLOCKS_FC0_STATUS_DONE_BITS)) {
|
||||
tight_loop_contents();
|
||||
}
|
||||
|
||||
// Return the result
|
||||
return fc->result >> CLOCKS_FC0_RESULT_KHZ_LSB;
|
||||
}
|
||||
/// \end::frequency_count_khz[]
|
||||
|
||||
static void clocks_handle_resus(void) {
|
||||
// Set clk_sys back to the ref clock rather than it being forced to clk_ref
|
||||
// by resus. Call the user's resus callback if they have set one
|
||||
|
||||
// CLK SYS = CLK_REF. Must be running for this code to be running
|
||||
uint clk_ref_freq = clock_get_hz(clk_ref);
|
||||
clock_configure(clk_sys,
|
||||
CLOCKS_CLK_SYS_CTRL_SRC_VALUE_CLK_REF,
|
||||
0,
|
||||
clk_ref_freq,
|
||||
clk_ref_freq);
|
||||
|
||||
// Assert we have been resussed
|
||||
assert(clocks_hw->resus.status & CLOCKS_CLK_SYS_RESUS_STATUS_RESUSSED_BITS);
|
||||
|
||||
// Now we have fixed clk_sys we can safely remove the resus
|
||||
hw_set_bits(&clocks_hw->resus.ctrl, CLOCKS_CLK_SYS_RESUS_CTRL_CLEAR_BITS);
|
||||
hw_clear_bits(&clocks_hw->resus.ctrl, CLOCKS_CLK_SYS_RESUS_CTRL_CLEAR_BITS);
|
||||
|
||||
// Now we should no longer be resussed
|
||||
assert(!(clocks_hw->resus.status & CLOCKS_CLK_SYS_RESUS_STATUS_RESUSSED_BITS));
|
||||
|
||||
// Call the user's callback to notify them of the resus event
|
||||
if (_resus_callback) {
|
||||
_resus_callback();
|
||||
}
|
||||
}
|
||||
|
||||
static void clocks_irq_handler(void) {
|
||||
// Clocks interrupt handler. Only resus but handle irq
|
||||
// defensively just in case.
|
||||
uint32_t ints = clocks_hw->ints;
|
||||
|
||||
if (ints & CLOCKS_INTE_CLK_SYS_RESUS_BITS) {
|
||||
ints &= ~CLOCKS_INTE_CLK_SYS_RESUS_BITS;
|
||||
clocks_handle_resus();
|
||||
}
|
||||
|
||||
#ifndef NDEBUG
|
||||
if (ints) {
|
||||
panic("Unexpected clocks irq\n");
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
void clocks_enable_resus(resus_callback_t resus_callback) {
|
||||
// Restart clk_sys if it is stopped by forcing it
|
||||
// to the default source of clk_ref. If clk_ref stops running this will
|
||||
// not work.
|
||||
|
||||
// Store user's resus callback
|
||||
_resus_callback = resus_callback;
|
||||
|
||||
irq_set_exclusive_handler(CLOCKS_IRQ, clocks_irq_handler);
|
||||
|
||||
// Enable the resus interrupt in clocks
|
||||
clocks_hw->inte = CLOCKS_INTE_CLK_SYS_RESUS_BITS;
|
||||
|
||||
// Enable the clocks irq
|
||||
irq_set_enabled(CLOCKS_IRQ, true);
|
||||
|
||||
// 2 * clk_ref freq / clk_sys_min_freq;
|
||||
// assume clk_ref is 3MHz and we want clk_sys to be no lower than 1MHz
|
||||
uint timeout = 2 * 3 * 1;
|
||||
|
||||
// Enable resus with the maximum timeout
|
||||
clocks_hw->resus.ctrl = CLOCKS_CLK_SYS_RESUS_CTRL_ENABLE_BITS | timeout;
|
||||
}
|
||||
|
||||
void clock_gpio_init(uint gpio, uint src, uint div) {
|
||||
// Bit messy but it's as much code to loop through a lookup
|
||||
// table. The sources for each gpout generators are the same
|
||||
// so just call with the sources from GP0
|
||||
uint gpclk = 0;
|
||||
if (gpio == 21) gpclk = clk_gpout0;
|
||||
else if (gpio == 23) gpclk = clk_gpout1;
|
||||
else if (gpio == 24) gpclk = clk_gpout2;
|
||||
else if (gpio == 26) gpclk = clk_gpout3;
|
||||
else {
|
||||
invalid_params_if(CLOCKS, true);
|
||||
}
|
||||
|
||||
// Set up the gpclk generator
|
||||
clocks_hw->clk[gpclk].ctrl = (src << CLOCKS_CLK_GPOUT0_CTRL_AUXSRC_LSB) |
|
||||
CLOCKS_CLK_GPOUT0_CTRL_ENABLE_BITS;
|
||||
clocks_hw->clk[gpclk].div = div << CLOCKS_CLK_GPOUT0_DIV_INT_LSB;
|
||||
|
||||
// Set gpio pin to gpclock function
|
||||
gpio_set_function(gpio, GPIO_FUNC_GPCK);
|
||||
}
|
||||
|
||||
static const uint8_t gpin0_src[CLK_COUNT] = {
|
||||
CLOCKS_CLK_GPOUT0_CTRL_AUXSRC_VALUE_CLKSRC_GPIN0, // CLK_GPOUT0
|
||||
CLOCKS_CLK_GPOUT1_CTRL_AUXSRC_VALUE_CLKSRC_GPIN0, // CLK_GPOUT1
|
||||
CLOCKS_CLK_GPOUT2_CTRL_AUXSRC_VALUE_CLKSRC_GPIN0, // CLK_GPOUT2
|
||||
CLOCKS_CLK_GPOUT3_CTRL_AUXSRC_VALUE_CLKSRC_GPIN0, // CLK_GPOUT3
|
||||
CLOCKS_CLK_REF_CTRL_AUXSRC_VALUE_CLKSRC_GPIN0, // CLK_REF
|
||||
CLOCKS_CLK_SYS_CTRL_AUXSRC_VALUE_CLKSRC_GPIN0, // CLK_SYS
|
||||
CLOCKS_CLK_PERI_CTRL_AUXSRC_VALUE_CLKSRC_GPIN0, // CLK_PERI
|
||||
CLOCKS_CLK_USB_CTRL_AUXSRC_VALUE_CLKSRC_GPIN0, // CLK_USB
|
||||
CLOCKS_CLK_ADC_CTRL_AUXSRC_VALUE_CLKSRC_GPIN0, // CLK_ADC
|
||||
CLOCKS_CLK_RTC_CTRL_AUXSRC_VALUE_CLKSRC_GPIN0, // CLK_RTC
|
||||
};
|
||||
|
||||
// Assert GPIN1 is GPIN0 + 1
|
||||
static_assert(CLOCKS_CLK_GPOUT0_CTRL_AUXSRC_VALUE_CLKSRC_GPIN1 == (CLOCKS_CLK_GPOUT0_CTRL_AUXSRC_VALUE_CLKSRC_GPIN0 + 1), "hw mismatch");
|
||||
static_assert(CLOCKS_CLK_GPOUT1_CTRL_AUXSRC_VALUE_CLKSRC_GPIN1 == (CLOCKS_CLK_GPOUT1_CTRL_AUXSRC_VALUE_CLKSRC_GPIN0 + 1), "hw mismatch");
|
||||
static_assert(CLOCKS_CLK_GPOUT2_CTRL_AUXSRC_VALUE_CLKSRC_GPIN1 == (CLOCKS_CLK_GPOUT2_CTRL_AUXSRC_VALUE_CLKSRC_GPIN0 + 1), "hw mismatch");
|
||||
static_assert(CLOCKS_CLK_GPOUT3_CTRL_AUXSRC_VALUE_CLKSRC_GPIN1 == (CLOCKS_CLK_GPOUT3_CTRL_AUXSRC_VALUE_CLKSRC_GPIN0 + 1), "hw mismatch");
|
||||
static_assert(CLOCKS_CLK_REF_CTRL_AUXSRC_VALUE_CLKSRC_GPIN1 == (CLOCKS_CLK_REF_CTRL_AUXSRC_VALUE_CLKSRC_GPIN0 + 1), "hw mismatch");
|
||||
static_assert(CLOCKS_CLK_SYS_CTRL_AUXSRC_VALUE_CLKSRC_GPIN1 == (CLOCKS_CLK_SYS_CTRL_AUXSRC_VALUE_CLKSRC_GPIN0 + 1), "hw mismatch");
|
||||
static_assert(CLOCKS_CLK_PERI_CTRL_AUXSRC_VALUE_CLKSRC_GPIN1 == (CLOCKS_CLK_PERI_CTRL_AUXSRC_VALUE_CLKSRC_GPIN0 + 1), "hw mismatch");
|
||||
static_assert(CLOCKS_CLK_USB_CTRL_AUXSRC_VALUE_CLKSRC_GPIN1 == (CLOCKS_CLK_USB_CTRL_AUXSRC_VALUE_CLKSRC_GPIN0 + 1), "hw mismatch");
|
||||
static_assert(CLOCKS_CLK_ADC_CTRL_AUXSRC_VALUE_CLKSRC_GPIN1 == (CLOCKS_CLK_ADC_CTRL_AUXSRC_VALUE_CLKSRC_GPIN0 + 1), "hw mismatch");
|
||||
static_assert(CLOCKS_CLK_RTC_CTRL_AUXSRC_VALUE_CLKSRC_GPIN1 == (CLOCKS_CLK_RTC_CTRL_AUXSRC_VALUE_CLKSRC_GPIN0 + 1), "hw mismatch");
|
||||
|
||||
bool clock_configure_gpin(enum clock_index clk_index, uint gpio, uint32_t src_freq, uint32_t freq) {
|
||||
// Configure a clock to run from a GPIO input
|
||||
uint gpin = 0;
|
||||
if (gpio == 20) gpin = 0;
|
||||
else if (gpio == 22) gpin = 1;
|
||||
else {
|
||||
invalid_params_if(CLOCKS, true);
|
||||
}
|
||||
|
||||
// Work out sources. GPIN is always an auxsrc
|
||||
uint src = 0;
|
||||
|
||||
// GPIN1 == GPIN0 + 1
|
||||
uint auxsrc = gpin0_src[clk_index] + gpin;
|
||||
|
||||
if (has_glitchless_mux(clk_index)) {
|
||||
// AUX src is always 1
|
||||
src = 1;
|
||||
}
|
||||
|
||||
// Set the GPIO function
|
||||
gpio_set_function(gpio, GPIO_FUNC_GPCK);
|
||||
|
||||
// Now we have the src, auxsrc, and configured the gpio input
|
||||
// call clock configure to run the clock from a gpio
|
||||
return clock_configure(clk_index, src, auxsrc, src_freq, freq);
|
||||
}
|
194
src/rp2_common/hardware_clocks/include/hardware/clocks.h
Normal file
194
src/rp2_common/hardware_clocks/include/hardware/clocks.h
Normal file
@ -0,0 +1,194 @@
|
||||
/*
|
||||
* Copyright (c) 2020 Raspberry Pi (Trading) Ltd.
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-3-Clause
|
||||
*/
|
||||
|
||||
#ifndef _HARDWARE_CLOCKS_H_
|
||||
#define _HARDWARE_CLOCKS_H_
|
||||
|
||||
#include "pico.h"
|
||||
#include "hardware/structs/clocks.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/** \file hardware/clocks.h
|
||||
* \defgroup hardware_clocks hardware_clocks
|
||||
*
|
||||
* Clock Management API
|
||||
*
|
||||
* This API provides a high level interface to the clock functions.
|
||||
*
|
||||
* The clocks block provides independent clocks to on-chip and external components. It takes inputs from a variety of clock
|
||||
* sources allowing the user to trade off performance against cost, board area and power consumption. From these sources
|
||||
* it uses multiple clock generators to provide the required clocks. This architecture allows the user flexibility to start and
|
||||
* stop clocks independently and to vary some clock frequencies whilst maintaining others at their optimum frequencies
|
||||
*
|
||||
* Please refer to the datasheet for more details on the RP2040 clocks.
|
||||
*
|
||||
* The clock source depends on which clock you are attempting to configure. The first table below shows main clock sources. If
|
||||
* you are not setting the Reference clock or the System clock, or you are specifying that one of those two will be using an auxiliary
|
||||
* clock source, then you will need to use one of the entries from the subsequent tables.
|
||||
*
|
||||
* **Main Clock Sources**
|
||||
*
|
||||
* Source | Reference Clock | System Clock
|
||||
* -------|-----------------|---------
|
||||
* ROSC | CLOCKS_CLK_REF_CTRL_SRC_VALUE_ROSC_CLKSRC_PH | |
|
||||
* Auxiliary | CLOCKS_CLK_REF_CTRL_SRC_VALUE_CLKSRC_CLK_REF_AUX | CLOCKS_CLK_SYS_CTRL_SRC_VALUE_CLKSRC_CLK_SYS_AUX
|
||||
* XOSC | CLOCKS_CLK_REF_CTRL_SRC_VALUE_XOSC_CLKSRC | |
|
||||
* Reference | | CLOCKS_CLK_SYS_CTRL_SRC_VALUE_CLK_REF
|
||||
*
|
||||
* **Auxiliary Clock Sources**
|
||||
*
|
||||
* The auxiliary clock sources available for use in the configure function depend on which clock is being configured. The following table
|
||||
* describes the available values that can be used. Note that for clk_gpout[x], x can be 0-3.
|
||||
*
|
||||
*
|
||||
* Aux Source | clk_gpout[x] | clk_ref | clk_sys
|
||||
* -----------|------------|---------|--------
|
||||
* System PLL | CLOCKS_CLK_GPOUTx_CTRL_AUXSRC_VALUE_CLKSRC_PLL_SYS | | CLOCKS_CLK_SYS_CTRL_AUXSRC_VALUE_CLKSRC_PLL_SYS
|
||||
* GPIO in 0 | CLOCKS_CLK_GPOUTx_CTRL_AUXSRC_VALUE_CLKSRC_GPIN0 | CLOCKS_CLK_REF_CTRL_AUXSRC_VALUE_CLKSRC_GPIN0 | CLOCKS_CLK_SYS_CTRL_AUXSRC_VALUE_CLKSRC_GPIN0
|
||||
* GPIO in 1 | CLOCKS_CLK_GPOUTx_CTRL_AUXSRC_VALUE_CLKSRC_GPIN1 | CLOCKS_CLK_REF_CTRL_AUXSRC_VALUE_CLKSRC_GPIN1 | CLOCKS_CLK_SYS_CTRL_AUXSRC_VALUE_CLKSRC_GPIN1
|
||||
* USB PLL | CLOCKS_CLK_GPOUTx_CTRL_AUXSRC_VALUE_CLKSRC_PLL_USB | CLOCKS_CLK_REF_CTRL_AUXSRC_VALUE_CLKSRC_PLL_USB| CLOCKS_CLK_SYS_CTRL_AUXSRC_VALUE_CLKSRC_PLL_USB
|
||||
* ROSC | CLOCKS_CLK_GPOUTx_CTRL_AUXSRC_VALUE_ROSC_CLKSRC | | CLOCKS_CLK_SYS_CTRL_AUXSRC_VALUE_ROSC_CLKSRC
|
||||
* XOSC | CLOCKS_CLK_GPOUTx_CTRL_AUXSRC_VALUE_XOSC_CLKSRC | | CLOCKS_CLK_SYS_CTRL_AUXSRC_VALUE_ROSC_CLKSRC
|
||||
* System clock | CLOCKS_CLK_GPOUTx_CTRL_AUXSRC_VALUE_CLK_SYS | | |
|
||||
* USB Clock | CLOCKS_CLK_GPOUTx_CTRL_AUXSRC_VALUE_CLK_USB | | |
|
||||
* ADC clock | CLOCKS_CLK_GPOUTx_CTRL_AUXSRC_VALUE_CLK_ADC | | |
|
||||
* RTC Clock | CLOCKS_CLK_GPOUTx_CTRL_AUXSRC_VALUE_CLK_RTC | | |
|
||||
* Ref clock | CLOCKS_CLK_GPOUTx_CTRL_AUXSRC_VALUE_CLK_REF | | |
|
||||
*
|
||||
* Aux Source | clk_peri | clk_usb | clk_adc
|
||||
* -----------|-----------|---------|--------
|
||||
* System PLL | CLOCKS_CLK_PERI_CTRL_AUXSRC_VALUE_CLKSRC_PLL_SYS | CLOCKS_CLK_USB_CTRL_AUXSRC_VALUE_CLKSRC_PLL_SYS | CLOCKS_CLK_ADC_CTRL_AUXSRC_VALUE_CLKSRC_PLL_SYS
|
||||
* GPIO in 0 | CLOCKS_CLK_PERI_CTRL_AUXSRC_VALUE_CLKSRC_GPIN0 | CLOCKS_CLK_USB_CTRL_AUXSRC_VALUE_CLKSRC_GPIN0 | CLOCKS_CLK_ADC_CTRL_AUXSRC_VALUE_CLKSRC_GPIN0
|
||||
* GPIO in 1 | CLOCKS_CLK_PERI_CTRL_AUXSRC_VALUE_CLKSRC_GPIN1 | CLOCKS_CLK_USB_CTRL_AUXSRC_VALUE_CLKSRC_GPIN1 | CLOCKS_CLK_ADC_CTRL_AUXSRC_VALUE_CLKSRC_GPIN1
|
||||
* USB PLL | CLOCKS_CLK_PERI_CTRL_AUXSRC_VALUE_CLKSRC_PLL_USB | CLOCKS_CLK_USB_CTRL_AUXSRC_VALUE_CLKSRC_PLL_USB | CLOCKS_CLK_ADC_CTRL_AUXSRC_VALUE_CLKSRC_PLL_USB
|
||||
* ROSC | CLOCKS_CLK_PERI_CTRL_AUXSRC_VALUE_ROSC_CLKSRC_PH | CLOCKS_CLK_USB_CTRL_AUXSRC_VALUE_ROSC_CLKSRC_PH | CLOCKS_CLK_ADC_CTRL_AUXSRC_VALUE_ROSC_CLKSRC_PH
|
||||
* XOSC | CLOCKS_CLK_PERI_CTRL_AUXSRC_VALUE_XOSC_CLKSRC | CLOCKS_CLK_USB_CTRL_AUXSRC_VALUE_XOSC_CLKSRC | CLOCKS_CLK_ADC_CTRL_AUXSRC_VALUE_XOSC_CLKSRC
|
||||
* System clock | CLOCKS_CLK_PERI_CTRL_AUXSRC_VALUE_CLK_SYS | | |
|
||||
*
|
||||
* Aux Source | clk_rtc
|
||||
* -----------|----------
|
||||
* System PLL | CLOCKS_CLK_RTC_CTRL_AUXSRC_VALUE_CLKSRC_PLL_SYS
|
||||
* GPIO in 0 | CLOCKS_CLK_RTC_CTRL_AUXSRC_VALUE_CLKSRC_GPIN0
|
||||
* GPIO in 1 | CLOCKS_CLK_RTC_CTRL_AUXSRC_VALUE_CLKSRC_GPIN1
|
||||
* USB PLL | CLOCKS_CLK_RTC_CTRL_AUXSRC_VALUE_CLKSRC_PLL_USB
|
||||
* ROSC | CLOCKS_CLK_RTC_CTRL_AUXSRC_VALUE_ROSC_CLKSRC_PH
|
||||
* XOSC | CLOCKS_CLK_RTC_CTRL_AUXSRC_VALUE_XOSC_CLKSRC
|
||||
|
||||
*
|
||||
* \section clock_example Example
|
||||
* \addtogroup hardware_clocks
|
||||
* \include hello_48MHz.c
|
||||
*/
|
||||
|
||||
#define KHZ 1000
|
||||
#define MHZ 1000000
|
||||
|
||||
// PICO_CONFIG: PARAM_ASSERTIONS_ENABLED_CLOCKS, Enable/disable assertions in the clocks module, type=bool, default=0, group=hardware_clocks
|
||||
#ifndef PARAM_ASSERTIONS_ENABLED_CLOCKS
|
||||
#define PARAM_ASSERTIONS_ENABLED_CLOCKS 0
|
||||
#endif
|
||||
|
||||
/*! \brief Initialise the clock hardware
|
||||
* \ingroup hardware_clocks
|
||||
*
|
||||
* Must be called before any other clock function.
|
||||
*/
|
||||
void clocks_init();
|
||||
|
||||
/*! \brief Configure the specified clock
|
||||
* \ingroup hardware_clocks
|
||||
*
|
||||
* See the tables in the description for details on the possible values for clock sources.
|
||||
*
|
||||
* \param clk_index 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 Frequency of the input clock source
|
||||
* \param freq Requested frequency
|
||||
*/
|
||||
bool clock_configure(enum clock_index clk_index, uint32_t src, uint32_t auxsrc, uint32_t src_freq, uint32_t freq);
|
||||
|
||||
/*! \brief Stop the specified clock
|
||||
* \ingroup hardware_clocks
|
||||
*
|
||||
* \param clk_index The clock to stop
|
||||
*/
|
||||
void clock_stop(enum clock_index clk_index);
|
||||
|
||||
/*! \brief Get the current frequency of the specified clock
|
||||
* \ingroup hardware_clocks
|
||||
*
|
||||
* \param clk_index Clock
|
||||
* \return Clock frequency in Hz
|
||||
*/
|
||||
uint32_t clock_get_hz(enum clock_index clk_index);
|
||||
|
||||
/*! \brief Measure a clocks frequency using the Frequency counter.
|
||||
* \ingroup hardware_clocks
|
||||
*
|
||||
* Uses the inbuilt frequency counter to measure the specified clocks frequency.
|
||||
* Currently, this function is accurate to +-1KHz. See the datasheet for more details.
|
||||
*/
|
||||
uint32_t frequency_count_khz(uint src);
|
||||
|
||||
/*! \brief Set the "current frequency" of the clock as reported by clock_get_hz without actually changing the clock
|
||||
* \ingroup hardware_clocks
|
||||
*
|
||||
* \see clock_get_hz
|
||||
*/
|
||||
void clock_set_reported_hz(enum clock_index clk_index, uint hz);
|
||||
|
||||
/// \tag::frequency_count_mhz[]
|
||||
static inline float frequency_count_mhz(uint src) {
|
||||
return ((float) (frequency_count_khz(src))) / KHZ;
|
||||
}
|
||||
/// \end::frequency_count_mhz[]
|
||||
|
||||
/*! \brief Resus callback function type.
|
||||
* \ingroup hardware_clocks
|
||||
*
|
||||
* User provided callback for a resus event (when clk_sys is stopped by the programmer and is restarted for them).
|
||||
*/
|
||||
typedef void (*resus_callback_t)(void);
|
||||
|
||||
/*! \brief Enable the resus function. Restarts clk_sys if it is accidentally stopped.
|
||||
* \ingroup hardware_clocks
|
||||
*
|
||||
* The resuscitate function will restart the system clock if it falls below a certain speed (or stops). This
|
||||
* could happen if the clock source the system clock is running from stops. For example if a PLL is stopped.
|
||||
*
|
||||
* \param resus_callback a function pointer provided by the user to call if a resus event happens.
|
||||
*/
|
||||
void clocks_enable_resus(resus_callback_t resus_callback);
|
||||
|
||||
/*! \brief Output an optionally divided clock to the specified gpio pin.
|
||||
* \ingroup hardware_clocks
|
||||
*
|
||||
* \param gpio The GPIO pin to output the clock to. Valid GPIOs are: 21, 23, 24, 26. These GPIOs are connected to the GPOUT0-3 clock generators.
|
||||
* \param src The source clock. See the register field CLOCKS_CLK_GPOUT0_CTRL_AUXSRC for a full list. The list is the same for each GPOUT clock generator.
|
||||
* \param div The amount to divide the source clock by. This is useful to not overwhelm the GPIO pin with a fast clock.
|
||||
*/
|
||||
void clock_gpio_init(uint gpio, uint src, uint div);
|
||||
|
||||
/*! \brief Configure a clock to come from a gpio input
|
||||
* \ingroup hardware_clocks
|
||||
*
|
||||
* \param clk_index The clock to configure
|
||||
* \param gpio The GPIO pin to run the clock from. Valid GPIOs are: 20 and 22.
|
||||
* \param src_freq Frequency of the input clock source
|
||||
* \param freq Requested frequency
|
||||
*/
|
||||
bool clock_configure_gpin(enum clock_index clk_index, uint gpio, uint32_t src_freq, uint32_t freq);
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
37
src/rp2_common/hardware_clocks/scripts/vcocalc.py
Executable file
37
src/rp2_common/hardware_clocks/scripts/vcocalc.py
Executable file
@ -0,0 +1,37 @@
|
||||
#!/usr/bin/env python3
|
||||
|
||||
import argparse
|
||||
|
||||
parser = argparse.ArgumentParser(description="PLL parameter calculator")
|
||||
parser.add_argument("--input", "-i", default=12, help="Input (reference) frequency. Default 12 MHz", type=float)
|
||||
parser.add_argument("--vco-max", default=1600, help="Override maximum VCO frequency. Default 1600 MHz", type=float)
|
||||
parser.add_argument("--vco-min", default=400, help="Override minimum VCO frequency. Default 400 MHz", type=float)
|
||||
parser.add_argument("--low-vco", "-l", action="store_true", help="Use a lower VCO frequency when possible. This reduces power consumption, at the cost of increased jitter")
|
||||
parser.add_argument("output", help="Output frequency in MHz.", type=float)
|
||||
args = parser.parse_args()
|
||||
|
||||
# Fixed hardware parameters
|
||||
fbdiv_range = range(16, 320 + 1)
|
||||
postdiv_range = range(1, 7 + 1)
|
||||
|
||||
best = (0, 0, 0, 0)
|
||||
best_margin = args.output
|
||||
|
||||
for fbdiv in (fbdiv_range if args.low_vco else reversed(fbdiv_range)):
|
||||
vco = args.input * fbdiv
|
||||
if vco < args.vco_min or vco > args.vco_max:
|
||||
continue
|
||||
# pd1 is inner loop so that we prefer higher ratios of pd1:pd2
|
||||
for pd2 in postdiv_range:
|
||||
for pd1 in postdiv_range:
|
||||
out = vco / pd1 / pd2
|
||||
margin = abs(out - args.output)
|
||||
if margin < best_margin:
|
||||
best = (out, fbdiv, pd1, pd2)
|
||||
best_margin = margin
|
||||
|
||||
print("Requested: {} MHz".format(args.output))
|
||||
print("Achieved: {} MHz".format(best[0]))
|
||||
print("FBDIV: {} (VCO = {} MHz)".format(best[1], args.input * best[1]))
|
||||
print("PD1: {}".format(best[2]))
|
||||
print("PD2: {}".format(best[3]))
|
Reference in New Issue
Block a user