Use the arm Standard C library for Embedded Application Development

Source: Internet
Author: User

Introduction
With the increasing demand for high processing capabilities, real-time multitasking, and ultra-low power consumption, high-end embedded processors have entered the field of view of Chinese developers and gained widespread attention and application in China. Arm is currently the most widely used structure in the embedded field. It takes a leading position in the field of embedded system application because of its advantages such as low cost, low power consumption, and high performance. ADS is an arm integrated development environment launched by arm. It provides support for C and C ++ and is currently the main tool for ARM development. Based on the shortened embedded development cycle and the Development and debugging experience of the arm system, this paper systematically analyzes the application development using the arm standard library.

1 Introduction to the arm standard library
Analyticdb provides ansi c and C ++ standard libraries. This document only discusses the ansi c library, which contains the following parts:

  • Functions defined in iso c library standards;
  • In the semlhosted environment, it is used to implement functions related to the objective of the C library functions;
  • The heiper function used by the C and C ++ compilers.

This library provides devices such as file input and output, using the standard arm semihosted execution environment (semihosting is a mechanism for the arm target machine, it can communicate with hosts running scheduling functions based on input/output requests of application code. This technology allows hosts to provide host resources for target hardware without input and output functions ). Armulator, Angel, and multi-lce support this environment. You can use the development tools provided by ads to develop applications, and then run and debug the program on armulator or the Development Board. If you want to make the application system independent from this environment, you must re-implement the functions dependent on this environment in the C library, and appropriately cut the C library according to the operating environment of the user system.

Using ANSI standard C library for program development not only improves development efficiency but also enhances program portability. To use a library function in a program, you must first create an environment where the library function can be executed. All these work is done by the function in the database. When the application links the functions in the C library, the functions in the C library are completed:

  • Create the execution environment required for the C program (create a stack, if you need to create a heap, initialize some libraries used by the Program );
  • Call the main () function to start executing the C program;
  • Functions Defined by is0 used by the program;
  • Capture running errors and signals. If necessary, terminate execution based on errors or exit the program.

2. Cut down the arm Standard C function library
The standard library contains some functions that depend on the execution environment of arm semihosted. The function names of these functions contain one or two underscores (-) and need to be implemented again. If these functions are defined in a program, the compiler will use the newly defined functions, which are called the reduction of library functions. Generally, you only need to redefine a few functions to use the C library.

The arm application system starts to execute user applications. The application must first be loaded into the execution domain to establish the execution environment of the application. When using the C library, most of the tedious work is done by the C function. After the assembler completes system initialization, it jumps to the population of the C program _ main () (Note: It is not main (). When the main () function is defined in the C program, the compiler generates the _ main code ). The initialization of the C execution environment is completed by the _ main () bootstrap library function. The specific process is as follows:

  • Copy the RO and RW domain code that are not started from the load domain address to the execution domain address;
  • Clears the Zi domain;
  • Jump to _ rt_entry.

Calling _ main () greatly simplifies compilation of the Assembly startup code. The Assembly Code only needs to initialize the system hardware, and there is no need to copy the code from the load domain address to the execution domain address, and Zi domain resetting. Especially when distributed loading is used, the function of _ main () is more obvious. However, _ main () does not create an environment required for running the C library. This task is completed by _ rt_entry (). The main call process is as follows:

  • Call _ rt_stackheap_init () to create a heap and stack;
  • Call _ rt_lib_init () to initialize the referenced library function. If necessary, set the parameters argc and argv of the main () function;
  • Call the main () function to execute the application. You can use the library function;
  • Use the return value of the main () function as the parameter to call exit ().

_ Rt_entry is not a C function. It is the starting point for programming with the arm C library. _ Rt_entry cannot be banquet in C language, because the stack has not been created yet, and the stack is created by _ rt_stackheap_init.

The above briefly introduced the C program using the library function call process, by _ rt-stackheap_init () to establish the C library using the memory model-heap and stack. Because the arm library is created in the semihosted execution environment and its memory model is based on this environment, you must modify this memory model to establish a mechanism. Table 1 lists the functions that need to be implemented again. Once these functions are implemented, the application can run independently from the host environment. The _ User initial_stackheap () must be implemented again, because the default implementation is based on the semihosted execution environment, which is called by _ n_stackheap_init () to create a memory model, the other two functions are not implemented by default.
 
To implement this function, the following conditions must be met:

  • Use a stack space of no more than 96 bytes;
  • Do not pollute other registers except R12 (IP;
  • Store the stack base address, stack base address, stack boundary, and stack boundary in RO ~ R3 is the return parameter;
  • The heap must be 8 bytes aligned.

The implementation routine is as follows:

To improve application development efficiency and portability, we hope to use the standard input/output library function provided by the arm library on the target system.

High-level input and output functions do not depend on the target system environment, but high-level input and output functions must call underlying functions that depend on the target system to achieve the input and output of the application system. You can use the standard input/output library functions provided by the Library by redefining these underlying functions based on the hardware environment of the target system. The following describes how to reduce the printf series output functions provided by the arm standard library.

The most common functions in the standard I/O library are printf series functions, including _ printf (), printf (), _ fprintf (), fprintf (), vprintf (), and vfprintf (). All these functions use the _ file transparently and only depend on the fputc () and ferror () functions. The difference between functions _ printf () and _ fprintf () and printf () is that the first two functions cannot format floating point values. You only need to define your own _ file version, fputc (), and ferror () functions, and define a _ stdout variable with file type, you can use the printf series, fwrite (), fputs (), and puts () functions without any modification.

The specific implementation template is provided below, which can be modified as needed.
# Include <stdio. h>
Struct _ file
{
Int handle;
/* Any code required by the user (if the file is used only for debugging and prinft is used to output information at the standard output end, no file processing code is required )*/
};
Flle_stdout;/* file is defined in stdio. h as: typedef struct _
File file ;*/
Int fputc (int ch, file * f ){
/* User-implemented fpute code. Output a character, which can be implemented as needed */
Return ch;

}
Int ferror (File * f ){
/* User-implemented ferror Code */
Return EOF;
}

Conclusion
This article analyzes the working mechanism of the arm standard library and provides the key steps for program development by reducing the C library. In actual applications, You need to reduce the C library according to the specific hardware environment and application requirements to improve code execution efficiency.

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.