Build it step by step
0 of 7 doneInstall PlatformIO
Select the Blue Pill board.
Connect the ST-Link
Four wires over SWD.
Build and upload an empty program
Check it connects.
Enable the port clocks
Ports A and C.
Set PA0 as output
Toggle in a loop.
Add the timer
Set prescaler and reload.
Blink PC13 from the interrupt
Compare the two blinks.
How do you build a bare-metal blink on STM32?
Program a Blue Pill without Arduino: clocks, GPIO registers and a timer interrupt. You need STM32 Blue Pill (STM32F103C8T6), ST-Link V2 programmer (clone), LED 5 mm red, Resistor 220 Ω and Breadboard 830 Tie Points. It takes 7 steps and about 180 minutes.
- Level
- Intermediate
- Time
- About 180 min
- Parts
- 5
Parts list
- Development BoardsBuy
- InterfacesBuy
- SemiconductorsBuyLED 5 mm red × 1
- PassivesBuyResistor 220 Ω × 1
- PrototypingBuy
Tick what you already own. It saves to My Garage, so “Build with what I have” knows.
Wiring
| From | To | Why |
|---|---|---|
| ST-Link GND | Blue Pill GND | Ground |
| ST-Link SWDIO | Blue Pill DIO | Debug data |
| ST-Link SWCLK | Blue Pill DCLK | Debug clock |
| ST-Link 3.3V | Blue Pill 3.3 pin | Power from the programmer (do not also plug in USB) |
| Blue Pill PA0 | 220 ohm resistor to red LED anode | External LED |
| Red LED cathode | Blue Pill GND | Return |
| BOOT0 and BOOT1 jumpers | Both on 0 | Run from flash |
Code
Arduino hides what a microcontroller really does: you switch on the clock for a port, tell each pin whether it is an output, and then write to a register that is wired to the pin. This build does all of that by hand. The on-board LED flashes from a timer interrupt, with no waiting in the program, while the external LED flashes from a busy loop, so you can see the difference between a timer and a delay. The Blue Pill needs a programmer: the ST-Link clone connects over SWD, and it is simplest to power the board from the programmer, not USB at the same time. Many Blue Pills carry a clone chip that reports a different ID; if the programmer refuses it, use the clone-chip setting in the upload options. Keep the BOOT0 jumper on 0. If the header file is not found in your toolchain, use framework = stm32cube, which has the same register names. This is a learning exercise; the numbers in the register manual are the reference.
// Bare-metal Blue Pill (STM32F103): register-level GPIO and a timer interrupt, with no Arduino library.
// Toolchain: PlatformIO with platform = ststm32, board = bluepill_f103c8, framework = cmsis, upload_protocol = stlink.
// The chip runs from its 8 MHz internal oscillator at reset, so the numbers below assume 8 MHz.
#include "stm32f1xx.h"
volatile uint32_t ticks = 0;
void TIM2_IRQHandler(void) {
if (TIM2->SR & TIM_SR_UIF) {
TIM2->SR &= ~TIM_SR_UIF; // clear the interrupt flag
GPIOC->ODR ^= (1u << 13); // toggle the on-board LED on PC13
ticks++;
}
}
int main(void) {
RCC->APB2ENR |= RCC_APB2ENR_IOPAEN | RCC_APB2ENR_IOPCEN; // clock for ports A and C
GPIOA->CRL = (GPIOA->CRL & ~(0xFu << 0)) | (0x2u << 0); // PA0 push-pull output, 2 MHz
GPIOC->CRH = (GPIOC->CRH & ~(0xFu << 20)) | (0x2u << 20); // PC13 push-pull output, 2 MHz
RCC->APB1ENR |= RCC_APB1ENR_TIM2EN;
TIM2->PSC = 7999; // 8 MHz / 8000 = 1 kHz
TIM2->ARR = 499; // 500 counts: an interrupt twice a second
TIM2->DIER |= TIM_DIER_UIE;
NVIC_EnableIRQ(TIM2_IRQn);
TIM2->CR1 |= TIM_CR1_CEN;
while (1) {
GPIOA->ODR ^= 1u; // blink PA0 from a busy loop
for (volatile uint32_t i = 0; i < 300000; i++) { }
}
}Test checklist
What you should see at each check. If a reading is far off, stop and use the troubleshooting list.
| Check | Expected |
|---|---|
| External LED | Blinks from the main loop |
| On-board LED | Blinks twice a second |
| Add a long delay to the loop | PC13 still keeps time |
| Reset button | Both restart |
If it does not work
Programmer cannot connect
Check the four wires and the BOOT jumpers.
Wrong chip ID
Choose the clone-chip option for upload.
No blink
Check the clock enable lines before the pin set-up.
Still stuck? Find the fault step by step with your multimeter →
Common questions
What parts do I need for the bare-metal blink on STM32?
STM32 Blue Pill (STM32F103C8T6), ST-Link V2 programmer (clone), LED 5 mm red, Resistor 220 Ω and Breadboard 830 Tie Points.
How long does the bare-metal blink on STM32 take to build?
About 180 minutes for a intermediate maker, in 7 steps.
How do I know the bare-metal blink on STM32 works?
External LED: Blinks from the main loop. On-board LED: Blinks twice a second. Add a long delay to the loop: PC13 still keeps time.
Programmer cannot connect: what should I check?
Check the four wires and the BOOT jumpers.
Wrong chip ID: what should I check?
Choose the clone-chip option for upload.
No blink: what should I check?
Check the clock enable lines before the pin set-up.
