Detailed explanation of the use of stm32 standard peripheral library

Publisher:满足的36号Latest update time:2016-06-14 Source: eefocusKeywords:stm32 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
Download the code

     The stm32 standard peripheral library is the peripheral driver for the full range of stm32 chips. With it, we can greatly accelerate the development of stm32.
    First, download the latest stm32 standard peripheral library from the website of st company. The latest version is v3.5.0 when writing this article.

    Unzip the zip file and get the following folders and files:
STM32F10x_StdPeriph_Lib_V3.5.0\
   _htmresc
   Libraries
   Project
   Utilities
   Release_Notes.html
   stm32f10x_stdperiph_lib_um.chm


    Libraries contains the source code of the library, Project contains examples of using various stm32 peripherals and a project template, Utilities is an example of using the st company's evaluation board, and stm32f10x_stdperiph_lib_um.chm teaches us how to use the standard peripheral library.

Since we are going to use 32-bit microcontrollers, it should be a big project, so we should also plan the project directory. Here I recommend the directory structure
    I use. Assuming the project name is template, create a folder called template. There are three fixed folders under this directory: doc, src, and include. doc is used to store project-related data files, src is used to store source code, and each functional module has a folder under src. Include is used to store public header files used by each module. output is used to store compiled output files, which contains two subfolders: obj and list.

template\
   doc
   src
   include
   output\obj
             \listOrganize
      
library code
    Because many codes in the CMSIS folder under Libraries are related to compilers and chips, there are many folders and they are deep, which is not conducive to project maintenance. In fact, a project often uses fixed compilers and chips, so it is necessary to organize the library.
    Create a libstm32 directory under src1
    . Copy the contents under Libraries\STM32F10x_StdPeriph_Driver\ to the libstm32 directory2
    . Create a cmsis folder under the libstm32 directory, and copy core_cm3.c, core_cm3.h under Libraries\CMSIS\CM3\CoreSupport\; stm32f10x.h, system_stm32f10x.c, system_stm32f10x.h under Libraries\CMSIS\CM3\DeviceSupport\ST\STM32F10x\ to the cmsis folder.
    3. Copy the corresponding startup file under Libraries\CMSIS\CM3\DeviceSupport\ST\STM32F10x\startup\arm\ to the cmsis folder according to the chip type you selected. Here I copied startup_stm32f10x_hd.s (the startup file for the large-capacity stm32 chip).

   Here is a brief introduction to the library file:
    The contents under Libraries\STM32F10x_StdPeriph_Driver\ are the driver codes for various peripheral modules of stm32.
    misc.h and misc.c are the driver codes for NVIC and SysTick related to the CM3 core.   
    What is under Libraries\CMSIS? The full name of cmsis is Cortex Microcontroller Software Interface Standard, which is the hardware abstraction layer of the Cortex series processors and can be understood as the software interface of the cortex core.
    core_cm3.c, core_cm3.h,
    their directory name is CoreSupport, indicating that these two files are CM3 core support files, and other chips using the CM3 core can also use them, not necessarily stm32. These two files are used to obtain and set the CM3 core and configure some core registers.
    stm32f10x.h, system_stm32f10x.c, system_stm32f10x.h and startup_stm32f10x_hd.s are in the DeviceSupport directory, which means these files are related to specific chips, that is, the support files of the stm32 chip. Among them, stm32f10x.h is the entry of the standard peripheral library. The code using the standard peripheral library must include this header file. The two files system_stm32f10x.c and system_stm32f10x.h provide functions to initialize the stm32 chip, configure the PLL, system clock and built-in flash interface. startup_stm32f10x_hd.s is the startup file of the large-capacity stm32 chip.
    
Create a project   
    Use keil MDK (I use version 4.12) to create a project in the template directory, and the project name is template. Choose a chip from the stm32 series, it doesn't matter which one (I chose STM32F101RC because my board uses this chip). The next thing to note is that when the pop-up pops up whether to copy the startup code to the project folder, you must select No, because the startup code is already in the standard peripheral library.
    Change the top-level directory name in the project window in UV4 to template, and change the first group name to libstm32. Load all .c and .s files in the libstm32 directory into the libstm32 in the project.
    Create an init directory under src to place the system initialization code. Copy stm32f10x_it.c under Project\STM32F10x_StdPeriph_Template\ to the init folder, and copy stm32f10x_it.h and stm32f10x_conf.h to the include folder.
    stm32f10x_it.c and stm32f10x_it.h are interrupt service program files. stm32f10x_conf.h is the configuration file of the standard peripheral library. For peripherals not needed in the project, you can comment out the included header files. Here I suggest leaving only stm32f10x_gpio.h, stm32f10x_rcc.h, misc.h, and then opening what you need, so that the compilation will be faster. Of course, you can also keep them all.

Use the stm32 standard peripheral library
    In fact, the use of the stm32 standard peripheral library is described in the section How to use the Library in stm32f10x_stdperiph_lib_um.chm. Here I list the steps:
1. According to the selected chip, add the startup code in Libraries\CMSIS\CM3\DeviceSupport\ST\STM32F10x\startup\arm to the project. This step has been done above.
2. In lines 66-73 of stm32f10x.h, remove the corresponding comments according to the selected chip type. Here I remove the comments of the STM32F10X_HD line (large-capacity stm32 chip).
3. Remove the USE_STDPERIPH_DRIVER comment on line 105 to enable the stm32 standard peripheral library.
4. In lines 110-115 of system_stm32f10x.c, remove the corresponding comments according to the selected chip frequency. The default SYSCLK_FREQ_72MHz comment has been removed. If your chip frequency is 72MHz, you don’t need to make any changes. My chip is 36MHz, comment SYSCLK_FREQ_72MHz, and remove the SYSCLK_FREQ_36MHz comment.

The marquee program
   can now use the stm32 standard peripheral library. The following is an illustration of a simple marquee program.

Create main.c in the init directory as the system entry.

Create a bsp directory under src to place the board-level support code, and create led.c and led.h.

The code is as follows:
led.h

 
  1. #ifndef _LED_H_  
  2. #define _LED_H_  
  3.   
  4. #include   
  5.   
  6. #define LED_0     0  
  7. #define LED_1     1  
  8. #define LED_2     2  
  9.   
  10. void led_init(void);  
  11. void led_on(uint32_t n);  
  12. void led_off(uint32_t n);  
  13.   
  14. #endif  
  15.   
  16.   

 

led.c

 
  1. #include "stm32f10x.h"  
  2. #include "led.h"  
  3.   
  4. void led_init(void)  
  5. {  
  6.  GPIO_InitTypeDef GPIO_InitStructure;  
  7.    
  8.  RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);  
  9.  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7 | GPIO_Pin_8;  
  10.  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;  
  11.  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;  
  12.  GPIO_Init(GPIOC, &GPIO_InitStructure);  
  13. }  
  14.   
  15. void led_on(uint32_t n)  
  16. {  
  17.  switch (n)  
  18.  {  
  19.   case LED_0:  
  20.    GPIO_SetBits(GPIOC, GPIO_Pin_6);  
  21.    break;  
  22.   case LED_1:  
  23.    GPIO_SetBits(GPIOC, GPIO_Pin_7);  
  24.    break;  
  25.   case LED_2:  
  26.    GPIO_SetBits(GPIOC, GPIO_Pin_8);  
  27.    break;  
  28.   default:  
  29.    break;  
  30.  }  
  31. }  
  32.   
  33. void led_off(uint32_t n)  
  34. {  
  35.  switch (n)  
  36.  {  
  37.   case LED_0:  
  38.    GPIO_ResetBits(GPIOC, GPIO_Pin_6);  
  39.    break;  
  40.   case LED_1:                              
  41.    GPIO_ResetBits(GPIOC, GPIO_Pin_7);  
  42.    break;  
  43.   case LED_2:  
  44.    GPIO_ResetBits(GPIOC, GPIO_Pin_8);  
  45.    break;  
  46.   default:  
  47.    break;  
  48.  }  
  49. }  

 

The pins in the code need to be modified according to different boards, but the structure is the same.

main.c

 
  1. #include "led.h"  
  2.   
  3. static void delay(uint32_t ms)  
  4. {  
  5.  uint32_t count = 8000;  
  6.  while (ms--)  
  7.  {  
  8.   while (count--);  
  9.   count = 8000;  
  10.  }  
  11. }  
  12.   
  13. int main(void)  
  14. {  
  15.  led_heat();  
  16.    
  17.  for (;;)  
  18.  {  
  19.   led_on(LED_0);  
  20.   led_off(LED_1);  
  21.   led_off(LED_2);  
  22.   delay(1000);  
  23.   
  24.   led_off(LED_0);  
  25.   led_on(LED_1);  
  26.   led_off(LED_2);  
  27.   delay(1000);  
  28.   
  29.   led_off(LED_0);  
  30.   led_off(LED_1);  
  31.   led_on(LED_2);   
  32.   delay(1000);  
  33.  }  
  34. }  
  35.   


 

    Create init and bsp groups in the project and add various codes. In the project Options, add .\include;.\src\libstm32\cmsis;.\src\libstm32\inc;.\src\bsp; to Include Paths in the c/c++ tab. Select . 
    \output\obj in Select Folder for Objects in the Output tab.
    Select .\output\list in Select Folder for Listings in the Listing tab.
    Check use ULINK Cortex Debugger, Run to main() in the Debug tab. You can make different choices in this step according to the simulator you have.

Keywords:stm32 Reference address:Detailed explanation of the use of stm32 standard peripheral library

Previous article:Detailed explanation of ucosii transplantation on stm32 4
Next article:Detailed explanation of ucosii transplantation on stm32 1

Recommended ReadingLatest update time:2024-11-16 17:59

How to use TXE and TC flags when STM32 USART sends data
There are two registers at the transmitting end of USART, one is the USART_DR register (the shaded TDR in the figure below) that can be seen by the program, and the other is the shift register (the shaded Transmit Shift Register in the figure below) that cannot be seen by the program. There are two flags correspondi
[Microcontroller]
How to use TXE and TC flags when STM32 USART sends data
RS485 communication based on Modbus + STM32 + IAR
I have been reading Modbus related content recently and searching the Internet for a long time. I found that most of the content is based on written explanations of the protocol, and there are few successful examples for reference. Here I would like to share with you the example I recently modified and debugged. I wou
[Microcontroller]
RS485 communication based on Modbus + STM32 + IAR
STM32 RTC reading and writing incorrectly
When debugging RTC, I found that the reading and writing were incorrect. Reading and writing are library functions that are called, and the year, month, day, hour, minute, and second are operated separately When reading and writing, it is found that after the year, month and day are written, it takes several secon
[Microcontroller]
STM32-Library Development-Address Mapping
1. stm32 has AHB, APB2, and APB1 buses.    The APB2 peripheral address space is from 0x40010000 to 0x40013FFF. The first address is called the base address. 2. The port configuration register in the reference manual has an address offset of 0x04, so the address of GPIOC_CRH is GPIOC_BASE+0x04   GPIO_TypeDef structur
[Microcontroller]
STM32 SPI slave routine
#include "stm32f10x.h" /* RCC clock configuration*/ void RCC_config(void) {  ErrorStatus HSEStartUpStatus; /* RCC registers are set to default configuration*/ RCC_DeInit(); /* Turn on external high-speed clock*/ RCC_HSEConfig(RCC_HSE_ON); /* Wait for external high-speed clock to stabilize*/ HSEStartUpStatus = RCC_Wa
[Microcontroller]
Three programming download methods for STM32
J-link can debug almost all arm chips, it is a good thing. Here are three ways to download programs for stm32: 1. J-Flash download (requires J-link) 2. MDK configuration download (requires J-link) 3. ISP download (serial port download, you need to use the official serial port download software, those who have used
[Microcontroller]
Three programming download methods for STM32
STM32 fast read and write AT24C16 code simulation I2C
This post is only applicable to chips AT24C16 and below. The reading and writing methods of chips AT24C32 and above are different, so it is not applicable!!! If your code can read and write 24C01/02 normally, you can use it to read 24C16 directly, but it can only read 256 bytes. AT24C16 is different from AT24C01/0
[Microcontroller]
STM32 fast read and write AT24C16 code simulation I2C
Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号