📄 corea.c
字号:
/*************************************************************************
Copyright(c) 2005 Analog Devices, Inc. All Rights Reserved.
Please refer to the "readme" file for a description of this example.
************************************************************************/
/*********************************************************************
Include files
*********************************************************************/
#include <services/services.h>
#include <ezkitutilities.h>
#include <drivers/adi_dev.h>
#include <drivers/ppi/adi_ppi.h>
#include "../Buffer.h"
#include "core.h"
//for cycle count
#include <ccblkfn.h>
#include <sysreg.h>
#define YELLOW (0xD292D210) // yellow pixel pattern
extern void motion_detect(char *ref_frame, char* curr_frame,unsigned int width, char* curr_out_frame);
void Setup_VideoIn(void);
void Setup_VideoOut(void);
void sys_init(void);
void buffer_init(void);
/*********************************************************************
data
*********************************************************************/
// storage for interrupt manager secondary handlers
u8 IntMgrData[(ADI_INT_SECONDARY_MEMORY * ADI_NUM_SECONDARY_HANDLERS)];
// DMA Manager data (base memory + memory for 1 DMA channel)
u8 DMAMgrData[ADI_DMA_BASE_MEMORY + (ADI_DMA_CHANNEL_MEMORY * 16)];
// Deferred Callback Manager data (memory for 1 service plus 4 posted callbacks)
#if defined(USE_DEFERRED_CALLBACKS)
//u8 DCBMgrData[ADI_DCB_QUEUE_SIZE + (ADI_DCB_ENTRY_SIZE)*4];
#endif
// Device Manager data (base memory + memory for 1 device)
u8 DevMgrData[ADI_DEV_BASE_MEMORY + (ADI_DEV_DEVICE_MEMORY * ADI_NUM_DEVICES)];
ADI_DCB_HANDLE DCBManagerHandle; // handle to the callback service
ADI_DMA_MANAGER_HANDLE DMAManagerHandle; // handle to the DMA Manager
ADI_DEV_MANAGER_HANDLE DeviceManagerHandle; // handle to the Device Manager
ADI_DMA_STREAM_HANDLE StreamHandle; // handle to the memory stream
ADI_DMA_STREAM_HANDLE StreamHandle1; // handle to the memory stream
ADI_DEV_DEVICE_HANDLE DeviceHandle; // handle to the device driver
volatile static unsigned int Counter = 0; // count the number of input buffers processed
volatile static unsigned int count_frames_processed= 0;
volatile static unsigned int cycle_count=0;
extern ADI_DEV_2D_BUFFER Buffer2D_IN[NUM_BUFFERS];
extern ADI_DEV_2D_BUFFER Buffer2D_OUT[NUM_BUFFERS];
extern char L2_slice0[1716*L2NUMROWS];
extern char L2_slice_Out[1716*L2NUMROWS];
/*********************************************************************
Function: ExceptionHandler
HWErrorHandler
Description: We should never get an exception or hardware error,
but just in case we'll catch them and simply turn
on all the LEDS should one ever occur.
*********************************************************************/
static ADI_INT_HANDLER(ExceptionHandler) // exception handler
{
ezErrorCheck(1);
return(ADI_INT_RESULT_PROCESSED);
}
static ADI_INT_HANDLER(HWErrorHandler) // hardware error handler
{
ezErrorCheck(1);
return(ADI_INT_RESULT_PROCESSED);
}
/*********************************************************************
Function: PPI0_callback
Description:A callback is generated when the PPI
has completed processing of the input buffer.
Increment the counter for every input frame captured.
*********************************************************************/
static void PPI0_callback(
void *AppHandle,
u32 Event,
void *pArg)
{
ADI_DEV_2D_BUFFER *pBuffer; // pointer to the buffer that was processed
// CASEOF (event type)
switch (Event) {
// CASE (buffer processed)
case ADI_DEV_EVENT_BUFFER_PROCESSED:
// point to the buffer
pBuffer = (ADI_DEV_2D_BUFFER *)pArg;
// increment our counter
Counter++;
break;
// CASE (an error)
case ADI_DEV_EVENT_DMA_ERROR_INTERRUPT:
case ADI_PPI_EVENT_ERROR_INTERRUPT:
// turn on all LEDs and wait for help
ezTurnOnAllLEDs();
while (1) ;
// ENDCASE
}
// return
}
/*********************************************************************
Function: PPI1_callback
Description:A callback is generated when the PPI
has completed processing of the output buffer.
In the current implementation the callback routine is not
called for output buffers transferred
*********************************************************************/
static void PPI1_callback(
void *AppHandle,
u32 Event,
void *pArg)
{
ADI_DEV_2D_BUFFER *pBuffer; // pointer to the buffer that was processed
// CASEOF (event type)
switch (Event) {
// CASE (buffer processed)
case ADI_DEV_EVENT_BUFFER_PROCESSED:
break;
// CASE (an error)
case ADI_DEV_EVENT_DMA_ERROR_INTERRUPT:
case ADI_PPI_EVENT_ERROR_INTERRUPT:
// turn on all LEDs and wait for help
ezTurnOnAllLEDs();
while (1) ;
// ENDCASE
}
// return
}
/*********************************************************************
Function: ButtonCallback
Description: This function is called when a button is pressed.
*********************************************************************/
static void ButtonCallback(
void *AppHandle,
u32 Event,
void *pArg)
{
ButtonPressed = TRUE;
}
/************************************************************************
Function:Setup_VideoIn
Description:
************************************************************************/
void Setup_VideoIn()
{
ADI_DEV_CMD_VALUE_PAIR InboundConfigurationTable [] = {
{ ADI_DEV_CMD_SET_DATAFLOW_METHOD, (void *) ADI_DEV_MODE_CHAINED_LOOPBACK },
{ ADI_PPI_CMD_SET_CONTROL_REG, (void *)0x0184 },
{ ADI_PPI_CMD_SET_LINES_PER_FRAME_REG, (void *)525 },
{ ADI_DEV_CMD_END, NULL },
};
ezEnableVideoDecoder();
// open the PPI driver for input
ezErrorCheck(adi_dev_Open(DeviceManagerHandle, &ADIPPIEntryPoint, 0, NULL, &DeviceHandle, ADI_DEV_DIRECTION_INBOUND, DMAManagerHandle, DCBManagerHandle, PPI0_callback));
// configure the PPI driver with the values from the inbound configuration table
ezErrorCheck(adi_dev_Control(DeviceHandle, ADI_DEV_CMD_TABLE, InboundConfigurationTable));
// give the PPI driver the buffer to process
ezErrorCheck(adi_dev_Read(DeviceHandle, ADI_DEV_2D, (ADI_DEV_BUFFER *)&Buffer2D_IN[0]));
// tell the PPI driver to enable data flow
ezErrorCheck(adi_dev_Control(DeviceHandle, ADI_DEV_CMD_SET_DATAFLOW, (void *)TRUE));
}
/************************************************************************
Function:Setup_VideoOut
Description:
************************************************************************/
void Setup_VideoOut()
{
ADI_DEV_CMD_VALUE_PAIR OutboundConfigurationTable [] = {
{ ADI_DEV_CMD_SET_DATAFLOW_METHOD, (void *)ADI_DEV_MODE_CHAINED_LOOPBACK },
{ ADI_PPI_CMD_SET_CONTROL_REG, (void *)0x0182 },
{ ADI_PPI_CMD_SET_LINES_PER_FRAME_REG, (void *)525 },
{ ADI_DEV_CMD_END, NULL },
};
ezEnableVideoEncoder();
// open the PPI driver for output
ezErrorCheck(adi_dev_Open(DeviceManagerHandle, &ADIPPIEntryPoint, 1, NULL, &DeviceHandle, ADI_DEV_DIRECTION_OUTBOUND, DMAManagerHandle, DCBManagerHandle, PPI1_callback));
// configure the PPI driver with the values from the outbound configuration table
ezErrorCheck(adi_dev_Control(DeviceHandle, ADI_DEV_CMD_TABLE, OutboundConfigurationTable));
// give the PPI driver the buffer to process
ezErrorCheck(adi_dev_Write(DeviceHandle, ADI_DEV_2D, (ADI_DEV_BUFFER *)&Buffer2D_OUT[0]));
ezErrorCheck(adi_dev_Control(DeviceHandle, ADI_DEV_CMD_SET_DATAFLOW, (void *)TRUE));
}
/*********************************************************************
Function: main
Description:Calls the init routines for initialiazing system
services and buffers.
Transfer data from PPI to SDRAM (L3) to L2 to L3 to PPI on a
slice basis. Transfer current frame and reference frame data to
L2 memory.
*********************************************************************/
void main(void) {
u32 i, ResponseCount; // response counter
ADI_DMA_2D_TRANSFER L3_Buffer2D0_DMA_IN;
ADI_DMA_2D_TRANSFER L3_Buffer2D1_DMA_IN;
ADI_DMA_2D_TRANSFER L3_Buffer2D0_DMA_OUT;
ADI_DMA_2D_TRANSFER L3_Buffer2D0;
ADI_DMA_2D_TRANSFER L3_Buffer2D1;
ADI_DMA_2D_TRANSFER L2_Buffer2D0_DMA_IN;
ADI_DMA_2D_TRANSFER L2_Buffer2D1_DMA_IN;
ADI_DMA_2D_TRANSFER L2_Buffer2D0_DMA_OUT;
int rowIndex,lineIndex,curr_Counter;
unsigned int offset;
char *psframeIN;
char *psframeREF;
char *psframeOUT;
sys_init();
buffer_init();
L2BufferInit(&L2_Buffer2D0_DMA_IN, L2_slice0);
L2BufferInit(&L2_Buffer2D1_DMA_IN, L2_slice1);
L2BufferInit(&L2_Buffer2D0_DMA_OUT, L2_slice_Out);
L3BufferInit(&L3_Buffer2D0_DMA_IN, sFrame0IN);
L3BufferInit(&L3_Buffer2D1_DMA_IN, sFrame1IN);
L3BufferInit(&L3_Buffer2D0_DMA_OUT, sFrame0OUT);
L3Buffer_FullFrameInit(&L3_Buffer2D0,sFrame0IN);
L3Buffer_FullFrameInit(&L3_Buffer2D1,sFrameRef);
Setup_VideoIn();
Setup_VideoOut();
ezErrorCheck(adi_dma_MemoryOpen(DMAManagerHandle,
ADI_DMA_MDMA1_0,
(void *)0x12345678,
&StreamHandle,
NULL));
ezErrorCheck(adi_dma_MemoryOpen(DMAManagerHandle,
ADI_DMA_MDMA1_1,
(void *)0x12345678,
&StreamHandle1,
NULL));
offset=0;
psframeIN =sFrame0IN+offset;
psframeREF =sFrameRef+offset;
L3_Buffer2D0_DMA_IN.StartAddress=psframeIN;
L3_Buffer2D1_DMA_IN.StartAddress=psframeREF;
while(Counter<1);
curr_Counter=Counter;
while(ButtonPressed == FALSE)
{
//Proceed if Counter incremented. Counter is incremented in the call back routine.
if(curr_Counter < Counter)
{
curr_Counter=Counter;
//Four input and four output frame buffers are placed in L3 memory.
//Point to current input buffer
//Previous frame becomes reference frame
switch(curr_Counter%4)
{
case 1 :
L3_Buffer2D0_DMA_OUT.StartAddress=sFrame0OUT;
L3_Buffer2D0_DMA_IN.StartAddress=sFrame0IN;
L3_Buffer2D0.StartAddress=sFrame0IN;
psframeIN=sFrame0IN;
psframeOUT=sFrame0OUT;
psframeREF=sFrame3IN;
L3_Buffer2D1.StartAddress=sFrame0OUT;
break;
case 2 :L3_Buffer2D0_DMA_OUT.StartAddress=sFrame1OUT;
L3_Buffer2D0_DMA_IN.StartAddress=sFrame1IN;
L3_Buffer2D0.StartAddress=sFrame1IN;
psframeIN=sFrame1IN;
psframeOUT=sFrame1OUT;
psframeREF=sFrame0IN;
L3_Buffer2D1.StartAddress=sFrame1OUT;
break;
case 3 :L3_Buffer2D0_DMA_OUT.StartAddress=sFrame2OUT;
L3_Buffer2D0_DMA_IN.StartAddress=sFrame2IN;
L3_Buffer2D0.StartAddress=sFrame2IN;
psframeIN=sFrame2IN;
psframeOUT=sFrame2OUT;
psframeREF=sFrame1IN;
L3_Buffer2D1.StartAddress=sFrame2OUT;
break;
case 0 :L3_Buffer2D0_DMA_OUT.StartAddress=sFrame3OUT;
L3_Buffer2D0_DMA_IN.StartAddress=sFrame3IN;
L3_Buffer2D0.StartAddress=sFrame3IN;
psframeIN=sFrame3IN;
psframeOUT=sFrame3OUT;
psframeREF=sFrame2IN;
L3_Buffer2D1.StartAddress=sFrame3OUT;
break;
}
//1. Transfer slice of frame from L3 to L2.
//2. Process
//3. Transfer processed slice from L2 to L3
for(rowIndex=0;rowIndex<NUMROWS;rowIndex+=L2NUMROWS)
{
L3_Buffer2D0_DMA_OUT.StartAddress=psframeOUT+offset;
L3_Buffer2D0_DMA_IN.StartAddress=psframeIN+offset;
L3_Buffer2D1_DMA_IN.StartAddress=psframeREF+offset;
BufferL3toL2(&L3_Buffer2D0_DMA_IN,&L2_Buffer2D0_DMA_IN,StreamHandle);
BufferL3toL2(&L3_Buffer2D1_DMA_IN,&L2_Buffer2D1_DMA_IN,StreamHandle1);
BufferL2toL2(&L2_Buffer2D0_DMA_OUT,&L2_Buffer2D0_DMA_IN,StreamHandle);
sysreg_write(reg_CYCLES,0x0);
sysreg_write(reg_CYCLES2,0x0);
/*
*current Frame slice -> L2_Buffer2D0_DMA_IN.StartAddress
*reference Frame slice -> L2_Buffer2D1_DMA_IN.StartAddress
*output Frame -> L3_Buffer2D0_DMA_OUT.StartAddress
*/
motion_detect(L2_Buffer2D1_DMA_IN.StartAddress, L2_Buffer2D0_DMA_IN.StartAddress,1440, L2_Buffer2D0_DMA_OUT.StartAddress);
cycle_count=sysreg_read(reg_CYCLES);
BufferL2toL3(&L3_Buffer2D0_DMA_OUT,&L2_Buffer2D0_DMA_OUT,StreamHandle1);
offset+=1716*L2NUMROWS;
}//end of for
offset=0;
}//end of if
}
//close memdma
adi_dma_MemoryClose(StreamHandle,TRUE);
// close the PPI driver
ezErrorCheck(adi_dev_Close(DeviceHandle));
// close the Device Manager
ezErrorCheck(adi_dev_Terminate(DeviceManagerHandle));
// close down the DMA Manager
ezErrorCheck(adi_dma_Terminate(DMAManagerHandle));
// close down the Deferred Callback Manager
#if defined(USE_DEFERRED_CALLBACKS)
ezErrorCheck(adi_dcb_Terminate());
#endif
// return
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -