Code Generation
The firmware agent generates hardware-aware code using your MCU's register maps, clock constraints, and peripheral configurations. All generated code includes datasheet citations.
Generators
| Tool | Output |
|---|---|
fw_generate_peripheral_init | Peripheral initialization code (SPI, I2C, UART, ADC, etc.) |
fw_generate_gpio_config | GPIO pin configuration with alternate functions |
fw_generate_dma_config | DMA channel/stream configuration |
fw_generate_clock_config | Clock tree and PLL configuration |
fw_generate_isr | Interrupt service routine stubs with vector table |
fw_generate_linker_script | GNU ld linker script from MCU memory map |
fw_generate_rtos_task | RTOS task template (FreeRTOS, Zephyr, Embassy) |
fw_generate_init_sequence | Full hardware initialization sequence |
Peripheral Initialization
> fw_generate_peripheral_init USART1 baudrate=115200 wordLength=8 parity=none stopBits=1Generates complete initialization code including:
- Clock enable for the peripheral and GPIO port
- GPIO alternate function configuration
- Peripheral register configuration
- NVIC interrupt enable (if applicable)
- Proper initialization order
Supported peripherals: GPIO, UART/USART, SPI, I2C, ADC, DAC, DMA, Timer, RTC, PWM, CAN, USB, Ethernet, SDIO, RNG, DCMI, FSMC, QSPI
GPIO Configuration
> fw_generate_gpio_config PA5 mode=output speed=high pullup=none> fw_generate_gpio_config PA9 af=USART1_TX speed=very_highValidates against the MCU's AF database to ensure the pin supports the requested function.
DMA Configuration
> fw_generate_dma_config peripheral=USART1_RX buffer=rx_buf size=256 circular=trueGenerates DMA stream/channel configuration with:
- Correct stream/channel assignment for the peripheral
- Priority, FIFO, burst configuration
- Circular or normal mode
- Transfer complete interrupt handler
Clock Configuration
> fw_generate_clock_config target_sysclk=168MHz hse=8MHzSolves PLL parameters (M, N, P, Q) within the MCU's constraints and generates the clock initialization code.
ISR Generation
> fw_generate_isr DMA1_Stream0 USART1Generates ISR stubs with:
- Correct vector table names
- Flag clearing
- Typical handler patterns (e.g., DMA transfer complete, UART RX)
- MISRA-compliant volatile access patterns
Linker Script
> fw_generate_linker_scriptGenerates a GNU ld .ld script populated from the MCU's memory map:
- Flash origin and length
- RAM regions (SRAM1, SRAM2, CCM, ITCM, DTCM)
- Stack and heap configuration
- Section placement (.text, .data, .bss, .rodata)
- Interrupt vector table placement
RTOS Task Template
> fw_generate_rtos_task name=sensor_read stack=512 priority=3Generates a task template for the configured RTOS:
- FreeRTOS:
xTaskCreatewith task function - Zephyr:
K_THREAD_DEFINEmacro - Embassy: async task with
#[embassy_executor::task]
Init Sequence
> fw_generate_init_sequenceGenerates a complete initialization sequence for the project:
- System clock configuration
- GPIO initialization
- Peripheral initialization (in dependency order)
- NVIC priorities
- DMA setup
- Main loop entry
Code Quality
All generated code follows:
- MISRA C patterns (volatile pointers, explicit casts, no implicit conversions)
- Datasheet-cited comments (register descriptions from SVD)
- Language-appropriate style (C, C++, or Rust)
- No dynamic allocation in interrupt context
Last edited