• 中断:ZYNQ


    参考了大量其他讲解,自用。

    整个中断系统架构中,只包含以下三种中断:

     软件生成中断(Software Generated Interrupts,SGI)

     私有外设中断(Private Peripheral Interrupts,PPI)

     共享外设中断(Shared Peripheral Interrupts,SPI)

            每种中断都各自包含特定的中断源,如: PPI 包括全局定时器(global timer)、 私有看门狗定时器(private timer)、私有定时器(private timer)和 PL 中的 FIQ。

            SGI 则是通过写入通用中断控制器(Generic Interrupt Controller,GIC)中的寄存器 生成。SPI 是由 PS 和 PL 中的各种 I/O 和内存控制器生成的。

            对于一般的中低端 CPU 系统来说,上面的结构就足够实现中断的产生和处 理功能了。但是,我们 zynq 的 PS 可不是一般的处理器系统,它是一个结构复杂,性能强劲的双核 Cortex-A9 处理器系统。其包含一级缓存 L1 ,二级缓存 L2, 缓存能够提升 CPU 读写总线数据的性能。但是使用不当,也会造成 CPU 读写数 据实时性的下降,甚至因为缓存一致性的问题,把一些已经由其他外设更新但 是缓存中还是旧值的数据取用,从而产生错误。由于中断的实时性和重要性,中断控制器的相关寄存器的读写是不能因为缓存的存在而被延误的,所以 CPU 并不通过 L2 来访问 GIC 中的寄存器,而是通过一个名为窥探控制单元 Snoop Control Unit(简称 SCU),采用私有总线访问 GIC 的寄存器,以此来提升中断 的处理效率。

    IRQ和FRQ:普通中断和快中断

            通用中断控制器(GIC)是一种集中式资源,是联系中断和 CPU 的桥梁,也是各 CPU 之间中断互联的通道(也带有管理功能),它负责管理、分发从 PS 和 PL 发送到 CPU 的中断。

            当 CPU 接口接受下一个中断时,控制器启用、禁用、 屏蔽中断源并确定其优先级,然后按照编程设定将它们发送给选定的 CPU。

            当然,并不是说所有的中断请求一发出都能立马被 CPU 处理。所有中断源 都有唯一的中断标识号标识以及自己可配置的优先级和目标 CPU 列表。

            图中的 中断分发器(Interrupt Controller Distributor ,ICD)保存着每个 CPU 的挂起中断列表。它会集中所有的中断源,随后根据其优先级(优先级设置值越大,优先级越小),优先将高优先级的中断源分配给指定 CPU 处理,以确保针对多个 CPU 的中断一次只能由一个 CPU 处理。

            但是优先级逻辑在物理上是重复的,也 就是说不同的中断也可以设置为同一优先级。对于这种情况,中断分发器会优先将中断标识号最低的中断源分配给 CPU 处理。

    软中断

            每个 CPU 都可以使用软件生成的中断 (SGI) 来中断自身、另一个 CPU 或两个 CPU。
            有 16 个软件生成的中断(见表 7-2)。通过将 SGI 中断号写入 ICDSGIR 寄存器并指定目标 CPU 来生成 SGI。此写入通过 CPU 自己的专用总线进行。每个 CPU 都有自己的一组 SGI 寄存器,用于生成 16 个软件生成的中断中的一个或多个。通过读取 ICCIAR(中断应答)寄存器或将 1 写入 ICDICPR(中断清除挂起)寄存器的相应位来清除中断。

            所有 SGI 都是边沿触发的。 SGI 的灵敏度类型是固定的且无法更改; ICDICFR0 寄存器是只读的,因为它指定了所有 16 个 SGI 的灵敏度类型。

            每个 CPU 专用的一组 16 个中断源,可以路由到最多 16 个公共中断目标,其中每个目标可以是一个或多个 CPU。

    私有设备中断

            每个 CPU 连接到一组私有的五个外设中断。 PPI 列于表 7-3 中。
            PPI 的敏感度类型是固定的且无法更改;因此,ICDICFR1 寄存器是只读的,因为它指定了所有 5 个 PPI 的灵敏度类型。请注意,来自 PL 的快速中断 (FIQ) 信号和中断 (IRQ) 信号被反转,然后发送到中断控制器。因此,尽管 ICDICFR1 寄存器将它们反映为低电平有效,但它们在 PS-PL 接口处为高电平有效。

    共享设备中断

            来自各个模块的一组大约 60 个中断可以路由到一个或两个 CPU 或 PL。中断控制器管理 CPU 的这些中断的优先级和接收。
            除了 IRQ #61 至 #68 和 #84 至 #91 之外,所有中断敏感度类型均由请求源固定且无法更改。 GIC 必须进行编程以适应这种情况。引导 ROM 不会对这些寄存器进行编程;因此,SDK 设备驱动程序必须对 GIC 进行编程以适应这些敏感度类型。

            对于电平敏感类型的中断,请求源必须提供一种机制,以便中断处理程序在中断被应答后清除中断。此要求适用于任何具有高级别灵敏度类型的 IRQF2P[n](来自 PL)。
            对于上升沿敏感的中断,请求源必须提供足够宽的脉冲供 GIC 捕获。这通常至少是 2 个 CPU_2x3x 周期。此要求适用于任何具有上升沿敏感类型的 IRQF2P[n](来自 PL)。
            ICDICFR2 至 ICDICFR5 寄存器配置所有 SPI 的中断类型。每个中断都有一个 2 位字段,指定敏感类型和处理模型。
            GPIO中断号为 #52,触发类型为高电平。

            而 AXI GPIO 由于是 PL 逻辑构成的,因此 AXI GPIO 中断属于 PL 中断,中断号为#[91:84]和#[68:61],触发类型为高电平或上升沿。 该中断在使用时需要在 ZYNQ 核中使能对应中断端口。

    具体开发情况

    开发步骤:

    ZYNQ-实现GPIO的中断控制_zynq gpio中断_Vuko-wxh的博客-CSDN博客

    第一个环节:

    初始化,注册,设置回调,使能各个步骤。 

    1. 初始化CPU异常功能
    2. 初始化中断控制器
    3. 向CPU注册异常处理回调函数(CPU在中断以后执行的函数)
    4. 向中断控制器中对应的中断ID和中断控制器相连接(并且注册第六步的回调函数)
    5. 设置中断的类型
    6. 设置GPIO中断的回调函数(用户自己设置)
    7. 使能对应引脚的中断
    8. 使能中断控制器
    9. 使能异常处理功能
      1. void GPIO_GIC_INIT(){
      2. int Status;
      3. Xil_ExceptionInit();
      4. Interrupt_Gpio_ConFig = XScuGic_LookupConfig(GPIO_DEVICE_ID); //param文件获取
      5. if (NULL == Interrupt_Gpio_ConFig) {
      6. return XST_FAILURE;
      7. }
      8. Status = XScuGic_CfgInitialize(&Interrupt_Gpio_instance,
      9. Interrupt_Gpio_ConFig,
      10. Interrupt_Gpio_ConFig->CpuBaseAddress);
      11. if (Status != XST_SUCCESS) {
      12. return XST_FAILURE;
      13. }
      14. Xil_ExceptionRegisterHandler(XIL_EXCEPTION_ID_INT,
      15. (Xil_ExceptionHandler) XScuGic_InterruptHandler,
      16. &Interrupt_Gpio_instance); //注册主回调函数
      17. Status = XScuGic_Connect(&Interrupt_Gpio_instance,
      18. Interrupt_ID_Gpio,
      19. (Xil_InterruptHandler )XGpioPs_IntrHandler,
      20. /*这里如果直接传回自己的回调函数也可以,效率会快*/
      21. (void *)&GPIO_Decive
      22. );
      23. if (Status != XST_SUCCESS) {
      24. return XST_FAILURE;
      25. }
      26. XGpioPs_SetIntrType(&GPIO_Decive,
      27. GPIO_BANK_2,
      28. 0xffffffff,
      29. 0x00,
      30. 0x00) ;
      31. XGpioPs_SetCallbackHandler(&GPIO_Decive,
      32. (void *) &GPIO_Decive,
      33. (XGpioPs_Handler )IntrHandler);
      34. XGpioPs_IntrEnable(&GPIO_Decive,
      35. GPIO_BANK_2,
      36. (1<<(Bottom-54))/emio的地址偏移
      37. );
      38. XScuGic_Enable(&Interrupt_Gpio_instance,
      39. Interrupt_ID_Gpio);
      40. Xil_ExceptionEnableMask(XIL_EXCEPTION_IRQ);
      41. //这是根据XIL_EXCEPTION_ID_INT的指定来调整的
      42. return XST_SUCCESS;
      43. }

      第二个环节:设置回调函数和中断清除

      1. 回调函数API
      2. XGpioPs_IntrGetStatusPin();检测该引脚状态,1为触发;
      3. XGpioPs_IntrClearPin()清楚该引脚挂起的中断;
      4. XGpioPs_IntrDisablePin()关闭该引脚中断;
      5. XGpioPs_IntrEnablePin()打开该引脚中断;

      ZYNQ-GPIO-Interrupt_xgpiops_intrclearpin-CSDN博客

     步骤:

            1.调用回调函数的场合,中断已经开始进行,

            XGpioPs_IntrGetStatusPin(const XGpioPs *InstancePtr, u32 Pin)通过检查指定引脚的状态返回True或者False,True则为中断状态。

            2.XGpioPs_IntrClearPin(const XGpioPs *InstancePtr, u32 Pin)来清除该管脚的中断状态,并且使用XGpioPs_IntrDisablePin(const XGpioPs *InstancePtr, u32 Pin)对其禁用,防止多次触发,

            3.执行回调函数。

            4.XGpioPs_IntrEnablePin重新使能端口

    1. static void IntrHandler(void *CallBackRef, u32 Bank, u32 Status)
    2. {
    3. XGpioPs *Gpio = (XGpioPs *)CallBackRef;
    4. u32 DataRead;
    5. int cnt=0;
    6. u32 Interrupt_Status;
    7. // Interrupt_Status = XGpioPs_IntrGetStatusPin(Gpio,Bottom);
    8. // if(Interrupt_Status == 1){
    9. XGpioPs_IntrClearPin(Gpio,Bottom);
    10. XGpioPs_IntrDisablePin(Gpio,Bottom);
    11. DataRead = XGpioPs_ReadPin(Gpio,Bottom);
    12. if(DataRead == 0){
    13. LED_value = ~LED_value;
    14. XGpioPs_WritePin(Gpio,LED0,LED_value);
    15. XGpioPs_WritePin(Gpio,LED1,~LED_value);
    16. Interrupt_CNT++;
    17. printf("Interrupt_CNT = %d\n",Interrupt_CNT);
    18. }
    19. while (cnt < 100){
    20. DataRead = XGpioPs_ReadPin(Gpio,Bottom);
    21. if(DataRead == 1){
    22. cnt++;}
    23. else{
    24. cnt=0;}
    25. usleep(1000);
    26. }
    27. XGpioPs_IntrEnablePin(Gpio,Bottom);
    28. // }
    29. }

    GIC驱动程序组件。

            中断控制器驱动程序对各种处理程序使用优先级的概念:

            优先级是 1 到 31 范围内的整数,默认值 1 是最高优先级中断源。各种源的优先级可以根据需要通过硬件配置动态更改。
            6通用中断控制器支持以下功能: 特定的单独中断 启用/禁用特定的单独中断确认 附加特定的回调函数来处理中断源 如果默认值不可接受,则为中断源分配所需的优先级。
            有关连接驱动程序中断处理程序的详细信息包含在特定于中断处理的源文件 xscugic_intr.c 中。
            该驱动程序旨在独立于 RTOS 和处理器。它仅适用于物理地址。任何对动态内存管理、线程或线程互斥、虚拟内存或高速缓存控制的需求都必须由该驱动程序之上的层来满足。


    中断向量表

            驱动程序使用中断控制器设备的设备 ID 作为配置数据表的直接索引。用户应使用XScuGic_Connect() 和 XScuGic_Disconnect() 函数在运行时使用处理程序和回调填充向量表。
            每个向量表条目对应一个可以产生中断的设备。每个条目都包含一个中断处理程序函数和一个在中断发生时传递给处理程序的参数。当中断处理程序采用基地址以外的参数时,用户必须使用 XScuGic_Connect()。


    嵌套中断处理

    该驱动程序不支持嵌套中断。
            注意:通用中断控制器不是监听控制单元的一部分,如驱动程序名称中的前缀“scu”所示。它是APU中的一个独立模块。

    函数介绍:

    步骤1:初始化CPU异常功能

    Xil_ExceptionInit

            将中断控制器中断处理程序连接到ARM 处理器中的硬件中断处理逻辑。

            我在新的例子中没有看到这个函数了。

            * @brief 该函数是一个通用 API,用于在所有支持的手臂上初始化异常处理程序 *处理器。对于 ARM Cortex-A53、Cortex-R5、* 和 Cortex-A9,异常处理程序正在静态初始化,并且该函数不执行任何操作。
            * 然而,它仍然存在以解决向后兼容性 * 问题(在早期版本的 BSP 中,此 API 用于 * 初始化异常处理程序)。

    1. /****************************************************************************/
    2. /**
    3. * @brief The function is a common API used to initialize exception handlers
    4. * across all supported arm processors. For ARM Cortex-A53, Cortex-R5,
    5. * and Cortex-A9, the exception handlers are being initialized
    6. * statically and this function does not do anything.
    7. * However, it is still present to take care of backward compatibility
    8. * issues (in earlier versions of BSPs, this API was being used to
    9. * initialize exception handlers).
    10. *
    11. * @return None.
    12. *
    13. *****************************************************************************/
    14. void Xil_ExceptionInit(void)
    15. {
    16. return;
    17. }

    步骤2:初始化中断控制器

            和配置其他的没什么不同,不详细列举。

    1. GicConfig = XScuGic_LookupConfig(DeviceId);
    2. if (NULL == GicConfig) {
    3. return XST_FAILURE;
    4. }
    5. Status = XScuGic_CfgInitialize(&InterruptController, GicConfig,
    6. GicConfig->CpuBaseAddress);
    7. if (Status != XST_SUCCESS) {
    8. return XST_FAILURE;
    9. }

    步骤3:向CPU注册异常处理回调函数Xil_ExceptionRegisterHandler()

    void Xil_ExceptionRegisterHandler(u32 Exception_id,
                        Xil_ExceptionHandler Handler,
                        void *Data)

    此处的Exception_id 需要传入xilinx写好的宏定义:XIL_EXCEPTION_ID_INT

            此参数需要根据需求来选择,这是最常选择的。

    Handler则需要传入SCU提供的函数:

            XScuGic_InterruptHandler

    Data需要传入指向 XScuGic 实例的指针。

            例如&Interrupt_Gpio_instance

    1. /******************************************************** **************************/
    2. /** * @brief 为特定异常注册处理程序。当处理器遇到指定的异常时,将调用此处理程序。
    3. * * @param Exception_id 包含异常源的 ID,并且应该
    4. * 在 0 到 XIL_EXCEPTION_ID_LAST 的范围内。
    5. * 有关更多信息,请参阅 xil_exception.h。
    6. * @param Handler 到该异常的处理程序。
    7. * @param Data 是对数据的引用,当处理程序被调用时,该数据将被传递给处理程序。
    8. * * @返回无。
    9. ********************************************************* **************************/
    1. /*****************************************************************************/
    2. /**
    3. * @brief Register a handler for a specific exception. This handler is being
    4. * called when the processor encounters the specified exception.
    5. *
    6. * @param Exception_id contains the ID of the exception source and should
    7. * be in the range of 0 to XIL_EXCEPTION_ID_LAST.
    8. * See xil_exception.h for further information.
    9. * @param Handler to the Handler for that exception.
    10. * @param Data is a reference to Data that will be passed to the
    11. * Handler when it gets called.
    12. *
    13. * @return None.
    14. *
    15. ****************************************************************************/
    16. void Xil_ExceptionRegisterHandler(u32 Exception_id,
    17. Xil_ExceptionHandler Handler,
    18. void *Data)
    19. {
    20. #if (defined (versal) && !defined(ARMR5) && EL3) || defined(ARMR52)
    21. if ( XIL_EXCEPTION_ID_IRQ_INT == Exception_id )
    22. {
    23. /*
    24. * Cortexa72 processor in versal is coupled with GIC-500, and
    25. * GIC-500 supports only FIQ at EL3. Hence, tweaking this API
    26. * to act on IRQ, if Exception_id is pointing to IRQ
    27. */
    28. Exception_id = XIL_EXCEPTION_ID_FIQ_INT;
    29. }
    30. #endif
    31. XExc_VectorTable[Exception_id].Handler = Handler;
    32. XExc_VectorTable[Exception_id].Data = Data;
    33. }

    步骤4:连接GPIO中断信号并注册GPIO回调函数:XScuGic_Connect()

    s32  XScuGic_Connect(XScuGic *InstancePtr, u32 Int_Id,
                    Xil_InterruptHandler Handler, void *CallBackRef)

    InstancePtr – is a pointer to the XScuGic instance.
    Int_Id – contains the ID of the interrupt source and should be in the range of 0 to XSCUGIC_MAX_NUM_INTR_INPUTS - 1
    Handler – to the handler for that interrupt.
    CallBackRef – is the callback reference, usually the instance pointer of the connecting driver.

    Xil_InterruptHandler Handler需要传传入:

            GPIO 中断的中断处理程序XGpioPs_IntrHandler、

    void *CallBackRef同样需要传递实例化指针

            这次是传递给XGpioPs_IntrHandler,所以需要传递的是指向GPIO实例的指针,而不是是指向 XScuGic 实例的指针。

    1. /******************************************************** ****************************/
    2. /** * * 在中断源的 Int_Id 和关联的处理程序之间建立连接是在识别到中断时运行。
    3. * 在此调用中作为 Callbackref 提供的参数在调用处理程序时用作处理程序的参数。
    4. * * @param InstancePtr 是指向 XScuGic 实例的指针。
    5. * @param Int_Id 包含中断源的 ID,并且应在 0 到 XSCUGIC_MAX_NUM_INTR_INPUTS - 1 的范围内
    6. * @param Handler 指向该中断的处理程序。
    7. * @param CallBackRef 是回调引用,通常是连接驱动程序的实例指针。
    8. * * @return * * - XST_SUCCESS 如果处理程序连接正确。
    9. * * @note *
    10. * 警告:作为参数提供的处理程序将覆盖之前连接的任何处理程序。
    11. ********************************************************* ****************************/
    1. /*****************************************************************************/
    2. /**
    3. *
    4. * Makes the connection between the Int_Id of the interrupt source and the
    5. * associated handler that is to run when the interrupt is recognized. The
    6. * argument provided in this call as the Callbackref is used as the argument
    7. * for the handler when it is called.
    8. *
    9. * @param InstancePtr is a pointer to the XScuGic instance.
    10. * @param Int_Id contains the ID of the interrupt source and should be
    11. * in the range of 0 to XSCUGIC_MAX_NUM_INTR_INPUTS - 1
    12. * @param Handler to the handler for that interrupt.
    13. * @param CallBackRef is the callback reference, usually the instance
    14. * pointer of the connecting driver.
    15. *
    16. * @return
    17. *
    18. * - XST_SUCCESS if the handler was connected correctly.
    19. *
    20. * @note
    21. *
    22. * WARNING: The handler provided as an argument will overwrite any handler
    23. * that was previously connected.
    24. *
    25. ****************************************************************************/
    26. s32 XScuGic_Connect(XScuGic *InstancePtr, u32 Int_Id,
    27. Xil_InterruptHandler Handler, void *CallBackRef)
    28. {
    29. /*
    30. * Assert the arguments
    31. */
    32. Xil_AssertNonvoid(InstancePtr != NULL);
    33. Xil_AssertNonvoid(Int_Id < XSCUGIC_MAX_NUM_INTR_INPUTS);
    34. Xil_AssertNonvoid(Handler != NULL);
    35. Xil_AssertNonvoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
    36. /*
    37. * The Int_Id is used as an index into the table to select the proper
    38. * handler
    39. */
    40. InstancePtr->Config->HandlerTable[Int_Id].Handler = (Xil_InterruptHandler)Handler;
    41. InstancePtr->Config->HandlerTable[Int_Id].CallBackRef = CallBackRef;
    42. return XST_SUCCESS;
    43. }

    步骤5:设置GPIO中断类型XGpioPs_SetIntrType()

    void XGpioPs_SetIntrType(const XGpioPs *InstancePtr, u8 Bank, u32 IntrType,
                  u32 IntrPolarity, u32 IntrOnAny)

    或:

    void XGpioPs_SetIntrTypePin(const XGpioPs *InstancePtr, u32 Pin, u8 IrqType)

            配置单个Pin,这两个函数有许多不同之处 

    zynq开发系列4:MIO按键中断控制LED_xgpios intr example_只是有点小怂的博客-CSDN博客

     先介绍后一个:void XGpioPs_SetIntrTypePin(const XGpioPs *InstancePtr, u32 Pin, u8 IrqType)
    1. /******************************************************** **************************/
    2. /** * * 该函数用于设置单个GPIO引脚的IRQ类型。
    3. * * @param InstancePtr 是指向 XGpioPs 实例的指针。
    4. * @param Pin 是要设置IRQ 类型的引脚号。
    5. * Zynq 中的有效值为 0-117,Zynq Ultrascale+ MP 中的有效值为 0-173。
    6. * @param IrqType 是 GPIO 引脚的 IRQ 类型。使用xgpiops.h中定义的XGPIOPS_IRQ_TYPE_* *来指定IRQ类型。
    7. * * @返回无。
    8. * * @note 无。
    9. ********************************************************* ****************************/

    1. #define XGPIOPS_IRQ_TYPE_EDGE_RISING 0x00U /**< 上升沿中断 */
    2. #define XGPIOPS_IRQ_TYPE_EDGE_FALLING 0x01U /**< 下降沿中断 */
    3. #define XGPIOPS_IRQ_TYPE_EDGE_BOTH 0x02U /**< 双沿中断 */
    4. #define XGPIOPS_IRQ_TYPE_LEVEL_HIGH 0x03U /**< 中断高电平 */
    5. #define XGPIOPS_IRQ_TYPE_LEVEL_LOW 0x04U /**< 低电平中断 */
    1. /****************************************************************************/
    2. /**
    3. *
    4. * This function is used for setting the IRQ Type of a single GPIO pin.
    5. *
    6. * @param InstancePtr is a pointer to an XGpioPs instance.
    7. * @param Pin is the pin number whose IRQ type is to be set.
    8. * Valid values are 0-117 in Zynq and 0-173 in Zynq Ultrascale+ MP.
    9. * @param IrqType is the IRQ type for GPIO Pin. Use XGPIOPS_IRQ_TYPE_*
    10. * defined in xgpiops.h to specify the IRQ type.
    11. *
    12. * @return None.
    13. *
    14. * @note None.
    15. *
    16. *****************************************************************************/
    17. void XGpioPs_SetIntrTypePin(const XGpioPs *InstancePtr, u32 Pin, u8 IrqType)
    18. {
    19. u32 IntrTypeReg;
    20. u32 IntrPolReg;
    21. u32 IntrOnAnyReg;
    22. u8 Bank;
    23. u8 PinNumber;
    24. Xil_AssertVoid(InstancePtr != NULL);
    25. Xil_AssertVoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
    26. Xil_AssertVoid(Pin < InstancePtr->MaxPinNum);
    27. Xil_AssertVoid(IrqType <= XGPIOPS_IRQ_TYPE_LEVEL_LOW);
    28. /* Get the Bank number and Pin number within the bank. */
    29. #ifdef versal
    30. XGpioPs_GetBankPin(InstancePtr,(u8)Pin, &Bank, &PinNumber);
    31. #else
    32. XGpioPs_GetBankPin((u8)Pin, &Bank, &PinNumber);
    33. #endif
    34. IntrTypeReg = XGpioPs_ReadReg(InstancePtr->GpioConfig.BaseAddr,
    35. ((u32)(Bank) * XGPIOPS_REG_MASK_OFFSET) +
    36. XGPIOPS_INTTYPE_OFFSET);
    37. IntrPolReg = XGpioPs_ReadReg(InstancePtr->GpioConfig.BaseAddr,
    38. ((u32)(Bank) * XGPIOPS_REG_MASK_OFFSET) +
    39. XGPIOPS_INTPOL_OFFSET);
    40. IntrOnAnyReg = XGpioPs_ReadReg(InstancePtr->GpioConfig.BaseAddr,
    41. ((u32)(Bank) * XGPIOPS_REG_MASK_OFFSET) +
    42. XGPIOPS_INTANY_OFFSET);
    43. switch (IrqType) {
    44. case XGPIOPS_IRQ_TYPE_EDGE_RISING:
    45. IntrTypeReg |= ((u32)1 << (u32)PinNumber);
    46. IntrPolReg |= ((u32)1 << (u32)PinNumber);
    47. IntrOnAnyReg &= ~((u32)1 << (u32)PinNumber);
    48. break;
    49. case XGPIOPS_IRQ_TYPE_EDGE_FALLING:
    50. IntrTypeReg |= ((u32)1 << (u32)PinNumber);
    51. IntrPolReg &= ~((u32)1 << (u32)PinNumber);
    52. IntrOnAnyReg &= ~((u32)1 << (u32)PinNumber);
    53. break;
    54. case XGPIOPS_IRQ_TYPE_EDGE_BOTH:
    55. IntrTypeReg |= ((u32)1 << (u32)PinNumber);
    56. IntrOnAnyReg |= ((u32)1 << (u32)PinNumber);
    57. break;
    58. case XGPIOPS_IRQ_TYPE_LEVEL_HIGH:
    59. IntrTypeReg &= ~((u32)1 << (u32)PinNumber);
    60. IntrPolReg |= ((u32)1 << (u32)PinNumber);
    61. break;
    62. case XGPIOPS_IRQ_TYPE_LEVEL_LOW:
    63. IntrTypeReg &= ~((u32)1 << (u32)PinNumber);
    64. IntrPolReg &= ~((u32)1 << (u32)PinNumber);
    65. break;
    66. default:
    67. /**< Default statement is added for MISRA C compliance. */
    68. break;
    69. }
    70. XGpioPs_WriteReg(InstancePtr->GpioConfig.BaseAddr,
    71. ((u32)(Bank) * XGPIOPS_REG_MASK_OFFSET) +
    72. XGPIOPS_INTTYPE_OFFSET, IntrTypeReg);
    73. XGpioPs_WriteReg(InstancePtr->GpioConfig.BaseAddr,
    74. ((u32)(Bank) * XGPIOPS_REG_MASK_OFFSET) +
    75. XGPIOPS_INTPOL_OFFSET, IntrPolReg);
    76. XGpioPs_WriteReg(InstancePtr->GpioConfig.BaseAddr,
    77. ((u32)(Bank) * XGPIOPS_REG_MASK_OFFSET) +
    78. XGPIOPS_INTANY_OFFSET, IntrOnAnyReg);
    79. }
    前一个:void XGpioPs_SetIntrType(const XGpioPs *InstancePtr, u8 Bank, u32 IntrType,u32 IntrPolarity, u32 IntrOnAny)
    1. /******************************************************** **************************/
    2. /** * * 该函数用于设置中断类型、中断极性和 * Interrupt On Any对于指定的 GPIO Bank 引脚。
    3. * * @param InstancePtr 是指向 XGpioPs 实例的指针。
    4. * @param Bank 是要操作的 GPIO 的 Bank 号。
    5. * Zynq 中的有效值为 0-3,Zynq Ultrascale+ MP 中的有效值为 0-5。
    6. * @param IntrType 是中断类型的 32 位掩码。
    7. * 0 表示电平敏感,1 表示边缘敏感。
    8. * @param IntrPolarity 是中断极性的 32 位掩码。
    9. * 0 表示低电平有效或下降沿,1 表示高电平有效或 * 上升沿。
    10. * @param IntrOnAny 是边沿触发中断的中断触发的 32 位掩码。
    11. * 0 表示使用配置的中断极性在单边沿触发,1 表示在双边沿触发。
    12. * * @返回无。
    13. * * @note 该函数用于设置指定bank中所有引脚的中断相关属性。引脚之前的 * 状态不会保持。
    14. * 要更改单个 GPIO 引脚的中断属性,请使用函数 XGpioPs_SetPinIntrType()。
    15. ********************************************************* ****************************/
    1. /****************************************************************************/
    2. /**
    3. *
    4. * This function is used for setting the Interrupt Type, Interrupt Polarity and
    5. * Interrupt On Any for the specified GPIO Bank pins.
    6. *
    7. * @param InstancePtr is a pointer to an XGpioPs instance.
    8. * @param Bank is the bank number of the GPIO to operate on.
    9. * Valid values are 0-3 in Zynq and 0-5 in Zynq Ultrascale+ MP.
    10. * @param IntrType is the 32 bit mask of the interrupt type.
    11. * 0 means Level Sensitive and 1 means Edge Sensitive.
    12. * @param IntrPolarity is the 32 bit mask of the interrupt polarity.
    13. * 0 means Active Low or Falling Edge and 1 means Active High or
    14. * Rising Edge.
    15. * @param IntrOnAny is the 32 bit mask of the interrupt trigger for
    16. * edge triggered interrupts. 0 means trigger on single edge using
    17. * the configured interrupt polarity and 1 means trigger on both
    18. * edges.
    19. *
    20. * @return None.
    21. *
    22. * @note This function is used for setting the interrupt related
    23. * properties of all the pins in the specified bank. The previous
    24. * state of the pins is not maintained.
    25. * To change the Interrupt properties of a single GPIO pin, use the
    26. * function XGpioPs_SetPinIntrType().
    27. *
    28. *****************************************************************************/
    29. void XGpioPs_SetIntrType(const XGpioPs *InstancePtr, u8 Bank, u32 IntrType,
    30. u32 IntrPolarity, u32 IntrOnAny)
    31. {
    32. Xil_AssertVoid(InstancePtr != NULL);
    33. Xil_AssertVoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
    34. Xil_AssertVoid(Bank < InstancePtr->MaxBanks);
    35. #ifdef versal
    36. if(InstancePtr->PmcGpio == (u32)TRUE) {
    37. Xil_AssertVoid(Bank != XGPIOPS_TWO);
    38. } else {
    39. Xil_AssertVoid((Bank !=XGPIOPS_ONE) && (Bank !=XGPIOPS_TWO));
    40. }
    41. #endif
    42. XGpioPs_WriteReg(InstancePtr->GpioConfig.BaseAddr,
    43. ((u32)(Bank) * XGPIOPS_REG_MASK_OFFSET) +
    44. XGPIOPS_INTTYPE_OFFSET, IntrType);
    45. XGpioPs_WriteReg(InstancePtr->GpioConfig.BaseAddr,
    46. ((u32)(Bank) * XGPIOPS_REG_MASK_OFFSET) +
    47. XGPIOPS_INTPOL_OFFSET, IntrPolarity);
    48. XGpioPs_WriteReg(InstancePtr->GpioConfig.BaseAddr,
    49. ((u32)(Bank) * XGPIOPS_REG_MASK_OFFSET) +
    50. XGPIOPS_INTANY_OFFSET, IntrOnAny);
    51. }

    步骤6:设置GPIO的回调函数XGpioPs_SetCallbackHandler()

    void XGpioPs_SetCallbackHandler(XGpioPs *InstancePtr, void *CallBackRef,

                     XGpioPs_Handler FuncPointer)

    这里的void *CallBackRef仍是需要传递GPIO的实例指针

            交由回调函数XGpioPs_Handler FuncPointer进行使用。

    直接回调:

            如果频繁发生中断,那么函数套的太多了。故可以在XScuGic_Connect()直接设置用户的中断回调函数IntrHandler()提高效率,减少一层回调。

    1. /******************************************************** **************************/
    2. /** * * 该函数设置状态回调函数。当中断发生时,回调函数由 XGpioPs_IntrHandler 调用。
    3. * * @param InstancePtr 是指向 XGpioPs 实例的指针。
    4. * @param CallBackRef 是调用回调函数时传回的上层回调引用。
    5. * @param FuncPointer 是回调函数的指针。
    6. * * * @return 无。
    7. *
    8. * @note 处理程序是在中断上下文中调用的,因此它应该快速完成其工作,
    9. * 并将可能耗时的工作排队到任务级线程。
    10. ********************************************************* *****************************/
    1. /*****************************************************************************/
    2. /**
    3. *
    4. * This function sets the status callback function. The callback function is
    5. * called by the XGpioPs_IntrHandler when an interrupt occurs.
    6. *
    7. * @param InstancePtr is a pointer to the XGpioPs instance.
    8. * @param CallBackRef is the upper layer callback reference passed back
    9. * when the callback function is invoked.
    10. * @param FuncPointer is the pointer to the callback function.
    11. *
    12. *
    13. * @return None.
    14. *
    15. * @note The handler is called within interrupt context, so it should do
    16. * its work quickly and queue potentially time-consuming work to a
    17. * task-level thread.
    18. *
    19. ******************************************************************************/
    20. void XGpioPs_SetCallbackHandler(XGpioPs *InstancePtr, void *CallBackRef,
    21. XGpioPs_Handler FuncPointer)
    22. {
    23. Xil_AssertVoid(InstancePtr != NULL);
    24. Xil_AssertVoid(FuncPointer != NULL);
    25. Xil_AssertVoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
    26. InstancePtr->Handler = FuncPointer;
    27. InstancePtr->CallBackRef = CallBackRef;
    28. }

    步骤7:使能对应pin的中断XGpioPs_IntrEnable()和XGpioPs_IntrEnablePin()

    void XGpioPs_IntrEnable(const XGpioPs *InstancePtr, u8 Bank, u32 Mask)

    此处的u32 Mask

            需要传入特定的bit位。

    比如:输入引脚是EMIO,则:

           1<< (Input_Bank_Pin-Base_Number)

            输入引脚是MIO时:

            1 << Input_Bank_Pin

    XGpioPs_IntrEnable(Gpio, GPIO_BANK, (1 << Input_Bank_Pin));

    也可以使用void XGpioPs_IntrEnablePin(const XGpioPs *InstancePtr, u32 Pin)来指定使能。

    XGpioPs_IntrEnable
    1. /******************************************************** **************************/
    2. /** * * 该函数启用指定* Bank 中指定引脚的中断。
    3. * * @param InstancePtr 是指向 XGpioPs 实例的指针。
    4. * @param Bank 是要操作的 GPIO 的 Bank 号。
    5. * Zynq 中的有效值为 0-3,Zynq Ultrascale+ MP 中的有效值为 0-5。
    6. * @param Mask 是要启用中断的引脚的位掩码。位位置 1 将被启用。位位置
    7. * 为 0 将保留先前的设置。
    8. * * @返回无。
    9. * * @note 无。
    10. ********************************************************* **************************/
    1. /****************************************************************************/
    2. /**
    3. *
    4. * This function enables the interrupts for the specified pins in the specified
    5. * bank.
    6. *
    7. * @param InstancePtr is a pointer to the XGpioPs instance.
    8. * @param Bank is the bank number of the GPIO to operate on.
    9. * Valid values are 0-3 in Zynq and 0-5 in Zynq Ultrascale+ MP.
    10. * @param Mask is the bit mask of the pins for which interrupts are to
    11. * be enabled. Bit positions of 1 will be enabled. Bit positions
    12. * of 0 will keep the previous setting.
    13. *
    14. * @return None.
    15. *
    16. * @note None.
    17. *
    18. *****************************************************************************/
    19. void XGpioPs_IntrEnable(const XGpioPs *InstancePtr, u8 Bank, u32 Mask)
    20. {
    21. Xil_AssertVoid(InstancePtr != NULL);
    22. Xil_AssertVoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
    23. Xil_AssertVoid(Bank < InstancePtr->MaxBanks);
    24. #ifdef versal
    25. if(InstancePtr->PmcGpio == (u32)TRUE) {
    26. Xil_AssertVoid(Bank != XGPIOPS_TWO);
    27. } else {
    28. Xil_AssertVoid((Bank !=XGPIOPS_ONE) && (Bank !=XGPIOPS_TWO));
    29. }
    30. #endif
    31. XGpioPs_WriteReg(InstancePtr->GpioConfig.BaseAddr,
    32. ((u32)(Bank) * XGPIOPS_REG_MASK_OFFSET) +
    33. XGPIOPS_INTEN_OFFSET, Mask);
    34. }
    XGpioPs_IntrEnablePin
    1. /******************************************************** **************************/
    2. /** * * 该函数使能指定引脚的中断。
    3. * * @param InstancePtr 是指向 XGpioPs 实例的指针。
    4. * @param Pin 是要启用中断的引脚号。
    5. * Zynq 中的有效值为 0-117,Zynq Ultrascale+ MP 中的有效值为 0-173。
    6. * * @返回无。
    7. * * @note 无。
    8. ********************************************************* **************************/
    1. /****************************************************************************/
    2. /**
    3. *
    4. * This function enables the interrupt for the specified pin.
    5. *
    6. * @param InstancePtr is a pointer to the XGpioPs instance.
    7. * @param Pin is the pin number for which the interrupt is to be enabled.
    8. * Valid values are 0-117 in Zynq and 0-173 in Zynq Ultrascale+ MP.
    9. *
    10. * @return None.
    11. *
    12. * @note None.
    13. *
    14. *****************************************************************************/
    15. void XGpioPs_IntrEnablePin(const XGpioPs *InstancePtr, u32 Pin)
    16. {
    17. u8 Bank;
    18. u8 PinNumber;
    19. u32 IntrReg;
    20. Xil_AssertVoid(InstancePtr != NULL);
    21. Xil_AssertVoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
    22. Xil_AssertVoid(Pin < InstancePtr->MaxPinNum);
    23. /* Get the Bank number and Pin number within the bank. */
    24. #ifdef versal
    25. XGpioPs_GetBankPin(InstancePtr,(u8)Pin, &Bank, &PinNumber);
    26. #else
    27. XGpioPs_GetBankPin((u8)Pin, &Bank, &PinNumber);
    28. #endif
    29. IntrReg = ((u32)1 << (u32)PinNumber);
    30. XGpioPs_WriteReg(InstancePtr->GpioConfig.BaseAddr,
    31. ((u32)(Bank) * XGPIOPS_REG_MASK_OFFSET) +
    32. XGPIOPS_INTEN_OFFSET, IntrReg);
    33. }

    步骤8:使能中断控制器中的gpio中断XScuGic_Enable()

    void XScuGic_Enable(XScuGic *InstancePtr, u32 Int_Id)

            Int_Id需要和Exception_id对应,

            比如:

    Exception_id传入了:

            #define XIL_EXCEPTION_ID_INT    XIL_EXCEPTION_ID_IRQ_INT

    Int_Id就需要传入:XIL_EXCEPTION_IRQ

    Exception_id传入了:

            #define XIL_EXCEPTION_ID_INT    XIL_EXCEPTION_ID_FIQ_INT

    Int_Id就需要传入:XIL_EXCEPTION_FIQ

    1. /******************************************************** **************************/
    2. /** * * 启用作为参数 Int_Id 提供的中断源。
    3. * 调用此函数后,将发生指定 Int_Id 的任何未决中断条件。
    4. * 该 API 还将中断映射到请求的 CPU。
    5. * * @param InstancePtr 是指向 XScuGic 实例的指针。
    6. * @param Int_Id 包含中断源的 ID,并且应该 * 在 0 到 XSCUGIC_MAX_NUM_INTR_INPUTS - 1 的范围内 * * @return None。
    7. * * @note 无。
    8. ********************************************************* **************************/
    1. /*****************************************************************************/
    2. /**
    3. *
    4. * Enables the interrupt source provided as the argument Int_Id. Any pending
    5. * interrupt condition for the specified Int_Id will occur after this function is
    6. * called.
    7. * This API also maps the interrupt to the requesting CPU.
    8. *
    9. * @param InstancePtr is a pointer to the XScuGic instance.
    10. * @param Int_Id contains the ID of the interrupt source and should be
    11. * in the range of 0 to XSCUGIC_MAX_NUM_INTR_INPUTS - 1
    12. *
    13. * @return None.
    14. *
    15. * @note None.
    16. *
    17. ****************************************************************************/
    18. void XScuGic_Enable(XScuGic *InstancePtr, u32 Int_Id)
    19. {
    20. u32 Mask;
    21. u8 Cpu_Identifier = (u8)CpuId;
    22. #if defined (GICv3)
    23. u32 Temp;
    24. #endif
    25. /*
    26. * Assert the arguments
    27. */
    28. Xil_AssertVoid(InstancePtr != NULL);
    29. Xil_AssertVoid(Int_Id < XSCUGIC_MAX_NUM_INTR_INPUTS);
    30. Xil_AssertVoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
    31. #if defined (GICv3)
    32. if (Int_Id < XSCUGIC_SPI_INT_ID_START) {
    33. Int_Id &= 0x1f;
    34. Int_Id = 1 << Int_Id;
    35. Temp = XScuGic_ReDistSGIPPIReadReg(InstancePtr,XSCUGIC_RDIST_ISENABLE_OFFSET);
    36. Temp |= Int_Id;
    37. XScuGic_ReDistSGIPPIWriteReg(InstancePtr,XSCUGIC_RDIST_ISENABLE_OFFSET,Temp);
    38. return;
    39. }
    40. #endif
    41. XScuGic_InterruptMaptoCpu(InstancePtr, Cpu_Identifier, Int_Id);
    42. /*
    43. * Call spinlock to protect multiple applications running at separate
    44. * CPUs to write to the same register. This macro also ensures that
    45. * the spinlock mechanism is used only if spinlock is enabled by
    46. * user.
    47. */
    48. XIL_SPINLOCK();
    49. /*
    50. * The Int_Id is used to create the appropriate mask for the
    51. * desired bit position.
    52. */
    53. Mask = (u32)0x00000001U << (Int_Id % 32U);
    54. /*
    55. * Enable the selected interrupt source by setting the
    56. * corresponding bit in the Enable Set register.
    57. */
    58. XScuGic_DistWriteReg(InstancePtr, (u32)XSCUGIC_ENABLE_SET_OFFSET +
    59. ((Int_Id / 32U) * 4U), Mask);
    60. /*
    61. * Release the lock previously taken. This macro ensures that the lock
    62. * is given only if spinlock mechanism is enabled by the user.
    63. */
    64. XIL_SPINUNLOCK();
    65. }

    步骤9:使能异常处理Xil_ExceptionEnableMask()

    1. /****************************************************************************/
    2. /**
    3. * @brief Enable the IRQ exception.
    4. *
    5. * @return None.
    6. *
    7. * @note None.
    8. *
    9. ******************************************************************************/
    10. #if (defined (versal) && !defined(ARMR5) && EL3) || defined(ARMR52)
    11. #define Xil_ExceptionEnable() \
    12. Xil_ExceptionEnableMask(XIL_EXCEPTION_FIQ)
    13. #else
    14. #define Xil_ExceptionEnable() \
    15. Xil_ExceptionEnableMask(XIL_EXCEPTION_IRQ)
    16. #endif

    其他函数:

    全局中断处理回调函数:XScuGic_InterruptHandler

            用于注册到CPU中作为中断处理程序,输入到Xil_ExceptionRegisterHandler()中。

    1. /******************************************************** **************************/
    2. /**
    3. * 该函数是驱动程序的主要中断处理程序。它必须连接到中断源,
    4. * 以便在中断控制器的中断处于活动状态时调用它。
    5. * 它将解析哪些中断处于活动状态并启用,
    6. * 并调用适当的中断处理程序。它使用
    7. * 中断类型信息来确定何时确认中断。
    8. * 首先处理最高优先级的中断。
    9. * * 该函数假设中断向量表已预先初始化。
    10. * 在调用中断处理程序之前,它不会验证表中的条目是否有效。
    11. * * * @param InstancePtr 是指向 XScuGic 实例的指针。
    12. * * @返回无。
    13. * * @note 无。
    14. ********************************************************* ******************************/
    1. /*****************************************************************************/
    2. /**
    3. * This function is the primary interrupt handler for the driver. It must be
    4. * connected to the interrupt source such that it is called when an interrupt of
    5. * the interrupt controller is active. It will resolve which interrupts are
    6. * active and enabled and call the appropriate interrupt handler. It uses
    7. * the Interrupt Type information to determine when to acknowledge the interrupt.
    8. * Highest priority interrupts are serviced first.
    9. *
    10. * This function assumes that an interrupt vector table has been previously
    11. * initialized. It does not verify that entries in the table are valid before
    12. * calling an interrupt handler.
    13. *
    14. *
    15. * @param InstancePtr is a pointer to the XScuGic instance.
    16. *
    17. * @return None.
    18. *
    19. * @note None.
    20. *
    21. ******************************************************************************/
    22. void XScuGic_InterruptHandler(XScuGic *InstancePtr)
    23. {
    24. u32 InterruptID;
    25. #if !defined (GICv3)
    26. u32 IntIDFull;
    27. #endif
    28. XScuGic_VectorTableEntry *TablePtr;
    29. /* Assert that the pointer to the instance is valid
    30. */
    31. Xil_AssertVoid(InstancePtr != NULL);
    32. /*
    33. * Read the int_ack register to identify the highest priority
    34. * interrupt ID and make sure it is valid. Reading Int_Ack will
    35. * clear the interrupt in the GIC.
    36. */
    37. #if defined (GICv3)
    38. InterruptID = XScuGic_get_IntID();
    39. #else
    40. IntIDFull = XScuGic_CPUReadReg(InstancePtr, XSCUGIC_INT_ACK_OFFSET);
    41. InterruptID = IntIDFull & XSCUGIC_ACK_INTID_MASK;
    42. #endif
    43. if (XSCUGIC_MAX_NUM_INTR_INPUTS <= InterruptID) {
    44. goto IntrExit;
    45. }
    46. /*
    47. * If the interrupt is shared, do some locking here if
    48. * there are multiple processors.
    49. */
    50. /*
    51. * If pre-eption is required:
    52. * Re-enable pre-emption by setting the CPSR I bit for non-secure ,
    53. * interrupts or the F bit for secure interrupts
    54. */
    55. /*
    56. * If we need to change security domains, issue a SMC
    57. * instruction here.
    58. */
    59. /*
    60. * Execute the ISR. Jump into the Interrupt service routine
    61. * based on the IRQSource. A software trigger is cleared by
    62. *.the ACK.
    63. */
    64. TablePtr = &(InstancePtr->Config->HandlerTable[InterruptID]);
    65. if (TablePtr != NULL) {
    66. TablePtr->Handler(TablePtr->CallBackRef);
    67. }
    68. IntrExit:
    69. /*
    70. * Write to the EOI register, we are all done here.
    71. * Let this function return, the boot code will restore the stack.
    72. */
    73. #if defined (GICv3)
    74. XScuGic_ack_Int(InterruptID);
    75. #else
    76. XScuGic_CPUWriteReg(InstancePtr, XSCUGIC_EOI_OFFSET, IntIDFull);
    77. #endif
    78. /*
    79. * Return from the interrupt. Change security domains
    80. * could happen here.
    81. */
    82. }
    83. /** @} */

    GPIO 中断的中断处理程序XGpioPs_IntrHandler

    1. /******************************************************** ****************************/
    2. /** *
    3. * 该函数是 GPIO 中断的中断处理程序。它检查
    4. * 中断状态
    5. * 所有存储体的寄存器以确定已触发中断的实际存储体。
    6. * 然后它调用函数XGpioPs_SetBankHandler()设置的上层回调
    7. * 处理程序。
    8. * 当中断时调用回调
    9. * * @param InstancePtr 是指向 XGpioPs 实例的指针。
    10. * * @返回无。
    11. * * @note 此函数不会保存和恢复处理器上下文 * 因此用户必须提供此处理。
    12. ********************************************************* *****************************/
    1. /*****************************************************************************/
    2. /**
    3. *
    4. * This function is the interrupt handler for GPIO interrupts.It checks the
    5. * interrupt status registers of all the banks to determine the actual bank in
    6. * which an interrupt has been triggered. It then calls the upper layer callback
    7. * handler set by the function XGpioPs_SetBankHandler(). The callback is called
    8. * when an interrupt
    9. *
    10. * @param InstancePtr is a pointer to the XGpioPs instance.
    11. *
    12. * @return None.
    13. *
    14. * @note This function does not save and restore the processor context
    15. * such that the user must provide this processing.
    16. *
    17. ******************************************************************************/
    18. void XGpioPs_IntrHandler(const XGpioPs *InstancePtr)
    19. {
    20. u8 Bank;
    21. u32 IntrStatus;
    22. u32 IntrEnabled;
    23. Xil_AssertVoid(InstancePtr != NULL);
    24. Xil_AssertVoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
    25. for (Bank = 0U; Bank < InstancePtr->MaxBanks; Bank++) {
    26. #ifdef versal
    27. if(InstancePtr->PmcGpio == (u32)TRUE) {
    28. if(Bank == XGPIOPS_TWO) {
    29. continue;
    30. }
    31. } else {
    32. if((Bank == XGPIOPS_ONE) || (Bank == XGPIOPS_TWO)) {
    33. continue;
    34. }
    35. }
    36. #endif
    37. IntrStatus = XGpioPs_IntrGetStatus(InstancePtr, Bank);
    38. IntrEnabled = XGpioPs_IntrGetEnabled(InstancePtr,Bank);
    39. if ((IntrStatus & IntrEnabled) != (u32)0) {
    40. XGpioPs_IntrClear(InstancePtr, Bank,
    41. (IntrStatus & IntrEnabled));
    42. InstancePtr->Handler(InstancePtr->
    43. CallBackRef, Bank,
    44. (IntrStatus & IntrEnabled));
    45. }
    46. }
    47. }
    48. /** @} */

    返回指定引脚的中断使能状态函数:XGpioPs_IntrGetStatusPin

    (这个函数也有返回整个BANK中断状态的版本)

    1. /******************************************************** **************************/ 0/** * * 该函数返回指定引脚的中断使能状态。
    2. * * @param InstancePtr 是指向 XGpioPs 实例的指针。
    3. * @param Pin 是要知道中断启用状态的引脚号。
    4. * Zynq 中的有效值为 0-117,Zynq Ultrascale+ MP 中的有效值为 0-173。
    5. * * @return * - 如果发生中断则为 TRUE。
    6. * - 如果中断未发生,则为 FALSE。
    7. * * @note 无。
    8. ********************************************************* **************************/
    1. /****************************************************************************/
    2. /**
    3. *
    4. * This function returns interrupt enable status of the specified pin.
    5. *
    6. * @param InstancePtr is a pointer to the XGpioPs instance.
    7. * @param Pin is the pin number for which the interrupt enable status
    8. * is to be known.
    9. * Valid values are 0-117 in Zynq and 0-173 in Zynq Ultrascale+ MP.
    10. *
    11. * @return
    12. * - TRUE if the interrupt has occurred.
    13. * - FALSE if the interrupt has not occurred.
    14. *
    15. * @note None.
    16. *
    17. *****************************************************************************/
    18. u32 XGpioPs_IntrGetStatusPin(const XGpioPs *InstancePtr, u32 Pin)
    19. {
    20. u8 Bank;
    21. u8 PinNumber;
    22. u32 IntrReg;
    23. Xil_AssertNonvoid(InstancePtr != NULL);
    24. Xil_AssertNonvoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
    25. Xil_AssertNonvoid(Pin < InstancePtr->MaxPinNum);
    26. /* Get the Bank number and Pin number within the bank. */
    27. #ifdef versal
    28. XGpioPs_GetBankPin(InstancePtr,(u8)Pin, &Bank, &PinNumber);
    29. #else
    30. XGpioPs_GetBankPin((u8)Pin, &Bank, &PinNumber);
    31. #endif
    32. IntrReg = XGpioPs_ReadReg(InstancePtr->GpioConfig.BaseAddr,
    33. ((u32)(Bank) * XGPIOPS_REG_MASK_OFFSET) +
    34. XGPIOPS_INTSTS_OFFSET);
    35. return (((IntrReg & ((u32)1 << PinNumber)) != (u32)0)? (u32)TRUE : (u32)FALSE);
    36. }

    清除挂起中断函数XGpioPs_IntrClearPin

    1. /******************************************************** **************************/
    2. /** * * 该函数清除指定的挂起中断。该函数应该在软件处理完待处理的中断后调用。
    3. * * @param InstancePtr 是指向 XGpioPs 实例的指针。
    4. * @param Pin 是要清除中断状态的引脚号。
    5. * Zynq 中的有效值为 0-117,
    6. * Zynq Ultrascale+ MP 中的有效值为 0-173。
    7. * * @note 无。
    8. ********************************************************* **************************/
    1. /****************************************************************************/
    2. /**
    3. *
    4. * This function clears the specified pending interrupt. This function should be
    5. * called after the software has serviced the interrupts that are pending.
    6. *
    7. * @param InstancePtr is a pointer to the XGpioPs instance.
    8. * @param Pin is the pin number for which the interrupt status is to be
    9. * cleared. Valid values are 0-117 in Zynq and 0-173 in Zynq Ultrascale+ MP.
    10. *
    11. * @note None.
    12. *
    13. *****************************************************************************/
    14. void XGpioPs_IntrClearPin(const XGpioPs *InstancePtr, u32 Pin)
    15. {
    16. u8 Bank;
    17. u8 PinNumber;
    18. u32 IntrReg;
    19. Xil_AssertVoid(InstancePtr != NULL);
    20. Xil_AssertVoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
    21. Xil_AssertVoid(Pin < InstancePtr->MaxPinNum);
    22. /* Get the Bank number and Pin number within the bank. */
    23. #ifdef versal
    24. XGpioPs_GetBankPin(InstancePtr,(u8)Pin, &Bank, &PinNumber);
    25. #else
    26. XGpioPs_GetBankPin((u8)Pin, &Bank, &PinNumber);
    27. #endif
    28. /* Clear the specified pending interrupts. */
    29. IntrReg = XGpioPs_ReadReg(InstancePtr->GpioConfig.BaseAddr,
    30. ((u32)(Bank) * XGPIOPS_REG_MASK_OFFSET) +
    31. XGPIOPS_INTSTS_OFFSET);
    32. IntrReg &= ((u32)1 << PinNumber);
    33. XGpioPs_WriteReg(InstancePtr->GpioConfig.BaseAddr,
    34. ((u32)(Bank) * XGPIOPS_REG_MASK_OFFSET) +
    35. XGPIOPS_INTSTS_OFFSET, IntrReg);
    36. }

    禁用指定引脚的中断的函数XGpioPs_IntrDisablePin

    1. /******************************************************** **************************/
    2. /** * * 该函数禁用指定引脚的中断。
    3. * * @param InstancePtr 是指向 XGpioPs 实例的指针。
    4. * @param Pin 是要禁用中断的引脚号。
    5. * Zynq 中的有效值为 0-117,Zynq Ultrascale+ MP 中的有效值为 0-173。
    6. * * @返回无。
    7. * * @note 无。
    8. ********************************************************* ****************************/
    1. /****************************************************************************/
    2. /**
    3. *
    4. * This function disables the interrupts for the specified pin.
    5. *
    6. * @param InstancePtr is a pointer to the XGpioPs instance.
    7. * @param Pin is the pin number for which the interrupt is to be disabled.
    8. * Valid values are 0-117 in Zynq and 0-173 in Zynq Ultrascale+ MP.
    9. *
    10. * @return None.
    11. *
    12. * @note None.
    13. *
    14. *****************************************************************************/
    15. void XGpioPs_IntrDisablePin(const XGpioPs *InstancePtr, u32 Pin)
    16. {
    17. u8 Bank;
    18. u8 PinNumber;
    19. u32 IntrReg;
    20. Xil_AssertVoid(InstancePtr != NULL);
    21. Xil_AssertVoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
    22. Xil_AssertVoid(Pin < InstancePtr->MaxPinNum);
    23. /* Get the Bank number and Pin number within the bank. */
    24. #ifdef versal
    25. XGpioPs_GetBankPin(InstancePtr,(u8)Pin, &Bank, &PinNumber);
    26. #else
    27. XGpioPs_GetBankPin((u8)Pin, &Bank, &PinNumber);
    28. #endif
    29. IntrReg = ((u32)1 << (u32)PinNumber);
    30. XGpioPs_WriteReg(InstancePtr->GpioConfig.BaseAddr,
    31. ((u32)(Bank) * XGPIOPS_REG_MASK_OFFSET) +
    32. XGPIOPS_INTDIS_OFFSET, IntrReg);
    33. }

    使能指定引脚的中断函数XGpioPs_IntrEnablePin

    1. /******************************************************** **************************/
    2. /** * * 该函数使能指定引脚的中断。
    3. * * @param InstancePtr 是指向 XGpioPs 实例的指针。
    4. * @param Pin 是要启用中断的引脚号。
    5. * Zynq 中的有效值为 0-117,Zynq Ultrascale+ MP 中的有效值为 0-173。
    6. * * @返回无。
    7. * * @note 无。
    8. ********************************************************* ****************************/
    1. /****************************************************************************/
    2. /**
    3. *
    4. * This function enables the interrupt for the specified pin.
    5. *
    6. * @param InstancePtr is a pointer to the XGpioPs instance.
    7. * @param Pin is the pin number for which the interrupt is to be enabled.
    8. * Valid values are 0-117 in Zynq and 0-173 in Zynq Ultrascale+ MP.
    9. *
    10. * @return None.
    11. *
    12. * @note None.
    13. *
    14. *****************************************************************************/
    15. void XGpioPs_IntrEnablePin(const XGpioPs *InstancePtr, u32 Pin)
    16. {
    17. u8 Bank;
    18. u8 PinNumber;
    19. u32 IntrReg;
    20. Xil_AssertVoid(InstancePtr != NULL);
    21. Xil_AssertVoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
    22. Xil_AssertVoid(Pin < InstancePtr->MaxPinNum);
    23. /* Get the Bank number and Pin number within the bank. */
    24. #ifdef versal
    25. XGpioPs_GetBankPin(InstancePtr,(u8)Pin, &Bank, &PinNumber);
    26. #else
    27. XGpioPs_GetBankPin((u8)Pin, &Bank, &PinNumber);
    28. #endif
    29. IntrReg = ((u32)1 << (u32)PinNumber);
    30. XGpioPs_WriteReg(InstancePtr->GpioConfig.BaseAddr,
    31. ((u32)(Bank) * XGPIOPS_REG_MASK_OFFSET) +
    32. XGPIOPS_INTEN_OFFSET, IntrReg);
    33. }

    例程

    1. /******************************************************************************
    2. * Copyright (C) 2010 - 2021 Xilinx, Inc. All rights reserved.
    3. * SPDX-License-Identifier: MIT
    4. ******************************************************************************/
    5. /*****************************************************************************/
    6. /**
    7. * @file xgpiops_intr_example.c
    8. *
    9. * This file contains a design example using the GPIO driver (XGpioPs) in an
    10. * interrupt driven mode of operation.
    11. *
    12. * The example uses the interrupt capability of the GPIO to detect push button
    13. * events and set the output LEDs based on the input.
    14. *
    15. * @note
    16. * This example assumes that there is a Uart device in the HW design.
    17. * This example is to provide support only for zcu102 on ZynqMp Platform and
    18. * only for zc702 on Zynq Platform.
    19. * For ZynqMP Platform, Input pin is 22(sw19 on zcu102 board) and Output Pin is
    20. * 23(DS50 on zcu102 board).
    21. * For Zynq Platform, Input Pins are 12(sw14 on zc702 board), 14(sw13 on
    22. * zc702 board) and Output Pin is 10(DS23 on zc702 board). SW15 on zc702 board
    23. * is a combination of sw13 and sw14. To operate either of the input
    24. * pins, keep SW15 low(both should be 00).
    25. * This example supports the VCK190 and VMK180 for Versal, but requires a PL
    26. * shim. See Answer Record AR# 75677 for details.
    27. * On the Versal Platform we have two GPIOPS instances :PMC GPIO and PS GPIO
    28. * PMC GPIO contain 4 banks and 116 pins, organized as follows:
    29. * Bank 0 I/Os: 25:0 (MIO)
    30. * Bank 1: I/Os: 51:26 (MIO)
    31. * Bank 3: I/Os: 83:52 (EMIO)
    32. * Bank 4: I/Os: 115:84 (EMIO)
    33. * PS GPIO contains 2 banks and 58 pins
    34. * Bank 0 I/Os: 25:0 (MIO)
    35. * Bank 3: I/Os: 57:26 (EMIO)
    36. * See Figure 61 in AM011 Versal TRM for details.
    37. * Driver supports both PS GPIO and PMC GPIO.
    38. * For accessing PMC GPIOs application you need to set "GPIO.PmcGpio = 1"
    39. * otherwise it accesses PS GPIO.
    40. *
    41. *
    42. * MODIFICATION HISTORY:
    43. *
    44. * Ver Who Date Changes
    45. * ----- ---- -------- -----------------------------------------------
    46. * 1.00a sv 01/18/10 First Release
    47. * 3.3 ms 04/17/17 Added notes about gpio input and output pin description
    48. * for zcu102 and zc702 boards, configured Interrupt pin
    49. * to input pin for proper working of interrupt example.
    50. * 3.7 sne 12/04/19 Reverted versal example support.
    51. * 3.8 sne 09/17/20 Added description for Versal PS and PMC GPIO pins.
    52. * 3.9 sne 11/19/20 Added versal PmcGpio example support.
    53. *
    54. *
  • *
  • ******************************************************************************/
  • /***************************** Include Files *********************************/
  • #include "xparameters.h"
  • #include "xgpiops.h"
  • #include "xscugic.h"
  • #include "xil_exception.h"
  • #include "xplatform_info.h"
  • #include
  • /************************** Constant Definitions *****************************/
  • /*
  • * The following constants map to the names of the hardware instances that
  • * were created in the EDK XPS system. They are defined here such that
  • * the user can easily change all the needed device IDs in one place.
  • */
  • #define GPIO_DEVICE_ID XPAR_XGPIOPS_0_DEVICE_ID
  • #define INTC_DEVICE_ID XPAR_SCUGIC_SINGLE_DEVICE_ID
  • #ifdef versal
  • #define GPIO_INTERRUPT_ID XPMC_GPIO_INT_ID
  • #else
  • #define GPIO_INTERRUPT_ID XPAR_XGPIOPS_0_INTR
  • #endif
  • /* The following constants define the GPIO banks that are used. */
  • #ifdef versal
  • #define GPIO_BANK XGPIOPS_BANK3 /* Bank 3 of the GPIO Device */
  • #else
  • #define GPIO_BANK XGPIOPS_BANK0 /* Bank 0 of the GPIO Device */
  • #endif
  • /**************************** Type Definitions *******************************/
  • /***************** Macros (Inline Functions) Definitions *********************/
  • /************************** Function Prototypes ******************************/
  • static int GpioIntrExample(XScuGic *Intc, XGpioPs *Gpio, u16 DeviceId,
  • u16 GpioIntrId);
  • static void IntrHandler(void *CallBackRef, u32 Bank, u32 Status);
  • static int SetupInterruptSystem(XScuGic *Intc, XGpioPs *Gpio, u16 GpioIntrId);
  • /************************** Variable Definitions *****************************/
  • /*
  • * The following are declared globally so they are zeroed and so they are
  • * easily accessible from a debugger.
  • */
  • static XGpioPs Gpio; /* The Instance of the GPIO Driver */
  • static XScuGic Intc; /* The Instance of the Interrupt Controller Driver */
  • static u32 AllButtonsPressed; /* Intr status of the bank */
  • static u32 Input_Bank_Pin; /* Pin Number within Bank */
  • static u32 Input_Pin; /* Switch button */
  • static u32 Output_Pin; /* LED button */
  • /****************************************************************************/
  • /**
  • *
  • * Main function that invokes the GPIO Interrupt example.
  • *
  • *
  • * @return
  • * - XST_SUCCESS if the example has completed successfully.
  • * - XST_FAILURE if the example has failed.
  • *
  • * @note None.
  • *
  • *****************************************************************************/
  • int main(void)
  • {
  • int Status;
  • xil_printf("GPIO Interrupt Example Test \r\n");
  • /*
  • * Run the GPIO interrupt example, specify the parameters that
  • * are generated in xparameters.h.
  • */
  • Status = GpioIntrExample(&Intc, &Gpio, GPIO_DEVICE_ID,
  • GPIO_INTERRUPT_ID);
  • if (Status != XST_SUCCESS) {
  • xil_printf("GPIO Interrupt Example Test Failed\r\n");
  • return XST_FAILURE;
  • }
  • xil_printf("Successfully ran GPIO Interrupt Example Test\r\n");
  • return XST_SUCCESS;
  • }
  • /****************************************************************************/
  • /**
  • * This function shows the usage of interrupt fucntionality of the GPIO device.
  • * It is responsible for initializing the GPIO device, setting up interrupts and
  • * providing a foreground loop such that interrupts can occur in the background.
  • *
  • * @param Intc is a pointer to the XScuGic driver Instance.
  • * @param Gpio is a pointer to the XGpioPs driver Instance.
  • * @param DeviceId is the XPAR__PS_DEVICE_ID value
  • * from xparameters.h.
  • * @param GpioIntrId is XPAR___VEC_ID value
  • * from xparameters.h
  • *
  • * @return
  • * - XST_SUCCESS if the example has completed successfully.
  • * - XST_FAILURE if the example has failed.
  • *
  • * @note None
  • *
  • *****************************************************************************/
  • int GpioIntrExample(XScuGic *Intc, XGpioPs *Gpio, u16 DeviceId, u16 GpioIntrId)
  • {
  • XGpioPs_Config *ConfigPtr;
  • int Status;
  • int Type_of_board;
  • /* Initialize the Gpio driver. */
  • ConfigPtr = XGpioPs_LookupConfig(DeviceId);
  • if (ConfigPtr == NULL) {
  • return XST_FAILURE;
  • }
  • Type_of_board = XGetPlatform_Info();
  • switch (Type_of_board) {
  • case XPLAT_ZYNQ_ULTRA_MP:
  • Input_Bank_Pin = 22;
  • Input_Pin = 22;
  • Output_Pin = 23;
  • break;
  • case XPLAT_ZYNQ:
  • Input_Bank_Pin = 14;
  • Input_Pin = 14;
  • Output_Pin = 10;
  • break;
  • #ifdef versal
  • case XPLAT_VERSAL:
  • /* Accessing PMC GPIO by setting field to 1 */
  • Gpio->PmcGpio = 1;
  • Input_Bank_Pin = 4;
  • Input_Pin = 56;
  • Output_Pin = 52;
  • break;
  • #endif
  • }
  • XGpioPs_CfgInitialize(Gpio, ConfigPtr, ConfigPtr->BaseAddr);
  • /* Run a self-test on the GPIO device. */
  • Status = XGpioPs_SelfTest(Gpio);
  • if (Status != XST_SUCCESS) {
  • return XST_FAILURE;
  • }
  • /* Set the direction for the specified pin to be input */
  • XGpioPs_SetDirectionPin(Gpio, Input_Pin, 0x0);
  • /* Set the direction for the specified pin to be output. */
  • XGpioPs_SetDirectionPin(Gpio, Output_Pin, 1);
  • XGpioPs_SetOutputEnablePin(Gpio, Output_Pin, 1);
  • XGpioPs_WritePin(Gpio, Output_Pin, 0x0);
  • /*
  • * Setup the interrupts such that interrupt processing can occur. If
  • * an error occurs then exit.
  • */
  • Status = SetupInterruptSystem(Intc, Gpio, GPIO_INTERRUPT_ID);
  • if (Status != XST_SUCCESS) {
  • return XST_FAILURE;
  • }
  • xil_printf("\n\rPush Switch button to exit\n\r");
  • AllButtonsPressed = FALSE;
  • /*
  • * Loop forever while the button changes are handled by the interrupt
  • * level processing.
  • */
  • while(AllButtonsPressed == FALSE);
  • return XST_SUCCESS;
  • }
  • /****************************************************************************/
  • /**
  • * This function is the user layer callback function for the bank 0 interrupts of
  • * the GPIO device. It checks if all the switches have been pressed to stop the
  • * interrupt processing and exit from the example.
  • *
  • * @param CallBackRef is a pointer to the upper layer callback reference.
  • * @param Status is the Interrupt status of the GPIO bank.
  • *
  • * @return None.
  • *
  • * @note None.
  • *
  • ******************************************************************************/
  • static void IntrHandler(void *CallBackRef, u32 Bank, u32 Status)
  • {
  • XGpioPs *Gpio = (XGpioPs *)CallBackRef;
  • u32 DataRead;
  • /* Push the switch button */
  • DataRead = XGpioPs_ReadPin(Gpio, Input_Pin);
  • if (DataRead != 0) {
  • XGpioPs_SetDirectionPin(Gpio, Output_Pin, 1);
  • XGpioPs_SetOutputEnablePin(Gpio, Output_Pin, 1);
  • XGpioPs_WritePin(Gpio, Output_Pin, DataRead);
  • AllButtonsPressed = TRUE;
  • }
  • }
  • /*****************************************************************************/
  • /**
  • *
  • * This function sets up the interrupt system for the example. It enables falling
  • * edge interrupts for all the pins of bank 0 in the GPIO device.
  • *
  • * @param GicInstancePtr is a pointer to the XScuGic driver Instance.
  • * @param GpioInstancePtr contains a pointer to the instance of the GPIO
  • * component which is going to be connected to the interrupt
  • * controller.
  • * @param GpioIntrId is the interrupt Id and is typically
  • * XPAR___VEC_ID value from
  • * xparameters.h.
  • *
  • * @return XST_SUCCESS if successful, otherwise XST_FAILURE.
  • *
  • * @note None.
  • *
  • ****************************************************************************/
  • static int SetupInterruptSystem(XScuGic *GicInstancePtr, XGpioPs *Gpio,
  • u16 GpioIntrId)
  • {
  • int Status;
  • XScuGic_Config *IntcConfig; /* Instance of the interrupt controller */
  • Xil_ExceptionInit();
  • /*
  • * Initialize the interrupt controller driver so that it is ready to
  • * use.
  • */
  • IntcConfig = XScuGic_LookupConfig(INTC_DEVICE_ID);
  • if (NULL == IntcConfig) {
  • return XST_FAILURE;
  • }
  • Status = XScuGic_CfgInitialize(GicInstancePtr, IntcConfig,
  • IntcConfig->CpuBaseAddress);
  • if (Status != XST_SUCCESS) {
  • return XST_FAILURE;
  • }
  • /*
  • * Connect the interrupt controller interrupt handler to the hardware
  • * interrupt handling logic in the processor.
  • */
  • Xil_ExceptionRegisterHandler(XIL_EXCEPTION_ID_INT,
  • (Xil_ExceptionHandler)XScuGic_InterruptHandler,
  • GicInstancePtr);
  • /*
  • * Connect the device driver handler that will be called when an
  • * interrupt for the device occurs, the handler defined above performs
  • * the specific interrupt processing for the device.
  • */
  • Status = XScuGic_Connect(GicInstancePtr, GpioIntrId,
  • (Xil_ExceptionHandler)XGpioPs_IntrHandler,
  • (void *)Gpio);
  • if (Status != XST_SUCCESS) {
  • return Status;
  • }
  • /* Enable falling edge interrupts for all the pins in GPIO bank. */
  • XGpioPs_SetIntrType(Gpio, GPIO_BANK, 0x00, 0xFFFFFFFF, 0x00);
  • /* Set the handler for gpio interrupts. */
  • XGpioPs_SetCallbackHandler(Gpio, (void *)Gpio, IntrHandler);
  • /* Enable the GPIO interrupts of GPIO Bank. */
  • XGpioPs_IntrEnable(Gpio, GPIO_BANK, (1 << Input_Bank_Pin));
  • /* Enable the interrupt for the GPIO device. */
  • XScuGic_Enable(GicInstancePtr, GpioIntrId);
  • /* Enable interrupts in the Processor. */
  • Xil_ExceptionEnableMask(XIL_EXCEPTION_IRQ);
  • return XST_SUCCESS;
  • }
  • 相关阅读:
    easyexcel升级3.3.4失败的经历
    一文速通Nginx网关与gateway网关区分
    Java中泛型——类型擦除
    Freeswitch API调用方式
    微信加好友操作频繁了,怎么办?
    pycharm 远程运行报错 Failed to prepare environment
    app clear data源码追踪
    鲲山科技:引入和鲸 ModelWhale,实现量化策略的高效迭代
    蓝牙学习六(GATT)
    windows系统执行ps1(powershell)脚本文件无法识别命令的问题
  • 原文地址:https://blog.csdn.net/NoNoUnknow/article/details/133611285