Skip to content

Commit d27681b

Browse files
committed
vendor.GowinPlatform: account for rouding error in frequency calculation.
1 parent 47851c2 commit d27681b

File tree

1 file changed

+5
-2
lines changed

1 file changed

+5
-2
lines changed

amaranth/vendor/_gowin.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,10 @@ def _osc_div(self):
164164
div_range = range(2, 130, 2)
165165
div_frac = Fraction(self._osc_base_freq, self.osc_frequency)
166166

167-
if div_frac.denominator != 1 or div_frac not in div_range:
167+
# Check that the requested frequency is within 50 ppm. This takes care of small mismatches
168+
# arising due to rounding. The tolerance of a typical crystal oscillator is 50 ppm.
169+
if (abs(round(div_frac) - div_frac) > Fraction(50, 1_000_000) or
170+
int(div_frac) not in div_range):
168171
achievable = (
169172
min((frac for frac in div_range if frac > div_frac), default=None),
170173
max((frac for frac in div_range if frac < div_frac), default=None)
@@ -178,7 +181,7 @@ def _osc_div(self):
178181
f"achievable frequencies are " +
179182
", ".join(str(self._osc_base_freq // frac) for frac in achievable if frac))
180183

181-
return div_frac.numerator
184+
return int(div_frac)
182185

183186
# Common templates
184187

0 commit comments

Comments
 (0)