C language embedded: Memory operations

Source: Internet
Author: User

(Notes for Embedded C ++ language 3)

Data Pointer

In the actual debugging of the embedded system, the read/write capability of the absolute address unit content is mostly achieved by using the C language pointer. Direct operation of memory with pointers occurs mostly in

The following situations:

(1) an I/O chip is located in the CPU storage space rather than the I/O space, and the registers correspond to a specific address.

(2) two CPUs communicate with each other through dual-port RAM. The CPU needs to write content in the specific module (mail box) of the dual-port RAM to interrupt the other's CPU.

(3) read the Chinese characters and English characters burned by a specific unit in ROM or flash.

 

Unsigned char * P = (unsigned char *) 0xf000ff00;

* P = 11;

(P ++; P = 0xf000ff01 ;)

Meaning: Write 11 to the absolute address 0xf0000 + 0xff00 (80186 uses the 16-bit segment address and 16-bit offset address.

 

Int * P = (int *) 0xf000ff00;

P ++ -----> P = P + sizeof (INT );

P -- -----> P = p-sizeof (INT );

 

Note: The CPU address is in bytes, And the C language pointer uses the data type length as auto-increment and auto-increment.

 

Function pointer

Understand three questions:

(1) in C language, the number of letters directly corresponds to the address of the instruction code generated by the function in the memory. Therefore, the function name can be directly assigned to the pointer to the function.

(2) calling a function is actually equivalent to "transfer commands + parameter transfer processing + regression position into the stack". Essentially, the core operation is the first of the target code generated by the function.

The IP address is assigned to the PC register of the CPU.

(3) because the essence of function calling is to jump to the code of an address unit for execution, you can "call" a function entity that does not exist at all.

 

As mentioned in the microcomputer principle, after the 186cpu is started, it jumps to the absolute address 0xffff0 (corresponding to the C language pointer 0xf000fff0, 0xf000 is

Segment address, 0xfff0 is the intra-segment offset), for example:

 

Typedef void (* lpfunction) ();/* defines a function pointer type with no parameters and no return type
*/

/* Define a function pointer
, Pointing to the position where the first command is executed after the CPU is started */

Lpfunction lpreset = (lpfunction) 0xf000fff0;

Lpreset ();/* call a function */

The above code implements "soft restart.

 

 

Array vs. Dynamic Application

Any accidental Memory leakage in the embedded system will soon cause the system to crash. Therefore, make sure that malloc and free appear in pairs (Principle: who applies,

Who will release it. Otherwise, the Code Coupling increases ).

Memory application principles:

(1) Use arrays as much as possible. arrays cannot be accessed across borders.

(2) If dynamic application is used, you must determine whether the application is successful after the application, and malloc and free should appear in pairs.

(Value result parameter, value parameter)

 

Keyword const

Const means read-only.

Const int;

Int const;

Const int *;

Int * const;

Int const * a const;

The constants required in the compilation phase can only be defined in the # define macro!

Therefore, the following program in C language is invalid:

Const int size = 10;

Char A [size];/* variables cannot be used in the cheap phase */

 

Keyword volatile prevents Compiler Optimization

The volatile variable may be used in the following situations:

(1) Hardware registers of parallel devices (for example, Status Registers)

(2) Non-automatic variables (global variables) that will be accessed in an interrupt service subroutine)

(3) variables shared by several tasks in multi-threaded applications.

 

Processing of inconsistent CPU character length and memory Bit Width

The length of 80186 characters is 16, while that of NVRAM is 8. In this case, we need to provide an interface for reading and writing bytes and words for NVRAM, as shown below:

 

Typedef usigned char byte;

Typedef usigned int word;

/* Function: Read the bytes in NVRAM.

Parameter: woffset. The read location is offset relative to the NVRAM base address.

Returned byte value: Read

*/

Extern byte readbytenvram (word woffset)

{

Lpbyte lpaddr = (byte *) (NVRAM + woffset * 2);/* Why take 2? */

Return * lpaddr;

}

 

/* Function: Read the characters in NVRAM.

Parameter: woffset. The read location is offset relative to the NVRAM base address.

Return: The read word.

*/

 

Extern word readwordnvram (word woffset)

{

Word wtmp = 0;

Lpbyte lpaddr;

/* Read high byte */

Lpaddr = (byte *) (NVRAM + woffset * 2);/* Why does it take 2? */

Wtmp + = (* lpaddr) * 256;

/* Read low byte */

Lpaddr = (byte *) (NVRAM + (woffset + 1) * 2);/* Why take 2? */

Wtmp + = * lpaddr;

Return wtmp;

}

 

/* Function: Write a byte to NVRAM.

Parameter: woffset, which indicates the offset between the write location and the NVRAM base address.

Bydata, bytes to be written

*/

Extern void writebytenvram (word woffset, byte bydata)

{

......

}

 

/* Function: write a word to NVRAM

Parameter: woffset, which indicates the offset between the write location and the NVRAM base address.

Bydata: the word to be written.

*/

 

Extern void writewordnvram (word woffset, word wdata)

{

.......

}

 

________________

CPU | A3--------A2 |

80186 | A2-------A1 |

| A1-------A0 | NVRAM |

| A0 |

_______ | _________ |

The interconnection between 16-bit 80186 and 8-bit NVRAM can only be A0 to the address line A1, And the A0 of the CPU itself is not connected to the NVRAM. Therefore, the NVRAM address can only

It is an even number of addresses, so every time we move forward in 0x10!

 

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.