1#include "variant.h"
2
3void clockout(uint32_t gclk, int32_t divisor)
4{
5    GCLK_GENDIV_Type gendiv =
6    {
7        .bit.DIV = divisor,      // divider, linear or 2^(.DIV+1)
8        .bit.ID  = gclk,         // GCLK_GENERATOR_"gclk"
9    };
10    GCLK->GENDIV.reg = gendiv.reg;
11
12    // setup Clock Generator
13    GCLK_GENCTRL_Type genctrl =
14    {
15        .bit.RUNSTDBY = 0,        // Run in Standby
16        .bit.DIVSEL = 0,          // .DIV (above) Selection: 0=linear 1=powers of 2
17        .bit.OE = 1,              // Output Enable to observe on any port pin capable of outputting GCLK_IO["gclk"]
18        .bit.OOV = 0,             // Output Off Value
19        .bit.IDC = 1,             // Improve Duty Cycle
20        .bit.GENEN = 1,           // enable this GCLK
21        //.bit.SRC = GCLK_SOURCE_OSC8M,
22        .bit.SRC = GCLK_SOURCE_DFLL48M, // select GCLK source
23        .bit.ID = gclk,           // output GCLK_GENERATOR_"gclk" on GCLK_IO["gclk"]
24    };
25    GCLK->GENCTRL.reg = genctrl.reg;
26    while ( GCLK->STATUS.reg & GCLK_STATUS_SYNCBUSY )
27    {
28        /* Wait for synchronization */
29    }
30}
31