Skip to content

Commit

Permalink
[TG-239] Add script to estimate closest divider ratios targeting a ce…
Browse files Browse the repository at this point in the history
…ntral frequency (tomojitakasu#2)
  • Loading branch information
oriol-albora authored Aug 26, 2022
1 parent af78526 commit 4e4e91d
Showing 1 changed file with 63 additions and 0 deletions.
63 changes: 63 additions & 0 deletions python/max2772_pll_integer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import sys

# Compute the best divider values for a given target frequency
Ftarget = 1573.42e6 # DESIRED TARGET local frequency

### CONSTANTS
Ftcxo = 16.368e6 # MAX2771 EVK is preconfigured to 16.368MHz TCXO signal

# R-divider range
Rmin = 1
Rmax = 1023

# N-divider range
Nmin = 36
Nmax = 32767

# F-divider range
Fmin = 0
Fmax = 1048575

Fint = 0
Ncloser = 0
Rcloser = 0
for R in range(Rmin, Rmax):
for N in range(Nmin, Nmax):
Freq = Ftcxo / R * N
if abs(Freq-Ftarget) < abs(Fint-Ftarget):
Fint = Freq
Ncloser = N
Rcloser = R
if (Ftarget-Freq) > 0:
Ffrac = Freq
Nfrac = N
Rfrac = R
if Fint == Ftarget:
break
if Fint == Ftarget:
break

# Integer mode closer results
print(f'Target frequency: {Ftarget/1e6} MHz')
print(f' Integer mode results:')
print(f' Closer frequency: {Fint/1e6:.6f} MHz (offset: {(Fint-Ftarget):.6f}) Hz')
print(f' N: {Ncloser}')
print(f' R: {Rcloser}')
if Fint == Ftarget:
sys.exit(0)

# Fractional mode final adjustment, if needed
Fcloser = 0
for F in range(Fmin, Fmax):
Freq = Ftcxo / Rfrac * ( Nfrac + F / 2**20 )
if abs(Freq-Ftarget) < abs(Ffrac-Ftarget):
Ffrac = Freq
Fcloser = F
if Ffrac == Ftarget:
break

print(f' Fractional mode results:')
print(f' Closer frequency: {Ffrac/1e6:.6f} MHz (offset: {(Ffrac-Ftarget):.6f}) Hz')
print(f' N: {Nfrac}')
print(f' R: {Rfrac}')
print(f' F: {Fcloser}')

0 comments on commit 4e4e91d

Please sign in to comment.