2022-06-15 12:49:24 -04:00
|
|
|
/*
|
|
|
|
* This file is part of the MicroPython project, http://micropython.org/
|
|
|
|
*
|
|
|
|
* This file provides functions for configuring the clocks.
|
|
|
|
*
|
|
|
|
* The MIT License (MIT)
|
|
|
|
*
|
|
|
|
* Copyright (c) 2022 Robert Hammelrath
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdint.h>
|
|
|
|
|
|
|
|
#include "py/runtime.h"
|
2022-06-15 12:58:02 -04:00
|
|
|
#include "py/mphal.h"
|
2022-06-15 12:49:24 -04:00
|
|
|
#include "samd_soc.h"
|
|
|
|
|
|
|
|
static uint32_t cpu_freq = CPU_FREQ;
|
2022-06-30 10:50:15 -04:00
|
|
|
static uint32_t peripheral_freq = DFLL48M_FREQ;
|
2022-06-15 12:58:02 -04:00
|
|
|
static uint32_t dfll48m_calibration;
|
2022-06-15 12:49:24 -04:00
|
|
|
|
|
|
|
int sercom_gclk_id[] = {
|
|
|
|
GCLK_CLKCTRL_ID_SERCOM0_CORE, GCLK_CLKCTRL_ID_SERCOM1_CORE,
|
|
|
|
GCLK_CLKCTRL_ID_SERCOM2_CORE, GCLK_CLKCTRL_ID_SERCOM3_CORE,
|
|
|
|
GCLK_CLKCTRL_ID_SERCOM4_CORE, GCLK_CLKCTRL_ID_SERCOM5_CORE
|
|
|
|
};
|
|
|
|
|
|
|
|
uint32_t get_cpu_freq(void) {
|
|
|
|
return cpu_freq;
|
|
|
|
}
|
|
|
|
|
2022-06-30 10:50:15 -04:00
|
|
|
uint32_t get_peripheral_freq(void) {
|
|
|
|
return peripheral_freq;
|
2022-06-15 12:49:24 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void set_cpu_freq(uint32_t cpu_freq_arg) {
|
2022-06-29 11:22:20 -04:00
|
|
|
|
2023-05-12 05:04:04 -04:00
|
|
|
// Set 1 wait state to be safe
|
2022-06-29 11:22:20 -04:00
|
|
|
NVMCTRL->CTRLB.reg = NVMCTRL_CTRLB_MANW | NVMCTRL_CTRLB_RWS(1);
|
|
|
|
|
2023-01-24 12:37:27 -05:00
|
|
|
int div = MAX(DFLL48M_FREQ / cpu_freq_arg, 1);
|
|
|
|
peripheral_freq = DFLL48M_FREQ / div;
|
2022-06-29 11:22:20 -04:00
|
|
|
|
2023-01-24 12:37:27 -05:00
|
|
|
// Enable GCLK output: 48MHz from DFLL48M on both CCLK0 and GCLK2
|
2022-06-29 11:22:20 -04:00
|
|
|
GCLK->GENDIV.reg = GCLK_GENDIV_ID(0) | GCLK_GENDIV_DIV(div);
|
|
|
|
GCLK->GENCTRL.reg = GCLK_GENCTRL_GENEN | GCLK_GENCTRL_SRC_DFLL48M | GCLK_GENCTRL_ID(0);
|
|
|
|
while (GCLK->STATUS.bit.SYNCBUSY) {
|
|
|
|
}
|
|
|
|
GCLK->GENDIV.reg = GCLK_GENDIV_ID(2) | GCLK_GENDIV_DIV(div);
|
|
|
|
GCLK->GENCTRL.reg = GCLK_GENCTRL_GENEN | GCLK_GENCTRL_SRC_DFLL48M | GCLK_GENCTRL_ID(2);
|
|
|
|
while (GCLK->STATUS.bit.SYNCBUSY) {
|
|
|
|
}
|
2023-01-24 12:37:27 -05:00
|
|
|
// The comparison is >=, such that for 48MHz still the FDPLL96 is used for the CPU clock.
|
|
|
|
if (cpu_freq_arg >= 48000000) {
|
|
|
|
cpu_freq = cpu_freq_arg;
|
|
|
|
// Connect GCLK1 to the FDPLL96 input.
|
|
|
|
GCLK->CLKCTRL.reg = GCLK_CLKCTRL_GEN_GCLK1 | GCLK_CLKCTRL_ID_FDPLL | GCLK_CLKCTRL_CLKEN;
|
|
|
|
while (GCLK->STATUS.bit.SYNCBUSY) {
|
|
|
|
}
|
|
|
|
// configure the FDPLL96
|
2023-05-12 05:04:04 -04:00
|
|
|
// CtrlB: Set the ref source to GCLK, set the Wakeup-Fast Flag.
|
2023-01-24 12:37:27 -05:00
|
|
|
SYSCTRL->DPLLCTRLB.reg = SYSCTRL_DPLLCTRLB_REFCLK_GCLK | SYSCTRL_DPLLCTRLB_WUF;
|
|
|
|
// Set the FDPLL ratio and enable the DPLL.
|
2023-01-24 14:36:19 -05:00
|
|
|
int ldr = cpu_freq / FDPLL_REF_FREQ;
|
|
|
|
int frac = ((cpu_freq - ldr * FDPLL_REF_FREQ) / (FDPLL_REF_FREQ / 16)) & 0x0f;
|
|
|
|
SYSCTRL->DPLLRATIO.reg = SYSCTRL_DPLLRATIO_LDR((frac << 16 | ldr) - 1);
|
2023-01-24 12:37:27 -05:00
|
|
|
SYSCTRL->DPLLCTRLA.reg = SYSCTRL_DPLLCTRLA_ENABLE;
|
|
|
|
// Wait for the DPLL lock.
|
|
|
|
while (!SYSCTRL->DPLLSTATUS.bit.LOCK) {
|
|
|
|
}
|
|
|
|
// Finally switch GCLK0 to FDPLL96M.
|
|
|
|
GCLK->GENCTRL.reg = GCLK_GENCTRL_GENEN | GCLK_GENCTRL_SRC_DPLL96M | GCLK_GENCTRL_ID(0);
|
|
|
|
while (GCLK->STATUS.bit.SYNCBUSY) {
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
cpu_freq = peripheral_freq;
|
|
|
|
// Disable the FDPLL96M in case it was enabled.
|
|
|
|
SYSCTRL->DPLLCTRLA.reg = 0;
|
|
|
|
}
|
2022-06-29 11:22:20 -04:00
|
|
|
if (cpu_freq >= 8000000) {
|
|
|
|
// Enable GCLK output: 48MHz on GCLK5 for USB
|
|
|
|
GCLK->GENDIV.reg = GCLK_GENDIV_ID(5) | GCLK_GENDIV_DIV(1);
|
|
|
|
GCLK->GENCTRL.reg = GCLK_GENCTRL_GENEN | GCLK_GENCTRL_SRC_DFLL48M | GCLK_GENCTRL_ID(5);
|
|
|
|
while (GCLK->STATUS.bit.SYNCBUSY) {
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Disable GCLK output on GCLK5 for USB, since USB is not reliable below 8 Mhz.
|
|
|
|
GCLK->GENCTRL.reg = GCLK_GENCTRL_ID(5);
|
|
|
|
while (GCLK->STATUS.bit.SYNCBUSY) {
|
|
|
|
}
|
|
|
|
}
|
2023-05-12 05:04:04 -04:00
|
|
|
// Set 0 wait states for slower CPU clock
|
2022-06-29 11:22:20 -04:00
|
|
|
NVMCTRL->CTRLB.reg = NVMCTRL_CTRLB_MANW | NVMCTRL_CTRLB_RWS(cpu_freq > 24000000 ? 1 : 0);
|
2022-08-01 11:23:11 -04:00
|
|
|
SysTick_Config(cpu_freq / 1000);
|
2022-06-15 12:49:24 -04:00
|
|
|
}
|
|
|
|
|
2022-06-15 12:58:02 -04:00
|
|
|
void check_usb_recovery_mode(void) {
|
|
|
|
#if !MICROPY_HW_XOSC32K
|
2023-05-12 05:04:04 -04:00
|
|
|
// Check USB status for up to 1 second. If not connected,
|
|
|
|
// switch DFLL48M back to open loop
|
|
|
|
for (int i = 0; i < 100; i++) {
|
|
|
|
if (USB->DEVICE.DeviceEndpoint[0].EPCFG.reg != 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
mp_hal_delay_ms(10);
|
2022-06-15 12:58:02 -04:00
|
|
|
}
|
2023-05-12 05:04:04 -04:00
|
|
|
// Set/keep the open loop mode of the device.
|
|
|
|
SYSCTRL->DFLLVAL.reg = dfll48m_calibration;
|
|
|
|
SYSCTRL->DFLLCTRL.reg = SYSCTRL_DFLLCTRL_CCDIS | SYSCTRL_DFLLCTRL_ENABLE;
|
2022-06-15 12:58:02 -04:00
|
|
|
#endif // MICROPY_HW_XOSC32K
|
|
|
|
}
|
2022-06-15 12:49:24 -04:00
|
|
|
|
2022-08-13 11:33:04 -04:00
|
|
|
// Purpose of the #defines for the clock configuration.
|
|
|
|
//
|
2023-05-12 05:04:04 -04:00
|
|
|
// The CPU is either driven by the FDPLL96 oscillator for f >= 48MHz,
|
|
|
|
// or by the DFLL48M for lower frequencies. The FDPLL96 takes 32768 Hz
|
|
|
|
// as reference frequency, supplied through GCLK1.
|
|
|
|
//
|
|
|
|
// DFLL48M is used for the peripheral clock, e.g. for PWM, UART, SPI, I2C.
|
|
|
|
// DFLL48M is either free running, or controlled by the 32kHz crystal, or
|
|
|
|
// Synchronized with the USB clock.
|
2022-08-13 11:33:04 -04:00
|
|
|
// Both CPU and peripheral devices are clocked by the DFLL48M clock.
|
|
|
|
// DFLL48M is either free running, or controlled by the 32kHz crystal, or
|
|
|
|
// Synchronized with the USB clock.
|
|
|
|
//
|
|
|
|
// #define MICROPY_HW_XOSC32K (0 | 1)
|
|
|
|
//
|
|
|
|
// If MICROPY_HW_XOSC32K = 1, the 32kHz crystal is used as input for GCLK 1, which
|
|
|
|
// serves as reference clock source for the DFLL48M oscillator,
|
|
|
|
// The crystal is used, unless MICROPY_HW_MCU_OSC32KULP is set.
|
|
|
|
// In that case GCLK1 (and the CPU clock) is driven by the 32K Low power oscillator.
|
|
|
|
// The reason for offering this option is a design flaw of the Adafruit
|
2023-05-12 05:04:04 -04:00
|
|
|
// Feather boards, where the RGB LED and Debug signals interfere with the
|
2022-08-13 11:33:04 -04:00
|
|
|
// crystal, causing the CPU to fail if it is driven by the crystal.
|
|
|
|
//
|
|
|
|
// If MICROPY_HW_XOSC32K = 0, the 32kHz signal for GCLK1 (and the CPU) is
|
|
|
|
// created by dividing the 48MHz clock of DFLL48M, but not used otherwise.
|
|
|
|
//
|
|
|
|
// If MICROPY_HW_DFLL_USB_SYNC = 0, the DFLL48M oscillator is free running using
|
|
|
|
// the pre-configured trim values. In that mode, the peripheral clock is
|
2023-05-12 05:04:04 -04:00
|
|
|
// not exactly 48Mhz and has a substitutional temperature drift.
|
2022-08-13 11:33:04 -04:00
|
|
|
//
|
|
|
|
// If MICROPY_HW_DFLL_USB_SYNC = 1, the DFLL48 is synchronized with the 1 kHz USB sync
|
2023-05-12 05:04:04 -04:00
|
|
|
// signal. If after boot there is no USB sync within 1000 ms, the configuration falls
|
2022-08-13 11:33:04 -04:00
|
|
|
// back to a free running 48Mhz oscillator.
|
|
|
|
//
|
|
|
|
// In all modes, the 48MHz signal has a substantial jitter, largest when
|
|
|
|
// MICROPY_HW_DFLL_USB_SYNC is active. That is caused by the respective
|
|
|
|
// reference frequencies of 32kHz or 1 kHz being low. That affects most
|
|
|
|
// PWM. Std Dev at 1kHz 0.156Hz (w. Crystal) up to 0.4 Hz (with USB sync).
|
|
|
|
//
|
|
|
|
// If none of the mentioned defines is set, the device uses the internal oscillators.
|
|
|
|
|
2022-06-15 12:49:24 -04:00
|
|
|
void init_clocks(uint32_t cpu_freq) {
|
|
|
|
|
2022-06-15 12:58:02 -04:00
|
|
|
dfll48m_calibration = 0; // please the compiler
|
|
|
|
|
2022-06-15 12:49:24 -04:00
|
|
|
// SAMD21 Clock settings
|
2023-01-24 05:23:47 -05:00
|
|
|
//
|
2023-01-24 12:37:27 -05:00
|
|
|
// GCLK0: 48MHz, source: DFLL48M or FDPLL96M, usage: CPU
|
|
|
|
// GCLK1: 32kHz, source: XOSC32K or OSCULP32K, usage: FDPLL96M reference
|
2023-01-24 05:23:47 -05:00
|
|
|
// GCLK2: 1-48MHz, source: DFLL48M, usage: Peripherals
|
2023-01-25 12:05:40 -05:00
|
|
|
// GCLK3: 2Mhz, source: DFLL48M, usage: us-counter (TC4/TC5)
|
2023-01-24 05:23:47 -05:00
|
|
|
// GCLK4: 32kHz, source: XOSC32K, if crystal present, usage: DFLL48M reference
|
|
|
|
// GCLK5: 48MHz, source: DFLL48M, usage: USB
|
|
|
|
// GCLK8: 1kHz, source: XOSC32K or OSCULP32K, usage: WDT and RTC
|
|
|
|
// DFLL48M: Reference sources:
|
|
|
|
// - in closed loop mode: either XOSC32K or OSCULP32K or USB clock
|
2023-01-24 12:37:27 -05:00
|
|
|
// from GCLK4.
|
2023-01-24 05:23:47 -05:00
|
|
|
// - in open loop mode: None
|
2023-01-24 12:37:27 -05:00
|
|
|
// FDPLL96M: Reference source GCLK1
|
|
|
|
// Used for the CPU clock for freq >= 48Mhz
|
2022-06-15 12:49:24 -04:00
|
|
|
|
|
|
|
NVMCTRL->CTRLB.bit.MANW = 1; // errata "Spurious Writes"
|
|
|
|
NVMCTRL->CTRLB.bit.RWS = 1; // 1 read wait state for 48MHz
|
|
|
|
|
|
|
|
#if MICROPY_HW_XOSC32K
|
2023-05-12 05:04:04 -04:00
|
|
|
// Set up OSC32K according data sheet 17.6.3
|
2022-06-15 12:49:24 -04:00
|
|
|
SYSCTRL->XOSC32K.reg = SYSCTRL_XOSC32K_STARTUP(0x3) | SYSCTRL_XOSC32K_EN32K |
|
|
|
|
SYSCTRL_XOSC32K_XTALEN;
|
|
|
|
SYSCTRL->XOSC32K.bit.ENABLE = 1;
|
|
|
|
while (SYSCTRL->PCLKSR.bit.XOSC32KRDY == 0) {
|
|
|
|
}
|
|
|
|
// Set up the DFLL48 according to the data sheet 17.6.7.1.2
|
|
|
|
// Step 1: Set up the reference clock
|
2022-06-15 12:58:02 -04:00
|
|
|
|
|
|
|
#if MICROPY_HW_MCU_OSC32KULP
|
|
|
|
// Connect the GCLK1 to the XOSC32KULP
|
|
|
|
GCLK->GENDIV.reg = GCLK_GENDIV_ID(1) | GCLK_GENDIV_DIV(1);
|
|
|
|
GCLK->GENCTRL.reg = GCLK_GENCTRL_GENEN | GCLK_GENCTRL_SRC_OSCULP32K | GCLK_GENCTRL_ID(1);
|
|
|
|
#else
|
2023-01-24 12:37:27 -05:00
|
|
|
// Connect the GCLK1 to OSC32K
|
2022-06-15 12:49:24 -04:00
|
|
|
GCLK->GENDIV.reg = GCLK_GENDIV_ID(1) | GCLK_GENDIV_DIV(1);
|
|
|
|
GCLK->GENCTRL.reg = GCLK_GENCTRL_GENEN | GCLK_GENCTRL_SRC_XOSC32K | GCLK_GENCTRL_ID(1);
|
2022-06-15 12:58:02 -04:00
|
|
|
#endif
|
|
|
|
|
2022-06-15 12:49:24 -04:00
|
|
|
while (GCLK->STATUS.bit.SYNCBUSY) {
|
|
|
|
}
|
2022-06-15 12:58:02 -04:00
|
|
|
|
2023-01-24 05:23:47 -05:00
|
|
|
// Connect the GCLK4 to OSC32K
|
2022-06-15 12:58:02 -04:00
|
|
|
GCLK->GENDIV.reg = GCLK_GENDIV_ID(4) | GCLK_GENDIV_DIV(1);
|
|
|
|
GCLK->GENCTRL.reg = GCLK_GENCTRL_GENEN | GCLK_GENCTRL_SRC_XOSC32K | GCLK_GENCTRL_ID(4);
|
2023-01-24 05:23:47 -05:00
|
|
|
// Connect GCLK4 to the DFLL input.
|
|
|
|
GCLK->CLKCTRL.reg = GCLK_CLKCTRL_GEN_GCLK4 | GCLK_CLKCTRL_ID_DFLL48 | GCLK_CLKCTRL_CLKEN;
|
2022-06-15 12:58:02 -04:00
|
|
|
while (GCLK->STATUS.bit.SYNCBUSY) {
|
|
|
|
}
|
|
|
|
|
2023-05-12 05:04:04 -04:00
|
|
|
// Enable access to the DFLLCTRL register according to Errata 1.2.1
|
2022-06-15 12:49:24 -04:00
|
|
|
SYSCTRL->DFLLCTRL.reg = SYSCTRL_DFLLCTRL_ENABLE;
|
|
|
|
while (SYSCTRL->PCLKSR.bit.DFLLRDY == 0) {
|
|
|
|
}
|
|
|
|
// Step 2: Set the coarse and fine values.
|
2023-05-12 05:04:04 -04:00
|
|
|
// Get the coarse value from the calibration data. In case it is not set,
|
2022-06-15 12:49:24 -04:00
|
|
|
// set a midrange value.
|
|
|
|
uint32_t coarse = (*((uint32_t *)FUSES_DFLL48M_COARSE_CAL_ADDR) & FUSES_DFLL48M_COARSE_CAL_Msk)
|
|
|
|
>> FUSES_DFLL48M_COARSE_CAL_Pos;
|
|
|
|
if (coarse == 0x3f) {
|
|
|
|
coarse = 0x1f;
|
|
|
|
}
|
|
|
|
SYSCTRL->DFLLVAL.reg = SYSCTRL_DFLLVAL_COARSE(coarse) | SYSCTRL_DFLLVAL_FINE(512);
|
|
|
|
while (SYSCTRL->PCLKSR.bit.DFLLRDY == 0) {
|
|
|
|
}
|
|
|
|
// Step 3: Set the multiplication values. The offset of 16384 to the freq is for rounding.
|
|
|
|
SYSCTRL->DFLLMUL.reg = SYSCTRL_DFLLMUL_MUL((CPU_FREQ + 16384) / 32768) |
|
|
|
|
SYSCTRL_DFLLMUL_FSTEP(1) | SYSCTRL_DFLLMUL_CSTEP(1);
|
|
|
|
while (SYSCTRL->PCLKSR.bit.DFLLRDY == 0) {
|
|
|
|
}
|
|
|
|
// Step 4: Start the DFLL and wait for the PLL lock. We just wait for the fine lock, since
|
|
|
|
// coarse adjusting is bypassed.
|
2022-06-15 12:58:02 -04:00
|
|
|
SYSCTRL->DFLLCTRL.reg |= SYSCTRL_DFLLCTRL_MODE | SYSCTRL_DFLLCTRL_WAITLOCK | SYSCTRL_DFLLCTRL_STABLE |
|
2022-06-15 12:49:24 -04:00
|
|
|
SYSCTRL_DFLLCTRL_BPLCKC | SYSCTRL_DFLLCTRL_ENABLE;
|
|
|
|
while (SYSCTRL->PCLKSR.bit.DFLLLCKF == 0) {
|
|
|
|
}
|
2022-09-15 09:58:54 -04:00
|
|
|
// Set GCLK8 to 1 kHz.
|
|
|
|
GCLK->GENDIV.reg = GCLK_GENDIV_ID(8) | GCLK_GENDIV_DIV(32);
|
|
|
|
GCLK->GENCTRL.reg = GCLK_GENCTRL_GENEN | GCLK_GENCTRL_SRC_XOSC32K | GCLK_GENCTRL_ID(8);
|
|
|
|
while (GCLK->STATUS.bit.SYNCBUSY) {
|
|
|
|
}
|
2022-06-15 12:49:24 -04:00
|
|
|
|
|
|
|
#else // MICROPY_HW_XOSC32K
|
|
|
|
|
|
|
|
// Enable DFLL48M
|
|
|
|
SYSCTRL->DFLLCTRL.reg = SYSCTRL_DFLLCTRL_ENABLE;
|
|
|
|
while (!SYSCTRL->PCLKSR.bit.DFLLRDY) {
|
|
|
|
}
|
2022-06-15 12:58:02 -04:00
|
|
|
|
2022-06-15 12:49:24 -04:00
|
|
|
uint32_t coarse = (*((uint32_t *)FUSES_DFLL48M_COARSE_CAL_ADDR) & FUSES_DFLL48M_COARSE_CAL_Msk)
|
|
|
|
>> FUSES_DFLL48M_COARSE_CAL_Pos;
|
|
|
|
if (coarse == 0x3f) {
|
|
|
|
coarse = 0x1f;
|
|
|
|
}
|
2022-06-15 12:58:02 -04:00
|
|
|
SYSCTRL->DFLLVAL.reg = SYSCTRL_DFLLVAL_COARSE(coarse) | SYSCTRL_DFLLVAL_FINE(511);
|
|
|
|
|
|
|
|
#if MICROPY_HW_DFLL_USB_SYNC
|
|
|
|
// Configure the DFLL48M for USB clock recovery.
|
|
|
|
// Will have to switch back if no USB
|
|
|
|
SYSCTRL->DFLLSYNC.bit.READREQ = 1;
|
|
|
|
dfll48m_calibration = SYSCTRL->DFLLVAL.reg;
|
|
|
|
// Set the Multiplication factor.
|
|
|
|
SYSCTRL->DFLLMUL.reg = SYSCTRL_DFLLMUL_CSTEP(1) | SYSCTRL_DFLLMUL_FSTEP(1)
|
|
|
|
| SYSCTRL_DFLLMUL_MUL(48000);
|
|
|
|
// Set the mode to closed loop USB Recovery mode
|
|
|
|
SYSCTRL->DFLLCTRL.reg = SYSCTRL_DFLLCTRL_USBCRM | SYSCTRL_DFLLCTRL_CCDIS
|
2022-06-15 12:49:24 -04:00
|
|
|
| SYSCTRL_DFLLCTRL_MODE | SYSCTRL_DFLLCTRL_ENABLE;
|
2022-06-15 12:58:02 -04:00
|
|
|
#else
|
|
|
|
// Set/keep the open loop mode of the device.
|
|
|
|
SYSCTRL->DFLLCTRL.reg = SYSCTRL_DFLLCTRL_CCDIS | SYSCTRL_DFLLCTRL_ENABLE;
|
|
|
|
#endif
|
|
|
|
|
2022-06-15 12:49:24 -04:00
|
|
|
while (!SYSCTRL->PCLKSR.bit.DFLLRDY) {
|
|
|
|
}
|
2022-06-15 12:58:02 -04:00
|
|
|
|
2023-01-24 12:37:27 -05:00
|
|
|
// Connect the GCLK1 to the XOSC32KULP
|
|
|
|
GCLK->GENDIV.reg = GCLK_GENDIV_ID(1) | GCLK_GENDIV_DIV(1);
|
|
|
|
GCLK->GENCTRL.reg = GCLK_GENCTRL_GENEN | GCLK_GENCTRL_SRC_OSCULP32K | GCLK_GENCTRL_ID(1);
|
2022-06-15 12:49:24 -04:00
|
|
|
while (GCLK->STATUS.bit.SYNCBUSY) {
|
|
|
|
}
|
2022-09-15 09:58:54 -04:00
|
|
|
// Set GCLK8 to 1 kHz.
|
|
|
|
GCLK->GENDIV.reg = GCLK_GENDIV_ID(8) | GCLK_GENDIV_DIV(32);
|
|
|
|
GCLK->GENCTRL.reg = GCLK_GENCTRL_GENEN | GCLK_GENCTRL_SRC_OSCULP32K | GCLK_GENCTRL_ID(8);
|
|
|
|
while (GCLK->STATUS.bit.SYNCBUSY) {
|
|
|
|
}
|
2022-06-15 12:49:24 -04:00
|
|
|
|
|
|
|
#endif // MICROPY_HW_XOSC32K
|
|
|
|
|
2022-06-29 11:22:20 -04:00
|
|
|
set_cpu_freq(cpu_freq);
|
2022-06-15 12:49:24 -04:00
|
|
|
|
2023-01-25 12:05:40 -05:00
|
|
|
// Enable GCLK output: 2MHz on GCLK3 for TC4
|
|
|
|
GCLK->GENDIV.reg = GCLK_GENDIV_ID(3) | GCLK_GENDIV_DIV(24);
|
2022-06-15 12:49:24 -04:00
|
|
|
GCLK->GENCTRL.reg = GCLK_GENCTRL_GENEN | GCLK_GENCTRL_SRC_DFLL48M | GCLK_GENCTRL_ID(3);
|
|
|
|
while (GCLK->STATUS.bit.SYNCBUSY) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void enable_sercom_clock(int id) {
|
|
|
|
// Enable synchronous clock. The bits are nicely arranged
|
|
|
|
PM->APBCMASK.reg |= 0x04 << id;
|
|
|
|
// Select multiplexer generic clock source and enable.
|
|
|
|
GCLK->CLKCTRL.reg = GCLK_CLKCTRL_CLKEN | GCLK_CLKCTRL_GEN_GCLK2 | sercom_gclk_id[id];
|
|
|
|
// Wait while it updates synchronously.
|
|
|
|
while (GCLK->STATUS.bit.SYNCBUSY) {
|
|
|
|
}
|
|
|
|
}
|