How to set clock pins on SAMD21 in the variants.h file?

Hi,
I'm trying to configure an external crystal on pins PA14+PA15 (GLCK_IO[0] and GCLCK_IO[1]) on an ATSAMD21G18A. This is inherited hardware (30+ boards) that I am trying to adapt the Adafruit feather M0 definition for, however, the board has the external oscillator on different pins to their board (theirs is PA00,PA01).

In variants.h I see the definitions set the clock frequency:

/** Frequency of the board main oscillator */
#define VARIANT_MAINOSC		(32768ul)
/** Master clock frequency */
#define VARIANT_MCK	(F_CPU)

but in other board definitions such as an Adafruit feather M4 I see more extensive definitions:

/** Frequency of the board main oscillator */
#define VARIANT_MAINOSC		(32768ul)

/** Master clock frequency */
#define VARIANT_MCK        (F_CPU)

#define VARIANT_GCLK0_FREQ (F_CPU)
#define VARIANT_GCLK1_FREQ (48000000UL)
#define VARIANT_GCLK2_FREQ (100000000UL)

How do I just set the GLCK_IO[0] and GCLCK_IO[1] to have a crystal on them? There's not much documentation easily accessible for this?!

Thanks, Dan

Have you had a look at the datasheet?

There is an extensive 700+ pages about the ATSAMDxx series - in particular, the ATSAMD21 can be configured for a wide range of frequencies and dividers (up to 96Mhz). The crystal is generally used for the built-in RTC, while adafruit uses the internal clock for the master clock frequency iirc.

No crystal is actually required if you're just trying to use the MCU for a custom project - what specifically do you need the crystal for?

Yes, had a hunt through the datasheets/manual and got a little lost. long story short... I have 30-40 pcbs manufacturer prototypes that I'm reverse engineering and they have crystals... I've got it working without, but wanted to use the onboard crystal as designed (maybe for ebetter accuracy?)

7.2.1 Oscillator Pinout
The oscillators are not mapped to the normal port functions and their multiplexing are controlled by registers in the System Controller (SYSCTRL).
© 2020 Microchip Technology Inc.
Complete Datasheet DS40001882F-page 33

I don’t know offhand whether there are configuration variables you can set to enable this, or whether you’ll have to add actual code. Pa0/pa1 is the 32kHz crystal OWC, pa14/pa15 is for the high speed crystal Osc.

Hi @illuminateddan

If you intend to configure an Arduino compatible SAMD21 then you'll need to take the crystal configuration in the bootloader into account as well.

The easiest way if you require the accuracy of an external high speed crystal, is to use a crystal-less bootloader, then as has been previously mention employ the SYSCTRL module to configure the clock source at the start of your sketch. Once the new crystal clock source is operational, it can be switched it over to generic clock 0 (GCLK0) that supplies the CPU's master clock (MCLK).

I haven't used this crystal option myself, however switching over to GCLK0 simply involves specifying the new clock source. This example code switches the external 32.768kHz crystal over to GCLK0, replacing the 48MHz digital frequency locked loop. (Note uploading this code requires placing the board in bootloader mode in order to recover, since the switch to the slow clock disconnects the USB port):

GCLK->GENCTRL.reg = //GCLK_GENCTRL_OE |            // Test: enable GCLK output (on a selected pin)
                    GCLK_GENCTRL_IDC |           // Set the duty cycle to 50/50 HIGH/LOW
                    GCLK_GENCTRL_GENEN |         // Enable GCLK0
                    GCLK_GENCTRL_SRC_XOSC32K |   // Set the external 32.768kHz clock source (XOSC32K)
                    GCLK_GENCTRL_ID(0);          // Select GCLK0
while (GCLK->STATUS.bit.SYNCBUSY);               // Wait for synchronization

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.