⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 softpcdlg.cpp

📁 基于DAM6416的通过PCI的视频口和主机的通信程序
💻 CPP
字号:
// SOFTPCDlg.cpp : implementation file
//

#include "stdafx.h"
#include "SOFTPC.h"
#include "SOFTPCDlg.h"

#include "atetypes_1.h"
#include "IekC64Sdk.h"
#include "stdio.h"
#include "conio.h"
#include "memory.h"


#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

#ifdef _DEBUG
#define DSP_CODE_FILE  "..\\..\\SoftDsp\\Debug\\dspdemo.out"
#else
#define DSP_CODE_FILE  "..\\..\\SoftDsp\\Release\\dspdemo.out"
#endif

#define PAL_CIF		352*288
#define BufferSize	PAL_CIF*4

// thread define 
UINT myTreadProc(LPVOID pParam);

Bool exit_this_demo=FALSE;

// This buffer will hold data read from the DSP memory
Uint16 VideoFrameBuffer[BufferSize];
//Uint16 VideoFrameBuffer[4];

FILE *DataFile;
/////////////////////////////////////////////////////////////////////////////
// CSOFTPCDlg dialog

CSOFTPCDlg::CSOFTPCDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CSOFTPCDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CSOFTPCDlg)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CSOFTPCDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CSOFTPCDlg)
		// NOTE: the ClassWizard will add DDX and DDV calls here
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CSOFTPCDlg, CDialog)
	//{{AFX_MSG_MAP(CSOFTPCDlg)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTTON_Init, OnBUTTONInit)
	ON_BN_CLICKED(IDC_BUTTON_Download, OnBUTTONDownload)
	ON_BN_CLICKED(IDC_BUTTON_Start, OnBUTTONStart)
	ON_BN_CLICKED(IDC_BUTTON_Exit, OnBUTTONExit)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CSOFTPCDlg message handlers

BOOL CSOFTPCDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	// TODO: Add extra initialization here
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CSOFTPCDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CSOFTPCDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CSOFTPCDlg::OnBUTTONInit() 
{
	// TODO: Add your control notification handler code here
    IEKC64_STATUS status = IEKC64_OK;
    Uint32 uBoardId = 0;
 
	/***********************
	--- board initialize ---
	***********************/


	hEventMsg = NULL ;

	// This handle points to the IEK board object
	IekHandle = 0 ;

	SetDlgItemText(IDC_MessageDisplay, "Initializing board driver ...");
	Sleep(1000);
	
	// disable all buttons 
	GetDlgItem(IDC_BUTTON_Start)->EnableWindow(FALSE);
	GetDlgItem(IDC_BUTTON_Download)->EnableWindow(FALSE);
	GetDlgItem(IDC_BUTTON_Exit)->EnableWindow(FALSE);

	status = IEKC64_initialize( uBoardId, &IekHandle );
    if( !status )
	{
		SetDlgItemText(IDC_MessageDisplay, "Board initialized ...");
		Sleep(1000);

  	    status = IEKC64_resetDsp( IekHandle );
		if( !IEKC64_SUCCESS( status ) )
		{
			SetDlgItemText(IDC_MessageDisplay, "Failed to reset DSP. Pls Check the hardware!");
			return;
		}
		/*****************************************
		--- create event for message reception ---
		*****************************************/
    	SetDlgItemText(IDC_MessageDisplay, "Creating message event ...");
   	    hEventMsg = CreateEvent( NULL, TRUE, FALSE, NULL );
		if( !(hEventMsg == NULL) )
		{
			/*****************************************
			--- enable message event for reception ---
			*****************************************/
	    	SetDlgItemText(IDC_MessageDisplay, "Connecting Message Event ...");
			Sleep(1000);

			status = IEKC64_enableMsgEvent( IekHandle, hEventMsg );
			if( !status )
			{
		    	SetDlgItemText(IDC_MessageDisplay, "DSP Board Ready!");
				GetDlgItem(IDC_BUTTON_Download)->EnableWindow(TRUE);
				GetDlgItem(IDC_BUTTON_Init)->EnableWindow(FALSE);
			}
	    	else
				SetDlgItemText(IDC_MessageDisplay, "Error : connecting event to message failed !");
		}
		else
    		SetDlgItemText(IDC_MessageDisplay, "Error : can't create event!");
	}
	else
  		SetDlgItemText(IDC_MessageDisplay, "Error : board initialization failed  !");



}

void CSOFTPCDlg::OnBUTTONDownload() 
{
	// TODO: Add your control notification handler code here
    IEKC64_STATUS status = IEKC64_OK;

	char * FileNameString="dspdemo.out";
	char * FilterString="*.out||";

	// download DSP program ---

	CFileDialog myOpenFileDialog(TRUE,NULL,(LPSTR)FileNameString,NULL,(LPSTR)FilterString); 

    if(myOpenFileDialog.DoModal()!=IDOK) 
	{
		SetDlgItemText(IDC_MessageDisplay,"Please select the DSP OUT File!");
		return;
    } 
	

	lstrcpy(FileNameString, (LPCTSTR)myOpenFileDialog.GetPathName());
	status = IEKC64_loadProgram( IekHandle, (BYTE*)FileNameString);

	SetDlgItemText(IDC_MessageDisplay,"Downloading DSP code now ...");
	GetDlgItem(IDC_BUTTON_Init)->EnableWindow(FALSE);
	GetDlgItem(IDC_BUTTON_Download)->EnableWindow(FALSE);
	GetDlgItem(IDC_BUTTON_Start)->EnableWindow(FALSE);
	GetDlgItem(IDC_BUTTON_Exit)->EnableWindow(FALSE);
	Sleep(100);

	status = IEKC64_loadProgram( IekHandle, (BYTE*)DSP_CODE_FILE );
	if( !status )
	{
		SetDlgItemText(IDC_MessageDisplay,"DSP software downloaded ...");

		// wait for message from DSP "end of init" ---


		// We have to wait a little before trying to get the 1st message
		// because if we try to read messages before the DSP has done
		// it's PCI_init() call, we will get error IEKC64_ERR_MSG (0xA1080000)
		Sleep( 1000 );

		SetDlgItemText(IDC_MessageDisplay,"Waiting to receive Initialization message ...");
		Sleep( 1000 );

		status = waitMessage();
		if ( !IEKC64_SUCCESS(status) )
		{
			SetDlgItemText(IDC_MessageDisplay,"Error when waiting for DSP initialization message!");
			return;
		}
		// if the received message is 0x11111111
		if ( MessageBuffer[0] == 0x11111111 )
			SetDlgItemText(IDC_MessageDisplay,"Received DSP initialization message !");
		else
		{
			SetDlgItemText(IDC_MessageDisplay,"Error : Received unknown message !");
			return;
		}
	}
	else
	{
		SetDlgItemText(IDC_MessageDisplay,"Error : doanloding DSP code failed !");
		return;
	}

	SetDlgItemText(IDC_MessageDisplay,"DSP is waiting for START command ...");
	Sleep(100);
	GetDlgItem(IDC_BUTTON_Start)->EnableWindow(TRUE);
}

void CSOFTPCDlg::OnBUTTONStart() 
{
    IEKC64_STATUS status = IEKC64_OK;
	// TODO: Add your control notification handler code here

	GetDlgItem(IDC_BUTTON_Start)->EnableWindow(FALSE);


    if( (DataFile=fopen("c:\\temp\\video.dat", "wb" )) == NULL )
	{	
		SetDlgItemText(IDC_MessageDisplay,"Open File failed, exit now...");
		exit(1);
	}

	// send START message ---

	SetDlgItemText(IDC_MessageDisplay,"Sending Start message to DSP ...");
	printf( "Sending Start message to DSP\n" );
	Sleep(1000);

	MessageBuffer[0] = 0x22222222;

	status = IEKC64_sendDspMsg( IekHandle, 0, sizeof(Uint32), MessageBuffer );
	if( !IEKC64_SUCCESS(status) )
	{
		SetDlgItemText(IDC_MessageDisplay,"Sending Start message to DSP Failed!");
		printf("Error : Sending failed : 0x%08X : %s\n", status, IEKC64_getErrorString(status) );
		Sleep(1000);
		exit( 1 );
	}

	// We have to wait a little before trying to get the message

	SetDlgItemText(IDC_MessageDisplay,"Waiting for DSP to respond to START message ...");
	Sleep( 1000 );

	status = waitMessage();
	if ( !IEKC64_SUCCESS(status) )
	{
		SetDlgItemText(IDC_MessageDisplay,"Error when waiting for DSP START message!");
		return;
	}
	// if the received message is 0x33333333
	if ( MessageBuffer[0] == 0x33333333 )
		SetDlgItemText(IDC_MessageDisplay,"Received DSP START message !");
	else
	{
		SetDlgItemText(IDC_MessageDisplay,"Error : Received unknown message !");
		return;
	}

	Sleep( 1000 );
	SetDlgItemText(IDC_MessageDisplay,"DSP Program Running now ...");

	GetDlgItem(IDC_BUTTON_Exit)->EnableWindow(TRUE);

	// begin the message thread
	exit_this_demo=FALSE;
	AfxBeginThread(myTreadProc, this, THREAD_PRIORITY_NORMAL, 4096000);
}

void CSOFTPCDlg::OnBUTTONExit() 
{
	// TODO: Add your control notification handler code here
    IEKC64_STATUS status = IEKC64_OK;

	GetDlgItem(IDC_BUTTON_Exit)->EnableWindow(FALSE);

	// end the message thread
	exit_this_demo=TRUE;
	
	MessageBuffer[0] = 0x99999999;

	status = IEKC64_sendDspMsg( IekHandle, 0, sizeof(Uint32), MessageBuffer );
	if( !IEKC64_SUCCESS(status) )
	{
		SetDlgItemText(IDC_MessageDisplay,"Sending Exit message to DSP Failed!");
		printf("Error : Sending failed : 0x%08X : %s\n", status, IEKC64_getErrorString(status) );
		Sleep(1000);
		exit( 1 );
	}

	fclose(DataFile);

	SetDlgItemText(IDC_MessageDisplay,"Stop DSP Program and Exit now!");
	Sleep(2000);

	/*************************
	--- Uninitialize board ---
	*************************/
	IEKC64_unInitialize(IekHandle);
	printf("Board uninitialized ...\n");
	
}


IEKC64_STATUS CSOFTPCDlg::waitMessage( )
{

	IEKC64_STATUS status;
    Uint32 uMsgFlag = 0;
	Uint32 uBytesRead;

	// Clear the event
	ResetEvent( hEventMsg );

	// check to see if a message is already there
	status = IEKC64_getDspMsg( IekHandle, &uMsgFlag, &uBytesRead, sizeof(MessageBuffer), MessageBuffer );

	if ( status == IEKC64_ERR_NO_MSG )
	{
		// No message, wait for the event to be triggered
		printf( "Wait for message Event\n" );
	    DWORD uRes = WaitForSingleObject( hEventMsg, INFINITE );
		ResetEvent( hEventMsg );

		if ( uRes != WAIT_OBJECT_0 )
		{
			printf( "Error in WFSO(hEventMsg) !!!\n" );
			exit( 1 );
		}

		printf( "Event pulsed\n" );

		// get the message
		status = IEKC64_getDspMsg( IekHandle, &uMsgFlag, &uBytesRead, sizeof(MessageBuffer), MessageBuffer );
	}
	else
	{
		printf( "Message was already there\n" );
	}

	if ( IEKC64_SUCCESS(status) )
	{
		printf( "Message received\n");
	}
	else
	{
		printf( "Error in getting message : 0x%08X : %s !!!!\n", status, IEKC64_getErrorString( status ) );
	}


	return status;
}

// thread to retrieve message when DSP video I/O is running

UINT myTreadProc(LPVOID pParam )
{
    IEKC64_STATUS status = IEKC64_OK;
	Uint32	MessageBuffer[5];
    Uint32 DspDataAddr = 0;
	Uint32 MemLen=0;
	Uint32 nBytesRead=0;
	Uint32 temp=0;
	CString s;
	PVOID pDspMemory;
	CSOFTPCDlg* pDlg;
	pDlg = (CSOFTPCDlg*)pParam;

	while(1)
	{
		if (exit_this_demo)
			AfxEndThread(0);
	
		
		status = pDlg->waitMessage();
		if ( !IEKC64_SUCCESS(status) )
		{
			pDlg->SetDlgItemText(IDC_MessageDisplay,"Error when waiting for DSP running message!");
			AfxEndThread(1);
		}
		// if the received message is 0x44444444
		if ( pDlg->MessageBuffer[0] == 0x44444444 )
		{
			memset(VideoFrameBuffer, 0, sizeof( VideoFrameBuffer ));
			s.Format("Storeing DSP Video I/O Frame index: %d...", pDlg->MessageBuffer[1]);
			printf("%s\n", s);
			pDlg->SetDlgItemText(IDC_MessageDisplay,s);
			DspDataAddr=pDlg->MessageBuffer[2];
			MemLen=pDlg->MessageBuffer[3];
			// This call have 3 effects :
			//  - on all OS : if change the DSPP register so that PCI BAR0 maps the correct
			//    DSP memory page (4MB pages) depending on the DspDataAddr required
			//  - it lock a mutex so that no other process can change the DSPP until we unlock
			//  - On W2K/XP, it maps the DSP memory into PC space
		    status = IEKC64_lockDSPMem( pDlg->IekHandle, DspDataAddr, MemLen, &pDspMemory, 1000 );
			if ( !IEKC64_SUCCESS(status) )
			{
				pDlg->SetDlgItemText(IDC_MessageDisplay, "Error : unable access DSP memory! Exit now..." );
				AfxEndThread(0);
			}

			// Copy the data from DSP to PC
			if ( pDspMemory )
			{
				temp=sizeof( VideoFrameBuffer );
				memcpy( VideoFrameBuffer, pDspMemory, sizeof( VideoFrameBuffer ) );
				fwrite( VideoFrameBuffer, sizeof(Uint16), sizeof( VideoFrameBuffer )/sizeof(Uint16), DataFile);
			}

			// Now we can release the DSP memory
		    IEKC64_unlockDSPMem( pDlg->IekHandle );

			// send unlock signal to DSP
			MessageBuffer[0] = 0xAAAAAAAA;
			status = IEKC64_sendDspMsg( pDlg->IekHandle, 0, sizeof(Uint32), MessageBuffer );
			if( !IEKC64_SUCCESS(status) )
			{
				pDlg->SetDlgItemText(IDC_MessageDisplay,"Sending Unlock message to DSP Failed!");
				printf("Error : Sending failed : 0x%08X : %s\n", status, IEKC64_getErrorString(status) );
				Sleep(1000);
				exit( 1 );
			}
			
		}
		else
		{
			pDlg->SetDlgItemText(IDC_MessageDisplay,"Error : Received unknown message !");
			AfxEndThread(1);
		}
	}

	return 0;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -