|
| 1 | +--- |
| 2 | +id: code-gen-stm32 |
| 3 | +title: STM32 Code Generation |
| 4 | +sidebar_position: 1 |
| 5 | +--- |
| 6 | + |
| 7 | +# STM32 Code Generation |
| 8 | + |
| 9 | +LibXR provides the `xr_cubemx_cfg` command to automatically generate C++ initialization code from an STM32CubeMX project. This command wraps several tools including configuration parsing, code generation, interrupt patching, and CMake integration. |
| 10 | + |
| 11 | +--- |
| 12 | + |
| 13 | +## Quick Start |
| 14 | + |
| 15 | +In the root directory of your STM32CubeMX project, run: |
| 16 | + |
| 17 | +```bash |
| 18 | +xr_cubemx_cfg -d . |
| 19 | +``` |
| 20 | + |
| 21 | +This command will perform the following steps automatically: |
| 22 | + |
| 23 | +1. Initialize or update the `libxr` submodule |
| 24 | +2. Locate the `.ioc` file and convert it into `.config.yaml` |
| 25 | +3. Generate `app_main.cpp` with initialization code |
| 26 | +4. Patch interrupt handler files |
| 27 | +5. Modify `CMakeLists.txt` to integrate LibXR |
| 28 | + |
| 29 | +--- |
| 30 | + |
| 31 | +## Example Output |
| 32 | + |
| 33 | +```text |
| 34 | +[INFO] LibXR submodule already exists. Checking for updates... |
| 35 | +[INFO] [OK] cd . && git submodule update --init --recursive |
| 36 | +[INFO] LibXR submodule updated. |
| 37 | +Found .ioc file: .\atom.ioc |
| 38 | +Parsing .ioc file... |
| 39 | +[INFO] [OK] xr_parse_ioc -d . -o .\.config.yaml |
| 40 | +Generating C++ code... |
| 41 | +[INFO] [OK] xr_gen_code_stm32 -i .\.config.yaml -o .\User\app_main.cpp |
| 42 | +Modifying STM32 interrupt files... |
| 43 | +[INFO] [OK] xr_stm32_it .\Core/Src |
| 44 | +[INFO] [OK] xr_stm32_cmake . |
| 45 | +[INFO] [Pass] All tasks completed successfully! |
| 46 | +``` |
| 47 | + |
| 48 | +--- |
| 49 | + |
| 50 | +## Output Structure |
| 51 | + |
| 52 | +After execution, your project directory will contain the following generated or modified files: |
| 53 | + |
| 54 | +```txt |
| 55 | +. |
| 56 | +├── .config.yaml # Parsed configuration from .ioc |
| 57 | +├── User/ |
| 58 | +│ ├── app_main.cpp # Main initialization code |
| 59 | +│ ├── app_main.h # Header for app_main |
| 60 | +│ ├── libxr_config.yaml # LibXR runtime configuration |
| 61 | +│ └── flash_map.hpp # Flash address mapping table |
| 62 | +├── Core/Src/stm32f1xx_it.c # Patched interrupt handlers |
| 63 | +├── cmake/LibXR.CMake # CMake build config for LibXR |
| 64 | +├── CMakeLists.txt # Modified to include LibXR |
| 65 | +└── Middlewares/Third_Party/LibXR # LibXR as a Git submodule |
| 66 | +``` |
| 67 | + |
| 68 | +--- |
| 69 | + |
| 70 | +## How to Use |
| 71 | + |
| 72 | +Call `app_main()` at the appropriate entry point in your project: |
| 73 | + |
| 74 | +### Bare-metal Project |
| 75 | + |
| 76 | +```cpp |
| 77 | +#include "app_main.h" |
| 78 | + |
| 79 | +int main() { |
| 80 | + HAL_Init(); |
| 81 | + SystemClock_Config(); |
| 82 | + ... |
| 83 | + app_main(); // Initialize peripherals and start application |
| 84 | + while (1) { |
| 85 | + ... |
| 86 | + } |
| 87 | +} |
| 88 | +``` |
| 89 | + |
| 90 | +### FreeRTOS Project |
| 91 | + |
| 92 | +Place `app_main()` in the entry function of the main thread. |
| 93 | + |
| 94 | +--- |
| 95 | + |
| 96 | +## Optional Arguments |
| 97 | + |
| 98 | +| Argument | Description | |
| 99 | +| ---------- | --------------------------------------- | |
| 100 | +| `-d` | Specify STM32 project root directory | |
| 101 | +| `-t` | Set terminal peripheral (e.g. `usart1`) | |
| 102 | +| `-c` | Enable Clang build support | |
| 103 | +| `--xrobot` | Generate glue code for XRobot modules | |
| 104 | + |
| 105 | +--- |
| 106 | + |
| 107 | +## Project Requirements |
| 108 | + |
| 109 | +- Must be a CMake project exported from STM32CubeMX |
| 110 | +- Must contain a valid `.ioc` file |
| 111 | + |
| 112 | +--- |
| 113 | + |
| 114 | +## Subcommands (Internally used by `xr_cubemx_cfg`, can also be run separately) |
| 115 | + |
| 116 | +| Tool | Description | |
| 117 | +| ------------------- | ------------------------------------------------ | |
| 118 | +| `xr_parse_ioc` | Parses `.ioc` and generates `.config.yaml` | |
| 119 | +| `xr_gen_code_stm32` | Generates `app_main.cpp` from the YAML config | |
| 120 | +| `xr_stm32_it` | Patches interrupt handlers with UART/USB support | |
| 121 | +| `xr_stm32_cmake` | Integrates LibXR into the project build system | |
| 122 | + |
| 123 | +--- |
| 124 | + |
| 125 | +## References |
| 126 | + |
| 127 | +[LibXR CLI tool and documentation (PyPI)](https://pypi.org/project/libxr/) |
0 commit comments