Skip to content

Commit a99092b

Browse files
committed
Commonize and improve clock frequency conversion.
- Moved convert_frequency() to pyocd.utility.cmdline. - Added support for float values and 'Hz' suffix. - Using convert_frequency() for 'set clock' in commander.
1 parent f10e045 commit a99092b

File tree

3 files changed

+30
-18
lines changed

3 files changed

+30
-18
lines changed

pyocd/__main__.py

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
convert_vector_catch,
4242
convert_session_options,
4343
convert_reset_type,
44+
convert_frequency,
4445
)
4546
from .probe.pydapaccess import DAPAccess
4647
from .tools.lists import ListGenerator
@@ -82,20 +83,6 @@
8283
'sector',
8384
]
8485

85-
def convert_frequency(value):
86-
"""! @brief Applies scale suffix to frequency value string."""
87-
value = value.strip()
88-
suffix = value[-1].lower()
89-
if suffix in ('k', 'm'):
90-
value = int(value[:-1])
91-
if suffix == 'k':
92-
value *= 1000
93-
elif suffix == 'm':
94-
value *= 1000000
95-
return value
96-
else:
97-
return int(value)
98-
9986
def flatten_args(args):
10087
"""! @brief Converts a list of lists to a single list."""
10188
return [item for sublist in args for item in sublist]
@@ -164,7 +151,8 @@ def build_parser(self):
164151
connectOptions.add_argument("-t", "--target", dest="target_override", metavar="TARGET",
165152
help="Set the target type.")
166153
connectOptions.add_argument("-f", "--frequency", dest="frequency", default=None, type=convert_frequency,
167-
help="SWD/JTAG clock frequency in Hz, with optional k/K or m/M suffix for kHz or MHz.")
154+
help="SWD/JTAG clock frequency in Hz. Accepts a float or int with optional case-"
155+
"insensitive K/M suffix and optional Hz. Examples: \"1000\", \"2.5khz\", \"10m\".")
168156
connectOptions.add_argument("-W", "--no-wait", action="store_true",
169157
help="Do not wait for a probe to be connected if none are available.")
170158
connectOptions.add_argument("-M", "--connect", dest="connect_mode", metavar="MODE",

pyocd/tools/pyocd.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848
from ..flash.file_programmer import FileProgrammer
4949
from ..gdbserver.gdbserver import GDBServer
5050
from ..utility import (mask, conversion)
51-
from ..utility.cmdline import convert_session_options
51+
from ..utility.cmdline import (convert_session_options, convert_frequency)
5252
from ..utility.hex import (format_hex_width, dump_hex_data)
5353
from ..utility.progress import print_progress
5454
from ..utility.compatibility import get_terminal_size
@@ -453,7 +453,10 @@
453453
},
454454
'clock' : {
455455
'aliases' : [],
456-
'help' : "Set SWD or JTAG clock frequency in kilohertz."
456+
'help' : "Set SWD or JTAG clock frequency in Hertz. A case-insensitive metric scale "
457+
"suffix of either 'k' or 'm' is allowed, as well as a trailing \"Hz\". There must "
458+
"be no space between the frequency and the suffix. For example, \"2.5MHz\" sets "
459+
"the clock to 2.5 MHz."
457460
},
458461
'option' : {
459462
'aliases' : [],
@@ -1492,7 +1495,7 @@ def handle_set_clock(self, args):
14921495
print("Error: no clock frequency provided")
14931496
return 1
14941497
try:
1495-
freq_Hz = self.convert_value(args[0]) * 1000
1498+
freq_Hz = convert_frequency(args[0])
14961499
except:
14971500
print("Error: invalid frequency")
14981501
return 1

pyocd/utility/cmdline.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,3 +161,24 @@ def convert_reset_type(value):
161161
raise ValueError("unexpected value for reset_type option ('%s')" % value)
162162
return RESET_TYPE_MAP[value]
163163

164+
def convert_frequency(value):
165+
"""! @brief Applies scale suffix to frequency value string.
166+
@param value String with a float and possible 'k' or 'm' suffix (case-insensitive). "Hz" may
167+
also follow. No space is allowed between the float and suffix. Leading and trailing
168+
whitespace is allowed.
169+
@return Integer scaled according to optional metric suffix.
170+
"""
171+
value = value.strip().lower()
172+
if value.endswith("hz"):
173+
value = value[:-2]
174+
suffix = value[-1]
175+
if suffix in ('k', 'm'):
176+
value = float(value[:-1])
177+
if suffix == 'k':
178+
value *= 1000
179+
elif suffix == 'm':
180+
value *= 1000000
181+
return int(value)
182+
else:
183+
return int(float(value))
184+

0 commit comments

Comments
 (0)