c - Microblaze 多中断代码不起作用

标签 c interrupt microblaze

我有一个简单的 microblaze 设置,带有两个 GPIO(按钮和开关)。 我想处理两个设备的中断。

这是我当前的非工作代码。

#include<xparameters.h>
#include<xgpio.h>
#include<xintc.h>
#include<xil_exception.h>

static XGpio PushBt;
static XGpio sw;
static XIntc myIntc;
int delay, limit=3000000;

void pb_int_handler(void *baseaddr_p) {
        Xuint32 dsr;

        //DSR contains the INFORMATION of which button was depressed, so we can switch
        //on its value.
        dsr = XGpio_DiscreteRead(&PushBt, 1);
        switch(dsr) {

            case 0x01:
                xil_printf("Up\r\n");
                for(delay=0;delay<limit;delay++){}; // delay cycle
                break;

            case 0x02:
                xil_printf("Left\r\n");
                for(delay=0;delay<limit;delay++){}; // delay cycle
                break;

            case 0x08:
                xil_printf("Right\r\n");
                for(delay=0;delay<limit;delay++){}; // delay cycle
                break;

            case 0x04:
                xil_printf("Down\r\n");
                for(delay=0;delay<limit;delay++){}; // delay cycle
                break;

            default : {}

        }
        //Clear the interrupt both in the Gpio instance as well as the interrupt controller
        XGpio_InterruptClear(&PushBt, 0x3);
        XIntc_Acknowledge(&myIntc,XPAR_AXI_INTC_0_PUSH_IP2INTC_IRPT_INTR);
    }

void sw_int_handler(void *baseaddr_p) {
        Xuint32 dsr;

        //DSR contains the INFORMATION of which button was depressed, so we can switch
        //on its value.
        dsr = XGpio_DiscreteRead(&sw, 1);
        switch(dsr) {

            case 0x00:
                xil_printf("Switches 00 \r\n");
                for(delay=0;delay<limit;delay++){}; // delay cycle
                break;

            case 0x01:
                xil_printf("Switches 01 \r\n");
                for(delay=0;delay<limit;delay++){}; // delay cycle
                break;

            case 0x02:
                xil_printf("Switches 02 \r\n");
                for(delay=0;delay<limit;delay++){}; // delay cycle
                break;

            case 0x03:
                xil_printf("Switches 03 \r\n");
                for(delay=0;delay<limit;delay++){}; // delay cycle
                break;

            default : {}

        }
        //Clear the interrupt both in the Gpio instance as well as the interrupt controller
        XGpio_InterruptClear(&sw, 0x3);
        XIntc_Acknowledge(&myIntc,XPAR_AXI_INTC_0_SW_IP2INTC_IRPT_INTR);
    }

int main(void) {

    xil_printf("Setting up peripherals...\r\n");
    for(delay=0;delay<limit;delay++){}; // delay cycle

    xil_printf("Setting up push buttons...\r\n");
    for(delay=0;delay<limit;delay++){}; // delay cycle

        XGpio_Initialize(&PushBt, XPAR_PUSH_DEVICE_ID);
        XGpio_SetDataDirection(&PushBt,1,1); //set pb as input port
        XGpio_InterruptEnable(&PushBt, XPAR_PUSH_IP2INTC_IRPT_MASK);
        XGpio_InterruptGlobalEnable(&PushBt);

    xil_printf("Setting up switches...\r\n");
    for(delay=0;delay<limit;delay++){}; // delay cycle

        XGpio_Initialize(&sw, XPAR_SW_DEVICE_ID);
        XGpio_SetDataDirection(&sw,1,1); //set sw as input port
        XGpio_InterruptEnable(&sw, XPAR_SW_IP2INTC_IRPT_MASK);
        XGpio_InterruptGlobalEnable(&sw);

    xil_printf("Setting up interrupt controller...\r\n");
    for(delay=0;delay<limit;delay++){}; // delay cycle
    XIntc_Initialize(&myIntc, XPAR_INTC_0_DEVICE_ID);

    xil_printf("Register the interrupt...\r\n");
    for(delay=0;delay<limit;delay++){}; // delay cycle
    XIntc_Connect(&myIntc, XPAR_AXI_INTC_0_PUSH_IP2INTC_IRPT_INTR,
                              (XInterruptHandler)pb_int_handler,
                                   &PushBt);
    XIntc_Connect(&myIntc, XPAR_AXI_INTC_0_SW_IP2INTC_IRPT_INTR,
                                  (XInterruptHandler)sw_int_handler,
                                       &sw);

    xil_printf("Enable individual interrupt...\r\n");
    for(delay=0;delay<limit;delay++){}; // delay cycle
    XIntc_EnableIntr(&myIntc,XPAR_PUSH_IP2INTC_IRPT_MASK | XPAR_SW_IP2INTC_IRPT_MASK);

    xil_printf("Start the interrupt controller...\r\n");
    for(delay=0;delay<limit;delay++){}; // delay cycle
    XIntc_Start(&myIntc, XIN_REAL_MODE);
    XIntc_MasterEnable(&myIntc);
    microblaze_enable_interrupts();

    xil_printf("Setting up exceptions...\r\n");
    for(delay=0;delay<limit;delay++){}; // delay cycle
    Xil_ExceptionInit();

    Xil_ExceptionRegisterHandler(XIL_EXCEPTION_ID_M_AXI_I_EXCEPTION,
            (XExceptionHandler)XIntc_InterruptHandler,
                         &myIntc);
    Xil_ExceptionEnable();

    while(1) {
        xil_printf("Entering loop...\r\n");
        for(delay=0;delay<limit;delay++){}; // delay cycle
    }
}

有什么帮助解释正确的中断代码序列吗?

  • 我设置了处理程序。
  • 我初始化了中断 Controller 。
  • 我连接了处理程序。
  • 我启用了中断屏蔽。
  • 我启动了中断 Controller 。
  • 我启用了主中断 Controller 。

那么我忘记了什么?或者出了什么问题? 谢谢

最佳答案

找到解决方案:更改这些行

XGpio_InterruptEnable(&PushBt, XPAR_PUSH_IP2INTC_IRPT_MASK);

XGpio_InterruptEnable(&PushBt, 0xff);

XGpio_InterruptEnable(&sw, XPAR_SW_IP2INTC_IRPT_MASK);

XGpio_InterruptEnable(&sw, 0xff);

现在代码可以工作了,但我会调查原因:)这是一个中断掩码问题

关于c - Microblaze 多中断代码不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26566252/

相关文章:

c - 程序只有在 Debug模式下才能正确运行

c - 尝试遍历整个内存空间,过早重置地址变量

c - c中的字符串比较

c - 堆栈内存是如何组织的?

java - 我怎样才能对 main 有多个定义?

c - 从文本文件读取后,结果显示不正确

c - 标量对象需要初始化器中的一个元素

c - 为什么页面错误和不可恢复的错误需要不可屏蔽?

video - 中断 10h

c - AVR 中断的变量在 main 中更新