-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcaravel_pll.py
185 lines (147 loc) · 7.03 KB
/
caravel_pll.py
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# SPDX-License-Identifier: Apache-2.0
# Copyright 2021 Konrad Beckmann
import argparse
import sys
import json
"""
This tool generates a PLL clock configuration for the Caravel management core.
The PLL multiplies clkin with the feedback divisor register.
clkin -> PLL -> no phase shift -> output divisor 1 -> clkout
\> phase shift 90 -> output divisor 2 -> clkout90
"""
VERBOSE = False
def vprint(*args, **kwargs):
if VERBOSE:
print(*args, **kwargs)
def eprint(*args, **kwargs):
print(*args, file=sys.stderr, **kwargs)
def generate_pll(clkin, clkout, pll_low_limit, pll_high_limit, multiplier=0, allow_deviation=False):
configs = []
ideal_configs = []
m_min = 1
m_max = 2**5
if multiplier != 0:
m_min = multiplier
m_max = multiplier + 1
# Iterate over the valid feedback divisor values
for m in range(m_min, m_max):
multiplied = clkin * m
if (multiplied < pll_low_limit or multiplied > pll_high_limit):
continue
for d in range(1, 2**3):
divided = multiplied / d
deviation = divided - clkout
configs.append({"m":m, "d":d, "deviation": deviation})
if divided == clkout:
ideal_configs.append({"m":m, "d":d})
vprint(f"Found a config without deviation: m={m}, d={d}")
# Find the config where the multiplied frequency is closest to the center of the pll limits
best_config = None
best_freq = 1e9
center_freq = pll_low_limit + (pll_high_limit - pll_low_limit) / 2
for c in ideal_configs:
freq = c["m"] * clkin
if abs(freq - center_freq) < abs(best_freq - center_freq):
vprint(f"Found a better config: m={c['m']}, d={c['d']}, pllfreq={freq}")
best_config = c
best_freq = freq
# If deviation is allowed, find the config with the lowest.
if best_config == None and allow_deviation:
best_deviation = 1e9
for c in configs:
freq = c["m"] * clkin / c["d"]
deviation = abs(freq - clkout)
if deviation < best_deviation:
vprint(f"Found a better deviating config: m={c['m']}, d={c['d']}, deviation={freq}")
best_config = c
best_deviation = deviation
return best_config
def generate_config(args):
clkin = args.clkin
clkout = args.clkout
clkout90 = clkout if not args.clkout90 else args.clkout90
pll_low_limit = args.pll_low_limit
pll_high_limit = args.pll_high_limit
allow_deviation = args.allow_deviation
clkout_conf = generate_pll(clkin, clkout, pll_low_limit, pll_high_limit, allow_deviation=allow_deviation)
if clkout_conf == None:
eprint("Failed to find a configuration for clkout")
exit(1)
clkout90_conf = generate_pll(clkin, clkout90, pll_low_limit, pll_high_limit, multiplier=clkout_conf["m"], allow_deviation=allow_deviation)
if clkout90_conf == None:
eprint("Failed to find a configuration for clkout90")
exit(1)
reg0x11 = (clkout_conf["d"] & 0b111) | ((clkout90_conf["d"] & 0b111) << 3)
reg0x12 = (clkout_conf["m"] & 0b11111)
if args.json:
print(json.dumps({
"clkin": clkin,
"clkout": args.clkin * clkout_conf["m"] / clkout_conf["d"],
"clkout90": args.clkin * clkout90_conf["m"] / clkout90_conf["d"],
"fbdiv": clkout_conf["m"],
"div1": clkout_conf["d"],
"div2": clkout90_conf["d"],
"reg0x11": reg0x11,
"reg0x12": reg0x12,
}))
else:
print(f"""PLL Parameters:
clkin: {args.clkin:#.2f} MHz
clkout: {(args.clkin * clkout_conf["m"] / clkout_conf["d"]):#.2f} MHz
clkout90: {(args.clkin * clkout90_conf["m"] / clkout90_conf["d"]):#.2f} MHz
PLL Feedback Divider: {clkout_conf["m"]}
PLL Output Divider 1: {clkout_conf["d"]}
PLL Output Divider 2: {clkout90_conf["d"]}
Register 0x11: {reg0x11:#04x}
Register 0x12: {reg0x12:#04x}""")
def list_configs(args):
clkin = args.clkin
pll_low_limit = args.pll_low_limit
pll_high_limit = args.pll_high_limit
configs = []
for m in range(1, 2**5):
multiplied = clkin * m
if (multiplied < pll_low_limit or multiplied > pll_high_limit):
continue
for d in range(1, 2**3):
freq = multiplied / d
configs.append({"m":m, "d":d, "freq": freq, "pllfreq": multiplied})
sorted_configs = sorted(configs, key=lambda c: c["freq"])
if args.json:
print(json.dumps(sorted_configs))
else:
print("clkout\t\tfbdiv\tdiv\tpllfreq")
for c in sorted_configs:
print(f"{(args.clkin * c['m'] / c['d']):8.3f} MHz\t{c['m']}\t{c['d']}\t{c['pllfreq']:5.1f} MHz")
def main():
parser = argparse.ArgumentParser(description='Generate PLL configurations for the Caravel management core. All frequencies are specified in MHz.')
subparsers = parser.add_subparsers(dest="action")
subparsers.required = True
parser.add_argument('--verbose', action='store_true', default=False, help='Enable verbose prints')
p_generate = subparsers.add_parser(
"generate",
help="generate configuration",
description="Generates a configuration for a desired output frequency")
p_generate.add_argument('--clkin', '-i', type=float, default=10, help='Frequency (MHz) of the input clock')
p_generate.add_argument('--clkout', '-o', type=float, required=True, help='Frequency (MHz) of the first output clock')
p_generate.add_argument('--clkout90', type=float, help='Frequency (MHz) of the second, 90 degrees phase shifted, output clock')
p_generate.add_argument('--allow-deviation', action='store_true', default=False, help='Allow deviation from the requested frequencies')
p_generate.add_argument('--pll-low-limit', type=float, default=90, help='Low limit of the allowed PLL output frequency')
p_generate.add_argument('--pll-high-limit', type=float, default=214, help='High limit of the allowed PLL output frequency')
p_generate.add_argument('--json', action='store_true', default=False, help='Output as JSON')
p_list = subparsers.add_parser(
"list",
help="list all valid configurations",
description="List all valid configurations for a given input frequency")
p_list.add_argument('--clkin', '-i', type=float, required=True, help='Frequency (MHz) of the input clock')
p_list.add_argument('--pll-low-limit', type=float, default=90, help='Low limit of the allowed PLL output frequency')
p_list.add_argument('--pll-high-limit', type=float, default=214, help='High limit of the allowed PLL output frequency')
p_list.add_argument('--json', action='store_true', default=False, help='Output as JSON')
args = parser.parse_args()
VERBOSE = args.verbose
if args.action == 'generate':
generate_config(args)
elif args.action == 'list':
list_configs(args)
if __name__ == "__main__":
main()