class GpioTest: """ """ def __init__(self): self._gpio = GpioController() self._state = 0 # SW cache of the GPIO output lines def open(self, out_pins): """Open a GPIO connection, defining which pins are configured as output and input""" out_pins &= 0xFF url = environ.get('FTDI_DEVICE', 'ftdi://ftdi:2232h/1') self._gpio.configure(url, direction=out_pins) def close(self): """Close the GPIO connection""" self._gpio.close() def set_gpio(self, line, on): """Set the level of a GPIO ouput pin. :param line: specify which GPIO to madify. :param on: a boolean value, True for high-level, False for low-level """ if on: state = self._state | (1 << line) else: state = self._state & ~(1 << line) self._commit_state(state) def get_gpio(self, line): """Retrieve the level of a GPIO input pin :param line: specify which GPIO to read out. :return: True for high-level, False for low-level """ value = self._gpio.read_port() return bool(value & (1 << line)) def _commit_state(self, state): """Update GPIO outputs """ self._gpio.write_port(state) # do not update cache on error self._state = state
def test_baudrate(self): """Check simple GPIO write and read sequence.""" # load custom CBUS config, with: # CBUS0: GPIO (gpio) # CBUS1: GPIO (gpio) # CBUS0: DRIVE1 (forced to high level) # CBUS0: TXLED (eq. to highz for tests) with open('pyftdi/tests/resources/ft230x_io.yaml', 'rb') as yfp: self.loader.load(yfp) gpio = GpioController() # access to the virtual GPIO port out_pins = 0xAA gpio.configure('ftdi://:230x/1', direction=out_pins) vftdi = self.loader.get_virtual_ftdi(1, 1) vftdi.get_port(1) baudrate = 1000000 gpio.set_frequency(baudrate) gpio.close()
if platform == "win32": default_url = "ftdi:///4" devices = UsbTools._find_devices(0x0403, 0x6011, True) for dev in devices: if get_interface_index(dev) == 3: default_url = format_ftdi_url(dev) else: default_url = "ftdi:///4" parser.add_argument('-u', '--url', default=default_url, help='FTDI URL') # run parser args = parser.parse_args() gpio = GpioController() gpio.configure(args.url, direction=0x0F) setting = 0x0F # start with 3.3V both VCC_IO1 and VCC_IO2 if args.VCC_IO1 == '3.3V': # do nothing, already the default pass elif args.VCC_IO1 == '2.5V': setting = setting & 0xFD elif args.VCC_IO1 == '1.8V': setting = setting & 0xFE if args.VCC_IO2 == '3.3V': # do nothing, already the default pass elif args.VCC_IO2 == '2.5V': setting = setting & 0xF7