找回密码
 立即注册
首页 业界区 安全 ZYNQ 以CPU私有定时器为例分析中断的使用 ...

ZYNQ 以CPU私有定时器为例分析中断的使用

卜笑 2025-5-31 23:23:54
ZYNQ是由PS + PL两个部分组成的SoC,PS端有两个双核的ARM A9处理器,说到ARM处理器就不得不说一下其中断处理机制。通过Xilinx的官方手册,我们可以知道ZYNQ芯片当中也有一个ARM的通用中断控制器,用于启用、鉴别和分配不同来源的中断到对应的CPU进行处理。
其中这些中断可以分为:软件中断、CPU私有外设中断、共享外设中断等三大类。其中CPU私有外设中断是每个CPU所独有的,无法跨CPU分配。而软件中断和共享外设中断则可以根据需要通用对中断控制器的配置分配给CPU0/CPU1进行处理。
1.png

 
我们先以私有中断为例学习ZYNQ配置中断的流程。在ZYNQ的PS端每个CPU都有一个私有的定时器可以用于精确的计时,其工作频率总是为CPU主频的一半。可以通过轮询方式获得时间计数,或者将定时器配置工作在中断模式,每隔一个固定的重装载周期触发一次中断处理函数。
2.png

 
接下来我们将介绍如何初始化ARM A9的中断异常处理,启动GIC对于私有定时器的中断使能,并配置私有定时器的重装载值使得CPU每经过1秒就会被中断一次。
首先我们来看官方定时器中断示例中配置中断系统的过程:
1. 首先初始化中断控制器的配置
2. 启动ARM处理器的异常处理
3. 注册中断控制器的异常处理函数
4. 连接定时器中断处理到中断控制器
5. 启动中断和异常处理
  1. static int TimerSetupIntrSystem(XScuGic *IntcInstancePtr,
  2. XScuTimer *TimerInstancePtr, u16 TimerIntrId)
  3. {
  4. int Status;
  5. #ifndef TESTAPP_GEN
  6. XScuGic_Config *IntcConfig;
  7. /*
  8. * Initialize the interrupt controller driver so that it is ready to
  9. * use.
  10. */
  11. IntcConfig = XScuGic_LookupConfig(INTC_DEVICE_ID);
  12. if (NULL == IntcConfig) {
  13. return XST_FAILURE;
  14. }
  15. Status = XScuGic_CfgInitialize(IntcInstancePtr, IntcConfig,
  16. IntcConfig->CpuBaseAddress);
  17. if (Status != XST_SUCCESS) {
  18. return XST_FAILURE;
  19. }
  20. Xil_ExceptionInit();
  21. /*
  22. * Connect the interrupt controller interrupt handler to the hardware
  23. * interrupt handling logic in the processor.
  24. */
  25. Xil_ExceptionRegisterHandler(XIL_EXCEPTION_ID_IRQ_INT,
  26. (Xil_ExceptionHandler)XScuGic_InterruptHandler,
  27. IntcInstancePtr);
  28. #endif
  29. /*
  30. * Connect the device driver handler that will be called when an
  31. * interrupt for the device occurs, the handler defined above performs
  32. * the specific interrupt processing for the device.
  33. */
  34. Status = XScuGic_Connect(IntcInstancePtr, TimerIntrId,
  35. (Xil_ExceptionHandler)TimerIntrHandler,
  36. (void *)TimerInstancePtr);
  37. if (Status != XST_SUCCESS) {
  38. return Status;
  39. }
  40. /*
  41. * Enable the interrupt for the device.
  42. */
  43. XScuGic_Enable(IntcInstancePtr, TimerIntrId);
  44. /*
  45. * Enable the timer interrupts for timer mode.
  46. */
  47. XScuTimer_EnableInterrupt(TimerInstancePtr);
  48. #ifndef TESTAPP_GEN
  49. /*
  50. * Enable interrupts in the Processor.
  51. */
  52. Xil_ExceptionEnable();
  53. #endif
  54. return XST_SUCCESS;
  55. }
复制代码
另一个关键的函数是中断处理函数,当中断产生后,将会触发中断服务函数的调用。因此我们可以再其中编写对应中断的处理逻辑。
  1. static void TimerIntrHandler(void *CallBackRef)
  2. {
  3. XScuTimer *TimerInstancePtr = (XScuTimer *) CallBackRef;
  4. /*
  5. * Check if the timer counter has expired, checking is not necessary
  6. * since that's the reason this function is executed, this just shows
  7. * how the callback reference can be used as a pointer to the instance
  8. * of the timer counter that expired, increment a shared variable so
  9. * the main thread of execution can see the timer expired.
  10. */
  11. if (XScuTimer_IsExpired(TimerInstancePtr)) {
  12. XScuTimer_ClearInterruptStatus(TimerInstancePtr);
  13. TimerExpired++;
  14. if (TimerExpired == 30) {
  15. XScuTimer_DisableAutoReload(TimerInstancePtr);
  16. }
  17. }
  18. }
复制代码
当然为了能够让定时器产生中断,我们需要在主函数中开启定时器的自动重装载,并启动定时器
  1. /*
  2. * Enable Auto reload mode.
  3. */
  4. XScuTimer_EnableAutoReload(TimerInstancePtr);
  5. /*
  6. * Load the timer counter register.
  7. */
  8. XScuTimer_LoadTimer(TimerInstancePtr, TIMER_LOAD_VALUE);
  9. /*
  10. * Start the timer counter and then wait for it
  11. * to timeout a number of times.
  12. */
  13. XScuTimer_Start(TimerInstancePtr);
复制代码
 

来源:程序园用户自行投稿发布,如果侵权,请联系站长删除
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!
您需要登录后才可以回帖 登录 | 立即注册