-
-
Notifications
You must be signed in to change notification settings - Fork 45
/
Copy pathfactory_setup.py
214 lines (168 loc) · 5.3 KB
/
factory_setup.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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
import argparse
import pathlib
import time
import rtmidi
import rtmidi.midiutil
from hubble import Hubble
from wintertools import fw_fetch, jlink, reportcard, thermalprinter, fs
from wintertools.print import print
from libgemini import (
adc_calibration,
cv_calibration,
gemini,
ramp_calibration,
clock_calibration,
)
DEVICE_NAME = "winterbloom_gemini"
JLINK_DEVICE = "ATSAMD21G18"
JLINK_SCRIPT = "scripts/flash.jlink"
REPORT = reportcard.Report(name="Castor & Pollux")
def erase_nvm():
print("# Erasing NVM")
gem = gemini.Gemini.get()
gem.erase_lut()
print("✓ Erased ramp look-up-table")
gem.reset_settings()
print("✓ Erased user settings")
gem.soft_reset()
print("Reset")
def get_firmware_and_serial():
print("# Firmware & serial")
gem = gemini.Gemini.get()
fw_version = gem.get_firmware_version()
serial = gem.get_serial_number()
print(f"Firmware version: {fw_version}")
print(f"Serial number: {serial}")
REPORT.ulid = serial
REPORT.sections.append(
reportcard.Section(
name="Firmware",
items=[
reportcard.LabelValueItem(
label="Version", value=fw_version, class_="stack"
),
reportcard.LabelValueItem(
label="Serial number", value=serial, class_="stack"
),
],
)
)
def has_midi_in_port(port_name):
midiin = rtmidi.MidiIn(rtmidi.midiutil.get_api_from_environment())
try:
ports = midiin.get_ports()
for port in ports:
if port_name in port:
return True
return False
finally:
midiin.delete()
def get_geminiboot():
try:
return pathlib.Path(fs.find_drive_by_name("GEMINIBOOT"))
except RuntimeError:
return None
def upload_new_bootloader(path):
print("Copying bootloader...")
try:
fs.copyfile(".cache/update-bootloader.winterbloom_gemini.uf2", path / "firmware.uf2")
except FileNotFoundError:
pass
def upload_new_firmware(path):
print("Copying firmware...")
try:
fs.copyfile("../firmware/build/gemini-firmware.uf2", path / "firmware.uf2")
except FileNotFoundError:
pass
def flash_new_firmware():
print("Flashing bootloader and firmware...")
fw_fetch.latest_bootloader(DEVICE_NAME)
jlink.run(JLINK_DEVICE, JLINK_SCRIPT)
def setup_firmware_and_bootloader():
geminiboot = get_geminiboot()
has_midi = has_midi_in_port(gemini.Gemini.MIDI_PORT_NAME)
# If already connected in the bootloader, copy new bootloader and then
# copy new firmware.
if geminiboot:
print("GEMINIBOOT found, updating bootloader and firmware...")
upload_new_bootloader(geminiboot)
fs.wait_for_drive("GEMINIBOOT")
upload_new_firmware(geminiboot)
elif has_midi:
print("Gemini MIDI port found, booting into bootloader and updating bootloader and firmware...")
gem = gemini.Gemini.get()
gem.reset_into_bootloader()
gem.close()
gemini.Gemini._instance = None
geminiboot = pathlib.Path(fs.wait_for_drive("GEMINIBOOT"))
upload_new_bootloader(geminiboot)
fs.wait_for_drive("GEMINIBOOT")
upload_new_firmware(geminiboot)
time.sleep(3)
else:
print("No port or drive detected, flashing blank board")
flash_new_firmware()
gem = gemini.Gemini.get()
version = gem.get_firmware_version()
print("✓ Firmware version {version}")
def main():
parser = argparse.ArgumentParser(
formatter_class=argparse.ArgumentDefaultsHelpFormatter
)
parser.add_argument(
"--stages",
type=str,
nargs="*",
default=["firmware", "clock", "ramp", "adc", "cv"],
help="Select which setup stages to run.",
)
parser.add_argument(
"--force-report",
type=bool,
help="Generate a report even if not all steps are run.",
)
args = parser.parse_args()
hubble = Hubble.get()
hubble.start()
if "firmware" in args.stages:
print("# Programming firmware")
setup_firmware_and_bootloader()
if "erase_nvm" in args.stages:
erase_nvm()
get_firmware_and_serial()
if "adc" in args.stages:
print("# Calibrating ADC")
REPORT.sections.append(adc_calibration.run())
if "cv" in args.stages:
print("# Calibrating pitch CV")
REPORT.sections.append(cv_calibration.run())
if "clock" in args.stages:
print("# Calibrating 8 MHz clock")
REPORT.sections.append(clock_calibration.run())
if "ramp" in args.stages:
print("# Calibrating ramps")
REPORT.sections.append(ramp_calibration.run(save=True))
gem = gemini.Gemini.get()
gem.soft_reset()
if not args.force_report and args.stages != [
"firmware",
"clock",
"ramp",
"adc",
"cv",
]:
print(REPORT)
print("!! Not all stages run, not saving report.")
return
print(REPORT)
REPORT.save()
reportcard.render_html(REPORT)
if REPORT.succeeded:
hubble.success()
print.success()
else:
hubble.fail()
print.failure()
thermalprinter.print_me_maybe(reportcard.render_image(REPORT))
if __name__ == "__main__":
main()