Skip to content

Commit 6b0e4c4

Browse files
committed
✨ add clock-configuration porject
1 parent adf7c04 commit 6b0e4c4

32 files changed

+49941
-0
lines changed

clock-configuration/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
build
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"configurations": [
3+
{
4+
"name": "Mac",
5+
"includePath": [
6+
"${workspaceFolder}/**",
7+
"${workspaceFolder}/include",
8+
"/Applications/ARM/arm-none-eabi/include"
9+
],
10+
"defines": [
11+
"STM32F103xB"
12+
],
13+
"macFrameworkPath": [],
14+
"compilerPath": "/Applications/ARM/bin/arm-none-eabi-gcc",
15+
"cStandard": "gnu17",
16+
"cppStandard": "gnu++14",
17+
"intelliSenseMode": "macos-gcc-arm"
18+
}
19+
],
20+
"version": 4
21+
}
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/blink.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+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"files.associations": {
3+
"stdint.h": "c",
4+
"lcd.h": "c",
5+
"string.h": "c",
6+
"stdio.h": "c",
7+
"features.h": "c"
8+
}
9+
}

clock-configuration/LICENSE

Lines changed: 674 additions & 0 deletions
Large diffs are not rendered by default.

clock-configuration/Makefile

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
TARGET := blink
2+
# debug build?Release
3+
BUILD_TYPE = Debug
4+
BUILD_DIR:= build
5+
6+
TRIPLE = arm-none-eabi
7+
CC = ${TRIPLE}-gcc
8+
LD = ${TRIPLE}-ld
9+
AS = ${TRIPLE}-as
10+
GDB = ${TRIPLE}-gdb
11+
OBJCOPY = ${TRIPLE}-objcopy
12+
13+
INCFLAGS := -Iinclude
14+
CFLAGS := -mcpu=cortex-m3 -mfloat-abi=soft -mthumb --specs=nano.specs $(INCFLAGS) -std=gnu11 -Os -Wall -fstack-usage -fdata-sections -ffunction-sections -DSTM32F103xB
15+
ASFLAGS := -mcpu=cortex-m3 -mfloat-abi=soft -mthumb --specs=nano.specs $(INCFLAGS) -x assembler-with-cpp
16+
LDFLAGS := -mcpu=cortex-m3 -mfloat-abi=soft -mthumb --specs=nosys.specs $(INCFLAGS)
17+
18+
# add debug flags if build type is debug
19+
ifeq ($(BUILD_TYPE), Debug)
20+
CFLAGS += -g -gdwarf-2
21+
endif
22+
23+
# Generate dependency information
24+
CFLAGS += -MMD -MP
25+
ASLAGS += -MMD -MP
26+
27+
SRC_DIR := src
28+
SRCS := $(shell find $(SRC_DIR) -name '*.c')
29+
OBJS := $(BUILD_DIR)/$(SRC_DIR)/startup_stm32f103c8tx.o $(SRCS:%.c=$(BUILD_DIR)/%.o)
30+
31+
$(BUILD_DIR)/$(TARGET).elf: $(OBJS) STM32F103C8TX_FLASH.ld
32+
$(CC) $(LDFLAGS) -o $@ $(OBJS) -T"STM32F103C8TX_FLASH.ld" -Wl,-Map="$(BUILD_DIR)/$(TARGET).map" -Wl,--gc-sections -static -Wl,--start-group -lc -lm -Wl,--end-group
33+
34+
$(BUILD_DIR)/%.o: %.c
35+
@mkdir -p $(dir $@)
36+
@$(CC) $(CFLAGS) -c $< -o $@
37+
@echo "CC " $< " ==> " $@
38+
39+
$(BUILD_DIR)/%.o: %.s
40+
@mkdir -p $(dir $@)
41+
@$(CC) $(ASFLAGS) -c $< -o $@
42+
@echo "AS " $< " ==> " $@
43+
44+
flash:
45+
openocd -d2 -f interface/stlink.cfg -c "transport select hla_swd" -f target/stm32f1x.cfg -c "program {$(BUILD_DIR)/$(TARGET).elf} verify reset; shutdown;"
46+
47+
all: $(BUILD_DIR)/$(TARGET).elf
48+
49+
clean:
50+
rm -rf $(BUILD_DIR)

clock-configuration/README.md

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
# Blue Pill - SysTick programming
2+
3+
![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/)
4+
5+
Configure the systick timer to generate periodic interrupts. Use this timer for generating accurate delay function.
6+
7+
## SysTick Timer
8+
9+
The processor has a 24 bit system timer, SysTick, that counts down from the reload value to zero, reloads and counts down on the subsequent clocks.\
10+
System Tick Time (SysTick) generates interrupt requests on regular basis. This allows an os to perform context switching to support multitasking. For applications that do not require an OS, the SysTick can be used for time keeping, time measurement or as an interrupt source for tasks that need to be executed regularly.\
11+
SysTick register can only be accessed using word access.
12+
13+
### Control flow
14+
![Control flow diagram for SysTick timer](https://github.com/csrohit/bluepill-systick/blob/main/docs/filled.png "SysTick timer - control flow diagram")
15+
16+
1. Program the reload value:\
17+
The reload value can be loaded by setting `LOAD` register. This value is set to 1 less that the number of clock cycles needed for the interrupt as the timer counts both reload value as well as zero. e.g. If the SysTick interrupt is required every 100 clock pulses, set RELOAD to 99.
18+
2. Clear current value:\
19+
This register can be accessed using `VAL` variable. Bits *24:31* are reserved and 24 bit value can be read from bits *23:0*. Writing any value this register sets it to zero along with setting `COUNT_FLAG` to zero.
20+
3. Configure SysTick and start:
21+
1. Select clock source-\
22+
Clock source can be set using `CLKSOURCE` bit (2) of `CTRL` register.\
23+
0 - AHB/8\
24+
1 - Processor Clock (AHB)
25+
2. Enable Tick interrupt-\
26+
To enable Tick interrupt set `TICKINT` bit (2) of `CTRL` register.
27+
3. Start SysTick timer-\
28+
`ENABLE` bit (0) of `CTRL` register enables the counter. When `ENABLE` is set to 1, the counter loads the `RELOAD` value from the `LOAD` register and then counts down. On reaching 0, it sets the `COUNTFLAG` to 1 and optionally asserts the `SysTick` depending on the value of `TICKINT`. It then loads the `RELOAD` value again, and begins counting.
29+
30+
## Project Working
31+
32+
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.
33+
34+
## Dependencies
35+
36+
* **make**\
37+
Make utility is required for configuring and building this project. You can install make on linux by running command:
38+
39+
```bash
40+
sudo apt install build-essential
41+
```
42+
43+
* **gcc-arm-none-eabi toolchain**\
44+
ARM cross-platform toolchain is required to build applications for arm mcus. Toolchain can be installed by running following command:
45+
46+
```bash
47+
sudo apt install gcc-arm-none-eabi
48+
```
49+
50+
* **openocd**\
51+
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:
52+
53+
```bash
54+
sudo apt install openocd -y
55+
```
56+
57+
* **Cortex Debug extension**\
58+
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.
59+
Launch VS Code Quick Open (Ctrl+P), paste the following command, and press enter.
60+
61+
```bash
62+
ext install marus25.cortex-debug
63+
```
64+
65+
## Project Structure
66+
67+
* `src` directory contains all source files for the project
68+
* `include` directory contains all header files for the project
69+
70+
### Source file description
71+
72+
* `STM32F103C8TX_FLASH.ld` - linker script
73+
* `src\main.c` - application code
74+
* `src\startup_stm32f103c8tx.s` - assembly startup script for blue pill board
75+
* `system_stm32f1xx.c` - clock configuration and system initialization functions
76+
* `STM32F103.svd` - contains the description of the system contained in Arm Cortex-M processor-based microcontrollers, in particular, the memory mapped registers of peripherals.
77+
78+
## Run Locally
79+
80+
Running the project is super easy. Just clone, build, and flash.
81+
82+
### Clone the project
83+
84+
1. Using https
85+
86+
```bash
87+
git clone https://github.com/csrohit/bluepill-systick.git
88+
cd bluepill-systick
89+
```
90+
91+
2. Using ssh
92+
93+
```bash
94+
git clone git@github.com:csrohit/bluepill-systick.git
95+
cd bluepill-systick
96+
```
97+
98+
## Configuration
99+
100+
All the configuration required for building this project is given below.
101+
102+
1. Build output directory
103+
In `Makefile`, output directory can be configured using variable `BUILD_DIR`.
104+
105+
2. Build type
106+
In `Makefile`, build type can be configured using variable`DEBUG`. Possible values are `Debug` and `Release`.
107+
108+
3. Binary name
109+
In `CMakeLists.txt`, output binary name can be configured using `project(<binary-name>)` macro.
110+
** update above info in `.vscode/launch.json` as well for debugging to work.
111+
112+
## Build
113+
114+
Run following command in terminal to generate flashable binaries for blue pill board. Build files will be written to **Build Output Directory** as configured.
115+
116+
```bash
117+
make all
118+
```
119+
120+
## Flash
121+
122+
1. Connect STlink to PC and blue pill board using swd headers.
123+
2. Put blue pill board in programming mode.
124+
3. Run following to flash board with binary.
125+
126+
```bash
127+
make flash
128+
```
129+
130+
## Output
131+
132+
Onboard led connected to Pin C13 can be observed to be blinking every second.
133+
134+
## Debug
135+
136+
Click in `Run and Debug` option in VsCode sidebar. Then launch `Cortex Debug` target.
137+
138+
Happy debugging....

0 commit comments

Comments
 (0)