# Puzzled G400 main clock frequency

One of my board had low voltage issue and it seems caused G400 running extra slow. The low voltage issue is resolved but I want to add check in firmware should this happen in future we can display it as an error.

I read the Atmel-11055F-ATARM-SAM9X35-Datasheet_31-Aug-15, on page 195 it lists the register CKGR_MCFR should contain main clock cycle count so I can easily calculate it with the formula:

fMAINCK = (MAINF * fSLCK) / 16 (Where frequency is in MHz)

This is my code to get the register value:
var CKGR_MCFR = 0xFFFFFC24;
var reg = new Register(CKGR_MCFR);
var va = reg.Value;
var reg1 = new Register(CKGR_MCFR);
var va2 = reg1.Value;

Both va & va2 are 0x118FC. The bit 16 is 1 which indicate MAINF is reliable. The MAINF = 0x18FC = 6396.

6396 * 0.032768 / 16 = 13.099008MHz

The result is far off from the G400 actual running frequency which should be 400MHz. I also checked the crystal on G400 board is 12.000MHz so the 13.09MHz couldn’t be referring to the crystal frequency.

I am little puzzled how is the main clock frequency is calculated.

That may be an assumption. I believe that CKGR_MCFR is the crystal clock frequency.

The crystal clock is multiplied to get to 400MHZ, with PLLs I believe.

But what do I know… I am a software guy.

Just figured out. My fMAINCK is actually 12MHz. The fMCK speed is 396.04MHz.

Here is the full code that calculate this:

``````		var CKGR_MCFR = new Register(0xFFFFFC24);
var fMAINCK = (CKGR_MCFR.Value & 0xffff) * 0.032768 / 16;

Debug.Print("fMAINCK:" + fMAINCK + "MHz");

var PMC_MCKR = new Register(0xFFFFFC30);
var PLLADIV2 = PMC_MCKR.Value & 0x1000;

var CKGR_PLLAR = new Register(0xFFFFFC28);
var MULA = CKGR_PLLAR.Value & 0xff0000;
MULA = MULA >> 16;
var DIVA = CKGR_PLLAR.Value & 0xff;
var fMCK = fMAINCK * ((MULA + 1) / DIVA) / (PLLADIV2 == 1 ? 2 : 1);

Debug.Print("fMCK:" + fMCK + "MHz");
``````

Output:

fMAINCK:12.00128MHz
fMCK:396.04223999999999MHz

Still one strange thing is sometimes I got fMAINCK = 12MHz and some other times 13MHz. If the fMAINCK is 13MHz this will results fMCK = 433MHz, 33MHz higher than the standard.

On the 13MHz times I checked the register it is indeed the external crystal instead of the internal RC is providing the main clock.

crystal is temperature sensitive. If you breath on it the frequency will increase.

Yeah, that’s true. But I thought 1MHz swing for a 12MHz crystal is bit of extreme.

<10% might be reasonable?

It looks like the G400 module uses SJK 12.000 crystal. According to the datasheet http://biakom.com/pdf/SJK_Catalog.pdf the H6 series should have at most 50ppm frequency error. For a 12MHz crystal it means 12 * 0.005 = 0.06MHz of frequency error should be in the allowance range.

It’s kind of weird mine says 13MHz sometimes.

I think the chip determines the frequency by sampling. Random noise could cause sampling errors?

You should not see 13mhz. When you do, it is an error elsewhere, not there crystal.