Introduction
Altcra niosii soft-core processor has the following advantages: fully customizable features, high performance, low product and real-time cost, ease of use, adaptability, and no time-out. The use of the Niosi processor will not be limited to the pre-manufacturing processor technology, but to customize the processor according to user standards, select the appropriate peripherals, memory and interfaces as needed. In addition, you can easily integrate user-specific functions to create a unique competitive advantage in your design.
The software development of nioⅱ processor is based on ansl C. Niosii IDE is the main software development tool for the Niosi series embedded processors. You can complete all software development tasks, such as editing, compiling, downloading, debugging, and Flash programming, in Niosi IDE. SoftwareCodeMemory size, which is related to the size of the storage code, so control and reduceProgramThe amount of code is an important method to reduce system costs. You must first start from the processor startup sequence.
1 niosii processor startup sequence and program entry address
You can use either automatic initialization or user-defined initialization to enable the Niosi processor. The ansi c standard definition application can be executed by calling Main. Before calling Main (), the application assumes that the runtime environment and all service systems are initialized and ready to run. Initialization can be automatically executed by the hardware abstraction layer (HAL) system library. Programmers do not need to consider the system output device and how to initialize each peripheral. Hal will automatically initialize the entire system.
In addition, the ansic standard also provides a variable entry point program to avoid automatic initialization. The ansi c standard also defines that programmers can manually initialize any hardware used. The alt_main () function provides a standalone programming environment with full control over system initialization. The independent programming environment allows programmers to manually write initialization system code.
Hal provides the system initialization code to run in the following startup sequence:
① Enable instruction and data cache;
② Configure the stack;
③ Configure a global pointer;
④ The linker provides _ bss_start and _ bss_end to zero initialize the BSS layer. The _ bss_stan and _ bss_end commands start and end BSS;
⑤ If the current system does not start the downloader, copy. rwdata and. rodata, or the remaining part is in Ram;
6. Call alt_main ().
If you do not call the alt_main () function, the system runs the following steps by default:
① Call alt_ OS _init () to perform initialization specific to any operating system. If Hal is run in the operating system, initialize the alt_fd_list_lock command. It controls access to the Hal file system, initializes the interrupt controller, and executes the interrupt.
② Call the alt_sys_init () function to initialize all driver devices and software components in the system.
③ Reset the C standard I/O channel (stdin, stdout, stderr) to use the appropriate device.
④ Call main ().
⑤ Call exit (). Rnain () returns the code as exit () input.
In the niosii ide project, you can simply define alt_main () to implement the user startup sequence and select the Hal service program. If the application requires an alt_main () entry point program, you can copy the default execution as the start point and customize it as required.
Alt_main () is a function that cannot be returned. Its prototype is voidalt_main ().
The use of a standalone programming environment increases the complexity of niosii programming. The independent programming environment is mainly used to reduce the amount of code, but to use this method, you need to be familiar with the Niosi processor peripherals and driver programming.
You can also use some options to reduce the capacity of the Hal system library in Niosi ide to reduce the amount of code, which is much easier than using a standalone programming environment.
2. method to reduce the amount of code
2.1 enable Compiler Optimization Options
With the "-O3" option in the nios2-elf-gcc compiler, code can be optimized to the maximum extent, including code size and execution speed. Note that compiler optimization may bring unexpected results. In addition, you must use the-O3 option in your project and system library, as shown in L.
2.2 Use small encapsulation driver Library
Hal does not provide two types of driver libraries outside the processor: one is fast execution speed, but a large version of the Code; the other is a small package version. By default, the Hal system uses a large version of code. You can select the reduced device drivers option to select a small encapsulated version to reduce the amount of code, as shown in figure 2.
2.3 use the new C language library
The complete anst C standard library is generally not applicable to embedded systems. Hal provides a series of new ansi c standard libraries which occupy a very small amount of code. You can select the small C library option to select the new ansi c standard library, as shown in 3.
2.4 remove the unused driver Library
When there are peripherals in the niosil system, Niosi ide considers these devices to require drivers, so the corresponding driver libraries are added to the Hal system. If you do not need to use these peripherals in your program, you can also do not load these driver libraries during initialization.
When the user program does not use some devices (such as SPI communication interfaces) in the Niosi system, these devices should be completely removed from the system. In this way, the amount of software code can be reduced, and FPGA resources can be reduced. The most common example is the f1ash storage chip in the system. Generally, you do not need to write the flash chip in your program. Therefore, you do not need to load the Flash Driver library. You can add "-dalt_no_cfi_flash" to the Preprocessor option of the Project attribute ", the Hal system does not add the flash chip driver to the system library.
2.5 use the _ exit () function
By default, the Hal system calls the exit () function to end the process. The exit () function is mainly used to clear the I/o cache in all C language libraries and call all functions in the atexit () function. In fact, the Return Statement in the main () function must be completed before execution.
In the embedded system, user programs do not exit the main () function. Therefore, the exit () code is redundant and can be removed. In a user program, you can use _ exit () instead of exit (). The _ exit () program does not perform any operations and does not need to modify the user program, you only need to add "-dexit = _ exit" to the pre-processor option of the Project property ".
3. Summary
The preceding method optimizes the code volume of a simple helloworld program. The procedure is as follows:
# Include <stdio. h>
Int main ()
{
Printf ("hello from niosii! ");
Return 0;
}
Before any optimization, the code size after compilation is 68 KB: After compilation by the above optimization method, the code size is changed to 7044 bytes. It can be seen that the above optimization method is very effective, and the amount of code is reduced to 1/10, which has important practical value in noⅱ embedded systems.