Skip to content

Instantly share code, notes, and snippets.

@tuanml
Forked from 18516329677/usb_reset.py
Created December 26, 2018 15:20
Show Gist options
  • Save tuanml/9f46dce8d98c8a398de24f1477687578 to your computer and use it in GitHub Desktop.
Save tuanml/9f46dce8d98c8a398de24f1477687578 to your computer and use it in GitHub Desktop.

Revisions

  1. @PaulFurtado PaulFurtado revised this gist Sep 19, 2014. 1 changed file with 4 additions and 2 deletions.
    6 changes: 4 additions & 2 deletions usb_reset.py
    Original file line number Diff line number Diff line change
    @@ -46,8 +46,10 @@ def send_reset(dev_path):
    See get_teensy for example of how to obtain this.
    """
    fd = os.open(dev_path, os.O_WRONLY)
    fcntl.ioctl(fd, USBDEVFS_RESET, 0)
    os.close(fd)
    try:
    fcntl.ioctl(fd, USBDEVFS_RESET, 0)
    finally:
    os.close(fd)


    def reset_teensy():
  2. @PaulFurtado PaulFurtado revised this gist Sep 19, 2014. 1 changed file with 30 additions and 1 deletion.
    31 changes: 30 additions & 1 deletion usb_reset.py
    Original file line number Diff line number Diff line change
    @@ -1,12 +1,32 @@
    """
    Example code for resetting the USB port that a Teensy microcontroller is
    attached to. There are a lot of situations where a Teensy or Arduino can
    end up in a bad state and need resetting, this code is useful for
    """

    import os
    import fcntl
    import subprocess


    # Equivalent of the _IO('U', 20) constant in the linux kernel.
    USBDEVFS_RESET = ord('U') << (4*2) | 20


    def get_teensy():
    """
    Gets the devfs path to a Teensy microcontroller by scraping the output
    of the lsusb command
    The lsusb command outputs a list of USB devices attached to a computer
    in the format:
    Bus 002 Device 009: ID 16c0:0483 Van Ooijen Technische Informatica Teensyduino Serial
    The devfs path to these devices is:
    /dev/bus/usb/<busnum>/<devnum>
    So for the above device, it would be:
    /dev/bus/usb/002/009
    This function generates that path.
    """
    proc = subprocess.Popen(['lsusb'], stdout=subprocess.PIPE)
    out = proc.communicate()[0]
    lines = out.split('\n')
    @@ -19,10 +39,19 @@ def get_teensy():


    def send_reset(dev_path):
    """
    Sends the USBDEVFS_RESET IOCTL to a USB device.
    dev_path - The devfs path to the USB device (under /dev/bus/usb/)
    See get_teensy for example of how to obtain this.
    """
    fd = os.open(dev_path, os.O_WRONLY)
    fcntl.ioctl(fd, USBDEVFS_RESET, 0)
    os.close(fd)


    def reset_teensy():
    send_reset(get_teensy())
    """
    Finds a teensy and reset it.
    """
    send_reset(get_teensy())
  3. @PaulFurtado PaulFurtado created this gist Sep 18, 2014.
    28 changes: 28 additions & 0 deletions usb_reset.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,28 @@
    import os
    import fcntl
    import subprocess


    USBDEVFS_RESET = ord('U') << (4*2) | 20


    def get_teensy():
    proc = subprocess.Popen(['lsusb'], stdout=subprocess.PIPE)
    out = proc.communicate()[0]
    lines = out.split('\n')
    for line in lines:
    if 'Teensyduino' in line:
    parts = line.split()
    bus = parts[1]
    dev = parts[3][:3]
    return '/dev/bus/usb/%s/%s' % (bus, dev)


    def send_reset(dev_path):
    fd = os.open(dev_path, os.O_WRONLY)
    fcntl.ioctl(fd, USBDEVFS_RESET, 0)
    os.close(fd)


    def reset_teensy():
    send_reset(get_teensy())