Is it possible to automatically detect 6Mhz crystal as well as 5/10?

Peter JakackiPeter Jakacki Posts: 6,487
edited July 30 in Propeller 1 Vote Up0Vote Down
Many years ago our famous Phil Pilgrim wrote a very tricky and useful routine to detect whether a Prop was running a 5Mhz or 10Mhz crystal. Well, it couldn't really tell if it were 5Mhz exactly but it could tell the whether it was in that range. rather than double.

Now perhaps this question is more for Phil but I throw it open for anyone to be able to comment but is it possible to detect 5/6/10 Mhz crystals automatically? I think the difference between 5 and 6 and the variations in silicon would preclude being able to detect this just as it would be almost impossible to detect 6.5Mhz too so I will say that this is IMPOSSIBLE! :)

Any takers?

I may however use baud-rate detection from ANSI terminal responses to reset and lock in the exact crystal frequency as a workaround and make sure all code references this for timing.

Phil's code that I use in Tachyon:
?(c) Copyright 2009 Philip C. Pilgrim (
see end of file for terms of use.

This object determines whether a 5MHz or 10MHz crystal is
being used and sets the PLL accordingly for 80MHz operation.
The main program should use the following settings:
  _clkmode      = xtal1 + pll8x
  _xinfreq      = 10_000_000
              org       _RUNMOD

_setpll       movi      ctra,#%0_00001_011      'Set ctra for pll on no pin at x1.
              movi      frqa,#%0100_0000_0      'Set frqa for clk / 4 (= 20MHz w/ 10MHz crystal, i.e. too high).
              add       pllx,cnt                'Give PLL time to lock (if it can) and stabilize.
              waitcnt   pllx,#0
              movi      vcfg,#$40               'Configure video for discrete pins, but no pins enabled.
              mov       vscl,#$10               'Set for 16 clocks per frame.
              waitvid   0,0                     'Wait once to clear time.
              neg       pllx,cnt                'Read -cnt.
              waitvid   0,0                     'Wait 16 pll clocks = 64 processor clks.
              add       pllx,cnt                'Compute elapsed time.
              cmp       pllx,#$40 wz            'Is it really 64 clocks?
        if_z  mov       pllx,#$6f               '  Yes: Crystal is 5MHz. Set clock mode to XTAL1 and PLL16X.
        if_z  clkset    pllx
        if_z  wrbyte    $-2, #4                 'update clkmode location
            jmp       unext

pllx          long      $1_0000                 '65536 clks for pll to stabilize.
Tachyon Forth - compact, fast, forthwright and interactive
Brisbane, Australia


Sign In or Register to comment.