test_tmx320c6412.cpp
来自「网友写的一个PCI卡,基于PCI总线的驱动」· C++ 代码 · 共 580 行
CPP
580 行
// Test_TMX320C6412.cpp
//
// Generated by DriverWizard version DriverStudio 2.7.0 (Build 562)
//
// This console application demonstrates how to open a handle
// to a device in your driver, and communicate with the driver
// using Read, Write, and DeviceIoControl calls, as appropriate.
//
// This test program attempts to open the device using the
// GUID defined in "..\TMX320C6412Deviceinterface.h"
#include <stdlib.h>
#include <stdio.h>
#include <windows.h>
#include <winioctl.h>
#include "vDsp.h"
#include "..\TMX320C6412ioctl.h"
#include "..\TMX320C6412Deviceinterface.h" // Has class GUID definition
// This function is found in module OpenByIntf.cpp
HANDLE OpenByInterface(GUID* pClassGuid, DWORD instance, PDWORD pError);
typedef void VOIDFUNC();
void CloseIfOpen(void);
// Global data
#define N_IOCODES 8
// Names of IOCTL codes
//
char *IOnames[N_IOCODES+1] =
{
//=== Parameterized IOCTL Example ===
// "IOCTL_PARAMETERIZED",
"TMX320C6412_IOCTL_CFG_READ",
"TMX320C6412_IOCTL_CFG_WRITE",
"TMX320C6412_IOCTL_BASE0_READ",
"TMX320C6412_IOCTL_BASE0_WRITE",
"TMX320C6412_IOCTL_BASE1_READ",
"TMX320C6412_IOCTL_BASE1_WRITE",
"TMX320C6412_IOCTL_BASE2_READ",
"TMX320C6412_IOCTL_BASE2_WRITE",
""
};
// IOCTL codes
//
int IOcodes[N_IOCODES+1] =
{
//=== Parameterized IOCTL Example ===
// IOCTL_PARAMETERIZED,
TMX320C6412_IOCTL_CFG_READ,
TMX320C6412_IOCTL_CFG_WRITE,
TMX320C6412_IOCTL_BASE0_READ,
TMX320C6412_IOCTL_BASE0_WRITE,
TMX320C6412_IOCTL_BASE1_READ,
TMX320C6412_IOCTL_BASE1_WRITE,
TMX320C6412_IOCTL_BASE2_READ,
TMX320C6412_IOCTL_BASE2_WRITE,
0
};
// Handle to device opened in driver.
//
HANDLE hDevice = INVALID_HANDLE_VALUE;
// Class GUID used to open device
//
GUID ClassGuid = TMX320C6412Device_CLASS_GUID;
////////////////////////////////////////////////////////////////////////
// Exit
//
// Print a message and exit
//
void
Exit(int res)
{
printf("Exiting...\n\n");
CloseIfOpen();
exit(res);
}
int
iDspPciInit()
{
USHORT command = 0;
PciReadConfig (0x04, 2, &command);
command |= 0x3;
PciWriteConfig (0x04, 2, &command);
PciReadConfig (0x04, 2, &command);
printf ("command:%04X\n", command);
return 0;
}
/*warm reset the DSP*/
void
vDspReset()
{
int i = 0;
CHAR value = 0x01;
vDspIOWriteBYTE (0x04, 1, &value);
for(i=0; i<100000; i++) {
/*wait 18 DSP cycle to get response*/
}
}
/*send DSPINT to start the execution*/
void
vDspStart()
{
CHAR value = 0x02;
vDspIOWriteBYTE (0x04, 1, &value);
printf("DSP started!\n");
}
void
vDspRegWrite32(UINT32 reg, ULONG value)
{
UINT32 addr = reg-0x01800000UL;
vDspRegWrite (addr, 1, &value);
}
void
vDspRegRead32(UINT32 reg, ULONG *value)
{
UINT32 addr = reg-0x01800000UL;
vDspRegRead (addr, 1, value);
}
void vDspEmifInit()
{
ULONG data[12];
UINT32 EmifA[12] = { 0x00092078UL, /* GCTL - 0x01800000*/
0xF3A88E02UL, /* CE1 - 0x01800004*/
0xFFFFFFDEUL, /* CE0 - 0x01800008*/
/* Address split 3*/
0x22a28a22UL, /* CE2 - 0x01800010*/
0x22a28a42UL, /* CE3 - 0x01800014*/
0x57115000UL, /* SDRAMCTL - 0x01800018*/
0x0000081bUL, /* SDRAMTIM - 0x0180001c*/
0x000a8529UL, /* SDRAMEXT - 0x01800020*/
/* Address split 9*/
0x00000002UL, /* CE1ECCTL - 0x01800044*/
0x00000002UL, /* CE0ECCTL - 0x01800048*/
/* Address split */
0x00000002UL, /* CE2ECCTL - 0x01800050*/
0x00000073UL, /* CE3ECCTL - 0x01800054*/
};
vDspRegWrite32(0x01800000UL, EmifA[0]);
vDspRegWrite32(0x01800004UL, EmifA[1]);
vDspRegWrite32(0x01800008UL, EmifA[2]);
vDspRegWrite32( 0x01800010UL, EmifA[3] );
vDspRegWrite32( 0x01800014UL, EmifA[4] );
vDspRegWrite32( 0x01800018UL, EmifA[5] );
vDspRegWrite32( 0x0180001CUL, EmifA[6] );
vDspRegWrite32( 0x01800020UL, EmifA[7] );
vDspRegWrite32( 0x01800044UL, EmifA[8] );
vDspRegWrite32( 0x01800048UL, EmifA[9] );
vDspRegWrite32( 0x01800050UL, EmifA[10] );
vDspRegWrite32( 0x01800054UL, EmifA[11] );
vDspRegRead32(0x01800000UL, &data[0]);
vDspRegRead32(0x01800004UL, &data[1]);
vDspRegRead32(0x01800008UL, &data[2]);
vDspRegRead32( 0x01800010UL, &data[3] );
vDspRegRead32( 0x01800014UL, &data[4] );
vDspRegRead32( 0x01800018UL, &data[5] );
vDspRegRead32( 0x0180001CUL, &data[6] );
vDspRegRead32( 0x01800020UL, &data[7] );
vDspRegRead32( 0x01800044UL, &data[8] );
vDspRegRead32( 0x01800048UL, &data[9] );
vDspRegRead32( 0x01800050UL, &data[10] );
vDspRegRead32( 0x01800054UL, &data[11] );
for (int i=0; i<12; i++) {
if (data[i] != EmifA[i])
printf ("%d : %08X %08X\r\n", i, data[i], EmifA[i]);
}
}
USHORT DSPP = 0;
////////////////////////////////////////////////////////////////////////
// Main entry point
//
//
int __cdecl
main(int argc, char *argv[])
{
DWORD Error;
int i;
CHAR mode = 0;
USHORT tmp = 0;
ULONG tmp2[100];
printf("Test application Test_TMX320C6412 starting...\n");
hDevice = OpenByInterface( &ClassGuid, 0, &Error);
if (hDevice == INVALID_HANDLE_VALUE) {
printf("ERROR opening device: (%0x) returned from CreateFile\n", GetLastError());
Exit(1);
} else
printf("Device found, handle open.\n");
iDspPciInit ();
/*warm reset the dsp*/
vDspReset();
vDspEmifInit ();
DSPP = 0x200;
vDspIOWriteWord (0x08, 1, &DSPP);
vDspIOReadWord (0x08, 1, &tmp);
/*
memset (tmp2, 0, sizeof (ULONG)*100);
for (i=0; i<0x400000; i+=100)
vDspMemoryWrite (i, 100, tmp2);
*/
DSPP = 0;
vDspIOWriteWord (0x08, 1, &DSPP);
/*
for (i=0; i<0x40000; i+=100)
vDspMemoryWrite (i, 100, tmp2);
*/
vDspIOReadByte (0x04, 1, &mode);
if (!(mode&0x4)) {
printf("Not in PCI boot mode\n");
Exit (0);
}
/*load .out file to DSP memory*/
ulDspLoad("C:\\TMX320C6412\\exe\\test.out");
/*start dsp*/
vDspStart();
for (i=0; i<10; i++) {
vDspMemoryWrite (0x20000, 100, tmp2);
printf ("%08X\n", tmp2[0]);
Sleep (500);
}
Exit (0);
return 0;
}
////////////////////////////////////////////////////////////////////////
// CloseIfOpen
//
// Close the device if we previously opened a handle to it.
//
void CloseIfOpen(void)
{
if (hDevice != INVALID_HANDLE_VALUE)
{
// Close the handle to the driver
if (!CloseHandle(hDevice))
{
printf("ERROR: CloseHandle returns %0x.\n", GetLastError());
}
hDevice = INVALID_HANDLE_VALUE;
}
}
#define IOCTL_INBUF_SIZE 512
#define IOCTL_OUTBUF_SIZE 512
//=== Parameterized IOCTL Example ===
//void Test_IOCTL_PARAMETERIZED(int nVal, ULONG dwVal)
//{
// Function body same as other IOCTL handlers, with command line
// parameters 'nVal' and 'dwVal' available as input.
//}
void
PciReadConfig (UINT32 offset, UINT32 size, PVOID data)
{
CHAR bufOutput[IOCTL_OUTBUF_SIZE]; // Output from device
ULONG nOutput; // Count written to bufOutput
if (!DeviceIoControl(hDevice,
TMX320C6412_IOCTL_CFG_READ,
NULL,
NULL,
bufOutput,
IOCTL_OUTBUF_SIZE,
&nOutput,
NULL)
)
{
printf("ERROR: DeviceIoControl returns %0x.", GetLastError());
}
memcpy (data, bufOutput+offset, size);
}
void
PciWriteConfig (UINT32 offset, UINT32 size, PVOID data)
{
CHAR bufInput[IOCTL_INBUF_SIZE]; // Input to device
ULONG nOutput; // Count written to bufOutput
PULONG ptmp = (PULONG)bufInput;
ptmp[0] = offset;
ptmp[1] = size;
memcpy (bufInput+8, data, size);
if (!DeviceIoControl(hDevice,
TMX320C6412_IOCTL_CFG_WRITE,
bufInput,
IOCTL_INBUF_SIZE,
NULL,
NULL,
&nOutput,
NULL)
)
{
printf("ERROR: DeviceIoControl returns %0x.", GetLastError());
}
}
void
vDspMemoryRead (ULONG address, ULONG size, ULONG data[])
{
ULONG bufInput[2]; // Input to device
ULONG nOutput; // Count written to bufOutput
bufInput[0] = address;
bufInput[1] = size;
if (!DeviceIoControl(hDevice,
TMX320C6412_IOCTL_BASE0_READ,
bufInput,
sizeof (bufInput),
data,
size,
&nOutput,
NULL)
)
{
printf("ERROR: DeviceIoControl returns %0x.", GetLastError());
}
}
void
vDspMemoryWrite(ULONG address, ULONG size, ULONG data[])
{
ULONG bufInput[IOCTL_INBUF_SIZE]; // Input to device
ULONG nOutput; // Count written to bufOutput
ULONG i;
bufInput[0] = address;
bufInput[1] = size;
for (i=0; i<size; i++)
bufInput[2+i] = data[i];
if (!DeviceIoControl(hDevice,
TMX320C6412_IOCTL_BASE0_WRITE,
bufInput,
IOCTL_INBUF_SIZE,
NULL,
0,
&nOutput,
NULL)
)
{
printf("ERROR: DeviceIoControl returns %0x.", GetLastError());
}
}
void
vDspRegRead (ULONG address, ULONG size, ULONG data[])
{
ULONG bufInput[2]; // Input to device
ULONG nOutput; // Count written to bufOutput
bufInput[0] = address;
bufInput[1] = size;
if (!DeviceIoControl(hDevice,
TMX320C6412_IOCTL_BASE1_READ,
bufInput,
sizeof (bufInput),
data,
size,
&nOutput,
NULL)
)
{
printf("ERROR: DeviceIoControl returns %0x.", GetLastError());
}
}
void
vDspRegWrite(ULONG address, ULONG size, ULONG data[])
{
ULONG bufInput[IOCTL_INBUF_SIZE]; // Input to device
ULONG nOutput; // Count written to bufOutput
ULONG i;
bufInput[0] = address;
bufInput[1] = size;
for (i=0; i<size; i++)
bufInput[2+i] = data[i];
if (!DeviceIoControl(hDevice,
TMX320C6412_IOCTL_BASE1_WRITE,
bufInput,
IOCTL_INBUF_SIZE,
NULL,
0,
&nOutput,
NULL)
)
{
printf("ERROR: DeviceIoControl returns %0x.", GetLastError());
}
}
void
vDspIOReadByte (ULONG address, ULONG size, CHAR data[])
{
ULONG bufInput[3]; // Input to device
ULONG nOutput; // Count written to bufOutput
bufInput[0] = address;
bufInput[1] = size;
bufInput[2] = 0;
if (!DeviceIoControl(hDevice,
TMX320C6412_IOCTL_BASE2_READ,
bufInput,
sizeof (bufInput),
data,
size,
&nOutput,
NULL)
)
{
printf("ERROR: DeviceIoControl returns %0x.", GetLastError());
}
}
void
vDspIOWriteBYTE(ULONG address, ULONG size, CHAR data[])
{
ULONG bufInput[IOCTL_INBUF_SIZE]; // Input to device
ULONG nOutput; // Count written to bufOutput
bufInput[0] = address;
bufInput[1] = size;
bufInput[2] = 0;
memcpy (&bufInput[3], data, size);
if (!DeviceIoControl(hDevice,
TMX320C6412_IOCTL_BASE2_WRITE,
bufInput,
sizeof (bufInput),
data,
size,
&nOutput,
NULL)
)
{
printf("ERROR: DeviceIoControl returns %0x.", GetLastError());
}
}
void
vDspIOReadWord (ULONG address, ULONG size, USHORT data[])
{
ULONG bufInput[3]; // Input to device
ULONG nOutput; // Count written to bufOutput
bufInput[0] = address;
bufInput[1] = size;
bufInput[2] = 1;
if (!DeviceIoControl(hDevice,
TMX320C6412_IOCTL_BASE2_READ,
bufInput,
sizeof (bufInput),
data,
size,
&nOutput,
NULL)
)
{
printf("ERROR: DeviceIoControl returns %0x.", GetLastError());
}
}
void
vDspIOWriteWord(ULONG address, ULONG size, USHORT data[])
{
ULONG bufInput[IOCTL_INBUF_SIZE]; // Input to device
ULONG nOutput; // Count written to bufOutput
bufInput[0] = address;
bufInput[1] = size;
bufInput[2] = 1;
memcpy (&bufInput[3], data, size*sizeof (USHORT));
if (!DeviceIoControl(hDevice,
TMX320C6412_IOCTL_BASE2_WRITE,
bufInput,
IOCTL_INBUF_SIZE,
NULL,
0,
&nOutput,
NULL)
)
{
printf("ERROR: DeviceIoControl returns %0x.", GetLastError());
}
}
void
vDspIOReadLong (ULONG address, ULONG size, ULONG data[])
{
ULONG bufInput[3]; // Input to device
ULONG nOutput; // Count written to bufOutput
bufInput[0] = address;
bufInput[1] = size;
bufInput[2] = 2;
if (!DeviceIoControl(hDevice,
TMX320C6412_IOCTL_BASE2_READ,
bufInput,
sizeof (bufInput),
data,
size,
&nOutput,
NULL)
)
{
printf("ERROR: DeviceIoControl returns %0x.", GetLastError());
}
}
void
vDspIOWriteLong(ULONG address, ULONG size, ULONG data[])
{
ULONG bufInput[IOCTL_INBUF_SIZE]; // Input to device
ULONG nOutput; // Count written to bufOutput
bufInput[0] = address;
bufInput[1] = size;
bufInput[2] = 2;
memcpy (&bufInput[3], data, size*sizeof (ULONG));
if (!DeviceIoControl(hDevice,
TMX320C6412_IOCTL_BASE2_WRITE,
bufInput,
IOCTL_INBUF_SIZE,
NULL,
0,
&nOutput,
NULL)
)
{
printf("ERROR: DeviceIoControl returns %0x.", GetLastError());
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?