#!/usr/bin/env python import usb.core import usb.util import time, struct from hidapi import hidapi import ctypes from elftools.elf.elffile import ELFFile pid_runtime = 0x6080 pid_bootloader = 0x6084 e = ELFFile(open('arcin.elf')) firmware = '' for segment in sorted(e.iter_segments(), key = lambda x: x.header.p_paddr): if segment.header.p_type != 'PT_LOAD': continue data = segment.data() lma = segment.header.p_paddr # Workaround for LD aligning segments to a larger boundary than 8k. if lma == 0x8000000: lma += 0x2000 data = data[0x2000:] # Add padding if necessary. firmware += '\0' * (lma - 0x8002000 - len(firmware)) firmware += data # Align to 64B if len(firmware) & (64 - 1): firmware += '\0' * (64 - (len(firmware) & (64 - 1))) # Find test board. dev = usb.core.find(idVendor = 0x1234, idProduct = 0x5678) if not dev: print 'Test board not found.' exit(1) def set_buttons(value): dev.ctrl_transfer(0xc0, 0xf0, value, 0, 0) def get_leds(): return struct.unpack(' %d, %d -> %d' % (value, a, va, b, vb) if (a + value) & 0xff != va: raise TestFail('qe1') if (b + value) & 0xff != vb: raise TestFail('qe2') def test_all(): print 'Testing leds.' test_leds(0) test_leds(0x7ff) for i in range(11): test_leds(1 << i) print 'Testing buttons.' test_buttons(0) test_buttons(0x7ff) for i in range(11): test_buttons(1 << i) print 'Testing encoders.' test_qe(5) test_qe(5) test_qe(-5) test_qe(-5) print 'All passed.' def process(): try: open_hiddev(pid_runtime) print 'Found runtime device, resetting to bootloader.' # Reset bootloader if hidapi.hid_send_feature_report(hiddev, ctypes.c_char_p('\x00\x10'), 2) != 2: raise RuntimeError('Reset failed.') time.sleep(1) except: pass try: open_hiddev(pid_bootloader) flash_board() time.sleep(1) open_hiddev(pid_runtime) test_all() except TestFail, e: print 'Test failed:', e except RuntimeError, e: print 'Error:', e while 1: raw_input('Press enter to start\n') process() print