aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/staging/comedi/drivers/ni_routing/tools/convert_csv_to_c.py
blob: 532eb6372a5adb7e88d20f42e175ca0cd35b1864 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
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()