freemyipod r532 - Code Review

Jump to: navigation, search
Repository:freemyipod
Revision:r531‎ | r532 | r533 >
Date:02:03, 6 February 2011
Author:farthen
Status:new
Tags:
Comment:
emcore tools: Convert numbers that were previously hex only to hex with 0x prefix, to binary with 0b prefix and to decimal with no prefix. This BREAKS anything that relied on numbers without prefix being converted to hex.
Modified paths:
  • /emcore/trunk/tools/emcore.py (modified) (history)
  • /emcore/trunk/tools/emcoreldr.py (modified) (history)
  • /emcore/trunk/tools/libemcore.py (modified) (history)
  • /emcore/trunk/tools/misc.py (modified) (history)

Diff [purge]

Index: emcore/trunk/tools/emcore.py
@@ -36,26 +36,9 @@
3737
3838 import libemcore
3939 import libemcoredata
40 -from misc import Error, Logger, getfuncdoc, gethwname
 40+from misc import Error, ArgumentError, ArgumentTypeError, Logger, getfuncdoc, gethwname, to_bool, to_int
4141
4242
43 -class NotImplementedError(Error):
44 - pass
45 -
46 -class ArgumentError(Error):
47 - pass
48 -
49 -class ArgumentTypeError(Error):
50 - def __init__(self, expected, seen=False):
51 - self.expected = expected
52 - self.seen = seen
53 - def __str__(self):
54 - if self.seen:
55 - return "Expected %s but got %s" % (self.expected, self.seen)
56 - else:
57 - return "Expected %s, but saw something else" % self.expected
58 -
59 -
6043 def usage(errormsg=None, specific=False, docstring=True):
6144 """
6245 Prints the usage information.
@@ -162,46 +145,8 @@
163146 self.logger.error("There is a problem with the USB connection.\n")
164147 else:
165148 usage("No such command!", docstring = False)
 149+
166150
167 - @staticmethod
168 - def _bool(something):
169 - """
170 - Converts quite everything into bool.
171 - """
172 - if type(something) == bool:
173 - return something
174 - if something is None:
175 - return False
176 - elif type(something) == int or type(something) == long:
177 - return bool(something)
178 - elif type(something == str):
179 - truelist = ['true', '1', 't', 'y', 'yes']
180 - falselist = ['false', '0', 'f', 'n', 'no']
181 - if something.lower() in truelist:
182 - return True
183 - elif something.lower() in falselist:
184 - return False
185 - raise ArgumentTypeError("bool", "'%s'" % something)
186 -
187 - @staticmethod
188 - def _hexint(something):
189 - """
190 - Converts quite everything to a hexadecimal represented integer.
191 - This works for default arguments too, because it returns
192 - None when it found that it got a NoneType object.
193 - """
194 - if type(something) == int or type(something) == long:
195 - return something
196 - elif type(something) == str:
197 - try:
198 - return int(something, 16)
199 - except ValueError:
200 - raise ArgumentTypeError("hexadecimal coded integer", "'%s'" % something)
201 - elif something is None:
202 - return None
203 - else:
204 - raise ArgumentTypeError("hexadecimal coded integer", "'%s'" % something)
205 -
206151 @command
207152 def help(self):
208153 """ Displays this help """
@@ -246,7 +191,7 @@
247192 If [force] is 1, the reset will be forced, otherwise it will be gracefully,
248193 which may take some time.
249194 """
250 - force = self._bool(force)
 195+ force = to_bool(force)
251196 if force: self.logger.info("Resetting forcefully...\n")
252197 else: self.logger.info("Resetting...\n")
253198 self.emcore.reset(force)
@@ -258,7 +203,7 @@
259204 If [force] is 1, the poweroff will be forced, otherwise it will be gracefully,
260205 which may take some time.
261206 """
262 - force = self._bool(force)
 207+ force = to_bool(force)
263208 if force: self.logger.info("Powering off forcefully...\n")
264209 else: self.logger.info("Powering off...\n")
265210 self.emcore.poweroff(force)
@@ -270,7 +215,7 @@
271216 <filename>: The path to the file
272217 [addr]: The address to upload the file to. Allocates a chunk of memory if not given.
273218 """
274 - addr = self._hexint(addr)
 219+ addr = to_int(addr)
275220 try:
276221 f = open(filename, 'rb')
277222 except IOError:
@@ -297,8 +242,8 @@
298243 <size>: the number of bytes to be read
299244 <filename>: the path to the file
300245 """
301 - addr = self._hexint(addr)
302 - size = self._hexint(size)
 246+ addr = to_int(addr)
 247+ size = to_int(size)
303248 try:
304249 f = open(filename, 'wb')
305250 except IOError:
@@ -317,8 +262,8 @@
318263 <addr>: the address to upload the integer to
319264 <integer>: the integer to upload
320265 """
321 - addr = self._hexint(addr)
322 - integer = self._hexint(integer)
 266+ addr = to_int(addr)
 267+ integer = to_int(integer)
323268 if integer > 0xFFFFFFFF:
324269 raise ArgumentError("Specified integer too long")
325270 data = struct.pack("I", integer)
@@ -331,7 +276,7 @@
332277 Downloads a single integer from the device and prints it to the console window
333278 <addr>: the address to download the integer from
334279 """
335 - addr = self._hexint(addr)
 280+ addr = to_int(addr)
336281 data = self.emcore.read(addr, 4)
337282 integer = struct.unpack("I", data)[0]
338283 self.logger.info("Read '0x%X' from address 0x%X\n" % (integer, addr))
@@ -345,10 +290,10 @@
346291 <addr>: the start address on the I2C device
347292 <size>: the number of bytes to read
348293 """
349 - bus = self._hexint(bus)
350 - slave = self._hexint(slave)
351 - addr = self._hexint(addr)
352 - size = self._hexint(size)
 294+ bus = to_int(bus)
 295+ slave = to_int(slave)
 296+ addr = to_int(addr)
 297+ size = to_int(size)
353298 data = self.emcore.i2cread(bus, slave, addr, size)
354299 bytes = struct.unpack("%dB" % len(data), data)
355300 self.logger.info("Data read from I2C:\n")
@@ -362,15 +307,15 @@
363308 <bus>: the bus index
364309 <slave>: the slave address
365310 <addr>: the start address on the I2C device
366 - <db1> ... <dbN>: the data in single bytes, encoded in hex,
 311+ <db1> ... <dbN>: the data in single bytes,
367312 seperated by whitespaces, eg. 37 5A 4F EB
368313 """
369 - bus = self._hexint(bus)
370 - slave = self._hexint(slave)
371 - addr = self._hexint(addr)
 314+ bus = to_int(bus)
 315+ slave = to_int(slave)
 316+ addr = to_int(addr)
372317 data = ""
373318 for arg in args:
374 - data += chr(self._hexint(arg))
 319+ data += chr(to_int(arg))
375320 self.logger.info("Writing data to I2C...\n")
376321 self.emcore.i2cwrite(bus, slave, addr, data)
377322 self.logger.info("done\n")
@@ -403,7 +348,7 @@
404349 Reads data continuously from one or more of the device's consoles.
405350 <bitmask>: the bitmask of the consoles to read from.
406351 """
407 - bitmask = self._hexint(bitmask)
 352+ bitmask = to_int(bitmask)
408353 while True:
409354 resp = self.emcore.cread()
410355 self.logger.write(resp.data)
@@ -415,7 +360,7 @@
416361 Writes the string <db1> ... <dbN> to one or more of the device's consoles.
417362 <bitmask>: the bitmask of the consoles to write to
418363 """
419 - bitmask = self._hexint(bitmask)
 364+ bitmask = to_int(bitmask)
420365 text = ""
421366 for word in args:
422367 text += word + " "
@@ -429,7 +374,7 @@
430375 flushes one or more of the device consoles' buffers.
431376 <bitmask>: the bitmask of the consoles to be flushed
432377 """
433 - bitmask = self._hexint(bitmask)
 378+ bitmask = to_int(bitmask)
434379 self.logger.info("Flushing consoles identified with the bitmask 0x%X\n" % bitmask)
435380 self.emcore.cflush(bitmask)
436381
@@ -494,7 +439,7 @@
495440 """
496441 Suspends the thread with the thread address <threadaddr>
497442 """
498 - threadaddr = self._hexint(threadaddr)
 443+ threadaddr = to_int(threadaddr)
499444 self.logger.info("Suspending the thread with the threadaddr 0x%X\n" % threadaddr)
500445 self.emcore.suspendthread(threadaddr)
501446
@@ -503,7 +448,7 @@
504449 """
505450 Resumes the thread with the thread address <threadaddr>
506451 """
507 - threadaddr = self._hexint(threadaddr)
 452+ threadaddr = to_int(threadaddr)
508453 self.logger.info("Resuming the thread with the threadaddr 0x%X\n" % threadaddr)
509454 self.emcore.resumethread(threadaddr)
510455
@@ -512,7 +457,7 @@
513458 """
514459 Kills the thread with the thread address <threadaddr>
515460 """
516 - threadaddr = self._hexint(threadaddr)
 461+ threadaddr = to_int(threadaddr)
517462 self.logger.info("Killing the thread with the threadaddr 0x%X\n" % threadaddr)
518463 self.emcore.killthread(threadaddr)
519464
@@ -528,11 +473,11 @@
529474 <priority>: the priority of the thread, from 1 to 255
530475 <state>: the thread's initial state, valid are: 1 => ready, 0 => suspended
531476 """
532 - nameptr = self._hexint(nameptr)
533 - entrypoint = self._hexint(entrypoint)
534 - stackptr = self._hexint(stackptr)
535 - stacksize = self._hexint(stacksize)
536 - priority = self._hexint(priority)
 477+ nameptr = to_int(nameptr)
 478+ entrypoint = to_int(entrypoint)
 479+ stackptr = to_int(stackptr)
 480+ stacksize = to_int(stacksize)
 481+ priority = to_int(priority)
537482 data = self.emcore.createthread(nameptr, entrypoint, stackptr, stacksize, threadtype, priority, state)
538483 name = self.emcore.readstring(nameptr)
539484 self.logger.info("Created a thread with the thread pointer 0x%X, the name \"%s\", the entrypoint at 0x%X," \
@@ -558,7 +503,7 @@
559504 """
560505 Executes the emCORE application at <addr>.
561506 """
562 - addr = self._hexint(addr)
 507+ addr = to_int(addr)
563508 self.logger.info("Starting emCORE app at 0x%X\n" % addr)
564509 self.emcore.execimage(addr)
565510
@@ -578,9 +523,9 @@
579524 <addr_bootflsh>: the address in bootflash to read from
580525 <addr_mem>: the address in memory to copy the data to
581526 """
582 - addr_flash = self._hexint(addr_flash)
583 - addr_mem = self._hexint(addr_mem)
584 - size = self._hexint(size)
 527+ addr_flash = to_int(addr_flash)
 528+ addr_mem = to_int(addr_mem)
 529+ size = to_int(size)
585530 self.logger.info("Dumping boot flash from 0x%X - 0x%X to 0x%X - 0x%X\n" %
586531 (addr_flash, addr_flash + size, addr_mem, addr_mem + size))
587532 self.emcore.bootflashread(addr_mem, addr_flash, size)
@@ -595,10 +540,10 @@
596541 <addr_bootflsh>: the address in bootflash to write to
597542 [force]: Use this flag to suppress the 5 seconds delay
598543 """
599 - addr_flash = self._hexint(addr_flash)
600 - addr_mem = self._hexint(addr_mem)
601 - size = self._hexint(size)
602 - force = self._bool(force)
 544+ addr_flash = to_int(addr_flash)
 545+ addr_mem = to_int(addr_mem)
 546+ size = to_int(size)
 547+ force = to_bool(force)
603548 self.logger.warn("Writing boot flash from the memory in 0x%X - 0x%X to 0x%X - 0x%X\n" %
604549 (addr_mem, addr_mem + size, addr_flash, addr_flash + size))
605550 if force == False:
@@ -615,7 +560,7 @@
616561 Uploads the firmware in <filename>
617562 to an allocated buffer and executes it at <targetaddr>.
618563 """
619 - targetaddr = self._hexint(targetaddr)
 564+ targetaddr = to_int(targetaddr)
620565 addr, size = self.uploadfile(filename)
621566 self.execfirmware(targetaddr, addr, size)
622567
@@ -624,8 +569,8 @@
625570 """
626571 Moves the firmware at <addr> with <size> to <targetaddr> and executes it
627572 """
628 - targetaddr = self._hexint(targetaddr)
629 - addr = self._hexint(addr)
 573+ targetaddr = to_int(targetaddr)
 574+ addr = to_int(addr)
630575 size = self._hexint(size)
631576 self.logger.info("Running firmware at "+self._hex(targetaddr)+". Bye.\n")
632577 self.emcore.execfirmware(targetaddr, addr, size)
@@ -638,9 +583,9 @@
639584 <size>: the size of the buffer
640585 <keyindex>: the index of the key in the crypto unit
641586 """
642 - addr = self._hexint(addr)
643 - size = self._hexint(size)
644 - keyindex = self._hexint(keyindex)
 587+ addr = to_int(addr)
 588+ size = to_int(size)
 589+ keyindex = to_int(keyindex)
645590 self.emcore.aesencrypt(addr, size, keyindex)
646591
647592 @command
@@ -651,9 +596,9 @@
652597 <size>: the size of the buffer
653598 <keyindex>: the index of the key in the crypto unit
654599 """
655 - addr = self._hexint(addr)
656 - size = self._hexint(size)
657 - keyindex = self._hexint(keyindex)
 600+ addr = to_int(addr)
 601+ size = to_int(size)
 602+ keyindex = to_int(keyindex)
658603 self.emcore.aesdecrypt(addr, size, keyindex)
659604
660605 @command
@@ -664,9 +609,9 @@
665610 <size>: the size of the buffer
666611 <destination>: the location where the key will be stored
667612 """
668 - addr = self._hexint(addr)
669 - size = self._hexint(size)
670 - destination = self._hexint(destination)
 613+ addr = to_int(addr)
 614+ size = to_int(size)
 615+ destination = to_int(destination)
671616 sha1size = 0x14
672617 self.logger.info("Generating hmac-sha1 hash from the buffer at 0x%X with the size 0x%X and saving it to 0x%X - 0x%X\n" %
673618 (addr, size, destination, destination + sha1size))
@@ -700,11 +645,11 @@
701646 [doecc]: use ecc error correction data
702647 [checkempty]: set statusflags if pages are empty
703648 """
704 - addr = self._hexint(addr)
705 - start = self._hexint(start)
706 - count = self._hexint(count)
707 - doecc = self._bool(doecc)
708 - checkempty = self._bool(checkempty)
 649+ addr = to_int(addr)
 650+ start = to_int(start)
 651+ count = to_int(count)
 652+ doecc = to_bool(doecc)
 653+ checkempty = to_bool(checkempty)
709654 self.logger.info("Reading 0x%X NAND pages starting at 0x%X to memory at 0x%X..." %
710655 (count, start, addr))
711656 self.emcore.ipodnano2g_nandread(addr, start, count, doecc, checkempty)
@@ -720,10 +665,10 @@
721666 <count>: block count
722667 [doecc]: create ecc error correction data
723668 """
724 - addr = self._hexint(addr)
725 - start = self._hexint(start)
726 - count = self._hexint(count)
727 - doecc = self._bool(doecc)
 669+ addr = to_int(addr)
 670+ start = to_int(start)
 671+ count = to_int(count)
 672+ doecc = to_bool(doecc)
728673 self.logger.info("Writing 0x%X NAND pages starting at 0x%X from memory at 0x%X..." %
729674 (count, start, addr))
730675 self.emcore.ipodnano2g_nandwrite(addr, start, count, doecc)
@@ -738,9 +683,9 @@
739684 <start>: start block
740685 <count>: block count
741686 """
742 - addr = self._hexint(addr)
743 - start = self._hexint(start)
744 - count = self._hexint(count)
 687+ addr = to_int(addr)
 688+ start = to_int(start)
 689+ count = to_int(count)
745690 self.logger.info("Erasing 0x%X NAND pages starting at 0x%X and logging to 0x%X..." %
746691 (count, start, addr))
747692 self.emcore.ipodnano2g_nanderase(addr, start, count)
@@ -814,7 +759,7 @@
815760 Uploads the bad block table <filename> to
816761 memory at <tempaddr> and writes it to the hard disk
817762 """
818 - tempaddr = self._hexint(tempaddr)
 763+ tempaddr = to_int(tempaddr)
819764 try:
820765 f = open(filename, 'rb')
821766 except IOError:
@@ -830,7 +775,7 @@
831776 Gathers some information about a storage volume used
832777 <volume>: volume id
833778 """
834 - volume = self._hexint(volume)
 779+ volume = to_int(volume)
835780 data = self.emcore.storage_get_info(volume)
836781 self.logger.info("Sector size: %d\n" % data["sectorsize"])
837782 self.logger.info("Number of sectors: %d\n" % data["numsectors"])
@@ -843,10 +788,10 @@
844789 """
845790 Reads <count> sectors starting at <sector> from storage <volume> to memory at <addr>.
846791 """
847 - volume = self._hexint(volume)
848 - sector = self._hexint(sector)
849 - count = self._hexint(count)
850 - addr = self._hexint(addr)
 792+ volume = to_int(volume)
 793+ sector = to_int(sector)
 794+ count = to_int(count)
 795+ addr = to_int(addr)
851796 self.logger.info("Reading volume %s sectors %X - %X to %08X..." % (volume, sector, sector + count - 1, addr))
852797 self.emcore.storage_read_sectors_md(volume, sector, count, addr)
853798 self.logger.info("done\n")
@@ -856,10 +801,10 @@
857802 """
858803 Writes memory contents at <addr> to <count> sectors starting at <sector> on storage <volume>.
859804 """
860 - volume = self._hexint(volume)
861 - sector = self._hexint(sector)
862 - count = self._hexint(count)
863 - addr = self._hexint(addr)
 805+ volume = to_int(volume)
 806+ sector = to_int(sector)
 807+ count = to_int(count)
 808+ addr = to_int(addr)
864809 self.logger.info("Writing %08X to volume %s sectors %X - %X..." % (addr, volume, sector, sector + count - 1))
865810 self.emcore.storage_write_sectors_md(volume, sector, count, addr)
866811 self.logger.info("done\n")
@@ -870,10 +815,10 @@
871816 Reads <count> sectors starting at <sector> from storage <volume> to file <file>,
872817 buffering them in memory at [buffer] in chunks of [buffsize] bytes (both optional).
873818 """
874 - volume = self._hexint(volume)
875 - sector = self._hexint(sector)
876 - count = self._hexint(count)
877 - buffsize = self._hexint(buffsize)
 819+ volume = to_int(volume)
 820+ sector = to_int(sector)
 821+ count = to_int(count)
 822+ buffsize = to_int(buffsize)
878823 try:
879824 f = open(file, 'wb')
880825 except IOError:
@@ -885,7 +830,7 @@
886831 buffer = self.emcore.malloc(buffsize)
887832 malloc = True
888833 else:
889 - buffer = self._hexint(buffer)
 834+ buffer = to_int(buffer)
890835 malloc = False
891836 try:
892837 self.logger.info("Reading volume %s sectors %X - %X to %s..." % (volume, sector, sector + count - 1, file))
@@ -908,10 +853,10 @@
909854 Writes contents of <file> to <count> sectors starting at <sector> on storage <volume>,
910855 buffering them in memory at [buffer] in chunks of [buffsize] bytes (both optional).
911856 """
912 - volume = self._hexint(volume)
913 - sector = self._hexint(sector)
914 - count = self._hexint(count)
915 - buffsize = self._hexint(buffsize)
 857+ volume = to_int(volume)
 858+ sector = to_int(sector)
 859+ count = to_int(count)
 860+ buffsize = to_int(buffsize)
916861 try:
917862 f = open(file, 'rb')
918863 except IOError:
@@ -923,7 +868,7 @@
924869 buffer = self.emcore.malloc(buffsize)
925870 malloc = True
926871 else:
927 - buffer = self._hexint(buffer)
 872+ buffer = to_int(buffer)
928873 malloc = False
929874 try:
930875 self.logger.info("Writing %s to volume %s sectors %X - %X..." % (file, volume, sector, sector + count - 1))
@@ -1007,7 +952,7 @@
1008953 [buffsize]: buffer size (optional)
1009954 [buffer]: buffer address (optional)
1010955 """
1011 - buffsize = self._hexint(buffsize)
 956+ buffsize = to_int(buffsize)
1012957 try:
1013958 f = open(localname, 'wb')
1014959 except IOError:
@@ -1021,7 +966,7 @@
1022967 buffer = self.emcore.malloc(buffsize)
1023968 malloc = True
1024969 else:
1025 - buffer = self._hexint(buffer)
 970+ buffer = to_int(buffer)
1026971 malloc = False
1027972 try:
1028973 self.logger.info("Downloading file %s to %s..." % (remotename, localname))
@@ -1047,7 +992,7 @@
1048993 [buffsize]: buffer size (optional)
1049994 [buffer]: buffer address (optional)
1050995 """
1051 - buffsize = self._hexint(buffsize)
 996+ buffsize = to_int(buffsize)
1052997 handle = self.emcore.dir_open(remotepath)
1053998 try:
1054999 if buffer is None:
@@ -1054,7 +999,7 @@
10551000 buffer = self.emcore.malloc(buffsize)
10561001 malloc = True
10571002 else:
1058 - buffer = self._hexint(buffer)
 1003+ buffer = to_int(buffer)
10591004 malloc = False
10601005 try:
10611006 try: os.mkdir(localpath)
@@ -1082,7 +1027,7 @@
10831028 [buffsize]: buffer size (optional)
10841029 [buffer]: buffer address (optional)
10851030 """
1086 - buffsize = self._hexint(buffsize)
 1031+ buffsize = to_int(buffsize)
10871032 try:
10881033 f = open(localname, 'rb')
10891034 except IOError:
@@ -1093,7 +1038,7 @@
10941039 buffer = self.emcore.malloc(buffsize)
10951040 malloc = True
10961041 else:
1097 - buffer = self._hexint(buffer)
 1042+ buffer = to_int(buffer)
10981043 malloc = False
10991044 try:
11001045 self.logger.info("Uploading file %s to %s..." % (localname, remotename))
@@ -1124,12 +1069,12 @@
11251070 [buffsize]: buffer size (optional)
11261071 [buffer]: buffer address (optional)
11271072 """
1128 - buffsize = self._hexint(buffsize)
 1073+ buffsize = to_int(buffsize)
11291074 if buffer is None:
11301075 buffer = self.emcore.malloc(buffsize)
11311076 malloc = True
11321077 else:
1133 - buffer = self._hexint(buffer)
 1078+ buffer = to_int(buffer)
11341079 malloc = False
11351080 try:
11361081 try: self.mkdir(remotepath)
@@ -1185,7 +1130,7 @@
11861131 @command
11871132 def malloc(self, size):
11881133 """ Allocates <size> bytes and returns a pointer to the allocated memory """
1189 - size = self._hexint(size)
 1134+ size = to_int(size)
11901135 self.logger.info("Allocating %d bytes of memory\n" % size)
11911136 addr = self.emcore.malloc(size)
11921137 self.logger.info("Allocated %d bytes of memory at 0x%X\n" % (size, addr))
@@ -1193,8 +1138,8 @@
11941139 @command
11951140 def memalign(self, align, size):
11961141 """ Allocates <size> bytes aligned to <align> and returns a pointer to the allocated memory """
1197 - align = self._hexint(align)
1198 - size = self._hexint(size)
 1142+ align = to_int(align)
 1143+ size = to_int(size)
11991144 self.logger.info("Allocating %d bytes of memory aligned to 0x%X\n" % (size, align))
12001145 addr = self.emcore.memalign(align, size)
12011146 self.logger.info("Allocated %d bytes of memory at 0x%X\n" % (size, addr))
@@ -1205,8 +1150,8 @@
12061151 expanding or reducing the amount of memory available in the block.
12071152 Returns a pointer to the reallocated memory.
12081153 """
1209 - ptr = self._hexint(ptr)
1210 - size = self._hexint(size)
 1154+ ptr = to_int(ptr)
 1155+ size = to_int(size)
12111156 self.logger.info("Reallocating 0x%X to have the new size %d\n" % (ptr, size))
12121157 addr = self.emcore.realloc(ptr, size)
12131158 self.logger.info("Reallocated memory at 0x%X to 0x%X with the new size %d\n" % (ptr, addr, size))
@@ -1214,8 +1159,8 @@
12151160 @command
12161161 def reownalloc(self, ptr, owner):
12171162 """ Changes the owner of the memory allocation <ptr> to the thread struct at addr <owner> """
1218 - ptr = self._hexint(ptr)
1219 - owner = self._hexint(owner)
 1163+ ptr = to_int(ptr)
 1164+ owner = to_int(owner)
12201165 self.logger.info("Changing owner of the memory region 0x%X to 0x%X\n" % (ptr, owner))
12211166 self.emcore.reownalloc(ptr, owner)
12221167 self.logger.info("Successfully changed owner of 0x%X to 0x%X\n" % (ptr, owner))
@@ -1223,7 +1168,7 @@
12241169 @command
12251170 def free(self, ptr):
12261171 """ Frees the memory space pointed to by 'ptr' """
1227 - ptr = self._hexint(ptr)
 1172+ ptr = to_int(ptr)
12281173 self.logger.info("Freeing the memory region at 0x%X\n" % ptr)
12291174 self.emcore.free(ptr)
12301175 self.logger.info("Successfully freed the memory region at 0x%X\n" % ptr)
Index: emcore/trunk/tools/misc.py
@@ -29,8 +29,29 @@
3030 import sys
3131 from ctypes import *
3232 from _ctypes import _SimpleCData
33 -import libemcoredata
3433
 34+
 35+class Error(Exception):
 36+ def __init__(self, value=None):
 37+ self.value = value
 38+ def __str__(self):
 39+ if self.value != None:
 40+ return repr(self.value)
 41+
 42+class ArgumentError(Error):
 43+ pass
 44+
 45+class ArgumentTypeError(Error):
 46+ def __init__(self, expected, seen=False):
 47+ self.expected = expected
 48+ self.seen = seen
 49+ def __str__(self):
 50+ if self.seen:
 51+ return "Expected %s but got %s" % (self.expected, self.seen)
 52+ else:
 53+ return "Expected %s, but saw something else" % self.expected
 54+
 55+
3556 class Logger(object):
3657 """
3758 Simple stdout/stderr/file logger.
@@ -203,20 +224,12 @@
204225 return string_at(addressof(self), sizeof(self))
205226
206227
207 -
208 -class Error(Exception):
209 - def __init__(self, value=None):
210 - self.value = value
211 - def __str__(self):
212 - if self.value != None:
213 - return repr(self.value)
214 -
215 -
216228 def gethwname(id):
217229 try:
218 - hwtype = libemcoredata.hwtypes[id]
 230+ from libemcoredata import hwtypes
 231+ hwtype = hwtypes[id]
219232 except KeyError:
220 - hwtype = "UNKNOWN (ID = " + self._hex(id) + ")"
 233+ hwtype = "UNKNOWN (ID = 0x%X)" % id
221234 return hwtype
222235
223236
@@ -315,4 +328,45 @@
316329 ret += logger.log(trimdoc(doc[function]['documentation']) + "\n", indent = 2 * indentwidth, target = logtarget)
317330 ret += logger.log("\"\"\"\n", indent = indentwidth, target = logtarget)
318331 ret += logger.log("\n", target = logtarget)
319 - return ret
\ No newline at end of file
 332+ return ret
 333+
 334+
 335+def to_bool(something):
 336+ """
 337+ Converts quite everything into bool.
 338+ """
 339+ if type(something) == bool:
 340+ return something
 341+ if something is None:
 342+ return False
 343+ elif type(something) == int or type(something) == long:
 344+ return bool(something)
 345+ elif type(something == str):
 346+ if something.lower() in ['true', '1', 't', 'y', 'yes']:
 347+ return True
 348+ elif something.lower() in ['false', '0', 'f', 'n', 'no']:
 349+ return False
 350+ raise ArgumentTypeError("bool", "'%s'" % something)
 351+
 352+def to_int(something):
 353+ """
 354+ Converts quite everything to a hexadecimal represented integer.
 355+ This works for default arguments too, because it returns
 356+ None when it found that it got a NoneType object.
 357+ """
 358+ if type(something) == int or type(something) == long:
 359+ return something
 360+ elif something is None:
 361+ return None
 362+ elif type(something) == str:
 363+ try:
 364+ if something[:2] == "0x": # Hexadecimal notation
 365+ return int(something[2:], 16)
 366+ elif something[:2] == "0b": # Binary notation
 367+ return int(something[2:], 2)
 368+ else: # Decimal notation
 369+ return int(something, 10)
 370+ except ValueError:
 371+ raise ArgumentTypeError("integer", "'%s'" % something)
 372+ else:
 373+ raise ArgumentTypeError("integer", "'%s'" % something)
\ No newline at end of file
Index: emcore/trunk/tools/emcoreldr.py
@@ -26,7 +26,9 @@
2727 import time
2828 import libemcoreldr
2929
 30+from misc import to_int
3031
 32+
3133 def usage():
3234 print ""
3335 print "Please provide a command and (if needed) parameters as command line arguments"
@@ -50,7 +52,7 @@
5153 print " Loads the specified file to 0x08000000 (SDRAM) and executes it."
5254 print " This is what you usually want to do."
5355 print ""
54 - print "All numbers are hexadecimal!"
 56+ print "All numbers can be provided as either hex (0x prefix), binary (0b prefix) or decimal (no prefix)"
5557 exit(2)
5658
5759
@@ -63,11 +65,11 @@
6466
6567 elif argv[1] == "download":
6668 if len(argv) != 5: usage()
67 - dev.download(int(argv[2], 16), int(argv[3], 16), argv[4])
 69+ dev.download(to_int(argv[2]), to_int(argv[3]), argv[4])
6870
6971 elif argv[1] == "execute":
7072 if len(argv) != 4: usage()
71 - dev.execute(int(argv[2], 16), int(argv[3], 16))
 73+ dev.execute(to_int(argv[2]), to_int(argv[3]))
7274
7375 elif argv[1] == "run":
7476 if len(argv) != 3: usage()
Index: emcore/trunk/tools/libemcore.py
@@ -32,12 +32,9 @@
3333 import usb.core
3434
3535 from libemcoredata import *
36 -from misc import Logger, Bunch, Error, gethwname
 36+from misc import Logger, Bunch, Error, ArgumentError, gethwname
3737 from functools import wraps
3838
39 -class ArgumentError(Error):
40 - pass
41 -
4239 class DeviceNotFoundError(Error):
4340 pass
4441