Esempio n. 1
0
 def test_default_gpio(self):
     """Check I/O.
     """
     gpio_in, gpio_out = GpioMpsseController(), GpioMpsseController()
     gpio_in.configure(self.urls[0], direction=0x0000, frequency=10e6,
                       debug=self.debug_mpsse)
     gpio_out.configure(self.urls[1], direction=0xFFFF, frequency=10e6,
                        debug=self.debug_mpsse)
     for out in range(3, 0x10000, 29):
         gpio_out.write(out)
         outv = gpio_out.read()[0]
         inv = gpio_in.read()[0]
         # check inputs match outputs
         self.assertEqual(inv, out)
         # check level of outputs match the ones written
         self.assertEqual(outv, out)
     gpio_in.close()
     gpio_out.close()
Esempio n. 2
0
 def test_peek_gpio(self):
     """Check I/O peeking
     """
     gpio_in, gpio_out = GpioMpsseController(), GpioMpsseController()
     gpio_in.configure(self.urls[0], direction=0xFF00, frequency=10e6,
                       debug=self.debug)
     gpio_out.configure(self.urls[1], direction=0x00FF, frequency=10e6,
                        debug=self.debug)
     for out in range(256):
         gpio_out.write(out)
         outv = gpio_out.read()[0]
         inv = gpio_in.read(peek=True)
         # check inputs match outputs
         self.assertEqual(inv, out)
         #print(f'{out} {inv}')
         # check level of outputs match the ones written
         self.assertEqual(outv, out)
     gpio_in.close()
     gpio_out.close()
Esempio n. 3
0
    def test_stream_gpio(self):
        """Check I/O streaming.

           Beware this test is CPU intensive w/ virtual framework
        """
        gpio_in, gpio_out = GpioMpsseController(), GpioMpsseController()
        gpio_in.configure(self.urls[0], direction=0x0000, frequency=10e6,
                          debug=self.debug)
        gpio_out.configure(self.urls[1], direction=0xFFFF, frequency=10e6,
                           debug=self.debug)
        outv = list(range(0, 0x10000, 29))
        max_count = min(gpio_out.ftdi.fifo_sizes[0],
                        gpio_in.ftdi.fifo_sizes[1])//2  # 2 bytes/value
        outv = outv[:max_count]
        gpio_out.write(outv)
        inv = gpio_in.read(len(outv))
        # for now, it is hard to test value exactness
        self.assertEqual(len(outv), len(inv))
        gpio_in.close()
        gpio_out.close()
Esempio n. 4
0
#!/usr/bin/env python
from pyftdi.gpio import GpioMpsseController
import time

pin = 4
delay = 0.1
controller = GpioMpsseController()
try:
    controller.configure(
        "ftdi://ftdi:ft232h/1",
        frequency=1e6,
        direction=0x0000)
    gpio = controller.get_gpio()
    while True:
        print(gpio.read()[0] >> pin & 1)
        time.sleep(delay)
finally:
    controller.close()
Esempio n. 5
0
class DeviceWrapper:
    def __init__(self):
        self.controller = None
        self.gpio = None
        self.output_pins = 0
        self.input_pins = 0

    def _setup(self):
        try:
            self.controller.configure(
                'ftdi://ftdi:ft232h/1',
                frequency=100,
                direction=0xFFFF,
                initial=0x0000,
            )
            self.gpio = self.controller.get_gpio()
            valid_pins = self.gpio.all_pins
            used_pins = self.output_pins | self.input_pins
            invalid_pins = ((valid_pins | used_pins) & ~valid_pins) & used_pins
            if invalid_pins != 0:
                formatted = \
                    [i for i in range(0, 16) if invalid_pins & 1 << i != 0]
                raise HardwareError(
                    "Cannot use pin(s) {} as GPIO.".format(formatted))
            # A low bit (equal to 0) indicates an input pin.
            # A high bit (equal to 1) indicates an output pin.
            new_direction = self.output_pins & ~self.input_pins
            self.gpio.set_direction(used_pins, new_direction)
        except UsbToolsError as error:
            self.gpio = None
            raise HardwareError(error) from None
        except USBError as error:
            self.gpio = None
            raise HardwareError(error) from None

    def _connect(self):
        if self.gpio:
            return
        if self.controller is not None:
            # Finicky way to get pyftdi to clean up and reconnect properly
            # after a hardware disconnect. Not necessary on first connect.
            UsbTools.release_device(self.controller._ftdi._usb_dev)
            self.controller.close()
            UsbTools.flush_cache()
        else:
            self.controller = GpioMpsseController()
        self._setup()

    def disconnect(self):
        if self.gpio:
            self.gpio = None
            self.controller.close()

    def add_output(self, pin):
        self.output_pins = self.output_pins | 1 << pin

    def add_input(self, pin):
        self.input_pins = self.input_pins | 1 << pin

    def _read_no_error_handling(self, pin):
        current = self.gpio.read()[0]
        return (current >> pin) & 1

    def _write_no_error_handling(self, pin, value):
        current = self.gpio.read()[0]
        new = set_bit(current, pin, value)
        self.gpio.write(new & self.gpio.direction)

    def _reconnect_and_try_again(self, action):
        self._connect()
        try:
            return action()
        except FtdiError as error1:
            try:
                self.disconnect()
                self._connect()
                return action()
            except FtdiError:
                raise HardwareError(error1)

    def read(self, pin):
        return self._reconnect_and_try_again(
            lambda: self._read_no_error_handling(pin))

    def write(self, pin, value):
        self._reconnect_and_try_again(
            lambda: self._write_no_error_handling(pin, value))