Skip to content

Commit 8043bca

Browse files
committed
📝 update documentation
1 parent c02fc96 commit 8043bca

File tree

5 files changed

+144
-78
lines changed

5 files changed

+144
-78
lines changed

dma/.gitignore

+9
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,13 @@
1+
# build files
12
build
3+
4+
# vscode files
5+
settings.json
6+
.cortex-debug*
7+
8+
# draw.io files
29
*.bkp
310
*.dtmp
11+
12+
# mac files
413
.DS_Store

dma/.vscode/settings.json

+8-1
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,12 @@
22
"files.associations": {
33
"time.h": "c",
44
"timer.h": "c"
5-
}
5+
},
6+
"makefile.launchConfigurations": [
7+
{
8+
"cwd": "/Users/csrohit/code/blue-pill/uart/dma/build",
9+
"binaryPath": "/Users/csrohit/code/blue-pill/uart/dma/build/dma-usart.elf",
10+
"binaryArgs": []
11+
}
12+
]
613
}

dma/Makefile

+56-33
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,14 @@ FLOAT_ABI = soft
1010

1111
# compiler option
1212
OPT := -Os
13-
CSTD ?= c11
14-
CXXSTD := c++17
13+
CSTD ?= gnu11
14+
CXXSTD := gnu++17
1515

1616
# Project specific configuration
1717
BUILD_DIR := build
1818
BUILD_TYPE ?= Debug
1919
SRC_DIR := src
20-
INC_DIRS = include
20+
INC_DIRS := include
2121

2222

2323
PREFIX ?= arm-none-eabi
@@ -35,80 +35,103 @@ GDB := $(PREFIX)-gdb
3535
SRCS := $(shell find $(SRC_DIR) -name '*.cpp' -or -name '*.c')
3636
OBJS := $(addsuffix .o,$(basename $(SRCS))) # replace .c with .o
3737
OBJS := $(addprefix $(BUILD_DIR)/,$(OBJS)) # replace .c with .o
38-
38+
ASMS := $(addsuffix .s,$(basename $(OBJS)))
3939

4040
# Define stm32 family macro
41-
DEFS += -D$(MCU_FAMILY)
41+
DEFS +=-D$(MCU_FAMILY)
4242

4343
# header library include flsgs
44-
INC_FLAGS = $(addprefix -I,$(INC_DIRS))
44+
INC_FLAGS =$(addprefix -I,$(INC_DIRS))
4545

4646

4747
# Target-specific flags
48-
CPU_FLAGS ?= -mfloat-abi=$(FLOAT_ABI) -m$(INSTR_SET) -mcpu=$(CPU)
49-
50-
CPPFLAGS ?=$(DEFS) $(INC_FLAGS)
51-
CFLAGS ?=$(CPU_FLAGS)
52-
CXXFLAGS :=$(CFLAGS) -fno-exceptions -fno-rtti
53-
LDFLAGS ?=$(CPU_FLAGS)
48+
CPU_FLAGS ?=-mfloat-abi=$(FLOAT_ABI) -m$(INSTR_SET) -mcpu=$(CPU)
49+
CPPFLAGS ?=$(DEFS) $(INC_FLAGS) -MMD
5450

51+
# optiomization level according to build type
5552
ifeq ($(BUILD_TYPE), Debug)
5653
CFLAGS += -g -gdwarf-2
54+
CXXFLAGS += -g -gdwarf-2
55+
LDFLAGS += -g -gdwarf-2
5756
else
5857
CFLAGS += $(OPT)
58+
CXXFLAGS += $(OPT)
59+
ASFLAGS += $(OPT)
5960
endif
6061

6162
# Do not link stdlib with executable
6263
CFLAGS += -nostdlib -fno-tree-loop-distribute-patterns -fdata-sections -ffunction-sections
63-
CXXFLAGS += -nostdlib -fno-tree-loop-distribute-patterns -fdata-sections -ffunction-sections
64+
CXXFLAGS += -nostdlib -fno-tree-loop-distribute-patterns -fdata-sections -ffunction-sections -fno-exceptions -fno-rtti
6465
LDFLAGS += -nostdlib
6566

6667

6768
# Warning options for C and CXX compiler
6869
CFLAGS += -Wall -Wextra -Wundef -Wshadow -Wredundant-decls -Wmissing-prototypes -Wstrict-prototypes
6970
CXXFLAGS += -Wall -Wextra -Wundef -Wshadow -Wredundant-decls -Weffc++ -Werror
7071

71-
72+
# Linker options
7273
LDFLAGS += -T $(LDSCRIPT)
7374
LDFLAGS += -Wl,-Map=$(basename $@).map,--gc-sections,-cref,--print-memory-usage
7475

75-
all: size bin
76-
size: $(BUILD_DIR)/$(TARGET).size
77-
elf: $(BUILD_DIR)/$(TARGET).elf
78-
bin: $(BUILD_DIR)/$(TARGET).bin
79-
hex: $(BUILD_DIR)/$(TARGET).hex
80-
srec: $(BUILD_DIR)/$(TARGET).srec
81-
list: $(BUILD_DIR)/$(TARGET).list
76+
all : size bin
77+
size : $(BUILD_DIR)/$(TARGET).size
78+
elf : $(BUILD_DIR)/$(TARGET).elf
79+
bin : $(BUILD_DIR)/$(TARGET).bin
80+
hex : $(BUILD_DIR)/$(TARGET).hex
81+
srec : $(BUILD_DIR)/$(TARGET).srec
82+
list : $(BUILD_DIR)/$(TARGET).list
83+
asm : $(ASMS)
84+
85+
# binary file to flash on target
86+
%.bin: %.elf
87+
@echo "COPY " $< " => " $@
88+
@$(OBJCOPY) -Obinary $(*).elf $(*).bin
89+
90+
# executable object files for debugging
91+
%.hex: %.elf
92+
$(OBJCOPY) -Oihex $(*).elf $(*).hex
93+
94+
%.srec: %.elf
95+
$(OBJCOPY) -Osrec $(*).elf $(*).srec
8296

97+
%.list: %.elf
98+
$(OBJDUMP) -S $(*).elf > $(*).list
8399

84100
%.size: %.elf
101+
@echo "Section wise usage: "
85102
@$(SIZE) $<
86103

87-
%.bin: %.elf
88-
@echo "COPY " $< " => " $@
89-
@$(OBJCOPY) -Obinary $(*).elf $(*).bin
104+
# Build assembly files
105+
$(BUILD_DIR)/%.s:%.c
106+
@mkdir -p $(dir $@)
107+
@echo "AS" $< " ==> " $@
108+
@$(CC) $(CPU_FLAGS) $(CPPFLAGS) $(CFLAGS) -o $@ -S $<
90109

110+
$(BUILD_DIR)/%.s:%.cpp
111+
@mkdir -p $(dir $@)
112+
@echo "AS" $< " ==> " $@
113+
@$(CXX) $(CPU_FLAGS) $(CPPFLAGS) $(CXXFLAGS) -o $@ -S $<
114+
115+
# Object files
91116
$(BUILD_DIR)/%.o:%.c
92117
@mkdir -p $(dir $@)
93118
@echo "CC" $< " ==> " $@
94-
$(CC) $(CPPFLAGS) $(CFLAGS) -o $@ -c $<
119+
@$(CC) $(CPU_FLAGS) $(CPPFLAGS) $(CFLAGS) -o $@ -c $<
95120

96121
$(BUILD_DIR)/%.o:%.cpp
97122
@mkdir -p $(dir $@)
98123
@echo "CXX" $< " ==> " $@
99-
$(CXX) $(CPPFLAGS) $(CXXFLAGS) -o $@ -c $<
100-
124+
@$(CXX) $(CPU_FLAGS) $(CPPFLAGS) $(CXXFLAGS) -o $@ -c $<
125+
126+
# final executable object file
101127
$(BUILD_DIR)/$(TARGET).elf: $(OBJS)
102128
@echo "Linking sources into "$@
103-
@$(CC) $(LDFLAGS) -o $@ $^
104-
129+
@$(CC) $(CPU_FLAGS) $(LDFLAGS) -o $@ $^
105130

131+
# upload binary to stm32
106132
flash: bin
107133
st-flash write $(BUILD_DIR)/$(TARGET).bin 0x8000000
108-
109-
debug: CFLAGS += -g -gdwarf-2
110-
debug: CXXFLAGS += -g -gdwarf-2
111-
debug: all
112134

135+
# remove generated files
113136
clean:
114137
rm -rf build

dma/README.md

+63-34
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,66 @@
1-
# USART communication using DMA
1+
# USART communication using DMA in Circular mode
22

3-
A boilerplate for generating projects for stm32 blue pill board using makefile. The projects does not use any tools provided by ST Microelectronics.
4-
Everything from development, flashing to debugging can be done in Visual Studio Code only.
3+
Communication between PC and STM32 using USART and DMA peripherals.
54

6-
![Build Passing](https://img.shields.io/badge/build-passing-brightgreen) [![GPLv3 License](https://img.shields.io/badge/License-GPL%20v3-yellow.svg)](https://opensource.org/licenses/)
5+
Transmit data using USART1 and DMA in continuous mode without interruption of CPU. This project does not require any IDE like CubeIde, any text editor will work including notepad, vim. For better debugging experience, VSCode is preferred.
76

7+
![Build Passing](https://img.shields.io/badge/build-passing-brightgreen) [![GPLv3 License](https://img.shields.io/badge/License-GPL%20v3-yellow.svg)](https://opensource.org/licenses/)
88

99
## Dependencies
1010

1111
* make
1212
Make utility is required for configuring and building this project. You can install make on linux by running command:
1313

1414
```bash
15+
# for debian based linux distros
1516
sudo apt install build-essential
17+
18+
# for macos
19+
xcode-select --install
20+
21+
# for macos using brew formulae
22+
brew install make
1623
```
1724

1825
* gcc-arm-none-eabi toolchain
1926
ARM cross-platform toolchain is required to build applications for arm mcus. Toolchain can be installed by running following command:
2027

2128
```bash
29+
# for debian based linux distros
2230
sudo apt install gcc-arm-none-eabi
31+
32+
# for macos
33+
brew install --cask gcc-arm-embedded
2334
```
2435

2536
* openocd
2637
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:
2738

2839
```bash
40+
# for debian based linux distros
2941
sudo apt install openocd -y
42+
43+
# for macos
44+
brew install openocd
3045
```
3146

47+
* stlink-tools
48+
This program is required for uploading binaries to the STM32 boards. You can install stlink tools by running the command:
49+
50+
```bash
51+
# for debian based linux distros
52+
sudo apt install stlink-tools
53+
54+
# for macos
55+
brew install stlink
56+
```
57+
3258
* Cortex Debug extension
33-
x`
59+
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. Launch VS Code Quick Open (Ctrl+P), paste the following command, and press enter.
3460

61+
```bash
62+
ext install marus25.cortex-debug
63+
```
3564

3665
## Project Structure
3766

@@ -40,68 +69,68 @@ x`
4069

4170
### Source file description
4271

43-
* `STM32F103C8TX_FLASH.ld` - linker script
44-
* `src\main.c` - application code
45-
* `src\startup_stm32f103c8tx.s` - assembly startup script for blue pill board
46-
* `system_stm32f1xx.c` - clock configuration and system initialization functions
72+
* `stm32f1.ld` - linker script for stm32f103
73+
* `src/main.c` - main application code
74+
* `src/startup_stm32f1.c` - boot sequence for arm cortex-m3 processors
75+
* `src/system_stm32f1xx.c` - clock configuration and system initialization functions
4776

4877
## Run Locally
4978

50-
Running the project is super easy. Just clone, build, and flash.
51-
52-
### Clone the project
79+
Running the project is super easy. Just clone, build, and flash. Clone this project using **Code** button above.
5380

54-
1. Using https
55-
56-
```bash
57-
git clone https://github.com/csrohit/bluepill-template-project.git
58-
cd bluepill-template-project
59-
```
60-
61-
2. Using ssh
62-
63-
```bash
64-
git clone git@github.com:csrohit/bluepill-template-project.git
65-
cd bluepill-template-project
66-
```
67-
## Configuration
81+
### Configuration
6882

6983
All the configuration required for building this project is given below.
7084

7185
1. Build output directory
7286
In `Makefile`, output directory can be configured using variable `BUILD_DIR`.
7387

7488
2. Build type
75-
In `Makefile`, build type can be configured using variable`DEBUG`. Possible values are `Debug` and `Release`.
89+
In `Makefile`, build type can be configured using variable`BUILD_TYPE`. Possible values are `Debug` and `Release`.
7690

7791
3. Binary name
78-
In `CMakeLists.txt`, output binary name can be configured using `project(<binary-name>)` macro.
79-
** update above info in `.vscode/launch.json` as well for debugging to work.
92+
In `Makefile`, output binary name can be configured using `TARGET` variable.
93+
** update the target name in the `executable` property `.vscode/launch.json` for the debugger to work.
8094

81-
## Build
95+
### Build
8296

8397
Run following command in terminal to generate flashable binaries for blue pill board. Build files will be written to **Build Output Directory** as configured.
8498

8599
```bash
86-
make all
100+
make
87101
```
88102

89103
## Flash
90104

91105
1. Connect Stlink to PC and blue pill board using swd headers.
92-
2. Put blue pill board in programming mode.
106+
2. Put blue pill board in programming mode *(optional)*.
107+
The *Boot0* jumper is set to *0*, set it to *1* and reset the device.
93108
3. Run following to flash board with binary.
94109

95110
```bash
96111
make flash
97112
```
98113

114+
4. Done.
115+
116+
## Hardware Setup
117+
118+
Connect the board with host through USB to TTL converter (FTDI board in our case). The connections are described as follows.
119+
120+
| Pin on Blue Pill | Pin on FTDI |
121+
|------------------ |------------- |
122+
| PA9 | Rx |
123+
| PA10 | Tx |
124+
| Gnd | Gnd |
125+
126+
![Connection diagram for USART1](../docs/label.png "Pin connection diagram for usart1")
127+
99128
## Output
100129

101-
Onboard led connected to Pin C13 can be observed to be blinking after 500ms.
130+
"Hello world" messages are visible on the terminal arriving continuously dues to circular mode of operation as seen below.
131+
![Serial prompt at 115200 baudrate](docs/out_115200_circ.png "Output on terminal")
102132

103133
## Debug
104134

105135
Click in `Run and Debug` option in VsCode sidebar. Then launch `Cortex Debug` target.
106136

107-
Happy debugging....

dma/src/startup_stm32f1.c

+8-10
Original file line numberDiff line numberDiff line change
@@ -177,21 +177,19 @@ void Default_Handler(void)
177177
void Reset_Handler(void)
178178
{
179179
// copy .data section to SRAM
180-
uint8_t *pSramData = (uint8_t *)&_sdata; // sram
181-
uint8_t *pFlashData = (uint8_t *)&_la_data; // flash
182-
uint32_t data_size = (uint32_t)&_edata - (uint32_t)&_sdata;
183-
for (uint32_t i = 0; i < data_size; i++)
180+
uint32_t *start_sram = (uint32_t *)&_sdata;
181+
uint32_t *start_flash = (uint32_t *)&_la_data;
182+
while (start_sram < (uint32_t *)&_edata)
184183
{
185-
*pSramData++ = *pFlashData++;
184+
*start_sram++ = *start_flash++;
186185
}
187186

188-
// init. the .bss section to zero in SRAM
189-
uint32_t bss_size = (uint32_t)&_ebss - (uint32_t)&_sbss;
190-
uint8_t *pBssData = (uint8_t *)&_sbss;
191-
for (uint32_t i = 0; i < bss_size; i++)
187+
uint32_t *start_bss = (uint32_t *)&_sbss;
188+
while (start_bss < (uint32_t *)&_ebss)
192189
{
193-
*pBssData++ = 0;
190+
*start_bss++ = 0;
194191
}
192+
195193
SystemCoreClockUpdate();
196194
// now invoke main
197195
main();

0 commit comments

Comments
 (0)