Introduction and use of STM32 standard library

Initial STM32 standard library

Because the cores used in the Cortex series chips are the same, the difference is mainly the difference on the chip outside the core (the chip peripherals are mainly determined by the chip manufacturer). These differences make it difficult for software to be ported on chips with the same core and different peripherals. In order to solve the compatibility problem of cortex microcontroller software produced by different chip manufacturers, ARM and chip manufacturers have established the CMSIS standard. The CMSIS layer is located between the hardware layer and the user layer. It provides a hardware abstraction layer that has nothing to do with the chip manufacturer, provides a software interface, and weakens the hardware difference.
The most important CMSIS standard is the CMSIS core layer, which includes:

  • Kernel function layer: used to access kernel registers. Provided by ARM.
  • Device Peripheral Access Layer: Provides on-chip peripheral peripheral address and interrupt definitions. This is mainly provided by chip manufacturers.

STM32 library directory, file function

The library file can be downloaded from ST's official website. Here the version 3.5.0 is used as an example to introduce the structure of the library.

  • libraries: The source code and startup files of the driver library.
  • Project: Examples and project templates written in the driver library.
  • utilities: Contains routines based on ST official experiment board.
  • stm32f10x_stdperiph_lib_um.chm: library help document that introduces the function description of each peripheral.

CMSIS core layer

CMSIS / CM3 under the libraries folder is the core library, including

  • coresupport: This is provided by ARM, which has core_cm3.c and core_cm3.h, which implements the mapping of core registers. The main operation is the core peripheral registers.
  • devicesupport / ST / STM32F10x /: It contains some files defined by ST manufacturers. The startup / arm directory contains the startup files of various types of boards (here it is divided with the capacity of flash). The stm32f10x.h file implements the mapping of all registers of on-chip peripherals and is a very important header file. The system_stm32f10x.c file implements the STM32 clock configuration, and the systemInit function sets the system clock to 72M.

Peripheral access interface

The STM32F10x_StdPeriph_Driver file is a driver written for each chip manufacturer. There are two folders, Inc and SRC, which are used to store header files and source files. Note that there is also a misc.c file that provides peripheral access to the NVIC in the kernel. This file needs to be added to the project when the configuration is interrupted.

Several files need to be added when the project is created:

  • stm32f10x_it.c This file is specially used to write interrupt service functions. The function of the interrupt service function is implemented in it. The function name is defined by the manufacturer and can be found in the assembly startup file.
  • system_stm32f10x.c: This file provides functions to initialize the system clock and expand external memory when the chip is powered on. The systemInit function is implemented in this file.
  • stm32f10x_conf.h: the header file of each peripheral of this file, if you don't need the header file, you can comment it out in this file. It is included in stm32f10x.h.
  • stm32f10x.h: This file is very important, the mapping of the registers of the on-chip peripherals is implemented here, and the header file that contains the on-chip peripherals that need to be used.

The relationship between the library files is as follows
Insert picture description here

Create a project using a library file

The structure of the STM32 library file is explained above, mainly to facilitate the development of the library file in development. In most cases, library files are used for development. Only in rare cases will register programming be used (for example, interrupt service functions that have strict requirements on resources (code runtime, memory) will only use register programming ). The following describes how to use the library file to build the project.

New local project folder

Create a new "Project Template" on the local computer, and then create the following 6 folders under it.

  • Doc: The file used to store the program description, added by the person who wrote the program.
  • Libraries: store library files. CMSIS stores CM3 kernel related libraries, and Periph_driver stores peripheral library files.
  • Listing: A list of C / Assembly / Links generated by the compiler during compilation.
  • output: stores debugging information, hex files, preview information, package library, etc. generated by compilation.
  • project: used to store the project.
  • user: The driver file written by the user. Among them stm32f10x_conf.h is used to configure the library header file, stm32f10x_it.h and stm32f10x_it.h interrupt related functions are written here. main.c main function file.

New Construction

  • Open KEIL5, create a new project, choose the project name, and then select the CPU model. Project personal habits are kept in the project.
    Insert picture description here
  • Add a group file to the project. The file is obtained from the locally created project folder.
    Insert picture description here
  • Add files, copy the library files to the directory of the corresponding folder of the project template, and then add these files in the newly created project. Double-click the group file and the path to add the file will appear. Selecting the file can either
    Insert picture description here
    create a directory and add a file or directly use manage project items to process it, directly in one step.
    Insert picture description here

Configure the Magic Wand tab:

  • If you want to use printf, you need to check Use Microlib in Target
    Insert picture description here
  • In the output option, locate the output folder to the output folder under our project directory, check the create HEX file will generate the hex file during the compilation process.
    Insert picture description here
  • In the listing tab, locate the output file to the "Listing" folder in our project directory.
    Insert picture description here
  • ** In the C / C ++ tab, add the path of the header file that is searched when the macro and compiler are compiled. ** If the header file path is incorrectly compiled, an error will be reported.
    Insert picture description here
    This will build a project. Below you can use the library file for development
Published 35 original articles · Like1 · Visits 1870

Guess you like

Origin blog.csdn.net/lzj_linux188/article/details/103723291