#!/usr/bin/env python3 """ Abstraction layer for simple control circuits. The simple control circuit can control the reset signal via GPIO.0 and the maskrom signal via GPIO.1. """ __copyright__ = 'Copyright (C) 2019 Theobroma Systems Design und Consulting GmbH' __license__ = 'MIT' import sys import time import command_processor import cp210x_controller # Power supply GPIO GPIO_RESET = 0 VALUE_RESET_DEASSERT = 0 VALUE_RESET_ASSERT = 1 # Maskrom (BIOS disable) GPIO GPIO_MASKROM = 1 VALUE_MASKROM_DEASSERT = 0 VALUE_MASKROM_ASSERT = 1 def invert_reset(): global VALUE_RESET_DEASSERT global VALUE_RESET_ASSERT VALUE_RESET_DEASSERT = 1 VALUE_RESET_ASSERT = 0 def invert_maskrom(): global VALUE_MASKROM_DEASSERT global VALUE_MASKROM_ASSERT VALUE_MASKROM_DEASSERT = 1 VALUE_MASKROM_ASSERT = 0 def do_status(dev): resetstate = cp210x_controller.get_gpio(dev, GPIO_RESET) bootmode = cp210x_controller.get_gpio(dev, GPIO_MASKROM) print("Board is {} ({})".format( ("running" if resetstate == VALUE_RESET_DEASSERT else "in reset"), ("normal boot" if bootmode == VALUE_MASKROM_DEASSERT else "boot to maskrom"))) def do_reset_assert(dev): cp210x_controller.set_gpio(dev, GPIO_RESET, VALUE_RESET_ASSERT) def do_reset_deassert(dev): cp210x_controller.set_gpio(dev, GPIO_RESET, VALUE_RESET_DEASSERT) def do_reset(dev): cp210x_controller.set_gpio(dev, GPIO_RESET, VALUE_RESET_ASSERT) time.sleep(0.3) cp210x_controller.set_gpio(dev, GPIO_RESET, VALUE_RESET_DEASSERT) def do_normalboot(dev): cp210x_controller.set_gpio(dev, GPIO_MASKROM, VALUE_MASKROM_DEASSERT) def do_maskrom(dev): cp210x_controller.set_gpio(dev, GPIO_MASKROM, VALUE_MASKROM_ASSERT) def do_reset_to_normal(dev): cp210x_controller.set_gpio(dev, GPIO_MASKROM, VALUE_MASKROM_DEASSERT) cp210x_controller.set_gpio(dev, GPIO_RESET, VALUE_RESET_ASSERT) time.sleep(0.3) cp210x_controller.set_gpio(dev, GPIO_RESET, VALUE_RESET_DEASSERT) def do_reset_to_maskrom(dev): cp210x_controller.set_gpio(dev, GPIO_MASKROM, VALUE_MASKROM_ASSERT) cp210x_controller.set_gpio(dev, GPIO_RESET, VALUE_RESET_ASSERT) time.sleep(0.3) cp210x_controller.set_gpio(dev, GPIO_RESET, VALUE_RESET_DEASSERT) time.sleep(0.3) cp210x_controller.set_gpio(dev, GPIO_MASKROM, VALUE_MASKROM_DEASSERT) def main(product_string): options_dict = { '--invert-reset': (invert_reset, 'invert voltage level of reset line'), '--invert-maskrom': (invert_maskrom, 'invert voltage level of maskrom line') } command_dict = { 'status': (do_status, 'show status of attached board'), 'reset-assert': (do_reset_assert, 'assert the reset line'), 'reset-deassert': (do_reset_deassert, 'deassert the reset line'), 'reset': (do_reset, 'assert the reset line for 300 ms'), 'normalboot': (do_normalboot, 'set bootmode to normal boot'), 'maskrom': (do_maskrom, 'set bootmode to maskrom'), 'reset-to-normal': (do_reset_to_normal, 'reset with normal boot target'), 'reset-to-maskrom': (do_reset_to_maskrom, 'reset to maskrom') } # Create argparser object cp = command_processor.CommandProcessor(product_string, options_dict, command_dict) # Valiate command line arguments and commands cp.validate_commandline(sys.argv[1:]) # Start executing the commands cp.run()