STM32 Standard Peripherals Library

Source: Internet
Author: User
Tags data structures volatile

The last technical article of the June, recorded a relatively simple STM32 standard peripheral library. 1. Overview of the standard Peripherals Library

The STM32 standard Peripheral library is a firmware function package that consists of programs, data structures, and macros, including the performance characteristics of all the peripherals of the microcontroller, which also includes driver descriptions and application examples for each peripheral, providing an intermediate API for developers to access the underlying hardware, by using the Firmware function library, Developers can easily apply every peripheral without having to master the underlying hardware details. Therefore, the use of the solid-state function library can greatly reduce the developer's development and use of the internal and external time, thereby reducing development costs. Each peripheral driver is comprised of a set of functions that cover all the functions of that peripheral. The development of each device is driven by a common, standardized API. The V3.5 version of the peripheral library is described here, and is the latest version to date. 2. stm32f10x Standard Peripheral library file structure

The download of the library is on the official website of St Company, the detailed address is
Http://www.stmicroelectronics.com.cn/zh/embedded-software/stm32-standard-peripheral-libraries.html? querycriteria=productid=ln1939

Note that the download is required to register/login account. After downloading, we get the following compressed package,


After decompression, the directory structure of the entire standard library is as follows:

2.1 _htmresc

Picture file, for release_notes.html file display, it's not useful for us 2.2 Project

Complete routines driven by standard peripheral libraries 2.3 Utilities

Dedicated drive 2.4 stm32f10x_stdperiph_lib_um.chm for the STM32 evaluation Board

Help documents used by library functions 2.5 Libraries

The source file of the library function, the file in this directory is what we want to use

ARM is a chip standard company, they are responsible for the chip core architecture design, and like St, Samsung they are based on ARM's core standards provided by the design of their own chips. So, any cortex-m3 chip, the kernel structure is the same, the difference lies in their memory capacity, on-chip peripherals, IO and other modules. In order to enable different chip companies to produce CORTEX-M3 chips can be basically compatible with the software, and chip manufacturers jointly put forward cmsis standards. Of course, the official St library is also designed according to this set of standards. The structure based on the Cmsis application is as follows:

Visible, the cmsis is divided into 3 functional layers:
(1) Internal and external access layer: ARM company provides, define the processor interior register address and function function
(2) Middleware Access layer: ARM provides, defines the API to access the middleware (CPU debugging functions integrated in this)
(3) Device-level peripheral access layer: Also known as the on-chip peripheral access layer, defines the address of the hardware register and the peripheral access function
The cmsis downward is responsible for working directly with the kernel and various peripherals, providing a function interface for real-time operating system user program calls. If there is no cmsis standard, the library functions designed by each company will be different.
For example, the use of STM32 when the first need to initialize, cmsis standard, the function name of the system initialization function must be systeminit,cmsis also on the inside and outside of each piece of the driver file name normalization, operation function normalization, such as Gpio_ The name of the Resetbits function.

The Libraries directory has cmsis and stm32f10x_stdperiph_driver two directories, which are the core files of the function firmware library and the on-chip peripheral operation files, where the Cmsis directory mainly contains the startup files, Stm32f10x_ Stdperiph_driver put is STM32 firmware library source files, Inc store stm32f10x_xxx.h header files, src directory is stored stm32f10x_ XXX.C source code files, each of the. C and. h files correspond to a set of files, each set of files corresponding to an on-chip peripheral, such as RCC, IIC. 3. Introduction to key directory files

Key files are located in the Libraries directory: 3.1 system_stm32f10x.c and System_stm32f10x.h

Core_cm3.c and Core_ under the Libraries\cmsis\cm3\coresupport Cm3.h is the core file of Cmsis, providing an interface to the CROTEX-M3 kernel, which is provided by ARTM company and is applicable to all M3 cores. and the Coresupport directory sibling is also used Devicesupport directory, devicesupport\st\stm32f10x directory, the main storage of some startup files as well as the basic register and interrupt vector definition of the file,

System_stm32f10x.c the corresponding header file System_stm32f10x.h, the function of this set of files is to set the system and bus clock, the most important function is the systeminit () function, this function is called at system startup, The clock used to set the entire system. The comment at the beginning of the System_stm32f10x.c file also clearly indicates the role of this set of files:

This file provides the functions and one global variable to being called from * User application: *-Systemini T (): Setups the system clock (System clock Source, PLL Multiplier * factors, Ahb/apbx prescalers an 
  d Flash Settings). * This function was called at startup just after reset and * before branch to Main program.
  This is made inside * the "startup_stm32f10x_xx.s" file.                                  * *-Systemcoreclock Variable:contains the Core clock (HCLK), it can be used *
  By the user application to setup the SysTick * Timer or configure other parameters. 
  * *-Systemcoreclockupdate (): Updates the variable systemcoreclock and must * be called whenever, the core clock is changed * Duri NG program execution.

The system clock value can be obtained by the variable Systemcoreclock, if the system is a 72M clock, then systemcoreclock=72000000.
Systemcoreclockupdate () allows the systemcoreclock variable to be updated after the system clock is changed. 3.2 stm32f10x.h

Stm32f10x.h This is a key file, in which there are many structures and macro definitions, which are mainly the definition of the system register and the operation of the wrapper.
such as the structure of the package can bus related registers:

typedef struct
{
  __io uint32_t TIR;
  __io uint32_t TDTR;
  __io uint32_t TDLR;
  __io uint32_t tdhr;
} Can_txmailbox_typedef;

The structure that encapsulates the GPIO-related registers:

typedef struct
{
  __io uint32_t CRL;
  __io uint32_t CRH;
  __io uint32_t IDR;
  __io uint32_t ODR;
  __io uint32_t BSRR;
  __io uint32_t BRR;
  __io uint32_t Lckr;
} Gpio_typedef;

Trace the specific type of __io, in the Libraries\cmsis\cm3\coresupport\core_cm3.h macro definition is the keyword volatile

#define     __io    volatile                  /*!< defines ' read/write ' permissions */
3.3 Startup

As the name implies, this is a directory that holds the startup files,

Under the Startup\arm directory

These 8 start of the. s file, is for different capacity of the chip's boot file, for the f103 series of STM32, mainly used in 3 of the boot files:

Startup_stm32f10x_hd.s: Suitable for small-capacity STM32
Startup_stm32f10x_ld.s: For medium-capacity STM32
Startup_stm32f10x_md.s: Suitable for large-capacity STM32

Here the so-called capacity refers to the size of the on-chip flash, the classification rules are:

Small capacity: <= 32K
medium Capacity: >= 64K && <= 128K
Large capacity: >= 256K

The purpose of the startup file is to initialize the stack, interrupt the vector table and the definition of the corresponding interrupt handler function, and boot the program into the main function. Reset_handler is the only interrupt handler that is implemented (the rest of the interrupt handler functions are not cyclic). Reset_handler is called when the system is started,

; Reset handler routine
reset_handler   PROC
        EXPORT  reset_handler   [WEAK]
    IMPORT  __main
    IMPORT  Systeminit
        Ldr     R0, =systeminit
        BLX     R0
        LDR     R0, =__main
        BX      R0
        ENDP

This assembler code first calls the Systeminit system initialization function before booting into the main function and entering the main function.

Note: The contents of the book "STM32 Incomplete Manual" library function version _v3.0.pdf

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.