关于alt_main和main

来源:互联网 发布:标书生成软件 编辑:程序博客网 时间:2024/05/24 03:21

NiosII处理器的启动可采用两种方式: 自动初始化和用户自定义初始化,nios中alt_main 和main的区别也在这里。

如果NiosII处理器自动初始化,ANSI C标准定义应用程序可以通过调用main()来开始执行。在调用main()之前,应用程序假定运行环境和所有的服务系统都被初始化并准备运行。初始化可以被硬件抽象层(HAL)系统库自动执行。程序员不需要考虑系统的输出设备以及如何初始化每一个外设,HAL会自动初始化整个系统。

 如果用户要避免自动初始化。ANSI C标准将提供了一个可变的入口点程序,定义程序员能手动初始化任何所用的硬件。alt_main()函数提供了一个独立式的编程环境,能够完全控制系统的初始化。例如用alt_irq_init (ALT_IRQ_BASE)函数初始化中断控制器。具体方法在例程hello_alt_main中能找到。

另外,能使用main函数作为开头开头时cpu可能需要更大的ram( on chip memory )空间。
最简单的例子(ram都设为4k的条件下):
程序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;
}
--------这样就没问题,编译能通过!!!!!
如果改成这样:
程序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;
}
----------编译就不能通过了,提示如下  
Console中的提示——————————————————
**** 00000047] overlaps section .exceptions [00000020 -> 00000ab7] overlaps section .exceptions [00000020 -> 00000ccb] overlaps section .text [000001c8 -> 00000ccb] overlaps section .text [000001c8 -> 0000261b]
/cygdrive/d/MyProgram/altera/kits/nios2_60/bin/nios2-gnutools/H-i686-pc-cygwin/bin/../lib/gcc/nios2-elf/3.4.1/../../../../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/../../../../nios2-elf/bin/ld section .rwdata [00000048 -> 00000ab7] overlaps section .exceptions [00000020 -> 000001c7]

region onchip_memory_0 is full 这就是提示ram不够,根据程序适当改大一点后就能编译了。
===========================================

 

 

一个问题整了几天,原来源于main()和alt_main()
2007-02-02 09:12

一个计时器中断程序始终跑不起来:
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);//清TO标志
    IOWR_ALTERA_AVALON_PIO_DATA(PIO_0_BASE, ++*led_ptr); //写到LED输出口
}

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);

原来是中断控制器没初始化,在alt_main开始加入alt_irq_init (ALT_IRQ_BASE);即搞定了。但是当用main代替alt_main时,不需要alt_irq_init 中断也能运行。

后来才查到:
    NiosII处理器的启动可采用两种方式: 自动初始化和用户自定义初始化。ANSI C标准定义应用程序可以通过调用main()来开始执行。在调用main()之前,应用程序假定运行环境和所有的服务系统都被初始化并准备运行。初始化可以被硬件抽象层(HAL)系统库自动执行。程序员不需要考虑系统的输出设备以及如何初始化每一个外设,HAL会自动初始化整个系统。

   另外,ANSI C标准也提供了一个可变的入口点程序,以避免自动初始化。ANSI C标准还定义程序员能手动初始化任何所用的硬件。alt_main()函数提供了一个独立式的编程环境,能够完全控制系统的初始化。
   
   如果用户不编写alt_main()函数,则系统会到默认先调用alt_main.c(在<nios2安装目录>/components/altera_hal/HAL/src)里面的alt_main(),这个函数进行如下操作

   ① 调用ALT_OS_INIT()来执行任何操作系统所特有的初始化。如果HAL是在操作系统里运行的,那么初始化alt_fd_list_lock命令。它可以控制访问HAL文件系统,初始化中断控制器并执行中断。
   ② 调用alt_sys_init()函数,以初始化系统里所有的驱动装置和软件组成部分。
   ③ 重新设置C标准I/O通道(stdin,stdout,stderr),以使用合适的器件。
   ④ 调用main()。
   ⑤ 调用exit()。main()的返回代码作为exit()的输入。

 也就是说在NiosII IDE工程中,只需简单定义alt_main()就可以实现用户的启动顺序,而且能够选择HAL的服务程序。如果应用程序需要一个alt_main()入口点程序,可以复制默认的执行作为开始点,根据要求来定制它。
==============================================
 
nios中alt_main 和main的区别
[ 2007-3-31 21:36:00 | By: caopengly ]
 
nios中alt_main 和main的区别?
caopengly:
一般程序都只有一个入口地址,main,但是为了程序的初始化方便或利用继承性(c++)中,定义了main的别名,这里的程序入口你可以想象成alt_main()是main()函数的内部隐藏带有驱动的入口。区别看了就知道了。
请看下面的程序
/* 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
 * calls "alt_main()" instead of "main()". The example's purpose is to
 * illustrate to the advanced embedded developer the low-level system
 * initialization steps needed for a "Hello World" type application. By
 * 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 illustrates.
 *
 * 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 Nios 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 Nios II IDE debugger can also be set to break at "alt_main()",
 * in which case the following statement would 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
 * inclusion 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 the
   * 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 the
   * file here:
   *    <system library name>/Release/system_deion/alt_sys_init.c
   *
   * Because it is generated on-the-fly, your library's copy of
   * alt_sys_init() is customized to initialize *only* the devices
   * in your *particuilar* Nios II 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--the
   * 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 should 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 the
   * "readme.html" file that accompanies this software example to see how
   * such a customized sys_init routine would look for UART initialization ONLY.
   */
   alt_sys_init();

  /*
   * I/O Stream Initialization.
   *
   * Initialize the STDOUT stream, and associate it with the
   * 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 particular example is not a C++ program.  But, if it were,
   * you would need to call all your static constructors as part of
   * the initialization process.  To do so, you would un-comment this
   * line:
   *
   */
   //_do_ctors();

  printf("Hello from Nios II Free-Standing!/n");
  /*
   * Exit gracefully.
   *
   * Many embedded 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 many 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 a     *
* 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, distribute, sublicense,    *
* and/or sell copies of the Software, and to permit persons to whom the       *
* Software is furnished to do so, subject to the following conditions:        *
*                                                                             *
* The above copyright notice and this permission notice shall be included 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 PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE *
* 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.          *                                                                 *
******************************************************************************/

 

原创粉丝点击