专栏中心

EEPW首页 > 专栏 > (转)Nios II按键中断调试程序及经验

(转)Nios II按键中断调试程序及经验

发布人:0750long 时间:2009-07-20 来源:工程师 发布文章

 

软件:Quatus II 6.1,Nios II 6.1
硬件:EP1C3T144 FPGA开发板

实验目的:熟悉nios II开发,掌握中断程序调试

系统配置:CPU+OnChipRam(4K)+LED_PIO+BUTTON_PIO

功能描述:按键实现中断,使led亮或灭
led配置成开发板上的led0,led1,led2,led3
按键采用简单按钮B17,B18,19,B20

不给过多注释了(其实注释够多了)
#include <stdlib.h>
#include <io.h>
#include "sys/alt_irq.h"
#include "altera_avalon_pio_regs.h"
#include "system.h"

/* A variable to hold the value of the button pio edge capture register. */
volatile int edge_capture;

#ifdef BUTTON_PIO_BASE
/* 按键中断服务程序*/
static void handle_button_interrupts(void* context, alt_u32 id)
{
    /* Cast context to edge_capture's type. It is important that this be
     * declared volatile to avoid unwanted compiler optimization.
     */
    volatile int* edge_capture_ptr = (volatile int*) context;
    /* Store the value in the Button's edge capture register in *context. */
    *edge_capture_ptr = IORD_ALTERA_AVALON_PIO_EDGE_CAP(BUTTON_PIO_BASE);
    /* Reset the Button's edge capture register. */
    IOWR_ALTERA_AVALON_PIO_EDGE_CAP(BUTTON_PIO_BASE, 0);
}

/* Initialize the button_pio. */
static void init_button_pio()
{
    /* Recast the edge_capture pointer to match the alt_irq_register() function
     * prototype. */
    void* edge_capture_ptr = (void*) &edge_capture;
    /* Enable all 4 button interrupts. */
    IOWR_ALTERA_AVALON_PIO_IRQ_MASK(BUTTON_PIO_BASE, 0xf);
    /* Reset the edge capture register. */
    IOWR_ALTERA_AVALON_PIO_EDGE_CAP(BUTTON_PIO_BASE, 0x0);
    /* Register the interrupt handler. */
    alt_irq_register( BUTTON_PIO_IRQ, edge_capture_ptr,
                      handle_button_interrupts );
}
#endif

int main (void) __attribute__ ((weak, alias ("alt_main")));

int alt_main(void)
{
    alt_irq_init(ALT_IRQ_BASE); //necessary,enable interrupts
    alt_sys_init();  //unnecessary
    alt_io_redirect(ALT_STDOUT,ALT_STDIN,ALT_STDERR);//unnecessary
    init_button_pio();
    while(1)
    {
        switch(edge_capture)
        {
        case 0x01:
            IOWR_ALTERA_AVALON_PIO_DATA(LED_PIO_BASE,0xFF);
            break;
        case 0x02:
            IOWR_ALTERA_AVALON_PIO_DATA(LED_PIO_BASE,0x00);
            break;
        case 0x04:
            IOWR_ALTERA_AVALON_PIO_DATA(LED_PIO_BASE,0xFF);
            break;
        case 0x08:
            IOWR_ALTERA_AVALON_PIO_DATA(LED_PIO_BASE,0x00);
            break;
        default:
            IOWR_ALTERA_AVALON_PIO_DATA(LED_PIO_BASE,0x0A);
            break;
        }
    }
    return 0;
}

1 因为使用的是片上RAM,容量有限,故得重载alt_main(), 否则空间不够
2 系统默认的alt_main()是干了很多活的...比如系统中断,IO设备驱动等,改写的时候得注意,哪些用户程序用到了.比如本例用到中断,故 alt_irq_init(ALT_IRQ_BASE);函数是必须的.开始没有调试成功就是因为没有开总中断

中断的调试过程可以依次看以下:
  
    status:最低位PIE(使能),为0禁止,为1允许中断
  ienable:3232位,每位对应一个外部中断,为1则对应使能
  ipending:表示处理器正在处理的中断
 全局变量:alt_irq.若注册成功的话就会向向量表写入ISR和Context.注册完后返回0则成功,为负则失败

专栏文章内容及配图由作者撰写发布,仅供工程师学习之用,如有侵权或者其他违规问题,请联系本站处理。 联系我们

关键词:

相关推荐

Microsoft 的 Entra ID 漏洞可能是灾难性的

FPGA典型应用领域及解决方案

视频 2009-10-22

了解射频系统中的动态范围和无杂散动态范围

塑造下一波量子创新浪潮的五大力量

智能计算 2025-09-22

C6000 DSP软件开发环境CCS介绍

视频 2009-10-22

嵌入式Linux优化:加快系统起动和应用起动的过程

视频 2009-10-22

Lam Research 在 2025 年 Nvidia 芯片消息发布后上涨是否合理?

EDA/PCB 2025-09-22

Marvell Technology的人工智能雄心面临市场审查

嵌入式系统引导程序移植

视频 2009-10-22

泰凌微端侧AI芯片获头部客户量产

分析Silicon Labs推出 FG23L无线SoC后估值

苹果控制着台积电半数2nm产能,剥夺了竞争对手的尖端技术

AWS在边缘使用Raspberry Pi进行EKS

智能计算 2025-09-22

第二季度WLAN市场同比增长 13.2%

手机与无线通信 2025-09-22

FPGA的DSP应用

视频 2009-10-22
更多 培训课堂
更多 焦点
更多 视频

技术专区