aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorQuentin Schulz <quentin.schulz@theobroma-systems.com>2023-06-29 15:17:33 +0200
committerQuentin Schulz <quentin.schulz@theobroma-systems.com>2023-08-04 13:33:03 +0200
commitad33cc2a47b9e956227c1b45506acb5dd2acc31b (patch)
tree9b725c686512ede1148fd26a6eb3dd359d86c6bd
parentc9b06d07394756da4edd8d05b16e87d78d4e3bf6 (diff)
usb-control: fix flake8 302 expected 2 blank lines, found 1
Signed-off-by: Quentin Schulz <quentin.schulz@theobroma-systems.com>
-rw-r--r--usb-control/command_processor.py3
-rw-r--r--usb-control/cp2102_haikou.py12
-rw-r--r--usb-control/cp2102_simple.py11
-rw-r--r--usb-control/cp210x_controller.py4
4 files changed, 30 insertions, 0 deletions
diff --git a/usb-control/command_processor.py b/usb-control/command_processor.py
index 6d534c6..945c337 100644
--- a/usb-control/command_processor.py
+++ b/usb-control/command_processor.py
@@ -8,14 +8,17 @@ __license__ = 'MIT'
import sys
import cp210x_controller
+
def do_delay(dev):
time.sleep(0.3)
+
def do_list(devs):
print("Found %d board%s:" % (len(devs), "s"[len(devs) == 1:]))
for idx, dev in enumerate(devs):
print("%d: %s %s" % (idx, dev.serial_number, dev.product))
+
class CommandProcessor:
def __init__(self, product_string, options_dict, command_dict):
self.product_string = product_string
diff --git a/usb-control/cp2102_haikou.py b/usb-control/cp2102_haikou.py
index c6ac6bf..932df40 100644
--- a/usb-control/cp2102_haikou.py
+++ b/usb-control/cp2102_haikou.py
@@ -31,6 +31,7 @@ VALUE_BIOS_DISABLE = 1
POWERSTATE_ON = 0
POWERSTATE_OFF = 1
+
def toggle_power(dev):
# We emulate a power button press for 300 ms
cp210x_controller.set_gpio(dev, GPIO_POWER, VALUE_POWER_PRESS)
@@ -38,6 +39,7 @@ def toggle_power(dev):
cp210x_controller.set_gpio(dev, GPIO_POWER, VALUE_POWER_RELEASE)
time.sleep(0.3)
+
def get_powerstate(dev):
# Reading the GPIO latch (get_gpio()) is useless, because it
# does not reflect the real status. However, we have CTS connected
@@ -45,11 +47,13 @@ def get_powerstate(dev):
# Returns POWERSTATE_ON or POWERSTATE_OFF
return cp210x_controller.get_modemstatus(dev)
+
def set_bootmode(dev, state):
# Set the bootmode state according the given argument.
# state should be VALUE_BIOS_NORMAL or VALUE_BIOS_DISABLE
cp210x_controller.set_gpio(dev, GPIO_BIOS, state)
+
def get_bootmode(dev):
# We can only check if we pull the line low.
# This could be overruled with the on-board slider.
@@ -65,25 +69,31 @@ def do_status(dev):
("ON" if powerstate == POWERSTATE_ON else "OFF"),
("BIOS disabled" if bootmode == VALUE_BIOS_DISABLE else "Normal boot (if not overruled by on-board switch)")))
+
def do_normalboot(dev):
set_bootmode(dev, VALUE_BIOS_NORMAL)
+
def do_biosdisable(dev):
set_bootmode(dev, VALUE_BIOS_DISABLE)
+
def do_powerbutton(dev):
toggle_power(dev)
+
def do_on(dev):
# Turn on if needed
if get_powerstate(dev) == POWERSTATE_OFF:
toggle_power(dev)
+
def do_off(dev):
# Turn off if needed
if get_powerstate(dev) == POWERSTATE_ON:
toggle_power(dev)
+
def do_cycle(dev):
# Turn off if needed
if get_powerstate(dev) == POWERSTATE_ON:
@@ -91,6 +101,7 @@ def do_cycle(dev):
# Turn on board
toggle_power(dev)
+
def do_cycle_to_normal(dev):
# Turn off if needed
if get_powerstate(dev) == POWERSTATE_ON:
@@ -100,6 +111,7 @@ def do_cycle_to_normal(dev):
# Turn on board
toggle_power(dev)
+
def do_cycle_to_maskrom(dev):
# Turn off if needed
if get_powerstate(dev) == POWERSTATE_ON:
diff --git a/usb-control/cp2102_simple.py b/usb-control/cp2102_simple.py
index 587e45d..9817a24 100644
--- a/usb-control/cp2102_simple.py
+++ b/usb-control/cp2102_simple.py
@@ -24,18 +24,21 @@ 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)
@@ -43,29 +46,36 @@ def do_status(dev):
("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)
@@ -74,6 +84,7 @@ def do_reset_to_maskrom(dev):
time.sleep(0.3)
cp210x_controller.set_gpio(dev, GPIO_MASKROM, VALUE_MASKROM_DEASSERT)
+
def main(product_string):
options_dict = {
'--invert-reset': (invert_reset,
diff --git a/usb-control/cp210x_controller.py b/usb-control/cp210x_controller.py
index 93e5b33..a800073 100644
--- a/usb-control/cp210x_controller.py
+++ b/usb-control/cp210x_controller.py
@@ -21,6 +21,7 @@ CP210X_WRITE_LATCH = 0x37E1
CP210X_READ_LATCH = 0x00C2
CP210X_GET_MDMSTS = 0x8
+
def set_gpio(dev, gpio, value):
# the latch register has 16 bit. the upper 8 bits are the gpio value,
# the lower 8 bit are the write mask.
@@ -30,6 +31,7 @@ def set_gpio(dev, gpio, value):
wValue = CP210X_WRITE_LATCH,
wIndex = val)
+
def get_gpio(dev, gpio):
# Read 1 byte from latch register
data = dev.ctrl_transfer(bmRequestType = REQTYPE_DEVICE_TO_HOST,
@@ -38,6 +40,7 @@ def get_gpio(dev, gpio):
data_or_wLength = 1)
return not not (data[0] & (1 << gpio))
+
def get_modemstatus(dev):
data = dev.ctrl_transfer(bmRequestType = REQTYPE_DEVICE_TO_HOST,
bRequest = CP210X_GET_MDMSTS,
@@ -47,6 +50,7 @@ def get_modemstatus(dev):
# Check CTS line
return ((data[0] >> 4) & 1)
+
def find_board_list(product_string, serialnumber):
"""
This function returns a list of found CP210x controllers.