af_parse.py 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. #!/usr/bin/env python
  2. '''
  3. create alternate function tables
  4. This assumes a csv file extracted from the datasheet using tablula:
  5. https://github.com/tabulapdf/tabula
  6. '''
  7. import sys, csv, os
  8. def is_pin(str):
  9. '''see if a string is a valid pin name'''
  10. if len(str) < 3:
  11. return False
  12. if str[0] != 'P':
  13. return False
  14. if str[1] not in "ABCDEFGHIJK":
  15. return False
  16. try:
  17. p = int(str[2:])
  18. if p < 0 or p > 15:
  19. return False
  20. return True
  21. except ValueError:
  22. return False
  23. def pin_compare(p1, p2):
  24. '''control pin sort order'''
  25. (p1,f1) = p1.split(':')
  26. (p2,f2) = p2.split(':')
  27. port1 = p1[:2]
  28. port2 = p2[:2]
  29. pin1 = int(p1[2:])
  30. pin2 = int(p2[2:])
  31. #print(port1, pin1, port2, pin2)
  32. if port1 == port2:
  33. if pin1 == pin2:
  34. if f1 < f2:
  35. return -1
  36. return 1
  37. if pin1 < pin2:
  38. return -1
  39. return 1
  40. if port1 < port2:
  41. return -1
  42. return 1
  43. def parse_af_table(fname, table):
  44. csvt = csv.reader(open(fname,'rb'))
  45. i = 0
  46. aflist = []
  47. for row in csvt:
  48. if len(row) > 2 and row[1] == 'AF0':
  49. # it is a AF list
  50. aflist = []
  51. for s in row[1:]:
  52. if s:
  53. aflist.append(int(s[2:]))
  54. if not is_pin(row[0]):
  55. if len(row) < 2 or not is_pin(row[1]):
  56. continue
  57. row = row[1:]
  58. pin = row[0]
  59. for i in range(len(aflist)):
  60. af = aflist[i]
  61. s = row[i+1]
  62. s = s.replace('_\r', '_')
  63. s = s.replace('\r_', '_')
  64. s = s.replace('\r', '')
  65. s = s.replace(' ', '')
  66. if s == '-' or len(s) == 0:
  67. continue
  68. functions = s.split('/')
  69. for f in functions:
  70. table[pin+':'+f.upper()] = af
  71. table = {}
  72. if len(sys.argv) != 2:
  73. print("Error: expected 1 CSV file")
  74. sys.exit(1)
  75. parse_af_table(sys.argv[1], table)
  76. sys.stdout.write("AltFunction_map = {\n");
  77. sys.stdout.write('\t# format is PIN:FUNCTION : AFNUM\n')
  78. sys.stdout.write('\t# extracted from %s\n' % os.path.basename(sys.argv[1]))
  79. for k in sorted(table.keys(), cmp=pin_compare):
  80. s = '"' + k + '"'
  81. sys.stdout.write('\t%-20s\t:\t%s,\n' % (s, table[k]))
  82. sys.stdout.write("}\n");