From 1f0e378875f6e24a1003c284017c000003853aaa Mon Sep 17 00:00:00 2001 From: Jenna Fox Date: Thu, 27 Sep 2012 09:59:22 +1000 Subject: Reflect name change to micronucleus in readme and ruby scripts --- Readme.txt | 8 ++- firmware/microboot.rb | 166 ----------------------------------------------- firmware/micronucleus.rb | 166 +++++++++++++++++++++++++++++++++++++++++++++++ firmware/upload.rb | 12 ++-- 4 files changed, 177 insertions(+), 175 deletions(-) delete mode 100644 firmware/microboot.rb create mode 100644 firmware/micronucleus.rb diff --git a/Readme.txt b/Readme.txt index f7568a3..f5d15a4 100644 --- a/Readme.txt +++ b/Readme.txt @@ -1,9 +1,11 @@ -microBoot tiny85 Note -===================== +micronucleus tiny85 Note +======================== This is a brand new project by bluebie, which merges code from embedded-creations' wonderful USBaspLoader-tiny85 project with bits from obdev's bootloadHID and bits of my own invention to create a minimalist bootloader for tiny85 chips. It's hot -off the presses, and so this readme and everything else is already way out of date +off the presses, and so this readme and everything else is already way out of date. +During it's early development it was called 'µBoot-t85', and the name has since +changed to avoid conflict with a similarly named ARM bootloader of no relation. Some quick notes: The stuff in commandline is a slightly modified version of bootloadHID's commandline diff --git a/firmware/microboot.rb b/firmware/microboot.rb deleted file mode 100644 index a0450dc..0000000 --- a/firmware/microboot.rb +++ /dev/null @@ -1,166 +0,0 @@ -require 'libusb' - -# Abstracts access to uBoot avr tiny85 bootloader - can be used only to upload bytes -class MicroBoot - Functions = [ - :get_info, - :write_page, - :erase_application, - :run_program - ] - - # return all thinklets - def self.all - usb = LIBUSB::Context.new - usb.devices.select { |device| - device.idVendor == 0x16d0 && device.idProduct == 0x0753 #&& device.product == "\xB5B" - }.map { |device| - self.new(device) - } - end - - def initialize devref - @device = devref - end - - def info - unless @info - result = control_transfer(function: :get_info, dataIn: 4) - flash_length, page_size, write_sleep = result.unpack('S>CC') - - @info = { - flash_length: flash_length, - page_size: page_size, - pages: (flash_length.to_f / page_size).ceil, - write_sleep: write_sleep.to_f / 1000.0, - version: "#{@device.bcdDevice >> 8}.#{@device.bcdDevice & 0xFF}", - version_numeric: @device.bcdDevice - } - end - - @info - end - - def erase! - puts "erasing" - info = self.info - control_transfer(function: :erase_application) - info[:pages].times do - sleep(info[:write_sleep]) # sleep for as many pages as the chip has to erase - end - puts "erased chip" - end - - # upload a new program - def program= bytestring - info = self.info - raise "Program too long!" if bytestring.bytesize > info[:flash_length] - bytes = bytestring.bytes.to_a - - erase! - - address = 0 - bytes.each_slice(info[:page_size]) do |slice| - slice.push(0xFF) while slice.length < info[:page_size] # ensure every slice is one page_size long - pad out if needed - - puts "uploading @ #{address} of #{bytes.length}" - control_transfer(function: :write_page, wIndex: address, wValue: slice.length, dataOut: slice.pack('C*')) - sleep(info[:write_sleep]) - address += slice.length - end - end - - def finished - info = self.info - - puts "asking device to finish writing" - control_transfer(function: :run_program) - puts "waiting for device to finish" - - # sleep for as many pages as the chip could potentially need to write - this could be smarter - info[:pages].times do - sleep(info[:write_sleep]) - end - - @io.close - @io = nil - end - - def inspect - "" - end - - protected - # raw opened device - def io - unless @io - @io = @device.open - end - - @io - end - - def control_transfer(opts = {}) - opts[:bRequest] = Functions.index(opts.delete(:function)) if opts[:function] - io.control_transfer({ - wIndex: 0, - wValue: 0, - bmRequestType: usb_request_type(opts), - timeout: 5000 - }.merge opts) - end - - # calculate usb request type - def usb_request_type opts #:nodoc: - value = LIBUSB::REQUEST_TYPE_VENDOR | LIBUSB::RECIPIENT_DEVICE - value |= LIBUSB::ENDPOINT_OUT if opts.has_key? :dataOut - value |= LIBUSB::ENDPOINT_IN if opts.has_key? :dataIn - return value - end -end - -class HexProgram - def initialize input - @bytes = Hash.new(0xFF) - input = input.read if input.is_a? IO - parse input - end - - def binary - highest_address = @bytes.keys.max - - bytestring = Array.new(highest_address + 1) { |index| - @bytes[index] - }.pack('C*') - end - - protected - - def parse input_text - input_text.each_line do |line| - next unless line.start_with? ':' - line.chomp! - length = line[1..2].to_i(16) # usually 16 or 32 - address = line[3..6].to_i(16) # 16-bit start address - record_type = line[7..8].to_i(16) - data = line[9.. 9 + (length * 2)] - checksum = line[9 + (length * 2).. 10 + (length * 2)].to_i(16) - checksum_section = line[1...9 + (length * 2)] - - checksum_calculated = checksum_section.chars.to_a.each_slice(2).map { |slice| - slice.join('').to_i(16) - }.reduce(0, &:+) - - checksum_calculated = (((checksum_calculated % 256) ^ 0xFF) + 1) % 256 - - raise "Hex file checksum mismatch @ #{line}" unless checksum == checksum_calculated - - if record_type == 0 # data record - data_bytes = data.chars.each_slice(2).map { |slice| slice.join('').to_i(16) } - data_bytes.each_with_index do |byte, index| - @bytes[address + index] = byte - end - end - end - end -end \ No newline at end of file diff --git a/firmware/micronucleus.rb b/firmware/micronucleus.rb new file mode 100644 index 0000000..3211d84 --- /dev/null +++ b/firmware/micronucleus.rb @@ -0,0 +1,166 @@ +require 'libusb' + +# Abstracts access to micronucleus avr tiny85 bootloader - can be used only to erase and upload bytes +class Micronucleus + Functions = [ + :get_info, + :write_page, + :erase_application, + :run_program + ] + + # return all micronucleus devices connected to computer + def self.all + usb = LIBUSB::Context.new + usb.devices.select { |device| + device.idVendor == 0x16d0 && device.idProduct == 0x0753 + }.map { |device| + self.new(device) + } + end + + def initialize devref + @device = devref + end + + def info + unless @info + result = control_transfer(function: :get_info, dataIn: 4) + flash_length, page_size, write_sleep = result.unpack('S>CC') + + @info = { + flash_length: flash_length, + page_size: page_size, + pages: (flash_length.to_f / page_size).ceil, + write_sleep: write_sleep.to_f / 1000.0, + version: "#{@device.bcdDevice >> 8}.#{@device.bcdDevice & 0xFF}", + version_numeric: @device.bcdDevice + } + end + + @info + end + + def erase! + puts "erasing" + info = self.info + control_transfer(function: :erase_application) + info[:pages].times do + sleep(info[:write_sleep]) # sleep for as many pages as the chip has to erase + end + puts "erased chip" + end + + # upload a new program + def program= bytestring + info = self.info + raise "Program too long!" if bytestring.bytesize > info[:flash_length] + bytes = bytestring.bytes.to_a + + erase! + + address = 0 + bytes.each_slice(info[:page_size]) do |slice| + slice.push(0xFF) while slice.length < info[:page_size] # ensure every slice is one page_size long - pad out if needed + + puts "uploading @ #{address} of #{bytes.length}" + control_transfer(function: :write_page, wIndex: address, wValue: slice.length, dataOut: slice.pack('C*')) + sleep(info[:write_sleep]) + address += slice.length + end + end + + def finished + info = self.info + + puts "asking device to finish writing" + control_transfer(function: :run_program) + puts "waiting for device to finish" + + # sleep for as many pages as the chip could potentially need to write - this could be smarter + info[:pages].times do + sleep(info[:write_sleep]) + end + + @io.close + @io = nil + end + + def inspect + "" + end + + protected + # raw opened device + def io + unless @io + @io = @device.open + end + + @io + end + + def control_transfer(opts = {}) + opts[:bRequest] = Functions.index(opts.delete(:function)) if opts[:function] + io.control_transfer({ + wIndex: 0, + wValue: 0, + bmRequestType: usb_request_type(opts), + timeout: 5000 + }.merge opts) + end + + # calculate usb request type + def usb_request_type opts #:nodoc: + value = LIBUSB::REQUEST_TYPE_VENDOR | LIBUSB::RECIPIENT_DEVICE + value |= LIBUSB::ENDPOINT_OUT if opts.has_key? :dataOut + value |= LIBUSB::ENDPOINT_IN if opts.has_key? :dataIn + return value + end +end + +class HexProgram + def initialize input + @bytes = Hash.new(0xFF) + input = input.read if input.is_a? IO + parse input + end + + def binary + highest_address = @bytes.keys.max + + bytestring = Array.new(highest_address + 1) { |index| + @bytes[index] + }.pack('C*') + end + + protected + + def parse input_text + input_text.each_line do |line| + next unless line.start_with? ':' + line.chomp! + length = line[1..2].to_i(16) # usually 16 or 32 + address = line[3..6].to_i(16) # 16-bit start address + record_type = line[7..8].to_i(16) + data = line[9.. 9 + (length * 2)] + checksum = line[9 + (length * 2).. 10 + (length * 2)].to_i(16) + checksum_section = line[1...9 + (length * 2)] + + checksum_calculated = checksum_section.chars.to_a.each_slice(2).map { |slice| + slice.join('').to_i(16) + }.reduce(0, &:+) + + checksum_calculated = (((checksum_calculated % 256) ^ 0xFF) + 1) % 256 + + raise "Hex file checksum mismatch @ #{line}" unless checksum == checksum_calculated + + if record_type == 0 # data record + data_bytes = data.chars.each_slice(2).map { |slice| slice.join('').to_i(16) } + data_bytes.each_with_index do |byte, index| + @bytes[address + index] = byte + end + end + end + end +end \ No newline at end of file diff --git a/firmware/upload.rb b/firmware/upload.rb index b2537d8..32aefa9 100644 --- a/firmware/upload.rb +++ b/firmware/upload.rb @@ -1,4 +1,4 @@ -require_relative './microboot' +require_relative './micronucleus' if ARGV[0] if ARGV[0].end_with? '.hex' @@ -15,19 +15,19 @@ end #test_data += ("\xFF" * 64) puts "Plug in programmable device now: (waiting)" -sleep 0.25 while MicroBoot.all.length == 0 +sleep 0.25 while Micronucleus.all.length == 0 -ubootable = MicroBoot.all.first -puts "Attached to device: #{ubootable.inspect}" +nucleus = Micronucleus.all.first +puts "Attached to device: #{nucleus.inspect}" #puts "Attempting to write '#{test_data.inspect}' to first thinklet's program memory" #puts "Bytes: #{test_data.bytes.to_a.inspect}" sleep(0.25) # some time to think? puts "Attempting to write supplied program in to device's memory" -ubootable.program = test_data +nucleus.program = test_data puts "Great! Starting program..." -ubootable.finished # let thinklet know it can go do other things now if it likes +nucleus.finished # let thinklet know it can go do other things now if it likes puts "All done!" -- cgit v1.2.3