ZYNQ. Interrupt(1)Private Timer

时间:2023-03-09 18:03:06
ZYNQ. Interrupt(1)Private Timer

Interrupt

zynq的中断。

The PS is based on ARM architecture, utilizing two Cortex-A9 processors(CPUs) and the GIC pl390 interrupt controller.

ZYNQ. Interrupt(1)Private Timer

Each CPU has a set of private peripheral interrupts (PPIs).

The PPIs include the global timer, private watchdog timer, private timer, and FIQ/IRQ from the PL.

The SGIs are generated by writing to the registers in the generic interrupt controller (GIC)

The shared peripheral interrupts (SPIs) are generated by the various I/O and memory controllers in the PS and PL.

Software Generated Interrupts (SGI)

软中断

ZYNQ. Interrupt(1)Private Timer

CPU Private Peripheral Interrupt (PPI)

私有外设中断包括有:全局定时器、私有定时器,私有看门狗定时器以及来自PL的FIQ/IRQ。

FIQ和IRQ是反向的,然后送到中断控制器(GIC) ,他们在PS-PL接口中为高电平触发,尽管在ICDICFR1寄存器中反映为低电平触发。

ZYNQ. Interrupt(1)Private Timer

Note that the fast interrupt (FIQ) signal and the interrupt (IRQ) signal from the PL are inverted and then sent to the interrupt
controller. Therefore, they are active High at the PS-PL interface, although the ICDICFR1 register reflects them as active Low level.

Shared Peripheral Interrupts (SPI)

一组大约60个中断,来自各个模块。

ZYNQ. Interrupt(1)Private Timer

ZYNQ. Interrupt(1)Private Timer

CPU Private Timer

特性:

  • 32位计数器,到零产生中断
  • 8位预分频器,能够好的控制中断周期
  • 可配置单次或自动重载模式
  • 可配置计数器初始值

私有中断硬件系统

只要添加一个ZYNQ核就可以使用。

软件部分

xilinx sdk 提供了两个库函数:

  • 定时器  xscutimer.h
  • 中断     xscugic.h

非中断方式使用私有定时器

 #include <stdio.h>
#include "xscutimer.h"
#include "xparameters.h" //1s
#define loadValue XPAR_PS7_CORTEXA9_0_CPU_CLK_FREQ_HZ / 2 -1 int main()
{
int Status;
int timerCounterValue;
int counter = ;
//--------------------------------------------
//instance Timer & TimerConfig
XScuTimer PriTimer;
XScuTimer_Config *Timer_Config;
//--------------------------------------------
//LookupConfig
Timer_Config = XScuTimer_LookupConfig(XPAR_PS7_SCUTIMER_0_DEVICE_ID);
//initialize Timer
Status = XScuTimer_CfgInitialize(&PriTimer, Timer_Config,
Timer_Config->BaseAddr);
//set counter value
XScuTimer_LoadTimer(&PriTimer, loadValue);
//Disable Auto Reload
XScuTimer_DisableAutoReload(&PriTimer);
//start
XScuTimer_Start(&PriTimer);
//-----------------------------------------------------------------------------
while(counter<=)
{
//get counter value
timerCounterValue = XScuTimer_GetCounterValue(&PriTimer);
if (timerCounterValue == )
{
//restart
XScuTimer_RestartTimer(&PriTimer);
xil_printf("Timer has reached zero %d times\n\r", counter++);
}
else
{
// xil_printf("Timer is still running (Timer value = %d)\n\r",
// timerCounterValue);
}
}
return ;
}

中断方式使用私有定时器

在sdk中有例程可以参考。

大概流程是:

  1. 定义私有定时器和中断控制器的结构体
  2. 初始化私有定时器和终端控制器,以及私有定时器自检
  3. ARM异常处理初始化,连接系统中断处理程序
  4. 连接私有定时器中断程序
  5. 使能GIC、使能定时器中断、使能ARM中断
  6. 配置重载、计数器初值,然后开始定时器,等中断
  7. 废弃这个定时器,通知处理器、通知GIC废弃

code:

 #include <stdio.h>
#include "xscutimer.h"
#include "xparameters.h"
#include "xscugic.h" #define INTRCOUNTER 10 // -------------- function Prototypes -------------------------
static void TimerInterruptHandler(void *CallBackRef);
//--------------------------------------------------------- int InterruptCounter = ; int main()
{
int Status;
//Structure Definition
XScuTimer TimerInstance;
XScuTimer_Config *TimerConfigPtr;
XScuGic GicInstance;
XScuGic_Config *GicConfigPtr; //initialize
GicConfigPtr = XScuGic_LookupConfig(XPAR_PS7_SCUGIC_0_DEVICE_ID);
Status = XScuGic_CfgInitialize(&GicInstance, GicConfigPtr,
GicConfigPtr->CpuBaseAddress);
TimerConfigPtr = XScuTimer_LookupConfig(XPAR_PS7_SCUTIMER_0_DEVICE_ID);
Status = XScuTimer_CfgInitialize(&TimerInstance, TimerConfigPtr,
TimerConfigPtr->BaseAddr);
//self-test
// Status = XScuTimer_SelfTest(TimerInstance);
// if (Status != XST_SUCCESS) {
// return XST_FAILURE;
// } //initialize & Connect Interrupt Controller
Xil_ExceptionInit();
Xil_ExceptionRegisterHandler(XIL_EXCEPTION_ID_IRQ_INT,
(Xil_ExceptionHandler)XScuGic_InterruptHandler, &GicInstance); //Connect Private Timer Interrupt Handler
Status = XScuGic_Connect(&GicInstance, XPAR_SCUTIMER_INTR,
(Xil_ExceptionHandler)TimerInterruptHandler, (void *)&TimerInstance); // Enable the interrupt for the device
XScuGic_Enable(&GicInstance, XPAR_SCUTIMER_INTR);
// Enable the timer interrupts for timer mode
XScuTimer_EnableInterrupt(&TimerInstance);
// Enable interrupts in the Processor,Enable the IRQ exception
Xil_ExceptionEnable(); //Load the timer counter register.
XScuTimer_LoadTimer(&TimerInstance, XPAR_PS7_CORTEXA9_0_CPU_CLK_FREQ_HZ / );
//Enable Auto Reload
XScuTimer_EnableAutoReload(&TimerInstance);
//Start the timer counter and then wait for it to timeout a number of times.
XScuTimer_Start(&TimerInstance); while()
{
/*
* Wait for the first timer counter to expire as indicated by
* the shared variable which the handler will increment.
*/
if (InterruptCounter >= INTRCOUNTER)
{
break;
}
} xil_printf("break loop\n\r"); //Disable the IRQ exception
Xil_ExceptionDisable();
//Disconnect and disable the interrupt for the Timer.
XScuGic_Disconnect(&GicInstance, XPAR_SCUTIMER_INTR); return ;
} static void TimerInterruptHandler(void *CallBackRef)
{
XScuTimer *TimerIntancePtr = (XScuTimer *) CallBackRef; if (XScuTimer_IsExpired(TimerIntancePtr))
{
// Clear the interrupt flag in the timer
XScuTimer_ClearInterruptStatus(TimerIntancePtr);
xil_printf("count %d times\n\r",InterruptCounter++); if (InterruptCounter >= INTRCOUNTER)
{
XScuTimer_DisableAutoReload(TimerIntancePtr);
}
}
}

//**************2018/11/15****************************

封装

 #include <stdio.h>
#include "xscutimer.h"
#include "xparameters.h"
#include "xscugic.h" //timer info
#define TIMER_DEVICE_ID XPAR_PS7_SCUTIMER_0_DEVICE_ID
#define INTC_DEVICE_ID XPAR_PS7_SCUGIC_0_DEVICE_ID
#define INTRCOUNTER 10
#define TIMER_IRPT_INTR XPAR_SCUTIMER_INTR
#define TIMERLOADVALUE XPAR_PS7_CORTEXA9_0_CPU_CLK_FREQ_HZ / 2 -1 static XScuGic Intc;
static XScuTimer Timer;
static int IntrCounter=; // -------------- function Prototypes -------------------------
static void TimerIntrHandler(void *CallBackRef);
void SetupTimerIntrSys(XScuGic *GicInstancePtr,
XScuTimer *TimerInstancePtr);
void DisableTimerIntrSys(XScuGic *IntcInstancePtr,
u16 TimerIntrId);
//--------------------------------------------------------- int main()
{
xil_printf("--- start ----\n"); //setup the timer interrupt
SetupTimerIntrSys(&Intc, &Timer); //Load the timer counter register.
XScuTimer_LoadTimer(&Timer, TIMERLOADVALUE);
//Enable Auto Reload
XScuTimer_EnableAutoReload(&Timer);
//Start the timer counter
XScuTimer_Start(&Timer); while(){
if (IntrCounter == INTRCOUNTER) {
XScuTimer_Stop(&Timer);
break;
}
}; xil_printf("break loop\n\r"); DisableTimerIntrSys(&Intc, TIMER_IRPT_INTR);
xil_printf("Disabled Timer Interrupt\n\r"); return ;
} static void TimerIntrHandler(void *CallBackRef)
{
XScuTimer *TimerIntancePtr = (XScuTimer *) CallBackRef; if (XScuTimer_IsExpired(TimerIntancePtr))
{
// Clear the interrupt flag in the timer
XScuTimer_ClearInterruptStatus(TimerIntancePtr);
xil_printf("count %d times\n\r",IntrCounter++); if (IntrCounter >= INTRCOUNTER)
{
XScuTimer_DisableAutoReload(TimerIntancePtr);
}
}
} void SetupTimerIntrSys(XScuGic *GicInstancePtr,
XScuTimer *TimerInstancePtr)
{
int Status;
XScuTimer_Config *TimerConfigPtr;
XScuGic_Config *GicConfigPtr; //initialize,get config
TimerConfigPtr = XScuTimer_LookupConfig(TIMER_DEVICE_ID);
Status = XScuTimer_CfgInitialize(TimerInstancePtr, TimerConfigPtr,
TimerConfigPtr->BaseAddr);
GicConfigPtr = XScuGic_LookupConfig(INTC_DEVICE_ID);
Status = XScuGic_CfgInitialize(GicInstancePtr, GicConfigPtr,
GicConfigPtr->CpuBaseAddress); //initialize & Connect Interrupt Controller
Xil_ExceptionInit();
Xil_ExceptionRegisterHandler(XIL_EXCEPTION_ID_IRQ_INT,
(Xil_ExceptionHandler)XScuGic_InterruptHandler,
GicInstancePtr); //Connect Private Timer Interrupt Handler
Status = XScuGic_Connect(GicInstancePtr, TIMER_IRPT_INTR,
(Xil_ExceptionHandler)TimerIntrHandler,
(void *)TimerInstancePtr); // Enable the interrupt for the device
XScuGic_Enable(GicInstancePtr, TIMER_IRPT_INTR);
// Enable the timer interrupts for timer mode
XScuTimer_EnableInterrupt(TimerInstancePtr);
// Enable interrupts in the Processor,Enable the IRQ exception
Xil_ExceptionEnable(); } void DisableTimerIntrSys(XScuGic *IntcInstancePtr, u16 TimerIntrId)
{
//Disable the IRQ exception
Xil_ExceptionDisable();
// Disconnect and disable the interrupt for the Timer.
XScuGic_Disconnect(IntcInstancePtr, TimerIntrId);
}

via

ug585 -- ch.7 Interrupts ch.8 Timers

https://blog.****.net/u014485485/article/details/79060978