#ifndef XP_ADC_H
#define XP_ADC_H

#include "x_basic_types.h"
#include "xp_cds_status_bus.h"
#include "xp_id_plate_info.h"

#define T_ADC_COUNT_ADR_PBUS		16	// count max elements in parallel bus 

#define T_ADC_SETUP_USE_ADR_PBUS 	0xffff // ïî óìîë÷àíèþ - íàñòðîéêà êàêèå ðåãèñòðû èñïîëüçîâàòü äëß PBUS, 0xffff - âñå âîçìîæíûå

/////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////
// read reg serial bus
/////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////
typedef struct {
//0
	UInt16 test;
//6
	union
	{
		UInt16 all;
		struct 
		{
	       UInt16 reserv :14;
		   UInt16 err_switch :1;
		   UInt16 enable_err_power :1;
		} bit;	
	} protect_error;
//7
	union
	{
		UInt16 all;
		struct 
		{
	       UInt16 reserv     :14;
		   UInt16 err_switch :1;
		   UInt16 err_power  :1;
		} bit;	
	} lock_status_error;

//14
	union
	{
		UInt16 all;
		struct 
		{
	       UInt16 revision   :5;
		   UInt16 version    :6;
		   T_plate_type plate_type :5;
		} bit;	
	} id_plate;

//15
	union
	{
		UInt16 all;
		struct 
		{
	       UInt16 reserv :14;
		   UInt16 err_switch :1;
		   UInt16 err_power :1;
		} bit;	
	} current_status_error;

} T_adc_read_sbus;

#define T_ADC_READ_SBUS_DEFAULTS  {0,0,0,0,0}
/////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////
//read reg serial bus
/////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////
typedef struct {
//0
	UInt16 test;

//6
	union
	{
		UInt16 all;
		struct 
		{
	       UInt16 reserv :14;
		   UInt16 err_switch :1;
		   UInt16 enable_err_power :1;
		} bit;	
	} protect_error;

//7
    UInt16 cmd_reset_error;

} T_adc_write_sbus;

#define T_ADC_WRITE_SBUS_DEFAULTS  {0,0,0}
/////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////
//read reg parallel bus
/////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////
typedef struct {
	UInt16 adc_value[T_ADC_COUNT_ADR_PBUS];
} T_adc_read_pbus;

#define T_ADC_READ_PBUS_DEFAULTS  {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}

/////////////////////////////////////////////////////////////
// Table for adr parallel bus 
/////////////////////////////////////////////////////////////
typedef struct {
		UInt16 adr_table[T_ADC_COUNT_ADR_PBUS];
} T_adc_adr_pbus;

#define T_ADC_ADR_PBUS_DEFAULTS  {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}


//////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////
//setup parallel bus
/////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////
typedef struct {
	UInt16	count_elements_pbus;
// use_or_not?
	union
	{
		UInt16 all;
		struct{
			UInt16 reg0		: 1;
			UInt16 reg1		: 1;
			UInt16 reg2		: 1;
			UInt16 reg3		: 1;

			UInt16 reg4		: 1;
			UInt16 reg5		: 1;
			UInt16 reg6		: 1;
			UInt16 reg7		: 1;

			UInt16 reg8		: 1;
			UInt16 reg9		: 1;
			UInt16 reg10	: 1;
			UInt16 reg11	: 1;

			UInt16 reg12	: 1;
			UInt16 reg13	: 1;
			UInt16 reg14	: 1;
			UInt16 reg15	: 1;			
		}bit;
	} use_reg_in_pbus;	

} T_adc_setup_pbus;

#define T_ADC_SETUP_PBUS_DEFAULTS  {T_ADC_COUNT_ADR_PBUS,T_ADC_SETUP_USE_ADR_PBUS}
//////////////////////////////////////////////////////////////




/////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////

typedef struct{
    T_adc_read_pbus			pbus;
	T_adc_read_sbus			sbus;
	Int16					type_cds_xilinx;
} T_adc_read;

typedef struct{
	T_adc_write_sbus		sbus;
} T_adc_write;

//////////////////////////////////////////////////////////////

typedef struct TS_adc{
	UInt16						plane_address; // 0 to 15
	UInt16						useit;
	Int16						type_cds_xilinx;
	T_adc_setup_pbus			setup_pbus;
	T_cds_status_serial_bus 	status_serial_bus;
	T_cds_status_parallel_bus 	status_parallel_bus;
	T_component_status			status;
    T_local_status              local_status;

    T_adc_write					write;
    T_adc_read					read;
	T_adc_adr_pbus				adr_pbus;

	UInt16						store_protect_error;

	UInt16						timer_wait_load;

    void (*init)();	    // Pointer to calculation function

    int (*read_all)();	    // Pointer to calculation function
    int (*write_all)();	    // Pointer to calculation function

    int (*read_sbus)();	        // Pointer to calculation function
    int (*write_sbus)();	    // Pointer to calculation function

    int (*read_pbus)();	        // Pointer to calculation function
    int (*write_pbus)();	    // Pointer to calculation function

    void (*reset_error)();	    // Pointer to calculation function
    void (*store_disable_error)();    // Pointer to calculation function
    void (*restore_enable_error)();	    // Pointer to calculation function

} T_adc;

typedef T_adc *T_adc_handle;

/*-----------------------------------------------------------------------------
Default initalizer for object.
-----------------------------------------------------------------------------*/                     
#define T_adc_DEFAULTS { 0,\
							0,\
							TYPE_CDS_XILINX_DEFAULTS,\
							T_ADC_SETUP_PBUS_DEFAULTS, \
							T_cds_status_serial_bus_DEFAULT,\
							T_cds_status_parallel_bus_DEFAULT,\
							component_NotReady,\
                            local_status_NotReady,\
							T_ADC_WRITE_SBUS_DEFAULTS,\
							{T_ADC_READ_PBUS_DEFAULTS,T_ADC_READ_SBUS_DEFAULTS,TYPE_CDS_XILINX_DEFAULTS},\
							T_ADC_ADR_PBUS_DEFAULTS,\
							0,\
							0, \
                       		(void (*)(Uint32))adc_init, \
                       		(int (*)(Uint32))adc_read_all, \
                       		(int (*)(Uint32))adc_write_all, \
                       		(int (*)(Uint32))adc_read_sbus, \
                       		(int (*)(Uint32))adc_write_sbus, \
                       		(int (*)(Uint32))adc_read_pbus_without_cycle, \
                       		(int (*)(Uint32))adc_write_pbus, \
                       		(void (*)(Uint32))adc_reset_error, \
                       		(void (*)(Uint32))adc_store_disable_error, \
                       		(void (*)(Uint32))adc_restore_enable_error \
							}

/////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////
void adc_init(T_adc_handle);

int adc_read_all(T_adc_handle);
int adc_write_all(T_adc_handle);

int adc_read_sbus(T_adc_handle);
int adc_write_sbus(T_adc_handle);

int adc_read_pbus(T_adc_handle);
int adc_write_pbus(T_adc_handle);
int adc_read_pbus_without_cycle(T_adc_handle);

void adc_reset_error(T_adc_handle);
void adc_store_disable_error(T_adc_handle);
void adc_restore_enable_error(T_adc_handle);

//------------------------------------------------------------------------------
// Return Type
//------------------------------------------------------------------------------

#endif