#ifndef XP_CDS_RS_H
#define XP_CDS_RS_H

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


#define T_CDS_RS_COUNT_ADR_PBUS		8	// count max elements in parallel bus 
#define T_CDS_RS_SETUP_USE_ADR_PBUS 0xffff // ïî óìîë÷àíèþ - íàñòðîéêà êàêèå ðåãèñòðû èñïîëüçîâàòü äëß PBUS, 0xffff - âñå âîçìîæíûå

#define USED_RS_SENSORS	4	// Number of measuring channels

typedef union {
	unsigned int all;
	struct {
		unsigned int survey_time:8;	//Ïåðèîä îïðîñà ïî 10ìêñ (0==10, 1==20,...)
		unsigned int channel4_enable:1;
		unsigned int channel3_enable:1;
		unsigned int channel2_enable:1;
		unsigned int channel1_enable:1;
		unsigned int transmition_speed:3;
		unsigned int plane_is_master:1;
	}bit;
} T_cds_rs_config;

#define T_CDS_RS_CONFIG_DEFAULT {0xA131}

typedef struct {
	T_cds_rs_config config;
} T_cds_rs_write_sbus;

#define T_CDS_RS_WRITE_SBUS_DEFAULT \
			{T_CDS_RS_CONFIG_DEFAULT}

typedef struct {
	int direction;
	unsigned long turned_angle;
	unsigned long angle;
} Sensor;

#define SENSOR_DEFAULT {0, 0, 0}

typedef struct {
	Sensor sensor[USED_RS_SENSORS];
} T_cds_rs_read_pbus;

#define T_CDS_RS_READ_PBUS_DEFAULT { \
			{SENSOR_DEFAULT, SENSOR_DEFAULT, SENSOR_DEFAULT, SENSOR_DEFAULT}}

typedef struct {
	Sensor sensor[USED_RS_SENSORS];
	T_cds_rs_config config;
} T_cds_rs_read_sbus;

#define T_CDS_RS_READ_SBUS_DEFAULT { \
			{SENSOR_DEFAULT, SENSOR_DEFAULT, SENSOR_DEFAULT, SENSOR_DEFAULT}, \
			T_CDS_RS_CONFIG_DEFAULT}

typedef struct {
	T_cds_rs_write_sbus sbus;
} T_cds_rs_write;

#define T_CDS_RS_WRITE_DEFAULT \
			{T_CDS_RS_WRITE_SBUS_DEFAULT}

typedef struct {
	T_cds_rs_read_pbus 	pbus;
	T_cds_rs_read_sbus 	sbus;
	Int16				type_cds_xilinx;
} T_cds_rs_read;

#define T_CDS_RS_READ_DEFAULT \
			{T_CDS_RS_READ_PBUS_DEFAULT, T_CDS_RS_READ_SBUS_DEFAULT,TYPE_CDS_XILINX_DEFAULTS}

/////////////////////////////////////////////////////////////
// Table for adr parallel bus 
/////////////////////////////////////////////////////////////
typedef struct {
		UInt16 adr_table[T_CDS_RS_COUNT_ADR_PBUS];
} T_cds_rs_adr_pbus;

#define T_CDS_RS_ADR_PBUS_DEFAULTS  {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 res		: 8;
		}bit;
	} use_reg_in_pbus;	

} T_cds_rs_setup_pbus;

#define T_CDS_RS_SETUP_PBUS_DEFAULTS  {T_CDS_RS_COUNT_ADR_PBUS,T_CDS_RS_SETUP_USE_ADR_PBUS}
//////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////



typedef struct {
    UInt16 						plane_address;
    UInt16 						useit;
	Int16						type_cds_xilinx;
	T_cds_rs_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_cds_rs_write              write;
    T_cds_rs_read               read;
    T_cds_rs_adr_pbus			adr_pbus;

    void(*init)();
    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

} T_cds_rs;

typedef T_cds_rs *T_cds_rs_handle;

/*-----------------------------------------------------------------------------
Default initalizer for object.
-----------------------------------------------------------------------------*/                     
#define T_cds_rs_DEFAULTS { 0,\
							0,\
							TYPE_CDS_XILINX_DEFAULTS,\
							T_CDS_RS_SETUP_PBUS_DEFAULTS,\
							T_cds_status_serial_bus_DEFAULT,\
							T_cds_status_parallel_bus_DEFAULT,\
							component_NotReady,\
                            local_status_NotReady,\
							T_CDS_RS_WRITE_DEFAULT,\
							T_CDS_RS_READ_DEFAULT, \
							T_CDS_RS_ADR_PBUS_DEFAULTS, \
							(void (*)(Uint32))cds_rs_init, \
                       		(int (*)(Uint32))cds_rs_read_all, \
                       		(int (*)(Uint32))cds_rs_write_all, \
                       		(int (*)(Uint32))cds_rs_read_sbus, \
                       		(int (*)(Uint32))cds_rs_write_sbus, \
                       		(int (*)(Uint32))cds_rs_read_pbus, \
                       		(int (*)(Uint32))cds_rs_write_pbus, \
                       		}

void cds_rs_init(T_cds_rs *v);
int cds_rs_read_all(T_cds_rs *v);
int cds_rs_write_all(T_cds_rs *v);
int cds_rs_write_sbus(T_cds_rs *v);
int cds_rs_write_pbus(T_cds_rs *v);
int cds_rs_read_sbus(T_cds_rs *v);
int cds_rs_read_pbus(T_cds_rs *v);


#endif  //XP_CDS_RS_H