forked from jhol/pyadf435x
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadf435xctl
executable file
·139 lines (121 loc) · 5.44 KB
/
adf435xctl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#!/usr/bin/env python
## SPDX-License-Identifier: GPL-3.0-or-later
##
## This file is part of the adf435x project.
##
## Copyright (C) 2017 Joel Holdsworth <joel@airwebreathe.org.uk>
## Copyright (C) 2022, 2023 Martin Homuth-Rosemann
##
## This program is free software; you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
## the Free Software Foundation; either version 3 of the License, or
## (at your option) any later version.
##
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with this program; if not, see <http://www.gnu.org/licenses/>.
##
from collections import OrderedDict
import adf435x.core
import adf435x.interfaces
import argparse
import inspect
import sys
import time
adf435x_version = f'adf435x version {adf435x.VERSION}'
parser = argparse.ArgumentParser(
description=f'Control program for ADF4350/1 eval board',
epilog=f'{adf435x_version}')
# Populate arguments
calculate_regs_spec = inspect.getfullargspec(adf435x.core.calculate_regs)
make_regs_spec = inspect.getfullargspec(adf435x.core.make_regs)
arg_dict = OrderedDict()
for arg, default in zip(calculate_regs_spec.args, calculate_regs_spec.defaults):
arg_dict[arg] = default
for arg, default in zip(make_regs_spec.args, make_regs_spec.defaults):
if arg not in arg_dict:
arg_dict[arg] = default
for arg in arg_dict.keys():
val=arg_dict[arg]
if arg == 'freq': # define '-f' shortcut for frequency
parser.add_argument('--freq', '-f', default=val, type=type(val), help=f'default = {val}')
else:
arg_string = '--' + arg.lower().replace('_', '-')
if type(val) == bool:
parser.add_argument( arg_string, action='store_true', help=f'default = {val}')
else:
parser.add_argument( arg_string, default=val, type=type(val), help=f'default = {val}')
for r in range(6):
parser.add_argument('--r%d' % r, default=None, type=str)
parser.add_argument('--interface', help='INTERFACE: FX2 (default), BusPirate, tinyADF, NONE', default='FX2')
parser.add_argument('--lock-detect', dest='lock_detect', action = 'store_true', help = 'query adf435x digital lock detect state')
parser.add_argument('-v', dest='verbose', action='count', default=0, help='increase verbosity')
parser.add_argument('-V', '--version', action = 'store_true', help = 'show adf435x version and exit')
init_group = parser.add_mutually_exclusive_group()
init_group.add_argument('--init-none', dest='init_none', action='store_true',
help='clear init settings and exit')
init_group.add_argument('--init-stand-alone', dest = 'init_stand_alone', action='store_true',
help='set current settings as init for no-USB mode and exit')
init_group.add_argument('--init-always', dest='init_always', action='store_true',
help='set current settings as init for power-on and exit')
# Parse
options = parser.parse_args()
if options.version:
print( adf435x_version )
sys.exit()
args = vars(options)
if options.interface == 'FX2': # do the communication with the ADF435x device
intf = getattr(adf435x.interfaces, args['interface'])() # which interface?
if options.init_none:
intf.set_startup( 0 ) # clear the power-on default
sys.exit()
if options.init_stand_alone:
intf.set_startup( 1 ) # store stand-alone power-on default
sys.exit()
if options.init_always:
intf.set_startup( 2 ) # store power-on default
sys.exit()
# Generate register values
calculate_regs_kw = {arg : args[arg.lower()]
for arg in calculate_regs_spec.args}
kw = {arg : args[arg.lower()] for arg in make_regs_spec.args}
kw['INT'], kw['MOD'], kw['FRAC'], kw['output_divider'], \
kw['band_select_clock_divider'] = adf435x.core.calculate_regs(
**calculate_regs_kw)
if options.lock_detect:
kw['mux_out'] = 6; # set MUXOUT to digital lock detect
regs = adf435x.core.make_regs(**kw)
for i in range(6):
r = args['r%d' % i]
if r != None:
regs[i] = int(r, 0)
# report register values
if options.verbose:
if options.verbose > 2:
for k in kw:
print(f'{k} = {kw[k]}')
if options.verbose > 1:
print( f'freq = {calculate_regs_kw["freq"]}' )
if options.verbose == 2:
print(f'INT = {kw["INT"]}')
if kw['FRAC']:
print(f'FRAC = {kw["FRAC"]}')
print(f'MOD = {kw["MOD"]}')
print(f'output_divider = {kw["output_divider"]}')
for r in range(5,-1, -1):
print('R%d = 0x%08x' % (r, regs[r]))
if options.interface != 'NONE': # do the communication with the ADF435x device
intf = getattr(adf435x.interfaces, args['interface'])() # which interface?
intf.set_regs(regs[::-1]) # write the 6 registers
if kw['mux_out']: # normally set to digital lock
time.sleep(.01) # wait 10 ms for PLL lock
status = intf.get_mux()[0] # get one byte, 0: MUXOUT=LOW or 1: MUXOUT=HIGH
if kw['mux_out'] == 6: # digital lock
print(f'{("NOLOCK", "LOCKED")[status]}')
else: # report the value (even if this state gives less info)
print(f'MUXOUT({kw["mux_out"]}) = {("LOW", "HIGH")[status]}')
sys.exit( not status ) # return value 0 = ok