def program_flash(port, filename, hex_address): """Program the flash memory using the contents of the C256's RAM.""" base_address = int(hex_address, 16) address = base_address print("About to upload image to address 0x{:X}".format(address), flush=True) if os.path.getsize(filename) == FLASH_SIZE: if confirm("Are you sure you want to reprogram the flash memory? (y/n): "): with open(filename, "rb") as f: c256 = foenix.FoenixDebugPort() try: c256.open(port) c256.enter_debug() try: block = f.read(CHUNK_SIZE) while block: c256.write_block(address, block) address += len(block) block = f.read(CHUNK_SIZE) print("Binary file uploaded...", flush=True) c256.erase_flash() print("Flash memory erased...", flush=True) c256.program_flash(base_address) print("Flash memory programmed...") finally: c256.exit_debug() finally: c256.close() else: print("The provided flash file is not the right size.")
def revision(port): """Get the version code for the debug port.""" c256 = foenix.FoenixDebugPort() try: c256.open(port) c256.enter_debug() try: data = c256.get_revision() return "%X" % data finally: c256.exit_debug() finally: c256.close()
def get(port, address, length): """Read a block of data from the C256.""" c256 = foenix.FoenixDebugPort() try: c256.open(port) c256.enter_debug() try: data = c256.read_block(int(address, 16), int(length, 16)) display(int(address, 16), data) finally: c256.exit_debug() finally: c256.close()
def dereference(port, file, label): """Get the address contained in the pointer with the label in the label file.""" c256 = foenix.FoenixDebugPort() try: address = lookup(file, label) c256.open(port) c256.enter_debug() try: data = c256.read_block(int(address, 16), 3) deref = data[2] << 16 | data[1] << 8 | data[0] return "%X" % deref finally: c256.exit_debug() finally: c256.close()
def upload_binary(port, filename, address): """Upload a binary file into the C256 memory.""" with open(filename, "rb") as f: c256 = foenix.FoenixDebugPort() try: c256.open(port) c256.enter_debug() try: current_addr = int(address, 16) block = f.read(CHUNK_SIZE) while block: c256.write_block(current_addr, block) current_addr += len(block) block = f.read(CHUNK_SIZE) finally: c256.exit_debug() finally: c256.close()
def send(port, filename): """Send the data in the hex file 'filename' to the C256 on the given serial port.""" infile = intelhex.HexFile() c256 = foenix.FoenixDebugPort() try: c256.open(port) infile.open(filename) try: infile.set_handler(lambda address, data: c256.write_block(address, bytes.fromhex(data))) c256.enter_debug() try: # Process the lines in the hex file infile.read_lines() finally: c256.exit_debug() finally: infile.close() finally: c256.close()
def send_text(port, filename, destination): """Send the a text file to the fake file area on the C256 at $16:0000""" file_size = os.path.getsize(filename) if file_size > 65536: raise Exception("text file is too big") data_to_send = "" with open(filename, 'r') as f: data_to_send = f.read() c256 = foenix.FoenixDebugPort() try: c256.open(port) c256.enter_debug() try: size_list = [file_size & 0xFF, (file_size >> 8) & 0xFF] c256.write_block(destination, bytes(size_list)) c256.write_block(destination + 2, bytes(data_to_send, 'ascii')) finally: c256.exit_debug() finally: c256.close()