-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTimerConfigurer.py
145 lines (123 loc) · 5.24 KB
/
TimerConfigurer.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
import sys
import json
# TODO : Add comments
DEFAULT_EXAMPLE_FILE = "example.json"
DEFAULT_OUT_FILE = "result.txt"
class Range:
def __init__(self, a, b):
assert(a<b)
self.a = a
self.b = b
def get(self):
return [self.a, self.b]
def get_max(self):
return self.b
def get_min(self):
return self.a
def contains(self, value):
return (self.a <= value and value <= self.b)
class Timer :
def __init__(self, clk_freq, prescaler_range, compare_range, wanted_freq):
assert(clk_freq>wanted_freq)
assert(prescaler_range.get_min() > 0)
assert(compare_range.get_min() > 0)
self.clk_freq = clk_freq
self.prescaler_range = prescaler_range
self.compare_range = compare_range
self.wanted_freq = wanted_freq
self.result_error = -1
self.result_freq = 0
self.result_compare = 0
self.result_prescaler = 0
def __str__(self):
out = "Reference clock = {0} Hz\n".format(self.clk_freq)
out += "Prescaler range is {0} to {1}\n".format(self.prescaler_range.get_min(),\
self.prescaler_range.get_max())
out += "Compare register range is {0} to {1}\n".format(self.compare_range.get_min(),\
self.compare_range.get_max())
out += "Target frequency is {0} Hz\n\n".format(self.wanted_freq)
if(self.result_error == -1):
out += "No solution found with the current values!\n"
else :
out += "The best solution found is :\n"
out += "error = {0} Hz\n".format(self.result_error)
out += "out frequency = {0} Hz\n".format(self.result_freq)
out += "compare register = {0}\n".format(self.result_compare)
out += "prescaler = {0}\n".format(self.result_prescaler)
return out
def SaveToFile(self, file_name):
f = open(file_name, "w")
f.write(self.__str__())
f.close
def computeBestFreq(self):
min_prescaler = int(self.wanted_freq * self.compare_range.get_max() / self.clk_freq)
if(min_prescaler<self.prescaler_range.get_min()):
print("ERROR: the prescaler range is not enough to meet the requirements")
return
current_prescaler = min_prescaler
while( current_prescaler <= self.prescaler_range.get_max()):
compare_value = max(int(round(self.clk_freq / (current_prescaler * self.wanted_freq))), 1)
result_freq = self.clk_freq/(current_prescaler*compare_value)
error = abs(result_freq - self.wanted_freq)
if(self.result_error == -1 or error < self.result_error):
self.result_error = error
self.result_freq = result_freq
self.result_compare = compare_value
self.result_prescaler = current_prescaler
if( error == 0):
break
current_prescaler = current_prescaler * 2
def PrintHelp():
help_string = "TimerConfigurer is a script used to find the best couple \n"
help_string+= "compare value/prescaler value of a timer.\n"
help_string+= "This script was implemented and tested with Python 2.7.\n"
help_string+= "Usage : TimerConfigurer <command> [<args>]\n"
help_string+= " --help : print help\n"
help_string+= " -c : create an example json file named example.json\n"
help_string+= " -i file_name : file_name is a json file containing the problem constraints\n\n"
help_string+= "Example : TimerConfigurer.py -i test.json -o result.txt\n"
help_string+= " This will read the file test.json and print the result in result.txt\n"
print(help_string)
def PrintDefaultFile(file_name):
file_str = """
{
"clock": 48000000,
"prescaler_range": [1, 128],
"compare_range": [1, 65535],
"out_freq": 14250
}
"""
f = open(file_name, "w")
f.write(file_str)
f.close()
if __name__ == "__main__":
print("Starting TimerConfigurer...")
if(len(sys.argv) < 2):
PrintHelp()
else :
in_file = ""
i = 1
while(i<len(sys.argv)):
if(sys.argv[i] == "--help"):
PrintHelp()
elif(sys.argv[i] == "-c"):
PrintDefaultFile(DEFAULT_EXAMPLE_FILE)
elif(sys.argv[i] == "-i"):
i += 1
in_file = sys.argv[i]
with open(in_file) as data_file:
data = json.load(data_file)
print(data)
assert(data["clock"])
assert(data["prescaler_range"])
assert(data["compare_range"])
assert(data["out_freq"])
prescaler_range = Range(data["prescaler_range"][0], data["prescaler_range"][1])
compare_range = Range(data["compare_range"][0], data["compare_range"][1])
timer = Timer(data["clock"], prescaler_range, compare_range, data["out_freq"])
timer.computeBestFreq()
timer.SaveToFile(DEFAULT_OUT_FILE)
print(timer)
else:
print("Command : {0} not valid!\n".format(sys.argv[i]))
i +=1