-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdsparse.py
More file actions
executable file
·194 lines (149 loc) · 5.03 KB
/
dsparse.py
File metadata and controls
executable file
·194 lines (149 loc) · 5.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
#!/usr/bin/python3
'''
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
'''
'''
Usage:
Select the section of the datasheet that you want to parse. Copy. Run. Paste.
For example, from the STM32L4 Technical Reference Manual, I'd select this
part, between the double quotes:
"
40.8.1 Control register 1 (USART_CR1)
Address offset: 0x00
Reset value: 0x00
<register info>
Bits 31:29 Reserved,
blah blah blah...
40.8.2 Control register 2 (USART_CR2)
.........
blah blah blah, to the end of the last bit of the last register
"
Copy that to the clipboard and then run the software. It will:
- Read the text from the clipboard
- Try to parse it
- Save the output to your clipboard (as well as print it).
Then you:
- Paste it into your code
- Select the next section you're interested in
- Rinse and repeat
'''
# This is tuned for ST datasheets, specifically for the TRM for the STM32L4. YMMV.
import re
import sys
import math
import time
import pyperclip
sectionSeparatorRE = r'^\d+\.\d+\.\d+'
scriptName = 'dsparse'
scriptVer = 'v1.2'
printScriptInfo = False
#infilename = "/tmp/cr1b.txt"
# These lengths determine how many tabs to use
tabWidth = 8
shortLen = (5*tabWidth)
longLen = (8*tabWidth)
intext = pyperclip.paste()
outtext = ''
def output(text=''):
global outtext
outtext += text+'\n'
print(text)
def processSection(lines):
linesDone = 0
registerNames = list()
if len(lines) < 5:
print("ERROR: Are you sure you copied the text?")
sys.exit(1)
registerbase = None
for linesStart in range(5):
registerbase = re.search(r'\((\w+)\)', lines[linesStart])
linesDone += 1
if registerbase is None:
continue
else:
break
if registerbase is None:
print("ERROR: Couldn't find register name in input")
sys.exit(1)
registerbase = registerbase.group(1)
output('/* --- '+registerbase+' values ----------------------------------------------------- */')
if printScriptInfo:
output('/* --- ('+scriptName+' '+scriptVer+' at '+time.strftime('%c')+') --- */')
doxygenGroupId = "{}_values".format(registerbase.lower())
doxygenGroupName = "{} values".format(registerbase)
output('/** @defgroup {} {}'.format(doxygenGroupId, doxygenGroupName))
output('@{*/')
output()
inSection = False
for line in lines[linesStart+1:]:
if inSection:
if re.search(sectionSeparatorRE, line) is not None:
# Start of next section, bail
break
inSection = True
linesDone += 1
if line.startswith('Bit'):
tokens = line.split(' ')
isRange = False
if line.startswith('Bits'):
isRange = True
name = re.search(r'\w+', tokens[2]).group(0)
if 'Reserved' in name:
continue
nameBits = re.search(r'\w+\[(\d+):(\d+)\]', tokens[2])
nameBits = [nameBits.group(1), nameBits.group(2)]
bit = tokens[1].split(':')
rangeLength = 1 + int(bit[0]) - int(bit[1])
rangeMask = '0x{:X}'.format((2 ** rangeLength) - 1)
else:
bit = tokens[1]
name = re.search(r'\w+', tokens[2]).group(0)
if 'Reserved' in name:
continue
description = line.split(':')[-1].strip()
startLine = '#define '+registerbase+'_'+name
if isRange:
startLine += '_MASK'
comment = '/** '+name+'['+nameBits[0]+':'+nameBits[1]+']: '+description+' */'
endLine = '('+rangeMask+' << '+bit[1]+')'
else:
comment = '/** '+name+': '+description+' */'
endLine = '(1 << '+bit+')'
if len(startLine) < shortLen:
tabs = '\t' * math.ceil((shortLen - len(startLine))/tabWidth)
elif len(startLine) < longLen:
tabs = '\t' * math.ceil((longLen - len(startLine))/tabWidth)
else:
tabs = '\t'
output(comment)
if name in registerNames:
output('/*************************************************')
output('** Fixme: Duplicate bit name in this register ****')
output('*************************************************/')
else:
registerNames.append(name)
output(startLine+tabs+endLine)
output()
# end the doxygen group and bail
output('/**@}*/')
return linesDone
def processSections(lines):
linesTodo = len(lines)
linesStart = 0
while (linesStart < linesTodo):
linesDone = processSection(lines[linesStart:])
linesStart += linesDone
output()
lines = intext.split('\n')
processSections(lines)
print("ALL DONE")
pyperclip.copy(outtext)