📄 addma.cpp
字号:
/*
****************************************************************************************
* Program : ADDMA.CPP *
* Description : Demo program for analog input function with DMA triggering *
* Revision : 1.00 *
* Date : 7/8/1999 Advantech Co., Ltd. *
****************************************************************************************
*/
// This Demo program is rewrited by Huang, Yingsong in July, 2003
#include <windows.h>
#include <windef.h>
#include <stdio.h>
#include <conio.h>
#include "..\..\..\include\driver.h"
//Static variable defined
static USHORT gwEvtFlag = 1; // Event flag
static USHORT gwCyclicCount= 0; // Number of cyclic count
static USHORT gwCyclicMode = 0; // Cyclic or non-cyclic mode
static PT_EnableEvent ptEnableEvent;
static PT_CheckEvent ptCheckEvent;
static BOOL bThreadloop = FALSE;
static BOOL bThreadFlag = FALSE; //the flag of the thread
static PT_FAITransfer ptFAITransfer;
// Buffer defined
float *pfUserBuf;
// Error variable defined
char szErrMsg[80];
DWORD dwErrCde;
LONG ErrCde;
long lDriverHandle; //DriverHandle defined
/******************
* Local function declare
******************/
void ErrorHandler(DWORD dwErrCde);
BOOL AllocateDataBuffer(long, int, int, long*, void**);
void FreeDataBuffer(long lDrvHandle, long *plDMABuf, void **pUserBuf);
void ErrorStop(long*, DWORD, long *, void**);
//Thread function declare:
void UserThread(LPVOID );
// Event functions declare:
void adInterruptEvent();
void adBufChangeEvent(int );
void adOverrunEvent();
void adTerminateEvent(int );
//Cyclic mode function declare
void GetStatus(int );
void Stop(int );
void main()
{
ULONG lDevNum = 0;
USHORT usChan = 0;
int iSamples = 0;
long lDmaBuf; //DMA buffer defined
PT_FAIDmaStart ptFAIDmaStart;
HANDLE hThreadHandle;
DWORD dwThreadID;
char cAnswer;
//Step 1: Display hardware and software settings for running this example
printf("\n\t\t\tADMA CONSOLE MODE SAMPLE v2.0\n\n");
printf("Before running this example,\n");
printf("please use the device installation utility to add the device on Device Number.\n\n");
printf("Add completed?(Y/N):");
scanf("%c", &cAnswer);
if (cAnswer!='y' && cAnswer!='Y') {
printf("Please use the device installation utility first\n");
printf("Program terminated\n");
printf("Press any key to exit....");
getch();
return;
}
//Step 2: Input parameters
fflush(stdin);
printf("\nPlease input parameters:");
printf("\nDevice Number: ");
scanf("%d", &lDevNum);
fflush(stdin);
printf("Input Channel: ");
scanf("%d", &usChan);
fflush(stdin);
printf("Cyclic model? (noncyclic:0, cyclic:1):");
scanf("%d", &gwCyclicMode);
fflush(stdin);
printf("\nSampling counts, should larger than FIFO size [ > 2048] \nand the multiple of 2: "); //Modified by Huang,Yingsong 7/24/03
scanf("%d", &iSamples); //Modified by Huang, Yingsong 7/24/03
fflush(stdin);
while (iSamples<=2048 || iSamples%2!=0) {
printf("Wrong input, the input value should greater than 2048 \nand the multiple of 2, input again:");
scanf("%d", &iSamples);
fflush(stdin);
}
//Step 3: Open device
dwErrCde = DRV_DeviceOpen(lDevNum, &lDriverHandle); // device
if (dwErrCde != SUCCESS)
{
ErrorHandler(dwErrCde);
printf("Program terminated!\n");
printf("Press any key to exit....");
getch();
return ;
}
//Step 4: Allocate DMA & data buffer for DMA transfer
if( AllocateDataBuffer(
lDriverHandle, //Driver handle
iSamples, //Data count
sizeof(float), //Size of one data
&lDmaBuf, //DMA buffer allocated
(void**)&pfUserBuf)==false) //User buffer allocated
{
printf("Program terminated\n");
DRV_DeviceClose(&lDriverHandle);
printf("Press any key to exit....");
getch();
return;
}
//Step5: Enable event
ptEnableEvent.EventType = ADS_EVT_INTERRUPT |
ADS_EVT_BUFCHANGE |
ADS_EVT_TERMINATED |
ADS_EVT_OVERRUN;
ptEnableEvent.Enabled = gwEvtFlag;
ptEnableEvent.Count = 512;
if ((ErrCde = DRV_EnableEvent(lDriverHandle,
(LPT_EnableEvent)&ptEnableEvent)) != 0)
{
DRV_GetErrorMessage(ErrCde,(LPSTR)szErrMsg);
printf("\n%s\n", szErrMsg);
FreeDataBuffer(lDriverHandle, &lDmaBuf, (void **)&pfUserBuf);
DRV_DeviceClose((LONG far *)&lDriverHandle);
return;
}
// Step 6: Start DMA transfer
ptFAIDmaStart.TrigSrc = 0; // 0: internal trigger, 1: external trigger
ptFAIDmaStart.SampleRate = 4000; // pacer rate: 1KHz
ptFAIDmaStart.chan = usChan; // channel for input
ptFAIDmaStart.gain = 0; // gain code: 0 (+-5V)
ptFAIDmaStart.count = iSamples; // number of samples //Modified by Huang,Yingsong 7/25/03
ptFAIDmaStart.buffer = (USHORT*)lDmaBuf; // data buffer pointer
dwErrCde = DRV_FAIDmaStart(lDriverHandle, &ptFAIDmaStart);
if (dwErrCde != SUCCESS)
{
ErrorStop(&lDriverHandle, dwErrCde, &lDmaBuf, (void**)&pfUserBuf);
return;
}
gwCyclicCount= 0;
//Step7: Start the Thread
if (gwEvtFlag)
{
hThreadHandle = CreateThread(0, 0,
(LPTHREAD_START_ROUTINE) UserThread,
&iSamples, 0, (LPDWORD)&dwThreadID);
bThreadFlag = TRUE;
bThreadloop = TRUE;
}
Sleep(0);
//Step8: Wait untill the DMA transmition completed or Ueser's command
do{
if (1 == gwCyclicMode) {
printf("Cyclic model\n");
printf("Status or Stop? (a:b):");
cAnswer = getch();
if ('a' == cAnswer) {
GetStatus(iSamples);
}else if('b' == cAnswer){
Stop(iSamples);
}
}
}while(TRUE == bThreadFlag);
// Step 9: Free buffer
FreeDataBuffer(lDriverHandle, &lDmaBuf, (void **)&pfUserBuf);
// Step 10: Close device
dwErrCde = DRV_DeviceClose(&lDriverHandle);
printf("\nPress any key to exit....");
getch();
CloseHandle(hThreadHandle);
}
/**********************************************************************
* Function: ErrorHandler
* Show the error message of corresponding errcode
* input: dwErrCde, IN, Error code
* return: none
**********************************************************************/
void ErrorHandler(DWORD dwErrCde)
{
char szErrMsg[180];
DRV_GetErrorMessage(dwErrCde, szErrMsg);
printf("\nError(%d): %s\n", dwErrCde & 0xffff, szErrMsg);
}
/**********************************************************************
* Function: AllocateDataBuffer
* Allocate data buffer for DMA transfer.
* Paramaters: lDrvHandle, IN, Driver handle
* iSamples, IN, Data count.
* iDataSize, IN, Size of one data
* plDMABuf, OUT, DMA buffer pointer.
* needed for internal DMA transfer.
* pUserBuf, OUT, user data buffer. Converted data is stored
* here.
* return: TRUE - memory allocate correctly.
* FALSE - allocate error
**********************************************************************/
BOOL AllocateDataBuffer(
long lDrvHandle,
int iSamples,
int iDataSize,
long *plDMABuf,
void **pUserBuf)
{
PT_AllocateDMABuffer ptAllocDMA;
unsigned long lActualSize;
DWORD dwErrCde;
// Allocate DMA buffer for DMA transfer
ptAllocDMA.CyclicMode = gwCyclicMode; // 0: non-cyclic mode, 1: cyclic-mode
ptAllocDMA.RequestBufSize = iSamples * sizeof(USHORT); // request buffer size, double for 12bits
ptAllocDMA.ActualBufSize = &lActualSize; // actual returned buffer size
ptAllocDMA.buffer = (long*)plDMABuf; // returned buffer address
dwErrCde = DRV_AllocateDMABuffer(lDrvHandle, &ptAllocDMA);
if (dwErrCde != SUCCESS)
{
ErrorHandler(dwErrCde);
return(false);
}
// Allocate memory for user buffer.
*pUserBuf = malloc(iSamples * iDataSize);
if (pUserBuf == NULL)
{
DRV_FreeDMABuffer(lDrvHandle, (LPARAM)plDMABuf);
printf("\nError: Allocate memory error.\n");
return(false);
}
return(true);
}//AllocateDataBuffer
/**********************************************************************
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -