From 7aecefd053f032abd76e5456fe5caeda8104d9b8 Mon Sep 17 00:00:00 2001 From: Imbus <> Date: Wed, 3 Apr 2024 17:47:08 +0200 Subject: [PATCH] Initial --- README.md | 15 +++++++++++++++ blink.c | 22 ++++++++++++++++++++++ makefile | 35 +++++++++++++++++++++++++++++++++++ 3 files changed, 72 insertions(+) create mode 100644 README.md create mode 100644 blink.c create mode 100644 makefile diff --git a/README.md b/README.md new file mode 100644 index 0000000..ddf093b --- /dev/null +++ b/README.md @@ -0,0 +1,15 @@ +# STM32 Example + +This is unfinished work in progress. +The goal is to create a simple C example of a STM32F103C8T6 Blue Pill board running a simple program. + +`sudo dnf install arm-none-eabi-gcc-cs stlink` + +- [STM32F103C8T6 Blue Pill](https://stm32-base.org/boards/STM32F103C8T6-Blue-Pill.html) +- [STM32F103C8T6 Blue Pill Datasheet](https://www.st.com/resource/en/datasheet/stm32f103c8.pdf) +- [Bare Metal C on the Blue Pill](https://leap.tardate.com/arm/stm32f103c8t6/baremetal/) +- [Bare Metal C on the Blue Pill Source Code](https://github.com/tardate/LittleArduinoProjects/tree/master/ARM/STM32F103C8T6/BareMetal) +- [Bare Metal Embedded playlist](https://www.youtube.com/playlist?list=PLERTijJOmYrDiiWd10iRHY0VRHdJwUH4g) +- [Getting Started With STM32](https://stm32-base.org/guides/getting-started) +- [STM32-base source](https://github.com/STM32-base/STM32-base) and the specific [F1 Template](https://github.com/STM32-base/STM32-base-F1-template) +- [stm32_bare_metal repository](https://github.com/mensi/stm32_bare_metal) diff --git a/blink.c b/blink.c new file mode 100644 index 0000000..06ee69d --- /dev/null +++ b/blink.c @@ -0,0 +1,22 @@ +#include "stm32f103xb.h" + +#define LED_PIN GPIO_PIN_13 +#define LED_PORT GPIOC + +void delay(uint32_t count) { + for(uint32_t i = 0; i < count; i++); +} + +int main(void) { + RCC->APB2ENR |= RCC_APB2ENR_IOPCEN; // Enable clock for GPIOC + GPIOC->CRH &= ~(GPIO_CRH_CNF13 | GPIO_CRH_MODE13); // Clear CNF and MODE bits for pin 13 + GPIOC->CRH |= GPIO_CRH_MODE13_0; // Output mode, max speed 10 MHz + + while (1) { + GPIOC->BSRR = LED_PIN; // Set pin high (turn LED on) + delay(1000000); // Delay + GPIOC->BSRR = LED_PIN << 16; // Reset pin (turn LED off) + delay(1000000); // Delay + } +} + diff --git a/makefile b/makefile new file mode 100644 index 0000000..fbfe41a --- /dev/null +++ b/makefile @@ -0,0 +1,35 @@ +# Toolchain +CC = arm-none-eabi-gcc +OBJCOPY = arm-none-eabi-objcopy +STFLASH = st-flash + +# Compiler flags +CFLAGS = -std=c99 -Wall -Wextra -Werror -g -mthumb -mcpu=cortex-m3 + +# Object files +OBJS = blink.o + +# Output files +ELF = blink.elf +BIN = blink.bin + +# Targets +.PHONY: all clean flash + +all: $(BIN) + +$(BIN): $(ELF) + $(OBJCOPY) -O binary $< $@ + +$(ELF): $(OBJS) + $(CC) $(CFLAGS) $^ -o $@ + +%.o: %.c + $(CC) $(CFLAGS) -c $< -o $@ + +clean: + rm -f $(OBJS) $(ELF) $(BIN) + +flash: $(BIN) + $(STFLASH) --reset write $< 0x8000000 +