初级会员
- 积分
- 135
- 金钱
- 135
- 注册时间
- 2022-6-19
- 在线时间
- 33 小时
|
1金钱
请问领航者文档中的AXI按键中断拓展训练,如何设置多个PL按键的中断啊,我使用两个通道中断函数,按下去就没有反应了。
- #include "xparameters.h"
- #include "xgpiops.h"
- #include "xgpio.h"
- #include "xscugic.h"
- #include "xil_exception.h"
- #include "xplatform_info.h"
- #include <xil_printf.h>
- #include "sleep.h"
- /************************** Constant Definitions *****************************/
- //以下常量映射到xparameters.h文件
- #define GPIOPS_DEVICE_ID XPAR_XGPIOPS_0_DEVICE_ID //PS端GPIO器件ID
- #define AXI_GPIO_DEVICE_ID XPAR_AXI_GPIO_0_DEVICE_ID //PL端AXI GPIO器件ID
- #define SCUGIC_ID XPAR_SCUGIC_SINGLE_DEVICE_ID //通用中断控制器ID
- #define AXI_GPIO_INT_ID XPAR_FABRIC_GPIO_0_VEC_ID //PL端AXI GPIO中断ID
- #define MIO_LED 0 //LED 连接到 MIO0
- #define LED2 8 //LED连接到MIO8
- #define KEY_CHANNEL1 1 //PL 按键0使用 AXI GPIO 通道 1
- #define KEY_CH1_MASK XGPIO_IR_CH1_MASK //通道 1的中断位定义
- #define KEY_CHANNEL2 2 //PL 按键1使用 AXI GPIO 通道 2
- #define KEY_CH2_MASK XGPIO_IR_CH2_MASK //通道 2的中断位定义
- /************************** Function Prototypes ******************************/
- void instance_init();
- int setup_interrupt_system(XScuGic *gic_inst_ptr, XGpio *axi_gpio_inst_ptr,
- u16 AXI_GpioIntrId);
- static void intr_handler(void *callback_ref);
- int setup_interrupt_system2(XScuGic *gic_inst_ptr, XGpio *axi_gpio_inst_ptr, u16 AXI_GpioIntrId);
- static void intr_handler2(void *callback_ref);
- /**************************Global Variable Definitions ***********************/
- XGpioPs gpiops_inst; //PS端GPIO驱动实例
- XGpio axi_gpio_inst; //PL端AXI GPIO驱动实例
- XScuGic scugic_inst; //通用中断控制器驱动实例
- int led_value=1; //ps端LED0的显示状态
- int led1_value=1; //ps端LED1的显示状态
- /************************** Function Definitions *****************************/
- int main(void)
- {
- int status;
- int status2;
- //初始化各器件驱动
- instance_init();
- xil_printf("AXI_Gpio interrupt test \r\n");
- //设置LED所连接的MIO引脚的方向为输出并使能输出
- XGpioPs_SetDirectionPin(&gpiops_inst, MIO_LED, 1);
- XGpioPs_SetOutputEnablePin(&gpiops_inst, MIO_LED, 1);
- XGpioPs_WritePin(&gpiops_inst, MIO_LED, led_value);
- XGpioPs_SetDirectionPin(&gpiops_inst, LED2, 1);
- XGpioPs_SetOutputEnablePin(&gpiops_inst, LED2, 1);
- XGpioPs_WritePin(&gpiops_inst, LED2, led_value);
- //建立中断,出现错误则打印信息并退出
- status = setup_interrupt_system(&scugic_inst, &axi_gpio_inst, AXI_GPIO_INT_ID);
- if (status != XST_SUCCESS) {
- xil_printf("Setup interrupt system failed\r\n");
- return XST_FAILURE;
- }
- status2 = setup_interrupt_system2(&scugic_inst, &axi_gpio_inst, AXI_GPIO_INT_ID);
- if (status2 != XST_SUCCESS) {
- xil_printf("Setup interrupt system failed\r\n");
- return XST_FAILURE;
- }
- return XST_SUCCESS;
- }
- //初始化各器件驱动
- void instance_init()
- {
- XScuGic_Config *scugic_cfg_ptr;
- XGpioPs_Config *gpiops_cfg_ptr;
- //初始化中断控制器驱动
- scugic_cfg_ptr = XScuGic_LookupConfig(SCUGIC_ID);
- XScuGic_CfgInitialize(&scugic_inst, scugic_cfg_ptr, scugic_cfg_ptr->CpuBaseAddress);
- //初始化PS端 GPIO驱动
- gpiops_cfg_ptr = XGpioPs_LookupConfig(GPIOPS_DEVICE_ID );
- XGpioPs_CfgInitialize(&gpiops_inst, gpiops_cfg_ptr, gpiops_cfg_ptr->BaseAddr);
- //初始化PL端 AXI GPIO驱动
- XGpio_Initialize(&axi_gpio_inst, AXI_GPIO_DEVICE_ID);
- }
- //建立中断系统,使能KEY按键的下降沿中断
- // [url=home.php?mod=space&uid=271674]@param[/url] GicInstancePtr是一个指向XScuGic驱动实例的指针
- // @param gpio是一个指向连接到中断的GPIO组件实例的指针
- // @param GpioIntrId是Gpio中断ID
- // @return 如果成功返回XST_SUCCESS, 否则返回XST_FAILURE
- int setup_interrupt_system(XScuGic *gic_inst_ptr, XGpio *axi_gpio_inst_ptr, u16 AXI_GpioIntrId)
- {
- //设置并使能中断异常
- Xil_ExceptionInit();
- Xil_ExceptionRegisterHandler(XIL_EXCEPTION_ID_INT,
- (Xil_ExceptionHandler) XScuGic_InterruptHandler, gic_inst_ptr);
- Xil_ExceptionEnable();
- //设置中断源的优先级和触发类型(高电平触发)
- XScuGic_SetPriorityTriggerType(gic_inst_ptr, AXI_GpioIntrId, 0xA0, 0x01);
- //为中断设置中断处理函数
- XScuGic_Connect(gic_inst_ptr, AXI_GpioIntrId,
- (Xil_ExceptionHandler) intr_handler, (void *) axi_gpio_inst_ptr);
- //使能来自于axi_Gpio器件的中断
- XScuGic_Enable(gic_inst_ptr, AXI_GpioIntrId);
- //配置PL端 AXI GPIO
- //设置 AXI GPIO 通道 1方向为输入
- XGpio_SetDataDirection(axi_gpio_inst_ptr, KEY_CHANNEL1, 1);
- XGpio_InterruptEnable(axi_gpio_inst_ptr, KEY_CH1_MASK); //使能通道1的中断
- XGpio_InterruptGlobalEnable(axi_gpio_inst_ptr); //使能axi gpio全局中断
- return XST_SUCCESS;
- }
- //中断处理函数
- // @param CallBackRef是指向上层回调引用的指针
- static void intr_handler(void *callback_ref)
- {
- XGpio *axi_gpio_inst_ptr = (XGpio *)callback_ref;
- usleep(20000); //延时20ms,按键消抖
- if (XGpio_DiscreteRead(axi_gpio_inst_ptr, KEY_CHANNEL1) == 0) {//按键有效按下
- print("Interrupt Detected!\r\n");
- led_value = ~led_value;
- XGpioPs_WritePin(&gpiops_inst, MIO_LED, led_value); //改变LED显示状态
- XGpio_InterruptDisable(axi_gpio_inst_ptr, KEY_CH1_MASK);//关闭 AXI GPIO中断使能
- }
- XGpio_InterruptClear(axi_gpio_inst_ptr, KEY_CH1_MASK); //清除中断
- XGpio_InterruptEnable(axi_gpio_inst_ptr, KEY_CH1_MASK); //使能AXI GPIO中断
- }
- int setup_interrupt_system2(XScuGic *gic_inst_ptr, XGpio *axi_gpio_inst_ptr, u16 AXI_GpioIntrId)
- {
- //设置并使能中断异常
- Xil_ExceptionInit();
- Xil_ExceptionRegisterHandler(XIL_EXCEPTION_ID_INT,
- (Xil_ExceptionHandler) XScuGic_InterruptHandler, gic_inst_ptr);
- Xil_ExceptionEnable();
- //设置中断源的优先级和触发类型(高电平触发)
- XScuGic_SetPriorityTriggerType(gic_inst_ptr, AXI_GpioIntrId, 0xA0, 0x01);
- //为中断设置中断处理函数
- XScuGic_Connect(gic_inst_ptr, AXI_GpioIntrId,
- (Xil_ExceptionHandler) intr_handler2, (void *) axi_gpio_inst_ptr);
- //使能来自于axi_Gpio器件的中断
- XScuGic_Enable(gic_inst_ptr, AXI_GpioIntrId);
- //配置PL端 AXI GPIO
- //设置 AXI GPIO 通道 1方向为输入
- XGpio_SetDataDirection(axi_gpio_inst_ptr, KEY_CHANNEL2, 1);
- XGpio_InterruptEnable(axi_gpio_inst_ptr, KEY_CH2_MASK); //使能通道1的中断
- XGpio_InterruptGlobalEnable(axi_gpio_inst_ptr); //使能axi gpio全局中断
- return XST_SUCCESS;
- }
- static void intr_handler2(void *callback_ref)
- {
- XGpio *axi_gpio_inst_ptr = (XGpio *)callback_ref;
- usleep(20000); //延时20ms,按键消抖
- if (XGpio_DiscreteRead(axi_gpio_inst_ptr, KEY_CHANNEL2) == 0) {//按键有效按下
- print("Interrupt Detected2!\r\n");
- led1_value = ~led1_value;
- XGpioPs_WritePin(&gpiops_inst, MIO_LED, led1_value); //改变LED显示状态
- XGpio_InterruptDisable(axi_gpio_inst_ptr, KEY_CH2_MASK);//关闭 AXI GPIO中断使能
- }
- XGpio_InterruptClear(axi_gpio_inst_ptr, KEY_CH2_MASK); //清除中断
- XGpio_InterruptEnable(axi_gpio_inst_ptr, KEY_CH2_MASK); //使能AXI GPIO中断
- }
复制代码
|
最佳答案
查看完整内容[请看2#楼]
在中断处理函数中,读取axi gpio通道的数据,然后看32位中每个数据位宽,对应bit位读到的是0,就是那个bit位连接的按键按下了
|