External Clock Signal prevents MKR from booting

So I’m not entirely sure where I’m going wrong here. I’ve been experimenting with clocking & time the last few days. The ultimate aim of my project is to sync a DS3231 RTC using GPS to UTC with sub-millisecond accuracy. I thought it might be nice to have a super accurate clock lying around. (whether it actually is accurate in the end is a different story but I thought it would be a nice learning project)

MY PROBLEM

I am using the 32kHz output from the DS3231 as an input to the MKR board on Pin 6 (GCLK_IO4). This is then configured as the Clock Source to the internal RTC of the SAMD21. This works fine and gives me a nice stream of ever increasing seconds out the USBSerial if I plug in the wire to the pin AFTER I reset the board and my sketch is at the 3rd line of setup with a while(!Serial);

If I attempt to reset the board while this clock line is still plugged in the board refuses to start up until I unplug the line, remove the power (via usb) and restart it, after which it boots straight into the bootloader download mode.

Does anyone with more knowledge than me, know why I might be running into boot issues when Ive got an external clock signal attached?

I am using modified versions of the Adafruit RTClib and the Arduino RTCZero libraries which I’ve attached below. The main edits are in the configureClock() function of the Arduino library and right at the bottom of the DS3231 library to enable the 32kHz output

Many Thanks

Sketch

#include "wiring_private.h"
#include <Arduino_PMIC.h>
#include <RTCZero.h>
#include <RTClib.h>

#define EXTERNAL_CLOCK
#define RTC_OSC_32K

#define RTC_32K (06ul)
#define RTC_PPS (07ul)

RTCZero rtcInternal;

RTC_DS3231 rtcExternal;

void gpioSetup()
{
    pinMode(LED_BUILTIN, OUTPUT);
    pinMode(RTC_PPS, INPUT_PULLUP);
    pinPeripheral(RTC_32K, PIO_AC_CLK);
}

void clockSync(void)
{
    if(true)                                                                        // Check for GPS fix - LATER
    {
        rtcExternal.writeSqwPinMode(DS3231_SquareWave1Hz);
        //attachInterrupt(digitalPinToInterrupt(RTC_PPS), rtcPPS, FALLING);
        rtcExternal.writeSqwPinMode(DS3231_OFF);
    }
}

void setup(void)
{
    gpioSetup();
    Serial.begin(9600);
    while(!Serial);
    if(!rtcExternal.begin())
    {
        errorHandler(true, F("Error: No External RTC"));
    }
    rtcExternal.enable32kOutput();
    rtcExternal.writeSqwPinMode(DS3231_OFF);
    
    if(!rtcExternal.status32kOutput())
    {
        errorHandler(true, F("Error: No 32K Output"));
    }
    rtcInternal.begin();
    DateTime now = rtcExternal.now();
    rtcInternal.setTime(now.hour(), now.minute(), now.second());
    rtcInternal.setDate(now.day(), now.month(), now.year());

    if (!PMIC.begin())
    {
        Serial.println("Failed to initialize PMIC!");
        while (1);
    }
    rtcInternal.
    
}

void loop(void)
{
    PMIC.isPowerGood();

    // Print date...
    print2digits(rtcInternal.getDay());
    Serial.print("/");
    print2digits(rtcInternal.getMonth());
    Serial.print("/");
    print2digits(rtcInternal.getYear());
    Serial.print(" ");

    // ...and time
    print2digits(rtcInternal.getHours());
    Serial.print(":");
    print2digits(rtcInternal.getMinutes());
    Serial.print(":");
    print2digits(rtcInternal.getSeconds());

    Serial.println();

    delay(1000);
}

void print2digits(int number) {
    if (number < 10) {
        Serial.print("0"); // print a 0 before if the number is < than 10
    }
    Serial.print(number);
}

/* Error Handler -------------------------------------------------------------*/
void errorHandler(bool reset, const __FlashStringHelper *error){
    Serial.println(error);
    if(reset){
        while (1) {
            digitalWrite(LED_BUILTIN, LOW);
            delay(100);
            digitalWrite(LED_BUILTIN, HIGH);
            delay(100);
        }
    } else {
        for(int i = 0; i < 10; i ++){
            digitalWrite(LED_BUILTIN, LOW);
            delay(100);
            digitalWrite(LED_BUILTIN, HIGH);
            delay(100);
        }
        digitalWrite(LED_BUILTIN, LOW);
    }
}

RTCZero.cpp (11.8 KB)

RTCZero.h (3.29 KB)

RTClib.cpp (53.5 KB)

RTClib.h (15.5 KB)

Seems like people are working on a similar problem here. Will add on to this thread instead!