embeddedsw/XilinxProcessorIPLib/drivers/bram/examples/xbram_intr_example.c
git perforce import user a539aa2c1a Initial import of //Rodin/HEAD/data/embeddedsw/ from the state at revision #head
[git-p4: depot-paths = "//Rodin/HEAD/data/embeddedsw/": change = 884492]
2014-04-09 17:06:57 +05:30

431 lines
13 KiB
C
Executable file

/******************************************************************************
*
* (c) Copyright 2010-2013 Xilinx, Inc. All rights reserved.
*
* This file contains confidential and proprietary information of Xilinx, Inc.
* and is protected under U.S. and international copyright and other
* intellectual property laws.
*
* DISCLAIMER
* This disclaimer is not a license and does not grant any rights to the
* materials distributed herewith. Except as otherwise provided in a valid
* license issued to you by Xilinx, and to the maximum extent permitted by
* applicable law: (1) THESE MATERIALS ARE MADE AVAILABLE "AS IS" AND WITH ALL
* FAULTS, AND XILINX HEREBY DISCLAIMS ALL WARRANTIES AND CONDITIONS, EXPRESS,
* IMPLIED, OR STATUTORY, INCLUDING BUT NOT LIMITED TO WARRANTIES OF
* MERCHANTABILITY, NON-INFRINGEMENT, OR FITNESS FOR ANY PARTICULAR PURPOSE;
* and (2) Xilinx shall not be liable (whether in contract or tort, including
* negligence, or under any other theory of liability) for any loss or damage
* of any kind or nature related to, arising under or in connection with these
* materials, including for any direct, or any indirect, special, incidental,
* or consequential loss or damage (including loss of data, profits, goodwill,
* or any type of loss or damage suffered as a result of any action brought by
* a third party) even if such damage or loss was reasonably foreseeable or
* Xilinx had been advised of the possibility of the same.
*
* CRITICAL APPLICATIONS
* Xilinx products are not designed or intended to be fail-safe, or for use in
* any application requiring fail-safe performance, such as life-support or
* safety devices or systems, Class III medical devices, nuclear facilities,
* applications related to the deployment of airbags, or any other applications
* that could lead to death, personal injury, or severe property or
* environmental damage (individually and collectively, "Critical
* Applications"). Customer assumes the sole risk and liability of any use of
* Xilinx products in Critical Applications, subject only to applicable laws
* and regulations governing limitations on product liability.
*
* THIS COPYRIGHT NOTICE AND DISCLAIMER MUST BE RETAINED AS PART OF THIS FILE
* AT ALL TIMES.
*
******************************************************************************/
/*****************************************************************************/
/**
* @file xbram_intr_example.c
*
* This file contains a design example using the BRAM driver
* (XBram) in an interrupt driven mode of operation. This example assumes
* that there is an interrupt controller in the hardware system and the
* BRAM device is connected to the interrupt controller.
*
*
* <pre>
* MODIFICATION HISTORY:
*
* Ver Who Date Changes
* ----- ---- -------- -------------------------------------------------------
* 1.00a sa 05/11/10 Initial release.
* 3.01a sa 13/01/12 Changed XBram_SelfTest(InstancePtr) to
* XBram_SelfTest(InstancePtr, XBRAM_IR_ALL_MASK)
* as per new API (CR 639274)
*</pre>
*
******************************************************************************/
/***************************** Include Files *********************************/
#include "xparameters.h"
#include "xbram.h"
#include "xil_exception.h"
#include "xintc.h"
#include "xil_cache.h"
#include <stdio.h>
/************************** Constant Definitions *****************************/
/*
* The following constants map to the XPAR parameters created in the
* xparameters.h file. They are defined here such that a user can easily
* change all the needed parameters in one place.
*/
#define BRAM_DEVICE_ID XPAR_BRAM_1_DEVICE_ID
#define INTC_DEVICE_ID XPAR_INTC_0_DEVICE_ID
#define INTC_BRAM_INTERRUPT_ID XPAR_INTC_0_BRAM_1_VEC_ID
/************************** Function Prototypes ******************************/
static void BramIntrExceptionHandler(void *InstancePtr);
static void BramDriverHandler(void *CallBackRef);
int BramIntrExample(XIntc* IntcInstancePtr, XBram* InstancePtr,
u16 DeviceId, u16 IntrId);
static void InitializeECC(XBram_Config *ConfigPtr, u32 EffectiveAddr);
static int BramSetupIntrSystem(XIntc* IntcInstancePtr, XBram* InstancePtr,
u16 DeviceId, u16 IntrId);
static void BramDisableIntr(XIntc* IntcInstancePtr, XBram* InstancePtr,
u16 IntrId);
/************************** Variable Definitions *****************************/
/*
* The following are declared globally so they are zeroed and so they are
* easily accessible from a debugger
*/
XBram Bram; /* The Instance of the BRAM Driver */
XIntc Intc; /* The Instance of the Interrupt Controller Driver */
static volatile u32 IntrCount; /* Total number of interrupts */
static volatile int ExceptionCount; /* Total number of exceptions */
/****************************************************************************/
/**
* This function is the main function of the BRAM example. It is
* for initializing the BRAM device and setting up interrupts.
*
* @param None.
*
* @return
* - XST_SUCCESS to indicate success.
* - XST_FAILURE to indicate failure.
*
* @note None.
*
*****************************************************************************/
#ifndef TESTAPP_GEN
int main(void)
{
int Status;
Status = BramIntrExample(&Intc, &Bram, BRAM_DEVICE_ID,
INTC_BRAM_INTERRUPT_ID);
if (Status != XST_SUCCESS ) {
xil_printf("Bram Interrupt Example FAILED.\r\n");
return XST_FAILURE;
}
xil_printf("Bram Interrupt Example PASSED.\r\n");
return XST_SUCCESS;
}
#endif
/*****************************************************************************/
/**
*
* This is the entry function from the TestAppGen tool generated application
* which tests the interrupts when enabled in the BRAM
*
* @param IntcInstancePtr is a reference to the Interrupt Controller
* driver Instance
* @param InstancePtr is a reference to the BRAM driver Instance
* @param DeviceId is the XPAR_<BRAM_instance>_DEVICE_ID
* value from xparameters.h
* @param IntrId is XPAR_<INTC_instance>_<BRAM_instance>_VEC_ID
* value from xparameters.h
*
* @return
* - XST_SUCCESS if the example is successful.
* - XST_FAILURE if the example failed.
*
* @note None.
*
******************************************************************************/
int BramIntrExample(XIntc* IntcInstancePtr, XBram* InstancePtr,
u16 DeviceId, u16 IntrId)
{
int Status;
XBram_Config *ConfigPtr;
/*
* Initialize the BRAM driver. If an error occurs then exit
*/
/*
* Lookup configuration data in the device configuration table.
* Use this configuration info down below when initializing this
* driver.
*/
ConfigPtr = XBram_LookupConfig(DeviceId);
if (ConfigPtr == (XBram_Config *) NULL) {
return XST_FAILURE;
}
Status = XBram_CfgInitialize(InstancePtr, ConfigPtr,
ConfigPtr->CtrlBaseAddress);
if (Status != XST_SUCCESS) {
return XST_FAILURE;
}
InitializeECC(ConfigPtr, ConfigPtr->CtrlBaseAddress);
Status = BramSetupIntrSystem(IntcInstancePtr,
InstancePtr, DeviceId,
IntrId);
if (Status != XST_SUCCESS) {
return XST_FAILURE;
}
IntrCount = 0;
ExceptionCount = 0;
/*
* Execute the BRAM driver selftest, and then disable exceptions again.
* Running the driver test will inject errors if Fault Injection is
* enabled.
*/
Status = XBram_SelfTest(InstancePtr, XBRAM_IR_ALL_MASK);
if (Status != XST_SUCCESS) {
return XST_FAILURE;
}
BramDisableIntr(IntcInstancePtr, InstancePtr, IntrId);
if (InstancePtr->Config.FaultInjectionPresent) {
if (ExceptionCount == 0 &&
InstancePtr->Config.UncorrectableFailingDataRegs) {
return XST_FAILURE;
}
if (IntrCount == 0 &&
InstancePtr->Config.EccStatusInterruptPresent) {
return XST_FAILURE;
}
}
return XST_SUCCESS;
}
/*****************************************************************************/
/**
*
* This function performs the BRAM set up for Interrupts
*
* @param IntcInstancePtr is a reference to the Interrupt Controller
* driver Instance
* @param InstancePtr is a reference to the BRAM driver Instance
* @param DeviceId is the XPAR_<BRAM_instance>_DEVICE_ID
* value from xparameters.h
* @param IntrId is XPAR_<INTC_instance>_<BRAM_instance>_INTERRUPT_INTR
* value from xparameters.h
*
* @return
* - XST_SUCCESS if the interrupt setup is successful.
* - XST_FAILURE if interrupt setup is not successful.
*
* @note None.
*
******************************************************************************/
static int BramSetupIntrSystem(XIntc* IntcInstancePtr, XBram* InstancePtr,
u16 DeviceId, u16 IntrId)
{
int Status;
#ifndef TESTAPP_GEN
/*
* Initialize the interrupt controller driver so that it's ready to
* use.specify the device ID that was generated in xparameters.h
*/
Status = XIntc_Initialize(IntcInstancePtr, INTC_DEVICE_ID);
if (Status != XST_SUCCESS) {
return XST_FAILURE;
}
#endif
/*
* Hook up interrupt Bram handler
*/
Status = XIntc_Connect(IntcInstancePtr, IntrId,
(XInterruptHandler)BramDriverHandler,
InstancePtr);
/*
* Enable the BRAM interrupts so that errors can be detected
*/
XBram_InterruptEnable(InstancePtr, XBRAM_IR_ALL_MASK);
/*
* Enable the interrupt vector at the interrupt controller
*/
XIntc_Enable(IntcInstancePtr, IntrId);
#ifndef TESTAPP_GEN
/*
* Initialize the exception table and register the interrupt
* controller handler with the exception table
*/
Xil_ExceptionInit();
Xil_ExceptionRegisterHandler(XIL_EXCEPTION_ID_INT,
(Xil_ExceptionHandler)XIntc_InterruptHandler,
IntcInstancePtr);
#endif
/*
* Set up exception handlers for bus error exceptions.
*/
Xil_ExceptionRegisterHandler(XIL_EXCEPTION_ID_IPLB_EXCEPTION,
(Xil_ExceptionHandler)BramIntrExceptionHandler,
0);
Xil_ExceptionRegisterHandler(XIL_EXCEPTION_ID_DPLB_EXCEPTION,
(Xil_ExceptionHandler)BramIntrExceptionHandler,
0);
#ifndef TESTAPP_GEN
/*
* Enable exceptions.
*/
Xil_ExceptionEnable();
/*
* Start the interrupt controller such that interrupts are recognized
* and handled by the processor
*/
Status = XIntc_Start(IntcInstancePtr, XIN_REAL_MODE);
if (Status != XST_SUCCESS) {
return XST_FAILURE;
}
#endif
return XST_SUCCESS;
}
/****************************************************************************/
/**
*
* This function ensures that ECC in the BRAM is initialized if no hardware
* initialization is available. The ECC bits are initialized by reading and
* writing data in the memory. This code is not optimized to only read data
* in initialized sections of the BRAM.
*
* @param ConfigPtr is a reference to a structure containing information
* about a specific BRAM device.
* @param EffectiveAddr is the device base address in the virtual memory
* address space.
*
* @return
* None
*
* @note None.
*
*****************************************************************************/
static void InitializeECC(XBram_Config *ConfigPtr, u32 EffectiveAddr)
{
u32 Addr;
volatile u32 Data;
if (ConfigPtr->EccPresent &&
ConfigPtr->EccOnOffRegister &&
ConfigPtr->EccOnOffResetValue == 0 &&
ConfigPtr->WriteAccess != 0) {
for (Addr = ConfigPtr->MemBaseAddress;
Addr < ConfigPtr->MemHighAddress; Addr+=4) {
Data = XBram_In32(Addr);
XBram_Out32(Addr, Data);
}
XBram_WriteReg(EffectiveAddr, XBRAM_ECC_ON_OFF_OFFSET, 1);
}
}
/*****************************************************************************/
/**
*
* This is the interrupt handler routine for the BRAM interrupt for this example.
*
* @param CallbackRef is the Callback reference for the handler.
*
* @return None.
*
* @note None.
*
******************************************************************************/
static void BramDriverHandler(void *CallbackRef)
{
XBram *BramPtr = (XBram *)CallbackRef;
IntrCount++;
/*
* Clear the Interrupt
*/
XBram_InterruptClear(BramPtr, XBRAM_IR_ALL_MASK);
}
/*****************************************************************************/
/**
*
* This function disables the interrupts for the BRAM and disables Exceptions.
*
* @param IntcInstancePtr is a pointer to the Interrupt Controller
* driver Instance
* @param InstancePtr is a pointer to the BRAM driver Instance
* @param IntrId is XPAR_<INTC_instance>_<BRAM_instance>_VEC_ID
* value from xparameters.h
*
* @return None
*
* @note None
*
******************************************************************************/
static void BramDisableIntr(XIntc* IntcInstancePtr, XBram* InstancePtr, u16 IntrId)
{
XBram_InterruptDisable(InstancePtr, XBRAM_IR_ALL_MASK);
XIntc_Disable(IntcInstancePtr, IntrId);
#ifndef TESTAPP_GEN
Xil_ExceptionDisable();
#endif
}
/*****************************************************************************/
/**
* Exception handler, invoked when an instruction bus exception or a data bus
* exception orrurs. The handler just counts exceptions, and returns.
*
* @param InstancePtr points to the exception instance
*
* @return None
*
* @note None
*
******************************************************************************/
static void BramIntrExceptionHandler(void *InstancePtr)
{
u32 Esr = mfesr();
if ((Esr & 0x81F) == 0x804)
ExceptionCount++;
}