コード例 #1
0
 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()
コード例 #2
0
ファイル: gpio.py プロジェクト: tinkertux/pyftdi
 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()
コード例 #3
0
ファイル: gpio.py プロジェクト: tinkertux/pyftdi
 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()
コード例 #4
0
ファイル: gpio.py プロジェクト: tinkertux/pyftdi
    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()
コード例 #5
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()
コード例 #6
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))
コード例 #7
0
 def readconfig(self):
    Settings.PinStatesMax = 7
    Settings.PinStates = ["Default","Input","Reserved","Reserved","Output","Output-Low","Output-High","Special","Reserved"]
    getLogger('pyftdi.i2c').setLevel(ERROR)
    for b in range(len(Settings.Pinout)):
     if Settings.Pinout[b]["altfunc"] != 0 and Settings.Pinout[b]["startupstate"]>0 and Settings.Pinout[b]["startupstate"]<7:
      Settings.Pinout[b]["startupstate"] = -1 # set to default
    correctdependencies()
    self.gpioctrl = [] # ftdi gpio
    self.gpios    = [] # rpigpio compatible layer
    self.i2cctrl  = [] # ftdi i2c
    self.spictrl  = [] # ftdi spi
    self.pinhandlers = []

    devlist = get_ftdi_devices(2)
    devs = get_ftdi_configured_devices()
    for cd in range(len(devs)):
     notfound = True
     for rd in range(len(devlist)):
      if devlist[rd][0] == devs[cd]:
       notfound = False
       break
     if notfound: # configured device missing?
      if is_serialnumber(devs[cd]): # delete if serial based
        self.removedevpinout(devs[cd])
        devs = get_ftdi_configured_devices()
      else:# replace if address based
       dt = ""
       rep = ""
       for p in range(len(Settings.Pinout)):
        if Settings.Pinout[p]["ftdevice"]==devs[cd]:
         dt = Settings.Pinout[p]["ftdevtype"]
         break
       if dt != "":
        for rd in range(len(devlist)):
          if not devlist[rd][0] in devs: # if not configured
           if (devlist[rd][1] == dt) and (is_serialnumber(devlist[rd][0])==False): # if found a similar, not configured one
            rep = devlist[rd][0]
            break
       misc.addLog(rpieGlobals.LOG_LEVEL_INFO,"Missing "+devs[cd]+" replaced with "+rep)
       if rep != "": # dynamic replacement
        for p in range(len(Settings.Pinout)):
         if Settings.Pinout[p]["ftdevice"]==devs[cd]:
          Settings.Pinout[p]["ftdevice"] = rep
        devs = get_ftdi_configured_devices()

    if len(devs)>0:
     for d in range(len(devs)):
       gtype = 0
       hconfpin = 0
       dirpin = 0
       cter = 0
       cpins = 0
       for p in range(len(Settings.Pinout)):
        if Settings.Pinout[p]["ftdevice"]==devs[d]:
         try:
          if Settings.Pinout[p]["startupstate"]>-1:
           hconfpin = Settings.Pinout[p]["realpin"]
           if Settings.Pinout[p]["startupstate"] in [4,5,6]:
            dirpin = set_bit(dirpin,hconfpin,1)
            cpins |= (1 << int(c))
           elif Settings.Pinout[p]["startupstate"] == 1:
            cpins |= (1 << int(c))
          if Settings.Pinout[p]["altfunc"]>0:
           gtype = Settings.Pinout[p]["altfunc"]
           cter += 1
         except:
          pass
#       print("readconfig ",gtype,hconfpin,dirpin)#debug
       self.gpioctrl.append({"ftdevice":devs[d],"o":None})
       try:
        if gtype==0:
         if hconfpin<8: # old style will be enough
          self.gpioctrl[d]["o"] = GpioAsyncController()
         else:          # wide style needed
          self.gpioctrl[d]["o"] = GpioMpsseController()
         try:
          reqfreq = 1.0E5
          self.gpioctrl[d]["o"].configure(devs[d],direction=dirpin,frequency=reqfreq)
         except:
          reqfreq = 0
         if reqfreq==0:
          self.gpioctrl[d]["o"].configure(devs[d],direction=dirpin)
        elif gtype==2:  # spi
         cter = cter - 3
         if cter<1:
          cter = 1
         elif cter>5:
          cter = 5
         self.spictrl.append({"ftdevice":devs[d],"o":None})
         self.spictrl[-1]["o"] = SpiController(cs_count=cter)
         self.spictrl[-1]["o"].configure(devs[d],direction=dirpin)
         self.gpioctrl[d]["o"] = self.spictrl[-1]["o"].get_gpio()
         self.gpioctrl[d]["o"].set_direction(cpins,dirpin)
        elif gtype==3:  # i2c
         self.i2cctrl.append({"ftdevice":devs[d],"o":None})
         self.i2cctrl[-1]["o"] = I2cController()
         self.i2cctrl[-1]["o"].configure(devs[d],direction=dirpin)
         self.gpioctrl[d]["o"] = self.i2cctrl[-1]["o"].get_gpio()
         self.gpioctrl[d]["o"].set_direction(cpins,dirpin)

        self.gpios.append({"ftdevice":devs[d],"o":None})
        if self.gpioctrl[d]["o"] is not None:
         try:
          freq = self.gpioctrl[d]["o"].frequency
         except:
          freq = 1.0E4
         self.gpios[d]["o"] = FTDIGPIO(self.gpioctrl[d]["o"],freq)
       except Exception as e:
        print("gpio init err",e)

     self.pinhandlers = []
     self.pinhandlers.append(None)
     for p in range(len(Settings.Pinout)):
      for d in range(len(devs)):
        if Settings.Pinout[p]["ftdevice"]==devs[d]:
#         if Settings.Pinout[p]["altfunc"]==2:#spi
#          self.pinhandlers.append(None)
#         elif Settings.Pinout[p]["altfunc"]==3:#i2c
#          self.pinhandlers.append(None)
#         else:
          self.pinhandlers.append(self.gpios[d]["o"])
     if self.get_first_i2c()>-1:
      rpieGlobals.extender = 256
     else:
      rpieGlobals.extender = 128
    return True