Skip to content

Commit 4a5e88c

Browse files
nkpro2000srbessman
authored andcommitted
Update frequency_to_prescaler
1 parent 3beb0d9 commit 4a5e88c

File tree

1 file changed

+17
-8
lines changed

1 file changed

+17
-8
lines changed

pslab/bus/spi.py

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,7 @@
1919
0
2020
"""
2121

22-
import numpy as np
2322
import sys
24-
from itertools import product
2523
from typing import List, Tuple
2624

2725
import pslab.protocol as CP
@@ -102,14 +100,25 @@ def _clock_phase(cls) -> int:
102100

103101
@classmethod
104102
def _get_prescaler(cls, frequency: float) -> Tuple[int]:
105-
frequencys = []
106-
for p, s in product(cls._PPRE_MAP, cls._SPRE_MAP):
107-
frequencys.append(CP.CLOCK_RATE / (p * s))
103+
min_diff = CP.CLOCK_RATE # highest
104+
# minimum frequency
105+
ppre = 0
106+
spre = 0
108107

109-
frequencys = np.array(frequencys)
110-
idx = np.abs(frequencys - frequency).argmin()
108+
for p in range(len(cls._PPRE_MAP)):
109+
for s in range(len(cls._SPRE_MAP)):
111110

112-
return divmod(idx, len(cls._SPRE_MAP))
111+
freq = CP.CLOCK_RATE / (cls._PPRE_MAP[p] * cls._SPRE_MAP[s])
112+
if frequency >= freq:
113+
114+
diff = frequency - freq
115+
if min_diff > diff:
116+
# better match
117+
min_diff = diff
118+
ppre = p
119+
spre = s
120+
121+
return ppre, spre
113122

114123
@staticmethod
115124
def _save_config(

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy