Timer1のみのコード
#include <stdio.h> #include "platform.h" #include "xil_types.h" #include "xtmrctr.h" #include "xparameters.h" #include "xil_io.h" #include "xil_exception.h" #include "xscugic.h" #include <stdbool.h> XScuGic InterruptController; /* Instance of the Interrupt Controller */ static XScuGic_Config *GicConfig;/* The configuration parameters of the controller */ void Timer_InterruptHandler(void *data, u8 TmrCtrNumber) { print(" Interrupt acknowledged\n\r"); XTmrCtr_Stop(data,TmrCtrNumber); XTmrCtr_Reset(data,TmrCtrNumber); XTmrCtr_Start(data,TmrCtrNumber); } int SetUpInterruptSystem(XScuGic *XScuGicInstancePtr) { Xil_ExceptionRegisterHandler(XIL_EXCEPTION_ID_INT, (Xil_ExceptionHandler) XScuGic_InterruptHandler, XScuGicInstancePtr); Xil_ExceptionEnable(); return XST_SUCCESS; } int ScuGicInterrupt_Init(u16 DeviceId,XTmrCtr *TimerInstancePtr) { int Status; GicConfig = XScuGic_LookupConfig(DeviceId); if (NULL == GicConfig) { return XST_FAILURE; } Status = XScuGic_CfgInitialize(&InterruptController, GicConfig, GicConfig->CpuBaseAddress); if (Status != XST_SUCCESS) { return XST_FAILURE; } Status = SetUpInterruptSystem(&InterruptController); if (Status != XST_SUCCESS) { return XST_FAILURE; } Status = XScuGic_Connect(&InterruptController, XPAR_FABRIC_AXI_TIMER_0_INTERRUPT_INTR, (Xil_ExceptionHandler)XTmrCtr_InterruptHandler, (void *)TimerInstancePtr); if (Status != XST_SUCCESS) { return XST_FAILURE; } XScuGic_Enable(&InterruptController, XPAR_FABRIC_AXI_TIMER_0_INTERRUPT_INTR); return XST_SUCCESS; } int main() { XTmrCtr TimerInstancePtr; int xStatus; print("##### Application Starts #####\n\r"); print("\r\n"); xStatus = XTmrCtr_Initialize(&TimerInstancePtr,XPAR_AXI_TIMER_0_DEVICE_ID); if(XST_SUCCESS != xStatus) print("TIMER INIT FAILED \n\r"); XTmrCtr_SetHandler(&TimerInstancePtr, Timer_InterruptHandler, &TimerInstancePtr); XTmrCtr_SetResetValue(&TimerInstancePtr, 0, //Change with generic value 0xf8000000); XTmrCtr_SetOptions(&TimerInstancePtr, XPAR_AXI_TIMER_0_DEVICE_ID, (XTC_INT_MODE_OPTION | XTC_AUTO_RELOAD_OPTION )); xStatus=ScuGicInterrupt_Init(XPAR_PS7_SCUGIC_0_DEVICE_ID,&TimerInstancePtr); if(XST_SUCCESS != xStatus) print(" :( SCUGIC INIT FAILED \n\r"); XTmrCtr_Start(&TimerInstancePtr,0); print("timer start \n\r"); print("Wait for the Timer interrupt to tigger \r\n"); print("########################################\r\n"); print(" \r\n"); while(1) { } cleanup_platform(); return 0; }
Timer2のみのコード
変更箇所
1. XTmrCtr_SetResetValue
XTmrCtr_SetResetValue(&TimerInstancePtr, 1, //Change with generic value 0xf8000000);
の1はTimer2のこと(Timer1: 0, Timer2: 1)
2. XTmrCtr_Start
XTmrCtr_Start(&TimerInstancePtr,1);
の1は同じくTimer2のこと
#include <stdio.h> #include "platform.h" #include "xil_types.h" #include "xtmrctr.h" #include "xparameters.h" #include "xil_io.h" #include "xil_exception.h" #include "xscugic.h" #include <stdbool.h> XScuGic InterruptController; /* Instance of the Interrupt Controller */ static XScuGic_Config *GicConfig;/* The configuration parameters of the controller */ void Timer_InterruptHandler(void *data, u8 TmrCtrNumber) { print(" Interrupt acknowledged\n\r"); XTmrCtr_Stop(data,TmrCtrNumber); XTmrCtr_Reset(data,TmrCtrNumber); XTmrCtr_Start(data,TmrCtrNumber); } int SetUpInterruptSystem(XScuGic *XScuGicInstancePtr) { Xil_ExceptionRegisterHandler(XIL_EXCEPTION_ID_INT, (Xil_ExceptionHandler) XScuGic_InterruptHandler, XScuGicInstancePtr); Xil_ExceptionEnable(); return XST_SUCCESS; } int ScuGicInterrupt_Init(u16 DeviceId,XTmrCtr *TimerInstancePtr) { int Status; GicConfig = XScuGic_LookupConfig(DeviceId); if (NULL == GicConfig) { return XST_FAILURE; } Status = XScuGic_CfgInitialize(&InterruptController, GicConfig, GicConfig->CpuBaseAddress); if (Status != XST_SUCCESS) { return XST_FAILURE; } Status = SetUpInterruptSystem(&InterruptController); if (Status != XST_SUCCESS) { return XST_FAILURE; } Status = XScuGic_Connect(&InterruptController, XPAR_FABRIC_AXI_TIMER_1_INTERRUPT_INTR, (Xil_ExceptionHandler)XTmrCtr_InterruptHandler, (void *)TimerInstancePtr); if (Status != XST_SUCCESS) { return XST_FAILURE; } XScuGic_Enable(&InterruptController, XPAR_FABRIC_AXI_TIMER_1_INTERRUPT_INTR); return XST_SUCCESS; } int main() { XTmrCtr TimerInstancePtr; int xStatus; print("##### Application Starts #####\n\r"); print("\r\n"); xStatus = XTmrCtr_Initialize(&TimerInstancePtr,XPAR_AXI_TIMER_1_DEVICE_ID); if(XST_SUCCESS != xStatus) print("TIMER INIT FAILED \n\r"); XTmrCtr_SetHandler(&TimerInstancePtr, Timer_InterruptHandler, &TimerInstancePtr); XTmrCtr_SetResetValue(&TimerInstancePtr, 1, //Change with generic value 0xf8000000); XTmrCtr_SetOptions(&TimerInstancePtr, XPAR_AXI_TIMER_1_DEVICE_ID, (XTC_INT_MODE_OPTION | XTC_AUTO_RELOAD_OPTION )); xStatus=ScuGicInterrupt_Init(XPAR_PS7_SCUGIC_0_DEVICE_ID,&TimerInstancePtr); if(XST_SUCCESS != xStatus) print(" :( SCUGIC INIT FAILED \n\r"); XTmrCtr_Start(&TimerInstancePtr,1); print("timer start \n\r"); print("Wait for the Timer interrupt to tigger \r\n"); print("########################################\r\n"); print(" \r\n"); while(1) { } cleanup_platform(); return 0; }
両者を共存させたコード
ポイント:SCU-GICに接続するInterruptControllerは1つ。TimerInstancePtrはそれぞれのタイマーに対して2つ用意する。
#include <stdio.h> #include "platform.h" #include "xil_types.h" #include "xtmrctr.h" #include "xparameters.h" #include "xil_io.h" #include "xil_exception.h" #include "xscugic.h" #include <stdbool.h> XScuGic InterruptController; /* Instance of the Interrupt Controller */ XTmrCtr TimerInstancePtr; XTmrCtr TimerInstancePtr2; static XScuGic_Config *GicConfig;/* The configuration parameters of the controller */ void Timer_InterruptHandler(void *data, u8 TmrCtrNumber) { print(" Interrupt acknowledged\n\r"); } void Timer_InterruptHandler2(void *data, u8 TmrCtrNumber) { print(" Interrupt 2 acknowledged\n\r"); } int SetUpInterruptSystem(XScuGic *XScuGicInstancePtr) { Xil_ExceptionRegisterHandler(XIL_EXCEPTION_ID_INT, (Xil_ExceptionHandler) XScuGic_InterruptHandler, XScuGicInstancePtr); Xil_ExceptionEnable(); return XST_SUCCESS; } int ScuGicInterrupt_Init(u16 DeviceId,XTmrCtr *TimerInstancePtr,XTmrCtr *TimerInstancePtr2) { int Status; GicConfig = XScuGic_LookupConfig(DeviceId); if (NULL == GicConfig) { return XST_FAILURE; } Status = XScuGic_CfgInitialize(&InterruptController, GicConfig, GicConfig->CpuBaseAddress); if (Status != XST_SUCCESS) { return XST_FAILURE; } Status = SetUpInterruptSystem(&InterruptController); if (Status != XST_SUCCESS) { return XST_FAILURE; } Status = XScuGic_Connect(&InterruptController, XPAR_FABRIC_AXI_TIMER_0_INTERRUPT_INTR, (Xil_ExceptionHandler)XTmrCtr_InterruptHandler, (void *)TimerInstancePtr); if (Status != XST_SUCCESS) { return XST_FAILURE; } Status = XScuGic_Connect(&InterruptController, XPAR_FABRIC_AXI_TIMER_1_INTERRUPT_INTR, (Xil_ExceptionHandler)XTmrCtr_InterruptHandler, (void *)TimerInstancePtr2); if (Status != XST_SUCCESS) { return XST_FAILURE; } XScuGic_Enable(&InterruptController, XPAR_FABRIC_AXI_TIMER_0_INTERRUPT_INTR); XScuGic_Enable(&InterruptController, XPAR_FABRIC_AXI_TIMER_1_INTERRUPT_INTR); return XST_SUCCESS; } int main() { int xStatus; print("##### Application Starts #####\n\r"); print("\r\n"); xStatus = XTmrCtr_Initialize(&TimerInstancePtr,XPAR_AXI_TIMER_0_DEVICE_ID); if(XST_SUCCESS != xStatus) print("TIMER INIT FAILED \n\r"); xStatus = XTmrCtr_Initialize(&TimerInstancePtr2,XPAR_AXI_TIMER_1_DEVICE_ID); if(XST_SUCCESS != xStatus) print("TIMER INIT FAILED \n\r"); XTmrCtr_SetHandler(&TimerInstancePtr, Timer_InterruptHandler, &TimerInstancePtr); XTmrCtr_SetHandler(&TimerInstancePtr2, Timer_InterruptHandler2, &TimerInstancePtr2); XTmrCtr_SetResetValue(&TimerInstancePtr, 0, //Change with generic value 0xf8000000); XTmrCtr_SetResetValue(&TimerInstancePtr2, 1, //Change with generic value 0xf8000000); XTmrCtr_SetOptions(&TimerInstancePtr, XPAR_AXI_TIMER_0_DEVICE_ID, (XTC_INT_MODE_OPTION | XTC_AUTO_RELOAD_OPTION )); XTmrCtr_SetOptions(&TimerInstancePtr2, XPAR_AXI_TIMER_1_DEVICE_ID, (XTC_INT_MODE_OPTION | XTC_AUTO_RELOAD_OPTION )); xStatus=ScuGicInterrupt_Init(XPAR_PS7_SCUGIC_0_DEVICE_ID,&TimerInstancePtr, &TimerInstancePtr2); if(XST_SUCCESS != xStatus) print(" :( SCUGIC INIT FAILED \n\r"); XTmrCtr_Start(&TimerInstancePtr,0); print("timer start \n\r"); XTmrCtr_Start(&TimerInstancePtr2,1); print("timer2 start \n\r"); print("Wait for the Timer interrupt to tigger \r\n"); print("########################################\r\n"); print(" \r\n"); while(1) { } cleanup_platform(); return 0; }