{"id":23,"date":"2025-02-27T12:16:54","date_gmt":"2025-02-27T12:16:54","guid":{"rendered":"https:\/\/xtronix.in\/?p=23"},"modified":"2025-02-27T12:31:47","modified_gmt":"2025-02-27T12:31:47","slug":"bare-minimum-stm32-blink-without-ide","status":"publish","type":"post","link":"https:\/\/xtronix.in\/?p=23","title":{"rendered":"Bare minimum STM32 Blink without IDE"},"content":{"rendered":"\n<p><\/p>\n\n\n\n<ol>\n<li>Install <strong>GCC ARM toolchain<\/strong>.<\/li>\n\n\n\n<li>Write <strong>bare-metal or HAL-based C code<\/strong>.<\/li>\n\n\n\n<li>Create a <strong>linker script<\/strong>.<\/li>\n\n\n\n<li>Compile using <code>arm-none-eabi-gcc<\/code>.<\/li>\n\n\n\n<li>Convert to binary with <code>arm-none-eabi-objcopy<\/code>.<\/li>\n\n\n\n<li>Flash using <code>st-flash<\/code> or <code>OpenOCD<\/code>.<\/li>\n\n\n\n<li>Debug with <code>GDB<\/code> (optional).<\/li>\n<\/ol>\n\n\n\n<p><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">1. <strong>Set Up the Toolchain<\/strong><\/h3>\n\n\n\n<p>To compile code for STM32, you need an ARM GCC toolchain:<\/p>\n\n\n\n<ul>\n<li>Download <strong>ARM GCC (GNU Arm Embedded Toolchain)<\/strong>:<br><a>https:\/\/developer.arm.com\/downloads\/-\/gnu-rm<\/a><br>Extract and add the <code>bin<\/code> folder to your system <code>PATH<\/code>.<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">2. <strong>Write Your Code (Bare-metal or HAL)<\/strong><\/h3>\n\n\n\n<p>Create a <code>main.c<\/code> file with your STM32 application logic. Example:<\/p>\n\n\n\n<pre><code class=\"C\">#include \"stm32f4xx.h\"\n\nvoid delay(volatile uint32_t s) {\n    for (; s > 0; s--);\n}\n\nint main(void) {\n    RCC->AHB1ENR |= (1 << 3);  \/\/ Enable GPIOD clock\n    GPIOD->MODER |= (1 << 26); \/\/ Set PD13 as output\n\n    while (1) {\n        GPIOD->ODR ^= (1 << 13); \/\/ Toggle LED\n        delay(1000000);\n    }\n}\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">3. <strong>Create a Linker Script (<code>STM32F4.ld<\/code>)<\/strong><\/h3>\n\n\n\n<p>Define memory sections for your STM32 chip:<\/p>\n\n\n\n<p>ld<\/p>\n\n\n\n<pre><code class=\"C\">MEMORY\n{\n    FLASH (rx) : ORIGIN = 0x08000000, LENGTH = 512K\n    SRAM (rwx) : ORIGIN = 0x20000000, LENGTH = 128K\n}\n\nSECTIONS\n{\n    .text : {\n        *(.isr_vector)\n        *(.text)\n        *(.rodata)\n        . = ALIGN(4);\n    } > FLASH\n\n    .data : {\n        *(.data)\n        . = ALIGN(4);\n    } > SRAM AT > FLASH\n\n    .bss : {\n        *(.bss)\n        . = ALIGN(4);\n    } > SRAM\n}\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">4. <strong>Compile Using GCC<\/strong><\/h3>\n\n\n\n<p>Run the following command to compile your <code>main.c<\/code> file:<\/p>\n\n\n\n<p>sh<\/p>\n\n\n\n<p><code>arm-none-eabi-gcc -mcpu=cortex-m4 -mthumb -O2 -T STM32F4.ld -o main.elf main.c -nostartfiles<\/code><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">5. <strong>Convert ELF to Binary<\/strong><\/h3>\n\n\n\n<p>To flash the STM32, convert the ELF file to a binary format:<\/p>\n\n\n\n<p>sh<\/p>\n\n\n\n<p><code>arm-none-eabi-objcopy -O binary main.elf main.bin<\/code><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">6. <strong>Flash the Code<\/strong><\/h3>\n\n\n\n<p>You can use <code>st-flash<\/code> (from <a href=\"https:\/\/github.com\/stlink-org\/stlink\">stlink<\/a>) to upload the binary to your STM32 via ST-Link:<\/p>\n\n\n\n<p>sh<\/p>\n\n\n\n<p><code>st-flash write main.bin 0x08000000<\/code><\/p>\n\n\n\n<p>Alternatively, use <strong>OpenOCD<\/strong>:<\/p>\n\n\n\n<p>sh<\/p>\n\n\n\n<p><code>openocd -f interface\/stlink.cfg -f target\/stm32f4x.cfg -c \"program main.elf verify reset exit\"<\/code><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">7. <strong>Debugging (Optional)<\/strong><\/h3>\n\n\n\n<p>If you want to debug without an IDE, use <strong>GDB<\/strong> with OpenOCD:<\/p>\n\n\n\n<p>sh<\/p>\n\n\n\n<p><code>openocd -f interface\/stlink.cfg -f target\/stm32f4x.cfg<\/code><\/p>\n\n\n\n<p>Then, in another terminal:<\/p>\n\n\n\n<p>sh<\/p>\n\n\n\n<p><code>arm-none-eabi-gdb main.elf (gdb) target remote localhost:3333 (gdb) monitor reset halt (gdb) load (gdb) continue<\/code><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><\/h3>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>1. Set Up the Toolchain To compile code for STM32, you need an ARM GCC toolchain: 2. Write Your Code (Bare-metal or HAL) Create a main.c file with your STM32 application logic. Example: #include &#8220;stm32f4xx.h&#8221; void delay(volatile uint32_t s) { for (; s > 0; s&#8211;); } int main(void) { RCC->AHB1ENR |= (1 MODER |= [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"_links":{"self":[{"href":"https:\/\/xtronix.in\/index.php?rest_route=\/wp\/v2\/posts\/23"}],"collection":[{"href":"https:\/\/xtronix.in\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/xtronix.in\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/xtronix.in\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/xtronix.in\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=23"}],"version-history":[{"count":10,"href":"https:\/\/xtronix.in\/index.php?rest_route=\/wp\/v2\/posts\/23\/revisions"}],"predecessor-version":[{"id":34,"href":"https:\/\/xtronix.in\/index.php?rest_route=\/wp\/v2\/posts\/23\/revisions\/34"}],"wp:attachment":[{"href":"https:\/\/xtronix.in\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=23"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/xtronix.in\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=23"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/xtronix.in\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=23"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}