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