STM32 _I _0 _IO volatile const

Publisher:DazzlingSmileLatest update time:2016-10-05 Source: eefocusKeywords:STM32  _IO  volatile  const Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
What do __I, __O, and __IO mean?
These are macro definitions in the ST library, and are defined as follows:
#define __I volatile const /*!< defines 'read only' permissions */
#define __O volatile /*!< defines 'write only' permissions */
#define __IO volatile /*!< defines 'read / write' permissions */


Obviously, these three macro definitions are used to replace volatile and const, so we must first understand the role of these two keywords:

Volatile
simply means not allowing the compiler to optimize, that is, every time a value is read or modified, it must be read or modified from memory or registers again.

    Generally speaking, volatile is used in the following places:
    1. Variables modified in interrupt service routines for detection by other programs need to be added with volatile;
    2. Flags shared between tasks in a multitasking environment should be added with volatile;
    3. Memory-mapped hardware registers usually also need to be added with volatile instructions, because each read and write to it may have different meanings;

I think this is the most basic issue that distinguishes C programmers from embedded system programmers. Embedded guys often deal with hardware, interrupts, RTOS, etc., all of which require the use of volatile variables. Not understanding the implications of volatile can spell disaster. Assuming the interviewee answered this question correctly (well, doubt it), I'll dig a little deeper to see if this guy really understands the full significance of volatile.
    1) Can a parameter be both const and volatile? Explain why.
    2); Can a pointer be volatile? Explain why.
    3); What's wrong with the following function:
int square(volatile int *ptr)  
{   
    return *ptr * *ptr;  
}  

    1) Yes. An example is a read-only status register. It is volatile because it can be changed unexpectedly. It is const because the program should not try to modify it.
    2); Yes. Although this is not very common. An example is when a service routine modifies a pointer to a buffer.
    3) This code is a bit perverted. The purpose of this code is to return the square of the value pointed to by pointer *ptr. However, since *ptr points to a volatile parameter, the compiler will generate code similar to the following:
int square(volatile int *ptr)  
{   
    int a,b;   
    a = *ptr;  
    b = *ptr;  
    return a * b;   
}  
Since the value of *ptr may be changed unexpectedly, a and b may be different. As a result, this code may not return the square value you expect! The correct code is as follows:
long square(volatile int *ptr)  
{   
    int a;   
    a = *ptr;  
    return a * a;   
}   



const
    read-only variables, that is, variables are stored in read-only static storage. During compilation, if you try to modify a read-only variable, the compiler will prompt an error, which can prevent accidental modification. Both
    const and define
   can be used to define constants, but when const is defined, the type of the constant is defined, so it is more precise (in fact, const defines a read-only variable, not a constant). #define is just a simple text replacement. In addition to defining constants, it can also be used to define some simple functions, which is a bit similar to built-in functions. Constants defined by const and define can be placed in header files. (Note: can be declared multiple times, but can only be defined once)

    const and pointers    
int me;   
const int * p1=&me; //p1 is mutable, *p1 is immutable const modifies *p1, that is, *p1 is immutable
int * const p2=&me; //p2 is immutable, *p2 is mutable const modifies p2, that is, p2 is immutable
const int *const p3=&me; //p3 is immutable, *p3 is also immutable The first const modifies *p3, and the second const modifies p3, both of which are immutable


The usage of volatile and const was introduced earlier. I wonder if you understand it? After understanding it, the following explanation will be easier:
__I: input port. Since it is an input, the value of the register can be modified externally at any time, so it cannot be optimized and must be read from the register again every time. It cannot be written, that is, read-only, otherwise it is not input but output.
__O: Output port, which cannot be optimized. Otherwise, if you output the same value twice in a row, the compiler will think that there is no change and ignore the latter output. If the value is modified externally between the two outputs, it will affect the output.__
IO: Input and output port, same as above


Why add underscores?

The reason is: to avoid naming conflicts
Generally, macro definitions are capitalized, but because there are fewer letters here, underscores are added to distinguish them. This generally avoids naming conflicts, because few people name them this way, and those who name them this way must know what they are used for.
When writing large projects, you will often find that there are always naming conflicts, either global variable conflicts or macro definition conflicts, so we should try to avoid these problems, otherwise we don’t know where the problem is when something goes wrong.


Volatile is generally used in the following three aspects:

1. Interrupt flag

2. Variables shared by multiple threads

3. Status registers 
from ouravr Wildfire M3
Keywords:STM32  _IO  volatile  const Reference address:STM32 _I _0 _IO volatile const

Previous article:FreeModbus ported to STM32 to implement Modbus RTU communication
Next article:STM32 CRC ST Library 3.0.0

Recommended ReadingLatest update time:2024-11-16 07:54

Application design of S3C44B0 in serial port server system
introduction At present, automated monitoring devices such as coal mine gas, negative pressure, motors, and power supply have been widely used in major mines, and have even been networked and operated in individual mines. However, most of the automated monitoring devices still use RS232, RS485 or CAN bus networking, w
[Microcontroller]
Application design of S3C44B0 in serial port server system
What is the direction of general-purpose MCUs in the future?
Source: 21ic Electronic Network   At the 2023 STM32 Summit, look at the future development direction of general MCUs. Since the release of the STM32F1, the first chip of the STM32 family, in 2007, ST has embarked on a legendary journey in the field of general-purpose MCUs. From 1 billion in 2013, 6 billion in 2020
[Embedded]
What is the direction of general-purpose MCUs in the future?
Analysis of I2C driver of linuxok6410 --- user mode driver
3 i2c-dev 3.1 Overview When introducing the I2C subsystem, we mentioned using the i2c-dev.c file to implement our I2C slave device driver in the application. However, it implements a virtual, temporary i2c_client, which is generated when the device file is opened and revoked when the device file is c
[Microcontroller]
How a microcontroller can elegantly read 0-10V sensor signals (2)
The previous article shared a 0-10V sensor signal acquisition circuit. In order to expand the thinking of solving problems in circuit design, this article will introduce you to a 0-10V acquisition circuit. The circuit is shown below: An operational amplifier is used to form a differential amplification circuit, whic
[Microcontroller]
ARM bare metal development: i.MX6U boot method
1. Hardware Platform: Zhengdian Atom i.MX6U Alpha Development Board 2. Startup method selection I.MX6U supports multiple boot modes and boot devices, such as booting from SD/EMMC, NAND Flash, QSPI Flash, etc. Users can choose the appropriate boot device according to actual conditions. A
[Microcontroller]
ARM bare metal development: i.MX6U boot method
Analysis of the characteristics and usage of 51 microcontroller P0 port
1. P0 port characteristics: Port P0 is a three-state bidirectional I/O port. For basic microcontroller systems with internal program memory (such as customized 8051), port P0 can be used as an input/output port to directly connect to external input/output devices; it can also be used as an address/data bus port for sy
[Microcontroller]
Analysis of the characteristics and usage of 51 microcontroller P0 port
STM32 introductory study notes: watchdog experiment (Part 2)
14.4.2 Window watchdog experiment Function: As soon as the program is run, LED1 connected to PB5 lights up for 300ms and then turns off, entering an infinite loop. Wait for the WWDG interrupt to arrive. In the interrupt, feed the dog and flip LED2 on PE5. You can see that LED2 flashes continuously, and LED1 only flash
[Microcontroller]
i.MX 93 Series Enables Intelligent Edge
The success of your product depends on a keen sense of market trends. As an IoT solution provider, you may need a cost-effective application processor that provides high performance, low power consumption, multiple power modes, advanced security, and support for segment-specific connectivity and network
[Embedded]
i.MX 93 Series Enables Intelligent Edge
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号