make-pins.py 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. #!/usr/bin/env python
  2. """Generates the pins file for the CC3200."""
  3. from __future__ import print_function
  4. import argparse
  5. import sys
  6. import csv
  7. SUPPORTED_AFS = { 'UART': ('TX', 'RX', 'RTS', 'CTS'),
  8. 'SPI': ('CLK', 'MOSI', 'MISO', 'CS0'),
  9. #'I2S': ('CLK', 'FS', 'DAT0', 'DAT1'),
  10. 'I2C': ('SDA', 'SCL'),
  11. 'TIM': ('PWM'),
  12. 'SD': ('CLK', 'CMD', 'DAT0'),
  13. 'ADC': ('CH0', 'CH1', 'CH2', 'CH3')
  14. }
  15. def parse_port_pin(name_str):
  16. """Parses a string and returns a (port, gpio_bit) tuple."""
  17. if len(name_str) < 3:
  18. raise ValueError("Expecting pin name to be at least 3 characters")
  19. if name_str[:2] != 'GP':
  20. raise ValueError("Expecting pin name to start with GP")
  21. if not name_str[2:].isdigit():
  22. raise ValueError("Expecting numeric GPIO number")
  23. port = int(int(name_str[2:]) / 8)
  24. gpio_bit = 1 << int(int(name_str[2:]) % 8)
  25. return (port, gpio_bit)
  26. class AF:
  27. """Holds the description of an alternate function"""
  28. def __init__(self, name, idx, fn, unit, type):
  29. self.name = name
  30. self.idx = idx
  31. if self.idx > 15:
  32. self.idx = -1
  33. self.fn = fn
  34. self.unit = unit
  35. self.type = type
  36. def print(self):
  37. print (' AF({:16s}, {:4d}, {:8s}, {:4d}, {:8s}), // {}'.format(self.name, self.idx, self.fn, self.unit, self.type, self.name))
  38. class Pin:
  39. """Holds the information associated with a pin."""
  40. def __init__(self, name, port, gpio_bit, pin_num):
  41. self.name = name
  42. self.port = port
  43. self.gpio_bit = gpio_bit
  44. self.pin_num = pin_num
  45. self.board_pin = False
  46. self.afs = []
  47. def add_af(self, af):
  48. self.afs.append(af)
  49. def print(self):
  50. print('// {}'.format(self.name))
  51. if len(self.afs):
  52. print('const pin_af_t pin_{}_af[] = {{'.format(self.name))
  53. for af in self.afs:
  54. af.print()
  55. print('};')
  56. print('pin_obj_t pin_{:4s} = PIN({:6s}, {:1d}, {:3d}, {:2d}, pin_{}_af, {});\n'.format(
  57. self.name, self.name, self.port, self.gpio_bit, self.pin_num, self.name, len(self.afs)))
  58. else:
  59. print('pin_obj_t pin_{:4s} = PIN({:6s}, {:1d}, {:3d}, {:2d}, NULL, 0);\n'.format(
  60. self.name, self.name, self.port, self.gpio_bit, self.pin_num))
  61. def print_header(self, hdr_file):
  62. hdr_file.write('extern pin_obj_t pin_{:s};\n'.format(self.name))
  63. class Pins:
  64. def __init__(self):
  65. self.board_pins = [] # list of pin objects
  66. def find_pin(self, port, gpio_bit):
  67. for pin in self.board_pins:
  68. if pin.port == port and pin.gpio_bit == gpio_bit:
  69. return pin
  70. def find_pin_by_num(self, pin_num):
  71. for pin in self.board_pins:
  72. if pin.pin_num == pin_num:
  73. return pin
  74. def find_pin_by_name(self, name):
  75. for pin in self.board_pins:
  76. if pin.name == name:
  77. return pin
  78. def parse_af_file(self, filename, pin_col, pinname_col, af_start_col):
  79. with open(filename, 'r') as csvfile:
  80. rows = csv.reader(csvfile)
  81. for row in rows:
  82. try:
  83. (port_num, gpio_bit) = parse_port_pin(row[pinname_col])
  84. except:
  85. continue
  86. if not row[pin_col].isdigit():
  87. raise ValueError("Invalid pin number {:s} in row {:s}".format(row[pin_col]), row)
  88. # Pin numbers must start from 0 when used with the TI API
  89. pin_num = int(row[pin_col]) - 1;
  90. pin = Pin(row[pinname_col], port_num, gpio_bit, pin_num)
  91. self.board_pins.append(pin)
  92. af_idx = 0
  93. for af in row[af_start_col:]:
  94. af_splitted = af.split('_')
  95. fn_name = af_splitted[0].rstrip('0123456789')
  96. if fn_name in SUPPORTED_AFS:
  97. type_name = af_splitted[1]
  98. if type_name in SUPPORTED_AFS[fn_name]:
  99. unit_idx = af_splitted[0][-1]
  100. pin.add_af(AF(af, af_idx, fn_name, int(unit_idx), type_name))
  101. af_idx += 1
  102. def parse_board_file(self, filename, cpu_pin_col):
  103. with open(filename, 'r') as csvfile:
  104. rows = csv.reader(csvfile)
  105. for row in rows:
  106. # Pin numbers must start from 0 when used with the TI API
  107. if row[cpu_pin_col].isdigit():
  108. pin = self.find_pin_by_num(int(row[cpu_pin_col]) - 1)
  109. else:
  110. pin = self.find_pin_by_name(row[cpu_pin_col])
  111. if pin:
  112. pin.board_pin = True
  113. def print_named(self, label, pins):
  114. print('')
  115. print('STATIC const mp_rom_map_elem_t pin_{:s}_pins_locals_dict_table[] = {{'.format(label))
  116. for pin in pins:
  117. if pin.board_pin:
  118. print(' {{ MP_ROM_QSTR(MP_QSTR_{:6s}), MP_ROM_PTR(&pin_{:6s}) }},'.format(pin.name, pin.name))
  119. print('};')
  120. print('MP_DEFINE_CONST_DICT(pin_{:s}_pins_locals_dict, pin_{:s}_pins_locals_dict_table);'.format(label, label));
  121. def print(self):
  122. for pin in self.board_pins:
  123. if pin.board_pin:
  124. pin.print()
  125. self.print_named('board', self.board_pins)
  126. print('')
  127. def print_header(self, hdr_filename):
  128. with open(hdr_filename, 'wt') as hdr_file:
  129. for pin in self.board_pins:
  130. if pin.board_pin:
  131. pin.print_header(hdr_file)
  132. def print_qstr(self, qstr_filename):
  133. with open(qstr_filename, 'wt') as qstr_file:
  134. pin_qstr_set = set([])
  135. af_qstr_set = set([])
  136. for pin in self.board_pins:
  137. if pin.board_pin:
  138. pin_qstr_set |= set([pin.name])
  139. for af in pin.afs:
  140. af_qstr_set |= set([af.name])
  141. print('// Board pins', file=qstr_file)
  142. for qstr in sorted(pin_qstr_set):
  143. print('Q({})'.format(qstr), file=qstr_file)
  144. print('\n// Pin AFs', file=qstr_file)
  145. for qstr in sorted(af_qstr_set):
  146. print('Q({})'.format(qstr), file=qstr_file)
  147. def main():
  148. parser = argparse.ArgumentParser(
  149. prog="make-pins.py",
  150. usage="%(prog)s [options] [command]",
  151. description="Generate board specific pin file"
  152. )
  153. parser.add_argument(
  154. "-a", "--af",
  155. dest="af_filename",
  156. help="Specifies the alternate function file for the chip",
  157. default="cc3200_af.csv"
  158. )
  159. parser.add_argument(
  160. "-b", "--board",
  161. dest="board_filename",
  162. help="Specifies the board file",
  163. )
  164. parser.add_argument(
  165. "-p", "--prefix",
  166. dest="prefix_filename",
  167. help="Specifies beginning portion of generated pins file",
  168. default="cc3200_prefix.c"
  169. )
  170. parser.add_argument(
  171. "-q", "--qstr",
  172. dest="qstr_filename",
  173. help="Specifies name of generated qstr header file",
  174. default="build/pins_qstr.h"
  175. )
  176. parser.add_argument(
  177. "-r", "--hdr",
  178. dest="hdr_filename",
  179. help="Specifies name of generated pin header file",
  180. default="build/pins.h"
  181. )
  182. args = parser.parse_args(sys.argv[1:])
  183. pins = Pins()
  184. print('// This file was automatically generated by make-pins.py')
  185. print('//')
  186. if args.af_filename:
  187. print('// --af {:s}'.format(args.af_filename))
  188. pins.parse_af_file(args.af_filename, 0, 1, 3)
  189. if args.board_filename:
  190. print('// --board {:s}'.format(args.board_filename))
  191. pins.parse_board_file(args.board_filename, 1)
  192. if args.prefix_filename:
  193. print('// --prefix {:s}'.format(args.prefix_filename))
  194. print('')
  195. with open(args.prefix_filename, 'r') as prefix_file:
  196. print(prefix_file.read())
  197. pins.print()
  198. pins.print_qstr(args.qstr_filename)
  199. pins.print_header(args.hdr_filename)
  200. if __name__ == "__main__":
  201. main()