zynq定时器中断linux,ZYNQ7000开发板(zedboard)定时器中断实验——LED闪烁

#include "xparameters.h"

#include "xgpiops.h"

#include "xstatus.h"

#include "xplatform_info.h"

#include #include "sleep.h"

#include "xscutimer.h"

#include "xscugic.h"

#define GPIO_DEVICE_IDXPAR_XGPIOPS_0_DEVICE_ID

#define MIO7_LED 7 //ps端LED

#define LED_DELAY10000000

#define TIMER_DEVICE_IDXPAR_XSCUTIMER_0_DEVICE_ID

#define TIMER_IRPT_INTRXPAR_SCUTIMER_INTR

#define TIMER_LOAD_VALUE0x3F83C3F //0.2s 闪烁一次,0.2*1000_000_000/(1000/333) - 1 = 3F83C3F

#define INTC_DEVICE_IDXPAR_SCUGIC_SINGLE_DEVICE_ID

XGpioPs Gpio;/* The driver instance for GPIO Device. */

XScuTimer TimerInstance;

XScuGic IntcInstance;

//中断处理函数

void TimerIntrHandler(void *CallBackRef)

{

//LED 状态,用于控制 LED 灯状态翻转

static int led_state = 0;

XScuTimer *timer_ptr = (XScuTimer *) CallBackRef;

if(led_state == 0)

led_state = 1;

else

led_state = 0;

//向指定引脚写入数据: 0 或 1

XGpioPs_WritePin(&Gpio, MIO7_LED,led_state);

//清除定时器中断标志

XScuTimer_ClearInterruptStatus(timer_ptr);

}

//mio初始化

void mio_init()

{

XGpioPs_Config *ConfigPtr;

//初始化gpio驱动

//根据器件id,查找器件配置信息

ConfigPtr = XGpioPs_LookupConfig(GPIO_DEVICE_ID);

//初始化gpio参数

XGpioPs_CfgInitialize(&Gpio, ConfigPtr,

ConfigPtr->BaseAddr);

//gpio方向设置为输出(0:输入、1:输出)

XGpioPs_SetDirectionPin(&Gpio, MIO7_LED, 1);

//XGpioPs_SetDirectionPin(&Gpio, 50, 0);

//设置输出使能(0:关闭、1:打开)

XGpioPs_SetOutputEnablePin(&Gpio, MIO7_LED, 1);

//XGpioPs_WritePin(&Gpio, MIO7_LED, 0x0);

}

//定时器中断初始化

void TimerSetupIntrSystem(XScuGic *IntcInstancePtr, XScuTimer *TimerInstancePtr, u16 TimerIntrId)

{

XScuGic_Config *IntcConfig;

//定时器中断初始化

IntcConfig = XScuGic_LookupConfig(INTC_DEVICE_ID);

XScuGic_CfgInitialize(IntcInstancePtr, IntcConfig,

IntcConfig->CpuBaseAddress);

Xil_ExceptionInit();

//关联中断函数

Xil_ExceptionRegisterHandler(XIL_EXCEPTION_ID_IRQ_INT,

(Xil_ExceptionHandler)XScuGic_InterruptHandler,

IntcInstancePtr);

//关联中断函数

XScuGic_Connect(IntcInstancePtr, TimerIntrId,

(Xil_ExceptionHandler)TimerIntrHandler,

(void *)TimerInstancePtr);

//使能GIC定时器中断

XScuGic_Enable(IntcInstancePtr, TimerIntrId);

//使能定时器中断

XScuTimer_EnableInterrupt(TimerInstancePtr);

Xil_ExceptionEnable();

}

// 定时器初始化

int timer_init(XScuGic *IntcInstancePtr, XScuTimer * TimerInstancePtr, u16 TimerDeviceId, u16 TimerIntrId)

{

int Status;

//int LastTimerExpired = 0;

XScuTimer_Config *ConfigPtr;

//初始化私有定时器

ConfigPtr = XScuTimer_LookupConfig(TimerDeviceId);

XScuTimer_CfgInitialize(TimerInstancePtr, ConfigPtr,

ConfigPtr->BaseAddr);

//硬件自测

Status = XScuTimer_SelfTest(TimerInstancePtr);

if (Status != XST_SUCCESS) {

return XST_FAILURE;

}

//设置中断系统

TimerSetupIntrSystem(IntcInstancePtr,

TimerInstancePtr, TimerIntrId);

//使能自动重下载模式

XScuTimer_EnableAutoReload(TimerInstancePtr);

//加载定时器计数器

XScuTimer_LoadTimer(TimerInstancePtr, TIMER_LOAD_VALUE);

//开始定时器计数

XScuTimer_Start(TimerInstancePtr);

return 0;

}

int main()

{

//u32 Data;

printf("***hello,hello,hello,hello,hello,hello***\r\n");

mio_init();

timer_init(&IntcInstance, &TimerInstance,TIMER_DEVICE_ID, TIMER_IRPT_INTR);

XScuTimer_Start(&TimerInstance); //启动定时器

return 0;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值