1# How to make a STM32 BSP for RT-Thread
2
3In order to allow developers to use BSP for development better and more conveniently, the RT-Thread team rearranged the existing STM32 series of BSPs and launched a new BSP framework. The new BSP framework has been greatly improved in terms of ease of use, portability, driver integrity, and code standardization. Development under the new BSP framework can greatly improve the efficiency of application development.
4
5The new BSP framework also introduces the CubeMX tool, which can be used to configure the peripheral pins which are used in the BSP. The CubeMX tool provides a graphical configuration interface. This graphical configuration method is more intuitive for developers. It not only allows developers to flexibly configure the resources used in the BSP, but also allows developers to understand the use of resources at a glance .
6
7The main features of the new STM32 BSP framework are as follows:
8
9- Provide multiple series of BSP templates, greatly reducing the difficulty of adding new BSPs;
10- Each BSP is equipped with a complete set of driver files, so that developers can easily use all drivers;
11- Developers can use the CubeMX tool to configure the BSP graphically.
12
13## 1. Introduction to BSP Framework
14
15The BSP frame structure is shown in the figure below:
16
17![BSP frame structure](./figures_en/frame.png)
18
19Each BSP of the STM32 series consists of three parts, namely the general library, the BSP template and the specific development board BSP. The following table uses the F1 series BSP as an example to introduce these three parts:
20
21| Item                                | Folder                              | Detail                                                       |
22| ----------------------------------- | ----------------------------------- | :----------------------------------------------------------- |
23| General library                     | stm32/libraries                     | Used to place HAL library and multi-series general peripheral driver files developed based on HAL library |
24| STM32F1 series BSP project template | stm32/libraries/templates/stm32f10x | You can make more F1 series BSP by modifying this template   |
25| Specific development board BSP      | stm32/stm32f103-blue-pill           | Modified on the basis of the BSP template                    |
26
27## 2. Knowledge of background
28
29## 3. Make a STM32 BSP for steps
30
31Making a STM32 BSP is divided into the following five steps:
32
331. Copy the generic template
342. Use CubeMX tool to configure the project
353. Modify the Kconfig file in the BSP
364. Modify the relevant files of the build project
375. Regenerate the project
38
39In the following chapters, these five steps will be introduced in detail to help developers quickly create the required BSP.
40
41### 3.1 Copy the generic template
42
43The first step in making a new BSP is to copy a BSP template of the same series as the basis, and obtain a new BSP by modifying the BSP template.
44
45The folder structure of the F1 series BSP template used in this example is as follows:
46
47![F1 series BSP template folder contents](figures_en/bsp_template_dir.png)
48
49Copy the `stm32f10x` folder under the template folder and change the name of the folder to `stm32f103-blue-pill`, as shown in the following figure:
50
51![Copying common templates](./figures_en/copy.png)
52
53Modify the configuration file in the board folder. The modified content is shown in the following table:
54
55| Item                      | Instruction                                                  |
56| ------------------------- | ------------------------------------------------------------ |
57| CubeMX_Config (folder)  | CubeMX project                                               |
58| linker_scripts (folder) | BSP link script                                              |
59| board.c/h                 | System clock, GPIO initialization function, chip memory size |
60| Kconfig                   | Chip series, peripheral resources                            |
61| SConscript                | Chip startup file, target chip model                         |
62
63### 3.2 Use CubeMX to configure the project
64
65Create a CubeMX project based on the target chip. The default CubeMX project is in the **CubeMX_Config** folder, double-click to open the `CubeMX_Config.ioc` project, as shown in the figure below:
66
67![open_cubemx](figures_en/open_cubemx.png)
68
69Change the chip model to STM32F103C8Tx in the CubeMX project.
70
71Note:The version of CubeMX used in this article is 6.12.0
72
73#### 3.2.1 Generate CubeMX project
74
75Configure the system clock, peripheral pins, etc. The steps are shown in the figure below:
76
771. Turn on the external clock, set the download mode, and turn on the serial peripherals (note that only the pins of the serial peripherals need to be selected, no other parameters need to be configured):
78
79   ![Turn on the clock](./figures_en/CubeMX_1.png)
80
81   ![Configuring the download Mode](figures_en/CubeMX_2.png)
82
83   ![Open the serial port](figures_en/CubeMX_3.png)
84
852. Configure the system clock:
86
87   ![Configuring the System Clock](./figures_en/CubeMX_4.png)
88
893. Set the project name and regenerate the CubeMX project at a specified address:
90
91   ![Generate the corresponding configuration code](./figures_en/CubeMX_5.png)
92
93   Note: When generating the code, do not check the following options (ie: Do not let it generate a peripheral initialization as a pair of .c/.h files per perioheral.)
94
95   ![generate-code](figures/generate-code.png)
96
974. The final project directory structure generated by CubeMX is shown in the figure below:
98
99   ![CubeMX 7](./figures_en/CubeMX_6.png)
100
101#### 3.2.2 Copy initialization function
102
103The function `SystemClock_Config()` is placed in the **board.c** file, which is responsible for initializing the system clock. When using the CubeMX tool to reconfigure the system clock, this function needs to be updated. This function is generated by the CubeMX tool and is placed in the file `board/CubeMX_Config/Src/main.c` by default. However, this file does not include in our project, so we need to copy this function from main.c to the board.c file.  The content of this function is as follows:
104
105![board_1](./figures_en/board_1.png)
106
107If your MCU clock tree is relatively advanced and you have configured peripherals to use PLL2, PLL3, or other non-default settings, resulting in the generation of the `PeriphCommonClock_Config()` function, please copy it as well. Since `rt-thread` does not explicitly call this function, you can manually add a call to it within `SystemClock_Config()`. The function content may look like the following:
108
109![board_pclock](figures_en/board_pclock.png)
110
111If your MCU includes an MPU peripheral and you do not want to use the `Memory Protection` provided by `rt-thread`, but instead prefer the configuration generated by `CubeMX`, you can copy `MPU_Config()`. Similarly, since `rt-thread` does not explicitly call this function, you can use `INIT_BOARD_EXPORT` or other methods to ensure it is called automatically. The function content may look like the following (it is recommended to configure it carefully in sections for actual use):
112
113![board_mpu](figures_en/board_mpu.png)
114
115The relevant parameters of FLASH and RAM are configured in the **board.h** file. What needs to be modified in this file is the parameters controlled by the two macros `STM32_FLASH_SIZE` and `STM32_SRAM_SIZE`. The flash size of the STM32F103C8Tx chip used in the BSP produced this time is 64k, and the size of the ram is 20k, so the file is modified as follows:
116
117![Modified board.h](./figures_en/board_h.png)
118
119#### 3.2.3 Heap memory configuration
120
121Normally, a part of the memory space in the system RAM will be used as heap memory. The function of the following code is to specify the start address **HEAP_BEGIN** and end address **HEAP_END** of the heap memory under different compilers. The values of **HEAP_BEGIN** and **HEAP_END** here need to be consistent with the configuration modified in the following section [3.4.1 Modify Link Script](# 3.4.1 Modify Link Script).
122
123In some series of chips, the chip RAM may be distributed in multiple discrete memory areas. At this time, the location of the heap memory can be in the same continuous memory area as the system memory, or it can be stored in a separate memory area. For example, on the L4 series of chips, the heap memory can be configured in a 96k memory space with a starting address of `0x20000000`, and the 32k memory space starting from `0x10000000` can be used as the system running memory.
124
125![heap_config](figures_en/heap_config.png)
126
127### 3.3 Modify Kconfig configuration
128
129Modify the contents of the `board/Kconfig` file in this section as follows:
130
131- Chip model and series
132- Peripheral support options on BSP
133
134The modification of chip model and series is shown in the following table:
135
136| Macro Definition   | Meaning     | Format             |
137| ------------------ | ----------- | ------------------ |
138| SOC_STM32F103RB    | Chip model  | SOC_STM32xxx       |
139| SOC_SERIES_STM32F1 | Chip series | SOC_SERIES_STM32xx |
140
141Regarding the peripheral support options on the BSP, a BSP submitted for the first time only needs to support the GPIO driver and the serial port driver, which means only these two driver configuration items need to be retained in the configuration options, as shown in the following figure:
142
143![Modified Kconfig](./figures_en/Kconfig.png)
144
145### 3.4 Modify project building related files
146
147#### 3.4.1 Modify the link script
148
149**linker_scripts** The link file is as shown in the figure below:
150
151![Link scripts that need to be modified](./figures_en/linker_scripts.png)
152
153The following uses the link script link.sct used by MDK as an example to demonstrate how to modify the link script:
154
155![linkscripts_change](figures_en/linkscripts_change.png)
156
157The chip used to make the BSP this time is STM32F103RB, and the FLASH is 128k, so modify the parameters of LR_IROM1 and ER_IROM1 to 0x00020000. The size of RAM is 20k, so modify the parameter of RW_IRAM1 to 0x00005000. Such a modification method is sufficient for general applications. If there are special requirements in the future, you need to modify it as required according to the syntax of the link script. When modifying the link script, you can refer to the [**3.2.3 Heap memory configuration**](# 3.2.3 Heap memory configuration) chapter to determine the BSP memory allocation.
158
159The other two link script files are link.icf used by IAR and link.lds used by the GCC compiler. The modification method is similar, as shown in the following figure:
160
161![link_icf](figures_en/link_icf.png)
162
163![link_lds](figures_en/link_lds.png)
164
165#### 3.4.2 Modify the build script
166
167The **SConscript** script determines the files to be added during the generation and compilation of the MDK/IAR project.
168
169In this step, you need to modify the chip model and the address of the chip startup file. The modification content is shown in the figure below:
170
171![Modify the startup file and chip model](./figures_en/SConscript.png)
172
173Note: If you cannot find the .s file of the corresponding series in the folder, it may be that multiple series of chips reuse the same startup file. At this time, you can generate the target chip project in CubeMX to see which startup file is used. Then modify the startup file name.
174
175#### 3.4.3 Modify the project template
176
177The **template** file is a template file for generating the MDK/IAR project. By modifying the file, you can set the chip model used in the project and the download method. The project template file of MDK4/MDK5/IAR, as shown in the figure below:
178
179![MDK/IAR engineering template](./figures_en/template_1.png)
180
181The following takes the modification of the MDK5 template as an example to introduce how to modify the template configuration:
182
183![Select the chip model](./figures_en/template_2.png)
184
185Modify the program download method:
186
187![Configuring the Download Mode](./figures_en/template_3.png)
188
189### 3.5 Regenerate the project
190
191Env tool is required to regenerate the project.
192
193#### 3.5.1 Regenerate the rtconfig.h file
194
195Enter the command menuconfig in the Env interface to configure the project and generate a new rtconfig.h file. As shown below:
196
197![Enter menuconfig to go to the configuration screen](./figures_en/menuconfig_1.png)
198
199#### 3.5.2 Rebuild the MDK/IAR project
200
201The following takes regenerating the MDK project as an example to introduce how to regenerate the BSP project. Use the Env tool to enter the command `scons --target=mdk5` to regenerate the project, as shown in the following figure:
202
203![Regenerate the BSP project](./figures_en/menuconfig_3.png)
204
205Rebuild the project successfully:
206
207![Regenerate the BSP project](./figures_en/menuconfig_4.png)
208
209At this point, the new BSP can be used. Next, we can use the commands `scons --target=mdk4` and `scons --target=iar` respectively to update the MDK4 and IAR projects so that the BSP becomes a complete BSP that can be submitted to GitHub (Making MDK4 project is optional).
210
211## 4. Specifications
212
213This chapter introduces the specifications that should be followed when making and submitting the RT-Thread STM32 series BSP. After the BSP is produced, the developer can check the produced BSP according to the checkpoints set out in this specification to ensure that the BSP has a higher quality before submission.
214
215### 4.1 Specification of making BSP
216
217The specifications of making STM32 BSP are mainly divided into three aspects: engineering configuration, ENV configuration and IDE configuration. In the existing STM32 series BSP templates, the templates have been configured according to the following specifications. In the process of making a new BSP, when copying the template for modification, you need to be careful not to modify these default configurations. After the BSP is completed, the newly-made BSP needs to be tested for its function, and the code is submitted after the function is normal. The production specifications of the BSP will be described in detail below:
218
219#### 4.1.1 Project configuration
220
221- Comply with RT-Thread coding standard, unified code comment style
222
223- The main function remains the same
224  - If there is an LED, **only put ONE** LED 1HZ flashing program in the main function
225- Heap initialization needs to be completed in `rt_hw_board_init`: call `rt_system_heap_init`
226- By default, only the GPIO driver and the serial port driver corresponding to FinSH are initialized, and DMA is not used
227- When the onboard peripheral driver is enabled, it should be able to compile, download and use without modifying the code
228- Before submitting, check whether the three compilers of GCC/MDK/IAR are directly compiled or compiled successfully after regenerating
229- Use the `dist` command to publish the BSP and check whether the project generated by the dist command can be used normally
230
231#### 4.1.2 ENV configuration
232
233- The system heartbeat is uniformly set to 1000 (Macro: RT_TICK_PER_SECOND)
234- The assertion in the debugging option needs to be turned on in the BSP (macro: RT_USING_DEBUG)
235- The system idle thread stack size is uniformly set to 256 (Macro: IDLE_THREAD_STACK_SIZE)
236- Turn on automatic component initialization (Macro: RT_USING_COMPONENTS_INIT)
237- Need to enable the user main option (Macro: RT_USING_USER_MAIN)
238- FinSH only uses MSH mode by default (Macro: FINSH_USING_MSH_ONLY)
239
240#### 4.1.3 IDE configuration
241
242- Enable automatic operation after downloading the code
243- Enable C99 support
244- Enable One ELF Section per Function (MDK)
245- Temporary files generated by MDK/IAR are placed in the keil/iar folder under build
246- The names of bin files generated by MDK/GCC/IAR are unified into rtthread.bin
247
248### 4.2 Specification of BSP submission
249
250- Please carefully modify the README.md file of the BSP before submission. The peripheral support form of the README.md file only fills in the peripherals supported by the BSP. You can refer to other BSPs to fill in.
251- Submission of BSP is divided into two stages:
252  - The first stage: Basic BSP includes serial port driver and GPIO driver, and can run FinSH console. Completion of MDK4, MDK5, IAR and GCC compiler support, if the chip does not support a certain type of compiler (such as MDK4), you don’t need to do it.
253  - The second stage: complete the onboard peripheral driver support, all onboard peripherals can be used directly after they are configured using `menuconfig`. If the development board does not have on-board peripherals, this stage don't need to be completed. Different drivers should be submitted separately to facilitate review and merging.
254
255- Only submit documents necessary for the BSP and delete irrelevant intermediate documents. Please check other BSPs for documents that can be submitted.
256- When submitting libraries of different series of STM32, please refer to the HAL libraries of f1/f4 series and delete redundant library files.
257- Compile and test the BSP before submission to ensure that it compiles properly under different compilers.
258- Perform functional tests on the BSP before submission to ensure that the BSP meets the requirements in the engineering configuration chapter before submission.
259
260