timer: fix function definitions for uC having several output compare
[aversive.git] / config / gen_headers / parse_doc.py
1 #!/usr/bin/python
2
3 import os, sys, re
4
5 def replace(s):
6 #    print s+" ---  "+ re.sub(".*/([0-9]*) .*",r"\1",s)
7     return re.sub(".*/([0-9]*).*",r"\1",s.split("\n")[0])
8
9  
10 def get_clocks(s):
11     timer=re.sub("CS([0-3n])0.*",r"\1",s.split("\n")[0])
12
13     cut=re.search("CS[^%s]"%timer, s)
14     if cut!= None:
15         s=s[:cut.start()]
16 #        print s
17     
18     separators=[" CK", "PCK", "clkI/O", "clkT0S", "clkT2S"]
19     separators=map(lambda x:(s.count(x),x), separators)
20     separators.sort(cmp=lambda x,y:cmp(x[0],y[0]))
21     sep=separators[-1][1]
22 #    print "---> " + sep
23
24     l=s.split(sep)
25     newlist=[timer, 0, 1]
26     prev_x=0
27     
28     for i in l:
29         try:
30             x=int(replace(i))
31         except:
32             continue
33
34         if x<prev_x: newlist=[timer, 0, 1] # reinit
35
36         if x!=1:newlist.append(x)
37         prev_x=x
38
39     if re.match(".*[fF]alling.*[rR]ising.*", s):
40         newlist.append(-1)
41         newlist.append(-2)
42         
43     return newlist
44
45 def get_defs(l):
46     out=""
47     for l in clks:
48         n=l[0]
49         l=l[1:]
50         out+="/* prescalers timer %s */\n"%n
51     
52         i=0
53         for d in l:
54             if d>=0:
55                 line="#define TIMER%s_PRESCALER_DIV_%d"%(n,d)
56                 out+="%s%s%d\n"%(line, (35-len(line))*" ", i)
57             elif d==-1:
58                 line="#define TIMER%s_PRESCALER_EXT_FALL"%(n)
59                 out+="%s%s%d\n"%(line, (35-len(line))*" ", i)
60             elif d==-2:
61                 line="#define TIMER%s_PRESCALER_EXT_RISE"%(n)
62                 out+="%s%s%d\n"%(line, (35-len(line))*" ", i)
63             i+=1
64         out+="\n"
65
66         i=0
67         for d in l:
68             out+="#define TIMER%s_PRESCALER_REG_%d     %d\n"%(n,i,d)
69             i+=1
70         out+="\n"
71         out+="\n"
72     return out
73
74
75
76 if len(sys.argv) != 3:
77     print "bad args. usage: parse_doc.py DOC_DIR DST_DIR"
78     sys.exit(1)
79
80 for name in os.listdir(sys.argv[1]):
81     f=open(os.path.join(sys.argv[1],name))
82     list=[]
83     s=f.read()
84     list+=(re.findall('CS00.*topped.' + '.*\n'*15, s))
85     list+=(re.findall('CS10.*topped.' + '.*\n'*15, s))
86     list+=(re.findall('CS20.*topped.' + '.*\n'*15, s))
87     list+=(re.findall('CS30.*topped.' + '.*\n'*15, s))
88     list+=(re.findall('CSn0.*topped.' + '.*\n'*15, s))
89     print "---- %s ----"%name
90 #    print list
91     g=open(os.path.join(sys.argv[2],name),"w")
92     clks=[]
93     for i in list:
94         elt=get_clocks(i)
95         if elt[0]=='n':
96             elt[0]='1'
97             clks.append(elt[:])
98             elt[0]='3'
99             clks.append(elt)
100         else:
101             clks.append(elt)
102
103     clks.sort(cmp=lambda x,y:cmp(int(x[0]),int(y[0])))
104     defines=get_defs(clks)
105     g.write("%s\n"%defines)
106     g.close()