| Index: emcore/trunk/tools/emcore.py |
| — | — | @@ -36,26 +36,9 @@ |
| 37 | 37 |
|
| 38 | 38 | import libemcore
|
| 39 | 39 | import libemcoredata
|
| 40 | | -from misc import Error, Logger, getfuncdoc, gethwname
|
| | 40 | +from misc import Error, ArgumentError, ArgumentTypeError, Logger, getfuncdoc, gethwname, to_bool, to_int
|
| 41 | 41 |
|
| 42 | 42 |
|
| 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 | | -
|
| 60 | 43 | def usage(errormsg=None, specific=False, docstring=True):
|
| 61 | 44 | """
|
| 62 | 45 | Prints the usage information.
|
| — | — | @@ -162,46 +145,8 @@ |
| 163 | 146 | self.logger.error("There is a problem with the USB connection.\n")
|
| 164 | 147 | else:
|
| 165 | 148 | usage("No such command!", docstring = False)
|
| | 149 | +
|
| 166 | 150 |
|
| 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 | | -
|
| 206 | 151 | @command
|
| 207 | 152 | def help(self):
|
| 208 | 153 | """ Displays this help """
|
| — | — | @@ -246,7 +191,7 @@ |
| 247 | 192 | If [force] is 1, the reset will be forced, otherwise it will be gracefully,
|
| 248 | 193 | which may take some time.
|
| 249 | 194 | """
|
| 250 | | - force = self._bool(force)
|
| | 195 | + force = to_bool(force)
|
| 251 | 196 | if force: self.logger.info("Resetting forcefully...\n")
|
| 252 | 197 | else: self.logger.info("Resetting...\n")
|
| 253 | 198 | self.emcore.reset(force)
|
| — | — | @@ -258,7 +203,7 @@ |
| 259 | 204 | If [force] is 1, the poweroff will be forced, otherwise it will be gracefully,
|
| 260 | 205 | which may take some time.
|
| 261 | 206 | """
|
| 262 | | - force = self._bool(force)
|
| | 207 | + force = to_bool(force)
|
| 263 | 208 | if force: self.logger.info("Powering off forcefully...\n")
|
| 264 | 209 | else: self.logger.info("Powering off...\n")
|
| 265 | 210 | self.emcore.poweroff(force)
|
| — | — | @@ -270,7 +215,7 @@ |
| 271 | 216 | <filename>: The path to the file
|
| 272 | 217 | [addr]: The address to upload the file to. Allocates a chunk of memory if not given.
|
| 273 | 218 | """
|
| 274 | | - addr = self._hexint(addr)
|
| | 219 | + addr = to_int(addr)
|
| 275 | 220 | try:
|
| 276 | 221 | f = open(filename, 'rb')
|
| 277 | 222 | except IOError:
|
| — | — | @@ -297,8 +242,8 @@ |
| 298 | 243 | <size>: the number of bytes to be read
|
| 299 | 244 | <filename>: the path to the file
|
| 300 | 245 | """
|
| 301 | | - addr = self._hexint(addr)
|
| 302 | | - size = self._hexint(size)
|
| | 246 | + addr = to_int(addr)
|
| | 247 | + size = to_int(size)
|
| 303 | 248 | try:
|
| 304 | 249 | f = open(filename, 'wb')
|
| 305 | 250 | except IOError:
|
| — | — | @@ -317,8 +262,8 @@ |
| 318 | 263 | <addr>: the address to upload the integer to
|
| 319 | 264 | <integer>: the integer to upload
|
| 320 | 265 | """
|
| 321 | | - addr = self._hexint(addr)
|
| 322 | | - integer = self._hexint(integer)
|
| | 266 | + addr = to_int(addr)
|
| | 267 | + integer = to_int(integer)
|
| 323 | 268 | if integer > 0xFFFFFFFF:
|
| 324 | 269 | raise ArgumentError("Specified integer too long")
|
| 325 | 270 | data = struct.pack("I", integer)
|
| — | — | @@ -331,7 +276,7 @@ |
| 332 | 277 | Downloads a single integer from the device and prints it to the console window
|
| 333 | 278 | <addr>: the address to download the integer from
|
| 334 | 279 | """
|
| 335 | | - addr = self._hexint(addr)
|
| | 280 | + addr = to_int(addr)
|
| 336 | 281 | data = self.emcore.read(addr, 4)
|
| 337 | 282 | integer = struct.unpack("I", data)[0]
|
| 338 | 283 | self.logger.info("Read '0x%X' from address 0x%X\n" % (integer, addr))
|
| — | — | @@ -345,10 +290,10 @@ |
| 346 | 291 | <addr>: the start address on the I2C device
|
| 347 | 292 | <size>: the number of bytes to read
|
| 348 | 293 | """
|
| 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)
|
| 353 | 298 | data = self.emcore.i2cread(bus, slave, addr, size)
|
| 354 | 299 | bytes = struct.unpack("%dB" % len(data), data)
|
| 355 | 300 | self.logger.info("Data read from I2C:\n")
|
| — | — | @@ -362,15 +307,15 @@ |
| 363 | 308 | <bus>: the bus index
|
| 364 | 309 | <slave>: the slave address
|
| 365 | 310 | <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,
|
| 367 | 312 | seperated by whitespaces, eg. 37 5A 4F EB
|
| 368 | 313 | """
|
| 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)
|
| 372 | 317 | data = ""
|
| 373 | 318 | for arg in args:
|
| 374 | | - data += chr(self._hexint(arg))
|
| | 319 | + data += chr(to_int(arg))
|
| 375 | 320 | self.logger.info("Writing data to I2C...\n")
|
| 376 | 321 | self.emcore.i2cwrite(bus, slave, addr, data)
|
| 377 | 322 | self.logger.info("done\n")
|
| — | — | @@ -403,7 +348,7 @@ |
| 404 | 349 | Reads data continuously from one or more of the device's consoles.
|
| 405 | 350 | <bitmask>: the bitmask of the consoles to read from.
|
| 406 | 351 | """
|
| 407 | | - bitmask = self._hexint(bitmask)
|
| | 352 | + bitmask = to_int(bitmask)
|
| 408 | 353 | while True:
|
| 409 | 354 | resp = self.emcore.cread()
|
| 410 | 355 | self.logger.write(resp.data)
|
| — | — | @@ -415,7 +360,7 @@ |
| 416 | 361 | Writes the string <db1> ... <dbN> to one or more of the device's consoles.
|
| 417 | 362 | <bitmask>: the bitmask of the consoles to write to
|
| 418 | 363 | """
|
| 419 | | - bitmask = self._hexint(bitmask)
|
| | 364 | + bitmask = to_int(bitmask)
|
| 420 | 365 | text = ""
|
| 421 | 366 | for word in args:
|
| 422 | 367 | text += word + " "
|
| — | — | @@ -429,7 +374,7 @@ |
| 430 | 375 | flushes one or more of the device consoles' buffers.
|
| 431 | 376 | <bitmask>: the bitmask of the consoles to be flushed
|
| 432 | 377 | """
|
| 433 | | - bitmask = self._hexint(bitmask)
|
| | 378 | + bitmask = to_int(bitmask)
|
| 434 | 379 | self.logger.info("Flushing consoles identified with the bitmask 0x%X\n" % bitmask)
|
| 435 | 380 | self.emcore.cflush(bitmask)
|
| 436 | 381 |
|
| — | — | @@ -494,7 +439,7 @@ |
| 495 | 440 | """
|
| 496 | 441 | Suspends the thread with the thread address <threadaddr>
|
| 497 | 442 | """
|
| 498 | | - threadaddr = self._hexint(threadaddr)
|
| | 443 | + threadaddr = to_int(threadaddr)
|
| 499 | 444 | self.logger.info("Suspending the thread with the threadaddr 0x%X\n" % threadaddr)
|
| 500 | 445 | self.emcore.suspendthread(threadaddr)
|
| 501 | 446 |
|
| — | — | @@ -503,7 +448,7 @@ |
| 504 | 449 | """
|
| 505 | 450 | Resumes the thread with the thread address <threadaddr>
|
| 506 | 451 | """
|
| 507 | | - threadaddr = self._hexint(threadaddr)
|
| | 452 | + threadaddr = to_int(threadaddr)
|
| 508 | 453 | self.logger.info("Resuming the thread with the threadaddr 0x%X\n" % threadaddr)
|
| 509 | 454 | self.emcore.resumethread(threadaddr)
|
| 510 | 455 |
|
| — | — | @@ -512,7 +457,7 @@ |
| 513 | 458 | """
|
| 514 | 459 | Kills the thread with the thread address <threadaddr>
|
| 515 | 460 | """
|
| 516 | | - threadaddr = self._hexint(threadaddr)
|
| | 461 | + threadaddr = to_int(threadaddr)
|
| 517 | 462 | self.logger.info("Killing the thread with the threadaddr 0x%X\n" % threadaddr)
|
| 518 | 463 | self.emcore.killthread(threadaddr)
|
| 519 | 464 |
|
| — | — | @@ -528,11 +473,11 @@ |
| 529 | 474 | <priority>: the priority of the thread, from 1 to 255
|
| 530 | 475 | <state>: the thread's initial state, valid are: 1 => ready, 0 => suspended
|
| 531 | 476 | """
|
| 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)
|
| 537 | 482 | data = self.emcore.createthread(nameptr, entrypoint, stackptr, stacksize, threadtype, priority, state)
|
| 538 | 483 | name = self.emcore.readstring(nameptr)
|
| 539 | 484 | self.logger.info("Created a thread with the thread pointer 0x%X, the name \"%s\", the entrypoint at 0x%X," \
|
| — | — | @@ -558,7 +503,7 @@ |
| 559 | 504 | """
|
| 560 | 505 | Executes the emCORE application at <addr>.
|
| 561 | 506 | """
|
| 562 | | - addr = self._hexint(addr)
|
| | 507 | + addr = to_int(addr)
|
| 563 | 508 | self.logger.info("Starting emCORE app at 0x%X\n" % addr)
|
| 564 | 509 | self.emcore.execimage(addr)
|
| 565 | 510 |
|
| — | — | @@ -578,9 +523,9 @@ |
| 579 | 524 | <addr_bootflsh>: the address in bootflash to read from
|
| 580 | 525 | <addr_mem>: the address in memory to copy the data to
|
| 581 | 526 | """
|
| 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)
|
| 585 | 530 | self.logger.info("Dumping boot flash from 0x%X - 0x%X to 0x%X - 0x%X\n" %
|
| 586 | 531 | (addr_flash, addr_flash + size, addr_mem, addr_mem + size))
|
| 587 | 532 | self.emcore.bootflashread(addr_mem, addr_flash, size)
|
| — | — | @@ -595,10 +540,10 @@ |
| 596 | 541 | <addr_bootflsh>: the address in bootflash to write to
|
| 597 | 542 | [force]: Use this flag to suppress the 5 seconds delay
|
| 598 | 543 | """
|
| 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)
|
| 603 | 548 | self.logger.warn("Writing boot flash from the memory in 0x%X - 0x%X to 0x%X - 0x%X\n" %
|
| 604 | 549 | (addr_mem, addr_mem + size, addr_flash, addr_flash + size))
|
| 605 | 550 | if force == False:
|
| — | — | @@ -615,7 +560,7 @@ |
| 616 | 561 | Uploads the firmware in <filename>
|
| 617 | 562 | to an allocated buffer and executes it at <targetaddr>.
|
| 618 | 563 | """
|
| 619 | | - targetaddr = self._hexint(targetaddr)
|
| | 564 | + targetaddr = to_int(targetaddr)
|
| 620 | 565 | addr, size = self.uploadfile(filename)
|
| 621 | 566 | self.execfirmware(targetaddr, addr, size)
|
| 622 | 567 |
|
| — | — | @@ -624,8 +569,8 @@ |
| 625 | 570 | """
|
| 626 | 571 | Moves the firmware at <addr> with <size> to <targetaddr> and executes it
|
| 627 | 572 | """
|
| 628 | | - targetaddr = self._hexint(targetaddr)
|
| 629 | | - addr = self._hexint(addr)
|
| | 573 | + targetaddr = to_int(targetaddr)
|
| | 574 | + addr = to_int(addr)
|
| 630 | 575 | size = self._hexint(size)
|
| 631 | 576 | self.logger.info("Running firmware at "+self._hex(targetaddr)+". Bye.\n")
|
| 632 | 577 | self.emcore.execfirmware(targetaddr, addr, size)
|
| — | — | @@ -638,9 +583,9 @@ |
| 639 | 584 | <size>: the size of the buffer
|
| 640 | 585 | <keyindex>: the index of the key in the crypto unit
|
| 641 | 586 | """
|
| 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)
|
| 645 | 590 | self.emcore.aesencrypt(addr, size, keyindex)
|
| 646 | 591 |
|
| 647 | 592 | @command
|
| — | — | @@ -651,9 +596,9 @@ |
| 652 | 597 | <size>: the size of the buffer
|
| 653 | 598 | <keyindex>: the index of the key in the crypto unit
|
| 654 | 599 | """
|
| 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)
|
| 658 | 603 | self.emcore.aesdecrypt(addr, size, keyindex)
|
| 659 | 604 |
|
| 660 | 605 | @command
|
| — | — | @@ -664,9 +609,9 @@ |
| 665 | 610 | <size>: the size of the buffer
|
| 666 | 611 | <destination>: the location where the key will be stored
|
| 667 | 612 | """
|
| 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)
|
| 671 | 616 | sha1size = 0x14
|
| 672 | 617 | 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" %
|
| 673 | 618 | (addr, size, destination, destination + sha1size))
|
| — | — | @@ -700,11 +645,11 @@ |
| 701 | 646 | [doecc]: use ecc error correction data
|
| 702 | 647 | [checkempty]: set statusflags if pages are empty
|
| 703 | 648 | """
|
| 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)
|
| 709 | 654 | self.logger.info("Reading 0x%X NAND pages starting at 0x%X to memory at 0x%X..." %
|
| 710 | 655 | (count, start, addr))
|
| 711 | 656 | self.emcore.ipodnano2g_nandread(addr, start, count, doecc, checkempty)
|
| — | — | @@ -720,10 +665,10 @@ |
| 721 | 666 | <count>: block count
|
| 722 | 667 | [doecc]: create ecc error correction data
|
| 723 | 668 | """
|
| 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)
|
| 728 | 673 | self.logger.info("Writing 0x%X NAND pages starting at 0x%X from memory at 0x%X..." %
|
| 729 | 674 | (count, start, addr))
|
| 730 | 675 | self.emcore.ipodnano2g_nandwrite(addr, start, count, doecc)
|
| — | — | @@ -738,9 +683,9 @@ |
| 739 | 684 | <start>: start block
|
| 740 | 685 | <count>: block count
|
| 741 | 686 | """
|
| 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)
|
| 745 | 690 | self.logger.info("Erasing 0x%X NAND pages starting at 0x%X and logging to 0x%X..." %
|
| 746 | 691 | (count, start, addr))
|
| 747 | 692 | self.emcore.ipodnano2g_nanderase(addr, start, count)
|
| — | — | @@ -814,7 +759,7 @@ |
| 815 | 760 | Uploads the bad block table <filename> to
|
| 816 | 761 | memory at <tempaddr> and writes it to the hard disk
|
| 817 | 762 | """
|
| 818 | | - tempaddr = self._hexint(tempaddr)
|
| | 763 | + tempaddr = to_int(tempaddr)
|
| 819 | 764 | try:
|
| 820 | 765 | f = open(filename, 'rb')
|
| 821 | 766 | except IOError:
|
| — | — | @@ -830,7 +775,7 @@ |
| 831 | 776 | Gathers some information about a storage volume used
|
| 832 | 777 | <volume>: volume id
|
| 833 | 778 | """
|
| 834 | | - volume = self._hexint(volume)
|
| | 779 | + volume = to_int(volume)
|
| 835 | 780 | data = self.emcore.storage_get_info(volume)
|
| 836 | 781 | self.logger.info("Sector size: %d\n" % data["sectorsize"])
|
| 837 | 782 | self.logger.info("Number of sectors: %d\n" % data["numsectors"])
|
| — | — | @@ -843,10 +788,10 @@ |
| 844 | 789 | """
|
| 845 | 790 | Reads <count> sectors starting at <sector> from storage <volume> to memory at <addr>.
|
| 846 | 791 | """
|
| 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)
|
| 851 | 796 | self.logger.info("Reading volume %s sectors %X - %X to %08X..." % (volume, sector, sector + count - 1, addr))
|
| 852 | 797 | self.emcore.storage_read_sectors_md(volume, sector, count, addr)
|
| 853 | 798 | self.logger.info("done\n")
|
| — | — | @@ -856,10 +801,10 @@ |
| 857 | 802 | """
|
| 858 | 803 | Writes memory contents at <addr> to <count> sectors starting at <sector> on storage <volume>.
|
| 859 | 804 | """
|
| 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)
|
| 864 | 809 | self.logger.info("Writing %08X to volume %s sectors %X - %X..." % (addr, volume, sector, sector + count - 1))
|
| 865 | 810 | self.emcore.storage_write_sectors_md(volume, sector, count, addr)
|
| 866 | 811 | self.logger.info("done\n")
|
| — | — | @@ -870,10 +815,10 @@ |
| 871 | 816 | Reads <count> sectors starting at <sector> from storage <volume> to file <file>,
|
| 872 | 817 | buffering them in memory at [buffer] in chunks of [buffsize] bytes (both optional).
|
| 873 | 818 | """
|
| 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)
|
| 878 | 823 | try:
|
| 879 | 824 | f = open(file, 'wb')
|
| 880 | 825 | except IOError:
|
| — | — | @@ -885,7 +830,7 @@ |
| 886 | 831 | buffer = self.emcore.malloc(buffsize)
|
| 887 | 832 | malloc = True
|
| 888 | 833 | else:
|
| 889 | | - buffer = self._hexint(buffer)
|
| | 834 | + buffer = to_int(buffer)
|
| 890 | 835 | malloc = False
|
| 891 | 836 | try:
|
| 892 | 837 | self.logger.info("Reading volume %s sectors %X - %X to %s..." % (volume, sector, sector + count - 1, file))
|
| — | — | @@ -908,10 +853,10 @@ |
| 909 | 854 | Writes contents of <file> to <count> sectors starting at <sector> on storage <volume>,
|
| 910 | 855 | buffering them in memory at [buffer] in chunks of [buffsize] bytes (both optional).
|
| 911 | 856 | """
|
| 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)
|
| 916 | 861 | try:
|
| 917 | 862 | f = open(file, 'rb')
|
| 918 | 863 | except IOError:
|
| — | — | @@ -923,7 +868,7 @@ |
| 924 | 869 | buffer = self.emcore.malloc(buffsize)
|
| 925 | 870 | malloc = True
|
| 926 | 871 | else:
|
| 927 | | - buffer = self._hexint(buffer)
|
| | 872 | + buffer = to_int(buffer)
|
| 928 | 873 | malloc = False
|
| 929 | 874 | try:
|
| 930 | 875 | self.logger.info("Writing %s to volume %s sectors %X - %X..." % (file, volume, sector, sector + count - 1))
|
| — | — | @@ -1007,7 +952,7 @@ |
| 1008 | 953 | [buffsize]: buffer size (optional)
|
| 1009 | 954 | [buffer]: buffer address (optional)
|
| 1010 | 955 | """
|
| 1011 | | - buffsize = self._hexint(buffsize)
|
| | 956 | + buffsize = to_int(buffsize)
|
| 1012 | 957 | try:
|
| 1013 | 958 | f = open(localname, 'wb')
|
| 1014 | 959 | except IOError:
|
| — | — | @@ -1021,7 +966,7 @@ |
| 1022 | 967 | buffer = self.emcore.malloc(buffsize)
|
| 1023 | 968 | malloc = True
|
| 1024 | 969 | else:
|
| 1025 | | - buffer = self._hexint(buffer)
|
| | 970 | + buffer = to_int(buffer)
|
| 1026 | 971 | malloc = False
|
| 1027 | 972 | try:
|
| 1028 | 973 | self.logger.info("Downloading file %s to %s..." % (remotename, localname))
|
| — | — | @@ -1047,7 +992,7 @@ |
| 1048 | 993 | [buffsize]: buffer size (optional)
|
| 1049 | 994 | [buffer]: buffer address (optional)
|
| 1050 | 995 | """
|
| 1051 | | - buffsize = self._hexint(buffsize)
|
| | 996 | + buffsize = to_int(buffsize)
|
| 1052 | 997 | handle = self.emcore.dir_open(remotepath)
|
| 1053 | 998 | try:
|
| 1054 | 999 | if buffer is None:
|
| — | — | @@ -1054,7 +999,7 @@ |
| 1055 | 1000 | buffer = self.emcore.malloc(buffsize)
|
| 1056 | 1001 | malloc = True
|
| 1057 | 1002 | else:
|
| 1058 | | - buffer = self._hexint(buffer)
|
| | 1003 | + buffer = to_int(buffer)
|
| 1059 | 1004 | malloc = False
|
| 1060 | 1005 | try:
|
| 1061 | 1006 | try: os.mkdir(localpath)
|
| — | — | @@ -1082,7 +1027,7 @@ |
| 1083 | 1028 | [buffsize]: buffer size (optional)
|
| 1084 | 1029 | [buffer]: buffer address (optional)
|
| 1085 | 1030 | """
|
| 1086 | | - buffsize = self._hexint(buffsize)
|
| | 1031 | + buffsize = to_int(buffsize)
|
| 1087 | 1032 | try:
|
| 1088 | 1033 | f = open(localname, 'rb')
|
| 1089 | 1034 | except IOError:
|
| — | — | @@ -1093,7 +1038,7 @@ |
| 1094 | 1039 | buffer = self.emcore.malloc(buffsize)
|
| 1095 | 1040 | malloc = True
|
| 1096 | 1041 | else:
|
| 1097 | | - buffer = self._hexint(buffer)
|
| | 1042 | + buffer = to_int(buffer)
|
| 1098 | 1043 | malloc = False
|
| 1099 | 1044 | try:
|
| 1100 | 1045 | self.logger.info("Uploading file %s to %s..." % (localname, remotename))
|
| — | — | @@ -1124,12 +1069,12 @@ |
| 1125 | 1070 | [buffsize]: buffer size (optional)
|
| 1126 | 1071 | [buffer]: buffer address (optional)
|
| 1127 | 1072 | """
|
| 1128 | | - buffsize = self._hexint(buffsize)
|
| | 1073 | + buffsize = to_int(buffsize)
|
| 1129 | 1074 | if buffer is None:
|
| 1130 | 1075 | buffer = self.emcore.malloc(buffsize)
|
| 1131 | 1076 | malloc = True
|
| 1132 | 1077 | else:
|
| 1133 | | - buffer = self._hexint(buffer)
|
| | 1078 | + buffer = to_int(buffer)
|
| 1134 | 1079 | malloc = False
|
| 1135 | 1080 | try:
|
| 1136 | 1081 | try: self.mkdir(remotepath)
|
| — | — | @@ -1185,7 +1130,7 @@ |
| 1186 | 1131 | @command
|
| 1187 | 1132 | def malloc(self, size):
|
| 1188 | 1133 | """ Allocates <size> bytes and returns a pointer to the allocated memory """
|
| 1189 | | - size = self._hexint(size)
|
| | 1134 | + size = to_int(size)
|
| 1190 | 1135 | self.logger.info("Allocating %d bytes of memory\n" % size)
|
| 1191 | 1136 | addr = self.emcore.malloc(size)
|
| 1192 | 1137 | self.logger.info("Allocated %d bytes of memory at 0x%X\n" % (size, addr))
|
| — | — | @@ -1193,8 +1138,8 @@ |
| 1194 | 1139 | @command
|
| 1195 | 1140 | def memalign(self, align, size):
|
| 1196 | 1141 | """ 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)
|
| 1199 | 1144 | self.logger.info("Allocating %d bytes of memory aligned to 0x%X\n" % (size, align))
|
| 1200 | 1145 | addr = self.emcore.memalign(align, size)
|
| 1201 | 1146 | self.logger.info("Allocated %d bytes of memory at 0x%X\n" % (size, addr))
|
| — | — | @@ -1205,8 +1150,8 @@ |
| 1206 | 1151 | expanding or reducing the amount of memory available in the block.
|
| 1207 | 1152 | Returns a pointer to the reallocated memory.
|
| 1208 | 1153 | """
|
| 1209 | | - ptr = self._hexint(ptr)
|
| 1210 | | - size = self._hexint(size)
|
| | 1154 | + ptr = to_int(ptr)
|
| | 1155 | + size = to_int(size)
|
| 1211 | 1156 | self.logger.info("Reallocating 0x%X to have the new size %d\n" % (ptr, size))
|
| 1212 | 1157 | addr = self.emcore.realloc(ptr, size)
|
| 1213 | 1158 | self.logger.info("Reallocated memory at 0x%X to 0x%X with the new size %d\n" % (ptr, addr, size))
|
| — | — | @@ -1214,8 +1159,8 @@ |
| 1215 | 1160 | @command
|
| 1216 | 1161 | def reownalloc(self, ptr, owner):
|
| 1217 | 1162 | """ 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)
|
| 1220 | 1165 | self.logger.info("Changing owner of the memory region 0x%X to 0x%X\n" % (ptr, owner))
|
| 1221 | 1166 | self.emcore.reownalloc(ptr, owner)
|
| 1222 | 1167 | self.logger.info("Successfully changed owner of 0x%X to 0x%X\n" % (ptr, owner))
|
| — | — | @@ -1223,7 +1168,7 @@ |
| 1224 | 1169 | @command
|
| 1225 | 1170 | def free(self, ptr):
|
| 1226 | 1171 | """ Frees the memory space pointed to by 'ptr' """
|
| 1227 | | - ptr = self._hexint(ptr)
|
| | 1172 | + ptr = to_int(ptr)
|
| 1228 | 1173 | self.logger.info("Freeing the memory region at 0x%X\n" % ptr)
|
| 1229 | 1174 | self.emcore.free(ptr)
|
| 1230 | 1175 | self.logger.info("Successfully freed the memory region at 0x%X\n" % ptr)
|
| Index: emcore/trunk/tools/misc.py |
| — | — | @@ -29,8 +29,29 @@ |
| 30 | 30 | import sys
|
| 31 | 31 | from ctypes import *
|
| 32 | 32 | from _ctypes import _SimpleCData
|
| 33 | | -import libemcoredata
|
| 34 | 33 |
|
| | 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 | +
|
| 35 | 56 | class Logger(object):
|
| 36 | 57 | """
|
| 37 | 58 | Simple stdout/stderr/file logger.
|
| — | — | @@ -203,20 +224,12 @@ |
| 204 | 225 | return string_at(addressof(self), sizeof(self))
|
| 205 | 226 |
|
| 206 | 227 |
|
| 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 | | -
|
| 216 | 228 | def gethwname(id):
|
| 217 | 229 | try:
|
| 218 | | - hwtype = libemcoredata.hwtypes[id]
|
| | 230 | + from libemcoredata import hwtypes
|
| | 231 | + hwtype = hwtypes[id]
|
| 219 | 232 | except KeyError:
|
| 220 | | - hwtype = "UNKNOWN (ID = " + self._hex(id) + ")"
|
| | 233 | + hwtype = "UNKNOWN (ID = 0x%X)" % id
|
| 221 | 234 | return hwtype
|
| 222 | 235 |
|
| 223 | 236 |
|
| — | — | @@ -315,4 +328,45 @@ |
| 316 | 329 | ret += logger.log(trimdoc(doc[function]['documentation']) + "\n", indent = 2 * indentwidth, target = logtarget)
|
| 317 | 330 | ret += logger.log("\"\"\"\n", indent = indentwidth, target = logtarget)
|
| 318 | 331 | 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 @@ |
| 27 | 27 | import time
|
| 28 | 28 | import libemcoreldr
|
| 29 | 29 |
|
| | 30 | +from misc import to_int
|
| 30 | 31 |
|
| | 32 | +
|
| 31 | 33 | def usage():
|
| 32 | 34 | print ""
|
| 33 | 35 | print "Please provide a command and (if needed) parameters as command line arguments"
|
| — | — | @@ -50,7 +52,7 @@ |
| 51 | 53 | print " Loads the specified file to 0x08000000 (SDRAM) and executes it."
|
| 52 | 54 | print " This is what you usually want to do."
|
| 53 | 55 | 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)"
|
| 55 | 57 | exit(2)
|
| 56 | 58 |
|
| 57 | 59 |
|
| — | — | @@ -63,11 +65,11 @@ |
| 64 | 66 |
|
| 65 | 67 | elif argv[1] == "download":
|
| 66 | 68 | 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])
|
| 68 | 70 |
|
| 69 | 71 | elif argv[1] == "execute":
|
| 70 | 72 | 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]))
|
| 72 | 74 |
|
| 73 | 75 | elif argv[1] == "run":
|
| 74 | 76 | if len(argv) != 3: usage()
|
| Index: emcore/trunk/tools/libemcore.py |
| — | — | @@ -32,12 +32,9 @@ |
| 33 | 33 | import usb.core
|
| 34 | 34 |
|
| 35 | 35 | from libemcoredata import *
|
| 36 | | -from misc import Logger, Bunch, Error, gethwname
|
| | 36 | +from misc import Logger, Bunch, Error, ArgumentError, gethwname
|
| 37 | 37 | from functools import wraps
|
| 38 | 38 |
|
| 39 | | -class ArgumentError(Error):
|
| 40 | | - pass
|
| 41 | | -
|
| 42 | 39 | class DeviceNotFoundError(Error):
|
| 43 | 40 | pass
|
| 44 | 41 |
|