]>
Commit | Line | Data |
---|---|---|
ec1bef8e | 1 | #!/usr/bin/env ruby |
2 | ||
3 | $:.unshift(File.dirname(__FILE__)+"/lib") | |
4 | ||
5 | require 'fnordlicht' | |
6 | require 'serialport' | |
7 | ||
8 | include Fnordlicht | |
9 | ||
10 | $dev = SerialPort.new("/dev/ttyUSB0", 19200) | |
11 | ||
12 | def crc16_update(crc, data) | |
13 | return nil if crc < 0 || crc > 0xffff | |
14 | return nil if data < 0 || data > 255 | |
15 | ||
16 | crc ^= data | |
17 | 0.upto(7) do |i| | |
18 | if crc % 2 == 1 | |
19 | crc = (crc >> 1) ^ 0xA001 | |
20 | else | |
21 | crc = (crc >> 1) | |
22 | end | |
23 | end | |
24 | ||
25 | return crc | |
26 | end | |
27 | ||
28 | ||
29 | def compute_checksum(data) | |
30 | checksum = 0xffff | |
31 | ||
32 | data.each_byte do |b| | |
33 | checksum = crc16_update(checksum, b) | |
34 | end | |
35 | ||
36 | return checksum | |
37 | end | |
38 | ||
39 | if ARGV.length != 2 | |
40 | $stderr.puts "usage: %s ADDRESS FILE" % $0 | |
41 | exit 1 | |
42 | end | |
43 | ||
44 | address = ARGV.shift.to_i | |
45 | file = ARGV.shift | |
46 | ||
47 | puts "sending sync sequence" | |
48 | sync() | |
49 | ||
50 | puts "flash" | |
51 | open(file, 'r') do |f| | |
52 | puts "configure bootloader" | |
53 | boot_config(address, 0) | |
54 | loop do | |
55 | d = f.read(512) | |
56 | break if d.nil? | |
57 | puts "writing chunk (%s bytes)" % d.length | |
58 | ||
59 | boot_init(address); | |
60 | d.split('').each_slice(13) do |s| | |
61 | boot_data(address, s.join('')) | |
62 | #sleep 0.05 | |
63 | end | |
64 | checksum = compute_checksum(d) | |
65 | boot_crc_check(address, d.length, checksum, 5); | |
66 | ||
67 | puts "flashing..." | |
68 | boot_flash(address) | |
69 | sleep 0.3 | |
70 | ||
71 | break if d.length < 512 | |
72 | end | |
73 | end | |
74 | ||
75 | data = "" | |
76 | open(file, 'r') do |f| | |
77 | d = f.read() | |
78 | break if d.nil? | |
79 | data += d | |
80 | end | |
81 | checksum = compute_checksum(data) | |
82 | puts "verifying checksum (%u bytes): 0x%04x" % [data.length, checksum] | |
83 | boot_crc_flash(address, 0, data.length, checksum, 50) | |
84 | puts "done" |