//*****************************************************************************
//
// "$RCSfile: AnyBus.h,v $"
//
// (c) 2004 - 2010 IMAGO Technologies GmbH
//*****************************************************************************
// Information by CVS or VSS:
//     Last check in on: $Date: 2010-02-19 10:01:33 $
//     by                $Author: schluckebier $
//     Revision        : $Revision: 1.13 $
//
// CVS: $Source: /Daten/Projekte/CVS/Lib/AB_Lib/src/AnyBus.h,v $
// VSS: $Archive:$
//
// $NoKeywords: $ expanding by VSS -->
//
// CVS: $Revision: 1.13 $
//
//*****************************************************************************
//! \file AnyBus.h
//! \brief AB Library Interface
//! \author (c) 2004 - 2010 IMAGO Technologies GmbH
//!
//! $Revision: 1.13 $
//! $Date: 2010-02-19 10:01:33 $
//*****************************************************************************

#define _AB_LIB_HID {"Anybus.h", __DATE__, __TIME__, _REVISION($Revision: 1.13 $)}

#include "vision_box.h"

#ifndef _ANYBUS_H_
#define _ANYBUS_H_

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

#define HPS_PACKET_LENGTH	144
#define HPS_WIDTH			128	//!< Dataset length
#define HPS_HEIGHT			640

// *** Addresses of module ***
#define AB_INPUT_DATA			0x0000
#define AB_OUTPUT_DATA			0x0200
#define AB_MAILBOX_IN			0x0400
#define AB_MAILBOX_OUT			0x0520
#define AB_APP_INDICATION		0x07FE
#define AB_ANYBUS_INDICATION	0x07FF

#define INDREG_MIN				0x80
#define INDREG_MOUT				0x40

#define ERR_MAILBOX_OUT			0x500	//!< Error code

#define AB_TIMEOUT_TOGGLE_MS	200
#define AB_TIMEOUT_RW_MS		500

// *** Node-Addresses ***
extern unsigned int AB_NODE_ADR_PMSRC;
extern unsigned int AB_NODE_ADR_EPTGT;
extern unsigned int AB_NODE_ADR_CTTGT;
extern unsigned int AB_NODE_ADR_SRCTGT;
extern unsigned int AB_MAINNODE_ADR;

//! AnyBus Commands
enum eABCMD
{
	AB_START_INIT	= 0x0001,	//!< Start Initialisation
	AB_ANYBUS_INIT	= 0x0002,	//!< AnyBus Initialisation
	AB_END_INIT		= 0x0003,	//!< End Initialisation
	AB_RESET		= 0x1,		//!< Reset
	AB_RUNNING		= 0x0		//!< Module is running
};

//! \addtogroup ABT AnybusTriggermodul Functions
//! \{

//! Trigger event: Input
enum eABTSEVT
{
	AB_IN0		= 0x01,	//!< Input channel 0
	AB_IN1		= 0x02,	//!< Input channel 1
	AB_IN2		= 0x04,	//!< Input channel 2
	AB_IN3		= 0x08,	//!< Input channel 3
	AB_INALL	= 0x0F,	//!< Input channel 0 - 3

	AB_SWTRIG0	= 0x10,	//!< Software trigger enable 0
	AB_SWTRIG1	= 0x20,	//!< Software trigger enable 1
	AB_SWTRIG2	= 0x40,	//!< Software trigger enable 2
	AB_SWTRIG3	= 0x80,	//!< Software trigger enable 3
	AB_SWTRIGALL= 0xF0,	//!< Software trigger enable 0 - 3

	AB_TRIGALL = 0xFF	//!< Trigger all
};

//! Trigger modul register addresses
enum eABTSREG
{
	AB_SOFT_TRIGGER		= 0x22000000,	//!< Software trigger

	AB_OUTA0_SRC		= 0x40000000,	//!< Actice Event connector A, channel 0
	AB_OUTA0_DELAY_A	= 0x41000000,	//!< Delay A [µs]
	AB_OUTA0_DURATION	= 0x42000000,	//!< Duration [µs]
	AB_OUTA0_DELAY_B	= 0x43000000,	//!< Delay B [µs]

	AB_OUTA1_SRC		= 0x44000000,	//!< Actice Event connector A, channel 1
	AB_OUTA1_DELAY_A	= 0x45000000,	//!< Delay A [µs]
	AB_OUTA1_DURATION	= 0x46000000,	//!< Duration [µs]
	AB_OUTA1_DELAY_B	= 0x47000000,	//!< Delay B [µs]

	AB_OUTA2_SRC		= 0x48000000,	//!< Actice Event connector A, channel 2
	AB_OUTA2_DELAY_A	= 0x49000000,	//!< Delay A [µs]
	AB_OUTA2_DURATION	= 0x4A000000,	//!< Duration [µs]
	AB_OUTA2_DELAY_B	= 0x4B000000,	//!< Delay B [µs]

	AB_OUTA3_SRC		= 0x4C000000,	//!< Actice Event connector A, channel 3
	AB_OUTA3_DELAY_A	= 0x4D000000,	//!< Delay A [µs]
	AB_OUTA3_DURATION	= 0x4E000000,	//!< Duration [µs]
	AB_OUTA3_DELAY_B	= 0x4F000000,	//!< Delay B [µs]

	AB_OUTB0_SRC		= 0x50000000,	//!< Actice Event connector B, channel 0
	AB_OUTB0_DELAY_A	= 0x51000000,	//!< Delay A [µs]
	AB_OUTB0_DURATION	= 0x52000000,	//!< Duration [µs]
	AB_OUTB0_DELAY_B	= 0x53000000,	//!< Delay B [µs]

	AB_OUTB1_SRC		= 0x50000000,	//!< Actice Event connector B, channel 1
	AB_OUTB1_DELAY_A	= 0x51000000,	//!< Delay A [µs]
	AB_OUTB1_DURATION	= 0x52000000,	//!< Duration [µs]
	AB_OUTB1_DELAY_B	= 0x53000000,	//!< Delay B [µs]

	AB_OUTB2_SRC		= 0x50000000,	//!< Actice Event connector B, channel 2
	AB_OUTB2_DELAY_A	= 0x51000000,	//!< Delay A [µs]
	AB_OUTB2_DURATION	= 0x52000000,	//!< Duration [µs]
	AB_OUTB2_DELAY_B	= 0x53000000,	//!< Delay B [µs]

	AB_OUTB3_SRC		= 0x50000000,	//!< Actice Event connector B, channel 3
	AB_OUTB3_DELAY_A	= 0x51000000,	//!< Delay A [µs]
	AB_OUTB3_DURATION	= 0x52000000,	//!< Duration [µs]
	AB_OUTB3_DELAY_B	= 0x53000000	//!< Delay B [µs]
};
//! \}

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

//! Anybus Mailheader
typedef struct sMAILHEADER
{
	unsigned short MessageID;
	unsigned short MessageInfo;
	unsigned short Command;
	unsigned short DataSize;
	unsigned short FrameCount;
	unsigned short FrameNumber;
	unsigned short OffsetHigh;
	unsigned short OffsetLow;
} MAILHEADER;

//! @brief Anybus info
//!
//! The #ANYBUSINFO structure stores the information of the connected AnyBus card.
typedef struct sANYBUSINFO
{
	char Vendor[40];	//!< Vendor name
	char BusType[40];	//!< Bus type (ProfiBus, Ethernet, etc)
	char ModuleType[40];//!< Type of AnyBus module
	char BL_Version[5];	//!< Bootloader version of AnyBus module
	char SW_Version[5];	//!< Firmware version of AnyBus module
	char SN[10];		//!< Serial number
} ANYBUSINFO;

//!The initialization configuration for an anybus device
typedef struct sANYBUSCONFIG
{
	unsigned short In_IO_length;	//!< Byte count for cyclic data, from module to field bus
	unsigned short In_DPRAM_length; //!< Length of the DPRAM
	unsigned short In_total_length; //!< Total length of the IN memory
	unsigned short Out_IO_length;	//!< Byte count for cyclic data, from field bus to the module
	unsigned short Out_DPRAM_length;//!< Length of the DPRAM
	unsigned short Out_total_length;//!< Total length of the OUT memory
} ANYBUSCONFIG;
/*************************************************************************/

extern int AB_Channel;

extern char AB_LibName[];
extern char AB_LibVersion[];
extern char AB_LibDate[];

/*************************************************************************\
* Prototypes                                                              *
\*************************************************************************/

#ifdef __cplusplus
extern "C" {
#endif
//!\name MailBox
//!\{
int  AB_WaitMailboxOut(void);
void AB_AckMailboxOut(void);
int  AB_WaitMailboxIn(void);
void AB_ToggleMailboxIn(void);
//!\}
//!\name Internal Read/Write functions (don't use it)
//!\{
int AB_Read(unsigned short Address);
int AB_ReadWord(unsigned short Address);
int AB_ReadBlock(unsigned short Address, short Length, unsigned char *pReadData);
int AB_Write(unsigned short Address, unsigned char Data);
int AB_WriteWord(unsigned short Address, unsigned short Data);
int AB_WriteBlock(unsigned short Address, short Length, const unsigned char *pWriteData);
//!\}
//!\name Configuration functions
//!\{
int AB_AnybusLibraryInit();
ANYBUSINFO AB_GetInfo(void);
void AB_PrintInfo(ANYBUSINFO AB_Info);
void AB_SetHWResetState(int Val);
void AB_HWReset(void);
int  AB_SWReset(void);
int  AB_HW_Chk(void);
int  AB_StartInit(void);
int  AB_AnybusInitEx(ANYBUSCONFIG AnyBusConfig);
int  AB_AnybusInit(void);
int  AB_EndInit(void);
int  AB_DpRamTest(void);
int  AB_ConfigureInterrupt(unsigned short PollAddress, void (*USER_INT_HANDLER)(unsigned int DinValue),
						   unsigned char SensitivityMask, unsigned char boPollingOn);
//!\}
//!\name User Read/Write functions
//!\{
int  AB_WriteData(const unsigned char *pData);
int  AB_ReadData(unsigned char *pData);
int  AB_ReadNByte(unsigned short Address, short Length, unsigned char *pReadData);
int  AB_WriteNByte(unsigned short Address, short Length, const unsigned char *pWriteData);
//!\}

//!\name Trigger card extension
//!\{
void ABTS_Reset(void);
int  ABTS_SetReg(unsigned int Header0, unsigned int Value);
int  ABTS_SunRead(unsigned int *pData, int WordCount, unsigned int msTimeout);
//!\}

#ifdef __cplusplus
}
#endif

#endif

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