微波EDA网,见证研发工程师的成长!
首页 > 研发问答 > 嵌入式设计讨论 > FPGA,CPLD和ASIC > 中断执行后,为何没有返回到主程序main()中?

中断执行后,为何没有返回到主程序main()中?

时间:10-02 整理:3721RD 点击:
--------------------------------------------------------------------------------
中断执行后,为何没有返回到主程序main()中?
我试着编了一个timer中断的小程序,当计到某一数值时可以调用一个中断,但调用中断后,为何没能返回到我的主程序main()继续执行下面的程序呢?请大侠过来看看。
#include "xparameters.h"
#include "stdio.h"
#include "xgpio_l.h"
#include "xtmrctr_l.h"
#include "xintc_l.h"
#include "xgpio.h"
#include "xexception_l.h"
XGpio Gpio;
int intc_command=0;
#define timer_count0x4000000
#defineGPIO_EXAMPLE_DEVICE_IDXPAR_GENERIC_GPIO_DEVICE_ID

/* Timer interrupt service routine */
voidtimeinthandle(void * baseaddr_p)//
{
int csr;
/* Read timer 0 CSR to see if it requested the interrupt */
csr = XTmrCtr_mGetControlStatusReg(XPAR_OPB_TIMER_0_BASEADDR, 0);
if(csr&XTC_CSR_INT_OCCURED_MASK)
{
XGpio_SetDataDirection(&Gpio, 1, 0x0);
XGpio_mSetDataReg(XPAR_GENERIC_GPIO_BASEADDR, 1, \
(0x03 ^ XGpio_mGetDataReg(XPAR_GENERIC_GPIO_BASEADDR, 1)));
}

/* Clear the timer interrupt */
XTmrCtr_mSetControlStatusReg(XPAR_OPB_TIMER_0_BASEADDR, 0, csr);
intc_command = XTRUE;
}
/* Interrupt test routine */
void InterruptTest(void)
{
int tmrvalue;
print("-- Entering InterruptTest() --\r\n");
/* Initialize exception handling */
XExc_Init();
/* Register external interrupt handler */
XExc_RegisterHandler(XEXC_ID_NON_CRITICAL_INT,
(XExceptionHandler)XIntc_DeviceInterruptHandler,
(void *)XPAR_OPB_INTC_0_DEVICE_ID);

/* Start the interrupt controller */
XIntc_mMasterEnable(XPAR_OPB_INTC_0_BASEADDR);
/* Set the gpio for LEDs as output 1 is the channel used*/
XGpio_Initialize(&Gpio, GPIO_EXAMPLE_DEVICE_ID);
XGpio_SetDataDirection(&Gpio, 1, 0x0);
/* Set the number of cycles the timer counts before interrupting */
XTmrCtr_mSetLoadReg(XPAR_OPB_TIMER_0_BASEADDR, 0, timer_count);

/* Reset the timers, and clear interrupts */
XTmrCtr_mSetControlStatusReg(XPAR_OPB_TIMER_0_BASEADDR, 0,
XTC_CSR_INT_OCCURED_MASK | XTC_CSR_LOAD_MASK );

// clear the load bit
XTmrCtr_mSetControlStatusReg(XPAR_OPB_TIMER_0_BASEADDR, 0,0x0 );

/* Enable timerinterrupt requests in the interrupt controller */
XIntc_mEnableIntr(XPAR_OPB_INTC_0_BASEADDR,
XPAR_OPB_TIMER_0_INTERRUPT_MASK);

/* Start the timers */
XTmrCtr_mSetControlStatusReg(XPAR_OPB_TIMER_0_BASEADDR, 0,
XTC_CSR_ENABLE_TMR_MASK | XTC_CSR_ENABLE_INT_MASK |
XTC_CSR_AUTO_RELOAD_MASK | XTC_CSR_DOWN_COUNT_MASK);


/* Enable PPC non-critical interrupts */
XExc_mEnableExceptions(XEXC_NON_CRITICAL);
//wait the interrupt to occur
while(!intc_command)
{
xil_printf("Main Program\n");
}

xil_printf("Main Program Continue\n");

/* Disable PPC non-critical interrupts */
XTmrCtr_mSetControlStatusReg(XPAR_OPB_TIMER_0_BASEADDR, 0,
XTC_CSR_IN T_OCCURED_MASK );
XExc_mDisableExceptions(XEXC_NON_CRITICAL);
print("-- Exiting InterruptTest() --\r\n");
}
/* End user-supplied interrupt test routine */
//====================================================
int main (void)
{
print("-- Entering main() --\r\n");
InterruptTest();
print("-- Exiting main() --\r\n");
return 0;
}

执行到蓝色部分时,会调用中断服务程序voidtimeinthandle(void * baseaddr_p) ,在这个程序程序 中令intc_command=XTRUE,这样就不再去执行中断了,但它却继续执行,而为什么没有退出到while((!intc_command)去执行下面的程序呢?

回复 #1 xtmtd 的帖子
try to declare intc_command as "volatile"

kankan

printf不能在中断中调用,该函数内部采用信号机制,可能要等待信号,在中断中导致Deadlock

问题出在 InterruptTest()中断函中,不能使用可重入的函数。
如printf(...)就是。

明白了,多谢了!

学到了,但是一般的书上都会讲的

Copyright © 2017-2020 微波EDA网 版权所有

网站地图

Top