1 /* 2 * Copyright (c) 2020 Raspberry Pi (Trading) Ltd. 3 * 4 * SPDX-License-Identifier: BSD-3-Clause 5 */ 6 7 #include "pico.h" 8 9 // For frequency related definitions etc 10 #include "hardware/clocks.h" 11 12 #include "hardware/platform_defs.h" 13 #include "hardware/regs/xosc.h" 14 #include "hardware/xosc.h" 15 16 #if XOSC_KHZ < (1 * KHZ) || XOSC_KHZ > (50 * KHZ) 17 // Note: Although an external clock can be supplied up to 50 MHz, the maximum frequency the 18 // XOSC cell is specified to work with a crystal is less, please see the RP2040 Datasheet. 19 #error XOSC_KHZ must be in the range 1,000-50,000KHz i.e. 1-50MHz XOSC frequency 20 #endif 21 22 #define STARTUP_DELAY (((XOSC_KHZ + 128) / 256) * PICO_XOSC_STARTUP_DELAY_MULTIPLIER) 23 24 // The DELAY field in xosc_hw->startup is 14 bits wide. 25 #if STARTUP_DELAY >= (1 << 13) 26 #error PICO_XOSC_STARTUP_DELAY_MULTIPLIER is too large: XOSC STARTUP.DELAY must be < 8192 27 #endif 28 xosc_init(void)29void xosc_init(void) { 30 // Assumes 1-15 MHz input, checked above. 31 xosc_hw->ctrl = XOSC_CTRL_FREQ_RANGE_VALUE_1_15MHZ; 32 33 // Set xosc startup delay 34 xosc_hw->startup = STARTUP_DELAY; 35 36 // Set the enable bit now that we have set freq range and startup delay 37 hw_set_bits(&xosc_hw->ctrl, XOSC_CTRL_ENABLE_VALUE_ENABLE << XOSC_CTRL_ENABLE_LSB); 38 39 // Wait for XOSC to be stable 40 while(!(xosc_hw->status & XOSC_STATUS_STABLE_BITS)); 41 } 42 xosc_disable(void)43void xosc_disable(void) { 44 uint32_t tmp = xosc_hw->ctrl; 45 tmp &= (~XOSC_CTRL_ENABLE_BITS); 46 tmp |= (XOSC_CTRL_ENABLE_VALUE_DISABLE << XOSC_CTRL_ENABLE_LSB); 47 xosc_hw->ctrl = tmp; 48 // Wait for stable to go away 49 while(xosc_hw->status & XOSC_STATUS_STABLE_BITS); 50 } 51 xosc_dormant(void)52void xosc_dormant(void) { 53 // WARNING: This stops the xosc until woken up by an irq 54 xosc_hw->dormant = XOSC_DORMANT_VALUE_DORMANT; 55 // Wait for it to become stable once woken up 56 while(!(xosc_hw->status & XOSC_STATUS_STABLE_BITS)); 57 } 58