def test_input_gpio(self): """Simple test to demonstrate input bit-banging on CBUS. You need a CBUS-capable FTDI (FT232R/FT232H/FT230X/FT231X), whose EEPROM has been configured to support GPIOs on CBUS0 and CBUS3. Hard-wiring is required to run this test: * CBUS0 (input) should be connected to RTS (output) * CBUS3 (input) should be connected to DTR (output) """ ftdi = Ftdi() ftdi.open_from_url(self.url) # sanity check: device should support CBUS feature self.assertEqual(ftdi.has_cbus, True) eeprom = FtdiEeprom() eeprom.connect(ftdi) # sanity check: device should have been configured for CBUS GPIOs self.assertEqual(eeprom.cbus_mask & 0b1001, 0b1001) # configure CBUS0 and CBUS3 as input ftdi.set_cbus_direction(0b1001, 0b0000) # no output pin available self.assertRaises(FtdiError, ftdi.set_cbus_gpio, 0) for cycle in range(40): rts = bool(cycle & 0x1) dtr = bool(cycle & 0x2) ftdi.set_rts(rts) ftdi.set_dtr(dtr) # need to inverse logical level as RS232 uses negative logic cbus = ~ftdi.get_cbus_gpio() sig = (cbus & 0x1) | ((cbus & 0x8) >> 2) value = cycle & 0x3 self.assertEqual(value, sig)
def test_230x(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) ftdi = Ftdi() ftdi.open_from_url('ftdi:///1') self.assertEqual(ftdi.has_cbus, True) vftdi = self.loader.get_virtual_ftdi(1, 1) vport = vftdi.get_port(1) # CBUS0: in, CBUS1: out, CBUS2: in, CBUS3: out # however, only CBUS0 and CBUS1 are mapped as GPIO, # CBUS2 forced to 1 and CBUS3 not usable as IO # even if use mask is 1111 eeprom_mask = 0b0011 eeprom_force = 0b0100 cbus_mask = 0b1111 cbus_dir = 0b1010 ftdi.set_cbus_direction(cbus_mask, cbus_dir) cbus_out = 0b0011 # CBUS0: 1, CBUS1: 1 # however, only CBUS1 is out, so CBUS0 output value should be ignored ftdi.set_cbus_gpio(cbus_out) exp_out = cbus_dir & cbus_out exp_out &= eeprom_mask exp_out |= eeprom_force vcbus, vactive = vport.cbus self.assertEqual(vcbus, exp_out) self.assertEqual(vactive, eeprom_mask | eeprom_force) cbus_out = 0b0000 ftdi.set_cbus_gpio(cbus_out) exp_out = cbus_dir & cbus_out exp_out &= eeprom_mask exp_out |= eeprom_force vcbus, vactive = vport.cbus self.assertEqual(vcbus, exp_out) cbus_in = 0b0101 vport.cbus = cbus_in cbus = ftdi.get_cbus_gpio() exp_in = cbus_in & eeprom_mask self.assertEqual(cbus, exp_in) ftdi.close()
def test_lc231x(self): """Check simple GPIO write and read sequence.""" # load custom CBUS config, with: # CBUS0: GPIO (gpio) # CBUS1: TXLED # CBUS2: DRIVE0 (to light up RX green led) # CBUS3: GPIO (gpio) # only CBUS0 and CBUS3 are available on LC231X # CBUS1 is connected to TX led, CBUS2 to RX led with open('pyftdi/tests/resources/ft231x_cbus.yaml', 'rb') as yfp: self.loader.load(yfp) ftdi = Ftdi() ftdi.open_from_url('ftdi:///1') self.assertEqual(ftdi.has_cbus, True) vftdi = self.loader.get_virtual_ftdi(1, 1) vport = vftdi.get_port(1) # CBUS0: in, CBUS1: out, CBUS2: in, CBUS3: out # however, only CBUS0 and CBUS3 are mapped as GPIO, # CBUS1 not usable as IO, CBUS2 is fixed to low # even if use mask is 1111 eeprom_mask = 0b1001 eeprom_force_low = 0b0100 cbus_mask = 0b1111 cbus_dir = 0b1010 ftdi.set_cbus_direction(cbus_mask, cbus_dir) cbus_out = 0b1111 # however, only CBUS0 & 3 are out, so CBUS1/CBUS2 should be ignored ftdi.set_cbus_gpio(cbus_out) exp_out = cbus_dir & cbus_out exp_out &= eeprom_mask vcbus, vactive = vport.cbus self.assertEqual(vcbus, exp_out) self.assertEqual(vactive, eeprom_mask | eeprom_force_low) cbus_out = 0b0000 ftdi.set_cbus_gpio(cbus_out) exp_out = cbus_dir & cbus_out exp_out &= eeprom_mask vcbus, vactive = vport.cbus self.assertEqual(vcbus, exp_out) cbus_in = 0b0101 vport.cbus = cbus_in cbus = ftdi.get_cbus_gpio() exp_in = cbus_in & eeprom_mask self.assertEqual(cbus, exp_in) ftdi.close()