Configure the systick timer to generate periodic interrupts. Use this timer for generating accurate delay function.
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.
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.
SysTick register can only be accessed using word access.
- Program the reload value:
The reload value can be loaded by settingLOAD
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. - Clear current value:
This register can be accessed usingVAL
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 settingCOUNT_FLAG
to zero. - Configure SysTick and start:
- Select clock source-
Clock source can be set usingCLKSOURCE
bit (2) ofCTRL
register.
0 - AHB/8
1 - Processor Clock (AHB) - Enable Tick interrupt-
To enable Tick interrupt setTICKINT
bit (2) ofCTRL
register. - Start SysTick timer-
ENABLE
bit (0) ofCTRL
register enables the counter. WhenENABLE
is set to 1, the counter loads theRELOAD
value from theLOAD
register and then counts down. On reaching 0, it sets theCOUNTFLAG
to 1 and optionally asserts theSysTick
depending on the value ofTICKINT
. It then loads theRELOAD
value again, and begins counting.
- Select clock source-
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.
-
make
Make utility is required for configuring and building this project. You can install make on linux by running command:sudo apt install build-essential
-
gcc-arm-none-eabi toolchain
ARM cross-platform toolchain is required to build applications for arm mcus. Toolchain can be installed by running following command:sudo apt install gcc-arm-none-eabi
-
openocd
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:sudo apt install openocd -y
-
Cortex Debug extension
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.ext install marus25.cortex-debug
src
directory contains all source files for the projectinclude
directory contains all header files for the project
STM32F103C8TX_FLASH.ld
- linker scriptsrc\main.c
- application codesrc\startup_stm32f103c8tx.s
- assembly startup script for blue pill boardsystem_stm32f1xx.c
- clock configuration and system initialization functionsSTM32F103.svd
- contains the description of the system contained in Arm Cortex-M processor-based microcontrollers, in particular, the memory mapped registers of peripherals.
Running the project is super easy. Just clone, build, and flash.
-
Using https
git clone https://github.com/csrohit/bluepill-baremetal-projects.git cd bluepill-baremetal-projects/systick
-
Using ssh
git clone git@github.com:csrohit/bluepill-baremetal-projects.git cd bluepill-baremetal-projects/systick
All the configuration required for building this project is given below.
-
Build output directory In
Makefile
, output directory can be configured using variableBUILD_DIR
. -
Build type In
Makefile
, build type can be configured using variableDEBUG
. Possible values areDebug
andRelease
. -
Binary name In
CMakeLists.txt
, output binary name can be configured usingproject(<binary-name>)
macro. ** update above info in.vscode/launch.json
as well for debugging to work.
Run following command in terminal to generate flashable binaries for blue pill board. Build files will be written to Build Output Directory as configured.
make all
- Connect STlink to PC and blue pill board using swd headers.
- Put blue pill board in programming mode.
- Run following to flash board with binary.
make flash
Onboard led connected to Pin C13 can be observed to be blinking every second.
Click in Run and Debug
option in VsCode sidebar. Then launch Cortex Debug
target.
Happy debugging....