Assert_param

Source: Internet
Author: User

The use of assert_param () can be seen everywhere in the STM32 firmware library and provided routines. If you open the file stm32f10x_conf.h in any routine, you can see that assert_param is actually a macro definition. In the firmware library, it is used to check whether the parameter passed to the function is a valid parameter.

A valid parameter is a parameter that meets the specified range. For example, a parameter can only be a positive integer smaller than 3. If the given parameter is greater than 3, the assert_param () you can report errors when a running program calls this function, so that the programmer can detect errors in time, without having to wait until the program running results are incorrect. This is a common software technology that helps programmers quickly eliminate those obvious errors in the debugging phase. It does sacrifice efficiency in program running (but only in the debugging phase), but it helps you improve efficiency in project development.

When your project is developed successfully, compiled in the release mode, or comment out the macro definition of USE_FULL_ASSERT in the stm32f10x_conf.h file, all assert_param () checks disappear, it does not affect the running efficiency of the final program. When performing the assert_param () check, if a parameter error is found, it will call the assert_failed () function to report the error to the programmer, in any routine main. c has the template of this function, as follows:

Void assert_failed (uint8_t * file, uint32_t line ){

While (1 ){}}

 

 

You can add appropriate statements to output error messages as needed, or modify the function to handle errors.

1. STM32F10xD. LIB is the library file in DEBUG mode. 2. STM32F10xR. LIB is the database file in Release mode. 3. To select the DEBUG and RELEASE modes, you must modify the content of stm32f10x_conf.h. # Define DEBUG indicates the DEBUG mode. If this statement is commented out, it is the RELEASE mode. 4. To select the DEBUG and RELEASE modes, you can also enter the pre-Definition of DEBUG in Options, C/C ++, and Define. In this way, you do not need to modify the content of stm32f10x_conf.h. 5. If you add a database to a project, you do not need to add the source file of the ST database to the project, which is convenient. However, the library selection must correspond to the DEBUG pre-definition.

 

When learning STM32, the assert_param function has a very high probability of appearance. You can search for it online. On the Internet, the assert_param function is generally used for programming and debugging. Next, let me talk about my views on these applications and learn things to understand them. 4. assert_param, an assert_param () function is used in almost every prototype of the assert_param function during Database function analysis. The following uses assert_param (IS_GPIO_ALL_PERIPH (GPIOx )); for example, I understand that the IS_GPIO_ALL_PERIPH (GPIOx) parameter of the function can be found in the prototype # define IS_GPIO_ALL_PERIPH (PERIPH) (* (uint32_t *) & (PERIPH )) = GPIOA_BASE) | \ (* (uint32_t *) & (PERIPH) = GPIOB_BASE) | \ (* (uint32_t *) & (PERIPH )) = GPIOC_BASE) | \ (* (uint32_t *) & (PERIPH) = GPIOD_BASE) | \ (* (uint32_t *) & (PERIPH) = GPIOE_BASE) | \ (* (uint32_t *) & (PERIPH) = GPIOF_BASE) | \ (* (uint32_t *) & (PERIPH )) = GPIOG_BASE) This macro is used to check the PERIPH parameter and determine whether the PERIPH parameter is GPIOX (... g) if one of the base addresses is true, its value is true. Otherwise, it is false. Needless to say, this is a basic logical operation in C language. Of course, this library function is also very interesting. See: first, retrieve the address of PERIPH, that is, find the address, & PERIPH, and then forcibly convert the address to a 32-bit pointer, add (uint32_t *), and then access the content in this address (pointer) through. If you don't want to talk about it, you can see it several times. Next we will return to the assert_param function. Where is this function? The prototype found in stm32f10x_conf.h is as follows: # ifdef USE_FULL_ASSERT

# Define assert_param (expr )? (Void) 0: assert_failed (uint8_t *) _ FILE __, _ LINE _) void assert_failed (uint8_t * file, uint32_t line ); # else # define assert_param (expr) (void) 0) # endif this is a pre-compiled file. If USE_FULL_ASSERT is defined, the following file is executed, we generally have no definition in the program, that is, execute the following statement (void) 0), this statement does not need to think much, there is no definition of USE_FULL_ASSERT is nothing to execute. The preceding statement IS_GPIO_ALL_PERIPH (GPIOx) does not perform any operations. If USE_FULL_ASSERT is defined, when we call this function assert_param and check the correctness of the IS_GPIO_ALL_PERIPH (GPIOx) parameter, we can use a binary operator in the C language to determine if 1 is returned, execute statement (void) 0. If 0 is returned, execute the following function assert_failed (uint8_t *) _ FILE __, _ LINE __), functions are interpreted in library functions to indicate the number of wrong rows and files. Note: __file __, _ LINE _ is the macro definition in the standard library function! Remember void assert_failed (uint8_t * file, uint32_t line). At first, I didn't understand why I added it here. I thought it was the function declaration in the header file. What about function entities? We can find it in main. c in the template of the official file. Void assert_failed (u8 * file, u32 line) {/* User can add his own implementation to report the file name and line number, ex: printf ("Wrong parameters value: file % s on line % d \ r \ n ", file, line )*/

/* Infinite loop */while (1) {}} the English comments also show how to use them. Input parameters to determine the location. The simplest method is serial port printing, the main idea of this function is that when there is a problem with the input parameter, but it cannot be compiled, it can help you check the effectiveness of the parameter, the benefits do not need to say more, just understand it. The following describes how assert_param is included? How can we apply the function declaration or macro definition defined in the header file stm32f10x_conf.h? Many online friends also encounter compilation failures when they first started learning. They finally solve this problem by adding USE_STDPERIPH_DRIVER to the file:

We can search for USE_STDPERIPH_DRIVER throughout the project. We can see from the header file that the standard peripheral file is used. In the stm32f10x. H file, we can find the following situation: # if! Defined USE_STDPERIPH_DRIVER/*** @ brief Comment the line below if you will not use the peripherals drivers. in this case, these drivers will not be supported DED and the application code will be based on direct access to peripherals registers */# define keys # endif # ifdef USE_STDPERIPH_DRIVER # include "stm32f10x_conf.h" # endif can be easily viewed come out, we will not add it here. This header file also sets a switch for us. As long as we remove the first comment, we do not need to add USE _ in the configuration _ STDPERIPH_DRIVER. In the second file, we can know how to include this control switch file. We also understand why we only need to include stm32f10x when writing a program. h can easily include all the file files. We only need to configure it in stm32f10x_conf.h to include the required library files.

From the above, we can see that through the mutual inclusion of header files, we can control the calls of peripherals and debugging files, so that we can clarify our ideas and understand them much better. Of course, some C language problems may not be fully understood in the study. You can search for them online or read more books, and you will soon understand them.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Assert_param

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.