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

📄 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

#define VIDEO_DATA_FILE  "c:\\temp\\video.dat"

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



Bool exit_this_demo=FALSE;

// This buffer will hold data read from the DSP memory
Uint16 VideoFrameBuffer[BufferSize];
Uint32 DspDataAddr = 0;
Uint32 MemLen=0;

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)
	ON_WM_TIMER()
	//}}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;
    Uint32 DspDataAddr = 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);
		
		/*****************************************
		--- 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 ---
	***************************/

	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);



	// 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 );

	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() 
{
	// TODO: Add your control notification handler code here
    IEKC64_STATUS status = IEKC64_OK;
	// TODO: Add your control notification handler code here

	GetDlgItem(IDC_BUTTON_Start)->EnableWindow(FALSE);


    if( (DataFile=fopen(VIDEO_DATA_FILE, "rb" )) == NULL )
	{	
		SetDlgItemText(IDC_MessageDisplay,"Open File failed, exit now...");
		Sleep(1000);
		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 !");
		DspDataAddr=MessageBuffer[1];
		MemLen=MessageBuffer[2];
	}	
	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);
	SetTimer( 1, 40, 0 );
}

void CSOFTPCDlg::OnBUTTONExit() 
{
	// TODO: Add your control notification handler code here
    IEKC64_STATUS status = IEKC64_OK;
	// TODO: Add your control notification handler code here
	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);
	KillTimer(1);
	SetDlgItemText(IDC_MessageDisplay,"Stop DSP Program and Exit now!");
	Sleep(2000);

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


void CSOFTPCDlg::OnTimer(UINT nIDEvent) 
{
	// TODO: Add your message handler code here and/or call default
    IEKC64_STATUS status = IEKC64_OK;
	Uint32	thisMessageBuffer[5];
	Uint32 nBytesWrite=0;
	Uint32 temp=0;
	CString s;
	static Uint32 index=1;
	PVOID pDspMemory;

		if (exit_this_demo)
			return;				
			
		if (feof(DataFile))
		{
			s.Format("Video File End. Press Stop to exit!");
			SetDlgItemText(IDC_MessageDisplay,s);
			return;
		}
			
			// copy video data to DSP
		    status = IEKC64_lockDSPMem( IekHandle, DspDataAddr, MemLen, &pDspMemory, 100 );
			if ( !IEKC64_SUCCESS(status) )
			{
				SetDlgItemText(IDC_MessageDisplay, "Error : unable access DSP memory! Exit now..." );
				AfxEndThread(1);
			}

			// Copy the data from PC to DSP
			if ( pDspMemory )
			{
				// On Windows2000/XP, we have mapped the DSP Memory into PC memory
				fread(VideoFrameBuffer, sizeof(Uint16), sizeof( VideoFrameBuffer )/sizeof(Uint16), DataFile);
				memcpy( pDspMemory, VideoFrameBuffer,  sizeof( VideoFrameBuffer ) );
			}
			// Now we can release the DSP memory
		    IEKC64_unlockDSPMem( IekHandle );

			// send video signal to DSP
			thisMessageBuffer[0] = 0x44444444;
			status = IEKC64_sendDspMsg( IekHandle, 0, sizeof(Uint32), thisMessageBuffer );
			if( !IEKC64_SUCCESS(status) )
			{
				SetDlgItemText(IDC_MessageDisplay,"Sending video message to DSP Failed!");
				printf("Error : Sending failed : 0x%08X : %s\n", status, IEKC64_getErrorString(status) );
				Sleep(1000);
				exit_this_demo=TRUE;
			}
		
			// wait for unlock signal
			status = waitMessage();
			if ( !IEKC64_SUCCESS(status) )
			{
				SetDlgItemText(IDC_MessageDisplay,"Error when waiting for DSP running message!");
				exit_this_demo=TRUE;
				return;
			}
			// if the received message is 0xAAAAAAAA
			if ( MessageBuffer[0] == 0xAAAAAAAA )
			{
				s.Format("Sent Video Frame to DSP. Index: %d...", index++);
				SetDlgItemText(IDC_MessageDisplay,s);
			}
		else
		{
			SetDlgItemText(IDC_MessageDisplay,"Error : Received unknown message !");
			exit_this_demo=TRUE;
			return;
		}


	CDialog::OnTimer(nIDEvent);
}

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;
}




⌨️ 快捷键说明

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