11#!/usr/bin/env python
2- """Generates the pins files for the CC3200."""
2+ """Generates the pins file for the CC3200."""
33
44from __future__ import print_function
55
88import csv
99
1010
11+ SUPPORTED_AFS = { 'UART' : ('TX' , 'RX' , 'RTS' , 'CTS' ),
12+ 'SPI' : ('CLK' , 'MOSI' , 'MISO' , 'CS0' ),
13+ #'I2S': ('CLK', 'FS', 'DAT0', 'DAT1'),
14+ 'I2C' : ('SDA' , 'SCL' ),
15+ 'TIM' : ('PWM0' , 'PWM1' , 'CC0' , 'CC1' ),
16+ 'SD' : ('CLK' , 'CMD' , 'DAT0' ),
17+ 'ADC' : ('CH0' , 'CH1' , 'CH2' , 'CH3' )
18+ }
19+
1120def parse_port_pin (name_str ):
1221 """Parses a string and returns a (port, gpio_bit) tuple."""
1322 if len (name_str ) < 3 :
@@ -21,53 +30,64 @@ def parse_port_pin(name_str):
2130 return (port , gpio_bit )
2231
2332
24- class Pin (object ):
33+ class AF :
34+ """Holds the description of an alternate function"""
35+ def __init__ (self , name , idx , fn , unit , type ):
36+ self .name = name
37+ self .idx = idx
38+ self .fn = fn
39+ self .unit = unit
40+ self .type = type
41+
42+ def print (self ):
43+ print (' AF({:16s}, {:4d}, {:8s}, {:4d}, {:8s}), // {}' .format (self .name , self .idx , self .fn , self .unit , self .type , self .name ))
44+
45+ class Pin :
2546 """Holds the information associated with a pin."""
2647 def __init__ (self , name , port , gpio_bit , pin_num ):
2748 self .name = name
2849 self .port = port
2950 self .gpio_bit = gpio_bit
3051 self .pin_num = pin_num
3152 self .board_pin = False
53+ self .afs = []
3254
33- def cpu_pin_name (self ):
34- return self .name
35-
36- def is_board_pin (self ):
37- return self .board_pin
38-
39- def set_is_board_pin (self ):
40- self .board_pin = True
55+ def add_af (self , af ):
56+ self .afs .append (af )
4157
4258 def print (self ):
43- print ('pin_obj_t pin_{:6s} = PIN({:6s}, {:1d}, {:3d}, {:2d});' .format (
44- self .name , self .name , self .port , self .gpio_bit , self .pin_num ))
59+ print ('// {}' .format (self .name ))
60+ print ('const pin_af_t pin_{}_af[] = {{' .format (self .name ))
61+ for af in self .afs :
62+ af .print ()
63+ print ('};' )
64+ print ('pin_obj_t pin_{:4s} = PIN({:6s}, {:1d}, {:3d}, {:2d}, pin_{}_af, {});\n ' .format (
65+ self .name , self .name , self .port , self .gpio_bit , self .pin_num , self .name , len (self .afs )))
4566
4667 def print_header (self , hdr_file ):
4768 hdr_file .write ('extern pin_obj_t pin_{:s};\n ' .format (self .name ))
4869
4970
50- class Pins (object ):
51-
71+ class Pins :
5272 def __init__ (self ):
53- self .cpu_pins = [] # list of pin objects
73+ self .board_pins = [] # list of pin objects
5474
5575 def find_pin (self , port , gpio_bit ):
56- for pin in self .cpu_pins :
76+ for pin in self .board_pins :
5777 if pin .port == port and pin .gpio_bit == gpio_bit :
5878 return pin
5979
6080 def find_pin_by_num (self , pin_num ):
61- for pin in self .cpu_pins :
81+ for pin in self .board_pins :
6282 if pin .pin_num == pin_num :
6383 return pin
6484
6585 def find_pin_by_name (self , name ):
66- for pin in self .cpu_pins :
86+ for pin in self .board_pins :
6787 if pin .name == name :
6888 return pin
6989
70- def parse_af_file (self , filename , pin_col , pinname_col ):
90+ def parse_af_file (self , filename , pin_col , pinname_col , af_start_col ):
7191 with open (filename , 'r' ) as csvfile :
7292 rows = csv .reader (csvfile )
7393 for row in rows :
@@ -76,11 +96,21 @@ def parse_af_file(self, filename, pin_col, pinname_col):
7696 except :
7797 continue
7898 if not row [pin_col ].isdigit ():
79- raise ValueError ("Invalid pin number: {:s} in row {:s}" .format (row [pin_col ]), row )
99+ raise ValueError ("Invalid pin number {:s} in row {:s}" .format (row [pin_col ]), row )
80100 # Pin numbers must start from 0 when used with the TI API
81101 pin_num = int (row [pin_col ]) - 1 ;
82102 pin = Pin (row [pinname_col ], port_num , gpio_bit , pin_num )
83- self .cpu_pins .append (pin )
103+ self .board_pins .append (pin )
104+ af_idx = 0
105+ for af in row [af_start_col :]:
106+ af_splitted = af .split ('_' )
107+ fn_name = af_splitted [0 ].rstrip ('0123456789' )
108+ if fn_name in SUPPORTED_AFS :
109+ type_name = af_splitted [1 ]
110+ if type_name in SUPPORTED_AFS [fn_name ]:
111+ unit_idx = af_splitted [0 ][- 1 ]
112+ pin .add_af (AF (af , af_idx , fn_name , int (unit_idx ), type_name ))
113+ af_idx += 1
84114
85115 def parse_board_file (self , filename , cpu_pin_col ):
86116 with open (filename , 'r' ) as csvfile :
@@ -92,37 +122,44 @@ def parse_board_file(self, filename, cpu_pin_col):
92122 else :
93123 pin = self .find_pin_by_name (row [cpu_pin_col ])
94124 if pin :
95- pin .set_is_board_pin ()
125+ pin .board_pin = True
96126
97127 def print_named (self , label , pins ):
98128 print ('' )
99129 print ('STATIC const mp_map_elem_t pin_{:s}_pins_locals_dict_table[] = {{' .format (label ))
100130 for pin in pins :
101- if pin .is_board_pin () :
102- print (' {{ MP_OBJ_NEW_QSTR(MP_QSTR_{:6s}), (mp_obj_t)&pin_{:6s} }},' .format (pin .cpu_pin_name (), pin .cpu_pin_name () ))
131+ if pin .board_pin :
132+ print (' {{ MP_OBJ_NEW_QSTR(MP_QSTR_{:6s}), (mp_obj_t)&pin_{:6s} }},' .format (pin .name , pin .name ))
103133 print ('};' )
104134 print ('MP_DEFINE_CONST_DICT(pin_{:s}_pins_locals_dict, pin_{:s}_pins_locals_dict_table);' .format (label , label ));
105135
106136 def print (self ):
107- for pin in self .cpu_pins :
108- if pin .is_board_pin () :
137+ for pin in self .board_pins :
138+ if pin .board_pin :
109139 pin .print ()
110- self .print_named ('board' , self .cpu_pins )
140+ self .print_named ('board' , self .board_pins )
111141 print ('' )
112142
113143 def print_header (self , hdr_filename ):
114144 with open (hdr_filename , 'wt' ) as hdr_file :
115- for pin in self .cpu_pins :
116- if pin .is_board_pin () :
145+ for pin in self .board_pins :
146+ if pin .board_pin :
117147 pin .print_header (hdr_file )
118148
119149 def print_qstr (self , qstr_filename ):
120150 with open (qstr_filename , 'wt' ) as qstr_file :
121- qstr_set = set ([])
122- for pin in self .cpu_pins :
123- if pin .is_board_pin ():
124- qstr_set |= set ([pin .cpu_pin_name ()])
125- for qstr in sorted (qstr_set ):
151+ pin_qstr_set = set ([])
152+ af_qstr_set = set ([])
153+ for pin in self .board_pins :
154+ if pin .board_pin :
155+ pin_qstr_set |= set ([pin .name ])
156+ for af in pin .afs :
157+ af_qstr_set |= set ([af .name ])
158+ print ('// Board pins' , file = qstr_file )
159+ for qstr in sorted (pin_qstr_set ):
160+ print ('Q({})' .format (qstr ), file = qstr_file )
161+ print ('\n // Pin AFs' , file = qstr_file )
162+ for qstr in sorted (af_qstr_set ):
126163 print ('Q({})' .format (qstr ), file = qstr_file )
127164
128165
@@ -169,12 +206,12 @@ def main():
169206 print ('//' )
170207 if args .af_filename :
171208 print ('// --af {:s}' .format (args .af_filename ))
172- pins .parse_af_file (args .af_filename , 0 , 1 )
209+ pins .parse_af_file (args .af_filename , 0 , 1 , 3 )
173210
174211 if args .board_filename :
175212 print ('// --board {:s}' .format (args .board_filename ))
176- pins .parse_board_file (args .board_filename , 1 )
177-
213+ pins .parse_board_file (args .board_filename , 1 )
214+
178215 if args .prefix_filename :
179216 print ('// --prefix {:s}' .format (args .prefix_filename ))
180217 print ('' )
0 commit comments