ARM: Using structural gymnastics as a register, volatile

Source: Internet
Author: User

Using the structure operator Register:

Register assignment and value, pay attention to the length of the register, and some register value only 8 bits. Also note that you use the volatile modifier register variable.
//Addr.htypedef unsignedints3c24x0_reg32;/*NAND FLASH (see s3c2410 Manual Chapter 6)*/typedefstruct{S3C24X0_REG32 nfconf;    S3C24X0_REG32 Nfcmd;    S3C24X0_REG32 nfaddr;    S3C24X0_REG32 Nfdata;    S3C24X0_REG32 Nfstat; S3C24X0_REG32 NFECC;} S3c2410_nand;//NAND.CStatics3c2410_nand* S3c2410nand = (S3c2410_nand *)0x4e000000;//Thus, S3c2410nand represents the starting address of the Register group. //&s3c2410nand->nfcmd represents the address of the Nfcmd register. //S3c2410nand->nfcmd represents the value of the Nfcmd register. //register assignment and value, pay attention to the length of the register, and some register value only 8 bits. //also note that you use the volatile modifier register variable. /*reading Data*/StaticUnsignedCharS3c2410_read_data (void){    volatileUnsignedChar*p = (volatileUnsignedChar*) &s3c2410nand->Nfdata; return*p;}

embedded programming often use volatile this keyword, on-line check his usage can be attributed to the following two points: [1] One: Tell compiler can not do any optimization such as to send two instructions to an address:int*ip = ...;//Device address*ip =1;//First instruction*ip =2;//second instructionThe above program compiler may be optimized as follows:int*ip = ...;*ip =2; Results The first instruction is missing. If volatile is used, compiler does not allow any optimizations to be made to ensure the program's original intent: volatileint*ip = ...;*ip =1;*ip =2; even if you want to compiler optimization, it will not be two times the value of the statement into one. It can only do other optimizations. This is useful for device driver programmers. 
again such as the following program: [2]  xbyte[2]=0x55;  xbyte[2]=0x56;  xbyte[2]=0x57;  xbyte[2]=0x58; If the above four statements of the external hardware represent different operations, there will be four different actions, then the compiler will not be as pure as the program to optimize the above four statements are considered xbyte[2]=0x58, and ignore the first three statements, and only produce 1 machine code. If Xbyte is modified with Volatileo, the compiler compiles each one at a time and produces the corresponding 4 machine codes.
Two: A variable defined with volatile is changed outside of the program and must be read from memory each time, and cannot be reused in the cache or register.   such as   volatile char a;           A=0; While        (!a) {//do some things;          }          doother ();    If no volatile doother () will not be executed


Here are a few examples of volatile variables: [The use of the process may be changed by hardware or other processes.] ]
1). Hardware registers for parallel devices (e.g., status registers)
2). Non-automatic variables that are accessed in an interrupt service subroutine (non-automatic variables)
variables shared by several tasks in multi-threaded applications


Reference

1. Volatile usage in C http://www.cnblogs.com/chio/archive/2007/11/24/970632.html
2. Volatile related knowledge http://www.100ask.org/bbs/forum.php?mod=viewthread&tid=3902&highlight=volatile

ARM: Using structural gymnastics as a register, volatile

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.