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