|
| 1 | +/* |
| 2 | + * Copyright (c) 2022, Andrey Kunitsyn |
| 3 | + * |
| 4 | + * This file is part of the modm project. |
| 5 | + * |
| 6 | + * This Source Code Form is subject to the terms of the Mozilla Public |
| 7 | + * License, v. 2.0. If a copy of the MPL was not distributed with this |
| 8 | + * file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 9 | + */ |
| 10 | +// ---------------------------------------------------------------------------- |
| 11 | + |
| 12 | +#include "clocks.hpp" |
| 13 | + |
| 14 | +#include <hardware/structs/xosc.h> |
| 15 | + |
| 16 | +#include "../device.hpp" |
| 17 | + |
| 18 | +#ifndef PICO_XOSC_STARTUP_DELAY_MULTIPLIER |
| 19 | +#define PICO_XOSC_STARTUP_DELAY_MULTIPLIER 1 |
| 20 | +#endif |
| 21 | + |
| 22 | +// CMSIS Core compliance |
| 23 | +constinit uint32_t modm_fastdata SystemCoreClock(modm::platform::ClockControl::BootFrequency); |
| 24 | + |
| 25 | +namespace modm::platform |
| 26 | +{ |
| 27 | +constinit uint16_t modm_fastdata delay_fcpu_MHz(computeDelayMhz(ClockControl::BootFrequency)); |
| 28 | +constinit uint16_t modm_fastdata delay_ns_per_loop(computeDelayNsPerLoop(ClockControl::BootFrequency)); |
| 29 | + |
| 30 | +static uint32_t configured_freq[CLK_COUNT]; |
| 31 | + |
| 32 | +void |
| 33 | +ClockControl::enableExternalCrystal(uint32_t freq) |
| 34 | +{ |
| 35 | + // Assumes 1-15 MHz input, checked above. |
| 36 | + xosc_hw->ctrl = XOSC_CTRL_FREQ_RANGE_VALUE_1_15MHZ; |
| 37 | + |
| 38 | + uint32_t delay = ((((freq / 1000) + 128) / 256) * PICO_XOSC_STARTUP_DELAY_MULTIPLIER); |
| 39 | + |
| 40 | + // Set xosc startup delay |
| 41 | + xosc_hw->startup = delay; |
| 42 | + |
| 43 | + // Set the enable bit now that we have set freq range and startup delay |
| 44 | + hw_set_bits(&xosc_hw->ctrl, XOSC_CTRL_ENABLE_VALUE_ENABLE << XOSC_CTRL_ENABLE_LSB); |
| 45 | + |
| 46 | + // Wait for XOSC to be stable |
| 47 | + while (!(xosc_hw->status & XOSC_STATUS_STABLE_BITS)) __NOP(); |
| 48 | +} |
| 49 | + |
| 50 | +void |
| 51 | +ClockControl::configureImpl(Clock clk, uint32_t src, uint32_t auxsrc, uint32_t div, uint32_t freq) |
| 52 | +{ |
| 53 | + clock_hw_t *clock = &clocks_hw->clk[uint32_t(clk)]; |
| 54 | + // If increasing divisor, set divisor before source. Otherwise set source |
| 55 | + // before divisor. This avoids a momentary overspeed when e.g. switching |
| 56 | + // to a faster source and increasing divisor to compensate. |
| 57 | + if (div > clock->div) clock->div = div; |
| 58 | + // If switching a glitchless slice to an aux source, switch |
| 59 | + // away from aux *first* to avoid passing glitches when changing aux mux. |
| 60 | + // Assume (!!!) glitchless source 0 is no faster than the aux source. |
| 61 | + if (src == CLOCKS_CLK_SYS_CTRL_SRC_VALUE_CLKSRC_CLK_SYS_AUX) |
| 62 | + { |
| 63 | + hw_clear_bits(&clock->ctrl, CLOCKS_CLK_REF_CTRL_SRC_BITS); |
| 64 | + while (!(clock->selected & 1u)) __NOP(); |
| 65 | + } |
| 66 | + // If no glitchless mux, cleanly stop the clock to avoid glitches |
| 67 | + // propagating when changing aux mux. Note it would be a really bad idea |
| 68 | + // to do this on one of the glitchless clocks. |
| 69 | + else |
| 70 | + { |
| 71 | + // Disable clock. On clk_ref and clk_sys this does nothing, |
| 72 | + // all other clocks have the ENABLE bit in the same position. |
| 73 | + hw_clear_bits(&clock->ctrl, CLOCKS_CLK_GPOUT0_CTRL_ENABLE_BITS); |
| 74 | + if (configured_freq[uint32_t(clk)] > 0) |
| 75 | + { |
| 76 | + // Delay for 3 cycles of the target clock, for ENABLE propagation. |
| 77 | + // Note XOSC_COUNT is not helpful here because XOSC is not |
| 78 | + // necessarily running, nor is timer... so, 3 cycles per loop: |
| 79 | + uint32_t delay_cyc = configured_freq[clk_sys] / configured_freq[uint32_t(clk)] + 1; |
| 80 | + asm volatile( |
| 81 | + ".syntax unified \n\t" |
| 82 | + "1: \n\t" |
| 83 | + "subs %0, #1 \n\t" |
| 84 | + "bne 1b" |
| 85 | + : "+r"(delay_cyc)); |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + // Set aux mux first, and then glitchless mux |
| 90 | + hw_write_masked(&clock->ctrl, (auxsrc << CLOCKS_CLK_SYS_CTRL_AUXSRC_LSB), |
| 91 | + CLOCKS_CLK_SYS_CTRL_AUXSRC_BITS); |
| 92 | + |
| 93 | + hw_write_masked(&clock->ctrl, src << CLOCKS_CLK_REF_CTRL_SRC_LSB, |
| 94 | + CLOCKS_CLK_REF_CTRL_SRC_BITS); |
| 95 | + while (!(clock->selected & (1u << src))) __NOP(); |
| 96 | + |
| 97 | + // Enable clock. On clk_ref and clk_sys this does nothing, |
| 98 | + // all other clocks have the ENABLE bit in the same position. |
| 99 | + hw_set_bits(&clock->ctrl, CLOCKS_CLK_GPOUT0_CTRL_ENABLE_BITS); |
| 100 | + |
| 101 | + // Now that the source is configured, we can trust that the user-supplied |
| 102 | + // divisor is a safe value. |
| 103 | + clock->div = div; |
| 104 | + // Store the configured frequency |
| 105 | + configured_freq[uint32_t(clk)] = freq; |
| 106 | +} |
| 107 | + |
| 108 | +void |
| 109 | +ClockControl::configureImpl(Clock clk, uint32_t auxsrc, uint32_t div, uint32_t freq) |
| 110 | +{ |
| 111 | + |
| 112 | + clock_hw_t *clock = &clocks_hw->clk[uint32_t(clk)]; |
| 113 | + // If increasing divisor, set divisor before source. Otherwise set source |
| 114 | + // before divisor. This avoids a momentary overspeed when e.g. switching |
| 115 | + // to a faster source and increasing divisor to compensate. |
| 116 | + if (div > clock->div) clock->div = div; |
| 117 | + // Disable clock. On clk_ref and clk_sys this does nothing, |
| 118 | + // all other clocks have the ENABLE bit in the same position. |
| 119 | + hw_clear_bits(&clock->ctrl, CLOCKS_CLK_GPOUT0_CTRL_ENABLE_BITS); |
| 120 | + if (configured_freq[uint32_t(clk)] > 0) |
| 121 | + { |
| 122 | + // Delay for 3 cycles of the target clock, for ENABLE propagation. |
| 123 | + // Note XOSC_COUNT is not helpful here because XOSC is not |
| 124 | + // necessarily running, nor is timer... so, 3 cycles per loop: |
| 125 | + uint32_t delay_cyc = configured_freq[clk_sys] / configured_freq[uint32_t(clk)] + 1; |
| 126 | + asm volatile( |
| 127 | + ".syntax unified \n\t" |
| 128 | + "1: \n\t" |
| 129 | + "subs %0, #1 \n\t" |
| 130 | + "bne 1b" |
| 131 | + : "+r"(delay_cyc)); |
| 132 | + } |
| 133 | + |
| 134 | + // Set aux mux |
| 135 | + hw_write_masked(&clock->ctrl, (auxsrc << CLOCKS_CLK_SYS_CTRL_AUXSRC_LSB), |
| 136 | + CLOCKS_CLK_SYS_CTRL_AUXSRC_BITS); |
| 137 | + |
| 138 | + // Enable clock. On clk_ref and clk_sys this does nothing, |
| 139 | + // all other clocks have the ENABLE bit in the same position. |
| 140 | + hw_set_bits(&clock->ctrl, CLOCKS_CLK_GPOUT0_CTRL_ENABLE_BITS); |
| 141 | + |
| 142 | + // Now that the source is configured, we can trust that the user-supplied |
| 143 | + // divisor is a safe value. |
| 144 | + clock->div = div; |
| 145 | + // Store the configured frequency |
| 146 | + configured_freq[uint32_t(clk)] = freq; |
| 147 | +} |
| 148 | +} // namespace modm::platform |
0 commit comments