About alt_main and main

Source: Internet
Author: User

Address: http://blog.csdn.net/henhen2002/archive/2010/03/09/5360162.aspx

 

 

You can use either automatic initialization or custom initialization to enable the Niosi processor. The differences between alt_main and main in the Nios are also described here.

If the nioⅱ processor is automatically initialized, ansi c standard defines the applicationProgramYou can call main () to start execution. 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. The programmer does not need to consider the output device of the system and how to initialize each peripheral. Hal will automatically initialize the entire system.

If you want to avoid automatic initialization. The ansi c standard will provide a variable entry point program that defines the programmer's ability to manually initialize any hardware used. The alt_main () function provides a standalone programming environment with full control over system initialization. For example, use the alt_irq_init (alt_irq_base) function to initialize the interrupt controller. The specific method can be found in the routine hello_alt_main.

In addition, when the main function can be used as the start, the CPU may need a larger RAM (on chip memory) space.
The simplest example (When Ram is set to 4 K ):
Procedure 1:
# Include <stdio. h>
# Include <stdlib. h>
# Include <string. h>
# Include "system. H"
# Include "sys/alt_sys_init.h"
# Include "sys/alt_irq.h"
# Include "priv/alt_file.h"
Int main (void) _ attribute _ (weak, alias ("alt_main ")));
Int alt_main (void)
{
Alt_irq_init (alt_irq_base );
Alt_sys_init ();
Alt_io_redirect (alt_stdout, alt_stdin, alt_stderr );
Int I;
I = 1;
Return 0;
}
-------- No problem. Compilation is successful !!!!!
If it is changed to the following:
Procedure 2
# Include <stdio. h>
# Include <stdlib. h>
# Include <string. h>
# Include "system. H"
# Include "sys/alt_sys_init.h"
# Include "sys/alt_irq.h"
# Include "priv/alt_file.h"
Int main (void)
{
Int I;
I = 1;
Return 0;
}
---------- Compilation fails. The prompt is as follows:
Console prompt ------------------
* *** 00000047] overlaps section. exceptions [00000020-> 00000ab7] overlaps section. exceptions [00000020-> 00000ccb] overlaps section. text [000001c8-> 00000ccb] overlaps section. text [000001c8-> 2017261b]
/Cygdrive/D/myprogram/Altera/kits/nios2_60/bin/nios2-gnutools/H-i686-pc-cygwin/bin /.. /lib/GCC/nios2-elf/3.4.1/http://www.cnblogs.com/http://www.cnblogs.com/nios2-elf/bin/ld section. rodata [00000020-> 00000047] overlaps section. exceptions [00000020-> 000001c7]
/Cygdrive/D/myprogram/Altera/kits/nios2_60/bin/nios2-gnutools/H-i686-pc-cygwin/bin /.. /lib/GCC/nios2-elf/3.4.1/http://www.cnblogs.com/http://www.cnblogs.com/nios2-elf/bin/ld section. rwdata [00000048-> 00000ab7] overlaps section. exceptions [00000020-> 000001c7]

Region onchip_memory_0 is full. This indicates that Ram is insufficient. You can compile the code after modifying the size of the program.
========================================================== ===
 

 

A problem has been solved for several days, originally from main () and alt_main ()

A timer interrupt program cannot run at all:
Volatile alt_u8 led;

Static void handle_timer_interrupts (void * context, alt_u32 ID)
{
Volatile alt_u8 * led_ptr = (volatile alt_u8 *) context;
Iowr_altera_avalon_timer_status (timer_0_base, 0); // clear to flag
Iowr_altera_avalon_pio_data (pio_0_base, ++ * led_ptr); // write to the LED output port
}

Int main (void) _ attribute _ (weak, alias ("alt_main ")));

Int alt_main (void)
{
Alt_irq_register (timer_0_irq, (void *) & LED, handle_timer_interrupts );
Iowr_altera_avalon_timer_control (timer_0_base, 7 );

While (1 ){};
Return 0;
}
Alt_irq_init (alt_irq_base );

It turns out that the interrupt controller is not initialized. You can add alt_irq_init (alt_irq_base) to alt_main. However, if you use main instead of alt_main, you can run it without alt_irq_init interruption.

Later I found out:
You can use either automatic initialization or custom initialization to start 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. The programmer does not need to consider the output device of the system and how to initialize each peripheral. Hal will automatically initialize the entire system.

In addition, the ansi c 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.

If you do not write the alt_main () function, the system first calls alt_main.c (in the <nios2 installation directory> \ Components \ altera_hal \ Hal \ SRC) by default (), this function performs the following operations:

① 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 can control access to the Hal file system, initialize the interrupt controller, and execute 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 suitable devices.
④ Call main ().
⑤ Call exit (). Return of main ()CodeAs the exit () input.

That is to say, 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.
========================================================== ======

Differences between alt_main and main
[21:36:00 | by: caopenly]

What is the difference between alt_main and main in NIOS?
Caopenugly:
Generally, a program has only one entry address, Main. However, for convenience of program initialization or inheritance (C ++), the alias of main is defined, the program entry here can be imagined as alt_main () is a hidden driver entry in the main () function. You can see the difference.
Please refer to the following program
/* Copyright? 2004 Altera Corporation, San Jose, California, USA. All rights
* Reserved. All use of this software and documentation is subject to the license
* Agreement located at the end of this file below.
**************************************** **************************************
* Danger ** warning ** please read before proceeding! ** Warning ** danger *
**************************************** **************************************
*
* "Hello world free-standing" (hello_alt_main) example.
*
* This Program is an example of a "free-standing" C application since it
* CILS "alt_main ()" instead of "Main ()". The example's purpose is
* Revoke strate to the advanced embedded developer the low-level system
* Initialization steps needed for a "hello World" type application.
* Calling "alt_main ()" instead of "Main ()", these system initialization steps
* Are not linked in automatically, and must be provided by the developer,
* Which this example extends strates.
*
* Please refer to file readme.txt for notes on this software example.
*/

# I nclude <stdio. h>
# I nclude <stdlib. h>
# I nclude "system. H"
# I nclude "sys/alt_sys_init.h"
# I nclude "sys/alt_irq.h"
# I nclude "priv/alt_file.h"
/*
* The following statement defines "Main ()" so that when the nio ii ide
* Debugger is set to break at "Main ()", it will break at the appropriate
* Place in this program, which does not contain a called "Main ()".
* Note that the nio ii ide debugger can also be set to break at "alt_main ()",
* In which case the following statement wocould be unneccessary since
* "Alt_main ()" is defined in this program.
*/
Int main (void) _ attribute _ (weak, alias ("alt_main ")));

/*
* _ Do_ctors () is used to call the C ++ constructors.
*
* It is declared weak so that we don't have to force
* Partition sion of _ do_ctors if there are no C ++ Constructors
* To call.
*
* Commended out here because this example is not a C ++ program:
*/
// Extern void _ do_ctors (void) alt_weak;

Int alt_main (void)
{
/*
* Enable interrutps
*
* Turn On interrupts, and initialize the low-level interrupt handler:
*/
Alt_irq_init (alt_irq_base );


/*
* Device-driver initialization
*
* Initialize all the device drivers for every piece of hardware
* In the current system.
*
* Note that the "alt_sys_init ()" is defined in
* Automatically-generated file alt_sys_init.c. This file is
* Generated "on the fly" into your system library project
* At library-build time (as part of the make-process). You can find
* File here:
* <System library name>/release/system_deion/alt_sys_init.c
*
* Because it is generated on-the-fly, your library's copy
* Alt_sys_init () is customized to initialize * only * The devices
* In your * maid * niosii system.
*
* Being machine-generated, alt_sys_init () initializes every single
* Device in your system -- even ones which your application may not
* Use. Indeed, this "Hello World" program only uses one --
* Stdout device. If you wish to save code-space, you may want
* To initialize only the devices you actually need and use.
*
* To do so, you shoshould define your own (e.g. small_sys_init ()).
* Do not edit the file alt_sys_init.c! If you do, your changes will be
* Overwritten the next time you build the library! Please refer to
* "Readme.html" file that accompanies this software example to see how
* Such a customized sys_init routine wowould look for UART initialization only.
*/
Alt_sys_init ();

/*
* I/O Stream initialization.
*
* Initialize the stdout stream, and associate it with
* System library's designated stdout device. Note that the symbols
* Alt_stdout (ETC) are defined in the (generated) file system. h.
*
*/
Alt_io_redirect (alt_stdout, alt_stdin, alt_stderr );

/*
* C ++ constructors.
*
* This particle example is not a C ++ program. But, if it were,
* You wowould need to call all your static constructors as part
* The initialization process. To do so, you wocould un-comment this
* Line:
*
*/
// _ Do_ctors ();

Printf ("hello from NiO II free-standing! \ N ");
/*
* Exit gracefully.
*
* Receivembedded programs run as long as the machine is powered-up.
* Those programs don't need to call exit (). But even this humble
* Little "Hello World" application (which, indeed, does terminate)
* Needs to call exit (). The exit (), amongst extends other
* Things, flushes the I/O buffers -- a singularly-important service
* For this example:
*/
Exit (0); // return code for "success! "If anyone is checking (they aren't ).
}
/*************************************** ***************************************
**
* License Agreement *
**
* Copyright (c) 2004 Altera Corporation, San Jose, California, USA .*
* All Rights Reserved .*
**
* Permission is hereby granted, free of charge, to any person obtaining *
* Copy of this software and associated documentation files (the "software "),*
* To deal in the software without restriction, including without limitation *
* The rights to use, copy, modify, merge, publish, distriense, sublicense ,*
* And/or compare copies of the software, and to permit persons to whom *
* Software is furnished to do so, subject to the following conditions :*
**
* The above copyright notice and this permission notice shall be encoded in *
* All copies or substantial portions of the Software .*
**
* The software is provided "as is", without warranty of any kind, express or *
* Implied, including but not limited to the warranties of merchantability ,*
* Fitness for a participant purpose and noninfringement. In no event shall *
* Authors or copyright holders be liable for any claim, damages or other *
* Liability, whether in an action of contract, tort or otherwise, arising *
* From, out of or in connection with the software or the use or other *
* Dealings in the software .*
**
* This Agreement shall be governed in all respects by the laws of the State *
* Of California and by the laws of the United States of America .*
* Altera does not recommend, suggest or require that this reference design *
* File be used in conjunction or combination with any other product .**
**************************************** **************************************/

 

 

 

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.