Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SAMx7x external clock #939

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions src/modm/board/samv71_xplained_ultra/board.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ using namespace modm::platform;

struct SystemClock
{
// 300MHz system clock generated by PLLA from internal Rc 12MHz clock
// 300 MHz system clock generated by PLLA from external 12 MHz crystal
static constexpr uint32_t PllAMult = 25;
static constexpr uint32_t Frequency = 300_MHz;
static constexpr uint32_t Mck = Frequency / 2; // 150 MHz max.
Expand All @@ -35,10 +35,11 @@ struct SystemClock
static bool inline
enable()
{
ClockGen::setFlashLatency<Frequency / 2>(); // Flash runs off MCK
ClockGen::setFlashLatency<Mck>();

ClockGen::enableMainExternalCrystal<12_MHz>(std::chrono::microseconds{1000});
ClockGen::selectMainClockSource(MainClockSource::External);

ClockGen::enableMainInternal(MainInternalFreq::Rc12Mhz);
ClockGen::selectMainClockSource(MainClockSource::Internal);
ClockGen::enablePllA<PllAMult>();
ClockGen::selectMasterClk<MasterClkSource::PLLA_CLK, MasterClkPrescaler::CLK_1, MasterClkDivider::Div2>();
ClockGen::updateCoreFrequency<Frequency>();
Expand Down
12 changes: 10 additions & 2 deletions src/modm/platform/clock/sam_pmc/clockgen.hpp.in
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/**
* Copyright (c) 2021, Jeff McBride
* Copyright (c) 2021, Tomasz Wasilczyk
* Copyright (c) 2023, Christopher Durand
*
* This file is part of the modm project.
*
Expand All @@ -12,7 +13,8 @@

#pragma once

#include <stdint.h>
#include <cstdint>
#include <chrono>
#include "../device.hpp"
#include <modm/architecture/interface/delay.hpp>

Expand Down Expand Up @@ -267,8 +269,14 @@ public:
inline static void
enableMainInternal(MainInternalFreq freq);

%% if target.family == "e7x/s7x/v7x"
template<uint32_t frequency>
static bool
enableMainExternalCrystal(std::chrono::microseconds startupTime);

inline static void
enableMainExternal(bool bypass);
enableMainExternalClock();
%% endif

inline static void
selectMainClockSource(MainClockSource src);
Expand Down
45 changes: 45 additions & 0 deletions src/modm/platform/clock/sam_pmc/clockgen_impl.hpp.in
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/*
* Copyright (c) 2021 Jeff McBride
* Copyright (c) 2021, Tomasz Wasilczyk
* Copyright (c) 2023, Christopher Durand
*
* This file is part of the modm project.
*
Expand All @@ -10,6 +11,8 @@
*/
// ----------------------------------------------------------------------------

#include <algorithm>

namespace modm::platform
{

Expand All @@ -28,6 +31,48 @@ ClockGen::enableMainInternal(MainInternalFreq freq)
while (!(PMC->PMC_SR & PMC_SR_MOSCRCS));
}

%% if target.family == "e7x/s7x/v7x"
template<uint32_t frequency>
bool
ClockGen::enableMainExternalCrystal(std::chrono::microseconds startupTime)
{
// disable external clock, clear startup time bits
PMC->CKGR_MOR = (PMC->CKGR_MOR | CKGR_MOR_KEY_PASSWD) &
~(CKGR_MOR_MOSCXTBY | CKGR_MOR_MOSCXTST_Msk);
// wait for external clock to shutdown
while (PMC->PMC_SR & PMC_SR_MOSCXTS);

// convert microseconds to startup time register value
// register unit is SCLK cycles (32768 Hz) multiplied by 8
const auto cycles = startupTime.count() * (32'768ll / 8) / 1'000'000;
chris-durand marked this conversation as resolved.
Show resolved Hide resolved
const uint32_t timeSetting = std::clamp(cycles, 0ll, 255ll) << CKGR_MOR_MOSCXTST_Pos;

PMC->CKGR_MOR |= CKGR_MOR_KEY_PASSWD | CKGR_MOR_MOSCXTEN | timeSetting;
while (!(PMC->PMC_SR & PMC_SR_MOSCXTS));

// measure crystal frequency
PMC->CKGR_MCFR = CKGR_MCFR_CCSS | CKGR_MCFR_RCMEAS;
// two subsequent reads of MAINFRDY must be 1
while (!(PMC->CKGR_MCFR & CKGR_MCFR_MAINFRDY));
while (!(PMC->CKGR_MCFR & CKGR_MCFR_MAINFRDY));
const auto resultFrequency = ((PMC->CKGR_MCFR & CKGR_MCFR_MAINF_Msk) >> CKGR_MCFR_MAINF_Pos)
* (32768u / 16u);

// default slow clock has big tolerance (20 ... 57 kHz), only coarse check is safe
return (resultFrequency > (frequency / 2)) && (resultFrequency < (frequency * 2));
}

void
ClockGen::enableMainExternalClock()
{
// disable external crystal
PMC->CKGR_MOR = (PMC->CKGR_MOR | CKGR_MOR_KEY_PASSWD) & ~CKGR_MOR_MOSCXTEN;

// enable external clock input
PMC->CKGR_MOR |= CKGR_MOR_KEY_PASSWD | CKGR_MOR_MOSCXTBY;
}
%% endif

void
ClockGen::selectMainClockSource(MainClockSource src)
{
Expand Down