def write_hwdef_header(outfilename): '''write hwdef header file''' print("Writing hwdef setup in %s" % outfilename) f = open(outfilename, 'w') f.write('''/* generated hardware definitions from hwdef.dat - DO NOT EDIT */ #pragma once #ifndef TRUE #define TRUE 1 #endif #ifndef FALSE #define FALSE 0 #endif ''') write_mcu_config(f) write_USB_config(f) write_SPI_config(f) write_ADC_config(f) write_GPIO_config(f) write_peripheral_enable(f) write_prototype_file() dma_resolver.write_dma_header(f, periph_list, mcu_type, dma_exclude=get_dma_exclude(periph_list), dma_priority=get_config('DMA_PRIORITY', default='TIM* SPI*', spaces=True), dma_noshare=get_config('DMA_NOSHARE', default='', spaces=True)) if not args.bootloader: write_PWM_config(f) write_I2C_config(f) write_UART_config(f) else: write_UART_config_bootloader(f) add_bootloader() if len(romfs) > 0: f.write('#define HAL_HAVE_AP_ROMFS_EMBEDDED_H 1\n') if mcu_series == 'STM32F100': f.write(''' /* * I/O ports initial setup, this configuration is established soon after reset * in the initialization code. * Please refer to the STM32 Reference Manual for details. */ #define PIN_MODE_OUTPUT_PP(n) (0 << (((n) & 7) * 4)) #define PIN_MODE_OUTPUT_OD(n) (4 << (((n) & 7) * 4)) #define PIN_MODE_AF_PP(n) (8 << (((n) & 7) * 4)) #define PIN_MODE_AF_OD(n) (12 << (((n) & 7) * 4)) #define PIN_MODE_ANALOG(n) (0 << (((n) & 7) * 4)) #define PIN_MODE_NOPULL(n) (4 << (((n) & 7) * 4)) #define PIN_MODE_PUD(n) (8 << (((n) & 7) * 4)) #define PIN_SPEED_MEDIUM(n) (1 << (((n) & 7) * 4)) #define PIN_SPEED_LOW(n) (2 << (((n) & 7) * 4)) #define PIN_SPEED_HIGH(n) (3 << (((n) & 7) * 4)) #define PIN_ODR_HIGH(n) (1 << (((n) & 15))) #define PIN_ODR_LOW(n) (0 << (((n) & 15))) #define PIN_PULLUP(n) (1 << (((n) & 15))) #define PIN_PULLDOWN(n) (0 << (((n) & 15))) #define PIN_UNDEFINED(n) PIN_INPUT_PUD(n) ''') else: f.write(''' /* * I/O ports initial setup, this configuration is established soon after reset * in the initialization code. * Please refer to the STM32 Reference Manual for details. */ #define PIN_MODE_INPUT(n) (0U << ((n) * 2U)) #define PIN_MODE_OUTPUT(n) (1U << ((n) * 2U)) #define PIN_MODE_ALTERNATE(n) (2U << ((n) * 2U)) #define PIN_MODE_ANALOG(n) (3U << ((n) * 2U)) #define PIN_ODR_LOW(n) (0U << (n)) #define PIN_ODR_HIGH(n) (1U << (n)) #define PIN_OTYPE_PUSHPULL(n) (0U << (n)) #define PIN_OTYPE_OPENDRAIN(n) (1U << (n)) #define PIN_OSPEED_VERYLOW(n) (0U << ((n) * 2U)) #define PIN_OSPEED_LOW(n) (1U << ((n) * 2U)) #define PIN_OSPEED_MEDIUM(n) (2U << ((n) * 2U)) #define PIN_OSPEED_HIGH(n) (3U << ((n) * 2U)) #define PIN_PUPDR_FLOATING(n) (0U << ((n) * 2U)) #define PIN_PUPDR_PULLUP(n) (1U << ((n) * 2U)) #define PIN_PUPDR_PULLDOWN(n) (2U << ((n) * 2U)) #define PIN_AFIO_AF(n, v) ((v) << (((n) % 8U) * 4U)) ''') for port in sorted(ports): f.write("/* PORT%s:\n" % port) for pin in range(pincount[port]): p = portmap[port][pin] if p.label is not None: f.write(" %s\n" % p) f.write("*/\n\n") if pincount[port] == 0: # handle blank ports for vtype in vtypes: f.write("#define VAL_GPIO%s_%-7s 0x0\n" % (port, vtype)) f.write("\n\n\n") continue for vtype in vtypes: f.write("#define VAL_GPIO%s_%-7s (" % (p.port, vtype)) first = True for pin in range(pincount[port]): p = portmap[port][pin] modefunc = getattr(p, "get_" + vtype) v = modefunc() if v is None: continue if not first: f.write(" | \\\n ") f.write(v) first = False if first: # there were no pin definitions, use 0 f.write("0") f.write(")\n\n")
def write_hwdef_header(outfilename): '''write hwdef header file''' print("Writing hwdef setup in %s" % outfilename) f = open(outfilename, 'w') f.write('''/* generated hardware definitions from hwdef.dat - DO NOT EDIT */ #pragma once #ifndef TRUE #define TRUE 1 #endif #ifndef FALSE #define FALSE 0 #endif ''') write_mcu_config(f) write_USB_config(f) write_SPI_config(f) write_ADC_config(f) write_GPIO_config(f) write_peripheral_enable(f) write_prototype_file() dma_resolver.write_dma_header(f, periph_list, mcu_type, dma_exclude=get_dma_exclude(periph_list), dma_priority=get_config('DMA_PRIORITY',default='TIM* SPI*', spaces=True), dma_noshare=get_config('DMA_NOSHARE',default='', spaces=True)) if not args.bootloader: write_PWM_config(f) write_I2C_config(f) write_UART_config(f) else: write_UART_config_bootloader(f) add_bootloader() if len(romfs) > 0: f.write('#define HAL_HAVE_AP_ROMFS_EMBEDDED_H 1\n') if mcu_series == 'STM32F100': f.write(''' /* * I/O ports initial setup, this configuration is established soon after reset * in the initialization code. * Please refer to the STM32 Reference Manual for details. */ #define PIN_MODE_OUTPUT_PP(n) (0 << (((n) & 7) * 4)) #define PIN_MODE_OUTPUT_OD(n) (4 << (((n) & 7) * 4)) #define PIN_MODE_AF_PP(n) (8 << (((n) & 7) * 4)) #define PIN_MODE_AF_OD(n) (12 << (((n) & 7) * 4)) #define PIN_MODE_ANALOG(n) (0 << (((n) & 7) * 4)) #define PIN_MODE_NOPULL(n) (4 << (((n) & 7) * 4)) #define PIN_MODE_PUD(n) (8 << (((n) & 7) * 4)) #define PIN_SPEED_MEDIUM(n) (1 << (((n) & 7) * 4)) #define PIN_SPEED_LOW(n) (2 << (((n) & 7) * 4)) #define PIN_SPEED_HIGH(n) (3 << (((n) & 7) * 4)) #define PIN_ODR_HIGH(n) (1 << (((n) & 15))) #define PIN_ODR_LOW(n) (0 << (((n) & 15))) #define PIN_PULLUP(n) (1 << (((n) & 15))) #define PIN_PULLDOWN(n) (0 << (((n) & 15))) #define PIN_UNDEFINED(n) PIN_INPUT_PUD(n) ''') else: f.write(''' /* * I/O ports initial setup, this configuration is established soon after reset * in the initialization code. * Please refer to the STM32 Reference Manual for details. */ #define PIN_MODE_INPUT(n) (0U << ((n) * 2U)) #define PIN_MODE_OUTPUT(n) (1U << ((n) * 2U)) #define PIN_MODE_ALTERNATE(n) (2U << ((n) * 2U)) #define PIN_MODE_ANALOG(n) (3U << ((n) * 2U)) #define PIN_ODR_LOW(n) (0U << (n)) #define PIN_ODR_HIGH(n) (1U << (n)) #define PIN_OTYPE_PUSHPULL(n) (0U << (n)) #define PIN_OTYPE_OPENDRAIN(n) (1U << (n)) #define PIN_OSPEED_VERYLOW(n) (0U << ((n) * 2U)) #define PIN_OSPEED_LOW(n) (1U << ((n) * 2U)) #define PIN_OSPEED_MEDIUM(n) (2U << ((n) * 2U)) #define PIN_OSPEED_HIGH(n) (3U << ((n) * 2U)) #define PIN_PUPDR_FLOATING(n) (0U << ((n) * 2U)) #define PIN_PUPDR_PULLUP(n) (1U << ((n) * 2U)) #define PIN_PUPDR_PULLDOWN(n) (2U << ((n) * 2U)) #define PIN_AFIO_AF(n, v) ((v) << (((n) % 8U) * 4U)) ''') for port in sorted(ports): f.write("/* PORT%s:\n" % port) for pin in range(pincount[port]): p = portmap[port][pin] if p.label is not None: f.write(" %s\n" % p) f.write("*/\n\n") if pincount[port] == 0: # handle blank ports for vtype in vtypes: f.write("#define VAL_GPIO%s_%-7s 0x0\n" % (port, vtype)) f.write("\n\n\n") continue for vtype in vtypes: f.write("#define VAL_GPIO%s_%-7s (" % (p.port, vtype)) first = True for pin in range(pincount[port]): p = portmap[port][pin] modefunc = getattr(p, "get_" + vtype) v = modefunc() if v is None: continue if not first: f.write(" | \\\n ") f.write(v) first = False if first: # there were no pin definitions, use 0 f.write("0") f.write(")\n\n")
def write_hwdef_header(outfilename): '''write hwdef header file''' print("Writing hwdef setup in %s" % outfilename) f = open(outfilename, 'w') f.write('''/* generated hardware definitions from hwdef.dat - DO NOT EDIT */ #pragma once ''') write_mcu_config(f) write_USB_config(f) write_I2C_config(f) write_SPI_config(f) write_PWM_config(f) write_ADC_config(f) write_GPIO_config(f) write_peripheral_enable(f) write_prototype_file() dma_resolver.write_dma_header(f, periph_list, mcu_type) write_UART_config(f) f.write(''' /* * I/O ports initial setup, this configuration is established soon after reset * in the initialization code. * Please refer to the STM32 Reference Manual for details. */ #define PIN_MODE_INPUT(n) (0U << ((n) * 2U)) #define PIN_MODE_OUTPUT(n) (1U << ((n) * 2U)) #define PIN_MODE_ALTERNATE(n) (2U << ((n) * 2U)) #define PIN_MODE_ANALOG(n) (3U << ((n) * 2U)) #define PIN_ODR_LOW(n) (0U << (n)) #define PIN_ODR_HIGH(n) (1U << (n)) #define PIN_OTYPE_PUSHPULL(n) (0U << (n)) #define PIN_OTYPE_OPENDRAIN(n) (1U << (n)) #define PIN_OSPEED_VERYLOW(n) (0U << ((n) * 2U)) #define PIN_OSPEED_LOW(n) (1U << ((n) * 2U)) #define PIN_OSPEED_MEDIUM(n) (2U << ((n) * 2U)) #define PIN_OSPEED_HIGH(n) (3U << ((n) * 2U)) #define PIN_PUPDR_FLOATING(n) (0U << ((n) * 2U)) #define PIN_PUPDR_PULLUP(n) (1U << ((n) * 2U)) #define PIN_PUPDR_PULLDOWN(n) (2U << ((n) * 2U)) #define PIN_AFIO_AF(n, v) ((v) << (((n) % 8U) * 4U)) ''') for port in sorted(ports): f.write("/* PORT%s:\n" % port) for pin in range(pincount[port]): p = portmap[port][pin] if p.label is not None: f.write(" %s\n" % p) f.write("*/\n\n") if pincount[port] == 0: # handle blank ports for vtype in vtypes: f.write("#define VAL_GPIO%s_%-7s 0x0\n" % (port, vtype)) f.write("\n\n\n") continue for vtype in vtypes: f.write("#define VAL_GPIO%s_%-7s (" % (p.port, vtype)) first = True for pin in range(pincount[port]): p = portmap[port][pin] modefunc = getattr(p, "get_" + vtype) v = modefunc() if v is None: continue if not first: f.write(" | \\\n ") f.write(v) first = False if first: # there were no pin definitions, use 0 f.write("0") f.write(")\n\n")
def write_hwdef_header(outfilename): '''write hwdef header file''' print("Writing hwdef setup in %s" % outfilename) f = open(outfilename, 'w') f.write('''/* generated hardware definitions from hwdef.dat - DO NOT EDIT */ #pragma once '''); write_mcu_config(f) write_USB_config(f) write_I2C_config(f) write_SPI_config(f) write_PWM_config(f) write_ADC_config(f) write_GPIO_config(f) write_peripheral_enable(f) write_prototype_file() dma_resolver.write_dma_header(f, periph_list, mcu_type) write_UART_config(f) f.write(''' /* * I/O ports initial setup, this configuration is established soon after reset * in the initialization code. * Please refer to the STM32 Reference Manual for details. */ #define PIN_MODE_INPUT(n) (0U << ((n) * 2U)) #define PIN_MODE_OUTPUT(n) (1U << ((n) * 2U)) #define PIN_MODE_ALTERNATE(n) (2U << ((n) * 2U)) #define PIN_MODE_ANALOG(n) (3U << ((n) * 2U)) #define PIN_ODR_LOW(n) (0U << (n)) #define PIN_ODR_HIGH(n) (1U << (n)) #define PIN_OTYPE_PUSHPULL(n) (0U << (n)) #define PIN_OTYPE_OPENDRAIN(n) (1U << (n)) #define PIN_OSPEED_VERYLOW(n) (0U << ((n) * 2U)) #define PIN_OSPEED_LOW(n) (1U << ((n) * 2U)) #define PIN_OSPEED_MEDIUM(n) (2U << ((n) * 2U)) #define PIN_OSPEED_HIGH(n) (3U << ((n) * 2U)) #define PIN_PUPDR_FLOATING(n) (0U << ((n) * 2U)) #define PIN_PUPDR_PULLUP(n) (1U << ((n) * 2U)) #define PIN_PUPDR_PULLDOWN(n) (2U << ((n) * 2U)) #define PIN_AFIO_AF(n, v) ((v) << (((n) % 8U) * 4U)) ''') for port in sorted(ports): f.write("/* PORT%s:\n" % port) for pin in range(pincount[port]): p = portmap[port][pin] if p.label is not None: f.write(" %s\n" % p) f.write("*/\n\n") if pincount[port] == 0: # handle blank ports for vtype in vtypes: f.write("#define VAL_GPIO%s_%-7s 0x0\n" % (port, vtype)) f.write("\n\n\n") continue for vtype in vtypes: f.write("#define VAL_GPIO%s_%-7s (" % (p.port, vtype)) first = True for pin in range(pincount[port]): p = portmap[port][pin] modefunc = getattr(p, "get_" + vtype) v = modefunc() if v is None: continue if not first: f.write(" | \\\n ") f.write(v) first = False if first: # there were no pin definitions, use 0 f.write("0") f.write(")\n\n")
# process input file hwdef_file = args.hwdef f = open(hwdef_file, "r") for line in f.readlines(): line = line.strip() if len(line) == 0 or line[0] == '#': continue process_line(line) outdir = args.outdir if outdir is None: outdir = os.path.dirname(hwdef_file) if not "MCU" in config: print("Missing MCU type in config") sys.exit(1) mcu_type = config['MCU'][1] print("Setup for MCU %s" % mcu_type) # build a list for peripherals for DMA resolver periph_list = build_peripheral_list() # write out pins.h write_pins_header(os.path.join(outdir, "pins.h")) dma_resolver.write_dma_header(os.path.join(outdir, "dma.h"), periph_list, mcu_type)