Skip to content

Instantly share code, notes, and snippets.

@niclashoyer niclashoyer/ioreg-svd.py
Last active Aug 29, 2015

Embed
What would you like to do?
Generating ioregs! macros from SVD files
from cmsis_svd.parser import SVDParser
def getaccess(field):
if field.access == 'read-only':
return ': ro'
if field.access == 'write-only':
return ': wo'
return ''
def getoffset(field):
if field.bit_width == 1:
return field.bit_offset
elif field.bit_width > 1:
return '%d..%d' % (
field.bit_offset + field.bit_width,
field.bit_offset
)
def getdescription(x):
if x.description == None:
return ' '
return ' '.join(x.description.split())
parser = SVDParser.for_packaged_svd('STMicro', 'STM32F40x.xml')
for peripheral in parser.get_device().peripherals:
print('/// {}'.format(getdescription(peripheral)))
print('ioregs! ({} @ 0x{:08x} = {{'.format(
peripheral.name,
peripheral.base_address))
for register in peripheral.registers:
print(' /// {}'.format(getdescription(register)))
print(' 0x{:02x} => reg{:d} {} {{'.format(
register.address_offset,
register.size,
register.name.lower()))
for field in register.fields:
print(' {:<6} => {:<20} //= {}'.format(
getoffset(field),
field.name.lower() +
getaccess(field) + ',',
getdescription(field)))
pprint.pprint(vars(field))
print(' },\n')
print('});')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.