|
| 1 | +# Blue Pill - SysTick programming |
| 2 | + |
| 3 | + [](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 | + |
| 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