Voting resources, early voting, and poll worker information - VOTE. ... Adafruit is open and shipping.
0

Output Oscillator to GPIO on grand central
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Output Oscillator to GPIO on grand central

by talofer99 on Thu Mar 19, 2020 7:03 am

I'm trying to output the external 32K oscillator to a GPIO on the board, I manged to find few similar examples, but none seemed to work.
This is the last code that I tried and was expecting to see the clock on the PA17 GPIO.
I'm using Platform-IO for coding and compiling.
Any input would be great


Code: Select all | TOGGLE FULL SIZE
#include <Arduino.h>



#if defined(ARDUINO_SAMD_ZERO) && defined(SERIAL_PORT_USBVIRTUAL)

// Required for Serial on Zero based boards

#define Serial SERIAL_PORT_USBVIRTUAL

#endif





void setup() {

  Serial.begin(115200);

  while (!Serial);

  pinMode(36,OUTPUT);

  pinMode(5,OUTPUT);



 



  OSC32KCTRL->XOSC32K.bit.CGM = 1; // control gain mode: 1 = standard/XT 2 = high-speed/HS

  OSC32KCTRL->XOSC32K.bit.STARTUP = 0; // oscillator startup time, 0 = 62 ms

  OSC32KCTRL->XOSC32K.bit.ONDEMAND = 1; // always run

  OSC32KCTRL->XOSC32K.bit.XTALEN = 1; // enable crystal driver circuit for XIN32/XOUT32 pins

  OSC32KCTRL->XOSC32K.bit.EN32K = 1; // enable the 32 kHz output clock

  OSC32KCTRL->XOSC32K.bit.ENABLE = 1;

  while (!OSC32KCTRL->STATUS.bit.XOSC32KRDY); // wait until crystal oscillator is stable and ready to be used as a clock source







  GCLK->PCHCTRL[TC3_GCLK_ID].reg = GCLK_PCHCTRL_GEN_GCLK1_Val |

                                   (1 << GCLK_PCHCTRL_CHEN_Pos);

 



  GCLK->GENCTRL[3].bit.SRC = 0x5; 

  GCLK->GENCTRL[3].bit.DIV = 1;

  GCLK->GENCTRL[3].bit.GENEN = 1;

  GCLK->GENCTRL[3].bit.IDC = 1 ;

  GCLK->GENCTRL[3].bit.OOV = 1;

 



  while ( GCLK->SYNCBUSY.reg & GCLK_SYNCBUSY_GENCTRL3 ){

    /* Wait for synchronization */

  }



  PORT->Group[0].PINCFG[17].bit.PMUXEN = 1;

  PORT->Group[0].PMUX[8].bit.PMUXE = 0xc;

 

 



  Serial.println("setup done");



}



void loop(){ }

talofer99
 
Posts: 2
Joined: Wed Apr 25, 2018 11:38 am

Re: Output Oscillator to GPIO on grand central

by MartinL2 on Fri Mar 20, 2020 10:48 am

Hi talofer99,

The following code will output the 32.768kHz external crystal on port pin PA17 via GCLK3:

Code: Select all | TOGGLE FULL SIZE
// Output the 32.768kHz external crystal on port pin PA17 via GCLK3
void setup() {
  GCLK->GENCTRL[3].reg = GCLK_GENCTRL_OE |                    // Enable GCLK3 output
                         GCLK_GENCTRL_IDC |                   // Improve duty-cycle to 50%
                         GCLK_GENCTRL_GENEN |                 // Enable generic clock
                         GCLK_GENCTRL_SRC_XOSC32K;            // Set the clock source to the external 32.768kHz crystal                   
  while (GCLK->SYNCBUSY.bit.GENCTRL3);                        // Wait for synchronization
  PORT->Group[PORTA].PINCFG[17].bit.PMUXEN = 1;               // Switch port pin PA17's multiplexer
  PORT->Group[PORTA].PMUX[17 >> 1].reg |= PORT_PMUX_PMUXO(MUX_PA17M_GCLK_IO3);  // Switch the PA17's port multiplexer to GCLK IO 
}

void loop() {}

MartinL2
 
Posts: 58
Joined: Sun Mar 10, 2019 2:17 pm

Re: Output Oscillator to GPIO on grand central

by talofer99 on Fri Mar 20, 2020 12:09 pm

Thank you very much, worked like magic :)

talofer99
 
Posts: 2
Joined: Wed Apr 25, 2018 11:38 am

Please be positive and constructive with your questions and comments.