4 ./dtprog.py <dthexfile> [-d /dev/ttyXXX] [-b <baudrate>]
6 bootROM, a very small bootloader for $NAME
9 abbrv: H = Host, CPU = C
11 value/cmd | direction | comment
12 ------------------------------------------------------------
13 'H' | H -> C | enter bootROM ("HI")
14 'O' | C -> H | ack bootROM entry ("OH HAI")
16 'W'0xZZZZZZZZ0xYYYYYYYY| H -> C | write instr (0xYY...Y) to
18 'D' | C -> H | instr write done
20 'R'0xZZZZZZZZ | H -> C | read instr from address (0xZZ..Z)
21 'F'0xYYYYYYYY | C -> H | instr read done and return instr
23 'Q'0xZZZZZZZZ0xYYYYYYYY| H -> C | write data (0xYY...Y) to
25 'A' | C -> H | data write done
27 'T'0xZZZZZZZZ | H -> C | read data from address (0xZZ..Z)
28 'G'0xYYYYYYYY | C -> H | read done and return data
30 'J'0xZZZZZZZZ | H -> C | jump to address (0xZZ...Z)
33 from SerialPort_linux import SerialPort, SerialPortException
37 class RAMSequence(object):
38 def __init__(self, type, address, data):
45 send a byte to the TTY-device
51 send a word to the TTY-device
54 sendbyte((word >> 8) & 0xFF)
58 send a dword to the TTY-device
60 sendbyte((dword >> 24) & 0xFF)
61 sendbyte((dword >> 16) & 0xFF)
62 sendbyte((dword >> 8) & 0xFF)
63 sendbyte(dword & 0xFF)
67 receive a byte from the TTY-device
69 return ord(tty.read())
72 return (recvbyte() << 24) & (recvbyte() << 16) & (recvbyte() << 8) & recvbyte()
77 def brwriteinstr(address, data):
79 send a WRITE-command (instr) to the bootROM-firmware
81 # send WRITE instr command
84 if (recvbyte() != 0x57): # 'W'
85 print "host: write instr fail (1)"
90 if (recvbyte() != 0x44): # 'D'
91 print "host: write instr fail (2)"
94 def brwritedata(address, data):
96 send a WRITE-command (data) to the bootROM-firmware
98 # send WRITE instr command
101 if (recvbyte() != 0x51): # 'Q'
102 print "host: write data fail (1)"
107 if (recvbyte() != 0x41): # 'A'
108 print "host: write data fail (2)"
111 def readdthexfile(filename): # desired dthex filename
113 proceeds a dthex file
115 filep = open(filename, "r")
116 retval = [] # returns a list of FlashSequence objects
120 # get rid of newline characters
123 # we're only interested in 0 or 1
124 if line[0:1] == "0" or line[0:1] == "1":
126 type = int(line[0:1], 10)
127 addr = int(line[2:10], 16)/4
128 data = int(line[11:19], 16)
129 # print "type: ", type, "; addr: ", hex(addr), "; data: ", hex(data)
130 retval.append(RAMSequence(type, addr, data))
136 print usage of dtprog
138 print "Usage: " + execf + " <target dthex-file> [-d DEVICE] [-b BAUDRATE]"
139 print "maybe arg-eval fail. fix the source!"
143 main function of frprog
145 # check command line arguments
149 if len(argv) == 2 and (argv[1] == "-v" or argv[1] == "--version"):
150 print "Version: lulz"
153 if len(argv) != 2 and len(argv) != 4 and len(argv) != 6:
159 # standard serial device to communicate with
160 device = "/dev/ttyS0"
163 # overrule standard device if provided with -d
171 # overrule standard baudrate if provided with -b
174 baudrate = int(argv[5],10)
179 # read in data from mhx-files before starting
181 ramseqs = readdthexfile(argv[1])
182 except IOError, error:
183 print argv[0] + ": Error - couldn't open file " + error.filename + "!"
186 print "Initializing serial port... (", device, ")"
188 tty = SerialPort(device, 100, baudrate)
194 if tty.read() == 'O':
196 except SerialPortException:
197 # timeout happened, who cares ;-)
198 print "host: cpu in BootROM?"
200 print "bootrom: OH HAI!"
202 # save time at this point for evaluating the duration at the end
203 starttime = time.time()
207 print "host: Transfering program to BootROM",
208 print #TODO: remove ;)
211 print "RAMing(", seq.type, ") ", hex(seq.data), " at address ",
214 print "", hex(seq.addr)
215 print "host: writing instr"
216 brwriteinstr(seq.addr, seq.data)
219 print "", hex(seq.addr*4)
220 print "host: writing data"
221 brwritedata(seq.addr * 4, seq.data)
226 sys.stdout.write(".")
232 print "Program was started. Have fun!"
233 duration = time.time() - starttime
234 print "Procedure complete, took", round(duration, 2), "seconds."
236 if __name__ == '__main__':