Skip to content

Commit e158702

Browse files
committed
atmel-samd/samd51: Use crystal for RTC
This uses the crystal to clock the RTC on boards which have a crystal. Disable clock generator 2 which was enabled in commit 8e20804 ("atmel-samd: Add rtc module support"). samd51 differs from samd21 when it comes to the RTC clock. samd51 doesn't have an explicit clock peripheral so no need for a clock generator. The same commit didn't even setup XOSC32K correctly, it missed EN1K and XTALEN. The RTC uses the 1k clock output, so enable it on the OSCULP32K even if it works without it.
1 parent ab7ddfd commit e158702

File tree

3 files changed

+13
-4
lines changed

3 files changed

+13
-4
lines changed

ports/atmel-samd/boards/feather_m4_express/mpconfigboard.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@
3232

3333
#include "external_flash/external_flash.h"
3434

35+
#define BOARD_HAS_CRYSTAL 1
36+
3537
#define DEFAULT_I2C_BUS_SCL (&pin_PA13)
3638
#define DEFAULT_I2C_BUS_SDA (&pin_PA12)
3739

ports/atmel-samd/boards/metro_m4_express/mpconfigboard.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@
3333

3434
#include "external_flash/external_flash.h"
3535

36+
#define BOARD_HAS_CRYSTAL 1
37+
3638
#define DEFAULT_I2C_BUS_SCL (&pin_PB03)
3739
#define DEFAULT_I2C_BUS_SDA (&pin_PB02)
3840

ports/atmel-samd/samd51_clocks.c

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,12 +81,14 @@ void disable_clock_generator(uint8_t gclk) {
8181

8282
static void init_clock_source_osculp32k(void) {
8383
// Calibration value is loaded at startup
84-
OSC32KCTRL->OSCULP32K.bit.EN1K = 0;
84+
OSC32KCTRL->OSCULP32K.bit.EN1K = 1;
8585
OSC32KCTRL->OSCULP32K.bit.EN32K = 0;
8686
}
8787

8888
static void init_clock_source_xosc32k(void) {
8989
OSC32KCTRL->XOSC32K.reg = OSC32KCTRL_XOSC32K_ONDEMAND |
90+
OSC32KCTRL_XOSC32K_EN1K |
91+
OSC32KCTRL_XOSC32K_XTALEN |
9092
OSC32KCTRL_XOSC32K_ENABLE |
9193
OSC32KCTRL_XOSC32K_CGM(1);
9294
}
@@ -105,15 +107,18 @@ void clock_init(void) {
105107
// DFLL48M is enabled by default
106108

107109
init_clock_source_osculp32k();
108-
init_clock_source_xosc32k();
109110

110-
OSC32KCTRL->RTCCTRL.bit.RTCSEL = OSC32KCTRL_RTCCTRL_RTCSEL_ULP1K_Val;
111+
if (board_has_crystal()) {
112+
init_clock_source_xosc32k();
113+
OSC32KCTRL->RTCCTRL.bit.RTCSEL = OSC32KCTRL_RTCCTRL_RTCSEL_XOSC1K_Val;
114+
} else {
115+
OSC32KCTRL->RTCCTRL.bit.RTCSEL = OSC32KCTRL_RTCCTRL_RTCSEL_ULP1K_Val;
116+
}
111117

112118
MCLK->CPUDIV.reg = MCLK_CPUDIV_DIV(1);
113119

114120
enable_clock_generator_sync(0, GCLK_GENCTRL_SRC_DPLL0_Val, 1, false);
115121
enable_clock_generator_sync(1, GCLK_GENCTRL_SRC_DFLL_Val, 1, false);
116-
enable_clock_generator_sync(2, GCLK_GENCTRL_SRC_OSCULP32K_Val, 32, false);
117122
enable_clock_generator_sync(4, GCLK_GENCTRL_SRC_DPLL0_Val, 1, false);
118123
enable_clock_generator_sync(5, GCLK_GENCTRL_SRC_DFLL_Val, 24, false);
119124

0 commit comments

Comments
 (0)