Skip to content

Instantly share code, notes, and snippets.

@niclashoyer
Last active August 29, 2015 14:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save niclashoyer/06e9c26076af1b2fd485 to your computer and use it in GitHub Desktop.
Save niclashoyer/06e9c26076af1b2fd485 to your computer and use it in GitHub Desktop.
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