00001
00002
00003 import xml.parsers.expat
00004 import sys, re
00005
00006 not_a_register=["IO_START_ADDR",
00007 "IO_STOP_ADDR",
00008 "EXT_IO_START_ADDR",
00009 "EXT_IO_STOP_ADDR",
00010 "MEM_START_ADDR",
00011 "MEM_STOP_ADDR",
00012 ]
00013 curtree=[]
00014 curattrtree=[]
00015
00016
00017
00018
00019 prescaler_timer_num = -1
00020 prescaler_timers = {}
00021
00022 def prescaler_module():
00023 global prescaler_timer_num
00024 global prescaler_timers
00025
00026 if curtree[-1] != "module":
00027 return
00028 if not curattrtree[-1].has_key("class"):
00029 return
00030 if not curattrtree[-1]["class"].startswith("TIMER_COUNTER"):
00031 return
00032 prescaler_timer_num = int(curattrtree[-1]["class"].replace("TIMER_COUNTER_", ""))
00033 prescaler_timers[prescaler_timer_num] = []
00034
00035 def prescaler_clk():
00036 global prescaler_timer_num
00037 global prescaler_timers
00038
00039 if curtree[-3:] != ["module", "enumerator", "enum"]:
00040 return
00041 if not curattrtree[-2].has_key("name"):
00042 return
00043 if not curattrtree[-2]["name"].startswith("CLK_SEL_"):
00044 return
00045 if not curattrtree[-3].has_key("class"):
00046 return
00047 if not curattrtree[-3]["class"].startswith("TIMER_COUNTER"):
00048 return
00049 val = int(curattrtree[-1]["val"], 16)
00050 text = curattrtree[-1]["text"]
00051 if text == "No Clock Source (Stopped)":
00052 prescaler = 0
00053 elif text == "Running, No Prescaling":
00054 prescaler = 1
00055 elif text.startswith("Running, CLK/"):
00056 prescaler = int(text.split("/")[1])
00057 elif text == "Running, ExtClk Tx Falling Edge":
00058 prescaler = -1
00059 elif text == "Running, ExtClk Tx Rising Edge":
00060 prescaler = -2
00061 else:
00062 prescaler = -3
00063 prescaler_timers[prescaler_timer_num].append((val,prescaler))
00064
00065 def prescaler_module_end():
00066 global prescaler_timer_num
00067 global prescaler_timers
00068
00069 if curtree[-1] != "module":
00070 return
00071 if prescaler_timer_num == -1:
00072 return
00073 prescaler_timer_num = -1
00074
00075 def prescaler_print():
00076 global prescaler_timers
00077 keys = prescaler_timers.keys()
00078 keys.sort()
00079 for k in keys:
00080 print "/* prescalers timer %d */"%(k)
00081 for p in prescaler_timers[k]:
00082 if p[1] == -1:
00083 txt = "FALL"
00084 elif p[1] == -2:
00085 txt = "RISE"
00086 else:
00087 txt = str(p[1])
00088 txt = "#define TIMER%d_PRESCALER_DIV_%s"%(k, txt)
00089 txt = txt.ljust(40)
00090 print "%s%d"%(txt, p[0])
00091 print
00092 for p in prescaler_timers[k]:
00093 txt = "#define TIMER%d_PRESCALER_REG_%d"%(k, p[0])
00094 txt = txt.ljust(40)
00095 print "%s%d"%(txt, p[1])
00096 print
00097 print
00098
00099
00100
00101
00102
00103 timer_dict={}
00104 sigtimer_OV_dict={}
00105 sigtimer_OC_dict={}
00106 sigtimer_IC_dict={}
00107
00108 def timer_intrp(data):
00109 if len(curtree) <= 3:
00110 return
00111 if curtree[-1] != "SOURCE":
00112 return
00113 if not curtree[-2].startswith("VECTOR"):
00114 return
00115 if not data.startswith("TIMER"):
00116 return
00117 timernum = re.sub("TIMER([0-9]).*", r"\1", data)
00118 subtimernum=re.sub("TIMER[0-9].*COMP([A-C])", r"\1", data)
00119 if len(subtimernum) != 1:
00120 subtimernum=""
00121 timerid = timernum+subtimernum
00122 if data.find("OVF") != -1:
00123 timer_dict[timerid] = 1
00124 sigtimer_OV_dict[timerid] = 1
00125 elif data.find("COMP") != -1:
00126 timer_dict[timerid] = 1
00127 sigtimer_OC_dict[timerid] = 1
00128 elif data.find("CAPT") != -1:
00129 timer_dict[timerid] = 1
00130 sigtimer_IC_dict[timerid] = 1
00131
00132 def timer_intrp_print():
00133 l=timer_dict.keys()
00134 l.sort()
00135 print "/* available timers */"
00136 for k in l:
00137 print "#define TIMER%s_AVAILABLE"%k
00138 print
00139
00140 l=sigtimer_OV_dict.keys()
00141 l.sort()
00142 i=0
00143 print "/* overflow interrupt number */"
00144 for k in l:
00145 print "#define SIG_OVERFLOW%s_NUM %d"%(k,i)
00146 i+=1
00147 print "#define SIG_OVERFLOW_TOTAL_NUM %d"%i
00148 print
00149
00150 l=sigtimer_OC_dict.keys()
00151 l.sort()
00152 i=0
00153 print "/* output compare interrupt number */"
00154 for k in l:
00155 print "#define SIG_OUTPUT_COMPARE%s_NUM %d"%(k,i)
00156 i+=1
00157 print "#define SIG_OUTPUT_COMPARE_TOTAL_NUM %d"%i
00158 print
00159
00160 i=0
00161 print "/* Pwm nums */"
00162 for k in l:
00163 print "#define PWM%s_NUM %d"%(k,i)
00164 i+=1
00165 print "#define PWM_TOTAL_NUM %d"%i
00166 print
00167
00168 l=sigtimer_IC_dict.keys()
00169 l.sort()
00170 i=0
00171 print "/* input capture interrupt number */"
00172 for k in l:
00173 print "#define SIG_INPUT_CAPTURE%s_NUM %d"%(k,i)
00174 i+=1
00175 print "#define SIG_INPUT_CAPTURE_TOTAL_NUM %d"%i
00176 print
00177
00178
00179
00180
00181
00182 bits={}
00183 regs={}
00184
00185 def regs_parse():
00186 if len(curtree) <= 3:
00187 return
00188 if curtree[-1].find("_MASK") == -1:
00189 return
00190 if curtree[-3] != "IO_MEMORY":
00191 return
00192 bitname = curtree[-1].replace("_MASK", "_REG")
00193 bitname = bitname.replace("-", "_")
00194
00195 if bits.has_key(bitname) == False:
00196 bits[bitname]=[]
00197 bits[bitname].append(curtree[-2])
00198
00199 if regs.has_key(curtree[-2])==False:
00200 regs[curtree[-2]]=[]
00201 regs[curtree[-2]].append(bitname)
00202
00203 def regs_print():
00204 for r in regs.keys():
00205 print
00206 print "/* %s */"%r
00207 for b in regs[r]:
00208 if len(bits[b]) != 1:
00209 reglist = bits[b][:]
00210 reglist.remove(r)
00211 regliststr = reduce(lambda x, y: x+", "+y, reglist)
00212 print "/* #define %s %s%s */ /* dup in %s */"%(b, " "*(20-len(b)), r, regliststr)
00213 else:
00214 print "#define %s %s%s"%(b, " "*(20-len(b)), r)
00215 print
00216
00217
00218
00219
00220
00221 alt_name = None
00222 pin_name = None
00223 pins_dict = {}
00224
00225 def pins_parse(data):
00226 global alt_name, pin_name
00227
00228 if len(curtree) < 4:
00229 return
00230 if curtree[-1] not in [ "NAME", "ALT_NAME", "PIN_NAME" ]:
00231 return
00232 if not curtree[-2].startswith("PIN"):
00233 return
00234 if curtree[-4] != "PACKAGE":
00235 return
00236 pins = data[1:-1].split(":")
00237 if curtree[-1] == "NAME":
00238 alt_name = pins[0]
00239 pin_name = pins[1:]
00240 elif curtree[-1] == "ALT_NAME":
00241 alt_name = pins[0]
00242 elif curtree[-1] == "PIN_NAME":
00243 pin_name = pins
00244
00245 def pins_end():
00246 global alt_name, pin_name
00247
00248 if len(curtree) < 3:
00249 return
00250 if not curtree[-1].startswith("PIN"):
00251 return
00252 if curtree[-3] != "PACKAGE":
00253 return
00254 if alt_name != None and re.match("P[A-Z][0-7]", alt_name):
00255 pins_dict[alt_name[1:]] = pin_name
00256 alt_name = None
00257 pin_name = None
00258
00259 def pins_print():
00260 keys = pins_dict.keys()
00261 keys.sort()
00262 print "/* pins mapping */"
00263 for k in keys:
00264 for p in pins_dict[k]:
00265 p = p.replace("'", "")
00266 print "#define %s_PORT PORT%s"%(p, k[0])
00267 print "#define %s_BIT %s"%(p, k[1])
00268 print
00269 print
00270
00271
00272
00273 def start_element(name, attrs):
00274 global state, curtree, sigtimer_list, not_a_register, bits, regs
00275
00276 curtree.append(name)
00277 curattrtree.append(attrs)
00278
00279 prescaler_module()
00280 prescaler_clk()
00281
00282 regs_parse()
00283
00284
00285 def end_element(name):
00286 global state, curtree, sigtimer_list, not_a_register
00287
00288 prescaler_module_end()
00289 pins_end()
00290
00291 curtree.pop()
00292 curattrtree.pop()
00293
00294
00295 def char_data(data):
00296 timer_intrp(data)
00297 pins_parse(data)
00298
00299
00300 print """/*
00301 * Copyright Droids Corporation, Microb Technology, Eirbot (2009)
00302 *
00303 * This program is free software; you can redistribute it and/or modify
00304 * it under the terms of the GNU General Public License as published by
00305 * the Free Software Foundation; either version 2 of the License, or
00306 * (at your option) any later version.
00307 *
00308 * This program is distributed in the hope that it will be useful,
00309 * but WITHOUT ANY WARRANTY; without even the implied warranty of
00310 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00311 * GNU General Public License for more details.
00312 *
00313 * You should have received a copy of the GNU General Public License
00314 * along with this program; if not, write to the Free Software
00315 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
00316 *
00317 * Revision : $Id $
00318 *
00319 */
00320
00321 /* WARNING : this file is automatically generated by scripts.
00322 * You should not edit it. If you find something wrong in it,
00323 * write to zer0@droids-corp.org */
00324
00325 """
00326
00327 p = xml.parsers.expat.ParserCreate()
00328
00329 p.StartElementHandler = start_element
00330 p.EndElementHandler = end_element
00331 p.CharacterDataHandler = char_data
00332
00333 f=open(sys.argv[1])
00334 p.Parse(f.read(), 1)
00335
00336 prescaler_print()
00337 timer_intrp_print()
00338 regs_print()
00339 pins_print()
00340
00341 f.close()