diff --git a/CHANGELOG.md b/CHANGELOG.md index 5bc3af6804..e86be897ea 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -558,6 +558,7 @@ #### [nidigital] Unreleased - Added - (Common) Added `nitlsconfig[grpc]>=1.0.0a2` to the optional gRPC dependencies. + - Added the `nidigital_create_pin_map.py` example. - Changed - Removed diff --git a/docs/nidigital/examples.rst b/docs/nidigital/examples.rst index b8c8cc689a..b5497f4741 100644 --- a/docs/nidigital/examples.rst +++ b/docs/nidigital/examples.rst @@ -21,6 +21,15 @@ nidigital_configure_time_set_and_voltage_levels.py :encoding: utf8 :caption: `(nidigital_configure_time_set_and_voltage_levels.py) `_ +nidigital_create_pin_map.py +--------------------------- + +.. literalinclude:: ../../src/nidigital/examples/create_pin_map/nidigital_create_pin_map.py + :language: python + :linenos: + :encoding: utf8 + :caption: `(nidigital_create_pin_map.py) `_ + nidigital_ppmu_source_and_measure.py ------------------------------------ diff --git a/src/nidigital/examples/create_pin_map/Pattern.digipat b/src/nidigital/examples/create_pin_map/Pattern.digipat new file mode 100644 index 0000000000..b96d897ab0 Binary files /dev/null and b/src/nidigital/examples/create_pin_map/Pattern.digipat differ diff --git a/src/nidigital/examples/create_pin_map/PinLevels.digilevels b/src/nidigital/examples/create_pin_map/PinLevels.digilevels new file mode 100644 index 0000000000..f10c5a2c29 --- /dev/null +++ b/src/nidigital/examples/create_pin_map/PinLevels.digilevels @@ -0,0 +1,15 @@ + + + + + + 0 + 3.3 + 0.8 + 2 + 0 + HighZ + + + + \ No newline at end of file diff --git a/src/nidigital/examples/create_pin_map/PinMap.pinmap b/src/nidigital/examples/create_pin_map/PinMap.pinmap new file mode 100644 index 0000000000..72d6bfd6b6 --- /dev/null +++ b/src/nidigital/examples/create_pin_map/PinMap.pinmap @@ -0,0 +1,33 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/nidigital/examples/create_pin_map/Timing.digitiming b/src/nidigital/examples/create_pin_map/Timing.digitiming new file mode 100644 index 0000000000..a51cf7721c --- /dev/null +++ b/src/nidigital/examples/create_pin_map/Timing.digitiming @@ -0,0 +1,23 @@ + + + + + + 100 ns + + + + 0 ns + 0 ns + 100 ns + + + 50 ns + + Pattern + + + + + + \ No newline at end of file diff --git a/src/nidigital/examples/create_pin_map/create_pin_map.digiproj b/src/nidigital/examples/create_pin_map/create_pin_map.digiproj new file mode 100644 index 0000000000..5294a77b4a --- /dev/null +++ b/src/nidigital/examples/create_pin_map/create_pin_map.digiproj @@ -0,0 +1,37 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/nidigital/examples/create_pin_map/nidigital_create_pin_map.py b/src/nidigital/examples/create_pin_map/nidigital_create_pin_map.py new file mode 100644 index 0000000000..40a7596cc0 --- /dev/null +++ b/src/nidigital/examples/create_pin_map/nidigital_create_pin_map.py @@ -0,0 +1,107 @@ +#!/usr/bin/python + +import argparse +import nidigital +import os +import sys +import tempfile +from xml.etree import ElementTree + + +def _create_pin_map(resource_name, file_path): + instruments = [name.strip() for name in resource_name.split(',')] + if len(instruments) != 2 or not all(instruments) or instruments[0] == instruments[1]: + raise ValueError('This example requires two distinct instrument resource names, for example PXI1Slot2,PXI1Slot3.') + + # Python does not expose the LabVIEW pin/channel map creation APIs, so create an XML file for load_pin_map. + # The bundled PinMap.pinmap is the equivalent reference map for create_pin_map.digiproj. + pin_map = ElementTree.Element('PinMap', xmlns='http://www.ni.com/TestStand/SemiconductorModule/PinMap.xsd', schemaVersion='1.5') + instrument_list = ElementTree.SubElement(pin_map, 'Instruments') + for instrument in instruments: + ElementTree.SubElement(instrument_list, 'NIDigitalPatternInstrument', name=instrument, numberOfChannels='32', group='Digital') + + # Pin and group names must match the bundled levels, timing, and pattern files. + dut_pins = ('SCL', 'SDA', 'CS') + system_pins = ('Vcc',) + dut_pin_connections = (('SCL', '0'), ('SDA', '1')) + shared_pin_connection = {'pin': 'CS', 'siteNumber': '0,1', 'instrument': instruments[0], 'channel': '30'} + system_pin_connections = (('Vcc', instruments[0], '3'),) + + pins = ElementTree.SubElement(pin_map, 'Pins') + for pin in dut_pins: + ElementTree.SubElement(pins, 'DUTPin', name=pin) + for pin in system_pins: + ElementTree.SubElement(pins, 'SystemPin', name=pin) + + pin_groups = ElementTree.SubElement(pin_map, 'PinGroups') + pin_group = ElementTree.SubElement(pin_groups, 'PinGroup', name='I2C_bus') + for pin in system_pins + dut_pins: + ElementTree.SubElement(pin_group, 'PinReference', pin=pin) + + sites = ElementTree.SubElement(pin_map, 'Sites') + connections = ElementTree.SubElement(pin_map, 'Connections') + # Both sites share CS on the first instrument; Vcc is a system pin, not a per-site pin. + ElementTree.SubElement(connections, 'Connection', **shared_pin_connection) + for site, instrument in enumerate(instruments): + ElementTree.SubElement(sites, 'Site', siteNumber=str(site)) + for pin, channel in dut_pin_connections: + ElementTree.SubElement(connections, 'Connection', pin=pin, siteNumber=str(site), instrument=instrument, channel=channel) + for pin, instrument, channel in system_pin_connections: + ElementTree.SubElement(connections, 'SystemConnection', pin=pin, instrument=instrument, channel=channel) + + ElementTree.ElementTree(pin_map).write(file_path, encoding='utf-8', xml_declaration=True) + + +def example(resource_name, options): + # Keep the generated pin map outside the example directory and remove it after closing the session. + with tempfile.TemporaryDirectory() as temp_dir: + pin_map_filename = os.path.join(temp_dir, 'PinMap.pinmap') + _create_pin_map(resource_name, pin_map_filename) + + with nidigital.Session(resource_name=resource_name, options=options) as session: + # Load the pin map created at runtime, rather than the bundled reference map. + session.load_pin_map(pin_map_filename) + + dir = os.path.dirname(os.path.abspath(__file__)) + levels_filename = os.path.join(dir, 'PinLevels.digilevels') + timing_filename = os.path.join(dir, 'Timing.digitiming') + session.load_specifications_levels_and_timing(levels_file_paths=levels_filename, timing_file_paths=timing_filename) + + session.apply_levels_and_timing(levels_filename, timing_filename) + + pattern_filename = os.path.join(dir, 'Pattern.digipat') + session.load_pattern(pattern_filename) + + print('Start bursting pattern') + try: + session.burst_pattern(start_label='new_pattern') + finally: + session.selected_function = nidigital.SelectedFunction.DISCONNECT + print('Done bursting pattern') + + +def _main(argsv): + parser = argparse.ArgumentParser(description='Demonstrates how to create a pin map file, load levels and timing, and burst a pattern on the digital pattern instrument.', formatter_class=argparse.ArgumentDefaultsHelpFormatter) + parser.add_argument('-n', '--resource-name', default='PXI1Slot2,PXI1Slot3', help='The specified resource name of a digital pattern instrument, for example, PXI1Slot3, where PXI1Slot3 is an instrument resource name. This example requires two distinct instrument resource names in site order.') + parser.add_argument('-s', '--simulate', default='True', choices=['True', 'False'], help='Whether to run on simulated hardware or real hardware') + args = parser.parse_args(argsv) + example(args.resource_name, 'Simulate=1, DriverSetup=Model:6571' if args.simulate == 'True' else '') + + +def main(): + _main(sys.argv[1:]) + + +def test_example(): + resource_name = 'PXI1Slot2,PXI1Slot3' + options = {'simulate': True, 'driver_setup': {'Model': '6571'}, } + example(resource_name, options) + + +def test_main(): + _main([]) + _main(['--resource-name', 'PXI1Slot4,PXI1Slot5', '--simulate', 'True']) + + +if __name__ == '__main__': + main()