STM32- The difference between variables vu and u (volatile usage of Laoganma's notes)

    Take u8 and vu8 as examples:

    u8: unsigned char, unsigned 8-bit data 

    vu8:  volatile unsigned char, volatile read and write access to unsigned 8-bit data

    The difference between the two is that there is one more volatile ,The function of volatile is as an instruction keyword to ensure that this instruction will not be omitted due to the optimization of the compiler , and it is required to read the value directly each time. The variable of volatile means that the variable may be changed unexpectedly. In this way, the compiler will No assumptions are made about the value of this variable.

        In general, the use cases of volatile are as follows:

        1. Variables modified in the interrupt service routine for detection by other programs need to be added with volatile;

        2. The flag shared by each task in a multitasking environment needs to be added with volatile;

        3. The memory-mapped hardware register should be specified with volatile, because each reading and writing of it may have different meanings;

        In addition, the integrity of the data should also be considered in the above situations (several interrelated flags are interrupted and rewritten after being read in half), in 1, it can be achieved by turning off interrupts, in 2, task scheduling can be disabled, and 3 can only rely on Good hardware design.



    https://baike.baidu.com/item/volatile/10606957?fr=aladdin (volatile Baidu Encyclopedia details)

    


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325559625&siteId=291194637