Travis Geiselbrecht | 1d0df69 | 2008-09-01 02:26:09 -0700 | [diff] [blame] | 1 | #!/usr/bin/python |
| 2 | |
| 3 | ## mkboard.py -- atmel pio mux utility |
| 4 | ## |
| 5 | ## Copyright 2006, Brian Swetland. All rights reserved. |
| 6 | ## See provided LICENSE file or http://frotz.net/LICENSE for details. |
| 7 | ## |
| 8 | |
| 9 | import os, sys, string |
| 10 | |
| 11 | # pindef -> num, out, pull, pio, sela, selb |
| 12 | |
| 13 | reg_output_disable = 0 |
| 14 | reg_output_enable = 0 |
| 15 | reg_pullup_disable = 0 |
| 16 | reg_pullup_enable = 0 |
| 17 | reg_pio_disable = 0 |
| 18 | reg_pio_enable = 0 |
| 19 | reg_select_a = 0 |
| 20 | reg_select_b = 0 |
| 21 | |
| 22 | def setup_registers(pindef): |
| 23 | global reg_output_disable |
| 24 | global reg_output_enable |
| 25 | global reg_pullup_disable |
| 26 | global reg_pullup_enable |
| 27 | global reg_pio_disable |
| 28 | global reg_pio_enable |
| 29 | global reg_select_a |
| 30 | global reg_select_b |
| 31 | |
| 32 | (num, out, pull, pio, sela, selb) = pindef |
| 33 | |
| 34 | bit = 1 << num |
| 35 | |
| 36 | if out: |
| 37 | reg_output_enable |= bit |
| 38 | reg_output_disable &= (~bit) |
| 39 | else: |
| 40 | reg_output_enable &= (~bit) |
| 41 | reg_output_disable |= bit |
| 42 | |
| 43 | if pull: |
| 44 | reg_pullup_enable |= bit |
| 45 | reg_pullup_disable &= (~bit) |
| 46 | else: |
| 47 | reg_pullup_enable &= (~bit) |
| 48 | reg_pullup_disable |= bit |
| 49 | |
| 50 | if pio: |
| 51 | reg_pio_enable |= bit |
| 52 | reg_pio_disable &= (~bit) |
| 53 | else: |
| 54 | reg_pio_enable &= (~bit) |
| 55 | reg_pio_disable |= bit |
| 56 | |
| 57 | if sela: |
| 58 | reg_select_a |= bit |
| 59 | if selb: |
| 60 | reg_select_b |= bit |
| 61 | |
| 62 | def import_pindef(fn): |
| 63 | pass |
| 64 | |
| 65 | def read_pins_def(fn, table): |
| 66 | output = "" |
| 67 | fd = open(fn,'r') |
| 68 | for line in fd.xreadlines(): |
| 69 | line = line.split('#')[0].strip() |
| 70 | if not line: continue |
| 71 | |
| 72 | (gpio,pa,pb) = line.split() |
| 73 | num = int(gpio[2:]) |
| 74 | |
| 75 | table[gpio+"_IN"] = (num, 0, 0, 1, 0, 0) |
| 76 | table[gpio+"_IN_PULLUP"] = (num, 0, 1, 1, 0, 0) |
| 77 | table[gpio+"_OUT"] = (num, 1, 0, 1, 0, 0) |
| 78 | table[gpio+"_"+pa] = (num, 0, 0, 0, 1, 0) |
| 79 | table[gpio+"_"+pb] = (num, 0, 0, 0, 0, 1) |
| 80 | |
| 81 | return output |
| 82 | |
| 83 | def read_board_def(fn, table): |
| 84 | pins = {} |
| 85 | output = "" |
| 86 | for n in range(0,32): |
| 87 | pins[n] = '' |
| 88 | |
| 89 | fd = open(fn,'r') |
| 90 | for line in fd.xreadlines(): |
| 91 | line = line.split('#')[0].strip() |
| 92 | if not line: continue |
| 93 | |
| 94 | if len(line.split('=')) == 2: |
| 95 | (line,func) = line.split('=') |
| 96 | line = line.strip() |
| 97 | func = func.strip() |
| 98 | else: |
| 99 | func = '' |
| 100 | |
| 101 | parts = line.split() |
| 102 | if len(parts) < 2: |
| 103 | print "ERROR: invalid definition '%s'" % line |
| 104 | sys.exit(1) |
| 105 | |
| 106 | if not func: |
| 107 | if (parts[1] == 'IN') or (parts[1] == 'OUT'): |
| 108 | func = parts[0] |
| 109 | else: |
| 110 | func = parts[1] |
| 111 | |
| 112 | pin = string.join(parts,"_") |
| 113 | |
| 114 | if not table.has_key(pin): |
| 115 | print "ERROR: pin '%s' does not exist" % pin |
| 116 | sys.exit(1) |
| 117 | |
| 118 | pindef = table[pin] |
| 119 | num = pindef[0] |
| 120 | if pins[num]: |
| 121 | print "ERROR: pin '%s' conflicts with pin '%s'" % (pin, pins[num]) |
| 122 | sys.exit(1) |
| 123 | pins[num] = pin |
| 124 | |
| 125 | setup_registers(pindef) |
| 126 | output += "#define PIN_%-12s (1 << %d)\n" % (func, num) |
| 127 | |
| 128 | return output |
| 129 | |
| 130 | table = {} |
| 131 | output = "" |
| 132 | |
| 133 | for fn in sys.argv[1:]: |
| 134 | if fn.endswith('.pins'): |
| 135 | if table: |
| 136 | print "ERROR: only one pin definition file allowed" |
| 137 | sys.exit(1) |
| 138 | output = read_pins_def(fn, table) |
| 139 | continue |
| 140 | |
| 141 | if fn.endswith('.def'): |
| 142 | if not table: |
| 143 | print "ERROR: must specify a pin definition file first" |
| 144 | sys.exit(1) |
| 145 | |
| 146 | reg_output_disable = 0xffffffffL |
| 147 | reg_output_enable = 0L |
| 148 | reg_pullup_disable = 0L |
| 149 | reg_pullup_enable = 0xffffffffL |
| 150 | reg_pio_disable = 0L |
| 151 | reg_pio_enable = 0xffffffffL |
| 152 | reg_select_a = 0L |
| 153 | reg_select_b = 0L |
| 154 | |
| 155 | output = read_board_def(fn, table) |
| 156 | fd = open(fn[:-4] + ".h", 'w') |
| 157 | fd.write("/* DO NOT EDIT -- AUTOGENERATED FROM '%s' */\n\n" % fn) |
| 158 | fd.write("#ifndef __BOARD_DEFINITION_FILE__\n") |
| 159 | fd.write("#define __BOARD_DEFINITION_FILE__\n\n") |
| 160 | fd.write(output) |
| 161 | fd.write("\n") |
| 162 | fd.write("#define BOARD_OUTPUT_DISABLE 0x%08x\n" % reg_output_disable) |
| 163 | fd.write("#define BOARD_OUTPUT_ENABLE 0x%08x\n" % reg_output_enable) |
| 164 | fd.write("#define BOARD_PULLUP_DISABLE 0x%08x\n" % reg_pullup_disable) |
| 165 | fd.write("#define BOARD_PULLUP_ENABLE 0x%08x\n" % reg_pullup_enable) |
| 166 | fd.write("#define BOARD_PIO_DISABLE 0x%08x\n" % reg_pio_disable) |
| 167 | fd.write("#define BOARD_PIO_ENABLE 0x%08x\n" % reg_pio_enable) |
| 168 | fd.write("#define BOARD_SELECT_A 0x%08x\n" % reg_select_a) |
| 169 | fd.write("#define BOARD_SELECT_B 0x%08x\n" % reg_select_b) |
| 170 | fd.write("\n#endif\n") |
| 171 | fd.close() |
| 172 | continue |
| 173 | |
| 174 | print "ERROR: what is '%s'?" % fn |
| 175 | sys.exit(1) |
| 176 | |
| 177 | |
| 178 | |