summaryrefslogtreecommitdiff
path: root/drivers/staging/comedi/drivers/ni_routing/tools
diff options
context:
space:
mode:
authorSpencer E. Olson <olsonse@umich.edu>2018-10-03 14:56:03 -0600
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>2018-10-09 15:34:51 +0200
commitba932fcfee28b6a23bb8a903ce5a2210ac861721 (patch)
treeeec064afaf199c02dcf0768a7f200b6117dee29d /drivers/staging/comedi/drivers/ni_routing/tools
parentd7569ad766511fe708a8bd7476baa305d1510daf (diff)
staging: comedi: ni_routing: Add NI signal routing info
See README for a thorough discussion of this content. Adds tables of all register values for routing various signals to various terminals on National Instruments hardware. This information is directly compared to and taken from register-level programming documentation and/or register-level programming examples as provided by National Instruments. Furthermore, this information was mostly compared (favorably) to the register values already used in the comedi drivers for NI hardware. Adds tables of valid routes for many devices. This information is not consistent from device to device, nor entirely consistent within device families. One additional major challenge is that this information does not seem to be obtainable in any programmatic fashion, neither through the proprietary NIDAQmx(-base) c-libraries, nor with register level programming, _nor_ through any documentation. In fact, the only consistent source of this information is through the proprietary NI-MAX software, which currently only runs on Windows platforms. A further challenge is that this information cannot be exported from NI-MAX, except by screenshot. The collection and maintenance of this information is somewhat tedious and requires frequent re-examination and comparison of NI-MAX and/or the NI-MHDDK documentation (register programming information) and NI-MHDDK examples. Tools are added with this patch to facilitate generating CSV files from the data tables. These CSV files can be used with a spreadsheet program to provide better visual comparision with screenshots gathered from NI-MAX. Tools are also added to regenerate the data tables from CSV content--this greatly enhances updating data tables with large changes (such as when adding devices). Signed-off-by: Spencer E. Olson <olsonse@umich.edu> Reviewed-by: Ian Abbott <abbotti@mev.co.uk> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Diffstat (limited to 'drivers/staging/comedi/drivers/ni_routing/tools')
-rw-r--r--drivers/staging/comedi/drivers/ni_routing/tools/.gitignore7
-rw-r--r--drivers/staging/comedi/drivers/ni_routing/tools/Makefile79
-rw-r--r--drivers/staging/comedi/drivers/ni_routing/tools/convert_c_to_py.c159
-rwxr-xr-xdrivers/staging/comedi/drivers/ni_routing/tools/convert_csv_to_c.py503
-rwxr-xr-xdrivers/staging/comedi/drivers/ni_routing/tools/convert_py_to_csv.py67
-rw-r--r--drivers/staging/comedi/drivers/ni_routing/tools/csv_collection.py40
-rwxr-xr-xdrivers/staging/comedi/drivers/ni_routing/tools/make_blank_csv.py32
-rw-r--r--drivers/staging/comedi/drivers/ni_routing/tools/ni_names.py56
8 files changed, 943 insertions, 0 deletions
diff --git a/drivers/staging/comedi/drivers/ni_routing/tools/.gitignore b/drivers/staging/comedi/drivers/ni_routing/tools/.gitignore
new file mode 100644
index 000000000000..ef38008280a9
--- /dev/null
+++ b/drivers/staging/comedi/drivers/ni_routing/tools/.gitignore
@@ -0,0 +1,7 @@
+comedi_h.py
+*.pyc
+ni_values.py
+convert_c_to_py
+c/
+csv/
+all_cfiles.c
diff --git a/drivers/staging/comedi/drivers/ni_routing/tools/Makefile b/drivers/staging/comedi/drivers/ni_routing/tools/Makefile
new file mode 100644
index 000000000000..1966850584d2
--- /dev/null
+++ b/drivers/staging/comedi/drivers/ni_routing/tools/Makefile
@@ -0,0 +1,79 @@
+# this make file is simply to help autogenerate these files:
+# ni_route_values.h
+# ni_device_routes.h
+# in order to do this, we are also generating a python representation (using
+# ctypesgen) of ../../comedi.h.
+# This allows us to sort NI signal/terminal names numerically to use a binary
+# search through the device_routes tables to find valid routes.
+
+ALL:
+ @echo Typical targets:
+ @echo "\`make csv-files\`"
+ @echo " Creates new csv-files using content of c-files of existing"
+ @echo " ni_routing/* content. New csv files are placed in csv"
+ @echo " sub-directory."
+ @echo "\`make c-files\`"
+ @echo " Creates new c-files using content of csv sub-directory. These"
+ @echo " new c-files can be compared to the active content in the"
+ @echo " ni_routing directory."
+ @echo "\`make csv-blank\`"
+ @echo " Create a new blank csv file. This is useful for establishing a"
+ @echo " new data table for either a device family \(less likely\) or a"
+ @echo " specific board of an existing device family \(more likely\)."
+ @echo "\`make clean-partial\`"
+ @echo " Remove all generated files/directories EXCEPT for csv/c files."
+ @echo "\`make clean\`"
+ @echo " Remove all generated files/directories."
+ @echo "\`make everything\`"
+ @echo " Build all csv-files, then all new c-files."
+
+everything : csv-files c-files csv-blank
+
+CPPFLAGS=-D"BIT(x)=(1UL<<(x))" -D__user=
+
+comedi_h.py : ../../../comedi.h
+ ctypesgen $< --include "sys/ioctl.h" --cpp 'gcc -E $(CPPFLAGS)' -o $@
+
+convert_c_to_py: all_cfiles.c
+ gcc -g convert_c_to_py.c -o convert_c_to_py -std=c99
+
+ni_values.py: convert_c_to_py
+ ./convert_c_to_py
+
+csv-files : ni_values.py comedi_h.py
+ ./convert_py_to_csv.py
+
+csv-blank :
+ ./make_blank_csv.py
+ @echo New blank csv signal table in csv/blank_route_table.csv
+
+c-files : comedi_h.py
+ ./convert_csv_to_c.py --route_values --device_routes
+
+ROUTE_VALUES_SRC=$(wildcard ../ni_route_values/*.c)
+DEVICE_ROUTES_SRC=$(wildcard ../ni_device_routes/*.c)
+all_cfiles.c : $(DEVICE_ROUTES_SRC) $(ROUTE_VALUES_SRC)
+ @for i in $(DEVICE_ROUTES_SRC) $(ROUTE_VALUES_SRC); do \
+ echo "#include \"$$i\"" >> all_cfiles.c; \
+ done
+
+clean-partial :
+ $(RM) -rf comedi_h.py ni_values.py convert_c_to_py all_cfiles.c *.pyc \
+ __pycache__/
+
+clean : partial_clean
+ $(RM) -rf c/ csv/
+
+# Note: One could also use ctypeslib in order to generate these files. The
+# caveat is that ctypeslib does not do a great job at handling macro functions.
+# The make rules are as follows:
+# comedi.h.xml : ../../comedi.h
+# # note that we have to use PWD here to avoid h2xml finding a system
+# # installed version of the comedilib/comedi.h file
+# h2xml ${PWD}/../../comedi.h -c -D__user="" -D"BIT(x)=(1<<(x))" \
+# -o comedi.h.xml
+#
+# comedi_h.py : comedi.h.xml
+# xml2py ./comedi.h.xml -o comedi_h.py
+# clean :
+# rm -f comedi.h.xml comedi_h.py comedi_h.pyc
diff --git a/drivers/staging/comedi/drivers/ni_routing/tools/convert_c_to_py.c b/drivers/staging/comedi/drivers/ni_routing/tools/convert_c_to_py.c
new file mode 100644
index 000000000000..dedb6f2fc678
--- /dev/null
+++ b/drivers/staging/comedi/drivers/ni_routing/tools/convert_c_to_py.c
@@ -0,0 +1,159 @@
+// SPDX-License-Identifier: GPL-2.0+
+/* vim: set ts=8 sw=8 noet tw=80 nowrap: */
+
+#include <stdint.h>
+#include <stdbool.h>
+#include <stddef.h>
+#include <errno.h>
+#include <stdlib.h>
+
+typedef uint8_t u8;
+typedef uint16_t u16;
+typedef int8_t s8;
+#define __user
+#define BIT(x) (1UL << (x))
+
+#define NI_ROUTE_VALUE_EXTERNAL_CONVERSION 1
+
+#include "../ni_route_values.c"
+#include "../ni_device_routes.c"
+#include "all_cfiles.c"
+
+#include <stdio.h>
+
+#define RVij(rv, src, dest) ((rv)->register_values[(dest)][(src)])
+
+/*
+ * write out
+ * {
+ * "family" : "<family-name>",
+ * "register_values": {
+ * <destination0>:[src0, src1, ...],
+ * <destination0>:[src0, src1, ...],
+ * ...
+ * }
+ * }
+ */
+void family_write(const struct family_route_values *rv, FILE *fp)
+{
+ fprintf(fp,
+ " \"%s\" : {\n"
+ " # dest -> {src0:val0, src1:val1, ...}\n"
+ , rv->family);
+ for (unsigned int dest = NI_NAMES_BASE;
+ dest < (NI_NAMES_BASE + NI_NUM_NAMES);
+ ++dest) {
+ unsigned int src = NI_NAMES_BASE;
+
+ for (; src < (NI_NAMES_BASE + NI_NUM_NAMES) &&
+ RVij(rv, B(src), B(dest)) == 0; ++src)
+ ;
+
+ if (src >= (NI_NAMES_BASE + NI_NUM_NAMES))
+ continue; /* no data here */
+
+ fprintf(fp, " %u : {\n", dest);
+ for (src = NI_NAMES_BASE; src < (NI_NAMES_BASE + NI_NUM_NAMES);
+ ++src) {
+ register_type r = RVij(rv, B(src), B(dest));
+ const char *M;
+
+ if (r == 0) {
+ continue;
+ } else if (MARKED_V(r)) {
+ M = "V";
+ } else if (MARKED_I(r)) {
+ M = "I";
+ } else if (MARKED_U(r)) {
+ M = "U";
+ } else {
+ fprintf(stderr,
+ "Invalid register marking %s[%u][%u] = %u\n",
+ rv->family, dest, src, r);
+ exit(1);
+ }
+
+ fprintf(fp, " %u : \"%s(%u)\",\n",
+ src, M, UNMARK(r));
+ }
+ fprintf(fp, " },\n");
+ }
+ fprintf(fp, " },\n\n");
+}
+
+bool is_valid_ni_sig(unsigned int sig)
+{
+ return (sig >= NI_NAMES_BASE) && (sig < (NI_NAMES_BASE + NI_NUM_NAMES));
+}
+
+/*
+ * write out
+ * {
+ * "family" : "<family-name>",
+ * "register_values": {
+ * <destination0>:[src0, src1, ...],
+ * <destination0>:[src0, src1, ...],
+ * ...
+ * }
+ * }
+ */
+void device_write(const struct ni_device_routes *dR, FILE *fp)
+{
+ fprintf(fp,
+ " \"%s\" : {\n"
+ " # dest -> [src0, src1, ...]\n"
+ , dR->device);
+
+ unsigned int i = 0;
+
+ while (dR->routes[i].dest != 0) {
+ if (!is_valid_ni_sig(dR->routes[i].dest)) {
+ fprintf(stderr,
+ "Invalid NI signal value [%u] for destination %s.[%u]\n",
+ dR->routes[i].dest, dR->device, i);
+ exit(1);
+ }
+
+ fprintf(fp, " %u : [", dR->routes[i].dest);
+
+ unsigned int j = 0;
+
+ while (dR->routes[i].src[j] != 0) {
+ if (!is_valid_ni_sig(dR->routes[i].src[j])) {
+ fprintf(stderr,
+ "Invalid NI signal value [%u] for source %s.[%u].[%u]\n",
+ dR->routes[i].src[j], dR->device, i, j);
+ exit(1);
+ }
+
+ fprintf(fp, "%u,", dR->routes[i].src[j]);
+
+ ++j;
+ }
+ fprintf(fp, "],\n");
+
+ ++i;
+ }
+ fprintf(fp, " },\n\n");
+}
+
+int main(void)
+{
+ FILE *fp = fopen("ni_values.py", "w");
+
+ /* write route register values */
+ fprintf(fp, "ni_route_values = {\n");
+ for (int i = 0; ni_all_route_values[i]; ++i)
+ family_write(ni_all_route_values[i], fp);
+ fprintf(fp, "}\n\n");
+
+ /* write valid device routes */
+ fprintf(fp, "ni_device_routes = {\n");
+ for (int i = 0; ni_device_routes_list[i]; ++i)
+ device_write(ni_device_routes_list[i], fp);
+ fprintf(fp, "}\n");
+
+ /* finish; close file */
+ fclose(fp);
+ return 0;
+}
diff --git a/drivers/staging/comedi/drivers/ni_routing/tools/convert_csv_to_c.py b/drivers/staging/comedi/drivers/ni_routing/tools/convert_csv_to_c.py
new file mode 100755
index 000000000000..532eb6372a5a
--- /dev/null
+++ b/drivers/staging/comedi/drivers/ni_routing/tools/convert_csv_to_c.py
@@ -0,0 +1,503 @@
+#!/usr/bin/env python3
+# SPDX-License-Identifier: GPL-2.0+
+# vim: ts=2:sw=2:et:tw=80:nowrap
+
+# This is simply to aide in creating the entries in the order of the value of
+# the device-global NI signal/terminal constants defined in comedi.h
+import comedi_h
+import os, sys, re
+from csv_collection import CSVCollection
+
+
+def c_to_o(filename, prefix='\t\t\t\t\t ni_routing/', suffix=' \\'):
+ if not filename.endswith('.c'):
+ return ''
+ return prefix + filename.rpartition('.c')[0] + '.o' + suffix
+
+
+def routedict_to_structinit_single(name, D, return_name=False):
+ Locals = dict()
+ lines = [
+ '\t.family = "{}",'.format(name),
+ '\t.register_values = {',
+ '\t\t/*',
+ '\t\t * destination = {',
+ '\t\t * source = register value,',
+ '\t\t * ...',
+ '\t\t * }',
+ '\t\t */',
+ ]
+ if (False):
+ # print table with index0:src, index1:dest
+ D0 = D # (src-> dest->reg_value)
+ #D1 : destD
+ else:
+ D0 = dict()
+ for src, destD in D.items():
+ for dest, val in destD.items():
+ D0.setdefault(dest, {})[src] = val
+
+
+ D0 = sorted(D0.items(), key=lambda i: eval(i[0], comedi_h.__dict__, Locals))
+
+ for D0_sig, D1_D in D0:
+ D1 = sorted(D1_D.items(), key=lambda i: eval(i[0], comedi_h.__dict__, Locals))
+
+ lines.append('\t\t[B({})] = {{'.format(D0_sig))
+ for D1_sig, value in D1:
+ if not re.match('[VIU]\([^)]*\)', value):
+ sys.stderr.write('Invalid register format: {}\n'.format(repr(value)))
+ sys.stderr.write(
+ 'Register values should be formatted with V(),I(),or U()\n')
+ raise RuntimeError('Invalid register values format')
+ lines.append('\t\t\t[B({})]\t= {},'.format(D1_sig, value))
+ lines.append('\t\t},')
+ lines.append('\t},')
+
+ lines = '\n'.join(lines)
+ if return_name:
+ return N, lines
+ else:
+ return lines
+
+
+def routedict_to_routelist_single(name, D, indent=1):
+ Locals = dict()
+
+ indents = dict(
+ I0 = '\t'*(indent),
+ I1 = '\t'*(indent+1),
+ I2 = '\t'*(indent+2),
+ I3 = '\t'*(indent+3),
+ I4 = '\t'*(indent+4),
+ )
+
+ if (False):
+ # data is src -> dest-list
+ D0 = D
+ keyname = 'src'
+ valname = 'dest'
+ else:
+ # data is dest -> src-list
+ keyname = 'dest'
+ valname = 'src'
+ D0 = dict()
+ for src, destD in D.items():
+ for dest, val in destD.items():
+ D0.setdefault(dest, {})[src] = val
+
+ # Sort by order of device-global names (numerically)
+ D0 = sorted(D0.items(), key=lambda i: eval(i[0], comedi_h.__dict__, Locals))
+
+ lines = [ '{I0}.device = "{name}",\n'
+ '{I0}.routes = (struct ni_route_set[]){{'
+ .format(name=name, **indents) ]
+ for D0_sig, D1_D in D0:
+ D1 = [ k for k,v in D1_D.items() if v ]
+ D1.sort(key=lambda i: eval(i, comedi_h.__dict__, Locals))
+
+ lines.append('{I1}{{\n{I2}.{keyname} = {D0_sig},\n'
+ '{I2}.{valname} = (int[]){{'
+ .format(keyname=keyname, valname=valname, D0_sig=D0_sig, **indents)
+ )
+ for D1_sig in D1:
+ lines.append( '{I3}{D1_sig},'.format(D1_sig=D1_sig, **indents) )
+ lines.append( '{I3}0, /* Termination */'.format(**indents) )
+
+ lines.append('{I2}}}\n{I1}}},'.format(**indents))
+
+ lines.append('{I1}{{ /* Termination of list */\n{I2}.{keyname} = 0,\n{I1}}},'
+ .format(keyname=keyname, **indents))
+
+ lines.append('{I0}}},'.format(**indents))
+
+ return '\n'.join(lines)
+
+
+class DeviceRoutes(CSVCollection):
+ MKFILE_SEGMENTS = 'device-route.mk'
+ SET_C = 'ni_device_routes.c'
+ ITEMS_DIR = 'ni_device_routes'
+ EXTERN_H = 'all.h'
+ OUTPUT_DIR = 'c'
+
+ output_file_top = """\
+// SPDX-License-Identifier: GPL-2.0+
+/* vim: set ts=8 sw=8 noet tw=80 nowrap: */
+/*
+ * comedi/drivers/ni_routing/{filename}
+ * List of valid routes for specific NI boards.
+ *
+ * COMEDI - Linux Control and Measurement Device Interface
+ * Copyright (C) 2016 Spencer E. Olson <olsonse@umich.edu>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ */
+
+/*
+ * The contents of this file are generated using the tools in
+ * comedi/drivers/ni_routing/tools
+ *
+ * Please use those tools to help maintain the contents of this file.
+ */
+
+#include "ni_device_routes.h"
+#include "{extern_h}"\
+""".format(filename=SET_C, extern_h=os.path.join(ITEMS_DIR, EXTERN_H))
+
+ extern_header = """\
+/* SPDX-License-Identifier: GPL-2.0+ */
+/* vim: set ts=8 sw=8 noet tw=80 nowrap: */
+/*
+ * comedi/drivers/ni_routing/{filename}
+ * List of valid routes for specific NI boards.
+ *
+ * COMEDI - Linux Control and Measurement Device Interface
+ * Copyright (C) 2016 Spencer E. Olson <olsonse@umich.edu>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ */
+
+/*
+ * The contents of this file are generated using the tools in
+ * comedi/drivers/ni_routing/tools
+ *
+ * Please use those tools to help maintain the contents of this file.
+ */
+
+#ifndef _COMEDI_DRIVERS_NI_ROUTING_NI_DEVICE_ROUTES_EXTERN_H
+#define _COMEDI_DRIVERS_NI_ROUTING_NI_DEVICE_ROUTES_EXTERN_H
+
+#include "../ni_device_routes.h"
+
+{externs}
+
+#endif //_COMEDI_DRIVERS_NI_ROUTING_NI_DEVICE_ROUTES_EXTERN_H
+"""
+
+ single_output_file_top = """\
+// SPDX-License-Identifier: GPL-2.0+
+/* vim: set ts=8 sw=8 noet tw=80 nowrap: */
+/*
+ * comedi/drivers/ni_routing/{filename}
+ * List of valid routes for specific NI boards.
+ *
+ * COMEDI - Linux Control and Measurement Device Interface
+ * Copyright (C) 2016 Spencer E. Olson <olsonse@umich.edu>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ */
+
+/*
+ * The contents of this file are generated using the tools in
+ * comedi/drivers/ni_routing/tools
+ *
+ * Please use those tools to help maintain the contents of this file.
+ */
+
+#include "../ni_device_routes.h"
+#include "{extern_h}"
+
+struct ni_device_routes {table_name} = {{\
+"""
+
+ def __init__(self, pattern='csv/device_routes/*.csv'):
+ super(DeviceRoutes,self).__init__(pattern)
+
+ def to_listinit(self):
+ chunks = [ self.output_file_top,
+ '',
+ 'struct ni_device_routes *const ni_device_routes_list[] = {'
+ ]
+ # put the sheets in lexical order of device numbers then bus
+ sheets = sorted(self.items(), key=lambda i : tuple(i[0].split('-')[::-1]) )
+
+ externs = []
+ objs = [c_to_o(self.SET_C)]
+
+ for sheet,D in sheets:
+ S = sheet.lower()
+ dev_table_name = 'ni_{}_device_routes'.format(S.replace('-','_'))
+ sheet_filename = os.path.join(self.ITEMS_DIR,'{}.c'.format(S))
+ externs.append('extern struct ni_device_routes {};'.format(dev_table_name))
+
+ chunks.append('\t&{},'.format(dev_table_name))
+
+ s_chunks = [
+ self.single_output_file_top.format(
+ filename = sheet_filename,
+ table_name = dev_table_name,
+ extern_h = self.EXTERN_H,
+ ),
+ routedict_to_routelist_single(S, D),
+ '};',
+ ]
+
+ objs.append(c_to_o(sheet_filename))
+
+ with open(os.path.join(self.OUTPUT_DIR, sheet_filename), 'w') as f:
+ f.write('\n'.join(s_chunks))
+ f.write('\n')
+
+ with open(os.path.join(self.OUTPUT_DIR, self.MKFILE_SEGMENTS), 'w') as f:
+ f.write('# This is the segment that should be included in comedi/drivers/Makefile\n')
+ f.write('ni_routing-objs\t\t\t\t+= \\\n')
+ f.write('\n'.join(objs))
+ f.write('\n')
+
+ EXTERN_H = os.path.join(self.ITEMS_DIR, self.EXTERN_H)
+ with open(os.path.join(self.OUTPUT_DIR, EXTERN_H), 'w') as f:
+ f.write(self.extern_header.format(
+ filename=EXTERN_H, externs='\n'.join(externs)))
+
+ chunks.append('\tNULL,') # terminate list
+ chunks.append('};')
+ return '\n'.join(chunks)
+
+ def save(self):
+ filename=os.path.join(self.OUTPUT_DIR, self.SET_C)
+
+ try:
+ os.makedirs(os.path.join(self.OUTPUT_DIR, self.ITEMS_DIR))
+ except:
+ pass
+ with open(filename,'w') as f:
+ f.write( self.to_listinit() )
+ f.write( '\n' )
+
+
+class RouteValues(CSVCollection):
+ MKFILE_SEGMENTS = 'route-values.mk'
+ SET_C = 'ni_route_values.c'
+ ITEMS_DIR = 'ni_route_values'
+ EXTERN_H = 'all.h'
+ OUTPUT_DIR = 'c'
+
+ output_file_top = """\
+// SPDX-License-Identifier: GPL-2.0+
+/* vim: set ts=8 sw=8 noet tw=80 nowrap: */
+/*
+ * comedi/drivers/ni_routing/{filename}
+ * Route information for NI boards.
+ *
+ * COMEDI - Linux Control and Measurement Device Interface
+ * Copyright (C) 2016 Spencer E. Olson <olsonse@umich.edu>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ */
+
+/*
+ * This file includes the tables that are a list of all the values of various
+ * signals routes available on NI hardware. In many cases, one does not
+ * explicitly make these routes, rather one might indicate that something is
+ * used as the source of one particular trigger or another (using
+ * *_src=TRIG_EXT).
+ *
+ * The contents of this file are generated using the tools in
+ * comedi/drivers/ni_routing/tools
+ *
+ * Please use those tools to help maintain the contents of this file.
+ */
+
+#include "ni_route_values.h"
+#include "{extern_h}"\
+""".format(filename=SET_C, extern_h=os.path.join(ITEMS_DIR, EXTERN_H))
+
+ extern_header = """\
+/* SPDX-License-Identifier: GPL-2.0+ */
+/* vim: set ts=8 sw=8 noet tw=80 nowrap: */
+/*
+ * comedi/drivers/ni_routing/{filename}
+ * List of valid routes for specific NI boards.
+ *
+ * COMEDI - Linux Control and Measurement Device Interface
+ * Copyright (C) 2016 Spencer E. Olson <olsonse@umich.edu>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ */
+
+/*
+ * The contents of this file are generated using the tools in
+ * comedi/drivers/ni_routing/tools
+ *
+ * Please use those tools to help maintain the contents of this file.
+ */
+
+#ifndef _COMEDI_DRIVERS_NI_ROUTING_NI_ROUTE_VALUES_EXTERN_H
+#define _COMEDI_DRIVERS_NI_ROUTING_NI_ROUTE_VALUES_EXTERN_H
+
+#include "../ni_route_values.h"
+
+{externs}
+
+#endif //_COMEDI_DRIVERS_NI_ROUTING_NI_ROUTE_VALUES_EXTERN_H
+"""
+
+ single_output_file_top = """\
+// SPDX-License-Identifier: GPL-2.0+
+/* vim: set ts=8 sw=8 noet tw=80 nowrap: */
+/*
+ * comedi/drivers/ni_routing/{filename}
+ * Route information for {sheet} boards.
+ *
+ * COMEDI - Linux Control and Measurement Device Interface
+ * Copyright (C) 2016 Spencer E. Olson <olsonse@umich.edu>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ */
+
+/*
+ * This file includes a list of all the values of various signals routes
+ * available on NI 660x hardware. In many cases, one does not explicitly make
+ * these routes, rather one might indicate that something is used as the source
+ * of one particular trigger or another (using *_src=TRIG_EXT).
+ *
+ * The contents of this file can be generated using the tools in
+ * comedi/drivers/ni_routing/tools. This file also contains specific notes to
+ * this family of devices.
+ *
+ * Please use those tools to help maintain the contents of this file, but be
+ * mindful to not lose the notes already made in this file, since these notes
+ * are critical to a complete undertsanding of the register values of this
+ * family.
+ */
+
+#include "../ni_route_values.h"
+#include "{extern_h}"
+
+const struct family_route_values {table_name} = {{\
+"""
+
+ def __init__(self, pattern='csv/route_values/*.csv'):
+ super(RouteValues,self).__init__(pattern)
+
+ def to_structinit(self):
+ chunks = [ self.output_file_top,
+ '',
+ 'const struct family_route_values *const ni_all_route_values[] = {'
+ ]
+ # put the sheets in lexical order for consistency
+ sheets = sorted(self.items(), key=lambda i : i[0] )
+
+ externs = []
+ objs = [c_to_o(self.SET_C)]
+
+ for sheet,D in sheets:
+ S = sheet.lower()
+ fam_table_name = '{}_route_values'.format(S.replace('-','_'))
+ sheet_filename = os.path.join(self.ITEMS_DIR,'{}.c'.format(S))
+ externs.append('extern const struct family_route_values {};'.format(fam_table_name))
+
+ chunks.append('\t&{},'.format(fam_table_name))
+
+ s_chunks = [
+ self.single_output_file_top.format(
+ filename = sheet_filename,
+ sheet = sheet.upper(),
+ table_name = fam_table_name,
+ extern_h = self.EXTERN_H,
+ ),
+ routedict_to_structinit_single(S, D),
+ '};',
+ ]
+
+ objs.append(c_to_o(sheet_filename))
+
+ with open(os.path.join(self.OUTPUT_DIR, sheet_filename), 'w') as f:
+ f.write('\n'.join(s_chunks))
+ f.write( '\n' )
+
+ with open(os.path.join(self.OUTPUT_DIR, self.MKFILE_SEGMENTS), 'w') as f:
+ f.write('# This is the segment that should be included in comedi/drivers/Makefile\n')
+ f.write('ni_routing-objs\t\t\t\t+= \\\n')
+ f.write('\n'.join(objs))
+ f.write('\n')
+
+ EXTERN_H = os.path.join(self.ITEMS_DIR, self.EXTERN_H)
+ with open(os.path.join(self.OUTPUT_DIR, EXTERN_H), 'w') as f:
+ f.write(self.extern_header.format(
+ filename=EXTERN_H, externs='\n'.join(externs)))
+
+ chunks.append('\tNULL,') # terminate list
+ chunks.append('};')
+ return '\n'.join(chunks)
+
+ def save(self):
+ filename=os.path.join(self.OUTPUT_DIR, self.SET_C)
+
+ try:
+ os.makedirs(os.path.join(self.OUTPUT_DIR, self.ITEMS_DIR))
+ except:
+ pass
+ with open(filename,'w') as f:
+ f.write( self.to_structinit() )
+ f.write( '\n' )
+
+
+
+if __name__ == '__main__':
+ import argparse
+ parser = argparse.ArgumentParser()
+ parser.add_argument( '--route_values', action='store_true',
+ help='Extract route values from csv/route_values/*.csv' )
+ parser.add_argument( '--device_routes', action='store_true',
+ help='Extract route values from csv/device_routes/*.csv' )
+ args = parser.parse_args()
+ KL = list()
+ if args.route_values:
+ KL.append( RouteValues )
+ if args.device_routes:
+ KL.append( DeviceRoutes )
+ if not KL:
+ parser.error('nothing to do...')
+ for K in KL:
+ doc = K()
+ doc.save()
diff --git a/drivers/staging/comedi/drivers/ni_routing/tools/convert_py_to_csv.py b/drivers/staging/comedi/drivers/ni_routing/tools/convert_py_to_csv.py
new file mode 100755
index 000000000000..b3e6472bac22
--- /dev/null
+++ b/drivers/staging/comedi/drivers/ni_routing/tools/convert_py_to_csv.py
@@ -0,0 +1,67 @@
+#!/usr/bin/env python3
+# SPDX-License-Identifier: GPL-2.0+
+# vim: ts=2:sw=2:et:tw=80:nowrap
+
+from os import path
+import os, csv
+from itertools import chain
+
+from csv_collection import CSVCollection
+from ni_names import value_to_name
+import ni_values
+
+CSV_DIR = 'csv'
+
+def iter_src_values(D):
+ return D.items()
+
+def iter_src(D):
+ for dest in D:
+ yield dest, 1
+
+def create_csv(name, D, src_iter):
+ # have to change dest->{src:val} to src->{dest:val}
+ fieldnames = [value_to_name[i] for i in sorted(D.keys())]
+ fieldnames.insert(0, CSVCollection.source_column_name)
+
+ S = dict()
+ for dest, srcD in D.items():
+ for src,val in src_iter(srcD):
+ S.setdefault(src,{})[dest] = val
+
+ S = sorted(S.items(), key = lambda src_destD : src_destD[0])
+
+
+ csv_fname = path.join(CSV_DIR, name + '.csv')
+ with open(csv_fname, 'w') as F_csv:
+ dR = csv.DictWriter(F_csv, fieldnames, delimiter=';', quotechar='"')
+ dR.writeheader()
+
+ # now change the json back into the csv dictionaries
+ rows = [
+ dict(chain(
+ ((CSVCollection.source_column_name,value_to_name[src]),),
+ *(((value_to_name[dest],v),) for dest,v in destD.items())
+ ))
+ for src, destD in S
+ ]
+
+ dR.writerows(rows)
+
+
+def to_csv():
+ for d in ['route_values', 'device_routes']:
+ try:
+ os.makedirs(path.join(CSV_DIR,d))
+ except:
+ pass
+
+ for family, dst_src_map in ni_values.ni_route_values.items():
+ create_csv(path.join('route_values',family), dst_src_map, iter_src_values)
+
+ for device, dst_src_map in ni_values.ni_device_routes.items():
+ create_csv(path.join('device_routes',device), dst_src_map, iter_src)
+
+
+if __name__ == '__main__':
+ to_csv()
diff --git a/drivers/staging/comedi/drivers/ni_routing/tools/csv_collection.py b/drivers/staging/comedi/drivers/ni_routing/tools/csv_collection.py
new file mode 100644
index 000000000000..12617329a928
--- /dev/null
+++ b/drivers/staging/comedi/drivers/ni_routing/tools/csv_collection.py
@@ -0,0 +1,40 @@
+# SPDX-License-Identifier: GPL-2.0+
+# vim: ts=2:sw=2:et:tw=80:nowrap
+
+import os, csv, glob
+
+class CSVCollection(dict):
+ delimiter=';'
+ quotechar='"'
+ source_column_name = 'Sources / Destinations'
+
+ """
+ This class is a dictionary representation of the collection of sheets that
+ exist in a given .ODS file.
+ """
+ def __init__(self, pattern, skip_commented_lines=True, strip_lines=True):
+ super(CSVCollection, self).__init__()
+ self.pattern = pattern
+ C = '#' if skip_commented_lines else 'blahblahblah'
+
+ if strip_lines:
+ strip = lambda s:s.strip()
+ else:
+ strip = lambda s:s
+
+ # load all CSV files
+ key = self.source_column_name
+ for fname in glob.glob(pattern):
+ with open(fname) as F:
+ dR = csv.DictReader(F, delimiter=self.delimiter,
+ quotechar=self.quotechar)
+ name = os.path.basename(fname).partition('.')[0]
+ D = {
+ r[key]:{f:strip(c) for f,c in r.items()
+ if f != key and f[:1] not in ['', C] and
+ strip(c)[:1] not in ['', C]}
+ for r in dR if r[key][:1] not in ['', C]
+ }
+ # now, go back through and eliminate all empty dictionaries
+ D = {k:v for k,v in D.items() if v}
+ self[name] = D
diff --git a/drivers/staging/comedi/drivers/ni_routing/tools/make_blank_csv.py b/drivers/staging/comedi/drivers/ni_routing/tools/make_blank_csv.py
new file mode 100755
index 000000000000..89c90a0ba24d
--- /dev/null
+++ b/drivers/staging/comedi/drivers/ni_routing/tools/make_blank_csv.py
@@ -0,0 +1,32 @@
+#!/usr/bin/env python3
+# SPDX-License-Identifier: GPL-2.0+
+# vim: ts=2:sw=2:et:tw=80:nowrap
+
+from os import path
+import os, csv
+
+from csv_collection import CSVCollection
+from ni_names import value_to_name
+
+CSV_DIR = 'csv'
+
+def to_csv():
+ try:
+ os.makedirs(CSV_DIR)
+ except:
+ pass
+
+ csv_fname = path.join(CSV_DIR, 'blank_route_table.csv')
+
+ fieldnames = [sig for sig_val, sig in sorted(value_to_name.items())]
+ fieldnames.insert(0, CSVCollection.source_column_name)
+
+ with open(csv_fname, 'w') as F_csv:
+ dR = csv.DictWriter(F_csv, fieldnames, delimiter=';', quotechar='"')
+ dR.writeheader()
+
+ for sig in fieldnames[1:]:
+ dR.writerow({CSVCollection.source_column_name: sig})
+
+if __name__ == '__main__':
+ to_csv()
diff --git a/drivers/staging/comedi/drivers/ni_routing/tools/ni_names.py b/drivers/staging/comedi/drivers/ni_routing/tools/ni_names.py
new file mode 100644
index 000000000000..5f9b825968b1
--- /dev/null
+++ b/drivers/staging/comedi/drivers/ni_routing/tools/ni_names.py
@@ -0,0 +1,56 @@
+# SPDX-License-Identifier: GPL-2.0+
+# vim: ts=2:sw=2:et:tw=80:nowrap
+"""
+This file helps to extract string names of NI signals as included in comedi.h
+between NI_NAMES_BASE and NI_NAMES_BASE+NI_NUM_NAMES.
+"""
+
+# This is simply to aide in creating the entries in the order of the value of
+# the device-global NI signal/terminal constants defined in comedi.h
+import comedi_h
+
+
+ni_macros = (
+ 'NI_PFI',
+ 'TRIGGER_LINE',
+ 'NI_RTSI_BRD',
+ 'NI_CtrSource',
+ 'NI_CtrGate',
+ 'NI_CtrAux',
+ 'NI_CtrA',
+ 'NI_CtrB',
+ 'NI_CtrZ',
+ 'NI_CtrArmStartTrigger',
+ 'NI_CtrInternalOutput',
+ 'NI_CtrOut',
+ 'NI_CtrSampleClock',
+)
+
+def get_ni_names():
+ name_dict = dict()
+
+ # load all the static names; start with those that do not begin with NI_
+ name_dict['PXI_Star'] = comedi_h.PXI_Star
+ name_dict['PXI_Clk10'] = comedi_h.PXI_Clk10
+
+ #load all macro values
+ for fun in ni_macros:
+ f = getattr(comedi_h, fun)
+ name_dict.update({
+ '{}({})'.format(fun,i):f(i) for i in range(1 + f(-1) - f(0))
+ })
+
+ #load everything else in ni_common_signal_names enum
+ name_dict.update({
+ k:v for k,v in comedi_h.__dict__.items()
+ if k.startswith('NI_') and (not callable(v)) and
+ comedi_h.NI_COUNTER_NAMES_MAX < v < (comedi_h.NI_NAMES_BASE + comedi_h.NI_NUM_NAMES)
+ })
+
+ # now create reverse lookup (value -> name)
+
+ val_dict = {v:k for k,v in name_dict.items()}
+
+ return name_dict, val_dict
+
+name_to_value, value_to_name = get_ni_names()