switching to python2 and added pep8 auto-sorter to make.
[pinmux.git] / src / actual_pinmux.py
1 from parse import *
2 from string import digits
3 try:
4 from string import maketrans
5 except ImportError:
6 maketrans = str.maketrans
7
8
9 # dictionary of properties of signals that are supported.
10 dictionary = {
11 "uart_rx" : "input",
12 "uart_tx" : "output",
13 "spi_sclk" : "output",
14 "spi_mosi" : "output",
15 "spi_ss" : "output",
16 "spi_miso" : "input",
17 "twi_sda" : "inout",
18 "twi_scl" : "inout",
19 "sd_clk": "output",
20 "sd_cmd": "output",
21 "sd_d": "inout",
22 "pwm": "output"
23 }
24
25
26 # ============== common bsv templates ============ #
27 # first argument is the io-cell number being assigned.
28 # second argument is the mux value.
29 # Third argument is the signal from the pinmap file
30 mux_wire = '''
31 rule assign_{2}_on_cell{0}(wrmux{0}=={1});
32 {2}<=cell{0}_in;
33 endrule
34 '''
35 dedicated_wire = '''
36 rule assign_{1}_on_cell{0};
37 {1}<=cell{0}_in;
38 endrule
39 '''
40 # ============================================================
41 pinmux = ''' '''
42 digits = maketrans('0123456789', ' '*10) # delete space later
43
44 for cell in muxed_cells:
45 pinmux = pinmux + " cell" + str(cell[0]) + "_out="
46 i = 0
47 while(i < len(cell) - 1):
48 pinmux = pinmux + "wrmux" + \
49 str(cell[0]) + "==" + str(i) + "?" + cell[i + 1] + "_io:"
50 if(i + 2 == len(cell) - 1):
51 pinmux = pinmux + cell[i + 2] + "_io"
52 i = i + 2
53 else:
54 i = i + 1
55 pinmux = pinmux + ";\n"
56 # ======================================================== #
57
58 # check each cell if "peripheral input/inout" then assign its wire
59 # Here we check the direction of each signal in the dictionary.
60 # We choose to keep the dictionary within the code and not user-input
61 # since the interfaces are always standard and cannot change from
62 # user-to-user. Plus this also reduces human-error as well :)
63 for i in range(0, len(cell) - 1):
64 temp = cell[i + 1].translate(digits)
65 temp = temp.replace(' ', '')
66 x = dictionary.get(temp)
67 if(x is None):
68 print(
69 "ERROR: The signal : " +
70 str(cell[i + 1]) +
71 " of pinmap.txt isn't present in the current dictionary.\
72 \nUpdate dictionary or fix-typo.")
73 exit(1)
74 if(x == "input"):
75 pinmux = pinmux + \
76 mux_wire.format(cell[0], i, "wr" + cell[i + 1]) + "\n"
77 elif(x == "inout"):
78 pinmux = pinmux + \
79 mux_wire.format(cell[0], i, "wr" + cell[i + 1] +
80 "_in") + "\n"
81 # ============================================================ #
82
83 # ================== Logic for dedicated pins ========= #
84 for cell in dedicated_cells:
85 pinmux = pinmux + " cell" + \
86 str(cell[0]) + "_out=" + cell[1] + "_io;\n"
87 temp = cell[1].translate(digits)
88 x = dictionary.get(temp)
89 if(x == "input"):
90 pinmux = pinmux + \
91 dedicated_wire.format(cell[0], "wr" + cell[1]) + "\n"
92 elif(x == "inout"):
93 pinmux = pinmux + \
94 dedicated_wire.format(cell[0], "wr" + cell[1] + "_in") + "\n"
95 # =======================================================#