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_HZ < (1 * PICO_MHZ) || XOSC_HZ > (50 * PICO_MHZ) 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 appropriate RP-series datasheet. 19 #error XOSC_HZ must be in the range 1,000,000-50,000,000 i.e. 1-50MHz XOSC frequency 20 #endif 21 22 #define STARTUP_DELAY ((((XOSC_HZ / PICO_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 tight_loop_contents(); 42 } 43 } 44 xosc_disable(void)45void xosc_disable(void) { 46 uint32_t tmp = xosc_hw->ctrl; 47 tmp &= (~XOSC_CTRL_ENABLE_BITS); 48 tmp |= (XOSC_CTRL_ENABLE_VALUE_DISABLE << XOSC_CTRL_ENABLE_LSB); 49 xosc_hw->ctrl = tmp; 50 // Wait for stable to go away 51 while(xosc_hw->status & XOSC_STATUS_STABLE_BITS) { 52 tight_loop_contents(); 53 } 54 } 55 xosc_dormant(void)56void xosc_dormant(void) { 57 // WARNING: This stops the xosc until woken up by an irq 58 xosc_hw->dormant = XOSC_DORMANT_VALUE_DORMANT; 59 // Wait for it to become stable once woken up 60 while(!(xosc_hw->status & XOSC_STATUS_STABLE_BITS)) { 61 tight_loop_contents(); 62 } 63 } 64