//*****************************************************************************
//
// "$RCSfile: vision_box.h,v $"
//
// (c) 1999 - 2013 IMAGO Technologies GmbH
//*****************************************************************************
// Information by CVS or VSS:
//     Last check in on: $Date: 2013-08-21 07:55:15 $
//     by                $Author: schluckebier $
//     Revision        : $Revision: 1.271 $
//
// CVS: $Source: /Daten/Projekte/CVS/Lib/VIB_Lib/include/vision_box.h,v $
// VSS: $Archive:$
//
// $NoKeywords: $ expanding by VSS -->
//
// CVS: $Revision: 1.271 $
//
//*****************************************************************************
//! \file vision_box.h
//! \brief VIB Library Interface
//! \author (c) 1999 - 2013 IMAGO Technologies GmbH
//!
//! $Revision: 1.271 $
//*****************************************************************************

#ifndef _VISION_BOX_H_
#define _VISION_BOX_H_

#define _VISION_BOX_HID {"vision_box.h", __DATE__, __TIME__, _REVISION($Revision: 1.271 $)}

#include <stdio.h>
#include <stdlib.h>
#include <file.h>
#include <time.h>
#include <c6x.h>

//! Macro: Minimum
#define MIN(x,y) ((x)<(y)?(x):(y))
//! Macro: Maximum
#define MAX(x,y) ((x)>(y)?(x):(y))

#ifndef TRUE
#define TRUE 1
#endif
#ifndef FALSE
#define FALSE 0
#endif
typedef unsigned int BOOL;

#ifdef MPS2
	#define L2_BASE			0x00000000 //!< Start address of L2
	#define L2_SIZE			(1<<20)    //!< Size of L2
	#define SDRAM_BASE		0xA0000000 //!< Start address of SDRAM
	#define SDRAM_BANK0		0xA0000000 //!< Start address of first SDRAM bank
	#define SDRAM_BANK1		0xB0000000 //!< Start address of second SDRAM bank
	#define FLASH_BASE		0x64000000 //!< Start address of flash
#elif defined(GBIT_MODUL)
	#define L2_BASE			0x00000000 //!< Start address of L2
	#define L2_SIZE			(256<<10)  //!< Size of L2
	#define SDRAM_BASE		0xA0000000 //!< Start address of SDRAM
	#define FLASH_BASE		0x90000000 //!< Start address of flash
#elif defined(C6415MODUL)
	#define L2_BASE			0x00000000 //!< Start address of L2
	#define L2_SIZE			(1<<20)    //!< Size of L2
	#define SDRAM_BASE		0xA0000000 //!< Start address of SDRAM
	#define FLASH_BASE		0x64000000 //!< Start address of flash
#elif defined(C6455MODUL)
	#define L1P_BASE		0x00E00000 //!< Start address of L1P
	#define L1P_SIZE		(32<<10)   //!< Size of L1P
	#define L1D_BASE		0x00F00000 //!< Start address of L1D
	#define L1D_SIZE		(32<<10)   //!< Size of L1D
	#define L2_BASE			0x00800000 //!< Start address of L2
	#define L2_SIZE			(2<<20)    //!< Size of L2
	#define SDRAM_BASE		0xE0000000 //!< Start address of SDRAM
	#define FLASH_BASE		0xB0000000 //!< Start address of flash
#elif defined(DM643x)
	#define L1P_BASE		0x00E08000 //!< Start address of L1P
	#define L1P_SIZE		(32<<10)   //!< Size of L1P
	#define L1D_BASE		0x00F04000 //!< Start address of L1D
	#define L1D_SIZE		(80<<10)   //!< Size of L1D
	#define L2_BASE			0x00800000 //!< Start address of L2
	#define L2_SIZE			(128<<10)  //!< Size of L2
	#define SDRAM_BASE		0x80000000 //!< Start address of SDRAM
#elif defined(C6474)
	#define L1P_BASE		0x00E00000 //!< Start address of L1P
	#define L1P_SIZE		(32<<10)   //!< Size of L1P
	#define L1D_BASE		0x00F00000 //!< Start address of L1D
	#define L1D_SIZE		(32<<10)   //!< Size of L1D
	#define L2_BASE			0x00800000 //!< Start address of L2
	#define L2_SIZE			(1<<20)    //!< Size of L2
	#define SDRAM_BASE		0x80000000 //!< Start address of SDRAM
#elif defined(_TMS320C6600)
	#define L1P_BASE		0x00E00000 //!< Start address of L1P
	#define L1P_SIZE		(32<<10)   //!< Size of L1P
	#define L1D_BASE		0x00F00000 //!< Start address of L1D
	#define L1D_SIZE		(32<<10)   //!< Size of L1D
	#define L2_BASE			0x00800000 //!< Start address of L2
	#define L2_SIZE			(512<<10)  //!< Size of L2
	#define MCSRAM_BASE		0x0C000000 //!< Start address of MCSRAM
	#define MCSRAM_SIZE		(4<<20)	   //!< Size of MCSRAM
	#define SDRAM_BASE		0x80000000 //!< Start address of SDRAM
#endif

#define L2CACHELINESIZE 128		//!< Size of one L2 cache line

#ifndef _TMS320C6400_PLUS
	#define DNUM 0				//!< DSP Core Number (always 0 for C641x)
#endif

/*****************************************************************************/
// GlobalBuffer (internal Data Memory)
/*****************************************************************************/

extern far const unsigned int VIB_GlobalBufferSize;
extern far unsigned int VIB_GlobalBuffer[];
extern far unsigned int VIB_UsedBuffersize;

/*****************************************************************************/
//! \addtogroup Other Other Functions
/*****************************************************************************/
//! \{

//! Cache L1/L2 Update
enum eCACHE_UPDATEMODE
{
	CACHE_UPDATEMODE_WRITEBACK = 0x1,			//!<Write back
	CACHE_UPDATEMODE_WRITEBACKINVALIDATE = 0x2,	//!<Write back and invalidate
	CACHE_UPDATEMODE_INVALIDATE = 0x3			//!<Invalidate
};

//! \}

/*****************************************************************************/
//! \addtogroup AnsiC
/*****************************************************************************/
//! \{

//! Keyboard codes
enum eKB_CODES
{
	KB_BACKSP =   8, //!< Backspace
	KB_ENTER =   13, //!< Enter
	KB_ESC =     27, //!< Escape
	KB_UP =     208, //!< Cursor up
	KB_DOWN =   202, //!< Cursor down
	KB_LEFT =   204, //!< Cursor left
	KB_OK =     205,
	KB_RIGHT =  206, //!< Cursor right
	KB_PGUP =   209, //!< Page up
	KB_INS =    200, //!< Insert key
	KB_DEL =    127, //!< Delete key
	KB_PGDOWN = 203, //!< Page down
	KB_HOME =   207, //!< Home
	KB_END =    201, //!< End

	KB_SAVE =   225, //!< Ctrl S
	KB_CUT =    221, //!< Ctrl X
	KB_COPY =   222, //!< Ctrl C
	KB_PASTE =  224, //!< Ctrl V

	KB_F1 =     181,
	KB_F2 =     182,
	KB_F3 =     183,
	KB_F4 =     184,
	KB_F5 =     185,
	KB_F6 =     186,
	KB_F7 =     187,
	KB_F8 =     188,
	KB_F9 =     189,
	KB_F10 =    190,
	KB_F11 =    191,
	KB_F12 =    192
};

extern far int VIB_KB_Shift; //!< State of Shift key
extern far int VIB_KB_Alt;   //!< State of Alt key
extern far int VIB_KB_Ctrl;  //!< State of Ctrl key

//! Keyboard devices
enum eKEYBOARD_DEVICE
{
	KEYBOARD_DEVICE  = 0x100000,	//!< Default keyboard device
	KEYBOARD_DEVICE0 = 0x100000,	//!< Keyboard device on sat A
	KEYBOARD_DEVICE1 = 0x200000,	//!< Keyboard device on sat B
	KEYBOARD_DEVICE2 = 0x400000		//!< Keyboard device on sat C
};

//! \}

//=== Device parameter ===
#define VIB_RS232MAXPORTS 15		//!< Number of maximum R232 ports

/*****************************************************************************/
//! VisionBox hardware types
/*****************************************************************************/
enum eVIB_TYPE
{
	VIB_TYPE_UNKNOWN =		-1,	//!< Unknown type
	VIB_TYPE_C =			0,	//!< VisionBox Compact
	VIB_TYPE_MPS =			1,	//!< VisionBox MPS
	VIB_TYPE_MPS2 =			2,	//!< VisionBox MPS2
	VIB_TYPE_GBIT =			3,	//!< Gigabit Ethernet module
	VIB_TYPE_QUAD =			4,	//!< VisionBox Quad
	VIB_TYPE_COMPACT2 =		5,	//!< VisionBox Compact II
	VIB_TYPE_PSC =			6,	//!< VisionCam PS
	VIB_TYPE_ALYR =			7,	//!< VisionBox AGE
	VIB_TYPE_VCS = 			8,	//!< VisionCam S
	VIB_TYPE_AMC_MODULE =	9,	//!< AMC Module
	VIB_TYPE_AMC_CARRIER =	10,	//!< AMC Carrier
	VIB_TYPE_VCXS =			11,	//!< VisionCam XS
	VIB_TYPE_SM1 =			12, //!< VisionCam SM1
	VIB_TYPE_OCTA =			13  //!< VisionBox OCTA (KeyStone)
};

//! Flash vendors
enum eFLASH_TYPE
{
	FLASH_UNKNOWN =				  0,
	FLASH_TOSHIBA =				100,
	FLASH_TOSHIBA_TC58FVB641 =	101,
	FLASH_AMD =					200,
	FLASH_AMD_LV641DL =			201,
	FLASH_AMD_S29JL032H_01 =	202,
	FLASH_AMD_S29JL032H_02 =	203,
	FLASH_AMD_S29GL032M_R2 =	204,
	FLASH_AMD_S29GL032M_R4 =	205,
	FLASH_AMD_S29AL032D_03 =	206,
	FLASH_AMD_S29GL032N_04 =	207,
	FLASH_ATMEL =				300,
	FLASH_ATMEL_AT49BV322A =	301,
	FLASH_NUMONYX =				400,
	FLASH_NUMONYX_M25P32 =		401,
	FLASH_NUMONYX_N25Q128=		402
};

//! FPGA types
enum eFPGA_TYPE
{
	FPGA_UNKNOWN =		  0, //!< Unknown FPGA
	FPGA_UNUSED =		  1, //!< Not used
	FPGA_EP1C4 =		  2, //!< Altera EP1C4
	FPGA_EP1C12 =		  3, //!< Altera EP1C12
	FPGA_EP2C8 =		  4, //!< Altera EP2C8
	FPGA_EP2C15 =		  5, //!< Altera EP2C15
	FPGA_EP2C20 =		  6, //!< Altera EP2C20
	FPGA_EP2C35 =		  7, //!< Altera EP2C35
	FPGA_EP3C16 =		  8, //!< Altera EP3C16
	FPGA_EP3C40 =		  9, //!< Altera EP3C40
	FPGA_EP3C5 =		  10 //!< Altera EP3C5
};

//! FPGA designs
enum eVBF_DESIGN
{
	VBF_UNKNOWN =		  0, //!< Unknown board
	VBF_SAT_DUMMY =		  1, //!< No board
	VBF_EARTH =			 50, //!< Earth design
	VBF_MOON  =			 51, //!< Moon design
	VBF_SAT_WDVI =		100, //!< DVI board
	VBF_SAT_ANALOG =	102, //!< Analog board
	VBF_SAT_CLIO =		200, //!< CameraLink In/Out board
	VBF_SAT_CLIN =		201, //!< CameraLink In/In board
	VBF_SAT_CLCH =		203, //!< CameraLink Ring board
	VBF_SAT_1394 =		204, //!< IEEE 1394 board
	VBF_SAT_LVDS =		205, //!< LVDS board
	VBF_SAT_CLINr1 =	206, //!< CameraLink ExtTrigger
	VBF_SAT_CHCLO =		207, //!< CameraLink Chain In, CameraLink Out board
	VBF_SAT_CHININ =	208, //!< CameraLink Chain 2*In board
	VBF_SAT_ANYBUS =	209, //!< AnyBus board

	VBF_SAT_GBIT =		300, //!< GBit Ethernet board
	VBF_SAT_CAN =		301, //!< CAN-Bus board
	VBF_SAT_LWL =		302, //!< LWL board
	VBF_SAT_ATA =		303, //!< ATA storage board
	VBF_SAT_DIO =		304, //!< Digital IO board
	VBF_SAT_Q7 =		305, //!< Q7 Module

	VBF_FLASHBOX =		400  //!< FlashBox
};

/*****************************************************************************/
/*! \brief Hardware info

	The VIB library uses a global variable #VIB_HWInfo to indicate the
	VisionBox type and to store information about the connected interface cards.
******************************************************************************/
typedef struct sHWINFO
{
	char Type;					//!< VisionBox type (see #eVIB_TYPE)
	unsigned short CPU;			//!< CPU type
	unsigned char CPUID;		//!< ID of the CPU within the VisionBox (starting with 0)
	unsigned char CPUCount;		//!< Total number of CPUs available
	unsigned short Freq;		//!< DSP core frequency in MHz
	unsigned char FPGA_Type[2];	//!< Type of main FPGAs (see #eFPGA_TYPE)
	int FPGA_Version[2];		//!< Main FPGAs firmware version numbers (e.g. 116 => version 1.16)
	int AddOn[4];				//!< Add-on modules FPGA firmware types (see #eVBF_DESIGN)
	int AddOnVersion[4];		//!< Add-on modules FPGA firmware version numbers (e.g. 304 => version 3.04)
	unsigned int FlashVendor;	//!< Type of internal flash memory
#ifdef _TMS320C6600
	unsigned long long SDRAM_Size;	//!< Total size of SDRAM in bytes
#else
	unsigned int SDRAM_Size;	//!< Total size of SDRAM in bytes
#endif
	unsigned int Bootloader;	//!< Bootloader's version number (e.g. 103 => version 1.03)
} HWINFO;

extern near HWINFO VIB_HWInfo;		//!< Information about VisionBox type and connected interface cards
#define VIB_Freq VIB_HWInfo.Freq	//!< Frequency of DSP (MHz)
#define VIB_Type VIB_HWInfo.Type	//!< Type of VisionBox
#define VIB_CPUID VIB_HWInfo.CPUID	//!< CPU ID

extern far char VIB_HardwareConfigFile[];
extern far char *VIB_LibName;
extern far char *VIB_LibVersion;
extern far char *VIB_LibDate;

/*****************************************************************************/
//! RS232 device
/*****************************************************************************/
typedef struct sVIB_RS232
{
	unsigned char dummy[552];	// Workaround for external msTimeOut access
	unsigned int   msTimeout;
	void		  *pDeviceData;
} VIB_RS232;

extern far VIB_RS232 *pVIB_Rs232[VIB_RS232MAXPORTS]; //!< RS232 Configuration structures

/*****************************************************************************/
//! \addtogroup Display
/*****************************************************************************/
//! \{

//! Video display modes
enum eDVI2_DISPLAY_MODE
{
	res640x480,   //!< (Dual) DVI  640 x  480
	res800x600,   //!< (Dual) DVI  800 x  600
	res1024x768,  //!< (Dual) DVI 1024 x  768
	res1280x1024, //!< (Dual) DVI 1280 x 1024
	res1600x1200, //!< (Dual) DVI 1600 x 1200
	vga800x600,   //!< VGA  800 x  600
	vga1024x768,  //!< VGA 1024 x  768
	vga1280x1024, //!< VGA 1280 x 1024
	vga1600x1200, //!< VGA 1600 x 1200
	sdvi800x600,  //!< Single DVI  800 x  600
	sdvi1024x768, //!< Single DVI 1024 x  768
	sdvi1280x1024,//!< Single DVI 1280 x 1024
	sdvi1600x1200 //!< Single DVI 1600 x 1200
};

//! DVI device ID
enum eDVI2_DEVICE
{
	DVI2_DEVICE_A = 0x1,	//!< 1st display device
	DVI2_DEVICE_B = 0x2		//!< 2nd display device
};

//! Device types
enum eDISPLAY_DEVICE
{
	DISPLAYDEVICE_DVI = 0,	//!< DVI/Analog board
	DISPLAYDEVICE_TCP = 1	//!< Ethernet board
};

//! Internal display device structure
typedef struct sVIB_DISPLAYDEVICE
{
	unsigned short width;	//!< Display width
	unsigned short height;	//!< Display height
	unsigned char DeviceType;	//!< Device type
	int (*Display) (unsigned int SourceR, unsigned int SourceG, unsigned int SourceB,
	                unsigned int StartX, unsigned int StartY,
	                unsigned int Width, unsigned int Height, int lineOffset,
	                struct sVIB_DISPLAYDEVICE *pDisplayDevice);	//!< Pointer to display function
	void *pDeviceData;		//!< Internal device data
} VIB_DISPLAYDEVICE;
//! \}

/*****************************************************************************/
//! \addtogroup TRIGGER_UNIT
/*****************************************************************************/
//! \{

//! Handle for using a Trigger Unit
typedef struct sTRIGGER_UNIT *TRIGGER_UNIT;
//! \}

/*****************************************************************************/
//! \addtogroup CL
/*****************************************************************************/
//! \{

//! @brief The Camera Link data format is used with the input and output functions.
//!
//! It defines how the Camera Link data bits are arranged in DSP memory.
enum eCL_FORMAT
{
	CL_FORMAT8 =		1,		//!< 8-bit words on port A (Flags #CL_FLAG_PORT_B or
								//! #CL_FLAG_PORT_C can be used to alter the port
								//! for the input functions)
	CL_FORMAT12 =		0x81,	//!< Pack two 12-bit pixels into two 16-bit words
								//! (only available for input functions)
	CL_FORMAT16 =		2,		//!< 16-bit words (Port A/B)
	CL_FORMAT24 =		3,		//!< 24-bit words (Port A/B/C)
	CL_FORMAT24X4 =		0x80,	//!< Sort four 24-bit RGB pixels into RRRRGGGGBBBB
								//! (only available for input functions)
	CL_FORMAT565 =		0x82,	//!< 16-bit words using 5 bits of port A,
								//! 6 bits of Port B and 5 bits of port C
	CL_FORMATRAW =		0,		//!< 32-bit words: Data bits 0..23, spare signal bit 24
								//! (only available for input functions)
	CL_FORMAT24OF32 =	0
};
//! \}

/*****************************************************************************/
//! \addtogroup CLIN
/*****************************************************************************/
//! \{

//! Flags for a Camera Link Receiver (used with VIB_ClInSetupReceiver())
enum eCL_RECEIVER_FLAG
{
	CL_AREACAM =		0,		//!< The camera is an area scan camera
	CL_LINECAM =		4,		//!< The camera is a line scan camera (the FVAL signal is ignored)
	CL_DVAL_NOTACTIVE = 8,		//!< If set, the Camera Link DVAL signal is ignored
								//! (depends on the camera)
	CL_GRAY10 =			0x20,	//!< Use the 8 MSBs of a 10-bit camera (used together with #CL_FORMAT8)
	CL_GRAY12 =			0x40,	//!< Use the 8 MSBs of a 12-bit camera (used together with #CL_FORMAT8)
	CL_PORT_A_C_SWAP =	0x200,	//!< Swap Camera Link ports A and C to change the order of the color
								//!< components (RGB => BGR; Used together with #CL_FORMAT24,
								//!< #CL_FORMAT24X4 or #CL_FORMAT565)
	CL_KEEP_FPGA =		0x20000	//!< Don't reconfigure the Camera Link FPGA
								//! (use it only if the FPGA is already configured)
};

//! Flags for a Camera Link Channel (used with VIB_ClInOpenChannel()).
enum eCL_CHANNEL_FLAG
{
	CL_FLAG_PLANE24 	= 1,	//!< Write 24-bit RGB values into separate planes for each line
								//!  (used together with #CL_FORMAT24X4)
	CL_FLAG_PORT_B		= 0x10,	//!< Get data on Port B (used together with #CL_FORMAT8)
	CL_FLAG_PORT_C		= 0x20,	//!< Get data on Port C (used together with #CL_FORMAT8)
	CL_FLAG_SINGLE_FRAME= 0x100	//!< Stop acquisition after one frame when using VIB_ClInStartChannelLoop()
};

//! Camera Link Receiver structure
typedef struct sCL_IN_RECEIVER
{
	unsigned int BaseAddress;
	unsigned int ReturnAddress;
	unsigned int Format;
	unsigned char AddressShift;
	unsigned char Connector;
	unsigned char Version;
	unsigned char ComPort;
	TRIGGER_UNIT TriggerUnit;
} CL_IN_RECEIVER;

//! Camera Link Channel structure
typedef struct sCL_IN_CHANNEL
{
	CL_IN_RECEIVER *pClInReceiver;
	unsigned short Width;
	unsigned short Height;
	unsigned short BufferLines;
	unsigned short PacketSize;
	unsigned int PacketCount;
	unsigned char PixelSize;
	unsigned char ChannelId;
	char DmaChannel;
	unsigned int Flags;
	void *hEdmaLoop;
	void *hEdmaPacket;
	void *hEdmaR;
	void *hEdmaG;
	void *hEdmaB;
	unsigned int IntThrottleRate;
	unsigned int LinesPerInt;
	void *hEdmaInt[2];
	void *hEdmaGetLine[2];
	void (*INT_HANDLER)(int Channel);
} CL_IN_CHANNEL;
//! \}

/*****************************************************************************/
//! \addtogroup CLIN_CC
/*****************************************************************************/
//! \{

//! Camera Link Input CC signal states
enum eCLIN_CC
{
	CLIN_CC_LOW =		0, //!< Low signal
	CLIN_CC_HIGH =		2, //!< High signal
	CLIN_CC_GENERATOR =	3, //!< Generator signal source
	CLIN_CC_FVAL =		4, //!< FVAL (frame valid) signal source
	CLIN_CC_LVAL =		5  //!< LVAL (line valid) signal source
};

//! Camera Link Input CC signal structure
typedef union uCL_IN_CC
{
	unsigned short Register;
	struct
	{
		unsigned int CC1 :4;
		unsigned int CC2 :4;
		unsigned int CC3 :4;
		unsigned int CC4 :4;
	} Bits;
} CL_IN_CC;
//! \}

/*****************************************************************************/
//! \addtogroup CLIN_OLD
/*****************************************************************************/
//! \{

//! Camera Link Input Device structure
typedef struct sCL_IN_DEVICE
{
	CL_IN_RECEIVER Receiver;	//!< Camera Link Receiver
	CL_IN_CHANNEL  Channel;		//!< Camera Link Channel
} CL_IN_DEVICE;
//! \}

/*****************************************************************************/
//! \addtogroup CLOUT
/*****************************************************************************/
//! \{

//! Camera Link Output Device structure
typedef struct sCL_OUT_DEVICE
{
	unsigned int BaseAddress;
	unsigned char ReturnAddressPos;
	char DmaChannel;
	unsigned short width;
	unsigned short height;
	unsigned char  dataFormat;
	unsigned short BetweenSizeX;
	unsigned short BetweenSizeY;
	unsigned short OffsetX;
	unsigned char  LValMode;
	unsigned char  Frequency;
	unsigned short PacketSize;
	unsigned int PacketCount;
	unsigned short TriggerMode;
	unsigned short TriggerEdge;
	void *pSunWorkQueue;
} CL_OUT_DEVICE;
//! \}

/*****************************************************************************/
//! \addtogroup ATAPI
/*****************************************************************************/
//! \{

//! File system constants
enum eATAPI_CONST
{
	MAX_UDMA_MODE = 4,		//!< UDMA-66
	FAT_BYTSPERSEC = 512,	//!< Bytes per sector
	VIB_MAXDRIVES = 5		//!< Maximum number of supported storage drives
};

//! FAT Device IDs
enum eFAT_DEVICES
{
	DEVICE_FLASH = 0x0001, //!< Internal flash
	DEVICE_CF =    0x0102, //!< Compact Flash Card
	DEVICE_HD =    0x0203, //!< Internal HD
	DEVICE_SD =    0x0304, //!< SD Card
	DEVICE_RAM =   0x0405, //!< RAM disk
	DEVICE_HOST =  0xff06  //!< Host PC
};

//! File attributes
enum eFILE_ATTR
{
	ATTR_FILE =      0x00,
	ATTR_READ_ONLY = 0x01,
	ATTR_HIDDEN =    0x02,
	ATTR_SYSTEM =    0x04,
	ATTR_VOLUME_ID = 0x08,
	ATTR_DIRECTORY = 0x10,
	ATTR_ARCHIVE =   0x20,
	ATTR_DELETE =    0x40,
	ATTR_LONG_NAME = (ATTR_READ_ONLY | ATTR_HIDDEN | ATTR_SYSTEM | ATTR_VOLUME_ID),
	ATTR_LONG_NAME_MASK = (ATTR_READ_ONLY | ATTR_HIDDEN | ATTR_SYSTEM | ATTR_VOLUME_ID | ATTR_DIRECTORY | ATTR_ARCHIVE)
};

//! Flags for VIB_ReadDir()
enum eREAD_DIR_FLAG
{
	RD_FILESIZE = 0x01, //!< Show size of file
	RD_DIR =      0x02, //!< Show directories
	RD_DATE =     0x04, //!< Show date of file
	RD_TIME =     0x08, //!< Show time of file
	RD_ATTR =     0x10, //!< Show attributes of file
	RD_PATH =     0x20  //!< Use directory text string as path
};

//! File system error codes
enum eFAT_ERR_CODE
{
	E_NONE =             0, //!< No error
	E_NODEVICEPRESENT = -3, //!< No device present
	E_FILENOTFOUND =    -4, //!< File not found
	E_INVALIDFATTYPE =  -5, //!< Invalid FAT type
	E_FILEEXISTS =      -6, //!< File already exists
	E_DISKFULL =        -7, //!< Disk is full
	E_DIRECTORYFULL  =  -8, //!< Directory is full
	E_CONTROLLERFAULT = -9, //!< Device controller fault
	E_BADPATH =        -10, //!< Bad path
	E_READONLY =       -11, //!< File is read only or device is write-protected
	E_NOFILENAME =     -12, //!< No file name
	E_INVALIDFILE =    -13, //!< Invalid file
	E_ACCESSDENIED =   -14, //!< Access denied
	E_UNSUPPORTED =    -15, //!< Unsupported feature for this device
	E_OUTOFMEMORY =    -16, //!< Out of memory
	E_STRING2LONG =    -17, //!< String too long
	E_INVALIDCRC =     -18, //!< Invalid CRC checksum
	E_INVALIDSERIAL =  -19, //!< Invalid serial number
	E_TIMEOUT =        -20, //!< Time out
	E_UNINITIALIZED =  -21, //!< Device is uninitialized
	E_CANTWRITE	=	   -22, //!< Cannot write
	E_WRITEPROTECTIONISON = -23, //!< Write protection of the device was active. Disable this by calling VIB_FlashSetWriteProtection().
	E_UNKNOWN =        -99  //!< Unknown error
};

//! Special file sizes
enum eFILESIZE
{
	FAT_NOFILE    = -1,	//!< File not found
	FAT_DIRECTORY = -2	//!< File is directory
};

//! \brief This union is used to store the date of a file.
//!
//!	\par Example
//!	\code
//!FDate.Bits.Month = 12;
//!	\endcode
typedef union uFATDATE
{
	unsigned short Date;	//!< 16 bit packed value
	struct
	{
		unsigned Day:5;		//!< Day, range 1..31
		unsigned Month:4;	//!< Month, range 1..12
		unsigned Year:7;	//!< Year, range 0..127 (1980..2107)
	} Bits;					//!< Bit-field
} FATDATE;

/** \brief This union is used to store the time of a file.

\par Example
\code
FATTIME FTime;
FTime.Bits.Hour = 12;
\endcode*/
typedef union uFATTIME
{
	unsigned short Time;	//!< 16 bit packed value
	struct
	{
		unsigned Second:5;	//!< Seconds (multiple of 2), range 0..29
		unsigned Minute:6;	//!< Minutes, range 0..59
		unsigned Hour:5;	//!< Hours, range 0..23
	} Bits;					//!< Bit-field
} FATTIME;

//! This structure consists of information about a written file.
typedef struct sFILEINFO
{
	char Name[13];			//!< Short name
	char LName[256];		//!< Long name
	unsigned char Attr;		//!< File attributes
	int FileSize;			//!< File size in byte
	FATTIME CrtTime;		//!< Time file was created
	FATDATE CrtDate;		//!< Date file was created
	FATTIME WrtTime;		//!< Time file was written
	FATDATE WrtDate;		//!< Date file was written
} FILEINFO;

//! This structure consists of information about a drive.
typedef struct sDRIVEINFO
{
	char Name[13];			//!< Volume name
	unsigned int Size;		//!< Maximum of usable space in KB
	unsigned int Free;		//!< Free space in KB
	unsigned int FATType;	//!< Type of file system
} DRIVEINFO;

//=== FAT in Flash ===
extern far unsigned int FLA_FreeBlocks;		//!< Free blocks of 512 Bytes
extern far          int FLA_NoWriteCache;	//!< Disable Flash write cache
extern far          int FLA_ShowFileSize;	//!< Show file size in VIB_ReadDir()

//=== ATA globals ===
extern far          int FAT_LastError;
extern far          int FAT_ShowDir;		//!< Show sub-directories in VIB_ReadDir()

extern far int VIB_Drive[VIB_MAXDRIVES];	//!< Storage drive table
extern far int VIB_DriveCount; 				//!< Number of storage devices

//! \}

/*****************************************************************************/
//! \addtogroup DspIo
/*****************************************************************************/
//! \{

//! Structure for DSP I/O functions
typedef struct sIO_DEVICE
{
	unsigned int BaseAddress;
	int AddressShift;
	int (*OfferDevice)(struct sIO_DEVICE *pIoDevice, unsigned char nRemoteAddress, int DeviceType, const void *DeviceData, int DataSize);
	int (*GetDevice)(struct sIO_DEVICE *pIoDevice, unsigned char nRemoteAddress, int DeviceType, void *pDeviceData, int DataSize);
	int (*DMASend)(struct sIO_DEVICE *pIoDevice, const unsigned int *Buffer, unsigned int Header0, unsigned int Header1, unsigned int PacketCount, void (*DMAIntHandler)(), void *UserData);
} IO_DEVICE;
//! \}

/*****************************************************************************/
//! \addtogroup StrobeOut
/*****************************************************************************/
//! \{

typedef void *HSTROBE;	//!< Handle to strobe configuration

//! Modes for the Strobe Output (see VIB_StrobeSetTriggerMode())
typedef enum eSTROBE_MODE
{
	STROBE_MODE_SOFTWARE_EDGE = 0,	//!< Software triggered
	STROBE_MODE_SOFTWARE_COPY = 1,	//!< Manual ON/OFF by Software
	STROBE_MODE_HARDWARE_EDGE = 2,	//!< Hardware triggered by edge
	STROBE_MODE_HARDWARE_COPY = 3	//!< Hardware triggered by level
} STROBE_MODE;

//! Trigger sources for the Strobe Output (see VIB_StrobeSetTriggerSource())
typedef enum eSTROBE_SOURCE
{
	STROBE_SOURCE_DIG_IN0 =		0,	//!< Digital input 0 (add value to select different input)
	STROBE_SOURCE_RS422_IN0 =	16	//!< RS422 input 0 (add value to select different input)
} STROBE_SOURCE;
//! \}

/*****************************************************************************/
//! \addtogroup VPFE_VCS
/*****************************************************************************/
//! \{

//! Type/modes of the image sensor
enum eVPFE_SENSOR_TYPE
{
	VPFE_STYPE_UNKNOWN =	-1,	//!< Unknown type
	VPFE_STYPE_MT9V032C =	1,	//!< MT9V032 WVGA color
	VPFE_STYPE_MT9V032M =	2,	//!< MT9V032 WVGA mono
	VPFE_STYPE_MT9M021C =	3,	//!< MT9M021 SVGA color
	VPFE_STYPE_MT9M021M =	4,	//!< MT9M021 SVGA mono
	VPFE_STYPE_DLIS2K =		5,	//!< DLIS 2k line
	VPFE_STYPE_EV76C560M =	6, 	//!< EV76C560 SXGA mono
	VPFE_STYPE_EV76C560C =	7, 	//!< EV76C560 SXGA color
	VPFE_STYPE_EV76C570M =	8, 	//!< EV76C570 UXGA mono
	VPFE_STYPE_EV76C570C =	9 	//!< EV76C570 UXGA color
};

//! Output size (bits per pixel) after the PLD has shifted the data (use the upper MSB)
enum eVPFE_PLD_OUTPUT_WIDTH
{
	VPFE_PWIDTH_8BIT =			0,	//!< 8 Bit per pixel edge
	VPFE_PWIDTH_10BIT =			1,	//!< 10 Bit per pixel edge
	VPFE_PWIDTH_12BIT =			2,	//!< 12 Bit per pixel edge
	VPFE_PWIDTH_MAXSENSORWIDTH = 3	//!< The full pixel width is available (the bits are not shifted). Independently from the pixel size.
};

//! Error codes of the VPFE DSP Unit
enum eVPFE_ERR_CODE
{
	E_VPFE_TIMEOUT_GRAB = -2,						//!< A timeout has occurred before a new image was ready.
	E_VPFE_NORUNNING_GRAB = -3,						//!< ---
	E_VPFE_INAVLID_IMAGEBUFFER = -5,				//!< \p NULL pointer or not 256-byte aligned
	E_VPFE_INAVLID_IMAGESIZE = -6,					//!< New image size out of range
	E_VPFE_INAVLID_UNABLETOGRABWHILEGRABBING = -7,	//!< VIB_VPFE_GrabStart() called again before the last asynchronous grabbing was finished.
	E_VPFE_INAVLID_ARGUMENT = -8,					//!< Called a function with invalid arguments, e.g. \p NULL pointer or out of range?
	E_VPFE_INAVLID_INNONEHDRMODE = -10,				//!< The function is only valid in HDR mode.
	E_VPFE_UNABLE_TOTRIGGERINNONETRIGGERMODE = -9,	//!< You can't trigger an image manually without triggering enabled.
	E_VPFE_UNABLE_TOWRITEDATAOVERIIC = -20,			//!< Unable to write data over I²C
	E_VPFE_UNABLE_TOREADDATAOVERIIC = -21,			//!< Unable to read data over I²C
	E_VPFE_UNKNOWN = -100							//!< Unknown error
};
//! \}

/*****************************************************************************/
//! \addtogroup DIO
/*****************************************************************************/
//! \{

//! Input signals for VIB_SetGPIOMultiplexer() on the VisionCam XS.
enum eDIO_GPIOMuxInput_VCXS
{
	//! Accessible / settable with VIB_SetDIO(), bit 0
	DIO_GPIOMuxIn_VCXS_RegBit0 = 0x00,
	//! Accessible / settable with VIB_SetDIO(), bit 1
	DIO_GPIOMuxIn_VCXS_RegBit1 = 0x01,
	//! Accessible / settable with VIB_SetDIO(), bit 2
	DIO_GPIOMuxIn_VCXS_RegBit2 = 0x02,
	//! Accessible / settable with VIB_SetDIO(), bit 3
	DIO_GPIOMuxIn_VCXS_RegBit3 = 0x03,

	//! First optocoupler input. Readable with VIB_GetDIO() or VIB_OpenDin(), bit 0
	DIO_GPIOMuxIn_VCXS_OptoIn0 = 0x04,
	//! Second optocoupler input. Readable with VIB_GetDIO() or VIB_OpenDin(), bit 1
	DIO_GPIOMuxIn_VCXS_OptoIn1 = 0x05,

	//! DSP-GPIO Output, for future use
	DIO_GPIOMuxIn_VCXS_DSPGPIO0 = 0x06,
	//! VIB_VPFE_TriggerImage() function uses this output to trigger the next image
	DIO_GPIOMuxIn_VCXS_DSPGPIO1 = 0x07,

	//! Directly from the image sensor, indicates an open shutter
	DIO_GPIOMuxIn_VCXS_ImgSensorStrobeOut = 0x08,
	//! First output (TrigOut0) from the internal generator
	DIO_GPIOMuxIn_VCXS_TriggerGeneratorOut0 = 0x09,
	//! Second output (TrigOut0) from the internal generator
	DIO_GPIOMuxIn_VCXS_TriggerGeneratorOut1 = 0x0A,

	//! Output from the internal line shutter generator
	DIO_GPIOMuxIn_VCXS_LineShutterTriggerGeneratorOut = 0x0B
};

//! Output signals of the GPIO multiplexer on the VisionCam XS.
enum eDIO_GPIOMuxOutput_VCXS
{
	//! Routed to the 1. digital optocoupler output. <b>Default source:</b> #DIO_GPIOMuxIn_VCXS_RegBit0
	DIO_GPIOMuxOut_VCXS_OptoOut0 = 0x30000000,
	//! <b>Default source:</b> #DIO_GPIOMuxIn_VCXS_RegBit1
	DIO_GPIOMuxOut_VCXS_OptoOut1 = 0x31000000,
	//! <b>Default source:</b> #DIO_GPIOMuxIn_VCXS_RegBit2
	DIO_GPIOMuxOut_VCXS_OptoOut2 = 0x32000000,
	//! <b>Default source:</b> #DIO_GPIOMuxIn_VCXS_RegBit3
	DIO_GPIOMuxOut_VCXS_OptoOut3 = 0x33000000,

	//! Hardware trigger input of the image sensor. <b>Default source:</b> #DIO_GPIOMuxIn_VCXS_DSPGPIO1
	DIO_GPIOMuxOut_VCXS_ImgSensorTrigger = 0x34000000,
	//! First trigger input of the internal flash box. <b>Default source:</b> #DIO_GPIOMuxIn_VCXS_ImgSensorStrobeOut
	DIO_GPIOMuxOut_VCXS_StrobeTrigger = 0x35000000,
	//! First trigger input of the internal trigger generator. <b>Default source:</b> #DIO_GPIOMuxIn_VCXS_OptoIn0
	DIO_GPIOMuxOut_VCXS_TriggerGeneratorIn0 = 0x36000000,
	//! Second trigger input of the internal trigger generator. <b>Default source:</b> #DIO_GPIOMuxIn_VCXS_OptoIn0
	DIO_GPIOMuxOut_VCXS_TriggerGeneratorIn1 = 0x37000000,

	//! Input of the internal line shutter trigger generator. <b>Default source:</b> #DIO_GPIOMuxIn_VCXS_DSPGPIO1
	DIO_GPIOMuxOut_VCXS_LineShutterGenTriggerIn = 0x38000000,
	//! Input of the internal line image sync generator. <b>Default source:</b> #DIO_GPIOMuxIn_VCXS_DSPGPIO1
	DIO_GPIOMuxOut_VCXS_LineSyncGenVTriggerIn = 0x39000000
};
//! \}

/*****************************************************************************/
// Global variables
/*****************************************************************************/

extern near unsigned short VIB_InitWaitValue;
extern near int VIB_ForceMemCpy;

extern far VIB_DISPLAYDEVICE *VIB_ActiveDisplayDevice;

extern near int VIB_BiosIsRunning;	//!< Value indicating whether DSP/BIOS is present

/*****************************************************************************/
// Function prototypes
/*****************************************************************************/

#ifdef __cplusplus
extern "C" {
#endif

//! EDMA interrupt handler function
typedef void (*EdmaIntHandler)(int tccNum);
//! \addtogroup DspIo
//! \{

//! \brief DSP I/O Message dispatcher function
//! \param nRemoteAddress Id of sending DSP
//! \param pData Message data
//! \param Size Size of data
typedef void (*VIB_FxnMessageDispatcher)(unsigned char nRemoteAddress, void *pData, unsigned int Size);
//! \}
//! \addtogroup TRIGGER_UNIT
//! \{

//! \brief Trigger interrupt handler function
//! \param TrigIn Binary representation of the trigger inputs
//! \param TrigIntern Binary representation of the internal trigger signals
//! \param pUserData Custom data
typedef void (*VIB_FxnTriggerIntHandler)(unsigned int TrigIn, unsigned int TrigIntern, void *pUserData);
//! \}
//! \addtogroup DIO
//! \{

//! \brief Digital input interrupt handler function
//! \param DinValue Contains the current value of the digital inputs and the changed signals (see VIB_OpenDin())
typedef void (*VIB_FxnDioIntHandler)(unsigned int DinValue);
//! \}

//! Function returning \p void
typedef void (*VIB_Fxn)();

//! \name Real-Time Clock Functions
//! \{
int  VIB_GetTime(unsigned int *pHours, unsigned int *pMinutes, unsigned int *pSeconds);
int  VIB_SetTime(unsigned int Hours, unsigned int Minutes, unsigned int Seconds);
int  VIB_GetDate(unsigned int *pDay, unsigned int *pDate, unsigned int *pMonth, unsigned int *pYear);
int  VIB_SetDate(unsigned int Day, unsigned int Date, unsigned int Month, unsigned int Year);
int  VIB_RTC_Set2HostTime(void);
time_t VIB_HostTime(time_t *pTimer);
time_t VIB_time(time_t *pTimer);
int  VIB_GetSerialNumber(char *pSerialnumber);
void VIB_GetModuleSerialNumber(char *pSerialnumber);
//! \}

//! \name FPGA Trigger Unit
//! \{
TRIGGER_UNIT VIB_TriggerUnitInit(void *DeviceData);
int  VIB_TriggerUnitSet(TRIGGER_UNIT TriggerUnit, const char *Command);
int  VIB_TriggerUnitGet(TRIGGER_UNIT TriggerUnit, const char *Command, unsigned int *pData);
int  VIB_TriggerUnitSetIntHandler(TRIGGER_UNIT TriggerUnit, VIB_FxnTriggerIntHandler IntHandler, void *UserData);
//! \}

//! \name Camera Link Output Functions
//! \{
int  VIB_ClOutNewDevice(CL_OUT_DEVICE *ClOutDevice, int BoardID,
						unsigned short Width, unsigned short Height,
						unsigned short BetweenSizeX, unsigned short BetweenSizeY,
						unsigned short OffsetX, unsigned char DataFormat,
						unsigned char LValMode, unsigned char nPowerdown);
int  VIB_ClOutSetFrequency(CL_OUT_DEVICE *ClOutDevice, int MHz);
int  VIB_ClOutSetLoopMode(CL_OUT_DEVICE *pClOutDevice, int LoopData, int LoopSer, int LoopCC);
int  VIB_ClOutCloseDevice(CL_OUT_DEVICE *ClOutDevice);
int  VIB_ClOutSetTriggerMode(CL_OUT_DEVICE *ClOutDevice, int triggerMode, int triggerEdge);
int  VIB_ClOutSend(CL_OUT_DEVICE *ClOutDevice, const unsigned int *pData);
int  VIB_ClOutWait(CL_OUT_DEVICE *ClOutDevice, unsigned int usTimeout);
int  VIB_ClOutGetCC(CL_OUT_DEVICE *ClOutDevice);
//! \}

//! \name CameraLink Functions
//! \{
int  VIB_ClInCloseChannel(CL_IN_CHANNEL *ClInChannel);
int  VIB_ClInWaitChannel(CL_IN_CHANNEL *pClInChannel, int usTimeout);
unsigned int VIB_ClChannelGetLine(CL_IN_CHANNEL *ClChannel);
int  VIB_ClSetupChannelIntHandler(CL_IN_CHANNEL *ClInChannel, EdmaIntHandler IntHandler, int LinesPerInt);
int  VIB_ClInSetupReceiver(CL_IN_RECEIVER *ClInReceiver, int BoardID,
						  unsigned int Connector, unsigned int Format);
int  VIB_ClInOpenChannel(CL_IN_CHANNEL *pClInChannel, CL_IN_RECEIVER *pClInReceiver,
						unsigned int ChannelId, unsigned short PosX, unsigned short PosY,
						unsigned short Width, unsigned short Height, unsigned int Flags);
int  VIB_ClInStartChannel(CL_IN_CHANNEL *ClInChannel, unsigned int *pBuffer, unsigned int Flags);
int  VIB_ClInStartChannelLoop(CL_IN_CHANNEL *ClInChannel, unsigned int *pBuffer,
							 int BufferLines, unsigned int Flags);
int  VIB_ClInStopChannel(CL_IN_CHANNEL *ClInChannel);
int  VIB_ClInOfferDevice(IO_DEVICE *pIoDevice, int RemoteAddress, CL_IN_RECEIVER *pClInReceiver);
int  VIB_ClInGetDevice(IO_DEVICE *pIoDevice, int RemoteAddress, CL_IN_RECEIVER *pClInReceiver);
//! \}

//! \name Camera Link Trigger Unit
//! \{
int  VIB_ClInSetupNoneClReceiver(CL_IN_RECEIVER *pClReceiver);
int  VIB_ClInTriggerSet(CL_IN_RECEIVER *pClReceiver, const char *Command);
int  VIB_ClInTriggerGet(CL_IN_RECEIVER *pClReceiver, const char *Command, unsigned int *pData);
int  VIB_ClInTriggerSetIntHandler(CL_IN_RECEIVER *pClReceiver, VIB_FxnTriggerIntHandler IntHandler, void *UserData);
int  VIB_ClReceiverSetCC(CL_IN_RECEIVER *pClReceiver, CL_IN_CC ccValue);
int  VIB_ClReceiverConfGenerator(CL_IN_RECEIVER *pClReceiver, int TimeLow, int TimeHigh);
int  VIB_ClReceiverSetGenerator(CL_IN_RECEIVER *pClReceiver, int Value);
//! \}

int  VIB_ClInNewDevice(CL_IN_DEVICE *ClInDevice, int BoardID, unsigned char Port,
					   unsigned short Width, unsigned short Height, unsigned int DataFormat);
static inline int VIB_ClInCloseDevice(CL_IN_DEVICE *pClInDevice)
	{ return VIB_ClInCloseChannel(&(pClInDevice)->Channel); }
static inline int VIB_ClInStartGrab(CL_IN_DEVICE *pClInDevice, unsigned int *pData)
	{ return VIB_ClInStartChannel(&(pClInDevice)->Channel, pData, 0); }
static inline int VIB_ClInWait(CL_IN_DEVICE *pClInDevice, unsigned int usTimeout)
	{ return VIB_ClInWaitChannel(&(pClInDevice)->Channel, usTimeout); }
static inline int VIB_ClInStop(CL_IN_DEVICE *pClInDevice)
	{ return VIB_ClInStopChannel(&(pClInDevice)->Channel); }
static inline int VIB_ClInStartGrabLoop(CL_IN_DEVICE *pClInDevice,
	unsigned int *pBuffer, int BufferLines)
	{ return VIB_ClInStartChannelLoop(&(pClInDevice)->Channel, pBuffer, BufferLines, 0); }
static inline int VIB_ClSetupIntHandler(CL_IN_DEVICE *pClInDevice,
	EdmaIntHandler IntHandler, int LinesPerInt)
	{ return VIB_ClSetupChannelIntHandler(&(pClInDevice)->Channel, IntHandler, LinesPerInt); }
static inline unsigned int VIB_ClInGetLine(CL_IN_DEVICE *pClInDevice)
	{ return VIB_ClChannelGetLine(&pClInDevice->Channel); }
static inline void VIB_ClInSetCC(CL_IN_DEVICE *pClInDevice, CL_IN_CC ccValue)
	{ VIB_ClReceiverSetCC(&pClInDevice->Receiver, ccValue); }
static inline void VIB_ClInConfGenerator(CL_IN_DEVICE *pClInDevice, int TimeLow, int TimeHigh)
	{ VIB_ClReceiverConfGenerator(&pClInDevice->Receiver, TimeLow, TimeHigh); }

//! \name Firmware Update Functions
//! \{
int  VIB_FirmwareUpdateFileVersion(int Device, const char *FileName, char **ppDescription, int *pBootloader, int pFPGAVersion[2], int pAddOnVersion[4], int *pGBitVersion);
int  VIB_FirmwareUpdateVersion(const unsigned char *pVbuBuffer, int VbuSize, char **ppDescription, int *pBootloader, int pFPGAVersion[2], int pAddOnVersion[4], int *pGBitVersion);
int  VIB_FirmwareUpdate(const unsigned char *pVbuBuffer, int VbuSize, FILE *pLog, int Flags);
int  VIB_FirmwareUpdateFile(int Device, const char *FileName, FILE *pLog, int Flags);
//! \}

//! \name Timer Functions
//! \{
int  VIB_TimerFreq(void);
unsigned int VIB_TimerCount(void);
unsigned int VIB_TimerGetTime(unsigned int t0);
unsigned int VIB_Clock(void);
int  VIB_TimerHWIOpen(unsigned int Timer, unsigned int IntNumber, void (*TimerISR)());
int  VIB_TimerHWIClose(unsigned int Timer);
int  VIB_TimerHWIStart(unsigned int Timer, unsigned int TimePeriod, unsigned int SingleShot);
int  VIB_TimerHWIStop(unsigned int Timer);
void VIB_Wait (unsigned int Timer, unsigned int ms);
void VIB_Wait2(unsigned int Timer, unsigned int us);
void VIB_WaitBios(unsigned int ms);
//! \}

//! \name DMA Functions
//! \{
void VIB_DMAWait(int Channel);
void VIB_DMAWaitBios(int Channel);
int  VIB_DMACopy(int Channel, int Mode, const void *pSrc, void *pDst, unsigned int ByteCount);
int  VIB_DMACopy2D(int Channel, int Mode, const void *pSrc, void *pDst, int SrcIs2D, int DstIs2D,
				   unsigned short LineLen, unsigned short LineCnt, unsigned short LinePitch);
int  VIB_MemCopy(const void *pSrc, void *pDst, unsigned int ByteCount);
int  VIB_MemCopy2D(const void *pSrc, void *pDst, unsigned int LineLen,
				   unsigned int LineCnt, unsigned int SrcLinePitch, unsigned int DstLinePitch);
//! \}

int  VIB_WaitForKey(FILE *fKB);

//! \cond SHOWALL
int  VIB_SunReadRegister(unsigned int Header0, unsigned int DataOut, unsigned int *pDataIn, unsigned int msTimeout);
void VIB_SetReg(unsigned int Header0, unsigned int Value);
//! \endcond

int  VIB_WatchdogSetup(int Enable, int msTimeout);
int  VIB_WatchdogService(void);

//! \name Digital I/O Functions
//! \{
void VIB_SetLED(unsigned int Val);
int  VIB_GetDIO(void);
void VIB_SetDIO(unsigned int Out);
int  VIB_DebounceDin(unsigned int t_Low, unsigned int t_High);
int  VIB_OpenDin(unsigned int SensitivityMask, VIB_FxnDioIntHandler UserIntHandler);
void VIB_CloseDin(void);
int  VIB_SetRS422(unsigned int Out);
int  VIB_GetRS422(void);
int  VIB_SetRS485Termination(unsigned int boTurnTerminationOn);
int  VIB_SetRS485WriteEnable(unsigned int boWriteEnable);
int  VIB_SetGPIOMultiplexer(unsigned int eGPIOMuxOutput, unsigned int eGPIOMuxInput);
//! \}

//! \name RS232 Functions
//! \{
int  VIB_RS232Send(FILE *pFile, const void *pBuffer, int Count, int Timeout);
void VIB_RS232SendFlush(FILE *pFile);
int  VIB_RS232SendBusy(FILE *pFile);
int  VIB_RS232Recv(FILE *pFile, void *pBuffer, unsigned int Count, int Timeout);
void VIB_RS232RecvReset(FILE *pFile);
int  VIB_RS232RecvGetAvail(FILE *pFile, char *pLastChar);
//! \}

//! \name Display Functions
//! \{
int  VIB_Display(unsigned int SourceR, unsigned int SourceG, unsigned int SourceB,
				 unsigned int StartX, unsigned int StartY, unsigned int Width, unsigned int Height,
				 int LineOffset, VIB_DISPLAYDEVICE *pDisplayDevice);
int  VIB_InitDisplay(VIB_DISPLAYDEVICE *pDeviceA, VIB_DISPLAYDEVICE *pDeviceB);
int  VIB_DVI2Init(int ModePortA, int ModePortB,
				  VIB_DISPLAYDEVICE *pDeviceA, VIB_DISPLAYDEVICE *pDeviceB, int BoardID);
int  VIB_VGAInit(int ModePort, VIB_DISPLAYDEVICE *pDevice, int BoardID);
void VIB_DVI2CloseDisplay(VIB_DISPLAYDEVICE *pDeviceA, VIB_DISPLAYDEVICE *pDeviceB);
int  VIB_DVI2OfferDevice(IO_DEVICE *pIoDevice, int RemoteAddress, VIB_DISPLAYDEVICE *pDevice);
int  VIB_DVI2GetDevice(IO_DEVICE *pIoDevice, int RemoteAddress, VIB_DISPLAYDEVICE *pDevice);
int  VIB_DVI2AutoSwitchBank(VIB_DISPLAYDEVICE *pDisplayDevice, int Enable);
int  VIB_DVI2SwitchBank(VIB_DISPLAYDEVICE *pDisplayDevice);
//! \}

//! \name File System Functions
//! \{
void VIB_SetDefaultDrives(void);
//int VIB_InitFileDevices(void);
int VIB_AddDeviceCFC(void);
int VIB_AddDeviceDRV(void);
int VIB_CreateRamDisk(int Size);
void VIB_DeleteRamDisk(void);
int VIB_ReadFile(int Device, const char *pPath, void *pFileBuffer);
int VIB_WriteFile(int Device, const char *pPath, const void *pFileBuffer, unsigned int FileSize);
int VIB_DeleteFile(int Device, const char *pPath);
int VIB_RenameFile(int Device, const char *pOldName, const char *pNewName);
int VIB_Format(int Device);
int VIB_MkDir(int Device, const char *pDirname);
int VIB_ChDir(int Device, const char *pDirname);
int VIB_RmDir(int Device, const char *pDirname);
int VIB_FindFile(int Device, const char *pPath, unsigned int *pFirstCluster);
int VIB_ReadFileBlock(int Device, const char *pPath, void *pFileBuffer, unsigned int StartPos, unsigned int EndPos);
int VIB_WriteFileBlock(int Device, const char *pPath, const void *pFileBuffer, unsigned int StartPos, unsigned int EndPos);
int VIB_CreateFile(int Device, const char *pPath, unsigned int FileSize);
int VIB_AppendFile(int Device, const char *pPath, const void *pFileBuffer, unsigned int Size);
int VIB_AppendFileEmpty(int Device, const char *pPath, unsigned int Size);
int VIB_ReadDir(int Device, char *pDirText);
int VIB_ReadDirEx(int Device, char *pDirText, unsigned char Flags);
int VIB_ReadDirFilter(int Device, char *pDirText, const char *pFilter);
int VIB_ReadDirFilterEx(int Device, char *pDirText, unsigned char Flags, const char *pFilter);
int VIB_ReadDirFInfo(int Device, FILEINFO *pFInfoList, int NumList, const char *pFilter);
int VIB_GetFileInfo(int Device, const char *pFilename, FILEINFO *pFInfo);
int VIB_GetDriveInfo(int Device, DRIVEINFO *pDInfo);
int VIB_GetCurrentPath(int Device, char *pPath);
int VIB_GetFileDevice(const char *pPath, char *pDir, char *pFName);
int VIB_FileIsOpen(const char *pPath);
void VIB_SplitPath(const char *pPath, char *pDrive, char *pDir, char *pFName, char *pExt);
unsigned int VIB_GetFreeBlocks(int Device);
const char *VIB_GetDeviceName(int Device);
//! \}
static inline int VIB_FindFile2(int Device, const char *pPath)
	{ unsigned int Dummy; return VIB_FindFile(Device, pPath, &Dummy); }

//! \name Other Functions
//! \{
int  VIB_Init(void);
void VIB_InitDSP(void);
void VIB_SDRAMCache(int Enable);
void VIB_CacheUpdate(const void *pData, unsigned int ByteCount, int Mode);
int  VIB_IICReadTemp(unsigned short *pData);
//! \cond SHOWALL
void VIB_FlashCache(int Enable);
int  VIB_ReadSPD(unsigned int *pBanks, unsigned int *pBankSize);
//! \endcond
int  VIB_FlashSetWriteProtection(BOOL boWriteProtection);
char *VIB_SStrCpy(register char *dest, register const char *src, register size_t n);
int  VIB_StartApp(const void *pFilebuffer, int CoffFileSize);
int  VIB_RunApplication(int FileDevice, const char *pFileName);
void VIB_Reboot(void);
void VIB_SetKBLayout(int Language);
int  VIB_Control(const char *Command, void *pData);
//! \}

//! \name Information Functions
//! \{
int  VIB_GetLibVersion(int *pMajor, int *pMinor);
int  VIB_GetIOModuleType(unsigned int ModuleId);
int  VIB_GetIOModuleVersion(unsigned int ModuleId);
int  VIB_VbfGetInfo(const unsigned char *pVbfInfo, int *pType, int *pVersion);
int  VIB_VbfGetFileInfo(int Device, const char *FileName, int *pType, int *pVersion);
void VIB_PrintModuleVersions(void);
void VIB_GetModuleVersions(char *pText);
void VIB_ModuleVersions(VIB_Fxn pfFunc);
//! \}

//! \name Color Conversions Functions
//! \{
void VIB_YUV2RGB(const void *pYUV, void *pR, void *pG, void *pB,
				 int YUVWidth, int YUVHeight, int RGBWidth, int RGBHeight);
void VIB_YUV2Y(const void *pYUV, void *pY,
			   int YUVWidth, int YUVHeight, int YWidth, int YHeight);
void VIB_Bayer2RGB(const void *pSrc, void *pR, void *pG, void *pB,
				   int BWidth, int BHeight, int RGBWidth, int RGBHeight, int Flag);
void VIB_Bayer2RGBHQ(const void *pSrc, void *pR, void *pG, void *pB,
					 int BWidth, int BHeight, int RGBWidth, int RGBHeight, int Flag);
void VIB_Bayer2Y(const void *pSrc, void *pY,
				 int BWidth, int BHeight, int YWidth, int YHeight, int Flag);
void VIB_PackedRGB2RGB(const void *pRGB, void *pR, void *pG, void *pB,
					   int PWidth, int PHeight, int RGBWidth, int RGBHeight);
void VIB_RGB2RGBPacked(const void *pR, const void *pG, const void *pB,
					   void *pRGB, int Width, int Height, int RGBWidth, int RGBHeight);
void VIB_RGB2RGBPacked32(const void *pR, const void *pG, const void *pB,
						 void *pRGB, int Width, int Height, int RGBWidth, int RGBHeight);
void VIB_InitGammaTable(int Gamma, unsigned char *pGammaTable);
void VIB_InitGammaTable12(int Gamma, unsigned char *pGammaTable);
void VIB_Y162Y8(const void *pY16, void *pY8, int Width16, int Height16,
				int Width8, int Height8, const unsigned char *pGammaTable);
void VIB_Y122Y8(const void *pY16, void *pY8, int Width16, int Height16,
				int Width8, int Height8, const unsigned char *pGammaTable);
int VIB_Y162YN(unsigned char *pImage, unsigned int SrcByteWidthCount, 
			   unsigned int SrcByteHeightCount, unsigned char NumberOfBitsToShift);
int  VIB_FPGA_LUT8Setup(unsigned char LUT[256]);
int  VIB_FPGA_BayerSetGainR(unsigned char Gain);
int  VIB_FPGA_BayerSetGainG(unsigned char Gain);
int  VIB_FPGA_BayerSetGainB(unsigned char Gain);
//! \}

//! \name DSP to DSP Communication Functions
//! \{
int  VIB_DspIoInitStart(IO_DEVICE *pIoDevice, int InitMask, VIB_FxnMessageDispatcher MessageDispatcher);
int  VIB_DspIoInitState(IO_DEVICE *pIoDevice, int *pInitDoneMask);
int  VIB_DspIoInit(IO_DEVICE *pIoDevice, VIB_FxnMessageDispatcher MessageDispatcher);
void VIB_DspIoClose(IO_DEVICE *pIoDevice);
int  VIB_DspIoSetBlockSize(IO_DEVICE *pIoDevice, unsigned int BlockSize);
int  VIB_DspIoSendMessage(IO_DEVICE *pIoDevice, unsigned char DspID, const void *pData, unsigned int nBytes);
int  VIB_DspIoSend(IO_DEVICE *pIoDevice, unsigned char DspID, const void *pBuffer, unsigned int nBytes, int msTimeout);
int  VIB_DspIoSendWait(IO_DEVICE *pIoDevice, unsigned char DspID, int msTimeout);
int  VIB_DspIoSendAbort(IO_DEVICE *pIoDevice, unsigned char DspID);
int  VIB_DspIoRecv(IO_DEVICE *pIoDevice, unsigned char DspID, void *pBuffer, unsigned int nBytes, int msTimeout);
int  VIB_DspIoRecvWait(IO_DEVICE *pIoDevice, unsigned char DspID, int msTimeout);
int  VIB_DspIoRecvQuery(IO_DEVICE *pIoDevice, unsigned char DspID);
int  VIB_DspIoRecvAbort(IO_DEVICE *pIoDevice, unsigned char DspID);
int  VIB_DspIoSendApp(IO_DEVICE *pIoDevice, unsigned char DspID, const void *pAppData, unsigned int nBytes);
int  VIB_DspIoSlaveReboot(IO_DEVICE *pIoDevice, unsigned char DspID);
//! \}

//! \name Strobe Output Functions
//! \{
int VIB_StrobeGetLastErrorString(char *pText, unsigned int MaxStringSize);
HSTROBE VIB_StrobeInit(const char *OpenString, unsigned int ResetParameter);
int  VIB_StrobeClose(HSTROBE hStrobe, unsigned int SwitchOff);
int  VIB_StrobeSetLimits(HSTROBE hStrobe, int LimitCurrent, int MaxSupplyVoltage, int MaxLoadVoltage,
						 unsigned int MaxOnTime, unsigned int MinOffTime, unsigned int MaxCurrent);
int  VIB_StrobeGetLimits(HSTROBE hStrobe, unsigned int *pMaxOnTime, unsigned int *pMinOffTime, unsigned int *pMaxCurrent);
int  VIB_StrobeSetTriggerMode(HSTROBE hStrobe, STROBE_MODE TriggerMode);
int  VIB_StrobeGetTriggerMode(HSTROBE hStrobe);
int  VIB_StrobeSetTriggerSource(HSTROBE hStrobe, unsigned int TriggerSource, int InvertTrigger);
int  VIB_StrobeGetTriggerSource(HSTROBE hStrobe, STROBE_SOURCE *pTriggerSource, int *pInvertTrigger);
int  VIB_StrobeSetTriggerDelay(HSTROBE hStrobe, unsigned int TriggerDelay);
int  VIB_StrobeGetTriggerDelay(HSTROBE hStrobe);
int  VIB_StrobeSetOnTime(HSTROBE hStrobe, unsigned int OnTime);
int  VIB_StrobeGetOnTime(HSTROBE hStrobe);
int  VIB_StrobeSetCurrent(HSTROBE hStrobe, int Current, int Validate);
int  VIB_StrobeGetCurrent(HSTROBE hStrobe);
int  VIB_StrobeGetVoltage(HSTROBE hStrobe);
int  VIB_StrobeSwTrigger(HSTROBE hStrobe, int Value);
int  VIB_StrobeSaveDefaultConfiguration(HSTROBE hStrobe);
int  VIB_StrobeClearDefaultConfiguration(HSTROBE hStrobe);
int  VIB_StrobeGetTemp(HSTROBE hStrobe, unsigned short *pTemp);
int  VIB_StrobeGetVersion(HSTROBE hStrobe, unsigned int *pVersion);
int  VIB_StrobeGetHWVersion(HSTROBE hStrobe, unsigned int *pHWVersion);
int  VIB_StrobeUpdateFPGA(HSTROBE hStrobe, unsigned int Size, const unsigned char *pData);
//! \}

//! \name VPFE Functions [VisionCam (X)S]
//! \{
int VIB_VPFE_GrabStart(unsigned char *pBuffer, const unsigned int Flags);
int VIB_VPFE_GrabAbort(void);
int VIB_VPFE_GrabWait(int usTimeout);
int VIB_VPFE_TriggerImage(void);
int VIB_VPFE_SetAOI(const unsigned short OffsetX, const unsigned short OffsetY,
					const unsigned short Width, const unsigned short Height);
int VIB_VPFE_GetBlackLevel(char *pBlackLevel);
int VIB_VPFE_SetBlackLevel(const char BlackLevel);
int VIB_VPFE_SetPixelWidth(const BOOL Use16BitPixelWidth);

int VIB_VPFE_SetTriggerMode(const unsigned char OnOff);
int VIB_VPFE_GetSensorType(enum eVPFE_SENSOR_TYPE *pSensorType);
int VIB_VPFE_ResetSensor();

int VIB_VPFE_GetPLDPixelWidth(enum eVPFE_PLD_OUTPUT_WIDTH *pWidth);
int VIB_VPFE_SetPLDPixelWidth(enum eVPFE_PLD_OUTPUT_WIDTH NewWidth);
int VIB_VPFE_GetMemoryPixelWidth(BOOL *pUse16BitPixelWidth);
int VIB_VPFE_SetMemoryPixelWidth(BOOL Use16BitPixelWidth);
int VIB_VPFE_SetCompression(BOOL UseCompression);
int VIB_VPFE_GetCompression(BOOL *pUseCompression);
//! \}

//! Case insensitive string compare
#ifndef _C_OSIF_H	// osif.h (NDK) will already declare stricmp()
#define stricmp VIB_stricmp
int  stricmp(const char *s1, const char *s2);
#endif
int  strincmp(const char *s1, const char *s2, int count);

#ifdef __cplusplus
}
#endif

/*****************************************************************************/
// Inline functions
/*****************************************************************************/

static inline void *VIB_MemAlloc(unsigned int Size)
{
	void *Buffer;
#if defined(DM643x) || defined(GUI_LIB_64p)
	// for VPFE Unit
	Size = (Size+256-1) & ~(256-1);
	Buffer = memalign(256, Size);
#else
	Size = (Size+L2CACHELINESIZE-1) & ~(L2CACHELINESIZE-1);
	Buffer = memalign(L2CACHELINESIZE, Size);
#endif
	return Buffer;
}

static inline void VIB_MemFree(void *pBuffer)
	{ free(pBuffer); }

static inline int VIB_DMACopy32(int Channel, int Mode, unsigned int Src,
	unsigned int Dst, unsigned int Words)
	{ return VIB_DMACopy(Channel, Mode, (void *)Src, (void *)Dst, 4*Words); }

static inline int VIB_DMACopy8(int Channel, int Mode, unsigned int Src,
	unsigned int Dst, unsigned int Bytes)
	{ return VIB_DMACopy(Channel, Mode, (void *)Src, (void *)Dst, Bytes); }

static inline void *VIB_CoreLocal2GlobalAddr(void *localAddr, unsigned int core)
{
#ifdef _TMS320C6600
	if (((unsigned int)localAddr & 0xff800000) == 0x00800000)
		return (void *)((1 << 28) + (core << 24) + (unsigned int)localAddr);
	else
#endif
		return localAddr;
}

/*****************************************************************************/

#endif // _VISION_BOX_H_

/*****************************************************************************/
