Hardware Design: SIE
Sign in or create your account | Project List | Help
Hardware Design: SIE Git Source Tree
Root/
| 1 | #!/usr/bin/env ruby |
| 2 | |
| 3 | require "serialport.so" |
| 4 | require "optparse" |
| 5 | |
| 6 | port_baud = 115200 |
| 7 | port_path = "/dev/ttyUSB0" |
| 8 | terminal_mode = false |
| 9 | verose = false |
| 10 | |
| 11 | ############################################################################# |
| 12 | # Extend SerialPort class with low-leven binary communication |
| 13 | class SerialPort |
| 14 | BOOT_SIG = "**soc-lm32/bootloader**" |
| 15 | |
| 16 | def put_uint32(i) |
| 17 | putc( (i >> 24) & 0xff ) |
| 18 | putc( (i >> 16) & 0xff ) |
| 19 | putc( (i >> 8) & 0xff ) |
| 20 | putc( (i >> 0) & 0xff ) |
| 21 | end |
| 22 | |
| 23 | def download(addr, size) |
| 24 | a = Array.new(size) |
| 25 | putc 'd' |
| 26 | put_uint32 addr |
| 27 | put_uint32 size |
| 28 | size.times do |i| |
| 29 | a[i] = getc |
| 30 | end |
| 31 | return a |
| 32 | end |
| 33 | |
| 34 | def upload(addr, data) |
| 35 | putc 'u' |
| 36 | put_uint32 addr |
| 37 | put_uint32 data.length |
| 38 | data.each do |c| |
| 39 | putc c |
| 40 | end |
| 41 | end |
| 42 | |
| 43 | def find_bootloader(max_tries = 32) |
| 44 | old_timeout = read_timeout |
| 45 | read_timeout = 500 |
| 46 | count = 0; |
| 47 | begin |
| 48 | count = count + 1 |
| 49 | if (count == max_tries) then |
| 50 | raise "Bootloader (#{BOOT_SIG}) not not found" |
| 51 | end |
| 52 | putc '\r' |
| 53 | l = gets |
| 54 | end while l.nil? or not l.index( BOOT_SIG ) |
| 55 | read_timeout = old_timeout |
| 56 | end |
| 57 | end |
| 58 | |
| 59 | ############################################################################# |
| 60 | # Main |
| 61 | |
| 62 | opts = OptionParser.new do |o| |
| 63 | o.banner = "Usage: upload.rb [options] <file.srec>" |
| 64 | |
| 65 | o.on( "-b", "--baud BAUDRATE", Integer, |
| 66 | "Serial port baudrate (default: #{port_baud})" ) do |baud| |
| 67 | port_baud = baud |
| 68 | end |
| 69 | |
| 70 | o.on( "-s", "--serial SERIALPORT", |
| 71 | "Path to serial port (default: #{port_path})" ) do |port| |
| 72 | port_path = port |
| 73 | end |
| 74 | |
| 75 | o.on( "-v", "--verbose", |
| 76 | "Be verbose and show serial I/O" ) do |
| 77 | verbose = true |
| 78 | end |
| 79 | |
| 80 | o.on_tail( "-h", "--help", "Display this help message" ) do |
| 81 | puts o |
| 82 | exit 0 |
| 83 | end |
| 84 | end |
| 85 | |
| 86 | # Check arguments, open serial port and file |
| 87 | begin |
| 88 | opts.parse!(ARGV) |
| 89 | |
| 90 | port = SerialPort.new(port_path, port_baud, 8, 1, SerialPort::NONE) |
| 91 | raise "Could not open serial port." if not port; |
| 92 | |
| 93 | rescue => e |
| 94 | STDERR.puts "\nERROR: #{e.message}" |
| 95 | STDERR.puts |
| 96 | STDERR.puts opts |
| 97 | STDERR.puts |
| 98 | exit 1 |
| 99 | end |
| 100 | |
| 101 | |
| 102 | |
| 103 | begin |
| 104 | BLOCK_SIZE = 0x800 |
| 105 | TEST_SIZE = 0x10000 |
| 106 | TEST_BASE = 0x40000000 |
| 107 | |
| 108 | STDOUT.sync = true |
| 109 | |
| 110 | # Find bootloader |
| 111 | print "Looking for soc-lm32 bootloader..." |
| 112 | port.find_bootloader |
| 113 | puts "found." |
| 114 | |
| 115 | # generate random data |
| 116 | data = Array.new( TEST_SIZE ) |
| 117 | data.map! do |e| e = rand(256) end |
| 118 | |
| 119 | # upload random data |
| 120 | print "Uploading 0x%X random bytes to 0x%X..." % [TEST_SIZE, TEST_BASE] |
| 121 | (TEST_SIZE / BLOCK_SIZE).times do |i| |
| 122 | print "." |
| 123 | offset = i*BLOCK_SIZE |
| 124 | block = data[ offset .. (offset+BLOCK_SIZE-1) ] |
| 125 | port.upload( TEST_BASE + i*BLOCK_SIZE, block ) |
| 126 | end |
| 127 | puts "done." |
| 128 | |
| 129 | # download again |
| 130 | print "Downloading again..." |
| 131 | ddata = Array.new |
| 132 | (TEST_SIZE / BLOCK_SIZE).times do |i| |
| 133 | print "." |
| 134 | ddata += port.download( TEST_BASE + i*BLOCK_SIZE, BLOCK_SIZE ) |
| 135 | end |
| 136 | puts "done." |
| 137 | |
| 138 | puts "Checking for memory errors..." |
| 139 | TEST_SIZE.times do |i| |
| 140 | if data[i] != ddata[i] then |
| 141 | puts "0x%08x: %02x %02x" % [i, data[i], ddata[i] ] |
| 142 | end |
| 143 | end |
| 144 | puts "done." |
| 145 | ensure |
| 146 | port.close unless port.nil? |
| 147 | end |
| 148 | |
| 149 | |
| 150 | |
| 151 |
Branches:
master
