The stm32 microprocessor is based on the ARM core, so many ARM-based embedded development environments can be used on the stm32 development platform. Development tools can be used for stm32 development. Selecting an appropriate development environment can speed up development and reduce development costs. This chapter briefly introduces the commonly used development tools Keil MDK and IAR ewarm of stm32, and then explains how to use resources on the stm32 Chip Based on the Development Board of the mega_sk Simulation Evaluation Board and the stm32f103c Development Board, finally, an application example of Data Collector Based on stm32 is provided.
5.1 Introduction to Keil MDK
Keil is a microcontroller software development platform developed by Keil (now incorporated into arm), a well-known German software company. It is a mainstream tool for ARM Kernel Single Chip Microcomputer Development. Keil provides a complete development solution including C compiler, macro assembly, connector, library management, and a powerful simulation debugger. These features are combined by an integrated development environment (uvision. Uvision's current highest version is uvision3. Its interface is similar to the commonly used Microsoft Vc ++ interface. The interface is friendly, easy to learn, and easy to use. During program debugging, software Simulation also has powerful functions. Therefore, many engineers who develop arm applications like it very much.
5.1.1 introduction to the development process and integrated development environment
1. Keil software development cycle
Use Keil to develop embedded software. The development cycle is similar to that of other platforms. There are roughly the following steps:
1. Create a project, select a target chip, and perform necessary engineering configuration.
2. compile C or compile the source file.
3. Compile the application.
4. Modify errors in the source program.
5. online debugging.
The following structure describes the entire process of Keil software development.
2. uvision3 integrated development environment
Uvision3 IDE is a Windows-based software development environment integrating editing, compilation, and project management. Uvision3 integrates the C language compiler, macro compilation, link/location, and HEX file generator. Uvision3 has the following features:
Fully functional source code editor,
The device library used to configure development tools,
The project manager used to create and maintain a project,
All tool configurations are performed in the dialog box,
Integrated with the source code-level simulation debugger, including high-speed CPU and peripheral simulators,
The Flash programming tool used to download applications to Flash ROM,
Complete development tool help documentation, device data tables, and user guide.
Uvision3 has a good interface style and is a typical debugging window.
Project Area: used to access file groups and files. debugging allows you to view CPU registers.
Output Window: displays the compilation result to quickly locate the error. It is also the DEBUG command input and output window, and can also be used to display the search result.
Memory Window: displays the content in the village of the specified address.
View and call the Stack window: Used to view and modify the value of a variable, and the current function call tree is used.
Code window: Used to view and edit source files.
Peripherals dialog box: Check the status of the On-chip peripherals.
3. ulink USB-JTAG interface adapter
The ulink USB-JTAG is a small hardware adapter used to connect the USB port of the PC and the JTAG port of the Development Board. With ulink, you can create, download, and test embedded applications on a real target board. Ulink supports the following operations:
Download the target program.
Check the memory and registers.
Run the program in one step.
Insert multiple breakpoints.
Real-time program running
Flash Storage
5.1.2 Project Management
In project development, it is not just a single source program. You also need to select a CPU for this project (Keil supports hundreds of CPUs, and these CPU features are not exactly the same ), determine the parameters for compilation, assembly, and connection, and specify the debugging method. Some projects also contain multiple files. For ease of management and use, Keil uses the project) this concept adds these parameter settings and all required files to a project, and only compiles (assembles) and connects the project rather than a single source program. The following is a simple example of helloworld to explain how to build and configure a project. In this example, we will flash the led1 on the Development Board. In this example, the stm32f103c Development Board is used as the target board.
5.1.2.1 create a project
Click "project" and select "New uvision Project". A dialog box is displayed, requesting a name for the project to be created.
Select the path you want to save and enter the name of the project file. Here we will name it helloworld. The extension of uvision3 project file is ". uv2", and then click "save ". A dialog box appears asking you to select the model of the target device.
You can select a processor based on your needs. If the processor model you are using cannot be found in the list, you can also find a compatible model that you are using. Here we select stm32f103c8. the right column shows the basic description of the chip and then click "OK ".
Some chips provide the startup Code. At this time, click "yes" and this project will be ready.
5.1.2.2 configuration project
After the project is established, further settings must be made for the project to meet the requirements.
Right-click (Note: Right-click) and click "Target 1" in the project window on the left. A menu is displayed, select Options for target 'target 1' (you can also click target 1 in the project window ", use the menu "project"-> "options for target 'target 1') to display the project configuration dialog box, as shown in:
This dialog box is very complex and related to the chip selected by the root. There are 10 pages, most of which can be selected by default. The following describes some configuration notes.
1. settings on the output Tab
Select folder for objects: select the directory in which the compiled target file is stored. The default location is the directory of the project file.
Name of executable: name of the generated target file. The default value is the project name.
Create executable: Generate OMF and hex files. The OMF file name is the same as the project file name but does not contain the extension.
Debug information: used to generate debugging information for the debug version. Otherwise, you cannot perform one-step debugging.
Create batch file: generate a batch file used to implement the entire compilation process. This file can be used to compile provincial programs without IDE.
Create HEX file: This option is not selected by default. If you want to write a piece for a hardware experiment, you must select this option. This is easy for beginners to neglect. In this case, note that you must select the file. Otherwise, the Hex file will not be compiled.
Big endian: the encoding format, which is related to the CPU. If the CPU uses big endian encoding, check it.
Browse information: Generate information used to quickly locate the source file.
Create Library: generate the Lib library file, which is not selected by default.
In the helloworld project we just created, we changed three places and created an output directory under the project directory to save the target file to avoid mixing with the source file. In addition, create HEX file and browse information are selected, as shown in.
2. C/C ++ tab settings
Include paths: Specifies the search path for the header file. You can add multiple search paths.
Here, we can keep the default options for all our options.
3. debug tab settings
On the left is the simulation environment corresponding to uvision3, and on the right is the simulator. Here, the ulink cortex debugger simulator on the right is selected as an example.
If you have connected the ulink simulator to your computer, click "Settings" to go to the arm target driver setup interface.
Ulink-JTAG/SWD adapter:
Serial No: list the serial numbers of all the ulink adapters currently connected to the host. You can select the ulink adapter to use from the list.
The ulink version, device family, and firmware version lists the currently selected ulink adapter versions, device families, and firmware versions.
Swj and port: select a port based on the Interface Type of the Development Board. There are two types: JTAG and SW. If you select swj, two methods are supported.
Max clock: specifies the maximum communication clock with the Development Board.
JTAG device chain: displays the Development Board currently connected through the adapter.
Automatic Detection: automatic monitoring. If you select this option, the system automatically detects the connected development board. We recommend that you use this option.
Manual configuration: manual configuration. You can manually set attributes such as ID code, device name, and IR Len to find the device.
Debug:
Cache options:
Cache code: notifies the debugger that the Downloaded Program code will not change. If selected, uvision will not read the program code from the target system.
Cache memory: determines whether to update the memory display when the program stops running during the debugging process.
Download options:
Verify code download: Compares the content of the application on the target storage and debugger.
Download to flash: Download the code to all storage areas. If not selected, the debugger will not download the code to the memory address range specified in Flash download setup.
Misc options:
Use reset at startup: When selected, the debugger initiates a CPU reset when Debugging starts.
Load application at startup: import the executable file specified in the output tag to the start address of the debugger.
Run to main: The main function entry is paused when Debugging starts.
Initialization File: Specifies a file that contains a set of Debugging commands. These commands are used by the debugger or the debugging function during debugging.
Restore debug session settings: Use the previous debugging process for breakpoints, watchpoints, memory display, and Toolbox (if these items are selected ).
Driver DLL-parameter: The target driver DLL set by Device database. Do not modify it.
Dialog DLL-parameter: the dialog box DLL set by Device database. Do not modify it.
Here we have modified two places, selected use ulink and run to main, and made some adjustments to the ulink settings, as shown in Figure 5.11.
4. Setting the utilities Tab
Configure flash menu command
Use target driver for flash programming: select the driver that is consistent with the debugging interface in the list. The init file settings are also consistent with the preceding debugging settings. Click settings to go to the Flash download Setup page.
Download function: defines the operations performed during flash writing.
Erase full chip: select one of the first three items, and erase the entire flash memory before writing the program.
Erase sectors: the slice to be used by the program before the program is burned.
Do not erase: Do not erase
Program: Use the program of the current uvision project to burn the Rom.
Verify: Verify that the Flash ROM content is consistent with the program in the current project.
Reset and run: reset the Development Board and run the program after writing and verification are complete.
Ram for algorithm: Specifies the ram region used to burn the Write Program, which is usually a piece of On-Chip space on the microcontroller.
Start: Start address.
Size: size.
You can click Add to add. Click Add. You will see the following selection list. You can select an appropriate chip based on your selection, or manually add it.
Use external tool for flash programming: Use a third-party tool for flash download.
Command: The command file of the flash burning tool to be used (usually an .exe file ).
Arguments: parameters passed to the Flash burning tool.
Run independent: when the program is selected, uvision does not wait for the flash to finish writing. If this parameter is not selected, uvision will wait for the flash to finish writing and display the burning result in the output window.
Modify flash download setup in helloworld, as shown in. The project settings are complete.
5.1.2.3 open the project
Choose "project"> "open project" to open an existing project. The open file dialog box is displayed, asking us to select the project file to open.
Select the path of the project you want to open and click "open" to open the project. We can also find a uvision3 project file suffixed with "uv2", just like opening other files, and double-click it directly. Windows will automatically call uvision3 to open this file, the premise is that you have installed uvision3 on your computer and associated with the "uv2" file.
5.1.3 compile the source program
Choose "file"> "new" from the menu, or click "new file" in the toolbar to open a new text editing window on the right of the Project window. In this window, you can enter the program code.
It should be noted that the source file is a common text file, not necessarily written using Keil software, can be written using any text editor, and the Keil editor does not support Chinese characters, we recommend that you use edit software such as ultraedit to input the source program.
Each program has at least one main function prototype: int main (void). This is the entry address of the program and the program will start running from here. In addition, we also need to initialize the Development Board for clock and interrupt, which will be completed in the functions rcc_configuration and nvic_configuration. The source file code list is as follows.
- # Include "stm32f10x_lib.h"
- Gpio_inittypedef gpio_initstructure;
- Void delay (vu32 ncount );
- /*************************************** **************************************** Function Name: main
- * Description: Main Program
- * Input: None
- * Output: None
- * Return: None
- **************************************** **************************************/
- Int main (void)
- {
- Rcc_apb2periphclockcmd (rcc_apb2periph _ gpiob, enable );
- Gpio_initstructure.gpio_pin = gpio_pin_9;
- Gpio_initstructure.gpio_mode = gpio_mode_out_pp;
- Gpio_initstructure.gpio_speed = gpio_speed_50mhz;
- Gpio_init (gpiob, & gpio_initstructure );
- While (1)
- {
- Gpio_writebit (gpiob, gpio_pin_9,
- (Bitaction) (1-gpio_readoutputdatabit (gpiob, gpio_pin_9 )));
- Delay (1000000 );
- }
- }
- Void delay (vu32 ncount)
- {
- For (; ncount! = 0; ncount --);
- }
After the code is edited, you can save the source file, select "file"> "save" from the menu, or click the save file button on the toolbar to save the source file.
A File Save dialog box appears. Select the path you want to save and enter the file name helloworld. C. Note that you must enter the extension. If it is a C program file extension of. C, if it is an assembly file extension of. S, it can also be stored as. ASM. The description file can be saved as the. txt extension.
After the source file is edited, we also need to add the source file to the project. After the project is created, "Target 1" will appear on the file page of the Project window ", there is a "+" in front, click "+" to expand, you can see the next layer of "Source group 1", we need to add the source file to this, click "Source group 1" to display it in reverse white. Then, right-click it and a drop-down menu appears, as shown in.
Select "add file to group 'source group 1'". A dialog box is displayed, asking for source files, as shown in.
Note: The "file type" in this dialog box is C source file by default (*. c), we can adjust this to filter the files in the desired format to help us quickly find the files. For an assembly file, select "ASM source file". For a target file, select "Object File". For a library file, select "Library File ". Click "add" or double-click the file to be added. Note: After you click "add", the window will not disappear. If you want to add multiple files, you can add them continuously. After adding the files, click "close" to close the window. Beginners often mistakenly believe that the operation fails and double-click the same file again or click "add" again. The following dialog box is displayed.
Here we set the newly created helloworld. c. After adding the file, click the "+" sign before "Source group 1" to find the file hellowrold we just added. C is already in it. Double-click the file name to open the source program file in the code area.
In addition to adding files, you can also add a new group. The operation is similar to adding files. After the drop-down menu appears, select "New Group". Then, the newly added group is displayed in the project window. You can also click the color icon on the toolbar to go to the "components, environment and books" window, as shown in.
You can double-click an item in the list to rename the item. You can click a blank space to add a new item, and the dotted box button to add a new item. Red Cross indicates that the selected item is deleted, the up and down arrows are used to adjust the position of the selected item in the list. "add files" can be used to add new source files. The operation procedure is the same as that of adding files before.
5.1.4 compile the program
After the program code is written, it enters the program compilation stage. You can initiate the compilation process through menus, toolbar, and floating menus, or through batch files, this batch file is mentioned in the settings on the output tab.
The area circled by the red line is the corresponding menu compilation command and toolbar compilation command. The meaning of each command is as follows:
Clean target: Clear the compilation result.
Build target: Compile the modified file and compile the application.
Rebuild all target files: recompile all source files and compile the application.
Batch build: compile with the batch file output.
Translate **. *: compile a source file. **. * indicates the source file to be compiled.
Stop build: This item is valid only during compilation.
By right-clicking "Target 1" in the project window, you can also pop up the corresponding compilation menu. The meaning of each command is the same as above.
Now let's compile our "helloworld", as shown in. The compilation result is displayed in the output window.
Unfortunately, there are many errors. You may have discovered that our code uses a lot of functions that we do not define and do not belong to Standard C, yes, this is the function provided by the St Firmware Library. What we need to do is to add the Firmware Library to our project and other development environments, we can directly add the source file of the Firmware Library and compile it together with the project, or add the compiled static Connection Library (*. lib) file. Here we will take the following as an example. These libraries are provided when we have installed the Keil development environment. The path is generally in the arm/rv31/lib/directory under your installation directory, for example, the path here is D:/Keil/ARM/rv31/lib/St. Here we will add a new group named fwlib. Then add the library file to this group. For detailed operation procedure, refer to the section on compiling the source program. After adding the library, we will compile it again.
The problem is solved. When "0 error (s), 0 warning (s)" occurs, it means that our program has passed the syntax check, sometimes some warnings do not affect program execution, but we should take them with caution and carefully analyze every warning. If there is a syntax error or warning in the source program, we can double-click the line in the output window to quickly locate the error.
5.1.5 program debugging
Compilation only indicates that our code has no syntax errors. As for other errors in the source program, it must be debugged before it can be found and solved. In fact, apart from extremely simple programs, most programs must be debugged repeatedly to obtain the correct results. Therefore, debugging is software development. Next, we need to run our program to verify whether the expected results have been achieved. That is, program debugging. program debugging is often the most difficult stage in the process of program development, especially for some relatively large programs. Next let's take a look at the support of uvision3 for debugging.
5.1.5.1 common Debugging commands
After successfully assembling and connecting the project, press Ctrl + F5 or use the menu debug-> start/stop debug session to enter the debugging status. After entering the debugging status, the interface has a significant change compared with the editing status. The DEBUG command can now be used only for commands that are unavailable in a single item, a toolbar is added for running and debugging, as shown in. You can find the corresponding shortcut buttons for most commands in the Debug menu.
Common Debug menu commands are as follows:
Start/stop debug session: start or stop debugging.
Run: the breakpoint that keeps executing the next activity.
Step: Perform one step.
Step over: the process is executed in one step. A function is executed as a statement.
Step out of current function: jump out of the current function.
Run to cursor line: the row where the cursor is located.
Stop running: Stop running.
Breakpoints: Open the breakpoint dialog box.
Insert/remove breakpoint: insert/delete a breakpoint in the current row.
Enable/disable breakpoint: enables the breakpoint of the current row or invalidates the breakpoint.
Disable all breakpoints: All Breakpoints in the program are invalid.
Kill all breakpoints: Delete All Breakpoints in the program.
To learn program debugging, two important concepts must be clarified: One-step execution and full-speed execution. Full-speed execution refers to the execution of the next program after the execution of a program is completed without stopping the program. In this way, the execution speed of the program is very fast and the overall effect of the program execution can be seen, that is, the final result is correct or wrong, but if the program is wrong, it is difficult to confirm the program lines in which the error occurs. One-step execution is to execute one program each time. After the execution of this program, it stops. Wait for the command to execute the next program. Then we can observe the results after the execution of this program, whether the result is the same as what we want to write the program line, so that we can find the problem in the program. In program debugging, both running modes must be used. Flexible application can greatly improve the debugging efficiency.
In the debugging window, we can see a yellow debugging arrow pointing to the currently executed program line.
5.1.5.2 breakpoint settings
During program debugging, it is difficult for some program lines to determine when the program can be executed. Such problems are not suitable for single-step debugging, this is another very important method in program debugging-breakpoint settings. There are multiple methods to set breakpoints. Generally, you can set breakpoints on a program line. After the breakpoint is set, you can run the program at full speed. Once the program line is executed, the program will be stopped, you can observe the related variable values here to determine the problem. For instructions on how to set breakpoint commands, see common Debugging commands in the previous section. Once a breakpoint is set for a line, we can see a red box on the left side of the line (as shown in the 5.24 debugging window). If the breakpoint is disabled, the box will become white.
In addition to setting breakpoints in a program line, uvision3 also provides multiple methods to set breakpoints. Press debug> breakpoints to display a dialog box, this dialog box is used to set breakpoints in detail, as shown in.
The edit box after expression in figure 5.26 is used to enter an expression. This expression is used to determine the conditions for the program to stop running. It is powerful and involves a set of debugging algorithms built in uvision3, which are not described in detail here, see the help documentation.
5.1.5.3 debugging window
I have discussed some debugging methods above, and I have mentioned several times to check the execution status of the program. The debugging window is used to view the program execution status. Uvision3 provides various debugging windows, such as register windows, memory windows, disassembly windows, and peripheral windows.
1. Register window
Figure 5.26 is the content of the project window register page. The register page contains the previous working registers and system registers. This register is displayed in Reversed color whenever an operation is performed on a register in the program, click and then press F2 (two consecutive clicks) to modify the value.
2. Memory Window
The memory window can display the values in various memory in the system. You can enter "letters: Numbers" in the edit box after the address to display the corresponding memory values, including C, D, I, X, it represents the code bucket, directly addressable in-chip bucket, indirectly addressable in-chip bucket, extended external Ram unit value, and typed C: 0 to display the value of the RoM unit starting from 0, that is, to view the binary code of the program. The display value of the window can be displayed in various forms, such as decimal, hexadecimal, and hexadecimal. To change the display mode, right-click and select from the shortcut menu. In addition to display, you can also modify the value in memory, as shown in.
3. View and call the Stack window
This window can help us to view the current call tree. We can also view and modify the values of some variables in this window. Right-click a variable and choose add ** To Watch window from the floating menu. The local window displays the values of some local variables, you can switch between hexadecimal and decimal values by right-clicking in the viewing window, click the value column of a variable and then press F2 (the mouse clicks twice consecutively) to modify the value. As shown in.
4. Disassembly Window
Click View-> dissambly window to open the Disassembly window, which displays the disassembly code, the source code, and the mixed code of the corresponding disassembly code, online assembly can be performed in this window, the code that has been found can be tracked using this window, and the code in this window can be executed in a single step by means of assembly code. Right-click the shortcut menu, and a shortcut menu is displayed, as shown in 5.29. The mixed mode is displayed in hybrid mode, and the Assembly mode is displayed in return encoding mode.
5. Peripheral window
Uvison3 provides a peripheral interface dialog box to help you intuitively understand the use of various peripherals in a single-chip microcomputer. From the peripherals menu, the content in the drop-down menu is related to the chip you selected. All peripherals on the selected chip are listed. You can select one item to view or modify the status of the peripheral. For example, we use pb9 in gpio to control the LED flashing. We can switch the gpiob status to 5.30.
Now we can debug our helloworld. When the program runs, you will see that L1 on the development board keeps flashing.