]> git.zerfleddert.de Git - fnordlicht-mini/blame - firmware/tests/flash_file.rb
add initialization of node address via eeprom
[fnordlicht-mini] / firmware / tests / flash_file.rb
CommitLineData
ec1bef8e 1#!/usr/bin/env ruby
2
3$:.unshift(File.dirname(__FILE__)+"/lib")
4
5require 'fnordlicht'
6require 'serialport'
7
8include Fnordlicht
9
10$dev = SerialPort.new("/dev/ttyUSB0", 19200)
11
12def 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
26end
27
28
29def 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
37end
38
39if ARGV.length != 2
40 $stderr.puts "usage: %s ADDRESS FILE" % $0
41 exit 1
42end
43
44address = ARGV.shift.to_i
45file = ARGV.shift
46
47puts "sending sync sequence"
48sync()
49
50puts "flash"
51open(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
73end
74
75data = ""
76open(file, 'r') do |f|
77 d = f.read()
78 break if d.nil?
79 data += d
80end
81checksum = compute_checksum(data)
82puts "verifying checksum (%u bytes): 0x%04x" % [data.length, checksum]
83boot_crc_flash(address, 0, data.length, checksum, 50)
84puts "done"
Impressum, Datenschutz