def main(): commands = {'scan': print_chain_info, 'svf': play_svf_file} # Set up a simple argument parser. parser = GreatFETArgumentParser( description="Utility for working with JTAG devices") parser.add_argument('command', choices=commands, help='the operation to complete') parser.add_argument('filename', metavar="[filename]", nargs='?', help='the filename to read from, for SVF playback') args = parser.parse_args() device = parser.find_specified_device() if args.command == 'scan': args.verbose = True elif args.filename == "-": args.verbose = False # Grab our log functions. log_function, log_error = parser.get_log_functions() # Execute the relevant command. command = commands[args.command] command(device.jtag, log_function, log_error, args)
def main(): from greatfet.utils import GreatFETArgumentParser supported_interfaces = {'spi': lambda d: d.spi, 'i2c': lambda d: d.i2c} supported_interface_string = ', '.join(supported_interfaces) # Set up a simple argument parser. parser = GreatFETArgumentParser( description="bus pirate emulation utility for GreatFET", verbose_by_default=True) parser.add_argument( 'interface', nargs='?', help= "The type of interface to use for our commands. Currently supported: {}" .format(supported_interface_string)) parser.add_argument('commands', nargs='*', default=[], help="Bus pirate command to execute.") args = parser.parse_args() log_function, log_error = parser.get_log_functions() device = parser.find_specified_device() # By default, if we have a command on the command line, run non-interactively. if args.commands: interface = supported_interfaces[args.interface](device) commands = ' '.join(args.commands) # Print the executed commands, and their results. log_function("> {}".format(commands)) run_batch(interface, commands, log_function) else: if args.interface: interface = supported_interfaces[args.interface](device) else: interface = None # FIXME: remove this when we support proper mode-switching log_error( "Mode-switching currently not supported from interactive mode. Specify a mode on the command line." ) sys.exit(-1) run_interactive(interface, print, args.commands)
def main(): # Simple type-arguments for parsing. int_from_msps = lambda x: from_eng_notation( x, units=['Hz', 'SPS'], to_type=int) int_from_eng = lambda x: from_eng_notation(x, to_type=int) # Set up our argument parser. parser = GreatFETArgumentParser( description="Logic analyzer implementation for GreatFET", verbose_by_default=True) parser.add_argument( 'command', choices=commands, help='the pattern shape to generate, or command to execute') parser.add_argument( '-n', '--samples', metavar='samples', type=int_from_eng, default=64, dest='samples', help='the number of samples to generate of the given pattern, up to 32K' ) parser.add_argument( '-w', '--width', metavar='bus_width', type=int, default=8, dest='bus_width', help='the width of the bus, in bits; up to 16 [default: 8]') parser.add_argument( '-f', '--samplerate', metavar='samples_per_second', type=int_from_msps, default=1000000, dest='sample_rate', help='samples to emit per second; up to 204MSPS [default: 1MSPS]') parser.add_argument( '--oneshot', dest='repeat', action='store_false', help='If provided, the given pattern will be shifted out only once.') parser.add_argument( '--debug-sgpio', dest='debug_sgpio', action='store_true', help= 'Developer option for debugging; dumps the SGPIO configuration after starting.' ) args = parser.parse_args() log_function, log_error = parser.get_log_functions() # Find our GreatFET. device = parser.find_specified_device() # Ensure the GreatFET supports our API. if not device.supports_api('pattern_generator'): log_error( "The connected GreatFET doesn't seem to support pattern generation. A firmware upgrade may help." ) # Create our pattern generator object. pattern_generator = PatternGenerator(device, sample_rate=args.sample_rate, bus_width=args.bus_width) # TODO: truncate things to the pattern generator object's limits # Figure out how the user wants their samples generated (or what the want done, in general). command_to_execute = commands[args.command] # Execute the core command, which usually generates samples. samples = command_to_execute(args, pattern_generator) # If the generator has generated samples, scan them out. if samples: pattern_generator.scan_out_pattern(samples, args.repeat) # If we've been asked to dump our SGPIO configuration, do so. if args.debug_sgpio: log_error(pattern_generator.dump_sgpio_config()) # Log what we've done to the user. if samples: log_function("Scanning out {} samples at {}.".format( len(samples), eng_notation(args.sample_rate, unit='SPS'))) log_function("Run '{} stop' to halt generation.".format(sys.argv[0])) log_function("")
def main(): """ Core command. """ global input_thread, termination_request, console parity_modes = { 'none': UART.PARITY_NONE, 'odd': UART.PARITY_ODD, 'even': UART.PARITY_EVEN, 'one': UART.PARITY_STUCK_AT_ONE, 'zero': UART.PARITY_STUCK_AT_ZERO } # Set up a simple argument parser. # TODO: support configurations such as '8n1' parser = GreatFETArgumentParser( description="Simple GreatFET UART monitor.") parser.add_argument( 'baud', nargs='?', type=from_eng_notation, default=115200, help="Baud rate; in symbols/second. Defaults to 115200.") parser.add_argument('-d', '--data', type=int, default=8, help="The number of data bits per frame.") parser.add_argument('-S', '--stop', type=int, default=1, help="The number of stop bits per frame.") parser.add_argument('-P', '--parity', choices=parity_modes, default=0, help="The type of parity to use.") parser.add_argument('-E', '--echo', action='store_true', help="If provided, local echo will be enabled.") parser.add_argument( '-N', '--no-newline-translation', action='store_false', dest='tr_newlines', help="Provide this option to disable newline translation.") args = parser.parse_args() device = parser.find_specified_device() # Grab our log functions. log_function, log_error = parser.get_log_functions() # Configure our UART. if not hasattr(device, 'uart'): log_error( "This device doesn't appear to support the UART API. Perhaps it needs a firmware upgrade?" ) sys.exit(-1) # Notify the user that we're entering monitor mode. log_function( "Entering monitor mode. To terminate, type CTRL+A, then CTRL+C.") # Create a console object. console = Console() console.setup() # Create a thread to capture input data into a locally-processed queue. input_queue = queue.Queue() termination_request = threading.Event() input_thread = threading.Thread(target=input_handler, args=(console, input_queue, termination_request)) input_thread.start() # Configure our UART parameters. device.uart.update_parameters(baud=args.baud, data_bits=args.data, stop_bits=args.stop, parity=args.parity) # Generate our UART monitor. while True: # Grab any data from the serial port, and print it to the screen. data = device.uart.read() # If we're preforming newline translation, prepend a "\r" to any newline. if args.tr_newlines and (data == b"\n"): console.write_bytes(b"\r") # Stick the UART data onscreen. console.write_bytes(data) # Grab any data from the user, and send it via serial. try: new_key = input_queue.get_nowait() handle_special_functions(new_key) # If local echo is on, print the character to our local console. if args.echo: sys.stdout.buffer.write(new_key) if args.tr_newlines and (new_key == b"\n"): device.uart.write(b"\r") device.uart.write(new_key) except queue.Empty: pass
def main(): commands = { 'info': print_flash_info, 'erase': erase_chip, 'read': dump_chip, 'write': program_chip } # Set up a simple argument parser. parser = GreatFETArgumentParser( description="Utility for programming and dumping SPI flash chips", verbose_by_default=True) parser.add_argument('command', choices=commands, help='the operation to complete') parser.add_argument( 'filename', metavar="[filename]", nargs='?', help='the filename to read or write to, for read/write operations') parser.add_argument('-a', '--address', metavar='<addr>', default=0, type=from_eng_notation, help="Starting offset in the given flash memory.") parser.add_argument('-l', '--length', metavar='<length>', type=from_eng_notation, default=None, help="number of bytes to read (default: flash size)") parser.add_argument( '-E', '--no-autoerase', action='store_false', dest='autoerase', help= "If provided, the target flash will not be erased before a write operation." ) parser.add_argument( '-C', '--cautious', '--cowardly', action='store_true', dest='cowardly', help="Refuses to do anything that might overwrite or lose chip data.") parser.add_argument( '-S', '--no-spdf', dest='autodetect', action='store_false', help="Don't attempt to use SPDF to autodetect flash parameters.") parser.add_argument( '-R', '--require-spdf', dest='allow_fallback', action='store_false', help="Only use SPDF; ignore any argument provided as fall-back options." ) parser.add_argument( '-T', '--auto-truncate', dest='truncate', action='store_true', help= "If provided, any read operations will truncate trailing unprogrammed words (0xFFs)." ) parser.add_argument( '--page-size', metavar='<bytes>', type=int, default=256, help= "manually specify the page size of the target flash; for use with -S") parser.add_argument( '--flash-size', metavar='<bytes>', type=int, default=8192, help= "manually specify the capacity of the target flash; for use with -S") parser.add_argument( '-J', '--allow-null-jedec-id', dest='bypass_jedec', action='store_true', help= "Allow the device to work even if it doesn't appear to support a JEDEC ID." ) args = parser.parse_args() device = parser.find_specified_device() if args.command == 'info': args.verbose = True elif args.filename == "-": args.verbose = False # Grab our log functions. log_function, log_error = parser.get_log_functions() try: # TODO: use a GreatFET method to automatically instantiate spi_flash # Figure out the "override" page and flash size for any arguments provided. # If autodetection is enabled and works, these aren't used. maximum_address = args.flash_size - 1 num_pages = (args.flash_size + (args.page_size - 1)) // args.page_size # Create a SPI flash object. spi_flash = device.create_programmer( 'spi_flash', args.autodetect, args.allow_fallback, page_size=args.page_size, maximum_address=maximum_address, allow_null_jedec=args.bypass_jedec) # If we have a device that's ancient enough to not speak JEDEC, notify the user. Pithily. if args.bypass_jedec and spi_flash.manufacturer_id == 0xff and spi_flash.part_id == 0xffff: log_function( "I... really can't see an SPI flash here. I'll trust you.") except CommandFailureError: log_error("This device doesn't appear to support identifying itself.") log_error(" You'll need to specify the page and flash size manually.") sys.exit(-1) except IOError as e: log_error(str(e)) log_error("If you believe you have a device properly connected, you") log_error(" may want to try again with --allow-null-jedec-id.") log_error("") sys.exit(-2) command = commands[args.command] command(spi_flash, log_function, log_error, args)