1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
| #include<iostream> #include<xparameters.h> #include<xgpiops.h> #include<xscutimer.h> #include<xscugic.h>
using namespace std;
#define GPIO_DEVICE_ID XPAR_XGPIOPS_0_DEVICE_ID #define EMIO54_LED0 54 XGpioPs Gpio;
#define TIMER_DEVICE_ID XPAR_XSCUTIMER_0_DEVICE_ID #define TIMER_IRPT_INTR XPAR_SCUTIMER_INTR #define TIMER_LOAD_VALUE 0x135EFB82
XScuTimer TimerInstance;
#define INTC_DEVICE_ID XPAR_SCUGIC_0_DEVICE_ID XScuGic IntcInstance;
void gpio_init(void); void timer_init(XScuTimer *TimerInstancePtr,u16 TimerDeviceId); void timer_intr_init(XScuGic *IntcInstancePtr,XScuTimer *TimerInstancePtr,u16 TimerIntrId); void timer_intr_handler(void *CallBackRef);
int main(void) { gpio_init(); timer_init(&TimerInstance, TIMER_DEVICE_ID); timer_intr_init(&IntcInstance, &TimerInstance, TIMER_IRPT_INTR); XScuTimer_Start(&TimerInstance); while(1); return 0; }
void gpio_init(void) { XGpioPs_Config * ConfigPtr;
ConfigPtr = XGpioPs_LookupConfig(GPIO_DEVICE_ID); XGpioPs_CfgInitialize(&Gpio, ConfigPtr, ConfigPtr->BaseAddr);
XGpioPs_SetDirectionPin(&Gpio,EMIO54_LED0, 1);
XGpioPs_SetOutputEnablePin(&Gpio, EMIO54_LED0, 1);
}
void timer_init(XScuTimer *TimerInstancePtr,u16 TimerDeviceId) { XScuTimer_Config *ConfigPtr;
ConfigPtr = XScuTimer_LookupConfig(TimerDeviceId); XScuTimer_CfgInitialize(TimerInstancePtr, ConfigPtr, ConfigPtr->BaseAddr);
XScuTimer_LoadTimer(TimerInstancePtr, TIMER_LOAD_VALUE);
XScuTimer_EnableAutoReload(TimerInstancePtr); }
void timer_intr_init(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); Xil_ExceptionEnable();
XScuGic_Connect(IntcInstancePtr, TimerIntrId, (Xil_ExceptionHandler)timer_intr_handler, (void *)TimerInstancePtr);
XScuGic_Enable(IntcInstancePtr, TimerIntrId);
XScuTimer_EnableInterrupt(TimerInstancePtr);
}
void timer_intr_handler(void *CallBackRef) { static int led_status = 1; XScuTimer *TimerInstancePtr = (XScuTimer *) CallBackRef; if(XScuTimer_IsExpired(TimerInstancePtr)) { XScuTimer_ClearInterruptStatus(TimerInstancePtr); XGpioPs_WritePin(&Gpio, EMIO54_LED0, led_status); led_status = ~led_status; }
}
|