企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
![](https://img.kancloud.cn/41/60/4160e90d9f7823e017ae775de29804fc_1038x663.png) 可以看到PL端的中断,通过SPI连接到GIC上,在双核模式下使用PL端的中断时,需要对中断进行绑定。 # 搭建硬件系统 为了快速上手,在搭建硬件系统时,设计一个定时器模块,该模块完成的功能就是每隔1s和2s向ZYNQ核提供一个PL端的中断。 timer模块是产生PL端中断的模块,该模块是自行使用Vivado封装的一个模块,对应代码如下: ![](https://img.kancloud.cn/1f/b4/1fb48bf53adb77bd8950299e022cac07_1177x465.png) ```verilog module timer( input wire clk , input wire rst_n , output wire pl_intr_0 , output wire pl_intr_1 ); //========================================== //parameter define //========================================== parameter ONE_SEC = 50000000; parameter TWO_SEC = ONE_SEC << 1; parameter ONE_US = 500; //========================================== //internal signals //========================================== reg [31:0] cnt_1s ; wire add_cnt_1s ; wire end_cnt_1s ; reg [31:0] cnt_2s ; wire add_cnt_2s ; wire end_cnt_2s ; reg pl_intr_1_r ; reg pl_intr_0_r ; assign pl_intr_0 = pl_intr_0_r; assign pl_intr_1 = pl_intr_1_r; //----------------cnt_1s------------------ always @(posedge clk or negedge rst_n) begin if (rst_n == 1'b0) begin cnt_1s <= 'd0; end else if (add_cnt_1s) begin if(end_cnt_1s) cnt_1s <= 'd0; else cnt_1s <= cnt_1s + 1'b1; end else begin cnt_1s <= 'd0; end end assign add_cnt_1s = 1; assign end_cnt_1s = add_cnt_1s && cnt_1s == (ONE_SEC - 1); //----------------cnt_2s------------------ always @(posedge clk or negedge rst_n) begin if (rst_n == 1'b0) begin cnt_2s <= 'd0; end else if (add_cnt_2s) begin if(end_cnt_2s) cnt_2s <= 'd0; else cnt_2s <= cnt_2s + 1'b1; end else begin cnt_2s <= 'd0; end end assign add_cnt_2s = 1; assign end_cnt_2s = add_cnt_2s && cnt_2s == (TWO_SEC - 1); //----------------pl_intr_0_r------------------ always @(posedge clk or negedge rst_n) begin if (rst_n==1'b0) begin pl_intr_0_r <= 1'b0; end else if (cnt_1s == (ONE_SEC - ONE_US - 1)) begin pl_intr_0_r <= 1'b1; end else if (end_cnt_1s) begin pl_intr_0_r <= 1'b0; end end //----------------pl_intr_1_r------------------ always @(posedge clk or negedge rst_n) begin if (rst_n==1'b0) begin pl_intr_1_r <= 1'b0; end else if (cnt_2s == (TWO_SEC - ONE_US - 1)) begin pl_intr_1_r <= 1'b1; end else if (end_cnt_2s) begin pl_intr_1_r <= 1'b0; end end endmodule ``` # 硬中断初始化流程 | 步骤 | 函数 | |---|---| |初始化异常处理系统|Xil\_ExceptionInit()| |初始化中断控制器|XScuGic_LookupConfig(),XScuGic_CfgInitialize()| |注册异常回调函数|Xil\_ExceptionRegisterHandler()| |将中断控制器与对应的中断ID相连接|XScuGic\_Connect()| |设置中断类型和优先级|XScuGic\_SetPriTrigTypeByDistAddr()| |硬中断映射到对应的CPU|XScuGic\_InterruptMaptoCpu()| |使能中断控制器|XScuGic\_Enable()| |使能异常处理系统|Xil\_ExceptionEnable()| 程序设计   程序设计十分简单,只需要在CPU0和CPU1接收到来自PL端的对应的中断时,就让其打印一下信息就行了。 CPU0应用程序   在设置硬中断时,需要设置中断的类型和优先级,使用到的函数是XScuGic_SetPriTrigTypeByDistAddr(DIST_BASE_ADDR, CPU0_HW_INT_ID, 0x20, 0x03); 其中第一个参数是,中断设置的基地址,ZYNQ的中断设置都有一个基地址,对各个中断号的中断的响应,可以通过中断号来进行偏移,除此之外还需要设置中断的优先级,中断的优先级是以8为递增的,也就是中断的优先级有0x00,0x08,0x10,0x18等等,优先级最低的是0xF8;中断的类型根据中断的类型不同可以设置为不同类型的中断,其中私有中断(PPI)默认为上升沿触发,软中断(SFI),共享中断(SPI)可以设置为电平中断和边沿中断。 ```c #include <stdio.h> #include "platform.h" #include "xil_printf.h" #include "xparameters.h" #include "xscugic.h" #include "sleep.h" // OCM #define OCM3_ADDR 0xFFFF0000 #define GIC_DEV_ID XPAR_PS7_SCUGIC_0_DEVICE_ID #define DIST_BASE_ADDR XPAR_PS7_SCUGIC_0_DIST_BASEADDR //software interrupt ID is 0x00~0x0F #define CPU0_SW_INT_ID 0x0D //CPU0的软中断ID,用于中断CPU0 #define CPU1_SW_INT_ID 0x0E //CPU1的软中断ID,用于中断CPU1 //PL 到 arm的中断 #define CPU0_HW_INT_ID 61 //CPU0的硬中断,1S计数器中断 #define CPU1_HW_INT_ID 62 //CPU1的硬中断,2S计数器中断 static XScuGic gicInst; static XScuGic_Config * gicCfg_Ptr; void hwIntrHandler(void * CallBackRef) { printf("CPU0 is interrupted by HW\n"); } int initGic() { int status; //1. 初始化异常处理系统 Xil_ExceptionInit(); //2. 初始化中断控制器 gicCfg_Ptr = XScuGic_LookupConfig(GIC_DEV_ID); status = XScuGic_CfgInitialize(&gicInst, gicCfg_Ptr, gicCfg_Ptr->CpuBaseAddress); if(status != XST_SUCCESS) { printf("initialize GIC failed\n"); return XST_FAILURE; } //3. 注册异常回调函数 Xil_ExceptionRegisterHandler(XIL_EXCEPTION_ID_INT, (Xil_ExceptionHandler)XScuGic_InterruptHandler, &gicInst); //4. 连接GIC对应的硬中断ID status = XScuGic_Connect(&gicInst, CPU0_HW_INT_ID, (Xil_InterruptHandler)hwIntrHandler, &gicInst); if(status != XST_SUCCESS) { printf("Connect GIC failed\n"); return XST_FAILURE; } XScuGic_SetPriTrigTypeByDistAddr(DIST_BASE_ADDR, CPU0_HW_INT_ID, 0x20, 0x03); //6. 将硬中断绑定到CPU0 XScuGic_InterruptMaptoCpu(&gicInst, 0x00, CPU0_HW_INT_ID); //7. 使能硬中断 XScuGic_Enable(&gicInst, CPU0_HW_INT_ID); //8. 使能异常处理系统 Xil_ExceptionEnable(); return status; } int main() { //初始化中断控制器 int status; status = initGic(); if(status != XST_SUCCESS) { printf("initialize GIC failed\n"); return XST_FAILURE; } while(1) { } return 0; } ``` ## CPU1应用程序 ```c #include <stdio.h> #include "platform.h" #include "xil_printf.h" #include "xparameters.h" #include "xscugic.h" #include "sleep.h" // on chip memory3 address #define OCM3_ADDR 0xFFFF0000 #define GIC_DEV_ID XPAR_PS7_SCUGIC_0_DEVICE_ID #define DIST_BASE_ADDR XPAR_PS7_SCUGIC_0_DIST_BASEADDR //software interrupt ID is 0x00~0x0F #define CPU0_SW_INT_ID 0x0D //CPU0的软中断ID,用于中断CPU0 #define CPU1_SW_INT_ID 0x0E //CPU1的软中断ID,用于中断CPU1 #define CPU0_HW_INT_ID 61 //CPU0的硬中断,1S计数器中断 #define CPU1_HW_INT_ID 62 //CPU1的硬中断,2S计数器中断 static XScuGic gicInst; static XScuGic_Config * gicCfg_Ptr; void hwIntrHandler(void * CallBackRef) { usleep(1000); printf("CPU1 is interrupted by HW\n"); } int initGic() { int status; //1. 初始化异常处理系统 Xil_ExceptionInit(); //2. 初始化中断控制器 gicCfg_Ptr = XScuGic_LookupConfig(GIC_DEV_ID); status = XScuGic_CfgInitialize(&gicInst, gicCfg_Ptr, gicCfg_Ptr->CpuBaseAddress); if(status != XST_SUCCESS) { printf("initialize GIC failed\n"); return XST_FAILURE; } //3. 注册异常回调函数,中断类型的异常 Xil_ExceptionRegisterHandler(XIL_EXCEPTION_ID_INT, (Xil_ExceptionHandler)XScuGic_InterruptHandler, &gicInst); //4. 连接GIC对应的硬中断ID status = XScuGic_Connect(&gicInst, CPU1_HW_INT_ID, (Xil_InterruptHandler)hwIntrHandler, &gicInst); if(status != XST_SUCCESS) { printf("Connect GIC failed\n"); return XST_FAILURE; } //5. 设置硬中断优先级和中断类型 XScuGic_SetPriTrigTypeByDistAddr(DIST_BASE_ADDR, CPU1_HW_INT_ID, 0x20, 0x03); //6. 将硬中断绑定到CPU0(双使用双核时,需要对硬中断进行一次映射) XScuGic_InterruptMaptoCpu(&gicInst, 0x01, CPU1_HW_INT_ID); //7. 使能硬中断 XScuGic_Enable(&gicInst, CPU1_HW_INT_ID); //8. 使能异常处理系统 Xil_ExceptionEnable(); return status; } int main() { //初始化中断控制器 int status; status = initGic(); if(status != XST_SUCCESS) { printf("initialize GIC failed\n"); return XST_FAILURE; } while(1) { } return 0; } ```