Skip to content

Commit 45e6dda

Browse files
committed
🎉 add cpp project
1 parent 2065eef commit 45e6dda

27 files changed

+48629
-0
lines changed

cpp/.vscode/c_cpp_properties.json

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"configurations": [
3+
{
4+
"name": "STM32",
5+
"includePath": [
6+
"${workspaceFolder}/**"
7+
],
8+
"defines": [
9+
"STM32F103xB"
10+
],
11+
"compilerPath": "/usr/bin/arm-none-eabi-gcc",
12+
"cStandard": "gnu17",
13+
"cppStandard": "gnu++17",
14+
"intelliSenseMode": "gcc-arm"
15+
}
16+
],
17+
"version": 4
18+
}

cpp/.vscode/launch.json

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
// Use IntelliSense to learn about possible attributes.
3+
// Hover to view descriptions of existing attributes.
4+
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
5+
"version": "0.2.0",
6+
"configurations": [
7+
{
8+
"name": "Cortex Debug",
9+
"cwd": "${workspaceFolder}",
10+
"executable": "./build/dma-usart.elf",
11+
"request": "launch",
12+
"type": "cortex-debug",
13+
"runToEntryPoint": "main",
14+
"servertype": "openocd",
15+
"configFiles": [
16+
"interface/stlink.cfg",
17+
"target/stm32f1x.cfg"
18+
],
19+
"svdFile": "${workspaceFolder}/STM32F103.svd"
20+
}
21+
]
22+
}

cpp/Makefile

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
TARGET = blink
2+
3+
4+
# Configure micro-controller
5+
MCU_FAMILY = STM32F103xB
6+
LDSCRIPT = stm32f1.ld
7+
CPU = cortex-m3
8+
INSTR_SET = thumb
9+
FLOAT_ABI = soft
10+
11+
# compiler option
12+
OPT := -Os
13+
CSTD ?= c11
14+
CXXSTD := c++17
15+
16+
# Project specific configuration
17+
BUILD_DIR := build
18+
BUILD_TYPE ?= Debug
19+
SRC_DIR := src lib/gpio
20+
INC_DIRS = include lib/gpio
21+
22+
23+
PREFIX ?= arm-none-eabi
24+
CC := $(PREFIX)-gcc
25+
CXX := $(PREFIX)-g++
26+
LD := $(PREFIX)-gcc
27+
AR := $(PREFIX)-ar
28+
AS := $(PREFIX)-as
29+
SIZE := $(PREFIX)-size
30+
OBJCOPY := $(PREFIX)-objcopy
31+
OBJDUMP := $(PREFIX)-objdump
32+
GDB := $(PREFIX)-gdb
33+
34+
# collect source files and generate object files
35+
SRCS := $(shell find $(SRC_DIR) -name '*.cpp' -or -name '*.c')
36+
OBJS := $(addsuffix .o,$(basename $(SRCS))) # replace .c with .o
37+
OBJS := $(addprefix $(BUILD_DIR)/,$(OBJS)) # replace .c with .o
38+
ASMS = $(addsuffix .s,$(basename $(OBJS))) # replace .c with .o
39+
40+
41+
# Define stm32 family macro
42+
DEFS += -D$(MCU_FAMILY)
43+
44+
# header library include flsgs
45+
INC_FLAGS = $(addprefix -I,$(INC_DIRS))
46+
47+
48+
# Target-specific flags
49+
CPU_FLAGS ?= -mfloat-abi=$(FLOAT_ABI) -m$(INSTR_SET) -mcpu=$(CPU)
50+
51+
CPPFLAGS ?=$(DEFS) $(INC_FLAGS)
52+
CFLAGS ?=$(CPU_FLAGS) $(OPT)
53+
CXXFLAGS :=$(CFLAGS) -fno-exceptions -fno-rtti
54+
LDFLAGS ?=$(CPU_FLAGS)
55+
56+
# dependency
57+
CXXFLAGS += -M
58+
59+
# Do not link stdlib with executable
60+
CFLAGS += -nostdlib -fno-tree-loop-distribute-patterns -fdata-sections -ffunction-sections
61+
CXXFLAGS += -nostdlib -fno-tree-loop-distribute-patterns -fdata-sections -ffunction-sections
62+
LDFLAGS += -nostdlib -fno-tree-loop-distribute-patterns
63+
64+
65+
# Warning options for C and CXX compiler
66+
CFLAGS += -Wall -Wextra -Wundef -Wshadow -Wredundant-decls -Wmissing-prototypes -Wstrict-prototypes
67+
CXXFLAGS += -Wall -Wextra -Wundef -Wshadow -Wredundant-decls -Weffc++ -Werror
68+
69+
70+
LDFLAGS += -T $(LDSCRIPT)
71+
LDFLAGS += -Wl,-Map=$(basename $@).map
72+
73+
all: size bin
74+
size: $(BUILD_DIR)/$(TARGET).size
75+
elf: $(BUILD_DIR)/$(TARGET).elf
76+
bin: $(BUILD_DIR)/$(TARGET).bin
77+
hex: $(BUILD_DIR)/$(TARGET).hex
78+
srec: $(BUILD_DIR)/$(TARGET).srec
79+
list: $(BUILD_DIR)/$(TARGET).list
80+
81+
asm: $(ASMS)
82+
83+
84+
%.size: %.elf
85+
@$(SIZE) $<
86+
87+
%.bin: %.elf
88+
@echo "COPY " $< " => " $@
89+
@$(OBJCOPY) -Obinary $(*).elf $(*).bin
90+
91+
$(BUILD_DIR)/%.s:%.cpp
92+
@mkdir -p $(dir $@)
93+
@echo "AS" $< " ==> " $@
94+
$(CXX) $(CPPFLAGS) $(CXXLAGS) -o $@ -S $<
95+
96+
# $(BUILD_DIR)/%.o:%.c
97+
# @mkdir -p $(dir $@)
98+
# @echo "CC" $< " ==> " $@
99+
# $(CC) $(CPPFLAGS) $(CFLAGS) -o $@ -c $<
100+
101+
$(BUILD_DIR)/%.o:%.cpp
102+
@mkdir -p $(dir $@)
103+
@echo "CXX" $< " ==> " $@
104+
$(CXX) $(CPPFLAGS) $(CXXFLAGS) -o $@ -c $<
105+
106+
$(BUILD_DIR)/$(TARGET).elf: $(OBJS)
107+
@echo "Linking sources into "$@
108+
@$(CC) $(LDFLAGS) -o $@ $^
109+
110+
111+
flash: bin
112+
st-flash write $(BUILD_DIR)/$(TARGET).bin 0x8000000
113+
114+
debug: CFLAGS += -g -gdwarf-2
115+
debug: CXXFLAGS += -g -gdwarf-2
116+
debug: all
117+
118+
clean:
119+
rm -rf build

cpp/README.md

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
# Writing Startup script for Cortex M controllers in C++
2+
3+
A definitive guide to developing the entire embedded C++ project for ARM Cortexm-M micro-controllers. This projects ais to provide as deep insight into the development of startup script in c++.
4+
5+
## Project Working
6+
7+
This project configures SysTick timer and uses it to generate time accurate delay for blinking an LED. The onboard LED connected to pin C13 blinks every second.
8+
9+
## Dependencies
10+
11+
* **make**\
12+
Make utility is required for configuring and building this project. You can install make on linux by running command:
13+
14+
```bash
15+
sudo apt install build-essential
16+
```
17+
18+
* **gcc-arm-none-eabi toolchain**\
19+
ARM cross-platform toolchain is required to build applications for arm mcus. Toolchain can be installed by running following command:
20+
21+
```bash
22+
sudo apt install gcc-arm-none-eabi
23+
```
24+
25+
* **openocd**\
26+
It is an Open On Circuit Debugging tool used to flash and debug arm micro controllers. You can install openocd on linux by running command:
27+
28+
```bash
29+
sudo apt install openocd -y
30+
```
31+
32+
* **st-link**\
33+
This package is provided by STMicro-electronics for flashing the binary on the micro-controller
34+
35+
```bash
36+
sudo apt install stlink
37+
```
38+
39+
* **Cortex Debug extension**\
40+
This extension for VSCode is helpful for debugging the application on Blue Pill. The contents of registers as well as memory are visible in the context menu.
41+
Launch VS Code Quick Open (Ctrl+P), paste the following command, and press enter.
42+
43+
```bash
44+
ext install marus25.cortex-debug
45+
```
46+
47+
## Project Structure
48+
49+
* `src` directory contains all source files for the project
50+
* `include` directory contains all header files for the project
51+
52+
### Source file description
53+
54+
* `stm31f1.ld` - linker script for STM32F103 MCU
55+
* `src\main.cpp` - application code
56+
* `src\startup_stm32f103.cpp` - startup script in cpp
57+
* `include\gpio.hpp` - slibrary for handling gpio functions
58+
* `system_stm32f1xx.c` - clock configuration and system initialization functions
59+
* `STM32F103.svd` - contains the description of the system contained in Arm Cortex-M processor-based microcontrollers, in particular, the memory mapped registers of peripherals.
60+
61+
## Run Locally
62+
63+
Running the project is super easy. Just clone, build, and flash.
64+
65+
### Clone the project
66+
67+
1. Using https
68+
69+
```bash
70+
git clone https://github.com/csrohit/stm32-startup-cpp.git
71+
cd stm32-startup-cpp
72+
```
73+
74+
2. Using ssh
75+
76+
```bash
77+
git clone git@github.com:csrohit/stm32-startup-cpp.git
78+
cd stm32-startup-cpp
79+
```
80+
81+
## Configuration
82+
83+
All the configuration required for building this project is given below.
84+
85+
1. Build output directory
86+
In `Makefile`, output directory can be configured using variable `BUILD_DIR`.
87+
88+
2. Binary name
89+
In `Makefile`, the name of binary can be configured using variable `TARGET`.
90+
91+
3. MCU
92+
In `Makefile`, the target mcu can be selected by seting the following flags/variables
93+
* `MCU_FAMILY`: Specifies the family, it used to define a maco for inclusion of header files
94+
* `LD_SCRIPT`: Specifies path to linker script of the target controller
95+
* `INSTR_SET`: Specifies the instruction set to use. e.g. `thumb`, `arm`, etc
96+
* `FLOAT_ABI`: Specifies the floating point implementation
97+
* `CPU`: Specifies the processor on the MCU. e.g. `cortex-m3`, `cortex-m4`, etc
98+
99+
## Build
100+
101+
Run following command in terminal to generate flashable binaries for blue pill board. Build files will be written to **Build Output Directory** as configured.
102+
103+
```bash
104+
make all
105+
```
106+
107+
## Flash
108+
109+
1. Connect STlink to PC and blue pill board using swd headers.
110+
2. Put blue pill board in programming mode.
111+
3. Run following to flash board with binary.
112+
113+
```C
114+
make flash
115+
```
116+
117+
Output
118+
119+
Onboard led connected to Pin C13 can be observed to be blinking every second.
120+
121+
## Debug
122+
123+
1. Run the following make command to build the program using debugging flags
124+
125+
```bash
126+
make debug
127+
```
128+
129+
2. Flash the controller using following command
130+
131+
```bash
132+
make flash
133+
```
134+
135+
3. Click in Run and Debug option in VsCode sidebar. Then launch Cortex Debug target.
136+
137+
Happy debugging....

0 commit comments

Comments
 (0)