aboutsummaryrefslogtreecommitdiff
path: root/usb-control/cp2102_simple.py
blob: c4b29245dad4f042d5958649f7d2f685a55f7401 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#!/usr/bin/env python

"""
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 reset == RESETSTATE_DEASSERTED else "in reset"),
        ("normal boot" if bootmode == VALUE_MASKROM_DEASSERTED 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()