|
1 |
| -# STM32-Bare-Metal |
2 |
| - |
3 |
| -STM32F103C8 bare metal template |
4 |
| - |
5 |
| -I'll add a detailed description ASAP. |
6 |
| -For now just read the comments in the code. |
| 1 | +# STM32F103C8 bare metal C template |
| 2 | + |
| 3 | +_If you're new to the STM32 world, I strongly suggest you read **all** this README file **before** you start coding. Also there are comments in almost every source file. It's a good idea to read them._ |
| 4 | + |
| 5 | + |
| 6 | + |
| 7 | +## Quick start guide |
| 8 | + |
| 9 | +To start programming using this template you need to follow these steps. |
| 10 | + |
| 11 | +#### 1. Get the files |
| 12 | + |
| 13 | +Clone this repo with `git clone https://github.com/al95/STM32-Bare-Metal.git` |
| 14 | +or download a zip file from [**here**](https://github.com/al95/STM32-Bare-Metal/archive/master.zip). |
| 15 | + |
| 16 | +#### 2. Configure the Makefile (Part 1) |
| 17 | + |
| 18 | +At the beginning of the file you'll find two variables that you may want to change. |
| 19 | + |
| 20 | ++ **TARGET**: This is the name of your program and will be used to name the "output" files. |
| 21 | +e.g., `TARGET = blinky` will generate `blinky.elf`, `blinky.bin` and `blinky.hex`. |
| 22 | + |
| 23 | ++ **OPTFLAGS**: This sets the gcc optimization level. |
| 24 | +I suggest using `-O2` to optimize for speed or `-Os` to optimize for size, but be aware that you might get some unexpected results if you're not careful with your code. |
| 25 | +If you're not sure just leave it at `-O0`. |
| 26 | + |
| 27 | +#### 3. Write your code |
| 28 | ++ Write your main function inside `main.c` |
| 29 | ++ Add as many other source files as you want |
| 30 | ++ If you're using interrupts, add your ISRs to the vector table in `init.c` |
| 31 | + |
| 32 | +#### 4. Configure the Makefile (Part 2) |
| 33 | +Add to `OTHER_SRCS` all the source files that you want to compile. |
| 34 | +e.g., `OTHER_SRCS = main.c usarts.c timers.c` |
| 35 | + |
| 36 | +#### 5. Compile |
| 37 | +Just run `make`. |
| 38 | +You should get (at least) three files: an ELF, a BIN and an HEX. |
| 39 | +The **.elf** file is the "final product" of the compilation. |
| 40 | +If you don't know what an ELF file is, it probably means you don't need it. |
| 41 | +The **.bin** and **.hex** files contain the actual data that will be written to the STM32 flash. |
| 42 | +Which one you need depends on the programming software you want to use. |
| 43 | + |
| 44 | +#### 6. Flash |
| 45 | +Flash your program to the STM32. |
| 46 | +If you have [**texane's stlink**](https://github.com/texane/stlink) installed you can just run `make flash`. |
| 47 | +Otherwise, use whatever programming software you prefer. |
| 48 | + |
| 49 | +#### 7. Have fun |
| 50 | +That's it! |
| 51 | + |
| 52 | + |
| 53 | + |
| 54 | +## Debugging guide |
| 55 | + |
| 56 | +To debug your program follow the first 4 steps of the *Quick start guide* then run `make debug`. |
| 57 | +This will disable any optimization (`-O0`), add detailed debug information for gdb (`-ggdb3`) and generate a few additional files. These are: |
| 58 | + |
| 59 | ++ __*.o__: intermediate object files |
| 60 | ++ __*.size__: size of the _.text_, _.data_ and _.bss_ sections |
| 61 | ++ __*.su__: stack usage statistics (`gcc -fstack-usage`) |
| 62 | ++ __*.map__: link map (`ld -Map`) |
| 63 | ++ __*.lst__: disassembled program with sections, symbols and source files (`objdump -x -S`) |
| 64 | ++ __*.dis__: complete disassembly of all sections (`objdump -D`) |
| 65 | + |
| 66 | +The Makefile included in this template can start a debugging session using [**st-util**](https://github.com/texane/stlink) and gdb: |
| 67 | +1. Open a terminal and `cd` into the source folder. |
| 68 | +2. Run `make server` to open the st-util server. |
| 69 | +3. Open another terminal and `cd` into the source folder. |
| 70 | +4. Run `make gdb` to run gdb. |
| 71 | +5. Inside gdb run `load` to load your program into flash. |
| 72 | +6. Good luck! |
| 73 | + |
| 74 | +You're on your own from here. If you don't know how to use gdb, Google is your friend. It's easier than it looks. |
| 75 | + |
| 76 | + |
| 77 | + |
| 78 | +## Frequent problems |
| 79 | + |
| 80 | +1. *When i run* `make` *I get* `/bin/sh: arm-none-eabi-gcc: command not found` |
| 81 | +Check that the ARM version of gcc is correctly installed. |
| 82 | +If your gcc is installed using a different name prefix or in a user-defined folder you must change the `CROSS_COMPILE` variable inside the Makefile. |
| 83 | +e.g., `CROSS_COMPILE = another-prefix-` will run `another-prefix-gcc` |
| 84 | +e.g., `CROSS_COMPILE = /home/john/arm/arm-none-eabi-` will run `/home/john/arm/arm-none-eabi-gcc` |
| 85 | + |
| 86 | + |
| 87 | + |
| 88 | +2. *When I run* `make server` *I get* `bind: Address already in use` |
| 89 | +By default st-util communicates with gdb through port 4242. |
| 90 | +If you get this error it means that port 4242 is already in use by another program. |
| 91 | +Change `STUTIL_PORT` inside the Makefile to use a different port. |
| 92 | +e.g., `STUTIL_PORT = 5555` will use port 5555 |
| 93 | + |
| 94 | + |
| 95 | + |
| 96 | +3. *When I run* `make server` *I get* `bind: Permission denied` |
| 97 | +You selected a privileged port number. Try another port >=1024. |
| 98 | + |
| 99 | + |
| 100 | + |
| 101 | +4. *When I run* `make server` *I get* `make: st-util: Command not found` |
| 102 | +Check that [**st-util**](https://github.com/texane/stlink) is correctly installed. |
| 103 | +If st-util is installed in a user-defined folder you must change the `STUTIL` variable inside the Makefile. |
| 104 | +e.g., `STUTIL = /home/john/stlink/st-util` will run `/home/john/stlink/st-util` |
| 105 | + |
| 106 | + |
| 107 | + |
| 108 | +5. *When I run* `make flash` *or* `make erase` *I get* `make: st-flash: Command not found` |
| 109 | +Check that [**st-flash**](https://github.com/texane/stlink) is correctly installed. |
| 110 | +If st-flash is installed in a user-defined folder you must change the `STFLASH` variable inside the Makefile. |
| 111 | +e.g., `STFLASH = /home/john/stlink/st-flash` will run `/home/john/stlink/st-flash` |
| 112 | + |
| 113 | + |
| 114 | + |
| 115 | +6. *When I use optimization levels higher than* `-O0` *my program doesn't work correctly* / *interrupts don't work.* |
| 116 | +Make sure that **every** global variable that gets changed by an ISR is declared as **volatile**. |
| 117 | + |
| 118 | + |
| 119 | + |
| 120 | +7. *Can I enable optimization for the debug build?* |
| 121 | +Yes, but be aware that debugging an optimized program is often a bad idea. |
| 122 | +You can set the debug optimization level using the `DBG_OPTFLAGS` variable inside the Makefile. |
| 123 | +e.g., `DBG_OPTFLAGS = -O2` will compile using `-O2` |
| 124 | + |
| 125 | + |
| 126 | + |
| 127 | +8. *Can I see what* `make` *is doing?* |
| 128 | +Yes, by changing the `VERBOSE` variable inside the Makefile. |
| 129 | +`VERBOSE = N` is the default option and `VERBOSE = Y` will enable the verbose mode. |
| 130 | + |
| 131 | + |
| 132 | + |
| 133 | +9. *When i run* `make` *I get* `region 'RAM' overflowed by ... bytes` |
| 134 | +This means you're using too much RAM. |
| 135 | +The linker script is set to keep 2KB of free RAM to be used by the stack and the heap. |
| 136 | +This is accomplished by defining two fake empty memory sections in the linker script. |
| 137 | +If you're sure that your program will work with less free RAM, you can lower the amount of free memory by changing the `_heap_size` and `_stack_size` variables in the linker script. |
| 138 | +By default they are set to 1KB each. |
| 139 | + |
| 140 | + |
| 141 | + |
| 142 | +--- |
| 143 | + |
| 144 | + |
| 145 | + |
| 146 | + |
| 147 | + |
| 148 | + |
| 149 | + |
0 commit comments