-
Notifications
You must be signed in to change notification settings - Fork 9
/
flash2.rb
executable file
·179 lines (154 loc) · 3.76 KB
/
flash2.rb
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
#!/usr/bin/env ruby
class Flasher
def initialize
@mcu = 'm32u4'
set_usbasp
@port = nil
end
def set_avrispmkii
@programmer = 'avrispmkii'
@extra_params = '-B 2 -v'
#@extra_params = '-B 2'
end
def set_usbasp
@programmer = 'usbasp'
@extra_params = '-B 2 -v'
end
def flash(items, quiet = false)
cmd = "avrdude -p #{@mcu} -c #{@programmer}"
unless @port.nil?
cmd << " -P #{@port}"
end
unless @extra_params.nil?
cmd << " #{@extra_params}"
end
items.each do |memtype, filename|
cmd << " -U #{memtype}:w:#{filename}"
end
if quiet
cmd << " -l /dev/null"
else
puts cmd
end
`#{cmd}`
$?.success?
end
def read(items, quiet = false)
cmd = "avrdude -p #{@mcu} -c #{@programmer}"
unless @port.nil?
cmd << " -P #{@port}"
end
unless @extra_params.nil?
cmd << " #{@extra_params}"
end
items.each do |memtype, filename|
cmd << " -U #{memtype}:r:#{filename}:i"
end
if quiet
cmd << " -l /dev/null"
else
puts cmd
end
`#{cmd}`
$?.success?
end
def dfu_fuses
{ lfuse: '0x5e:m', hfuse: '0xd9:m', efuse: '0xc3:m' }
end
def make_avrisp_mkii_clone
fw_file = 'AVRISP-MKII_ATmega32u4/AVRISP-MKII_ATmega32U4.hex'
items = dfu_fuses.merge(flash: fw_file)
flash_file(fw_file)
end
def flash_dfu_bootloader
fw_file = 'BootloaderDFU-LUFA-32u4.hex'
items = dfu_fuses.merge(flash: fw_file)
flash(items)
end
def flash_iris_r3
files = {
flash: "#{__dir__}/iris-r3/keebio_iris_rev3_via_production.hex",
eeprom: "#{__dir__}/iris-r3/20190603_iris.eep"
}
items = dfu_fuses.merge(files)
flash(items)
end
def flash_iris_r3_eeprom
flash(eeprom: 'iris-r3/20190603_iris.eep')
end
def flash_nyquist_r3
files = {
flash: "#{__dir__}/nyquist-r3/keebio_nyquist_rev3_default_production.hex",
eeprom: "#{__dir__}/nyquist-r3/20190816_nyquist.eep"
}
items = dfu_fuses.merge(files)
flash(items)
end
def flash_usbasp
@mcu = 'm8'
flash_file('usbasp.2011-05-28/bin/firmware/usbasp.atmega8.2011-05-28.hex')
end
def view_device_info
flash({})
end
def device_connected?
flash({}, quiet = true)
end
def flash_file(file)
flash(flash: file)
end
def read_eeprom(output_file)
read(eeprom: output_file)
end
def flash_eeprom(file)
flash(eeprom: file)
end
def show_searching_message
print '🔎 Searching for device (press Ctrl-C to stop).'
end
def bulk_flash(command, interval = 0.5)
show_searching_message
while true do
success = device_connected?
if success
puts
puts '🆗 Device found, flashing...'
command.call
flash_success = $?.success?
if flash_success
puts '✅ Device flashed successfully, disconnect'
else
puts '🛑 Device flashing unsuccessful, try again'
end
play_status_sound(flash_success)
sleep 3
show_searching_message
else
print '.'
end
sleep interval
end
end
def play_status_sound(success)
cmd = "sudo afplay"
if success
cmd << " /System/Library/Sounds/Glass.aiff"
else
cmd << " /System/Library/Sounds/Sosumi.aiff"
end
`#{cmd}`
end
end
# TODO: Add params to select action
flasher = Flasher.new()
#flasher.set_usbasp
flasher.set_avrispmkii
#flasher.view_device_info
#flasher.make_avrisp_mkii_clone
#flasher.flash_dfu_bootloader
#flasher.flash_file('/Users/danny/syncproj/qmk/keebio_levinson_rev3_bakingpy.hex')
#flasher.flash_iris_r3
#flasher.flash_iris_r3_eeprom
flasher.bulk_flash(flasher.method(:flash_nyquist_r3))
#flasher.read_eeprom('iris_rev4_default.hex')
#flasher.flash_eeprom('iris_rev4_default.hex')