📄 sicommon_amcc_lnx.cpp
字号:
///////////////////////////////////////////////////////////////////////////////
// sicommon_amcc_lnx.cpp
//
// Description:
// Common hardware access routines for AMCC. These calls are AMCC specific.
// These are aka API functions.
//
// Revision History:
// 2002-10-01: mik
// Created
// 2002-10-07: mik
// Passthrough read/write parameter order is now
// region, count, offset, data.
// 2002-10-29: mik
// Added dual-access calls. Removed unnecessary comments.
// 2002-11-18: mik
// Changed assignment to 0 from NULL in SI_AMCC_CloseDriver.
///////////////////////////////////////////////////////////////////////////////
#include <stdio.h> // for sprintf
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <signal.h>
#include <time.h>
#include <malloc.h>
#include "../drivers/linux/ioctldef.h"
#include "sicommon_amcc.h"
#define TRUE 1
#define FALSE 0
///////////////////////////////////////////////////////////////////////////////
// Performance timers. These globals are set used each time an API function is
// entered and exited. Read the difference to determine the relative times
// of each API function execution. They can give numbers such as words
// transferred per second, etc. Note that these values are only valid on
// successful completion of the function. For error conditions, these are
// invalid. Although you can get these values throuhg use of extern and
// perform subtraction yourself, use SIHW_AMCC_ConvertBenchmarkToSeconds
// instead to get the actual time.
UINT32 gSI_AMCC_BenchmarkTime0, gSI_AMCC_BenchmarkTime1;
///////////////////////////////////////////////////////////////////////////////
// INT32 SIHW_AMCC_GetBenchmarkTime
//
// Description:
// Returns the benchmark time difference in seconds. Note that the
// parameters are upper UINT32 and lower UINT32 since the benchmark time
// is a 64 bit entity.
//
// Parameters:
// UINT32 elapsedTime : 32 bit number for number of msec elapsed.
INT32 SIHW_AMCC_ConvertBenchmarkToSeconds ( UINT32 *elapsedMsec )
{
*elapsedMsec = gSI_AMCC_BenchmarkTime1 - gSI_AMCC_BenchmarkTime0;
return e_Err_NoError;
}
///////////////////////////////////////////////////////////////////////////////
// INT32 SI_AMCC_Sleep
//
// Description:
// Puts this process in sleep for sleepMSec
//
// Parameters:
// UINT32 sleepMSec : number of milliseconds to sleep.
INT32 SI_AMCC_Sleep( UINT32 sleepMSec )
{
usleep(sleepMSec * 1000);
return e_Err_NoError;
}
///////////////////////////////////////////////////////////////////////////////
// INT32 SI_AMCC_SetupCallbackEvent
//
// Description:
//
// Parameters:
// PAMCCDevice pAMCCDev : Handle to the device driver.
INT32 SI_AMCC_SetupCallbackEvent(PAMCCDevice pAMCCDev, void *callbackFunction)
{
pAMCCDev->callback.sa_handler = (__sighandler_t)callbackFunction;
sigemptyset( &pAMCCDev->callback.sa_mask );
pAMCCDev->callback.sa_flags = SA_RESTART;
if( sigaction(SIGIO, &pAMCCDev->callback, 0) != 0 )
return e_Err_MiscError;
return e_Err_NoError;
}
///////////////////////////////////////////////////////////////////////////////
// INT32 SI_AMCC_OpenDriver
//
// Description:
// Attempts to link a valid driver to our program
//
// Parameters:
// UINT32 board ID Number of the board
// PAMCCDevice pAMCCDev Handle to the driver
//
INT32 SI_AMCC_OpenDriver( UINT32 board, PAMCCDevice pAMCCDev )
{
char dev_name[1024];
int flags = -1;
// following must be initialized before calling this function.
//pAMCCDev->callback.sa_handler = callback_Function_Name;
sprintf(dev_name, "/dev/siamcc%d", board);
pAMCCDev->hDevice = open(dev_name, O_RDWR);
if(pAMCCDev->hDevice <= 0)
return e_Err_DeviceNotFound; //open() failed
if(fcntl(pAMCCDev->hDevice, F_SETOWN, getpid()) != 0)
{
SI_AMCC_CloseDriver(pAMCCDev);
return e_Err_DeviceNotFound; //Unable to set process ID
}
flags = fcntl(pAMCCDev->hDevice, F_GETFL);
if(flags == -1)
{
SI_AMCC_CloseDriver(pAMCCDev);
return e_Err_DeviceNotFound; //Unable to get flags
}
if(fcntl(pAMCCDev->hDevice, F_SETFL, flags | FASYNC) == -1)
{
SI_AMCC_CloseDriver(pAMCCDev);
return e_Err_DeviceNotFound; //Unable to set flags
}
return e_Err_NoError;
}
///////////////////////////////////////////////////////////////////////////////
// INT32 SI_AMCC_CloseDriver
//
// Description:
//
// Parameters:
// PAMCCDevice pAMCCDev : Handle to the device driver.
INT32 SI_AMCC_CloseDriver( PAMCCDevice pAMCCDev )
{
close(pAMCCDev->hDevice);
pAMCCDev->hDevice = 0;//(int)NULL;
return e_Err_NoError;
}
///////////////////////////////////////////////////////////////////////////////
// INT32 SI_AMCC_ReadFIFOBusMastered
//
// Description:
//
// Parameters:
// PAMCCDevice pAMCCDev : Handle to the device driver.
// UINT32 count : Number of UINT32 to access.
// UINT32 addonAddr: Addon address (byte offset).
// UINT32 *hostAddr: Pointer to host memory where the data resides.
INT32 SI_AMCC_ReadFIFOBusMastered
(
PAMCCDevice pAMCCDev,
UINT32 count, UINT32 *hostAddr
)
{
UINT32 driverParams[SI_PARAMS_COUNT];
driverParams[0] = count;
driverParams[1] = (UINT32)hostAddr;
if( ioctl(pAMCCDev->hDevice, BUSMASTERED_READ, driverParams) == -1 )
return e_Err_BusmasterReadError;
return e_Err_NoError;
}
///////////////////////////////////////////////////////////////////////////////
// INT32 SI_AMCC_WriteFIFOBusMastered
//
// Description:
//
// Parameters:
// PAMCCDevice pAMCCDev : Handle to the device driver.
// UINT32 count : Number of UINT32 to access.
// UINT32 addonAddr: Addon address (byte offset).
// UINT32 *hostAddr: Pointer to host memory where the data resides.
INT32 SI_AMCC_WriteFIFOBusMastered
(
PAMCCDevice pAMCCDev,
UINT32 count, UINT32 *hostAddr
)
{
UINT32 driverParams[SI_PARAMS_COUNT];
driverParams[0] = count;
driverParams[1] = (UINT32)hostAddr;
if( ioctl(pAMCCDev->hDevice, BUSMASTERED_WRITE, driverParams) == -1 )
return e_Err_BusmasterWriteError;
return e_Err_NoError;
}
///////////////////////////////////////////////////////////////////////////////
// INT32 SI_AMCC_WaitForBusmasteredRead
//
// Description:
//
// Parameters:
// PAMCCDevice pAMCCDev : Handle to the device driver.
// UINT32 count : bytes requested
INT32 SI_AMCC_WaitForBusmasteredRead
(
PAMCCDevice pAMCCDev,
UINT32 dwordCount
)
{
UINT32 driverParams[SI_PARAMS_COUNT];
driverParams[0] = TRUE;
while(driverParams[0])
{
ioctl(pAMCCDev->hDevice, BUSMASTERED_READ_DONE, driverParams);
}
// Linux BM calls are blocking, so this always returns no error.
return e_Err_NoError;
}
///////////////////////////////////////////////////////////////////////////////
INT32 SI_AMCC_WaitForBusmasteredWrite
(
PAMCCDevice pAMCCDev,
UINT32 dwordCount
)
{
UINT32 driverParams[SI_PARAMS_COUNT];
driverParams[0] = TRUE;
while(driverParams[0])
{
ioctl(pAMCCDev->hDevice, BUSMASTERED_WRITE_DONE, driverParams);
}
// Linux BM calls are blocking, so this always returns no error.
return e_Err_NoError;
}
///////////////////////////////////////////////////////////////////////////////
// INT32 SI_AMCC_CancelBusMastering
//
// Description:
//
// Parameters:
// PAMCCDevice pAMCCDev : Handle to the device driver.
// todo: fix this. this needs work.
INT32 SI_AMCC_CancelBusMastering
(
PAMCCDevice pAMCCDev
)
{
return e_Err_UnknownCommand;
}
///////////////////////////////////////////////////////////////////////////////
// INT32 SI_AMCC_SetTimeout
//
// Description: Bus Mastering Timeout in ms
//
// Parameters:
// PAMCCDevice pAMCCDev : Handle to the device driver.
// value : Timeout value to set in driver.
INT32 SI_AMCC_SetTimeout
(
PAMCCDevice pAMCCDev,
UINT32 value
)
{
UINT32 *fifo_info = &value;
if( ioctl(pAMCCDev->hDevice, BUSMASTERED_TIMEOUT, fifo_info) == -1 )
return e_Err_MiscError;
return e_Err_NoError;
}
///////////////////////////////////////////////////////////////////////////////
// INT32 SI_AMCC_ReadPCI_OpReg
//
// Description:
//
// Parameters:
// PAMCCDevice pAMCCDev : Handle to the device driver.
// UINT32 count : Number of UINT32 to access.
// UINT32 opRegNum : OpReg to access (byte offset).
// UINT32 *data : Pointer to host memory where the data resides.
INT32 SI_AMCC_ReadPCI_OpReg
(
PAMCCDevice pAMCCDev,
UINT32 count, UINT32 opRegNum, UINT32 *data
)
{
UINT32 opreg_info[2], cnt;
for (cnt=0; cnt<count; cnt++)
{
opreg_info[0] = ( opRegNum >> 2 ) + cnt;
if(ioctl(pAMCCDev->hDevice, OPREG_READ, opreg_info) == -1)
return e_Err_OpregReadError;
data[cnt] = opreg_info[1];
}
return e_Err_NoError;
}
///////////////////////////////////////////////////////////////////////////////
// INT32 SI_AMCC_WritePCI_OpReg
//
// Description:
//
// Parameters:
// PAMCCDevice pAMCCDev : Handle to the device driver.
// UINT32 count : Number of UINT32 to access.
// UINT32 opRegNum : OpReg to access (byte offset).
// UINT32 *data : Pointer to host memory where the data resides.
INT32 SI_AMCC_WritePCI_OpReg
(
PAMCCDevice pAMCCDev,
UINT32 count, UINT32 opRegNum, UINT32 *data
)
{
UINT32 opreg_info[2], cnt;
for (cnt=0; cnt<count; cnt++)
{
opreg_info[0] = ( opRegNum >> 2 ) + cnt;
opreg_info[1] = data[cnt];
if(ioctl(pAMCCDev->hDevice, OPREG_WRITE, opreg_info) == -1)
return e_Err_OpregWriteError;
}
return e_Err_NoError;
}
///////////////////////////////////////////////////////////////////////////////
// INT32 SI_AMCC_ReadFIFOOpReg
//
// Description:
//
// Parameters:
// PAMCCDevice pAMCCDev : Handle to the device driver.
// UINT32 count : Number of UINT32 to access.
// UINT32 *data : Pointer to host memory where the data resides.
INT32 SI_AMCC_ReadFIFOOpReg
(
PAMCCDevice pAMCCDev,
UINT32 count, UINT32 *data
)
{
UINT32 *fifo_info=data, cnt;
for (cnt=0; cnt<count; cnt++, fifo_info++)
{
if(ioctl(pAMCCDev->hDevice, FIFO_READ, fifo_info) == -1)
return e_Err_OpregReadError;
}
return e_Err_NoError;
}
///////////////////////////////////////////////////////////////////////////////
// INT32 SI_AMCC_WriteFIFOOpReg
//
// Description:
//
// Parameters:
// PAMCCDevice pAMCCDev : Handle to the device driver.
// UINT32 count : Number of UINT32 to access.
// UINT32 *data : Pointer to host memory where the data resides.
INT32 SI_AMCC_WriteFIFOOpReg
(
PAMCCDevice pAMCCDev,
UINT32 count, UINT32 *data
)
{
UINT32 *fifo_info=data, cnt;
for (cnt=0; cnt<count; cnt++, fifo_info++)
{
if(ioctl(pAMCCDev->hDevice, FIFO_WRITE, fifo_info) == -1)
return e_Err_OpregWriteError;
}
return e_Err_NoError;
}
///////////////////////////////////////////////////////////////////////////////
// INT32 SI_AMCC_ReadMailbox
//
// Description:
//
// Parameters:
// PAMCCDevice pAMCCDev : Handle to the device driver.
// UINT32 count : Number of UINT32 to access.
// UINT32 mailboxNum: Mailbox number (1 to 8).
// UINT32 *data : Pointer to host memory where the data resides.
INT32 SI_AMCC_ReadMailbox
(
PAMCCDevice pAMCCDev,
UINT32 count, UINT32 mailboxNum, UINT32 *data
)
{
UINT32 opreg_info[2], cnt;
for(cnt=0; cnt<count; cnt++)
{
opreg_info[0] = mailboxNum + cnt;
if(ioctl(pAMCCDev->hDevice, MAILBOX_READ, opreg_info) == -1)
return e_Err_OpregReadError;
data[cnt] = opreg_info[1];
}
return e_Err_NoError;
}
///////////////////////////////////////////////////////////////////////////////
// INT32 SI_AMCC_WriteMailbox
//
// Description:
//
// Parameters:
// PAMCCDevice pAMCCDev : Handle to the device driver.
// UINT32 count : Number of UINT32 to access.
// UINT32 mailboxNum: Mailbox number.
// UINT32 *data : Pointer to host memory where the data resides.
INT32 SI_AMCC_WriteMailbox
(
PAMCCDevice pAMCCDev,
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -