ZYNQ多中断(定时器中断&PS_UART中断)的实现

  1. time.intr.c
#include <stdio.h>
#include "xadcps.h"
#include "xil_types.h"
#include "Xscugic.h" //该文件包含配置驱动程序以及GIC的使用范围
#include "Xil_exception.h"  //该文件包含Cortex-A9的异常函数
#include "xscutimer.h"

#define TIMER_DEVICE_ID XPAR_XSCUTIMER_0_DEVICE_ID//定义timer设备号
#define INTC_DEVICE_ID XPAR_SCUGIC_SINGLE_DEVICE_ID//定义中断控制器设备号(GIC中断控制器)
#define TIMER_IRPT_INTR XPAR_SCUTIMER_INTR//私有时钟中断
#define TIMER_LOAD_VALUE (767/2*1000*1000-1)//欲装载值

 static void TimerIntrHandler(void *CallBackRef);

 void SetupInterruptSystem(XScuGic *GicInstancePtr,XScuTimer *TimerInstancePtr, u16 TimerIntrId)
{
	XScuGic_Config *IntcConfig; //GIC config
	//初始化SOC异常
	Xil_ExceptionInit();
	//initialise the GIC
	IntcConfig = XScuGic_LookupConfig(INTC_DEVICE_ID);
	XScuGic_CfgInitialize(GicInstancePtr, IntcConfig, IntcConfig->CpuBaseAddress);
	//connect to the hardware
	Xil_ExceptionRegisterHandler(XIL_EXCEPTION_ID_INT,(Xil_ExceptionHandler) XScuGic_InterruptHandler,GicInstancePtr);
	//连接到中断处理硬件
	XScuGic_Connect(GicInstancePtr, TimerIntrId,(Xil_ExceptionHandler) TimerIntrHandler,(void *) TimerInstancePtr);
	//enable the interrupt for the Timer at GIC
	XScuGic_Enable(GicInstancePtr, TimerIntrId);
	//enable interrupt on the timer
	XScuTimer_EnableInterrupt(TimerInstancePtr);
	// Enable interrupts in the Processor.
	Xil_ExceptionEnableMask(XIL_EXCEPTION_IRQ);
}

static void TimerIntrHandler(void *CallBackRef)
{
	static int sec = 0; //计数
	XScuTimer *TimerInstancePtr = (XScuTimer *) CallBackRef;
	XScuTimer_ClearInterruptStatus(TimerInstancePtr);  //清除挂起的中断
	sec++;
	xil_printf(" %d Second\n\r", sec); //每秒打印输出一次
}

  1. time_intr.h
#ifndef SRC_TIME_INTR_H_
#define SRC_TIME_INTR_H_


#include "xscutimer.h"
#include "Xscugic.h" //该文件包含配置驱动程序以及GIC的使用范围
#include "Xil_exception.h"  //该文件包含Cortex-A9的异常函数

#define TIMER_DEVICE_ID XPAR_XSCUTIMER_0_DEVICE_ID//定义timer设备号
#define INTC_DEVICE_ID XPAR_SCUGIC_SINGLE_DEVICE_ID//定义中断控制器设备号(GIC中断控制器)
#define TIMER_IRPT_INTR XPAR_SCUTIMER_INTR//私有时钟中断
#define TIMER_LOAD_VALUE (767/2*1000*1000-1)//欲装载值




 void SetupInterruptSystem(XScuGic *GicInstancePtr, XScuTimer *TimerInstancePtr, u16 TimerIntrId);
#endif /* SRC_TIME_INTR_H_ */

  1. Ps_Rs232.c
#include "xparameters.h"
#include "stdio.h"
#include "xuartps.h"
#include "xuartps_hw.h"
#include "xscugic.h"
#define UART_0_DEVICE_ID XPAR_PS7_UART_1_DEVICE_ID
#define INTR_DEVICE_ID		XPAR_SCUGIC_SINGLE_DEVICE_ID
#define UART_INT_IRQ_ID		XPAR_XUARTPS_1_INTR
XUartPs Uart_Inst;
void UartIntr_Handler(void *call_back_ref);
//uart初始化
int uart_init(){
	XUartPs_Config *UartPs_Cfg;
	int Status;
	//查找配置信息
	UartPs_Cfg= XUartPs_LookupConfig(UART_0_DEVICE_ID);
	//对uart控制器进行初始化
	XUartPs_CfgInitialize(&Uart_Inst, UartPs_Cfg, UartPs_Cfg->BaseAddress);
	//检测硬件搭建是否正确
	Status = XUartPs_SelfTest(&Uart_Inst);
	if (Status != XST_SUCCESS) {
		return XST_FAILURE;
	}
	//设置波特率
	XUartPs_SetBaudRate(&Uart_Inst,115200);
	//设置RXFIFO触发阈值
	XUartPs_SetFifoThreshold(&Uart_Inst,1);
	//设置操作模式
	XUartPs_SetOperMode(&Uart_Inst, XUARTPS_OPER_MODE_NORMAL);

	return XST_SUCCESS;
}

//中断初始化
void intr_init(XScuGic *intr, XUartPs *uart){
	XScuGic_Config *IntcConfig;
	//中断控制器初始化
	IntcConfig = XScuGic_LookupConfig(INTR_DEVICE_ID);
	XScuGic_CfgInitialize(intr,IntcConfig,IntcConfig->CpuBaseAddress);
	Xil_ExceptionInit();
	Xil_ExceptionRegisterHandler(XIL_EXCEPTION_ID_INT,
					(Xil_ExceptionHandler) XScuGic_InterruptHandler,
					(void *)intr);
	Xil_ExceptionEnable();
	//为中断设置中断处理函数
	XScuGic_Connect(intr, UART_INT_IRQ_ID,
					  (Xil_ExceptionHandler) UartIntr_Handler,
					  (void *) uart);
	//设置触发类型
	XUartPs_SetInterruptMask(uart, XUARTPS_IXR_RXOVR);
	//使能中断
	XScuGic_Enable(intr, UART_INT_IRQ_ID);

}
//中断处理函数
void UartIntr_Handler(void *call_back_ref){
	XUartPs *uartinst =(XUartPs *)call_back_ref;
	u32 read_data = 0;
	u32 intr_status;
	//读取中断ID寄存器
	intr_status = XUartPs_ReadReg(uartinst->Config.BaseAddress,
			XUARTPS_IMR_OFFSET);//读取掩码
	intr_status &= XUartPs_ReadReg(uartinst->Config.BaseAddress,
			XUARTPS_ISR_OFFSET);//读取状态
	if(intr_status & (u32)XUARTPS_IXR_RXOVR){
		read_data = XUartPs_RecvByte(XPAR_PS7_UART_1_BASEADDR);//接收发送的字节
		XUartPs_WriteReg(uartinst->Config.BaseAddress,XUARTPS_ISR_OFFSET,
				XUARTPS_IXR_RXOVR);//清除中断状态
	}
	//设置发送
	XUartPs_SendByte(XPAR_PS7_UART_1_BASEADDR,read_data);
}



  1. Ps_Rs232.h
#ifndef SRC_PS_RS232_H_
#define SRC_PS_RS232_H_

#include "xparameters.h"
#include "stdio.h"
#include "xuartps.h"
#include "xuartps_hw.h"
#include "xscugic.h"





int uart_init();
void intr_init(XScuGic *intr, XUartPs *uart);
void UartIntr_Handler(void *call_back_ref);

#endif /* SRC_PS_RS232_H_ */

  1. main.c

#include "time_intr.h"
#include "Ps_Rs232.h"

void test_time_intr();
void uart_test();
static XScuGic   Intc; //中断
static XScuTimer Timer; //定时器
XUartPs Uart_Inst;

int main() {


	test_time_intr();
	uart_test();

	while (1);
	return 0;
}


void test_time_intr(){

	    XScuTimer_Config *TMRConfigPtr; //timer config
	 	xil_printf("------------Timer Test-------------\n");
	 	//私有定时器初始化
	 	TMRConfigPtr = XScuTimer_LookupConfig(TIMER_DEVICE_ID);
	 	XScuTimer_CfgInitialize(&Timer, TMRConfigPtr, TMRConfigPtr->BaseAddr);
	 	XScuTimer_SelfTest(&Timer);
	 	//加载计数周期,私有定时器的时钟为CPU的一半
	 	XScuTimer_LoadTimer(&Timer, TIMER_LOAD_VALUE);
	 	//自动装载
	 	XScuTimer_EnableAutoReload(&Timer);
	 	//启动定时器
	 	XScuTimer_Start(&Timer);
	 	//set up the interrupts
	 	SetupInterruptSystem(&Intc, &Timer, TIMER_IRPT_INTR);

 }

void uart_test(){

	    //uart初试化函数
		uart_init();
		xil_printf("intr\n");
		//中断初始化
		intr_init(&Intc,&Uart_Inst);


}

  • 6
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一个会飞的小苏

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值