libopencm3/scripts/irq2nvic_h

125 lines
4.0 KiB
Plaintext
Raw Normal View History

2012-10-18 10:26:41 +00:00
#!/usr/bin/env python
# This file is part of the libopencm3 project.
#
# Copyright (C) 2012 chrysn <chrysn@fsfe.org>
#
# This library is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This library 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 Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this library. If not, see <http://www.gnu.org/licenses/>.
"""Generate an nvic.h header from a small YAML file describing the interrupt
numbers.
Code generation is chosen here because the resulting C code needs to be very
repetetive (definition of the IRQ numbers, function prototypes, weak fallback
definition and vector table definition), all being very repetitive. No portable
method to achive the same thing with C preprocessor is known to the author.
(Neither is any non-portable method, for that matter.)"""
2012-10-18 10:26:41 +00:00
import sys
import yaml
template_nvic_h = '''\
2012-10-18 10:26:41 +00:00
/* This file is part of the libopencm3 project.
*
* It was generated by the irq2nvic_h script.
*/
#ifndef {includeguard}
#define {includeguard}
#include <libopencm3/cm3/nvic.h>
2012-10-18 10:26:41 +00:00
/** @defgroup CM3_nvic_defines_{partname_doxygen} User interrupts for {partname_humanreadable}
@ingroup CM3_nvic_defines
@{{*/
{irqdefinitions}
#define NVIC_IRQ_COUNT {irqcount}
/**@}}*/
#define WEAK __attribute__ ((weak))
/** @defgroup CM3_nvic_isrprototypes_{partname_doxygen} User interrupt service routines (ISR) prototypes for {partname_humanreadable}
@ingroup CM3_nvic_isrprototypes
@{{*/
{isrprototypes}
/**@}}*/
#endif /* {includeguard} */
'''
template_vector_nvic_c = '''\
/* This file is part of the libopencm3 project.
*
* It was generated by the irq2nvic_h script.
*
* This part needs to get included in the compilation unit where
* blocking_handler gets defined due to the way #pragma works.
*/
2012-10-18 10:26:41 +00:00
/** @defgroup CM3_nvic_isrpragmas_{partname_doxygen} User interrupt service routines (ISR) defaults for {partname_humanreadable}
@ingroup CM3_nvic_isrpragmas
@{{*/
{isrpragmas}
/**@}}*/
/* Initialization template for the interrupt vector table. This definition is
* used by the startup code generator (vector.c) to set the initial values for
* the interrupt handling routines to the chip family specific _isr weak
* symbols. */
#define IRQ_HANDLERS \\
{vectortableinitialization}
'''
def convert(infile, outfile_nvic, outfile_vectornvic):
2012-10-18 10:26:41 +00:00
data = yaml.load(infile)
irq2name = list(enumerate(data['irqs']) if isinstance(data['irqs'], list) else data['irqs'].items())
irqnames = [v for (k,v) in irq2name]
if isinstance(data['irqs'], list):
data['irqcount'] = len(irq2name)
else:
data['irqcount'] = max(data['irqs'].keys()) + 1
2012-10-18 10:26:41 +00:00
data['irqdefinitions'] = "\n".join('#define NVIC_%s_IRQ %d'%(v.upper(),k) for (k,v) in irq2name)
data['isrprototypes'] = "\n".join('void WEAK %s_isr(void);'%name.lower() for name in irqnames)
data['isrpragmas'] = "\n".join('#pragma weak %s_isr = blocking_handler'%name.lower() for name in irqnames)
data['vectortableinitialization'] = ', \\\n '.join('[NVIC_%s_IRQ] = %s_isr'%(name.upper(), name.lower()) for name in irqnames)
outfile_nvic.write(template_nvic_h.format(**data))
outfile_vectornvic.write(template_vector_nvic_c.format(**data))
2012-10-18 10:26:41 +00:00
def main():
infile = sys.argv[1]
if not infile.startswith('./include/libopencm3/') or not infile.endswith('/irq.yaml'):
raise ValueError("Arguent must match ./include/libopencm3/**/irq.yaml")
nvic_h = infile.replace('irq.yaml', 'nvic.h')
vector_nvic_c = infile.replace('./include/libopencm3/', './lib/').replace('irq.yaml', 'vector_nvic.c')
convert(open(infile), open(nvic_h, 'w'), open(vector_nvic_c, 'w'))
2012-10-18 10:26:41 +00:00
if __name__ == "__main__":
main()