|
| 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