def test_device_serial_custom(session, device): assert len(session.available_devices) == 1 if float(device.fwver) < 2.10: # update to the latest firmware version (that supports custom serial numbers) session.add(device) session.devices[0].flash_firmware(DEV_FW) sys.stdout.write('\n') prompt('unplug/replug the device') session.scan() device = session.available_devices[0] # can't set serial numbers for devices in active sessions session.add_all() session.start(0) with pytest.raises(DeviceError): session.devices[0].serial = '' session.end() assert float(device.fwver) >= 2.10 orig_serial = device.serial device.serial = 'foo' device = session.available_devices[0] assert device.serial == 'foo' device.serial = '' device = session.available_devices[0] assert device.serial == orig_serial device.serial = 'my device' device = session.available_devices[0] assert device.serial == 'my device' device.serial = orig_serial device = session.available_devices[0] assert device.serial == orig_serial
def _read_hotplug(session, device, status): """Verify no stalls when unplugging a device during reads.""" printed = False num_samples = 0 start = time.time() if status == 'continuous': session.start(0) with pytest.raises(DeviceError) as excinfo: while True: if time.time() - start > 10: pytest.fail('failed to unplug the device within 10 seconds') if status == 'continuous': samples = device.read(10000, -1) else: samples = device.get_samples(10000) num_samples += len(samples) if not printed: print('\nACTION: unplug the device within 10 seconds') printed = True sys.stdout.write('\rreceived samples: {}'.format(num_samples)) sys.stdout.flush() assert 'device detached' == str(excinfo.value) sys.stdout.write('\n') prompt('plug the device back in') session, device = _add_device(session)
def test_read_initial_data(): """Verify data values of HI-Z samples on initial attach. See https://github.com/analogdevicesinc/m1k-fw/issues/9 for details. """ sys.stdout.write('\n') prompt( 'unplug the device, ground the metal USB connector, and plug it back in' ) # create our own session to avoid detach exceptions session = Session(add_all=False) session, device = _add_device(session) samples = device.get_samples(100) assert len(samples) == 100 # verify all samples are near 0 tests = [] for sample in samples: for x in chain.from_iterable(sample): tests.append(abs(round(x)) == 0) # old firmware versions exhibit bad data, newer versions should fix the issue if float(device.fwver) <= 2.10: assert not all(tests) else: assert all(tests) # request a bunch of extra samples to flush bad values device.get_samples(10000) # force session destruction session._close()
def test_scan(session): sys.stdout.write('\n') prompt('make sure at least one device is plugged in') session.scan() # available devices haven't been added to the session yet assert session.available_devices assert len(session.available_devices) != len(session.devices)
def test_read_write_overcurrent(device): """Verify overcurrent detection during data acquisition.""" if float(device.fwver) < 2.07: pytest.skip("requires firmware >= 2.07") sys.stdout.write('\n') for action, value in (('loopback 2.5V to CH A', True), ('loopback 5V to CH B', True), ('remove loopback', False)): prompt(action) device.channels['A'].mode = Mode.SVMI device.channels['A'].sine(0, 5, 100, -25) # Output a cosine wave for channel B voltage. device.channels['B'].mode = Mode.SVMI device.channels['B'].sine(0, 5, 100, 0) device.get_samples(1000) assert device.overcurrent == value
def test_flash_firmware(session): session.add_all() assert len(session.devices) == 1 serial = session.devices[0].serial # flash old firmware sys.stdout.write('\n') print('flashing firmware 2.02...') session.flash_firmware(OLD_FW) prompt('unplug/replug the device') session.add_all() assert len(session.devices) == 1 assert session.devices[0].serial == serial assert session.devices[0].fwver == '2.02' # flash new firmware print('flashing firmware 2.06...') session.flash_firmware(NEW_FW) prompt('unplug/replug the device') session.add_all() assert len(session.devices) == 1 assert session.devices[0].serial == serial assert session.devices[0].fwver == '2.06'
def test_samba_mode(session, device): # supported devices exist in the session num_available_devices = len(session.available_devices) assert num_available_devices orig_serial = session.available_devices[0].serial # pushing one into SAM-BA mode drops it from the session after rescan device.samba_mode() session.scan() assert len(session.available_devices) == num_available_devices - 1 assert not any(d.serial == orig_serial for d in session.available_devices) # flash device in SAM-BA mode session.flash_firmware(NEW_FW) sys.stdout.write('\n') prompt('unplug/replug the device') session.scan() # device is re-added after hotplug assert len(session.available_devices) == num_available_devices dev = session.available_devices[0] assert dev.serial == orig_serial assert dev.fwver == '2.06'
def test_hotplug(session): sys.stdout.write('\n') prompt('unplug/plug a device within 10 seconds') session.add_all() # create fake attach/detach callbacks to check basic triggering fake_attach = mock.Mock() fake_detach = mock.Mock() session.hotplug_attach(fake_attach) session.hotplug_detach(fake_detach) # create more realistic callbacks that try adding/removing the # hotplugged device from a session def attach(dev): serial = dev.serial session.add(dev) assert any(d.serial == serial for d in session.devices) def detach(dev): serial = dev.serial session.remove(dev, detached=True) assert not any(d.serial == serial for d in session.devices) session.hotplug_attach(attach) session.hotplug_detach(detach) start = time.time() print('waiting hotplug events...') while (True): time.sleep(1) end = time.time() elapsed = end - start if elapsed > 10 or (fake_attach.called and fake_detach.called): break assert fake_attach.called, 'attach callback not called' assert fake_detach.called, 'detach callback not called'
def test_write_calibration(session, device): default_cal = [[0.0, 1.0, 1.0] for x in range(8)] # old firmware versions don't support writing calibration data session.add_all() session.flash_firmware(OLD_FW) sys.stdout.write('\n') prompt('unplug/replug the device') session.add_all() device = session.devices[0] assert float(device.fwver) < 2.06 assert device.calibration == default_cal with pytest.raises(DeviceError): device.write_calibration(None) # update to firmware supporting calibration session.flash_firmware(NEW_FW) prompt('unplug/replug the device') session.add_all() device = session.devices[0] # writing nonexistent calibration file with pytest.raises(DeviceError): device.write_calibration('nonexistent') # writing bad calibration file cal_data = tempfile.NamedTemporaryFile() with open(cal_data.name, 'w') as f: f.write('foo') with pytest.raises(DeviceError): device.write_calibration(cal_data.name) # find default calibration file in repo dn = os.path.dirname default_cal_data = os.path.join(dn(dn(dn(dn(__file__)))), 'contrib', 'calib.txt') # writing modified calibration file cal_data = tempfile.NamedTemporaryFile() with open(default_cal_data) as default, open(cal_data.name, 'w') as f: for line in default: # randomly munge cal data if line.strip() == "<0.0000, 0.0000>": f.write("<1.0000, 2.0000>\n") else: f.write(line) # verify that the new file differs from the default assert not filecmp.cmp(cal_data.name, default_cal_data) device.write_calibration(cal_data.name) new_cal = device.calibration assert new_cal != default_cal # make sure calibration data survives firmware updates session.flash_firmware(NEW_FW) prompt('unplug/replug the device') session.add_all() device = session.devices[0] assert new_cal == device.calibration # reset calibration device.write_calibration(None) assert device.calibration == default_cal # writing good calibration file device.write_calibration(default_cal_data) assert device.calibration == default_cal
def device(session): if not len(session.available_devices): sys.stdout.write('\n') prompt('plug in a device') session.scan() return session.available_devices[0]