8000 atmel-samd/samd21: Use XOSC32K on boards with a crystal · godlygeek/circuitpython@2893e79 · GitHub
[go: up one dir, main page]

Skip to content

Commit 2893e79

Browse files
committed
atmel-samd/samd21: Use XOSC32K on boards with a crystal
Use XOSC32K on boards that have BOARD_HAS_CRYSTAL defined and set to 1.
1 parent 4adba51 commit 2893e79

File tree

4 files changed

+28
-2
lines changed

4 files changed

+28
-2
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,3 +46,5 @@
4646
GD25Q16C
4747

4848
#include "external_flash/external_flash.h"
49+
50+
#define BOARD_HAS_CRYSTAL 1

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,3 +47,5 @@
4747
GD25Q16C
4848

4949
#include "external_flash/external_flash.h"
50+
51+
#define BOARD_HAS_CRYSTAL 1

ports/atmel-samd/clocks.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#include <stdint.h>
3232

3333
#include "include/sam.h"
34+
#include "mpconfigboard.h" // for BOARD_HAS_CRYSTAL
3435

3536
#ifdef SAMD51
3637
#define CLOCK_48MHZ GCLK_GENCTRL_SRC_DFLL_Val
@@ -53,6 +54,14 @@ void disconnect_gclk_from_peripheral(uint8_t gclk, uint8_t peripheral);
5354
void enable_clock_generator(uint8_t gclk, uint32_t source, uint16_t divisor);
5455
void disable_clock_generator(uint8_t gclk);
5556

57+
static inline bool board_has_crystal(void) {
58+
#ifdef BOARD_HAS_CRYSTAL
59+
return BOARD_HAS_CRYSTAL == 1;
60+
#else
61+
return false;
62+
#endif
63+
}
64+
5665
void clock_init(void);
5766

5867
#endif // MICROPY_INCLUDED_ATMEL_SAMD_CLOCKS_H

ports/atmel-samd/samd21_clocks.c

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,13 @@ static void init_clock_source_osc32k(void) {
9494
while (!SYSCTRL->PCLKSR.bit.OSC32KRDY) {}
9595
}
9696

97+
static void init_clock_source_xosc32k(void) {
98+
SYSCTRL->XOSC32K.reg = SYSCTRL_XOSC32K_EN32K |
99+
SYSCTRL_XOSC32K_XTALEN |
100+
SYSCTRL_XOSC32K_ENABLE;
101+
while (!SYSCTRL->PCLKSR.bit.XOSC32KRDY) {}
102+
}
103+
97104
static void init_clock_source_dfll48m(void) {
98105
SYSCTRL->DFLLCTRL.reg = SYSCTRL_DFLLCTRL_ENABLE;
99106
while (!SYSCTRL->PCLKSR.bit.DFLLRDY) {}
@@ -116,9 +123,15 @@ static void init_clock_source_dfll48m(void) {
116123
void clock_init(void)
117124
{
118125
init_clock_source_osc8m();
119-
init_clock_source_osc32k();
126+
if (board_has_crystal())
127+
init_clock_source_xosc32k();
128+
else
129+
init_clock_source_osc32k();
120130
enable_clock_generator(0, GCLK_GENCTRL_SRC_DFLL48M_Val, 1);
121131
enable_clock_generator(1, GCLK_GENCTRL_SRC_DFLL48M_Val, 150);
122132
init_clock_source_dfll48m();
123-
enable_clock_generator(2, GCLK_GENCTRL_SRC_OSC32K_Val, 32);
133+
if (board_has_crystal())
134+
enable_clock_generator(2, GCLK_GENCTRL_SRC_XOSC32K_Val, 32);
135+
else
136+
enable_clock_generator(2, GCLK_GENCTRL_SRC_OSC32K_Val, 32);
124137
}

0 commit comments

Comments
 (0)
0